/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.dubbo.rpc.cluster.loadbalance;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * random load balance.
 *
 */
public class RandomLoadBalance extends AbstractLoadBalance {

    public static final String NAME = "random";

    @Override
    protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
        int length = invokers.size(); // Number of invokers
        int totalWeight = 0; // The sum of weights
        boolean sameWeight = true; // Every invoker has the same weight?
        for (int i = 0; i < length; i++) {
            int weight = getWeight(invokers.get(i), invocation);
            totalWeight += weight; // Sum
            if (sameWeight && i > 0
                    && weight != getWeight(invokers.get(i - 1), invocation)) {
                sameWeight = false;
            }
        }
        if (totalWeight > 0 && !sameWeight) {
            // If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight.
            int offset = ThreadLocalRandom.current().nextInt(totalWeight);
            // Return a invoker based on the random value.
            for (int i = 0; i < length; i++) {
                offset -= getWeight(invokers.get(i), invocation);
                if (offset < 0) {
                    return invokers.get(i);
                }
            }
        }
        // If all invokers have the same weight value or totalWeight=0, return evenly.
        return invokers.get(ThreadLocalRandom.current().nextInt(length));
    }

}

@@ -1362,17 +1362,20 @@
     
-int total
+boolean same
 Weig
@@ -1383,32 +1383,51 @@
 t = 
-0
+true
 ; // 
-The sum of
+Every invoker has the same
  weight
-s
+?
 %0A   
@@ -1435,72 +1435,126 @@
     
-boolean sameWeight = true; // Every invoker has the same
+int firstWeight = getWeight(invokers.get(0), invocation);%0A        int totalWeight = firstWeight; // The sum of
  weight
-?
+s
 %0A   
@@ -1563,33 +1563,33 @@
    for (int i = 
-0
+1
 ; i %3C length; i+
@@ -1733,37 +1733,8 @@
 t &&
- i %3E 0%0A                    &&
  wei
@@ -1744,50 +1744,19 @@
  != 
-ge
+firs
 tWeight
-(invokers.get(i - 1), invocation)
 ) %7B%0A

Optimize the doSelect method of RandomLoadBalance to reduce the times of invoke of the getWeight method of the AbstractLoadBalance (#2597)