|
21 | 21 | import org.apache.dubbo.rpc.Invocation; |
22 | 22 | import org.apache.dubbo.rpc.Invoker; |
23 | 23 |
|
24 | | -import java.util.LinkedHashMap; |
| 24 | +import java.util.ArrayList; |
25 | 25 | import java.util.List; |
26 | | -import java.util.Map; |
27 | 26 | import java.util.concurrent.ConcurrentHashMap; |
28 | 27 | import java.util.concurrent.ConcurrentMap; |
29 | 28 |
|
30 | 29 | /** |
31 | 30 | * Round robin load balance. |
32 | | - * |
33 | 31 | */ |
34 | 32 | public class RoundRobinLoadBalance extends AbstractLoadBalance { |
35 | 33 |
|
36 | 34 | public static final String NAME = "roundrobin"; |
37 | 35 |
|
38 | 36 | private final ConcurrentMap<String, AtomicPositiveInteger> sequences = new ConcurrentHashMap<String, AtomicPositiveInteger>(); |
39 | 37 |
|
| 38 | + private final ConcurrentMap<String, AtomicPositiveInteger> indexSeqs = new ConcurrentHashMap<String, AtomicPositiveInteger>(); |
| 39 | + |
40 | 40 | @Override |
41 | 41 | protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) { |
42 | 42 | String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName(); |
43 | 43 | int length = invokers.size(); // Number of invokers |
44 | 44 | int maxWeight = 0; // The maximum weight |
45 | 45 | 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<>(); |
48 | 47 | for (int i = 0; i < length; i++) { |
49 | 48 | int weight = getWeight(invokers.get(i), invocation); |
50 | 49 | maxWeight = Math.max(maxWeight, weight); // Choose the maximum weight |
51 | 50 | minWeight = Math.min(minWeight, weight); // Choose the minimum weight |
52 | 51 | if (weight > 0) { |
53 | | - invokerToWeightMap.put(invokers.get(i), new IntegerWrapper(weight)); |
54 | | - weightSum += weight; |
| 52 | + nonZeroWeightedInvokers.add(invokers.get(i)); |
55 | 53 | } |
56 | 54 | } |
57 | 55 | AtomicPositiveInteger sequence = sequences.get(key); |
58 | 56 | if (sequence == null) { |
59 | 57 | sequences.putIfAbsent(key, new AtomicPositiveInteger()); |
60 | 58 | sequence = sequences.get(key); |
61 | 59 | } |
62 | | - int currentSequence = sequence.getAndIncrement(); |
| 60 | + |
63 | 61 | 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); |
76 | 78 | } |
77 | 79 | } |
78 | 80 | } |
79 | 81 | // 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); |
101 | 83 | } |
102 | | - |
103 | 84 | } |
0 commit comments