Skip to content

Commit

Permalink
Adds dummy stats holder
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <[email protected]>
  • Loading branch information
Peter Alfonsi committed Apr 16, 2024
1 parent 00df37e commit 5ba6e47
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -53,32 +53,39 @@ public List<String> 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<String> dimensionValues) {
internalIncrement(dimensionValues, Node::incrementHits, true);
}

@Override
public void incrementMisses(List<String> dimensionValues) {
internalIncrement(dimensionValues, Node::incrementMisses, true);
}

@Override
public void incrementEvictions(List<String> dimensionValues) {
internalIncrement(dimensionValues, Node::incrementEvictions, true);
}

@Override
public void incrementSizeInBytes(List<String> 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<String> dimensionValues, long amountBytes) {
internalIncrement(dimensionValues, (node) -> node.decrementSizeInBytes(amountBytes), false);
}

@Override
public void incrementEntries(List<String> dimensionValues) {
internalIncrement(dimensionValues, Node::incrementEntries, true);
}

@Override
public void decrementEntries(List<String> dimensionValues) {
internalIncrement(dimensionValues, Node::decrementEntries, false);
}
Expand All @@ -87,6 +94,7 @@ public void decrementEntries(List<String> 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);
}
Expand All @@ -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();
Expand Down Expand Up @@ -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<String> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> dimensionValues);

void incrementMisses(List<String> dimensionValues);

void incrementEvictions(List<String> dimensionValues);

void incrementSizeInBytes(List<String> dimensionValues, long amountBytes);

void decrementSizeInBytes(List<String> dimensionValues, long amountBytes);

void incrementEntries(List<String> dimensionValues);

void decrementEntries(List<String> dimensionValues);

void reset();

long count();

void removeDimensions(List<String> dimensionValues);

ImmutableCacheStatsHolder getImmutableCacheStatsHolder();
}
Original file line number Diff line number Diff line change
@@ -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<String> dimensionNames;

public DummyCacheStatsHolder(List<String> dimensionNames) {
this.dimensionNames = dimensionNames;
this.entries = new CounterMetric();
}

@Override
public void incrementHits(List<String> dimensionValues) {}

@Override
public void incrementMisses(List<String> dimensionValues) {}

@Override
public void incrementEvictions(List<String> dimensionValues) {}

@Override
public void incrementSizeInBytes(List<String> dimensionValues, long amountBytes) {}

@Override
public void decrementSizeInBytes(List<String> dimensionValues, long amountBytes) {}

@Override
public void incrementEntries(List<String> dimensionValues) {
entries.inc();
}

@Override
public void decrementEntries(List<String> dimensionValues) {
entries.dec();
}

@Override
public void reset() {}

@Override
public long count() {
return entries.count();
}

@Override
public void removeDimensions(List<String> dimensionValues) {}

@Override
public ImmutableCacheStatsHolder getImmutableCacheStatsHolder() {
ImmutableCacheStatsHolder.Node dummyNode = new ImmutableCacheStatsHolder.Node("", null, new ImmutableCacheStats(0, 0, 0, 0, 0));
return new ImmutableCacheStatsHolder(dummyNode, dimensionNames);
}
}

0 comments on commit 5ba6e47

Please sign in to comment.