diff --git a/server/src/main/java/org/opensearch/common/cache/stats/CacheStatsHolder.java b/server/src/main/java/org/opensearch/common/cache/stats/CacheStatsHolder.java index a8b7c27ef9e79..308b1fba395dd 100644 --- a/server/src/main/java/org/opensearch/common/cache/stats/CacheStatsHolder.java +++ b/server/src/main/java/org/opensearch/common/cache/stats/CacheStatsHolder.java @@ -28,7 +28,7 @@ * * @opensearch.experimental */ -public class CacheStatsHolder { +public class CacheStatsHolder implements CacheStatsHolderInterface { // The list of permitted dimensions. Should be ordered from "outermost" to "innermost", as you would like to // aggregate them in an API response. @@ -53,32 +53,39 @@ public List getDimensionNames() { // For all these increment functions, the dimensions list comes from the key, and contains all dimensions present in dimensionNames. // The order has to match the order given in dimensionNames. + @Override public void incrementHits(List dimensionValues) { internalIncrement(dimensionValues, Node::incrementHits, true); } + @Override public void incrementMisses(List dimensionValues) { internalIncrement(dimensionValues, Node::incrementMisses, true); } + @Override public void incrementEvictions(List dimensionValues) { internalIncrement(dimensionValues, Node::incrementEvictions, true); } + @Override public void incrementSizeInBytes(List dimensionValues, long amountBytes) { internalIncrement(dimensionValues, (node) -> node.incrementSizeInBytes(amountBytes), true); } // For decrements, we should not create nodes if they are absent. This protects us from erroneously decrementing values for keys // which have been entirely deleted, for example in an async removal listener. + @Override public void decrementSizeInBytes(List dimensionValues, long amountBytes) { internalIncrement(dimensionValues, (node) -> node.decrementSizeInBytes(amountBytes), false); } + @Override public void incrementEntries(List dimensionValues) { internalIncrement(dimensionValues, Node::incrementEntries, true); } + @Override public void decrementEntries(List dimensionValues) { internalIncrement(dimensionValues, Node::decrementEntries, false); } @@ -87,6 +94,7 @@ public void decrementEntries(List dimensionValues) { * Reset number of entries and memory size when all keys leave the cache, but don't reset hit/miss/eviction numbers. * This is in line with the behavior of the existing API when caches are cleared. */ + @Override public void reset() { resetHelper(statsRoot); } @@ -98,6 +106,7 @@ private void resetHelper(Node current) { } } + @Override public long count() { // Include this here so caches don't have to create an entire CacheStats object to run count(). return statsRoot.getEntries(); @@ -156,10 +165,12 @@ private boolean internalIncrementHelper( /** * Produce an immutable version of these stats. */ + @Override public ImmutableCacheStatsHolder getImmutableCacheStatsHolder() { return new ImmutableCacheStatsHolder(statsRoot.snapshot(), dimensionNames); } + @Override public void removeDimensions(List dimensionValues) { assert dimensionValues.size() == dimensionNames.size() : "Must specify a value for every dimension when removing from StatsHolder"; // As we are removing nodes from the tree, obtain the lock diff --git a/server/src/main/java/org/opensearch/common/cache/stats/CacheStatsHolderInterface.java b/server/src/main/java/org/opensearch/common/cache/stats/CacheStatsHolderInterface.java new file mode 100644 index 0000000000000..2fdf11e75a332 --- /dev/null +++ b/server/src/main/java/org/opensearch/common/cache/stats/CacheStatsHolderInterface.java @@ -0,0 +1,39 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.common.cache.stats; + +import java.util.List; + +/** + * An abstract class extended by CacheStatsHolder and DummyCacheStatsHolder. + * Can be removed once the pluggable caches feature is no longer experimental. + */ +public interface CacheStatsHolderInterface { + void incrementHits(List dimensionValues); + + void incrementMisses(List dimensionValues); + + void incrementEvictions(List dimensionValues); + + void incrementSizeInBytes(List dimensionValues, long amountBytes); + + void decrementSizeInBytes(List dimensionValues, long amountBytes); + + void incrementEntries(List dimensionValues); + + void decrementEntries(List dimensionValues); + + void reset(); + + long count(); + + void removeDimensions(List dimensionValues); + + ImmutableCacheStatsHolder getImmutableCacheStatsHolder(); +} diff --git a/server/src/main/java/org/opensearch/common/cache/stats/DummyCacheStatsHolder.java b/server/src/main/java/org/opensearch/common/cache/stats/DummyCacheStatsHolder.java new file mode 100644 index 0000000000000..f12b4ed14bc77 --- /dev/null +++ b/server/src/main/java/org/opensearch/common/cache/stats/DummyCacheStatsHolder.java @@ -0,0 +1,70 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.common.cache.stats; + +import org.opensearch.common.metrics.CounterMetric; + +import java.util.List; + +/** + * A dummy version of CacheStatsHolder, which cache implementations use when FeatureFlags.PLUGGABLE_CACHES is false. + * Will be removed once pluggable caches is no longer an experimental feature. + * Returns all-zero stats when calling getImmutableCacheStatsHolder(). Does keep track of entries for use in ICache.count(). + */ +public class DummyCacheStatsHolder implements CacheStatsHolderInterface { + private final CounterMetric entries; + private final List dimensionNames; + + public DummyCacheStatsHolder(List dimensionNames) { + this.dimensionNames = dimensionNames; + this.entries = new CounterMetric(); + } + + @Override + public void incrementHits(List dimensionValues) {} + + @Override + public void incrementMisses(List dimensionValues) {} + + @Override + public void incrementEvictions(List dimensionValues) {} + + @Override + public void incrementSizeInBytes(List dimensionValues, long amountBytes) {} + + @Override + public void decrementSizeInBytes(List dimensionValues, long amountBytes) {} + + @Override + public void incrementEntries(List dimensionValues) { + entries.inc(); + } + + @Override + public void decrementEntries(List dimensionValues) { + entries.dec(); + } + + @Override + public void reset() {} + + @Override + public long count() { + return entries.count(); + } + + @Override + public void removeDimensions(List dimensionValues) {} + + @Override + public ImmutableCacheStatsHolder getImmutableCacheStatsHolder() { + ImmutableCacheStatsHolder.Node dummyNode = new ImmutableCacheStatsHolder.Node("", null, new ImmutableCacheStats(0, 0, 0, 0, 0)); + return new ImmutableCacheStatsHolder(dummyNode, dimensionNames); + } +}