From a4538f471996091af8565642f496984e4716430a Mon Sep 17 00:00:00 2001 From: godotg Date: Tue, 2 May 2023 07:44:28 +0800 Subject: [PATCH] ref[tree]: fast tree map --- .../ConsistentHashConsumerLoadBalancer.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/net/src/main/java/com/zfoo/net/consumer/balancer/ConsistentHashConsumerLoadBalancer.java b/net/src/main/java/com/zfoo/net/consumer/balancer/ConsistentHashConsumerLoadBalancer.java index 959f1c13..8947b665 100644 --- a/net/src/main/java/com/zfoo/net/consumer/balancer/ConsistentHashConsumerLoadBalancer.java +++ b/net/src/main/java/com/zfoo/net/consumer/balancer/ConsistentHashConsumerLoadBalancer.java @@ -80,18 +80,18 @@ public class ConsistentHashConsumerLoadBalancer extends AbstractConsumerLoadBala } var module = ProtocolManager.moduleByProtocolId(packet.protocolId()); - var treeMap = consistentHashMap.get(module.getId()); - if (treeMap == null) { - treeMap = updateModuleToConsistentHash(module); + var fastTreeMap = consistentHashMap.get(module.getId()); + if (fastTreeMap == null) { + fastTreeMap = updateModuleToConsistentHash(module); } - if (treeMap == null) { + if (fastTreeMap == null) { throw new RunException("ConsistentHashLoadBalancer [protocolId:{}][argument:{}], no service provides the [module:{}]", packet.protocolId(), argument, module); } - var index = treeMap.indexOfNearestCeilingKey(argument.hashCode()); - if (index < 0) { + var nearestIndex = fastTreeMap.indexOfNearestCeilingKey(argument.hashCode()); + if (nearestIndex < 0) { throw new RunException("no service provides the [module:{}]", packet.protocolId(), argument, module); } - var sid = treeMap.getByIndex(index); + var sid = fastTreeMap.getByIndex(nearestIndex); var session = NetContext.getSessionManager().getClientSession(sid); if (session == null) { throw new RunException("unknown no service provides the [module:{}]", packet.protocolId(), argument, module); @@ -117,6 +117,7 @@ public class ConsistentHashConsumerLoadBalancer extends AbstractConsumerLoadBala for (var entry : virtualNodeTreeMap.entrySet()) { virtualTreeMap.put(entry.getKey(), entry.getValue().getValue()); } + // 使用更高性能的tree map var fastTreeMap = new FastTreeMapIntLong(virtualTreeMap); consistentHashMap.set(module.getId(), fastTreeMap); return fastTreeMap;