[ agent ] [ executor ]- optimize the command executor cache log

This commit is contained in:
zeaslity
2023-01-18 14:10:28 +08:00
parent 4acba4ce8e
commit 9211966c59

View File

@@ -2,6 +2,7 @@ package io.wdd.agent.executor.thread;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.BufferedReader; import java.io.BufferedReader;
@@ -13,6 +14,7 @@ import java.util.List;
/** /**
* utils to cache store the command execution logs * utils to cache store the command execution logs
*
*/ */
@Component @Component
@Slf4j @Slf4j
@@ -70,8 +72,39 @@ public class LogToArrayListCache {
private int hashStreamKeyToCachedArrayListIndex(String streamKey) { private int hashStreamKeyToCachedArrayListIndex(String streamKey) {
int size = CachedCommandLog.size(); int size = CachedCommandLog.size();
int result = Math.abs(streamKey.hashCode() % size);
boolean hasRehashed = false;
return Math.abs(streamKey.hashCode() % size); if (CollectionUtils.isNotEmpty(CachedCommandLog.get(result))) {
for (int index = result+1; index < CachedCommandLog.size(); index++) {
// from the index to the end
if (CollectionUtils.isEmpty(CachedCommandLog.get(index))){
hasRehashed = true;
result = index;
break;
}
}
if (!hasRehashed) {
for (int index = 0; index < result; index++) {
// from begin to the index
if (CollectionUtils.isEmpty(CachedCommandLog.get(index))){
hasRehashed = true;
result = index;
break;
}
}
}
if (!hasRehashed) {
// reach here means no empty cache array list
CachedCommandLog.add(
new ArrayList<>(256)
);
result = size;
}
}
return result;
} }
} }