|
|
@@ -2,7 +2,8 @@ package com.xxl.job.admin.core.route.strategy;
|
|
2
|
2
|
|
|
3
|
3
|
import com.xxl.job.admin.core.route.ExecutorRouter;
|
|
4
|
4
|
|
|
5
|
|
-import java.util.ArrayList;
|
|
|
5
|
+import java.util.*;
|
|
|
6
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
6
|
7
|
|
|
7
|
8
|
/**
|
|
8
|
9
|
* 单个JOB对应的每个执行器,使用频率最低的优先被选举
|
|
|
@@ -13,12 +14,44 @@ import java.util.ArrayList;
|
|
13
|
14
|
*/
|
|
14
|
15
|
public class ExecutorRouteLFU extends ExecutorRouter {
|
|
15
|
16
|
|
|
|
17
|
+ private static ConcurrentHashMap<Integer, HashMap<String, Integer>> jobLfuMap = new ConcurrentHashMap<Integer, HashMap<String, Integer>>();
|
|
|
18
|
+ private static long CACHE_VALID_TIME = 0;
|
|
|
19
|
+
|
|
16
|
20
|
@Override
|
|
17
|
21
|
public String route(int jobId, ArrayList<String> addressList) {
|
|
18
|
22
|
|
|
19
|
|
- // TODO
|
|
|
23
|
+ // cache clear
|
|
|
24
|
+ if (System.currentTimeMillis() > CACHE_VALID_TIME) {
|
|
|
25
|
+ jobLfuMap.clear();
|
|
|
26
|
+ CACHE_VALID_TIME = System.currentTimeMillis() + 1000*60*60*24;
|
|
|
27
|
+ }
|
|
|
28
|
+
|
|
|
29
|
+ // lfu item init
|
|
|
30
|
+ HashMap<String, Integer> lfuItemMap = jobLfuMap.get(jobId); // Key排序可以用TreeMap+构造入参Compare;Value排序暂时只能通过ArrayList;
|
|
|
31
|
+ if (lfuItemMap == null) {
|
|
|
32
|
+ lfuItemMap = new HashMap<String, Integer>();
|
|
|
33
|
+ jobLfuMap.put(jobId, lfuItemMap);
|
|
|
34
|
+ }
|
|
|
35
|
+ for (String address: addressList) {
|
|
|
36
|
+ if (!lfuItemMap.containsKey(address)) {
|
|
|
37
|
+ lfuItemMap.put(address, 0);
|
|
|
38
|
+ }
|
|
|
39
|
+ }
|
|
|
40
|
+
|
|
|
41
|
+ // load least userd count address
|
|
|
42
|
+ List<Map.Entry<String, Integer>> lfuItemList = new ArrayList<Map.Entry<String, Integer>>(lfuItemMap.entrySet());
|
|
|
43
|
+ Collections.sort(lfuItemList, new Comparator<Map.Entry<String, Integer>>() {
|
|
|
44
|
+ @Override
|
|
|
45
|
+ public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
|
|
|
46
|
+ return o1.getValue().compareTo(o2.getValue());
|
|
|
47
|
+ }
|
|
|
48
|
+ });
|
|
|
49
|
+
|
|
|
50
|
+ Map.Entry<String, Integer> addressItem = lfuItemList.get(0);
|
|
|
51
|
+ String minAddress = addressItem.getKey();
|
|
|
52
|
+ addressItem.setValue(addressItem.getValue() + 1);
|
|
20
|
53
|
|
|
21
|
|
- return addressList.get(0);
|
|
|
54
|
+ return addressItem.getKey();
|
|
22
|
55
|
}
|
|
23
|
56
|
|
|
24
|
57
|
}
|