Skip to content

Commit f60fb08

Browse files
author
Peter Alfonsi
committed
Renamed snapshot -> ImmutableCacheStats
Signed-off-by: Peter Alfonsi <[email protected]>
1 parent da9e485 commit f60fb08

File tree

9 files changed

+61
-63
lines changed

9 files changed

+61
-63
lines changed

plugins/cache-ehcache/src/test/java/org/opensearch/cache/store/disk/EhCacheDiskCacheTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.opensearch.common.cache.RemovalNotification;
2121
import org.opensearch.common.cache.serializer.BytesReferenceSerializer;
2222
import org.opensearch.common.cache.serializer.Serializer;
23-
import org.opensearch.common.cache.stats.CacheStatsSnapshot;
23+
import org.opensearch.common.cache.stats.ImmutableCacheStats;
2424
import org.opensearch.common.cache.store.config.CacheConfig;
2525
import org.opensearch.common.metrics.CounterMetric;
2626
import org.opensearch.common.settings.Settings;
@@ -828,7 +828,7 @@ public void testInvalidateWithDropDimensions() throws Exception {
828828

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

831-
CacheStatsSnapshot snapshot = ehCacheDiskCachingTier.stats().getStatsForDimensionValues(keyToDrop.dimensions);
831+
ImmutableCacheStats snapshot = ehCacheDiskCachingTier.stats().getStatsForDimensionValues(keyToDrop.dimensions);
832832
assertNotNull(snapshot);
833833

834834
keyToDrop.setDropStatsForDimensions(true);

server/src/main/java/org/opensearch/common/cache/stats/CacheStats.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ public void add(CacheStats other) {
5454
internalAdd(other.getHits(), other.getMisses(), other.getEvictions(), other.getSizeInBytes(), other.getEntries());
5555
}
5656

57-
public void add(CacheStatsSnapshot snapshot) {
57+
public void add(ImmutableCacheStats snapshot) {
5858
if (snapshot == null) {
5959
return;
6060
}
6161
internalAdd(snapshot.getHits(), snapshot.getMisses(), snapshot.getEvictions(), snapshot.getSizeInBytes(), snapshot.getEntries());
6262
}
6363

64-
public void subtract(CacheStatsSnapshot other) {
64+
public void subtract(ImmutableCacheStats other) {
6565
if (other == null) {
6666
return;
6767
}
@@ -126,7 +126,7 @@ public void resetSizeAndEntries() {
126126
entries = new CounterMetric();
127127
}
128128

129-
public CacheStatsSnapshot snapshot() {
130-
return new CacheStatsSnapshot(hits.count(), misses.count(), evictions.count(), sizeInBytes.count(), entries.count());
129+
public ImmutableCacheStats immutableSnapshot() {
130+
return new ImmutableCacheStats(hits.count(), misses.count(), evictions.count(), sizeInBytes.count(), entries.count());
131131
}
132132
}

server/src/main/java/org/opensearch/common/cache/stats/CacheStatsHolder.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private boolean internalIncrementHelper(
156156
* Produce an immutable version of these stats.
157157
*/
158158
public ImmutableCacheStatsHolder getImmutableCacheStatsHolder() {
159-
ImmutableCacheStatsHolder.Node snapshot = new ImmutableCacheStatsHolder.Node("", true, statsRoot.getStatsSnapshot());
159+
ImmutableCacheStatsHolder.Node snapshot = new ImmutableCacheStatsHolder.Node("", true, statsRoot.getImmutableStats());
160160
// Traverse the tree and build a corresponding tree of MDCSDimensionNode, to pass to MultiDimensionCacheStats.
161161
if (statsRoot.getChildren() != null) {
162162
for (Node child : statsRoot.getChildren().values()) {
@@ -175,9 +175,9 @@ private void getImmutableCacheStatsHelper(Node currentNodeInOriginalTree, Immuta
175175
}
176176

177177
private ImmutableCacheStatsHolder.Node createMatchingImmutableCacheStatsHolderNode(Node node) {
178-
CacheStatsSnapshot nodeSnapshot = node.getStatsSnapshot();
178+
ImmutableCacheStats immutableCacheStats = node.getImmutableStats();
179179
boolean isLeafNode = node.getChildren().isEmpty();
180-
return new ImmutableCacheStatsHolder.Node(node.getDimensionValue(), !isLeafNode, nodeSnapshot);
180+
return new ImmutableCacheStatsHolder.Node(node.getDimensionValue(), !isLeafNode, immutableCacheStats);
181181
}
182182

183183
public void removeDimensions(List<String> dimensionValues) {
@@ -192,16 +192,16 @@ public void removeDimensions(List<String> dimensionValues) {
192192
}
193193

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

284-
CacheStatsSnapshot getStatsSnapshot() {
285-
return this.stats.snapshot();
284+
ImmutableCacheStats getImmutableStats() {
285+
return this.stats.immutableSnapshot();
286286
}
287287

288-
void decrementBySnapshot(CacheStatsSnapshot snapshot) {
288+
void decrementBySnapshot(ImmutableCacheStats snapshot) {
289289
this.stats.subtract(snapshot);
290290
}
291291

server/src/main/java/org/opensearch/common/cache/stats/CacheStatsSnapshot.java renamed to server/src/main/java/org/opensearch/common/cache/stats/ImmutableCacheStats.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,27 @@
2222
* @opensearch.experimental
2323
*/
2424
@ExperimentalApi
25-
public class CacheStatsSnapshot implements Writeable { // TODO: Make this extend ToXContent (in API PR)
25+
public class ImmutableCacheStats implements Writeable { // TODO: Make this extend ToXContent (in API PR)
2626
private final long hits;
2727
private final long misses;
2828
private final long evictions;
2929
private final long sizeInBytes;
3030
private final long entries;
3131

32-
public CacheStatsSnapshot(long hits, long misses, long evictions, long sizeInBytes, long entries) {
32+
public ImmutableCacheStats(long hits, long misses, long evictions, long sizeInBytes, long entries) {
3333
this.hits = hits;
3434
this.misses = misses;
3535
this.evictions = evictions;
3636
this.sizeInBytes = sizeInBytes;
3737
this.entries = entries;
3838
}
3939

40-
public CacheStatsSnapshot(StreamInput in) throws IOException {
40+
public ImmutableCacheStats(StreamInput in) throws IOException {
4141
this(in.readVLong(), in.readVLong(), in.readVLong(), in.readVLong(), in.readVLong());
4242
}
4343

44-
public static CacheStatsSnapshot addSnapshots(CacheStatsSnapshot s1, CacheStatsSnapshot s2) {
45-
return new CacheStatsSnapshot(
44+
public static ImmutableCacheStats addSnapshots(ImmutableCacheStats s1, ImmutableCacheStats s2) {
45+
return new ImmutableCacheStats(
4646
s1.hits + s2.hits,
4747
s1.misses + s2.misses,
4848
s1.evictions + s2.evictions,
@@ -85,10 +85,10 @@ public boolean equals(Object o) {
8585
if (o == null) {
8686
return false;
8787
}
88-
if (o.getClass() != CacheStatsSnapshot.class) {
88+
if (o.getClass() != ImmutableCacheStats.class) {
8989
return false;
9090
}
91-
CacheStatsSnapshot other = (CacheStatsSnapshot) o;
91+
ImmutableCacheStats other = (ImmutableCacheStats) o;
9292
return (hits == other.hits)
9393
&& (misses == other.misses)
9494
&& (evictions == other.evictions)

server/src/main/java/org/opensearch/common/cache/stats/ImmutableCacheStatsHolder.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

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

36-
public CacheStatsSnapshot getTotalStats() {
36+
public ImmutableCacheStats getTotalStats() {
3737
return statsRoot.getStats();
3838
}
3939

@@ -57,7 +57,7 @@ public long getTotalEntries() {
5757
return getTotalStats().getEntries();
5858
}
5959

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

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

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

95-
public CacheStatsSnapshot getStats() {
95+
public ImmutableCacheStats getStats() {
9696
return stats;
9797
}
9898

99-
public void setStats(CacheStatsSnapshot stats) {
99+
public void setStats(ImmutableCacheStats stats) {
100100
this.stats = stats;
101101
}
102102

server/src/test/java/org/opensearch/common/cache/stats/CacheStatsHolderTests.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ public void testAddAndGet() throws Exception {
3232
for (List<String> dimensionValues : expected.keySet()) {
3333
CacheStats expectedCounter = expected.get(dimensionValues);
3434

35-
CacheStatsSnapshot actualStatsHolder = CacheStatsHolderTests.getNode(dimensionValues, cacheStatsHolder.getStatsRoot())
36-
.getStatsSnapshot();
37-
CacheStatsSnapshot actualCacheStats = getNode(dimensionValues, cacheStatsHolder.getStatsRoot()).getStatsSnapshot();
35+
ImmutableCacheStats actualStatsHolder = CacheStatsHolderTests.getNode(dimensionValues, cacheStatsHolder.getStatsRoot())
36+
.getImmutableStats();
37+
ImmutableCacheStats actualCacheStats = getNode(dimensionValues, cacheStatsHolder.getStatsRoot()).getImmutableStats();
3838

39-
assertEquals(expectedCounter.snapshot(), actualStatsHolder);
40-
assertEquals(expectedCounter.snapshot(), actualCacheStats);
39+
assertEquals(expectedCounter.immutableSnapshot(), actualStatsHolder);
40+
assertEquals(expectedCounter.immutableSnapshot(), actualCacheStats);
4141
}
4242

4343
// Check overall total matches
4444
CacheStats expectedTotal = new CacheStats();
4545
for (List<String> dims : expected.keySet()) {
4646
expectedTotal.add(expected.get(dims));
4747
}
48-
assertEquals(expectedTotal.snapshot(), cacheStatsHolder.getStatsRoot().getStatsSnapshot());
48+
assertEquals(expectedTotal.immutableSnapshot(), cacheStatsHolder.getStatsRoot().getImmutableStats());
4949

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

6767
CacheStatsHolder.Node node = getNode(dimensionValues, cacheStatsHolder.getStatsRoot());
68-
CacheStatsSnapshot actual = node.getStatsSnapshot();
69-
assertEquals(originalCounter.snapshot(), actual);
68+
ImmutableCacheStats actual = node.getImmutableStats();
69+
assertEquals(originalCounter.immutableSnapshot(), actual);
7070
}
7171
}
7272

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

83-
assertEquals(3, cacheStatsHolder.getStatsRoot().getStatsSnapshot().getHits());
83+
assertEquals(3, cacheStatsHolder.getStatsRoot().getImmutableStats().getHits());
8484

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

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

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

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

98-
assertEquals(1, cacheStatsHolder.getStatsRoot().getStatsSnapshot().getHits());
98+
assertEquals(1, cacheStatsHolder.getStatsRoot().getImmutableStats().getHits());
9999
assertNull(getNode(List.of("A1", "B1"), cacheStatsHolder.getStatsRoot()));
100100
assertNull(getNode(List.of("A1"), cacheStatsHolder.getStatsRoot()));
101101

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

104104
cacheStatsHolder.removeDimensions(List.of("A2", "B3"));
105-
assertEquals(0, cacheStatsHolder.getStatsRoot().getStatsSnapshot().getHits());
105+
assertEquals(0, cacheStatsHolder.getStatsRoot().getImmutableStats().getHits());
106106
assertEquals(0, cacheStatsHolder.getStatsRoot().children.size());
107107
}
108108

@@ -156,12 +156,12 @@ public void testConcurrentRemoval() throws Exception {
156156
assertNull(getNode(List.of("A1"), cacheStatsHolder.getStatsRoot()));
157157
assertNotNull(getNode(List.of("A2", "B2"), cacheStatsHolder.getStatsRoot()));
158158
assertEquals(
159-
new CacheStatsSnapshot(0, 1, 0, 0, 0),
160-
getNode(List.of("A2", "B2"), cacheStatsHolder.getStatsRoot()).getStatsSnapshot()
159+
new ImmutableCacheStats(0, 1, 0, 0, 0),
160+
getNode(List.of("A2", "B2"), cacheStatsHolder.getStatsRoot()).getImmutableStats()
161161
);
162162
assertEquals(
163-
new CacheStatsSnapshot(1, 1, 0, 0, 0),
164-
getNode(List.of("A2", "B3"), cacheStatsHolder.getStatsRoot()).getStatsSnapshot()
163+
new ImmutableCacheStats(1, 1, 0, 0, 0),
164+
getNode(List.of("A2", "B3"), cacheStatsHolder.getStatsRoot()).getImmutableStats()
165165
);
166166
}
167167

@@ -256,9 +256,9 @@ private void assertSumOfChildrenStats(CacheStatsHolder.Node current) {
256256
if (!current.children.isEmpty()) {
257257
CacheStats expectedTotal = new CacheStats();
258258
for (CacheStatsHolder.Node child : current.children.values()) {
259-
expectedTotal.add(child.getStatsSnapshot());
259+
expectedTotal.add(child.getImmutableStats());
260260
}
261-
assertEquals(expectedTotal.snapshot(), current.getStatsSnapshot());
261+
assertEquals(expectedTotal.immutableSnapshot(), current.getImmutableStats());
262262
for (CacheStatsHolder.Node child : current.children.values()) {
263263
assertSumOfChildrenStats(child);
264264
}

server/src/test/java/org/opensearch/common/cache/stats/ImmutableCacheStatsHolderTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ public void testGet() throws Exception {
2626
for (List<String> dimensionValues : expected.keySet()) {
2727
CacheStats expectedCounter = expected.get(dimensionValues);
2828

29-
CacheStatsSnapshot actualStatsHolder = CacheStatsHolderTests.getNode(dimensionValues, cacheStatsHolder.getStatsRoot())
30-
.getStatsSnapshot();
31-
CacheStatsSnapshot actualCacheStats = getNode(dimensionValues, stats.getStatsRoot()).getStats();
29+
ImmutableCacheStats actualStatsHolder = CacheStatsHolderTests.getNode(dimensionValues, cacheStatsHolder.getStatsRoot())
30+
.getImmutableStats();
31+
ImmutableCacheStats actualCacheStats = getNode(dimensionValues, stats.getStatsRoot()).getStats();
3232

33-
assertEquals(expectedCounter.snapshot(), actualStatsHolder);
34-
assertEquals(expectedCounter.snapshot(), actualCacheStats);
33+
assertEquals(expectedCounter.immutableSnapshot(), actualStatsHolder);
34+
assertEquals(expectedCounter.immutableSnapshot(), actualCacheStats);
3535
}
3636

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

4444
assertEquals(expectedTotal.getHits(), stats.getTotalHits());
4545
assertEquals(expectedTotal.getMisses(), stats.getTotalMisses());
@@ -79,7 +79,7 @@ private void assertSumOfChildrenStats(ImmutableCacheStatsHolder.Node current) {
7979
for (ImmutableCacheStatsHolder.Node child : current.children.values()) {
8080
expectedTotal.add(child.getStats());
8181
}
82-
assertEquals(expectedTotal.snapshot(), current.getStats());
82+
assertEquals(expectedTotal.immutableSnapshot(), current.getStats());
8383
for (ImmutableCacheStatsHolder.Node child : current.children.values()) {
8484
assertSumOfChildrenStats(child);
8585
}

server/src/test/java/org/opensearch/common/cache/store/OpenSearchOnHeapCacheTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import org.opensearch.common.cache.LoadAwareCacheLoader;
1616
import org.opensearch.common.cache.RemovalListener;
1717
import org.opensearch.common.cache.RemovalNotification;
18-
import org.opensearch.common.cache.stats.CacheStatsSnapshot;
18+
import org.opensearch.common.cache.stats.ImmutableCacheStats;
1919
import org.opensearch.common.cache.store.config.CacheConfig;
2020
import org.opensearch.common.cache.store.settings.OpenSearchOnHeapCacheSettings;
2121
import org.opensearch.common.metrics.CounterMetric;
@@ -114,7 +114,7 @@ public void testInvalidateWithDropDimensions() throws Exception {
114114

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

117-
CacheStatsSnapshot snapshot = cache.stats().getStatsForDimensionValues(keyToDrop.dimensions);
117+
ImmutableCacheStats snapshot = cache.stats().getStatsForDimensionValues(keyToDrop.dimensions);
118118
assertNotNull(snapshot);
119119

120120
keyToDrop.setDropStatsForDimensions(true);

0 commit comments

Comments
 (0)