!79 feat:增加第一个及最后一个客户端路由功能;增加路由缓存定期清理功能
* feat(sj_1.2.0-beta1):增加第一和最后一个路由功能 * feat(sj_1.2.0-beta1):增加LRU和轮询路由缓存的定期清理
This commit is contained in:
parent
f25977a218
commit
4a70ee2e32
@ -0,0 +1,17 @@
|
|||||||
|
package com.aizuda.snailjob.server.common.allocate.client;
|
||||||
|
|
||||||
|
import com.aizuda.snailjob.server.common.ClientLoadBalance;
|
||||||
|
|
||||||
|
import java.util.TreeSet;
|
||||||
|
|
||||||
|
public class ClientLoadBalanceFirst implements ClientLoadBalance {
|
||||||
|
@Override
|
||||||
|
public String route(String key, TreeSet<String> clientAllAddressSet) {
|
||||||
|
return clientAllAddressSet.first();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int routeType() {
|
||||||
|
return ClientLoadBalanceManager.AllocationAlgorithmEnum.FIRST.getType();
|
||||||
|
}
|
||||||
|
}
|
@ -21,9 +21,16 @@ public class ClientLoadBalanceLRU implements ClientLoadBalance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ConcurrentHashMap<String, LinkedHashMap<String, String>> LRU_CACHE = new ConcurrentHashMap<>();
|
private ConcurrentHashMap<String, LinkedHashMap<String, String>> LRU_CACHE = new ConcurrentHashMap<>();
|
||||||
|
private static long CACHE_VALID_TIME = 0;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String route(String allocKey, TreeSet<String> clientAllAddressSet) {
|
public String route(String allocKey, TreeSet<String> clientAllAddressSet) {
|
||||||
|
// cache clear
|
||||||
|
if (System.currentTimeMillis() > CACHE_VALID_TIME) {
|
||||||
|
LRU_CACHE.clear();
|
||||||
|
// 每12个小时定时清理一次数据
|
||||||
|
CACHE_VALID_TIME = System.currentTimeMillis() + 1000*60*60*12;
|
||||||
|
}
|
||||||
LinkedHashMap<String, String> lruItem = LRU_CACHE.get(allocKey);
|
LinkedHashMap<String, String> lruItem = LRU_CACHE.get(allocKey);
|
||||||
if (Objects.isNull(lruItem)) {
|
if (Objects.isNull(lruItem)) {
|
||||||
lruItem = new LinkedHashMap<String, String>(16, 0.75f, true) {
|
lruItem = new LinkedHashMap<String, String>(16, 0.75f, true) {
|
||||||
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.aizuda.snailjob.server.common.allocate.client;
|
||||||
|
|
||||||
|
import com.aizuda.snailjob.server.common.ClientLoadBalance;
|
||||||
|
|
||||||
|
import java.util.TreeSet;
|
||||||
|
|
||||||
|
public class ClientLoadBalanceLast implements ClientLoadBalance {
|
||||||
|
@Override
|
||||||
|
public String route(String key, TreeSet<String> clientAllAddressSet) {
|
||||||
|
return clientAllAddressSet.last();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int routeType() {
|
||||||
|
return ClientLoadBalanceManager.AllocationAlgorithmEnum.LAST.getType();
|
||||||
|
}
|
||||||
|
}
|
@ -29,7 +29,9 @@ public class ClientLoadBalanceManager {
|
|||||||
CONSISTENT_HASH(1, new ClientLoadBalanceConsistentHash(100)),
|
CONSISTENT_HASH(1, new ClientLoadBalanceConsistentHash(100)),
|
||||||
RANDOM(2, new ClientLoadBalanceRandom()),
|
RANDOM(2, new ClientLoadBalanceRandom()),
|
||||||
LRU(3, new ClientLoadBalanceLRU(100)),
|
LRU(3, new ClientLoadBalanceLRU(100)),
|
||||||
ROUND(4, new ClientLoadBalanceRound());
|
ROUND(4, new ClientLoadBalanceRound()),
|
||||||
|
FIRST(5, new ClientLoadBalanceFirst()),
|
||||||
|
LAST(6, new ClientLoadBalanceLast());
|
||||||
|
|
||||||
private final int type;
|
private final int type;
|
||||||
private final ClientLoadBalance clientLoadBalance;
|
private final ClientLoadBalance clientLoadBalance;
|
||||||
|
@ -16,9 +16,16 @@ public class ClientLoadBalanceRound implements ClientLoadBalance {
|
|||||||
|
|
||||||
private static final ConcurrentHashMap<String, AtomicInteger> COUNTER = new ConcurrentHashMap<>();
|
private static final ConcurrentHashMap<String, AtomicInteger> COUNTER = new ConcurrentHashMap<>();
|
||||||
private static final int THRESHOLD = Integer.MAX_VALUE - 10000;
|
private static final int THRESHOLD = Integer.MAX_VALUE - 10000;
|
||||||
|
private static long CACHE_VALID_TIME = 0;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String route(final String allocKey, final TreeSet<String> clientAllAddressSet) {
|
public String route(final String allocKey, final TreeSet<String> clientAllAddressSet) {
|
||||||
|
// cache clear
|
||||||
|
if (System.currentTimeMillis() > CACHE_VALID_TIME) {
|
||||||
|
COUNTER.clear();
|
||||||
|
// 每12个小时定时清理一次数据
|
||||||
|
CACHE_VALID_TIME = System.currentTimeMillis() + 1000*60*60*12;
|
||||||
|
}
|
||||||
String[] addressArr = clientAllAddressSet.toArray(new String[0]);
|
String[] addressArr = clientAllAddressSet.toArray(new String[0]);
|
||||||
AtomicInteger next = COUNTER.getOrDefault(allocKey, new AtomicInteger(1));
|
AtomicInteger next = COUNTER.getOrDefault(allocKey, new AtomicInteger(1));
|
||||||
String nextClientId = addressArr[next.get() % clientAllAddressSet.size()];
|
String nextClientId = addressArr[next.get() % clientAllAddressSet.size()];
|
||||||
|
Loading…
Reference in New Issue
Block a user