Skip to content

Commit

Permalink
Added overall CacheStatsResponse object packaging all 5 metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <[email protected]>
  • Loading branch information
Peter Alfonsi committed Feb 14, 2024
1 parent 6059680 commit 7f5a455
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

package org.opensearch.common.cache.stats;

import org.opensearch.core.xcontent.ToXContentFragment;
import org.opensearch.core.common.io.stream.Writeable;

import java.util.List;

Expand All @@ -18,7 +18,28 @@
* When updating stats, we take in the list of dimensions associated with the key/value pair that caused the update.
* This allows us to aggregate stats by dimension when accessing them.
*/
public interface CacheStats extends CacheStatsBase { // TODO: Make this extend ToXContentFragment too
public interface CacheStats extends Writeable {

// Methods to get all 5 values at once, either in total or for a specific set of dimensions.
CacheStatsResponse getTotalStats();
CacheStatsResponse getStatsByDimensions(List<CacheStatsDimension> dimensions);

// Methods to get total values.
long getTotalHits();
long getTotalMisses();
long getTotalEvictions();
long getTotalMemorySize();
long getTotalEntries();

// Methods to get values for a specific set of dimensions.
// Returns the sum of values for cache entries that match all dimensions in the list.
long getHitsByDimensions(List<CacheStatsDimension> dimensions);
long getMissesByDimensions(List<CacheStatsDimension> dimensions);
long getEvictionsByDimensions(List<CacheStatsDimension> dimensions);
long getMemorySizeByDimensions(List<CacheStatsDimension> dimensions);
long getEntriesByDimensions(List<CacheStatsDimension> dimensions);


void incrementHitsByDimensions(List<CacheStatsDimension> dimensions);
void incrementMissesByDimensions(List<CacheStatsDimension> dimensions);
void incrementEvictionsByDimensions(List<CacheStatsDimension> dimensions);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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;

/**
* A class containing the 5 metrics tracked by a CacheStats object.
*/
public class CacheStatsResponse { // TODO: Make this extend ToXContent.
public final long hits;
public final long misses;
public final long evictions;
public final long memorySize;
public final long entries;

public CacheStatsResponse(long hits, long misses, long evictions, long memorySize, long entries) {
this.hits = hits;
this.misses = misses;
this.evictions = evictions;
this.memorySize = memorySize;
this.entries = entries;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ public SingleDimensionCacheStats(StreamInput in) throws IOException {
this.tierDimensionValue = in.readString();
}

@Override
public CacheStatsResponse getTotalStats() {
return new CacheStatsResponse(getTotalHits(), getTotalMisses(), getTotalEvictions(), getTotalMemorySize(), getTotalEntries());
}

@Override
public CacheStatsResponse getStatsByDimensions(List<CacheStatsDimension> dimensions) {
return new CacheStatsResponse(
getHitsByDimensions(dimensions),
getMissesByDimensions(dimensions),
getEvictionsByDimensions(dimensions),
getMemorySizeByDimensions(dimensions),
getEntriesByDimensions(dimensions)
);
}

@Override
public long getTotalHits() {
return this.totalHits.count();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.opensearch.common.cache.stats.CacheStats;
import org.opensearch.common.cache.stats.CacheStatsDimension;
import org.opensearch.common.cache.stats.CacheStatsResponse;
import org.opensearch.core.common.io.stream.StreamOutput;

import java.io.IOException;
Expand All @@ -30,6 +31,16 @@ public void writeTo(StreamOutput out) throws IOException {

}

@Override
public CacheStatsResponse getTotalStats() {
return null;
}

@Override
public CacheStatsResponse getStatsByDimensions(List<CacheStatsDimension> dimensions) {
return null;
}

@Override
public long getTotalHits() {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,23 +170,42 @@ private void checkShardResults(StatsAndExpectedResults statsAndExpectedResults)
String shardIdString = String.valueOf(shardId);
CacheStatsDimension dimension = getDim(shardId);

// Check the individual metric getters
assertEquals((long) expectedResults.get("hits").get(shardIdString), stats.getHitsByDimensions(List.of(dimension)));
assertEquals((long) expectedResults.get("misses").get(shardIdString), stats.getMissesByDimensions(List.of(dimension)));
assertEquals((long) expectedResults.get("evictions").get(shardIdString), stats.getEvictionsByDimensions(List.of(dimension)));
assertEquals((long) expectedResults.get("memory_size").get(shardIdString), stats.getMemorySizeByDimensions(List.of(dimension)));
assertEquals((long) expectedResults.get("entries").get(shardIdString), stats.getEntriesByDimensions(List.of(dimension)));

// Check the total metric getter
CacheStatsResponse response = stats.getStatsByDimensions(List.of(dimension));
assertEquals((long) expectedResults.get("hits").get(shardIdString), response.hits);
assertEquals((long) expectedResults.get("misses").get(shardIdString), response.misses);
assertEquals((long) expectedResults.get("evictions").get(shardIdString), response.evictions);
assertEquals((long) expectedResults.get("memory_size").get(shardIdString), response.memorySize);
assertEquals((long) expectedResults.get("entries").get(shardIdString), response.entries);
}
}

private void checkTotalResults(StatsAndExpectedResults statsAndExpectedResults) {
// check resulting total values are what we expect
Map<String, Map<String, Long>> expectedResults = statsAndExpectedResults.expectedShardResults;
SingleDimensionCacheStats stats = statsAndExpectedResults.stats;

// Check the individual metric getters
assertEquals(sumMap(expectedResults.get("hits")), stats.getTotalHits());
assertEquals(sumMap(expectedResults.get("misses")), stats.getTotalMisses());
assertEquals(sumMap(expectedResults.get("evictions")), stats.getTotalEvictions());
assertEquals(sumMap(expectedResults.get("memory_size")), stats.getTotalMemorySize());
assertEquals(sumMap(expectedResults.get("entries")), stats.getTotalEntries());

// Check the total metric getter
CacheStatsResponse totalResponse = stats.getTotalStats();
assertEquals(sumMap(expectedResults.get("hits")), totalResponse.hits);
assertEquals(sumMap(expectedResults.get("misses")), totalResponse.misses);
assertEquals(sumMap(expectedResults.get("evictions")), totalResponse.evictions);
assertEquals(sumMap(expectedResults.get("memory_size")), totalResponse.memorySize);
assertEquals(sumMap(expectedResults.get("entries")), totalResponse.entries);
}

// Convenience class to allow reusing setup code across tests
Expand Down

0 comments on commit 7f5a455

Please sign in to comment.