设计新的路由算法"LRU";规划新算法"LFU";

pull/1/head
xueli.xue 8 years ago
parent 9c03285d94
commit 1c5a841579

@ -12,6 +12,8 @@ public enum ExecutorRouteStrategyEnum {
ROUND("轮询", new ExecutorRouteRound()),
RANDOM("随机", new ExecutorRouteRandom()),
CONSISTENT_HASH("一致性HASH", new ExecutorRouteConsistentHash()),
LEAST_FREQUENTLY_USED("最不经常使用", new ExecutorRouteLFU()),
LEAST_RECENTLY_USED("最近最久未使用", new ExecutorRouteLRU()),
FAILOVER("故障转移", null);
ExecutorRouteStrategyEnum(String title, ExecutorRouter router) {

@ -23,28 +23,11 @@ public abstract class ExecutorRouter {
public static void main(String[] args) {
int c1 = 0;
int c2 = 0;
int c3 = 0;
long start = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
String ret = ExecutorRouter.route(i, new ArrayList<String>(Arrays.asList("127.0.0.1:0000", "127.0.0.1:2222", "127.0.0.1:3333")), ExecutorRouteStrategyEnum.CONSISTENT_HASH.name());
if (ret.equals("127.0.0.1:0000")) {
c1++;
} else if (ret.equals("127.0.0.1:2222")) {
c2++;
} else if (ret.equals("127.0.0.1:3333")) {
c3++;
}
}
long end = System.currentTimeMillis();
System.out.println(end - start); // 1000*100=740、1000*1=162、
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
for (int i = 0; i < 100; i++) {
String ret = ExecutorRouter.route(666, new ArrayList<String>(Arrays.asList("127.0.0.1:0000", "127.0.0.1:2222", "127.0.0.1:3333")), ExecutorRouteStrategyEnum.LEAST_RECENTLY_USED.name());
System.out.println(ret);
}
}

@ -10,7 +10,7 @@ import java.util.SortedMap;
import java.util.TreeMap;
/**
* JOBJOB
* JOBJOBJOB
* avirtual node
* bhash method replace hashCodeStringhashCodehashCode
* Created by xuxueli on 17/3/10.

@ -0,0 +1,24 @@
package com.xxl.job.admin.core.route.strategy;
import com.xxl.job.admin.core.route.ExecutorRouter;
import java.util.ArrayList;
/**
* JOB使
* a(*)LFU(Least Frequently Used)使/
* bLRU(Least Recently Used)使
*
* Created by xuxueli on 17/3/10.
*/
public class ExecutorRouteLFU extends ExecutorRouter {
@Override
public String route(int jobId, ArrayList<String> addressList) {
// TODO
return addressList.get(0);
}
}

@ -0,0 +1,45 @@
package com.xxl.job.admin.core.route.strategy;
import com.xxl.job.admin.core.route.ExecutorRouter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.concurrent.ConcurrentHashMap;
/**
* JOB使
* aLFU(Least Frequently Used)使/
* b(*)LRU(Least Recently Used)使
*
* Created by xuxueli on 17/3/10.
*/
public class ExecutorRouteLRU extends ExecutorRouter {
private static ConcurrentHashMap<Integer, LinkedHashMap<String, String>> jobLRUMap = new ConcurrentHashMap<Integer, LinkedHashMap<String, String>>();
@Override
public String route(int jobId, ArrayList<String> addressList) {
LinkedHashMap<String, String> lruItem = jobLRUMap.get(jobId);
if (lruItem == null) {
/**
* LinkedHashMap
* aaccessOrderture=访get/putfalse=
* bremoveEldestEntrytrueLinkedHashMaptrueLRU
*/
lruItem = new LinkedHashMap<>(16, 0.75f, true);
jobLRUMap.put(jobId, lruItem);
}
for (String address: addressList) {
if (!lruItem.containsKey(address)) {
lruItem.put(address, address);
}
}
String eldestKey = lruItem.entrySet().iterator().next().getKey();
String eldestValue = lruItem.get(eldestKey);
return eldestValue;
}
}
Loading…
Cancel
Save