From df8947e70679afee44965650da550d487e8c0c5f Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:49:13 +0530 Subject: [PATCH] Fixed the bug in CacheConfigMetricsCollector (#657) (#661) * Fixed the bug in CacheConfigMetricsCollector Signed-off-by: Atharva Sharma * Fixes error logging Signed-off-by: Gagan Juneja --------- Signed-off-by: Atharva Sharma Signed-off-by: Gagan Juneja Co-authored-by: Gagan Juneja (cherry picked from commit 489f318585ab4c572d2e1c4bc143a4a99807858c) Co-authored-by: Atharva Sharma <60044988+atharvasharma61@users.noreply.github.com> --- .../CacheConfigMetricsCollector.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/opensearch/performanceanalyzer/collectors/CacheConfigMetricsCollector.java b/src/main/java/org/opensearch/performanceanalyzer/collectors/CacheConfigMetricsCollector.java index e2828483..419a251e 100644 --- a/src/main/java/org/opensearch/performanceanalyzer/collectors/CacheConfigMetricsCollector.java +++ b/src/main/java/org/opensearch/performanceanalyzer/collectors/CacheConfigMetricsCollector.java @@ -17,6 +17,8 @@ import java.security.AccessController; import java.security.PrivilegedAction; import org.apache.commons.lang3.reflect.FieldUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.opensearch.common.cache.Cache; import org.opensearch.indices.IndicesService; import org.opensearch.performanceanalyzer.OpenSearchResources; @@ -43,6 +45,7 @@ public class CacheConfigMetricsCollector extends PerformanceAnalyzerMetricsColle implements MetricsProcessor { public static final int SAMPLING_TIME_INTERVAL = MetricsConfiguration.CONFIG_MAP.get(CacheConfigMetricsCollector.class).samplingInterval; + private static final Logger LOG = LogManager.getLogger(CacheConfigMetricsCollector.class); private static final int KEYS_PATH_LENGTH = 0; private StringBuilder value; @@ -104,10 +107,14 @@ public void collectMetrics(long startTime) { indicesService, "indicesRequestCache", true); + Object openSearchOnHeapCache = + FieldUtils.readField(reqCache, "cache", true); Cache requestCache = (Cache) FieldUtils.readField( - reqCache, "cache", true); + openSearchOnHeapCache, + "cache", + true); Long requestCacheMaxSize = (Long) FieldUtils.readField( @@ -118,10 +125,15 @@ public void collectMetrics(long startTime) { SHARD_REQUEST_CACHE.toString(), requestCacheMaxSize); } catch (Exception e) { - return new CacheMaxSizeStatus( - SHARD_REQUEST_CACHE.toString(), null); + LOG.error("Error while evaluating requestCacheMaxSize", e); + return null; } }); + + if (shardRequestCacheMaxSizeStatus == null) { + return; + } + value.append(PerformanceAnalyzerMetrics.sMetricNewLineDelimitor) .append(shardRequestCacheMaxSizeStatus.serialize()); saveMetricValues(value.toString(), startTime);