From b4705a125e91cfaa9774b4dc52620c70904ab7bd Mon Sep 17 00:00:00 2001 From: godotg Date: Mon, 1 May 2023 20:58:07 +0800 Subject: [PATCH] ref[tree]: fast tree map --- .../ConsistentHashConsumerLoadBalancer.java | 14 +++++++------- ...TreeMapIntLong.java => FastTreeMapIntLong.java} | 8 ++++---- ...ntLongTest.java => FastTreeMapIntLongTest.java} | 10 +++++----- 3 files changed, 16 insertions(+), 16 deletions(-) rename net/src/main/java/com/zfoo/net/util/{ReadOnlyTreeMapIntLong.java => FastTreeMapIntLong.java} (94%) rename net/src/test/java/com/zfoo/net/util/{ReadOnlyTreeMapIntLongTest.java => FastTreeMapIntLongTest.java} (84%) 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 d757cbfc..959f1c13 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 @@ -15,7 +15,7 @@ package com.zfoo.net.consumer.balancer; import com.zfoo.net.NetContext; import com.zfoo.net.session.Session; -import com.zfoo.net.util.ReadOnlyTreeMapIntLong; +import com.zfoo.net.util.FastTreeMapIntLong; import com.zfoo.protocol.IPacket; import com.zfoo.protocol.ProtocolManager; import com.zfoo.protocol.collection.CollectionUtils; @@ -42,7 +42,7 @@ public class ConsistentHashConsumerLoadBalancer extends AbstractConsumerLoadBala public static final ConsistentHashConsumerLoadBalancer INSTANCE = new ConsistentHashConsumerLoadBalancer(); private volatile int lastClientSessionChangeId = 0; - private static final AtomicReferenceArray consistentHashMap = new AtomicReferenceArray<>(ProtocolManager.MAX_MODULE_NUM); + private static final AtomicReferenceArray consistentHashMap = new AtomicReferenceArray<>(ProtocolManager.MAX_MODULE_NUM); private static final int VIRTUAL_NODE_NUMS = 200; private ConsistentHashConsumerLoadBalancer() { @@ -87,7 +87,7 @@ public class ConsistentHashConsumerLoadBalancer extends AbstractConsumerLoadBala if (treeMap == null) { throw new RunException("ConsistentHashLoadBalancer [protocolId:{}][argument:{}], no service provides the [module:{}]", packet.protocolId(), argument, module); } - var index = treeMap.indexOfCeilingKey(argument.hashCode()); + var index = treeMap.indexOfNearestCeilingKey(argument.hashCode()); if (index < 0) { throw new RunException("no service provides the [module:{}]", packet.protocolId(), argument, module); } @@ -101,7 +101,7 @@ public class ConsistentHashConsumerLoadBalancer extends AbstractConsumerLoadBala @Nullable - private ReadOnlyTreeMapIntLong updateModuleToConsistentHash(ProtocolModule module) { + private FastTreeMapIntLong updateModuleToConsistentHash(ProtocolModule module) { var sessionStringList = getSessionsByModule(module).stream() .map(session -> new Pair<>(session.getConsumerAttribute().toString(), session.getSid())) .sorted((a, b) -> a.getKey().compareTo(b.getKey())) @@ -117,9 +117,9 @@ public class ConsistentHashConsumerLoadBalancer extends AbstractConsumerLoadBala for (var entry : virtualNodeTreeMap.entrySet()) { virtualTreeMap.put(entry.getKey(), entry.getValue().getValue()); } - var treeMap = new ReadOnlyTreeMapIntLong(virtualTreeMap); - consistentHashMap.set(module.getId(), treeMap); - return treeMap; + var fastTreeMap = new FastTreeMapIntLong(virtualTreeMap); + consistentHashMap.set(module.getId(), fastTreeMap); + return fastTreeMap; } } diff --git a/net/src/main/java/com/zfoo/net/util/ReadOnlyTreeMapIntLong.java b/net/src/main/java/com/zfoo/net/util/FastTreeMapIntLong.java similarity index 94% rename from net/src/main/java/com/zfoo/net/util/ReadOnlyTreeMapIntLong.java rename to net/src/main/java/com/zfoo/net/util/FastTreeMapIntLong.java index 51a99011..fc7e3d66 100644 --- a/net/src/main/java/com/zfoo/net/util/ReadOnlyTreeMapIntLong.java +++ b/net/src/main/java/com/zfoo/net/util/FastTreeMapIntLong.java @@ -21,12 +21,12 @@ import java.util.TreeMap; /** * @author godotg */ -public class ReadOnlyTreeMapIntLong { +public class FastTreeMapIntLong { private int[] keys; private long[] values; - public ReadOnlyTreeMapIntLong(TreeMap treeMap) { + public FastTreeMapIntLong(TreeMap treeMap) { var size = treeMap.size(); keys = new int[size]; values = new long[size]; @@ -86,7 +86,7 @@ public class ReadOnlyTreeMapIntLong { * if no such entry exists, returns 0. * if no such entry exists, returns -1. */ - public int indexOfCeilingKey(int key) { + public int indexOfNearestCeilingKey(int key) { if (ArrayUtils.isEmpty(keys)) { return -1; } @@ -128,7 +128,7 @@ public class ReadOnlyTreeMapIntLong { } public long getValueByCeilingKey(int key) { - var index = indexOfCeilingKey(key); + var index = indexOfNearestCeilingKey(key); if (index < 0) { throw new NoSuchElementException(); } diff --git a/net/src/test/java/com/zfoo/net/util/ReadOnlyTreeMapIntLongTest.java b/net/src/test/java/com/zfoo/net/util/FastTreeMapIntLongTest.java similarity index 84% rename from net/src/test/java/com/zfoo/net/util/ReadOnlyTreeMapIntLongTest.java rename to net/src/test/java/com/zfoo/net/util/FastTreeMapIntLongTest.java index bbdbd4e2..0ac85f74 100644 --- a/net/src/test/java/com/zfoo/net/util/ReadOnlyTreeMapIntLongTest.java +++ b/net/src/test/java/com/zfoo/net/util/FastTreeMapIntLongTest.java @@ -20,7 +20,7 @@ import java.util.TreeMap; /** * @author godotg */ -public class ReadOnlyTreeMapIntLongTest { +public class FastTreeMapIntLongTest { @Test public void test() { @@ -29,7 +29,7 @@ public class ReadOnlyTreeMapIntLongTest { treeMap.put(i, (long) i); } - var fastTreeMap = new ReadOnlyTreeMapIntLong(treeMap); + var fastTreeMap = new FastTreeMapIntLong(treeMap); Assert.assertEquals(fastTreeMap.get(0), 0); Assert.assertEquals(fastTreeMap.get(8), 8); Assert.assertEquals(fastTreeMap.get(98), 98); @@ -44,9 +44,9 @@ public class ReadOnlyTreeMapIntLongTest { Assert.assertEquals(fastTreeMap.getValueByCeilingKey(45), 46); Assert.assertEquals(fastTreeMap.getValueByCeilingKey(97), 98); Assert.assertEquals(fastTreeMap.getValueByCeilingKey(100), 0); - Assert.assertEquals(fastTreeMap.indexOfCeilingKey(-1), 0); - Assert.assertEquals(fastTreeMap.indexOfCeilingKey(0), 0); - Assert.assertEquals(fastTreeMap.indexOfCeilingKey(1), 1); + Assert.assertEquals(fastTreeMap.indexOfNearestCeilingKey(-1), 0); + Assert.assertEquals(fastTreeMap.indexOfNearestCeilingKey(0), 0); + Assert.assertEquals(fastTreeMap.indexOfNearestCeilingKey(1), 1); } }