From 9211966c59801d68ffc9e4cf193c85606519067b Mon Sep 17 00:00:00 2001 From: zeaslity Date: Wed, 18 Jan 2023 14:10:28 +0800 Subject: [PATCH] [ agent ] [ executor ]- optimize the command executor cache log --- .../executor/thread/LogToArrayListCache.java | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/agent/src/main/java/io/wdd/agent/executor/thread/LogToArrayListCache.java b/agent/src/main/java/io/wdd/agent/executor/thread/LogToArrayListCache.java index a3523a6..12559a0 100644 --- a/agent/src/main/java/io/wdd/agent/executor/thread/LogToArrayListCache.java +++ b/agent/src/main/java/io/wdd/agent/executor/thread/LogToArrayListCache.java @@ -2,6 +2,7 @@ package io.wdd.agent.executor.thread; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections.CollectionUtils; import org.springframework.stereotype.Component; import java.io.BufferedReader; @@ -13,6 +14,7 @@ import java.util.List; /** * utils to cache store the command execution logs + * */ @Component @Slf4j @@ -70,8 +72,39 @@ public class LogToArrayListCache { private int hashStreamKeyToCachedArrayListIndex(String streamKey) { 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; } }