Skip to content

Commit

Permalink
fix the bug, consistenthash loadbalance always construct new Consiste…
Browse files Browse the repository at this point in the history
…ntHashSelector #5429
  • Loading branch information
CodingSinger committed Dec 9, 2019
1 parent 97e20f5 commit 68821a1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public class ConsistentHashLoadBalance extends AbstractLoadBalance {
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
String methodName = RpcUtils.getMethodName(invocation);
String key = invokers.get(0).getUrl().getServiceKey() + "." + methodName;
int identityHashCode = System.identityHashCode(invokers);
// using the hashcode of list to compute the hash only pay attention to the elements in the list
int identityHashCode = invokers.hashCode();
ConsistentHashSelector<T> selector = (ConsistentHashSelector<T>) selectors.get(key);
if (selector == null || selector.identityHashCode != identityHashCode) {
selectors.put(key, new ConsistentHashSelector<T>(invokers, methodName, identityHashCode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
*/
package org.apache.dubbo.rpc.cluster.loadbalance;

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

import org.apache.dubbo.rpc.cluster.LoadBalance;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -49,6 +52,14 @@ public void testConsistentHashLoadBalance() {
Assertions.assertEquals(1, hitedInvokers.size(), "the number of hitedInvoker should be 1");
Assertions.assertEquals(runs,
hitedInvokers.values().iterator().next().intValue(), "the number of hited count should be the number of runs");

LoadBalance lb = getLoadBalance(ConsistentHashLoadBalance.NAME);
URL url = invokers.get(0).getUrl();
Invoker hittedInvoker = hitedInvokers.keySet().iterator().next();
for (int i = 0; i < runs; i++) {
invokers = new ArrayList<>(invokers);
Assertions.assertEquals(hittedInvoker.hashCode(),lb.select(invokers, url, invocation).hashCode());
}
}

}

0 comments on commit 68821a1

Please sign in to comment.