From e69107194f4cca5de66b79c5f1ba615d83fe0484 Mon Sep 17 00:00:00 2001 From: Gagan Juneja Date: Wed, 30 Oct 2024 20:35:49 +0530 Subject: [PATCH] test changes Signed-off-by: Gagan Juneja --- .../RTFCacheConfigMetricsCollector.java | 136 ++++++++++++------ .../telemetry/RTFHeapMetricsCollector.java | 46 +++++- 2 files changed, 131 insertions(+), 51 deletions(-) diff --git a/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFCacheConfigMetricsCollector.java b/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFCacheConfigMetricsCollector.java index 8a5e3e5c..b5bc67a8 100644 --- a/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFCacheConfigMetricsCollector.java +++ b/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFCacheConfigMetricsCollector.java @@ -11,6 +11,7 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +import java.io.Closeable; import java.security.AccessController; import java.security.PrivilegedAction; import org.apache.commons.lang3.reflect.FieldUtils; @@ -34,10 +35,12 @@ public class RTFCacheConfigMetricsCollector extends PerformanceAnalyzerMetricsCollector implements TelemetryCollector { - private MetricsRegistry metricsRegistry; private static final Logger LOG = LogManager.getLogger(RTFCacheConfigMetricsCollector.class); private PerformanceAnalyzerController performanceAnalyzerController; private ConfigOverridesWrapper configOverridesWrapper; + private Closeable fieldDataCacheGauge; + private Closeable requestCacheGauge; + private boolean metricsInitialised; public RTFCacheConfigMetricsCollector( PerformanceAnalyzerController performanceAnalyzerController, @@ -56,11 +59,11 @@ public RTFCacheConfigMetricsCollector( public void collectMetrics(long l) { if (performanceAnalyzerController.isCollectorDisabled( configOverridesWrapper, getCollectorName())) { + closeOpenGaugeObservablesIfAny(); LOG.info("RTFCacheConfigMetricsCollector is disabled. Skipping collection."); return; } - - metricsRegistry = OpenSearchResources.INSTANCE.getMetricsRegistry(); + MetricsRegistry metricsRegistry = OpenSearchResources.INSTANCE.getMetricsRegistry(); if (metricsRegistry == null) { LOG.error("could not get the instance of MetricsRegistry class"); return; @@ -71,33 +74,59 @@ configOverridesWrapper, getCollectorName())) { LOG.error("could not get the instance of indicesService class"); return; } - LOG.debug("Executing collect metrics for RTFCacheConfigMetricsCollector"); - CacheMaxSizeStatus fieldDataCacheMaxSizeStatus = - AccessController.doPrivileged( - (PrivilegedAction) - () -> { - try { - Cache fieldDataCache = - indicesService - .getIndicesFieldDataCache() - .getCache(); - long fieldDataMaxSize = - (Long) - FieldUtils.readField( - fieldDataCache, - CACHE_MAX_WEIGHT, - true); - return new CacheMaxSizeStatus( - FIELD_DATA_CACHE.toString(), fieldDataMaxSize); - } catch (Exception e) { - LOG.debug( - "Error occurred while fetching fieldDataCacheMaxSizeStatus: " - + e.getMessage()); - return null; - } - }); + initialiseMetricsIfNeeded(metricsRegistry, indicesService); + } + + private void initialiseMetricsIfNeeded( + MetricsRegistry metricsRegistry, IndicesService indicesService) { + if (!metricsInitialised) { + fieldDataCacheGauge = + metricsRegistry.createGauge( + RTFMetrics.CacheConfigValue.Constants.CACHE_MAX_SIZE_VALUE, + "Cache Max Size metrics", + RTFMetrics.MetricUnits.BYTE.toString(), + () -> getFieldCacheMaxSizeStatus(indicesService), + Tags.create() + .addTag( + RTFMetrics.CacheConfigDimension.Constants.TYPE_VALUE, + FIELD_DATA_CACHE.toString())); + requestCacheGauge = + metricsRegistry.createGauge( + RTFMetrics.CacheConfigValue.Constants.CACHE_MAX_SIZE_VALUE, + "Cache Max Size metrics", + RTFMetrics.MetricUnits.BYTE.toString(), + () -> getRequestCacheMaxSizeStatus(indicesService), + Tags.create() + .addTag( + RTFMetrics.CacheConfigDimension.Constants.TYPE_VALUE, + SHARD_REQUEST_CACHE.toString())); + metricsInitialised = true; + } + } + + private void closeOpenGaugeObservablesIfAny() { + if (fieldDataCacheGauge != null) { + try { + fieldDataCacheGauge.close(); + } catch (Exception e) { + LOG.error("Unable to close the fieldDataCacheGauge observable"); + } finally { + fieldDataCacheGauge = null; + } + } + if (requestCacheGauge != null) { + try { + requestCacheGauge.close(); + } catch (Exception e) { + LOG.error("Unable to close the fieldDataCacheGauge observable"); + } finally { + requestCacheGauge = null; + } + } + } + private double getRequestCacheMaxSizeStatus(IndicesService indicesService) { CacheMaxSizeStatus shardRequestCacheMaxSizeStatus = AccessController.doPrivileged( (PrivilegedAction) @@ -132,28 +161,45 @@ configOverridesWrapper, getCollectorName())) { return null; } }); - - if (fieldDataCacheMaxSizeStatus != null - && fieldDataCacheMaxSizeStatus.getCacheMaxSize() > 0) { - recordMetrics(fieldDataCacheMaxSizeStatus); - } - if (shardRequestCacheMaxSizeStatus != null && shardRequestCacheMaxSizeStatus.getCacheMaxSize() > 0) { - recordMetrics(shardRequestCacheMaxSizeStatus); + return shardRequestCacheMaxSizeStatus.getCacheMaxSize(); + } else { + return 0.0; } } - private void recordMetrics(CacheMaxSizeStatus cacheMaxSizeStatus) { - metricsRegistry.createGauge( - RTFMetrics.CacheConfigValue.Constants.CACHE_MAX_SIZE_VALUE, - "Cache Max Size metrics", - RTFMetrics.MetricUnits.BYTE.toString(), - () -> (double) cacheMaxSizeStatus.getCacheMaxSize(), - Tags.create() - .addTag( - RTFMetrics.CacheConfigDimension.Constants.TYPE_VALUE, - cacheMaxSizeStatus.getCacheType())); + private static double getFieldCacheMaxSizeStatus(IndicesService indicesService) { + CacheMaxSizeStatus fieldDataCacheMaxSizeStatus = + AccessController.doPrivileged( + (PrivilegedAction) + () -> { + try { + Cache fieldDataCache = + indicesService + .getIndicesFieldDataCache() + .getCache(); + long fieldDataMaxSize = + (Long) + FieldUtils.readField( + fieldDataCache, + CACHE_MAX_WEIGHT, + true); + return new CacheMaxSizeStatus( + FIELD_DATA_CACHE.toString(), fieldDataMaxSize); + } catch (Exception e) { + LOG.debug( + "Error occurred while fetching fieldDataCacheMaxSizeStatus: " + + e.getMessage()); + return null; + } + }); + if (fieldDataCacheMaxSizeStatus != null + && fieldDataCacheMaxSizeStatus.getCacheMaxSize() > 0) { + return fieldDataCacheMaxSizeStatus.getCacheMaxSize(); + } else { + return 0.0; + } } static class CacheMaxSizeStatus extends MetricStatus { diff --git a/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFHeapMetricsCollector.java b/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFHeapMetricsCollector.java index 6872089d..81c1a1c0 100644 --- a/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFHeapMetricsCollector.java +++ b/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFHeapMetricsCollector.java @@ -5,7 +5,9 @@ package org.opensearch.performanceanalyzer.collectors.telemetry; +import java.io.Closeable; import java.lang.management.MemoryUsage; +import java.util.HashMap; import java.util.Map; import java.util.function.Supplier; import org.apache.logging.log4j.LogManager; @@ -38,6 +40,7 @@ public class RTFHeapMetricsCollector extends PerformanceAnalyzerMetricsCollector private boolean metricsInitialised; private PerformanceAnalyzerController performanceAnalyzerController; private ConfigOverridesWrapper configOverridesWrapper; + private Map memTypeToGaugeObservableMap; public RTFHeapMetricsCollector( PerformanceAnalyzerController performanceAnalyzerController, @@ -50,6 +53,7 @@ public RTFHeapMetricsCollector( this.metricsInitialised = false; this.performanceAnalyzerController = performanceAnalyzerController; this.configOverridesWrapper = configOverridesWrapper; + this.memTypeToGaugeObservableMap = new HashMap<>(); } @Override @@ -57,6 +61,7 @@ public void collectMetrics(long startTime) { if (performanceAnalyzerController.isCollectorDisabled( configOverridesWrapper, getCollectorName())) { LOG.info("RTFDisksCollector is disabled. Skipping collection."); + closeOpenGaugeObservablesIfAny(); return; } @@ -72,6 +77,21 @@ configOverridesWrapper, getCollectorName())) { recordMetrics(); } + private void closeOpenGaugeObservablesIfAny() { + for (String key : memTypeToGaugeObservableMap.keySet()) { + if (memTypeToGaugeObservableMap.get(key) != null) { + try { + Closeable observableGauge = memTypeToGaugeObservableMap.remove(key); + if (observableGauge != null) { + observableGauge.close(); + } + } catch (Exception e) { + LOG.error("Unable to close the observable gauge for key {}", key); + } + } + } + } + private void initialiseMetricsIfNeeded() { if (metricsInitialised == false) { gcCollectionEventMetrics = @@ -91,6 +111,7 @@ private void initialiseMetricsIfNeeded() { RTFMetrics.HeapValue.Constants.USED_VALUE, "GC Heap Used PA Metrics", RTFMetrics.MetricUnits.BYTE.toString()); + metricsInitialised = true; } } @@ -119,12 +140,25 @@ private void recordMetrics() { heapUsedMetrics.record( memoryUsage.getUsed(), Tags.create().addTag(memTypeAttributeKey, entry.getKey())); - metricsRegistry.createGauge( - RTFMetrics.HeapValue.Constants.MAX_VALUE, - "Heap Max PA metrics", - "", - () -> (double) memoryUsage.getMax(), - Tags.create().addTag(memTypeAttributeKey, entry.getKey())); + createGaugeInstanceIfNotAvailable(entry.getKey(), memoryUsage); } } + + private void createGaugeInstanceIfNotAvailable(String key, MemoryUsage memoryUsage) { + if (!memTypeToGaugeObservableMap.containsKey(key)) { + Closeable observableGauge = + metricsRegistry.createGauge( + RTFMetrics.HeapValue.Constants.MAX_VALUE, + "Heap Max PA metrics", + "", + () -> (double) memoryUsage.getMax(), + Tags.create().addTag(memTypeAttributeKey, key)); + memTypeToGaugeObservableMap.put(key, observableGauge); + } + } + + private double getMaxValueForMemTypeAttribute( + String memTypeAttributeKey, MemoryUsage memoryUsage) { + return memoryUsage.getMax(); + } }