Skip to content

Commit adb5b0e

Browse files
gudeggkimmking
authored andcommitted
Optimize RoundRobinLoadBalance (#2586)
* Optimize RoundRobinLoadBalance * Optimize RoundRobinLoadBalance * Optimize RoundRobinLoadBalance * name improvement * format
1 parent d738318 commit adb5b0e

File tree

1 file changed

+23
-42
lines changed

1 file changed

+23
-42
lines changed

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,83 +21,64 @@
2121
import org.apache.dubbo.rpc.Invocation;
2222
import org.apache.dubbo.rpc.Invoker;
2323

24-
import java.util.LinkedHashMap;
24+
import java.util.ArrayList;
2525
import java.util.List;
26-
import java.util.Map;
2726
import java.util.concurrent.ConcurrentHashMap;
2827
import java.util.concurrent.ConcurrentMap;
2928

3029
/**
3130
* Round robin load balance.
32-
*
3331
*/
3432
public class RoundRobinLoadBalance extends AbstractLoadBalance {
3533

3634
public static final String NAME = "roundrobin";
3735

3836
private final ConcurrentMap<String, AtomicPositiveInteger> sequences = new ConcurrentHashMap<String, AtomicPositiveInteger>();
3937

38+
private final ConcurrentMap<String, AtomicPositiveInteger> indexSeqs = new ConcurrentHashMap<String, AtomicPositiveInteger>();
39+
4040
@Override
4141
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
4242
String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
4343
int length = invokers.size(); // Number of invokers
4444
int maxWeight = 0; // The maximum weight
4545
int minWeight = Integer.MAX_VALUE; // The minimum weight
46-
final LinkedHashMap<Invoker<T>, IntegerWrapper> invokerToWeightMap = new LinkedHashMap<Invoker<T>, IntegerWrapper>();
47-
int weightSum = 0;
46+
final List<Invoker<T>> nonZeroWeightedInvokers = new ArrayList<>();
4847
for (int i = 0; i < length; i++) {
4948
int weight = getWeight(invokers.get(i), invocation);
5049
maxWeight = Math.max(maxWeight, weight); // Choose the maximum weight
5150
minWeight = Math.min(minWeight, weight); // Choose the minimum weight
5251
if (weight > 0) {
53-
invokerToWeightMap.put(invokers.get(i), new IntegerWrapper(weight));
54-
weightSum += weight;
52+
nonZeroWeightedInvokers.add(invokers.get(i));
5553
}
5654
}
5755
AtomicPositiveInteger sequence = sequences.get(key);
5856
if (sequence == null) {
5957
sequences.putIfAbsent(key, new AtomicPositiveInteger());
6058
sequence = sequences.get(key);
6159
}
62-
int currentSequence = sequence.getAndIncrement();
60+
6361
if (maxWeight > 0 && minWeight < maxWeight) {
64-
int mod = currentSequence % weightSum;
65-
for (int i = 0; i < maxWeight; i++) {
66-
for (Map.Entry<Invoker<T>, IntegerWrapper> each : invokerToWeightMap.entrySet()) {
67-
final Invoker<T> k = each.getKey();
68-
final IntegerWrapper v = each.getValue();
69-
if (mod == 0 && v.getValue() > 0) {
70-
return k;
71-
}
72-
if (v.getValue() > 0) {
73-
v.decrement();
74-
mod--;
75-
}
62+
AtomicPositiveInteger indexSeq = indexSeqs.get(key);
63+
if (indexSeq == null) {
64+
indexSeqs.putIfAbsent(key, new AtomicPositiveInteger(-1));
65+
indexSeq = indexSeqs.get(key);
66+
}
67+
length = nonZeroWeightedInvokers.size();
68+
while (true) {
69+
int index = indexSeq.incrementAndGet() % length;
70+
int currentWeight;
71+
if (index == 0) {
72+
currentWeight = sequence.incrementAndGet() % maxWeight;
73+
} else {
74+
currentWeight = sequence.get() % maxWeight;
75+
}
76+
if (getWeight(nonZeroWeightedInvokers.get(index), invocation) > currentWeight) {
77+
return nonZeroWeightedInvokers.get(index);
7678
}
7779
}
7880
}
7981
// Round robin
80-
return invokers.get(currentSequence % length);
81-
}
82-
83-
private static final class IntegerWrapper {
84-
private int value;
85-
86-
public IntegerWrapper(int value) {
87-
this.value = value;
88-
}
89-
90-
public int getValue() {
91-
return value;
92-
}
93-
94-
public void setValue(int value) {
95-
this.value = value;
96-
}
97-
98-
public void decrement() {
99-
this.value--;
100-
}
82+
return invokers.get(sequence.getAndIncrement() % length);
10183
}
102-
10384
}

0 commit comments

Comments
 (0)