Skip to content

Commit 07d3b30

Browse files
committed
1 parent 405003a commit 07d3b30

File tree

4 files changed

+89
-59
lines changed

4 files changed

+89
-59
lines changed

dubbo-cluster/src/main/java/com/alibaba/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalance.java

Lines changed: 77 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,70 +13,91 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.alibaba.dubbo.rpc.cluster.loadbalance;
17-
18-
import java.util.ArrayList;
16+
package com.alibaba.dubbo.rpc.cluster.loadbalance;
17+
18+
import java.util.LinkedHashMap;
1919
import java.util.List;
20+
import java.util.Map;
2021
import java.util.concurrent.ConcurrentHashMap;
2122
import java.util.concurrent.ConcurrentMap;
2223

2324
import com.alibaba.dubbo.common.URL;
2425
import com.alibaba.dubbo.common.utils.AtomicPositiveInteger;
2526
import com.alibaba.dubbo.rpc.Invocation;
2627
import com.alibaba.dubbo.rpc.Invoker;
27-
28-
/**
29-
* Round robin load balance.
30-
*
31-
* @author qian.lei
32-
* @author william.liangf
33-
*/
28+
29+
/**
30+
* Round robin load balance.
31+
*
32+
* @author qian.lei
33+
* @author william.liangf
34+
*/
3435
public class RoundRobinLoadBalance extends AbstractLoadBalance {
35-
36-
public static final String NAME = "roundrobin";
37-
38-
private final ConcurrentMap<String, AtomicPositiveInteger> sequences = new ConcurrentHashMap<String, AtomicPositiveInteger>();
3936

40-
private final ConcurrentMap<String, AtomicPositiveInteger> weightSequences = new ConcurrentHashMap<String, AtomicPositiveInteger>();
41-
42-
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
43-
String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
44-
int length = invokers.size(); // 总个数
45-
int maxWeight = 0; // 最大权重
46-
int minWeight = Integer.MAX_VALUE; // 最小权重
47-
for (int i = 0; i < length; i++) {
48-
int weight = getWeight(invokers.get(i), invocation);
49-
maxWeight = Math.max(maxWeight, weight); // 累计最大权重
50-
minWeight = Math.min(minWeight, weight); // 累计最小权重
51-
}
52-
if (maxWeight > 0 && minWeight < maxWeight) { // 权重不一样
53-
AtomicPositiveInteger weightSequence = weightSequences.get(key);
54-
if (weightSequence == null) {
55-
weightSequences.putIfAbsent(key, new AtomicPositiveInteger());
56-
weightSequence = weightSequences.get(key);
57-
}
58-
int currentWeight = weightSequence.getAndIncrement() % maxWeight;
59-
List<Invoker<T>> weightInvokers = new ArrayList<Invoker<T>>();
60-
for (Invoker<T> invoker : invokers) { // 筛选权重大于当前权重基数的Invoker
61-
if (getWeight(invoker, invocation) > currentWeight) {
62-
weightInvokers.add(invoker);
63-
}
64-
}
65-
int weightLength = weightInvokers.size();
66-
if (weightLength == 1) {
67-
return weightInvokers.get(0);
68-
} else if (weightLength > 1) {
69-
invokers = weightInvokers;
70-
length = invokers.size();
71-
}
72-
}
73-
AtomicPositiveInteger sequence = sequences.get(key);
74-
if (sequence == null) {
75-
sequences.putIfAbsent(key, new AtomicPositiveInteger());
76-
sequence = sequences.get(key);
77-
}
78-
// 取模轮循
79-
return invokers.get(sequence.getAndIncrement() % length);
80-
}
81-
37+
public static final String NAME = "roundrobin";
38+
39+
private final ConcurrentMap<String, AtomicPositiveInteger> sequences = new ConcurrentHashMap<String, AtomicPositiveInteger>();
40+
41+
private static final class IntegerWrapper {
42+
public IntegerWrapper(int value) {
43+
this.value = value;
44+
}
45+
46+
private int value;
47+
48+
public int getValue() {
49+
return value;
50+
}
51+
52+
public void setValue(int value) {
53+
this.value = value;
54+
}
55+
56+
public void decrement() {
57+
this.value--;
58+
}
59+
}
60+
61+
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
62+
String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
63+
int length = invokers.size(); // 总个数
64+
int maxWeight = 0; // 最大权重
65+
int minWeight = Integer.MAX_VALUE; // 最小权重
66+
final LinkedHashMap<Invoker<T>, IntegerWrapper> invokerToWeightMap = new LinkedHashMap<Invoker<T>, IntegerWrapper>();
67+
int weightSum = 0;
68+
for (int i = 0; i < length; i++) {
69+
int weight = getWeight(invokers.get(i), invocation);
70+
maxWeight = Math.max(maxWeight, weight); // 累计最大权重
71+
minWeight = Math.min(minWeight, weight); // 累计最小权重
72+
if (weight > 0) {
73+
invokerToWeightMap.put(invokers.get(i), new IntegerWrapper(weight));
74+
weightSum += weight;
75+
}
76+
}
77+
AtomicPositiveInteger sequence = sequences.get(key);
78+
if (sequence == null) {
79+
sequences.putIfAbsent(key, new AtomicPositiveInteger());
80+
sequence = sequences.get(key);
81+
}
82+
int currentSequence = sequence.getAndIncrement();
83+
if (maxWeight > 0 && minWeight < maxWeight) { // 权重不一样
84+
int mod = currentSequence % weightSum;
85+
for (int i = 0; i < maxWeight; i++) {
86+
for (Map.Entry<Invoker<T>, IntegerWrapper> each : invokerToWeightMap.entrySet()) {
87+
final Invoker<T> k = each.getKey();
88+
final IntegerWrapper v = each.getValue();
89+
if (mod == 0 && v.getValue() > 0) {
90+
return k;
91+
}
92+
if (v.getValue() > 0) {
93+
v.decrement();
94+
mod--;
95+
}
96+
}
97+
}
98+
}
99+
// 取模轮循
100+
return invokers.get(currentSequence % length);
101+
}
102+
82103
}

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ protected synchronized void doExport() {
207207
generic = Boolean.FALSE.toString();
208208
}
209209
if(local !=null){
210-
if(local=="true"){
210+
if("true".equals(local)){
211211
local=interfaceName+"Local";
212212
}
213213
Class<?> localClass;
@@ -221,7 +221,7 @@ protected synchronized void doExport() {
221221
}
222222
}
223223
if(stub !=null){
224-
if(stub=="true"){
224+
if("true".equals(stub)){
225225
stub=interfaceName+"Stub";
226226
}
227227
Class<?> stubClass;

dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/transport/AbstractEndpoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void reset(URL url) {
5454
+ url + ", cause: Channel closed. channel: " + getLocalAddress());
5555
}
5656
try {
57-
if (url.hasParameter(Constants.HEARTBEAT_KEY)) {
57+
if (url.hasParameter(Constants.TIMEOUT_KEY)) {
5858
int t = url.getParameter(Constants.TIMEOUT_KEY, 0);
5959
if (t > 0) {
6060
this.timeout = t;

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,5 +537,14 @@
537537
</roles>
538538
<timezone>+8</timezone>
539539
</developer>
540+
<developer>
541+
<name>WuXiaoFei(Teaey)</name>
542+
<id>xiaofei.wxf</id>
543+
<email>xiaofei.wxf (AT) alibaba-inc.com</email>
544+
<roles>
545+
<role>Developer</role>
546+
</roles>
547+
<timezone>+8</timezone>
548+
</developer>
540549
</developers>
541550
</project>

0 commit comments

Comments
 (0)