diff --git a/server/src/main/java/org/opensearch/common/cache/stats/NoopCacheStatsHolder.java b/server/src/main/java/org/opensearch/common/cache/stats/NoopCacheStatsHolder.java index ea050d306b5a7..47a4b54ba5f93 100644 --- a/server/src/main/java/org/opensearch/common/cache/stats/NoopCacheStatsHolder.java +++ b/server/src/main/java/org/opensearch/common/cache/stats/NoopCacheStatsHolder.java @@ -13,10 +13,16 @@ /** * A dummy version of CacheStatsHolder, which cache implementations use when FeatureFlags.PLUGGABLE_CACHES is false. * Returns all-zero stats when calling getImmutableCacheStatsHolder(). Always returns 0 for count(). + * A singleton instance is used for memory purposes. */ public class NoopCacheStatsHolder implements CacheStatsHolder { + private static final NoopCacheStatsHolder singletonInstance = new NoopCacheStatsHolder(); - public NoopCacheStatsHolder() {} + private NoopCacheStatsHolder() {} + + public static NoopCacheStatsHolder getInstance() { + return singletonInstance; + } @Override public void incrementHits(List dimensionValues) {} diff --git a/server/src/main/java/org/opensearch/common/cache/store/OpenSearchOnHeapCache.java b/server/src/main/java/org/opensearch/common/cache/store/OpenSearchOnHeapCache.java index e03a9ac8693f3..bb88aea9b80e6 100644 --- a/server/src/main/java/org/opensearch/common/cache/store/OpenSearchOnHeapCache.java +++ b/server/src/main/java/org/opensearch/common/cache/store/OpenSearchOnHeapCache.java @@ -54,7 +54,8 @@ public class OpenSearchOnHeapCache implements ICache, RemovalListene private final List dimensionNames; private final ToLongBiFunction, V> weigher; - private final Settings settings; + // private final Settings settings; + private final boolean useNoopStats; public OpenSearchOnHeapCache(Builder builder) { CacheBuilder, V> cacheBuilder = CacheBuilder., V>builder() @@ -66,14 +67,14 @@ public OpenSearchOnHeapCache(Builder builder) { } cache = cacheBuilder.build(); this.dimensionNames = Objects.requireNonNull(builder.dimensionNames, "Dimension names can't be null"); - if (useNoopStats(builder.getSettings())) { - this.cacheStatsHolder = new NoopCacheStatsHolder(); + this.useNoopStats = builder.useNoopStats; + if (useNoopStats) { + this.cacheStatsHolder = NoopCacheStatsHolder.getInstance(); } else { this.cacheStatsHolder = new DefaultCacheStatsHolder(dimensionNames); } this.removalListener = builder.getRemovalListener(); this.weigher = builder.getWeigher(); - this.settings = builder.getSettings(); } @Override @@ -130,7 +131,7 @@ public Iterable> keys() { @Override public long count() { - if (useNoopStats(settings)) { + if (useNoopStats) { return cache.count(); } else { return cacheStatsHolder.count(); @@ -165,11 +166,6 @@ public void onRemoval(RemovalNotification, V> notification) { } } - private boolean useNoopStats(Settings settings) { - // Use noop stats when pluggable caching is off - return !FeatureFlags.PLUGGABLE_CACHE_SETTING.get(settings); - } - /** * Factory to create OpenSearchOnheap cache. */ @@ -182,10 +178,10 @@ public ICache create(CacheConfig config, CacheType cacheType, Map> settingList = OpenSearchOnHeapCacheSettings.getSettingListForCacheType(cacheType); Settings settings = config.getSettings(); ICacheBuilder builder = new Builder().setDimensionNames(config.getDimensionNames()) + .setUseNoopStats(useNoopStats(config.getSettings())) .setMaximumWeightInBytes(((ByteSizeValue) settingList.get(MAXIMUM_SIZE_IN_BYTES_KEY).get(settings)).getBytes()) .setExpireAfterAccess(((TimeValue) settingList.get(EXPIRE_AFTER_ACCESS_KEY).get(settings))) .setWeigher(config.getWeigher()) - .setSettings(config.getSettings()) .setRemovalListener(config.getRemovalListener()); Setting cacheSettingForCacheType = CacheSettings.CACHE_TYPE_STORE_NAME.getConcreteSettingForNamespace( cacheType.getSettingPrefix() @@ -203,6 +199,11 @@ public ICache create(CacheConfig config, CacheType cacheType, public String getCacheName() { return NAME; } + + private boolean useNoopStats(Settings settings) { + // Use noop stats when pluggable caching is off + return !FeatureFlags.PLUGGABLE_CACHE_SETTING.get(settings); + } } /** @@ -212,12 +213,18 @@ public String getCacheName() { */ public static class Builder extends ICacheBuilder { private List dimensionNames; + private boolean useNoopStats; public Builder setDimensionNames(List dimensionNames) { this.dimensionNames = dimensionNames; return this; } + public Builder setUseNoopStats(boolean useNoopStats) { + this.useNoopStats = useNoopStats; + return this; + } + @Override public ICache build() { return new OpenSearchOnHeapCache(this);