Skip to content

Commit

Permalink
Renamed snapshot -> ImmutableCacheStats
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 Apr 12, 2024
1 parent da9e485 commit f60fb08
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.opensearch.common.cache.RemovalNotification;
import org.opensearch.common.cache.serializer.BytesReferenceSerializer;
import org.opensearch.common.cache.serializer.Serializer;
import org.opensearch.common.cache.stats.CacheStatsSnapshot;
import org.opensearch.common.cache.stats.ImmutableCacheStats;
import org.opensearch.common.cache.store.config.CacheConfig;
import org.opensearch.common.metrics.CounterMetric;
import org.opensearch.common.settings.Settings;
Expand Down Expand Up @@ -828,7 +828,7 @@ public void testInvalidateWithDropDimensions() throws Exception {

ICacheKey<String> keyToDrop = keysAdded.get(0);

CacheStatsSnapshot snapshot = ehCacheDiskCachingTier.stats().getStatsForDimensionValues(keyToDrop.dimensions);
ImmutableCacheStats snapshot = ehCacheDiskCachingTier.stats().getStatsForDimensionValues(keyToDrop.dimensions);
assertNotNull(snapshot);

keyToDrop.setDropStatsForDimensions(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public void add(CacheStats other) {
internalAdd(other.getHits(), other.getMisses(), other.getEvictions(), other.getSizeInBytes(), other.getEntries());
}

public void add(CacheStatsSnapshot snapshot) {
public void add(ImmutableCacheStats snapshot) {
if (snapshot == null) {
return;
}
internalAdd(snapshot.getHits(), snapshot.getMisses(), snapshot.getEvictions(), snapshot.getSizeInBytes(), snapshot.getEntries());
}

public void subtract(CacheStatsSnapshot other) {
public void subtract(ImmutableCacheStats other) {
if (other == null) {
return;
}
Expand Down Expand Up @@ -126,7 +126,7 @@ public void resetSizeAndEntries() {
entries = new CounterMetric();
}

public CacheStatsSnapshot snapshot() {
return new CacheStatsSnapshot(hits.count(), misses.count(), evictions.count(), sizeInBytes.count(), entries.count());
public ImmutableCacheStats immutableSnapshot() {
return new ImmutableCacheStats(hits.count(), misses.count(), evictions.count(), sizeInBytes.count(), entries.count());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private boolean internalIncrementHelper(
* Produce an immutable version of these stats.
*/
public ImmutableCacheStatsHolder getImmutableCacheStatsHolder() {
ImmutableCacheStatsHolder.Node snapshot = new ImmutableCacheStatsHolder.Node("", true, statsRoot.getStatsSnapshot());
ImmutableCacheStatsHolder.Node snapshot = new ImmutableCacheStatsHolder.Node("", true, statsRoot.getImmutableStats());
// Traverse the tree and build a corresponding tree of MDCSDimensionNode, to pass to MultiDimensionCacheStats.
if (statsRoot.getChildren() != null) {
for (Node child : statsRoot.getChildren().values()) {
Expand All @@ -175,9 +175,9 @@ private void getImmutableCacheStatsHelper(Node currentNodeInOriginalTree, Immuta
}

private ImmutableCacheStatsHolder.Node createMatchingImmutableCacheStatsHolderNode(Node node) {
CacheStatsSnapshot nodeSnapshot = node.getStatsSnapshot();
ImmutableCacheStats immutableCacheStats = node.getImmutableStats();
boolean isLeafNode = node.getChildren().isEmpty();
return new ImmutableCacheStatsHolder.Node(node.getDimensionValue(), !isLeafNode, nodeSnapshot);
return new ImmutableCacheStatsHolder.Node(node.getDimensionValue(), !isLeafNode, immutableCacheStats);
}

public void removeDimensions(List<String> dimensionValues) {
Expand All @@ -192,16 +192,16 @@ public void removeDimensions(List<String> dimensionValues) {
}

// Returns a CacheStatsCounterSnapshot object for the stats to decrement if the removal happened, null otherwise.
private CacheStatsSnapshot removeDimensionsHelper(List<String> dimensionValues, Node node, int depth) {
private ImmutableCacheStats removeDimensionsHelper(List<String> dimensionValues, Node node, int depth) {
if (depth == dimensionValues.size()) {
// Pass up a snapshot of the original stats to avoid issues when the original is decremented by other fn invocations
return node.getStatsSnapshot();
return node.getImmutableStats();
}
Node child = node.getChild(dimensionValues.get(depth));
if (child == null) {
return null;
}
CacheStatsSnapshot statsToDecrement = removeDimensionsHelper(dimensionValues, child, depth + 1);
ImmutableCacheStats statsToDecrement = removeDimensionsHelper(dimensionValues, child, depth + 1);
if (statsToDecrement != null) {
// The removal took place, decrement values and remove this node from its parent if it's now empty
node.decrementBySnapshot(statsToDecrement);
Expand Down Expand Up @@ -281,11 +281,11 @@ long getEntries() {
return this.stats.getEntries();
}

CacheStatsSnapshot getStatsSnapshot() {
return this.stats.snapshot();
ImmutableCacheStats getImmutableStats() {
return this.stats.immutableSnapshot();
}

void decrementBySnapshot(CacheStatsSnapshot snapshot) {
void decrementBySnapshot(ImmutableCacheStats snapshot) {
this.stats.subtract(snapshot);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@
* @opensearch.experimental
*/
@ExperimentalApi
public class CacheStatsSnapshot implements Writeable { // TODO: Make this extend ToXContent (in API PR)
public class ImmutableCacheStats implements Writeable { // TODO: Make this extend ToXContent (in API PR)
private final long hits;
private final long misses;
private final long evictions;
private final long sizeInBytes;
private final long entries;

public CacheStatsSnapshot(long hits, long misses, long evictions, long sizeInBytes, long entries) {
public ImmutableCacheStats(long hits, long misses, long evictions, long sizeInBytes, long entries) {
this.hits = hits;
this.misses = misses;
this.evictions = evictions;
this.sizeInBytes = sizeInBytes;
this.entries = entries;
}

public CacheStatsSnapshot(StreamInput in) throws IOException {
public ImmutableCacheStats(StreamInput in) throws IOException {
this(in.readVLong(), in.readVLong(), in.readVLong(), in.readVLong(), in.readVLong());
}

public static CacheStatsSnapshot addSnapshots(CacheStatsSnapshot s1, CacheStatsSnapshot s2) {
return new CacheStatsSnapshot(
public static ImmutableCacheStats addSnapshots(ImmutableCacheStats s1, ImmutableCacheStats s2) {
return new ImmutableCacheStats(
s1.hits + s2.hits,
s1.misses + s2.misses,
s1.evictions + s2.evictions,
Expand Down Expand Up @@ -85,10 +85,10 @@ public boolean equals(Object o) {
if (o == null) {
return false;
}
if (o.getClass() != CacheStatsSnapshot.class) {
if (o.getClass() != ImmutableCacheStats.class) {
return false;
}
CacheStatsSnapshot other = (CacheStatsSnapshot) o;
ImmutableCacheStats other = (ImmutableCacheStats) o;
return (hits == other.hits)
&& (misses == other.misses)
&& (evictions == other.evictions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

@ExperimentalApi
public class ImmutableCacheStatsHolder { // TODO: extends Writeable, ToXContent
// A snapshot of a StatsHolder containing stats maintained by the cache.
// An immutable snapshot of a stats within a CacheStatsHolder, containing all the stats maintained by the cache.
// Pkg-private for testing.
final Node statsRoot;
final List<String> dimensionNames;
Expand All @@ -33,7 +33,7 @@ public ImmutableCacheStatsHolder(Node statsRoot, List<String> dimensionNames) {
this.dimensionNames = dimensionNames;
}

public CacheStatsSnapshot getTotalStats() {
public ImmutableCacheStats getTotalStats() {
return statsRoot.getStats();
}

Expand All @@ -57,7 +57,7 @@ public long getTotalEntries() {
return getTotalStats().getEntries();
}

public CacheStatsSnapshot getStatsForDimensionValues(List<String> dimensionValues) {
public ImmutableCacheStats getStatsForDimensionValues(List<String> dimensionValues) {
Node current = statsRoot;
for (String dimensionValue : dimensionValues) {
current = current.children.get(dimensionValue);
Expand All @@ -75,10 +75,10 @@ static class Node {

// The stats for this node. If a leaf node, corresponds to the stats for this combination of dimensions; if not,
// contains the sum of its children's stats.
private CacheStatsSnapshot stats;
private ImmutableCacheStats stats;
private static final Map<String, Node> EMPTY_CHILDREN_MAP = new HashMap<>();

Node(String dimensionValue, boolean createChildrenMap, CacheStatsSnapshot stats) {
Node(String dimensionValue, boolean createChildrenMap, ImmutableCacheStats stats) {
this.dimensionValue = dimensionValue;
if (createChildrenMap) {
this.children = new TreeMap<>(); // This map should be ordered to enforce a consistent order in API response
Expand All @@ -92,11 +92,11 @@ Map<String, Node> getChildren() {
return children;
}

public CacheStatsSnapshot getStats() {
public ImmutableCacheStats getStats() {
return stats;
}

public void setStats(CacheStatsSnapshot stats) {
public void setStats(ImmutableCacheStats stats) {
this.stats = stats;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ public void testAddAndGet() throws Exception {
for (List<String> dimensionValues : expected.keySet()) {
CacheStats expectedCounter = expected.get(dimensionValues);

CacheStatsSnapshot actualStatsHolder = CacheStatsHolderTests.getNode(dimensionValues, cacheStatsHolder.getStatsRoot())
.getStatsSnapshot();
CacheStatsSnapshot actualCacheStats = getNode(dimensionValues, cacheStatsHolder.getStatsRoot()).getStatsSnapshot();
ImmutableCacheStats actualStatsHolder = CacheStatsHolderTests.getNode(dimensionValues, cacheStatsHolder.getStatsRoot())
.getImmutableStats();
ImmutableCacheStats actualCacheStats = getNode(dimensionValues, cacheStatsHolder.getStatsRoot()).getImmutableStats();

assertEquals(expectedCounter.snapshot(), actualStatsHolder);
assertEquals(expectedCounter.snapshot(), actualCacheStats);
assertEquals(expectedCounter.immutableSnapshot(), actualStatsHolder);
assertEquals(expectedCounter.immutableSnapshot(), actualCacheStats);
}

// Check overall total matches
CacheStats expectedTotal = new CacheStats();
for (List<String> dims : expected.keySet()) {
expectedTotal.add(expected.get(dims));
}
assertEquals(expectedTotal.snapshot(), cacheStatsHolder.getStatsRoot().getStatsSnapshot());
assertEquals(expectedTotal.immutableSnapshot(), cacheStatsHolder.getStatsRoot().getImmutableStats());

// Check sum of children stats are correct
assertSumOfChildrenStats(cacheStatsHolder.getStatsRoot());
Expand All @@ -65,8 +65,8 @@ public void testReset() throws Exception {
originalCounter.entries = new CounterMetric();

CacheStatsHolder.Node node = getNode(dimensionValues, cacheStatsHolder.getStatsRoot());
CacheStatsSnapshot actual = node.getStatsSnapshot();
assertEquals(originalCounter.snapshot(), actual);
ImmutableCacheStats actual = node.getImmutableStats();
assertEquals(originalCounter.immutableSnapshot(), actual);
}
}

Expand All @@ -80,13 +80,13 @@ public void testDropStatsForDimensions() throws Exception {
cacheStatsHolder.incrementHits(dims);
}

assertEquals(3, cacheStatsHolder.getStatsRoot().getStatsSnapshot().getHits());
assertEquals(3, cacheStatsHolder.getStatsRoot().getImmutableStats().getHits());

// When we invalidate A2, B2, we should lose the node for B2, but not B3 or A2.

cacheStatsHolder.removeDimensions(List.of("A2", "B2"));

assertEquals(2, cacheStatsHolder.getStatsRoot().getStatsSnapshot().getHits());
assertEquals(2, cacheStatsHolder.getStatsRoot().getImmutableStats().getHits());
assertNull(getNode(List.of("A2", "B2"), cacheStatsHolder.getStatsRoot()));
assertNotNull(getNode(List.of("A2"), cacheStatsHolder.getStatsRoot()));
assertNotNull(getNode(List.of("A2", "B3"), cacheStatsHolder.getStatsRoot()));
Expand All @@ -95,14 +95,14 @@ public void testDropStatsForDimensions() throws Exception {

cacheStatsHolder.removeDimensions(List.of("A1", "B1"));

assertEquals(1, cacheStatsHolder.getStatsRoot().getStatsSnapshot().getHits());
assertEquals(1, cacheStatsHolder.getStatsRoot().getImmutableStats().getHits());
assertNull(getNode(List.of("A1", "B1"), cacheStatsHolder.getStatsRoot()));
assertNull(getNode(List.of("A1"), cacheStatsHolder.getStatsRoot()));

// When we invalidate the last node, all nodes should be deleted except the root node

cacheStatsHolder.removeDimensions(List.of("A2", "B3"));
assertEquals(0, cacheStatsHolder.getStatsRoot().getStatsSnapshot().getHits());
assertEquals(0, cacheStatsHolder.getStatsRoot().getImmutableStats().getHits());
assertEquals(0, cacheStatsHolder.getStatsRoot().children.size());
}

Expand Down Expand Up @@ -156,12 +156,12 @@ public void testConcurrentRemoval() throws Exception {
assertNull(getNode(List.of("A1"), cacheStatsHolder.getStatsRoot()));
assertNotNull(getNode(List.of("A2", "B2"), cacheStatsHolder.getStatsRoot()));
assertEquals(
new CacheStatsSnapshot(0, 1, 0, 0, 0),
getNode(List.of("A2", "B2"), cacheStatsHolder.getStatsRoot()).getStatsSnapshot()
new ImmutableCacheStats(0, 1, 0, 0, 0),
getNode(List.of("A2", "B2"), cacheStatsHolder.getStatsRoot()).getImmutableStats()
);
assertEquals(
new CacheStatsSnapshot(1, 1, 0, 0, 0),
getNode(List.of("A2", "B3"), cacheStatsHolder.getStatsRoot()).getStatsSnapshot()
new ImmutableCacheStats(1, 1, 0, 0, 0),
getNode(List.of("A2", "B3"), cacheStatsHolder.getStatsRoot()).getImmutableStats()
);
}

Expand Down Expand Up @@ -256,9 +256,9 @@ private void assertSumOfChildrenStats(CacheStatsHolder.Node current) {
if (!current.children.isEmpty()) {
CacheStats expectedTotal = new CacheStats();
for (CacheStatsHolder.Node child : current.children.values()) {
expectedTotal.add(child.getStatsSnapshot());
expectedTotal.add(child.getImmutableStats());
}
assertEquals(expectedTotal.snapshot(), current.getStatsSnapshot());
assertEquals(expectedTotal.immutableSnapshot(), current.getImmutableStats());
for (CacheStatsHolder.Node child : current.children.values()) {
assertSumOfChildrenStats(child);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ public void testGet() throws Exception {
for (List<String> dimensionValues : expected.keySet()) {
CacheStats expectedCounter = expected.get(dimensionValues);

CacheStatsSnapshot actualStatsHolder = CacheStatsHolderTests.getNode(dimensionValues, cacheStatsHolder.getStatsRoot())
.getStatsSnapshot();
CacheStatsSnapshot actualCacheStats = getNode(dimensionValues, stats.getStatsRoot()).getStats();
ImmutableCacheStats actualStatsHolder = CacheStatsHolderTests.getNode(dimensionValues, cacheStatsHolder.getStatsRoot())
.getImmutableStats();
ImmutableCacheStats actualCacheStats = getNode(dimensionValues, stats.getStatsRoot()).getStats();

assertEquals(expectedCounter.snapshot(), actualStatsHolder);
assertEquals(expectedCounter.snapshot(), actualCacheStats);
assertEquals(expectedCounter.immutableSnapshot(), actualStatsHolder);
assertEquals(expectedCounter.immutableSnapshot(), actualCacheStats);
}

// test gets for total (this also checks sum-of-children logic)
CacheStats expectedTotal = new CacheStats();
for (List<String> dims : expected.keySet()) {
expectedTotal.add(expected.get(dims));
}
assertEquals(expectedTotal.snapshot(), stats.getTotalStats());
assertEquals(expectedTotal.immutableSnapshot(), stats.getTotalStats());

assertEquals(expectedTotal.getHits(), stats.getTotalHits());
assertEquals(expectedTotal.getMisses(), stats.getTotalMisses());
Expand Down Expand Up @@ -79,7 +79,7 @@ private void assertSumOfChildrenStats(ImmutableCacheStatsHolder.Node current) {
for (ImmutableCacheStatsHolder.Node child : current.children.values()) {
expectedTotal.add(child.getStats());
}
assertEquals(expectedTotal.snapshot(), current.getStats());
assertEquals(expectedTotal.immutableSnapshot(), current.getStats());
for (ImmutableCacheStatsHolder.Node child : current.children.values()) {
assertSumOfChildrenStats(child);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.opensearch.common.cache.LoadAwareCacheLoader;
import org.opensearch.common.cache.RemovalListener;
import org.opensearch.common.cache.RemovalNotification;
import org.opensearch.common.cache.stats.CacheStatsSnapshot;
import org.opensearch.common.cache.stats.ImmutableCacheStats;
import org.opensearch.common.cache.store.config.CacheConfig;
import org.opensearch.common.cache.store.settings.OpenSearchOnHeapCacheSettings;
import org.opensearch.common.metrics.CounterMetric;
Expand Down Expand Up @@ -114,7 +114,7 @@ public void testInvalidateWithDropDimensions() throws Exception {

ICacheKey<String> keyToDrop = keysAdded.get(0);

CacheStatsSnapshot snapshot = cache.stats().getStatsForDimensionValues(keyToDrop.dimensions);
ImmutableCacheStats snapshot = cache.stats().getStatsForDimensionValues(keyToDrop.dimensions);
assertNotNull(snapshot);

keyToDrop.setDropStatsForDimensions(true);
Expand Down
Loading

0 comments on commit f60fb08

Please sign in to comment.