Skip to content

Commit c8a8946

Browse files
authored
Fix the issue of high CPU load caused by continuously creating new ConsistentHashSelector instances under high concurrency (#15497)
1 parent 3effe10 commit c8a8946

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public class ConsistentHashLoadBalance extends AbstractLoadBalance {
5353
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
5454
String methodName = RpcUtils.getMethodName(invocation);
5555
String key = invokers.get(0).getUrl().getServiceKey() + "." + methodName;
56-
// using the hashcode of list to compute the hash only pay attention to the elements in the list
5756
int invokersHashCode = invokers.hashCode();
58-
ConsistentHashSelector<T> selector = (ConsistentHashSelector<T>) selectors.get(key);
59-
if (selector == null || selector.identityHashCode != invokersHashCode) {
60-
selectors.put(key, new ConsistentHashSelector<T>(invokers, methodName, invokersHashCode));
61-
selector = (ConsistentHashSelector<T>) selectors.get(key);
62-
}
57+
// using the hashcode of invoker list to create consistent selector by atomic computation.
58+
ConsistentHashSelector<T> selector = (ConsistentHashSelector<T>) selectors.compute(
59+
key,
60+
(k, oldSelector) -> (oldSelector == null || oldSelector.identityHashCode != invokersHashCode)
61+
? new ConsistentHashSelector<>(invokers, methodName, invokersHashCode)
62+
: oldSelector);
6363
return selector.select(invocation);
6464
}
6565

0 commit comments

Comments
 (0)