diff --git a/server/src/main/java/org/opensearch/common/cache/stats/SingleDimensionCacheStats.java b/server/src/main/java/org/opensearch/common/cache/stats/SingleDimensionCacheStats.java index 0fb0c00f1a3cd..b0cc183a78bc7 100644 --- a/server/src/main/java/org/opensearch/common/cache/stats/SingleDimensionCacheStats.java +++ b/server/src/main/java/org/opensearch/common/cache/stats/SingleDimensionCacheStats.java @@ -17,6 +17,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; /** * A CacheStats implementation for caches that aggregate over a single dimension. @@ -24,11 +26,11 @@ */ public class SingleDimensionCacheStats implements CacheStats { // Maintain a counter metric for each shard id (dimension values) - private final Map hitsMap; - private final Map missesMap; - private final Map evictionsMap; - private final Map memorySizeMap; - private final Map entriesMap; + private final ConcurrentMap hitsMap; + private final ConcurrentMap missesMap; + private final ConcurrentMap evictionsMap; + private final ConcurrentMap memorySizeMap; + private final ConcurrentMap entriesMap; // Also maintain a single total counter metric, to avoid having to sum over many values for shards private final CounterMetric totalHits; @@ -41,11 +43,11 @@ public class SingleDimensionCacheStats implements CacheStats { private final String allowedDimensionName; public SingleDimensionCacheStats(String allowedDimensionName) { - this.hitsMap = new HashMap<>(); - this.missesMap = new HashMap<>(); - this.evictionsMap = new HashMap<>(); - this.memorySizeMap = new HashMap<>(); - this.entriesMap = new HashMap<>(); + this.hitsMap = new ConcurrentHashMap<>(); + this.missesMap = new ConcurrentHashMap<>(); + this.evictionsMap = new ConcurrentHashMap<>(); + this.memorySizeMap = new ConcurrentHashMap<>(); + this.entriesMap = new ConcurrentHashMap<>(); this.totalHits = new CounterMetric(); this.totalMisses = new CounterMetric(); @@ -212,8 +214,8 @@ private Map convertCounterMapToLong(Map inp return result; } - private Map convertLongMapToCounterMetric(Map inputMap) { - Map result = new HashMap<>(); + private ConcurrentMap convertLongMapToCounterMetric(Map inputMap) { + ConcurrentMap result = new ConcurrentHashMap<>(); for (String key: inputMap.keySet()) { CounterMetric counter = new CounterMetric(); counter.inc(inputMap.get(key));