gudegg commented on issue #2578: The current RoundRobinLoadBalance 
implementation performance is not satisfactory
URL: 
https://github.com/apache/incubator-dubbo/issues/2578#issuecomment-425648009
 
 
   @kimmking The above implementation is also very bad when the greatest common 
divisor is 1. This performance will be much better. Help me to see if there are 
any problems. thx!
   ```java
   public class RoundRobinLoadBalance extends AbstractLoadBalance {
   
       public static final String NAME = "roundrobin";
   
       private final ConcurrentMap<String, AtomicPositiveInteger> sequences = 
new ConcurrentHashMap<String, AtomicPositiveInteger>();
   
       private final ConcurrentMap<String, AtomicPositiveInteger> indexSeqs = 
new ConcurrentHashMap<String, AtomicPositiveInteger>();
   
       @Override
       protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, 
Invocation invocation) {
           String key = invokers.get(0).getUrl().getServiceKey() + "." + 
invocation.getMethodName();
           int length = invokers.size(); // Number of invokers
           int maxWeight = 0; // The maximum weight
           int minWeight = Integer.MAX_VALUE; // The minimum weight
           final List<Invoker<T>> invokerToWeightList = new ArrayList<>();
           for (int i = 0; i < length; i++) {
               int weight = getWeight(invokers.get(i), invocation);
               maxWeight = Math.max(maxWeight, weight); // Choose the maximum 
weight
               minWeight = Math.min(minWeight, weight); // Choose the minimum 
weight
               if (weight > 0) {
                   invokerToWeightList.add(invokers.get(i));
               }
           }
           AtomicPositiveInteger sequence = sequences.get(key);
           if (sequence == null) {
               sequences.putIfAbsent(key, new AtomicPositiveInteger());
               sequence = sequences.get(key);
           }
           AtomicPositiveInteger indexSeq = indexSeqs.get(key);
           if (indexSeq == null) {
               indexSeqs.putIfAbsent(key, new AtomicPositiveInteger(-1));
               indexSeq = indexSeqs.get(key);
           }
   
           if (maxWeight > 0 && minWeight < maxWeight) {
               length = invokerToWeightList.size();
               while (true) {
                   int index = indexSeq.incrementAndGet() % length;
                   int currentWeight = sequence.get() % maxWeight;
                   if (index == 0) {
                       currentWeight = sequence.incrementAndGet() % maxWeight;
                   }
                   if (getWeight(invokerToWeightList.get(index), invocation) > 
currentWeight) {
                       return invokerToWeightList.get(index);
                   }
               }
           }
           // Round robin
           return invokers.get(sequence.incrementAndGet() % length);
       }
   }
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to