Skip to content

Commit

Permalink
Renamed classes to make more sense, removed interface
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <[email protected]>
  • Loading branch information
Peter Alfonsi committed Apr 12, 2024
1 parent c0b3dd2 commit 2f59ee7
Show file tree
Hide file tree
Showing 18 changed files with 421 additions and 486 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.opensearch.common.cache.RemovalListener;
import org.opensearch.common.cache.RemovalNotification;
import org.opensearch.common.cache.policy.CachedQueryResult;
import org.opensearch.common.cache.stats.CacheStats;
import org.opensearch.common.cache.stats.ImmutableCacheStatsHolder;
import org.opensearch.common.cache.store.config.CacheConfig;
import org.opensearch.common.settings.Setting;
import org.opensearch.common.settings.Settings;
Expand Down Expand Up @@ -195,7 +195,7 @@ public void close() throws IOException {
}

@Override
public CacheStats stats() {
public ImmutableCacheStatsHolder stats() {
return null; // TODO: in TSC stats PR
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.opensearch.common.cache.RemovalNotification;
import org.opensearch.common.cache.RemovalReason;
import org.opensearch.common.cache.serializer.Serializer;
import org.opensearch.common.cache.stats.CacheStats;
import org.opensearch.common.cache.store.builders.ICacheBuilder;
import org.opensearch.common.cache.store.config.CacheConfig;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import org.opensearch.common.cache.RemovalReason;
import org.opensearch.common.cache.serializer.ICacheKeySerializer;
import org.opensearch.common.cache.serializer.Serializer;
import org.opensearch.common.cache.stats.CacheStats;
import org.opensearch.common.cache.stats.StatsHolder;
import org.opensearch.common.cache.stats.CacheStatsHolder;
import org.opensearch.common.cache.stats.ImmutableCacheStatsHolder;
import org.opensearch.common.cache.store.builders.ICacheBuilder;
import org.opensearch.common.cache.store.config.CacheConfig;
import org.opensearch.common.collect.Tuple;
Expand Down Expand Up @@ -113,7 +113,7 @@ public class EhcacheDiskCache<K, V> implements ICache<K, V> {
private final Class<K> keyType;
private final Class<V> valueType;
private final TimeValue expireAfterAccess;
private final StatsHolder statsHolder;
private final CacheStatsHolder cacheStatsHolder;
private final EhCacheEventListener ehCacheEventListener;
private final String threadPoolAlias;
private final Settings settings;
Expand Down Expand Up @@ -162,7 +162,7 @@ private EhcacheDiskCache(Builder<K, V> builder) {
this.ehCacheEventListener = new EhCacheEventListener(builder.getRemovalListener(), builder.getWeigher());
this.cache = buildCache(Duration.ofMillis(expireAfterAccess.getMillis()), builder);
List<String> dimensionNames = Objects.requireNonNull(builder.dimensionNames, "Dimension names can't be null");
this.statsHolder = new StatsHolder(dimensionNames);
this.cacheStatsHolder = new CacheStatsHolder(dimensionNames);
}

@SuppressWarnings({ "rawtypes" })
Expand Down Expand Up @@ -277,9 +277,9 @@ public V get(ICacheKey<K> key) {
throw new OpenSearchException("Exception occurred while trying to fetch item from ehcache disk cache");
}
if (value != null) {
statsHolder.incrementHits(key.dimensions);
cacheStatsHolder.incrementHits(key.dimensions);
} else {
statsHolder.incrementMisses(key.dimensions);
cacheStatsHolder.incrementMisses(key.dimensions);
}
return value;
}
Expand Down Expand Up @@ -315,9 +315,9 @@ public V computeIfAbsent(ICacheKey<K> key, LoadAwareCacheLoader<ICacheKey<K>, V>
value = compute(key, loader);
}
if (!loader.isLoaded()) {
statsHolder.incrementHits(key.dimensions);
cacheStatsHolder.incrementHits(key.dimensions);
} else {
statsHolder.incrementMisses(key.dimensions);
cacheStatsHolder.incrementMisses(key.dimensions);
}
return value;
}
Expand Down Expand Up @@ -383,7 +383,7 @@ private V compute(ICacheKey<K> key, LoadAwareCacheLoader<ICacheKey<K>, V> loader
public void invalidate(ICacheKey<K> key) {
try {
if (key.getDropStatsForDimensions()) {
statsHolder.removeDimensions(key.dimensions);
cacheStatsHolder.removeDimensions(key.dimensions);
}
if (key.key != null) {
cache.remove(key);
Expand All @@ -398,7 +398,7 @@ public void invalidate(ICacheKey<K> key) {
@Override
public void invalidateAll() {
cache.clear();
statsHolder.reset();
cacheStatsHolder.reset();
}

/**
Expand All @@ -416,7 +416,7 @@ public Iterable<ICacheKey<K>> keys() {
*/
@Override
public long count() {
return statsHolder.count();
return cacheStatsHolder.count();
}

@Override
Expand Down Expand Up @@ -448,8 +448,8 @@ public void close() {
* @return CacheStats
*/
@Override
public CacheStats stats() {
return statsHolder.getCacheStats();
public ImmutableCacheStatsHolder stats() {
return cacheStatsHolder.getImmutableCacheStatsHolder();
}

/**
Expand Down Expand Up @@ -508,39 +508,39 @@ private long getNewValuePairSize(CacheEvent<? extends ICacheKey<K>, ? extends By
public void onEvent(CacheEvent<? extends ICacheKey<K>, ? extends ByteArrayWrapper> event) {
switch (event.getType()) {
case CREATED:
statsHolder.incrementEntries(event.getKey().dimensions);
statsHolder.incrementSizeInBytes(event.getKey().dimensions, getNewValuePairSize(event));
cacheStatsHolder.incrementEntries(event.getKey().dimensions);
cacheStatsHolder.incrementSizeInBytes(event.getKey().dimensions, getNewValuePairSize(event));
assert event.getOldValue() == null;
break;
case EVICTED:
this.removalListener.onRemoval(
new RemovalNotification<>(event.getKey(), deserializeValue(event.getOldValue()), RemovalReason.EVICTED)
);
statsHolder.decrementEntries(event.getKey().dimensions);
statsHolder.decrementSizeInBytes(event.getKey().dimensions, getOldValuePairSize(event));
statsHolder.incrementEvictions(event.getKey().dimensions);
cacheStatsHolder.decrementEntries(event.getKey().dimensions);
cacheStatsHolder.decrementSizeInBytes(event.getKey().dimensions, getOldValuePairSize(event));
cacheStatsHolder.incrementEvictions(event.getKey().dimensions);
assert event.getNewValue() == null;
break;
case REMOVED:
this.removalListener.onRemoval(
new RemovalNotification<>(event.getKey(), deserializeValue(event.getOldValue()), RemovalReason.EXPLICIT)
);
statsHolder.decrementEntries(event.getKey().dimensions);
statsHolder.decrementSizeInBytes(event.getKey().dimensions, getOldValuePairSize(event));
cacheStatsHolder.decrementEntries(event.getKey().dimensions);
cacheStatsHolder.decrementSizeInBytes(event.getKey().dimensions, getOldValuePairSize(event));
assert event.getNewValue() == null;
break;
case EXPIRED:
this.removalListener.onRemoval(
new RemovalNotification<>(event.getKey(), deserializeValue(event.getOldValue()), RemovalReason.INVALIDATED)
);
statsHolder.decrementEntries(event.getKey().dimensions);
statsHolder.decrementSizeInBytes(event.getKey().dimensions, getOldValuePairSize(event));
cacheStatsHolder.decrementEntries(event.getKey().dimensions);
cacheStatsHolder.decrementSizeInBytes(event.getKey().dimensions, getOldValuePairSize(event));
assert event.getNewValue() == null;
break;
case UPDATED:
long newSize = getNewValuePairSize(event);
long oldSize = getOldValuePairSize(event);
statsHolder.incrementSizeInBytes(event.getKey().dimensions, newSize - oldSize);
cacheStatsHolder.incrementSizeInBytes(event.getKey().dimensions, newSize - oldSize);
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +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.CacheStatsCounterSnapshot;
import org.opensearch.common.cache.stats.MultiDimensionCacheStats;
import org.opensearch.common.cache.stats.CacheStatsSnapshot;
import org.opensearch.common.cache.store.config.CacheConfig;
import org.opensearch.common.metrics.CounterMetric;
import org.opensearch.common.settings.Settings;
Expand Down Expand Up @@ -829,17 +828,15 @@ public void testInvalidateWithDropDimensions() throws Exception {

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

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

keyToDrop.setDropStatsForDimensions(true);
ehCacheDiskCachingTier.invalidate(keyToDrop);

// Now assert the stats are gone for any key that has this combination of dimensions, but still there otherwise
for (ICacheKey<String> keyAdded : keysAdded) {
snapshot = ((MultiDimensionCacheStats) ehCacheDiskCachingTier.stats()).getStatsForDimensionValues(keyAdded.dimensions);
snapshot = ehCacheDiskCachingTier.stats().getStatsForDimensionValues(keyAdded.dimensions);
if (keyAdded.dimensions.equals(keyToDrop.dimensions)) {
assertNull(snapshot);
} else {
Expand Down
4 changes: 2 additions & 2 deletions server/src/main/java/org/opensearch/common/cache/ICache.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
package org.opensearch.common.cache;

import org.opensearch.common.annotation.ExperimentalApi;
import org.opensearch.common.cache.stats.CacheStats;
import org.opensearch.common.cache.stats.ImmutableCacheStatsHolder;
import org.opensearch.common.cache.store.config.CacheConfig;

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

void refresh();

CacheStats stats();
ImmutableCacheStatsHolder stats();

/**
* Factory to create objects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

/**
* A key wrapper used for ICache implementations, which carries dimensions with it.
* @param <K> the type of the underlying key
* @param <K> the type of the underlying key. K must implement equals(), or else ICacheKey.equals()
* won't work properly and cache behavior may be incorrect!
*
* @opensearch.experimental
*/
Expand Down
127 changes: 112 additions & 15 deletions server/src/main/java/org/opensearch/common/cache/stats/CacheStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,125 @@

package org.opensearch.common.cache.stats;

import org.opensearch.common.annotation.ExperimentalApi;
import org.opensearch.common.metrics.CounterMetric;

import java.util.Objects;

/**
* Interface for access to any cache stats. Allows accessing stats by dimension values.
* Stores an immutable snapshot of stats for a cache. The cache maintains its own live counters.
*
* @opensearch.experimental
* A mutable class containing the 5 live metrics tracked by a StatsHolder object.
*/
@ExperimentalApi
public interface CacheStats { // TODO: also extends Writeable, ToXContentFragment (in API PR)
public class CacheStats {
CounterMetric hits;
CounterMetric misses;
CounterMetric evictions;
CounterMetric sizeInBytes;
CounterMetric entries;

public CacheStats(long hits, long misses, long evictions, long sizeInBytes, long entries) {
this.hits = new CounterMetric();
this.hits.inc(hits);
this.misses = new CounterMetric();
this.misses.inc(misses);
this.evictions = new CounterMetric();
this.evictions.inc(evictions);
this.sizeInBytes = new CounterMetric();
this.sizeInBytes.inc(sizeInBytes);
this.entries = new CounterMetric();
this.entries.inc(entries);
}

public CacheStats() {
this(0, 0, 0, 0, 0);
}

private void internalAdd(long otherHits, long otherMisses, long otherEvictions, long otherSizeInBytes, long otherEntries) {
this.hits.inc(otherHits);
this.misses.inc(otherMisses);
this.evictions.inc(otherEvictions);
this.sizeInBytes.inc(otherSizeInBytes);
this.entries.inc(otherEntries);
}

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

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

public void subtract(CacheStatsSnapshot other) {
if (other == null) {
return;
}
internalAdd(-other.getHits(), -other.getMisses(), -other.getEvictions(), -other.getSizeInBytes(), -other.getEntries());
}

@Override
public int hashCode() {
return Objects.hash(hits.count(), misses.count(), evictions.count(), sizeInBytes.count(), entries.count());
}

public void incrementHits() {
hits.inc();
}

public void incrementMisses() {
misses.inc();
}

public void incrementEvictions() {
evictions.inc();
}

public void incrementSizeInBytes(long amount) {
sizeInBytes.inc(amount);
}

public void decrementSizeInBytes(long amount) {
sizeInBytes.dec(amount);
}

public void incrementEntries() {
entries.inc();
}

public void decrementEntries() {
entries.dec();
}

public long getHits() {
return hits.count();
}

// Method to get all 5 values at once
CacheStatsCounterSnapshot getTotalStats();
public long getMisses() {
return misses.count();
}

// Methods to get total values.
long getTotalHits();
public long getEvictions() {
return evictions.count();
}

long getTotalMisses();
public long getSizeInBytes() {
return sizeInBytes.count();
}

long getTotalEvictions();
public long getEntries() {
return entries.count();
}

long getTotalSizeInBytes();
public void resetSizeAndEntries() {
sizeInBytes = new CounterMetric();
entries = new CounterMetric();
}

long getTotalEntries();
public CacheStatsSnapshot snapshot() {
return new CacheStatsSnapshot(hits.count(), misses.count(), evictions.count(), sizeInBytes.count(), entries.count());
}
}
Loading

0 comments on commit 2f59ee7

Please sign in to comment.