Skip to content

Commit 2f59ee7

Browse files
author
Peter Alfonsi
committed
Renamed classes to make more sense, removed interface
Signed-off-by: Peter Alfonsi <petealft@amazon.com>
1 parent c0b3dd2 commit 2f59ee7

File tree

18 files changed

+421
-486
lines changed

18 files changed

+421
-486
lines changed

modules/cache-common/src/main/java/org/opensearch/cache/common/tier/TieredSpilloverCache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.opensearch.common.cache.RemovalListener;
1818
import org.opensearch.common.cache.RemovalNotification;
1919
import org.opensearch.common.cache.policy.CachedQueryResult;
20-
import org.opensearch.common.cache.stats.CacheStats;
20+
import org.opensearch.common.cache.stats.ImmutableCacheStatsHolder;
2121
import org.opensearch.common.cache.store.config.CacheConfig;
2222
import org.opensearch.common.settings.Setting;
2323
import org.opensearch.common.settings.Settings;
@@ -195,7 +195,7 @@ public void close() throws IOException {
195195
}
196196

197197
@Override
198-
public CacheStats stats() {
198+
public ImmutableCacheStatsHolder stats() {
199199
return null; // TODO: in TSC stats PR
200200
}
201201

modules/cache-common/src/test/java/org/opensearch/cache/common/tier/MockDiskCache.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.opensearch.common.cache.RemovalNotification;
1717
import org.opensearch.common.cache.RemovalReason;
1818
import org.opensearch.common.cache.serializer.Serializer;
19-
import org.opensearch.common.cache.stats.CacheStats;
2019
import org.opensearch.common.cache.store.builders.ICacheBuilder;
2120
import org.opensearch.common.cache.store.config.CacheConfig;
2221

plugins/cache-ehcache/src/main/java/org/opensearch/cache/store/disk/EhcacheDiskCache.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import org.opensearch.common.cache.RemovalReason;
2525
import org.opensearch.common.cache.serializer.ICacheKeySerializer;
2626
import org.opensearch.common.cache.serializer.Serializer;
27-
import org.opensearch.common.cache.stats.CacheStats;
28-
import org.opensearch.common.cache.stats.StatsHolder;
27+
import org.opensearch.common.cache.stats.CacheStatsHolder;
28+
import org.opensearch.common.cache.stats.ImmutableCacheStatsHolder;
2929
import org.opensearch.common.cache.store.builders.ICacheBuilder;
3030
import org.opensearch.common.cache.store.config.CacheConfig;
3131
import org.opensearch.common.collect.Tuple;
@@ -113,7 +113,7 @@ public class EhcacheDiskCache<K, V> implements ICache<K, V> {
113113
private final Class<K> keyType;
114114
private final Class<V> valueType;
115115
private final TimeValue expireAfterAccess;
116-
private final StatsHolder statsHolder;
116+
private final CacheStatsHolder cacheStatsHolder;
117117
private final EhCacheEventListener ehCacheEventListener;
118118
private final String threadPoolAlias;
119119
private final Settings settings;
@@ -162,7 +162,7 @@ private EhcacheDiskCache(Builder<K, V> builder) {
162162
this.ehCacheEventListener = new EhCacheEventListener(builder.getRemovalListener(), builder.getWeigher());
163163
this.cache = buildCache(Duration.ofMillis(expireAfterAccess.getMillis()), builder);
164164
List<String> dimensionNames = Objects.requireNonNull(builder.dimensionNames, "Dimension names can't be null");
165-
this.statsHolder = new StatsHolder(dimensionNames);
165+
this.cacheStatsHolder = new CacheStatsHolder(dimensionNames);
166166
}
167167

168168
@SuppressWarnings({ "rawtypes" })
@@ -277,9 +277,9 @@ public V get(ICacheKey<K> key) {
277277
throw new OpenSearchException("Exception occurred while trying to fetch item from ehcache disk cache");
278278
}
279279
if (value != null) {
280-
statsHolder.incrementHits(key.dimensions);
280+
cacheStatsHolder.incrementHits(key.dimensions);
281281
} else {
282-
statsHolder.incrementMisses(key.dimensions);
282+
cacheStatsHolder.incrementMisses(key.dimensions);
283283
}
284284
return value;
285285
}
@@ -315,9 +315,9 @@ public V computeIfAbsent(ICacheKey<K> key, LoadAwareCacheLoader<ICacheKey<K>, V>
315315
value = compute(key, loader);
316316
}
317317
if (!loader.isLoaded()) {
318-
statsHolder.incrementHits(key.dimensions);
318+
cacheStatsHolder.incrementHits(key.dimensions);
319319
} else {
320-
statsHolder.incrementMisses(key.dimensions);
320+
cacheStatsHolder.incrementMisses(key.dimensions);
321321
}
322322
return value;
323323
}
@@ -383,7 +383,7 @@ private V compute(ICacheKey<K> key, LoadAwareCacheLoader<ICacheKey<K>, V> loader
383383
public void invalidate(ICacheKey<K> key) {
384384
try {
385385
if (key.getDropStatsForDimensions()) {
386-
statsHolder.removeDimensions(key.dimensions);
386+
cacheStatsHolder.removeDimensions(key.dimensions);
387387
}
388388
if (key.key != null) {
389389
cache.remove(key);
@@ -398,7 +398,7 @@ public void invalidate(ICacheKey<K> key) {
398398
@Override
399399
public void invalidateAll() {
400400
cache.clear();
401-
statsHolder.reset();
401+
cacheStatsHolder.reset();
402402
}
403403

404404
/**
@@ -416,7 +416,7 @@ public Iterable<ICacheKey<K>> keys() {
416416
*/
417417
@Override
418418
public long count() {
419-
return statsHolder.count();
419+
return cacheStatsHolder.count();
420420
}
421421

422422
@Override
@@ -448,8 +448,8 @@ public void close() {
448448
* @return CacheStats
449449
*/
450450
@Override
451-
public CacheStats stats() {
452-
return statsHolder.getCacheStats();
451+
public ImmutableCacheStatsHolder stats() {
452+
return cacheStatsHolder.getImmutableCacheStatsHolder();
453453
}
454454

455455
/**
@@ -508,39 +508,39 @@ private long getNewValuePairSize(CacheEvent<? extends ICacheKey<K>, ? extends By
508508
public void onEvent(CacheEvent<? extends ICacheKey<K>, ? extends ByteArrayWrapper> event) {
509509
switch (event.getType()) {
510510
case CREATED:
511-
statsHolder.incrementEntries(event.getKey().dimensions);
512-
statsHolder.incrementSizeInBytes(event.getKey().dimensions, getNewValuePairSize(event));
511+
cacheStatsHolder.incrementEntries(event.getKey().dimensions);
512+
cacheStatsHolder.incrementSizeInBytes(event.getKey().dimensions, getNewValuePairSize(event));
513513
assert event.getOldValue() == null;
514514
break;
515515
case EVICTED:
516516
this.removalListener.onRemoval(
517517
new RemovalNotification<>(event.getKey(), deserializeValue(event.getOldValue()), RemovalReason.EVICTED)
518518
);
519-
statsHolder.decrementEntries(event.getKey().dimensions);
520-
statsHolder.decrementSizeInBytes(event.getKey().dimensions, getOldValuePairSize(event));
521-
statsHolder.incrementEvictions(event.getKey().dimensions);
519+
cacheStatsHolder.decrementEntries(event.getKey().dimensions);
520+
cacheStatsHolder.decrementSizeInBytes(event.getKey().dimensions, getOldValuePairSize(event));
521+
cacheStatsHolder.incrementEvictions(event.getKey().dimensions);
522522
assert event.getNewValue() == null;
523523
break;
524524
case REMOVED:
525525
this.removalListener.onRemoval(
526526
new RemovalNotification<>(event.getKey(), deserializeValue(event.getOldValue()), RemovalReason.EXPLICIT)
527527
);
528-
statsHolder.decrementEntries(event.getKey().dimensions);
529-
statsHolder.decrementSizeInBytes(event.getKey().dimensions, getOldValuePairSize(event));
528+
cacheStatsHolder.decrementEntries(event.getKey().dimensions);
529+
cacheStatsHolder.decrementSizeInBytes(event.getKey().dimensions, getOldValuePairSize(event));
530530
assert event.getNewValue() == null;
531531
break;
532532
case EXPIRED:
533533
this.removalListener.onRemoval(
534534
new RemovalNotification<>(event.getKey(), deserializeValue(event.getOldValue()), RemovalReason.INVALIDATED)
535535
);
536-
statsHolder.decrementEntries(event.getKey().dimensions);
537-
statsHolder.decrementSizeInBytes(event.getKey().dimensions, getOldValuePairSize(event));
536+
cacheStatsHolder.decrementEntries(event.getKey().dimensions);
537+
cacheStatsHolder.decrementSizeInBytes(event.getKey().dimensions, getOldValuePairSize(event));
538538
assert event.getNewValue() == null;
539539
break;
540540
case UPDATED:
541541
long newSize = getNewValuePairSize(event);
542542
long oldSize = getOldValuePairSize(event);
543-
statsHolder.incrementSizeInBytes(event.getKey().dimensions, newSize - oldSize);
543+
cacheStatsHolder.incrementSizeInBytes(event.getKey().dimensions, newSize - oldSize);
544544
break;
545545
default:
546546
break;

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +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.CacheStatsCounterSnapshot;
24-
import org.opensearch.common.cache.stats.MultiDimensionCacheStats;
23+
import org.opensearch.common.cache.stats.CacheStatsSnapshot;
2524
import org.opensearch.common.cache.store.config.CacheConfig;
2625
import org.opensearch.common.metrics.CounterMetric;
2726
import org.opensearch.common.settings.Settings;
@@ -829,17 +828,15 @@ public void testInvalidateWithDropDimensions() throws Exception {
829828

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

832-
CacheStatsCounterSnapshot snapshot = ((MultiDimensionCacheStats) ehCacheDiskCachingTier.stats()).getStatsForDimensionValues(
833-
keyToDrop.dimensions
834-
);
831+
CacheStatsSnapshot snapshot = ehCacheDiskCachingTier.stats().getStatsForDimensionValues(keyToDrop.dimensions);
835832
assertNotNull(snapshot);
836833

837834
keyToDrop.setDropStatsForDimensions(true);
838835
ehCacheDiskCachingTier.invalidate(keyToDrop);
839836

840837
// Now assert the stats are gone for any key that has this combination of dimensions, but still there otherwise
841838
for (ICacheKey<String> keyAdded : keysAdded) {
842-
snapshot = ((MultiDimensionCacheStats) ehCacheDiskCachingTier.stats()).getStatsForDimensionValues(keyAdded.dimensions);
839+
snapshot = ehCacheDiskCachingTier.stats().getStatsForDimensionValues(keyAdded.dimensions);
843840
if (keyAdded.dimensions.equals(keyToDrop.dimensions)) {
844841
assertNull(snapshot);
845842
} else {

server/src/main/java/org/opensearch/common/cache/ICache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
package org.opensearch.common.cache;
1010

1111
import org.opensearch.common.annotation.ExperimentalApi;
12-
import org.opensearch.common.cache.stats.CacheStats;
12+
import org.opensearch.common.cache.stats.ImmutableCacheStatsHolder;
1313
import org.opensearch.common.cache.store.config.CacheConfig;
1414

1515
import java.io.Closeable;
@@ -45,7 +45,7 @@ public interface ICache<K, V> extends Closeable {
4545

4646
void refresh();
4747

48-
CacheStats stats();
48+
ImmutableCacheStatsHolder stats();
4949

5050
/**
5151
* Factory to create objects.

server/src/main/java/org/opensearch/common/cache/ICacheKey.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
/**
1616
* A key wrapper used for ICache implementations, which carries dimensions with it.
17-
* @param <K> the type of the underlying key
17+
* @param <K> the type of the underlying key. K must implement equals(), or else ICacheKey.equals()
18+
* won't work properly and cache behavior may be incorrect!
1819
*
1920
* @opensearch.experimental
2021
*/

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

Lines changed: 112 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,125 @@
88

99
package org.opensearch.common.cache.stats;
1010

11-
import org.opensearch.common.annotation.ExperimentalApi;
11+
import org.opensearch.common.metrics.CounterMetric;
12+
13+
import java.util.Objects;
1214

1315
/**
14-
* Interface for access to any cache stats. Allows accessing stats by dimension values.
15-
* Stores an immutable snapshot of stats for a cache. The cache maintains its own live counters.
16-
*
17-
* @opensearch.experimental
16+
* A mutable class containing the 5 live metrics tracked by a StatsHolder object.
1817
*/
19-
@ExperimentalApi
20-
public interface CacheStats { // TODO: also extends Writeable, ToXContentFragment (in API PR)
18+
public class CacheStats {
19+
CounterMetric hits;
20+
CounterMetric misses;
21+
CounterMetric evictions;
22+
CounterMetric sizeInBytes;
23+
CounterMetric entries;
24+
25+
public CacheStats(long hits, long misses, long evictions, long sizeInBytes, long entries) {
26+
this.hits = new CounterMetric();
27+
this.hits.inc(hits);
28+
this.misses = new CounterMetric();
29+
this.misses.inc(misses);
30+
this.evictions = new CounterMetric();
31+
this.evictions.inc(evictions);
32+
this.sizeInBytes = new CounterMetric();
33+
this.sizeInBytes.inc(sizeInBytes);
34+
this.entries = new CounterMetric();
35+
this.entries.inc(entries);
36+
}
37+
38+
public CacheStats() {
39+
this(0, 0, 0, 0, 0);
40+
}
41+
42+
private void internalAdd(long otherHits, long otherMisses, long otherEvictions, long otherSizeInBytes, long otherEntries) {
43+
this.hits.inc(otherHits);
44+
this.misses.inc(otherMisses);
45+
this.evictions.inc(otherEvictions);
46+
this.sizeInBytes.inc(otherSizeInBytes);
47+
this.entries.inc(otherEntries);
48+
}
49+
50+
public void add(CacheStats other) {
51+
if (other == null) {
52+
return;
53+
}
54+
internalAdd(other.getHits(), other.getMisses(), other.getEvictions(), other.getSizeInBytes(), other.getEntries());
55+
}
56+
57+
public void add(CacheStatsSnapshot snapshot) {
58+
if (snapshot == null) {
59+
return;
60+
}
61+
internalAdd(snapshot.getHits(), snapshot.getMisses(), snapshot.getEvictions(), snapshot.getSizeInBytes(), snapshot.getEntries());
62+
}
63+
64+
public void subtract(CacheStatsSnapshot other) {
65+
if (other == null) {
66+
return;
67+
}
68+
internalAdd(-other.getHits(), -other.getMisses(), -other.getEvictions(), -other.getSizeInBytes(), -other.getEntries());
69+
}
70+
71+
@Override
72+
public int hashCode() {
73+
return Objects.hash(hits.count(), misses.count(), evictions.count(), sizeInBytes.count(), entries.count());
74+
}
75+
76+
public void incrementHits() {
77+
hits.inc();
78+
}
79+
80+
public void incrementMisses() {
81+
misses.inc();
82+
}
83+
84+
public void incrementEvictions() {
85+
evictions.inc();
86+
}
87+
88+
public void incrementSizeInBytes(long amount) {
89+
sizeInBytes.inc(amount);
90+
}
91+
92+
public void decrementSizeInBytes(long amount) {
93+
sizeInBytes.dec(amount);
94+
}
95+
96+
public void incrementEntries() {
97+
entries.inc();
98+
}
99+
100+
public void decrementEntries() {
101+
entries.dec();
102+
}
103+
104+
public long getHits() {
105+
return hits.count();
106+
}
21107

22-
// Method to get all 5 values at once
23-
CacheStatsCounterSnapshot getTotalStats();
108+
public long getMisses() {
109+
return misses.count();
110+
}
24111

25-
// Methods to get total values.
26-
long getTotalHits();
112+
public long getEvictions() {
113+
return evictions.count();
114+
}
27115

28-
long getTotalMisses();
116+
public long getSizeInBytes() {
117+
return sizeInBytes.count();
118+
}
29119

30-
long getTotalEvictions();
120+
public long getEntries() {
121+
return entries.count();
122+
}
31123

32-
long getTotalSizeInBytes();
124+
public void resetSizeAndEntries() {
125+
sizeInBytes = new CounterMetric();
126+
entries = new CounterMetric();
127+
}
33128

34-
long getTotalEntries();
129+
public CacheStatsSnapshot snapshot() {
130+
return new CacheStatsSnapshot(hits.count(), misses.count(), evictions.count(), sizeInBytes.count(), entries.count());
131+
}
35132
}

0 commit comments

Comments
 (0)