【fix sonar】 ExecutorRouteLFU.java文件
This commit is contained in:
+12
-11
@@ -17,21 +17,21 @@ import java.util.concurrent.ConcurrentMap;
|
|||||||
*/
|
*/
|
||||||
public class ExecutorRouteLFU extends ExecutorRouter {
|
public class ExecutorRouteLFU extends ExecutorRouter {
|
||||||
|
|
||||||
private static ConcurrentMap<Integer, HashMap<String, Integer>> jobLfuMap = new ConcurrentHashMap<Integer, HashMap<String, Integer>>();
|
private static ConcurrentMap<Integer, HashMap<String, Integer>> jobLfuMap = new ConcurrentHashMap<>();
|
||||||
private static long CACHE_VALID_TIME = 0;
|
private long cacheValidTime = 0;
|
||||||
|
|
||||||
public String route(int jobId, List<String> addressList) {
|
public String route(int jobId, List<String> addressList) {
|
||||||
|
|
||||||
// cache clear
|
// cache clear
|
||||||
if (System.currentTimeMillis() > CACHE_VALID_TIME) {
|
if (System.currentTimeMillis() > cacheValidTime) {
|
||||||
jobLfuMap.clear();
|
jobLfuMap.clear();
|
||||||
CACHE_VALID_TIME = System.currentTimeMillis() + 1000*60*60*24;
|
cacheValidTime = System.currentTimeMillis() + 1000*60*60*24;
|
||||||
}
|
}
|
||||||
|
|
||||||
// lfu item init
|
// lfu item init
|
||||||
HashMap<String, Integer> lfuItemMap = jobLfuMap.get(jobId); // Key排序可以用TreeMap+构造入参Compare;Value排序暂时只能通过ArrayList;
|
HashMap<String, Integer> lfuItemMap = jobLfuMap.get(jobId); // Key排序可以用TreeMap+构造入参Compare;Value排序暂时只能通过ArrayList;
|
||||||
if (lfuItemMap == null) {
|
if (lfuItemMap == null) {
|
||||||
lfuItemMap = new HashMap<String, Integer>();
|
lfuItemMap = new HashMap<>();
|
||||||
jobLfuMap.putIfAbsent(jobId, lfuItemMap); // 避免重复覆盖
|
jobLfuMap.putIfAbsent(jobId, lfuItemMap); // 避免重复覆盖
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,23 +48,24 @@ public class ExecutorRouteLFU extends ExecutorRouter {
|
|||||||
delKeys.add(existKey);
|
delKeys.add(existKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (delKeys.size() > 0) {
|
if (!delKeys.isEmpty()) {
|
||||||
for (String delKey: delKeys) {
|
for (String delKey: delKeys) {
|
||||||
lfuItemMap.remove(delKey);
|
lfuItemMap.remove(delKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// load least userd count address
|
// load least userd count address
|
||||||
List<Map.Entry<String, Integer>> lfuItemList = new ArrayList<Map.Entry<String, Integer>>(lfuItemMap.entrySet());
|
List<Map.Entry<String, Integer>> lfuItemList = new ArrayList<>(lfuItemMap.entrySet());
|
||||||
Collections.sort(lfuItemList, new Comparator<Map.Entry<String, Integer>>() {
|
/* Collections.sort(lfuItemList, new Comparator<Map.Entry<String, Integer>>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
|
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
|
||||||
return o1.getValue().compareTo(o2.getValue());
|
return o1.getValue().compareTo(o2.getValue());
|
||||||
}
|
}
|
||||||
});
|
});*/
|
||||||
|
Collections.sort(lfuItemList, (o1,o2)-> o1.getValue().compareTo(o2.getValue()));
|
||||||
|
|
||||||
Map.Entry<String, Integer> addressItem = lfuItemList.get(0);
|
Map.Entry<String, Integer> addressItem = lfuItemList.get(0);
|
||||||
String minAddress = addressItem.getKey();
|
// String minAddress = addressItem.getKey();
|
||||||
addressItem.setValue(addressItem.getValue() + 1);
|
addressItem.setValue(addressItem.getValue() + 1);
|
||||||
|
|
||||||
return addressItem.getKey();
|
return addressItem.getKey();
|
||||||
@@ -73,7 +74,7 @@ public class ExecutorRouteLFU extends ExecutorRouter {
|
|||||||
@Override
|
@Override
|
||||||
public ReturnT<String> route(TriggerParam triggerParam, List<String> addressList) {
|
public ReturnT<String> route(TriggerParam triggerParam, List<String> addressList) {
|
||||||
String address = route(triggerParam.getJobId(), addressList);
|
String address = route(triggerParam.getJobId(), addressList);
|
||||||
return new ReturnT<String>(address);
|
return new ReturnT<>(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user