Skip to content

Commit

Permalink
Added reset() to CacheStats
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <petealft@amazon.com>
  • Loading branch information
Peter Alfonsi committed Mar 1, 2024
1 parent 2483981 commit 2aeaa53
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ public interface CacheStats extends Writeable {
void incrementEntriesByDimensions(List<CacheStatsDimension> dimensions);
void decrementEntriesByDimensions(List<CacheStatsDimension> dimensions);

// Resets memory and entries stats but leaves the others; called when the cache clears itself.
void reset();

}
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,15 @@ public void decrementEntriesByDimensions(List<CacheStatsDimension> dimensions) {
internalIncrement(dimensions, (response, amount) -> response.entries.inc(amount), -1);
}

@Override
public void reset() {
for (Key key : map.keySet()) {
CacheStatsResponse response = map.get(key);
response.memorySize.dec(response.getMemorySize());
response.entries.dec(response.getEntries());
}
}

private CacheStatsResponse internalGetStats(List<CacheStatsDimension> dimensions) {
assert dimensions.size() == dimensionNames.size();
CacheStatsResponse response = map.get(new Key(dimensions));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void invalidate(ICacheKey<K> key) {
@Override
public void invalidateAll() {
cache.invalidateAll();
stats = new MultiDimensionCacheStats(dimensionNames, TIER_DIMENSION_VALUE);
stats.reset();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.opensearch.common.Randomness;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.metrics.CounterMetric;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.io.stream.BytesStreamInput;
import org.opensearch.test.OpenSearchTestCase;
Expand Down Expand Up @@ -166,6 +167,30 @@ public void testKeyEquality() throws Exception {
assertEquals(key1.hashCode(), key2.hashCode());
}

public void testReset() throws Exception {
List<String> dimensionNames = List.of("dim1", "dim2");
MultiDimensionCacheStats stats = new MultiDimensionCacheStats(dimensionNames, tierDimensionValue);
Map<String, List<String>> usedDimensionValues = getUsedDimensionValues(stats, 10);
Map<Set<CacheStatsDimension>, CacheStatsResponse> expected = populateStats(stats, usedDimensionValues, 100, 10);

stats.reset();

for (Set<CacheStatsDimension> dimSet : expected.keySet()) {
List<CacheStatsDimension> dims = new ArrayList<>(dimSet);
CacheStatsResponse originalResponse = expected.get(dimSet);
originalResponse.memorySize = new CounterMetric();
originalResponse.entries = new CounterMetric();
CacheStatsResponse actual = stats.getStatsByDimensions(dims);
assertEquals(originalResponse, actual);

assertEquals(originalResponse.getHits(), stats.getHitsByDimensions(dims));
assertEquals(originalResponse.getMisses(), stats.getMissesByDimensions(dims));
assertEquals(originalResponse.getEvictions(), stats.getEvictionsByDimensions(dims));
assertEquals(originalResponse.getMemorySize(), stats.getMemorySizeByDimensions(dims));
assertEquals(originalResponse.getEntries(), stats.getEntriesByDimensions(dims));
}
}

private Map<String, List<String>> getUsedDimensionValues(MultiDimensionCacheStats stats, int numValuesPerDim) {
Map<String, List<String>> usedDimensionValues = new HashMap<>();
for (int i = 0; i < stats.dimensionNames.size(); i++) {
Expand Down

0 comments on commit 2aeaa53

Please sign in to comment.