mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-24 10:27:42 +00:00
ref[tree]: fast tree map
This commit is contained in:
+7
-7
@@ -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<ReadOnlyTreeMapIntLong> consistentHashMap = new AtomicReferenceArray<>(ProtocolManager.MAX_MODULE_NUM);
|
||||
private static final AtomicReferenceArray<FastTreeMapIntLong> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+4
-4
@@ -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<Integer, Long> treeMap) {
|
||||
public FastTreeMapIntLong(TreeMap<Integer, Long> 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();
|
||||
}
|
||||
+5
-5
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user