From ea33af842ccb3ff45f50f2639c4e4ec8972e6105 Mon Sep 17 00:00:00 2001 From: Peter Alfonsi Date: Fri, 1 Mar 2024 15:35:04 -0800 Subject: [PATCH] spotlessApply Signed-off-by: Peter Alfonsi --- .../common/tier/TieredSpilloverCache.java | 33 +++++++-------- .../tier/TieredSpilloverCacheTests.java | 2 +- .../opensearch/cache/EhcacheCachePlugin.java | 1 - .../cache/store/disk/EhcacheDiskCache.java | 42 ++++++++++++------- .../store/disk/EhCacheDiskCacheTests.java | 31 ++++++++++---- .../opensearch/common/cache/ICacheKey.java | 1 - .../common/cache/stats/CacheStats.java | 15 ++++++- .../cache/stats/CacheStatsDimension.java | 1 + .../cache/stats/CacheStatsResponse.java | 2 +- .../cache/stats/MultiDimensionCacheStats.java | 18 +++++--- .../cache/store/OpenSearchOnHeapCache.java | 20 ++++----- .../cache/store/builders/ICacheBuilder.java | 2 +- .../cache/store/config/CacheConfig.java | 2 +- .../indices/IRCKeyWriteableSerializer.java | 4 +- .../BytesReferenceSerializerTests.java | 1 - .../serializer/ICacheKeySerializerTests.java | 7 ++-- .../cache/stats/CacheStatsDimensionTests.java | 2 +- .../stats/MultiDimensionCacheStatsTests.java | 32 +++++++++++--- .../store/OpenSearchOnHeapCacheTests.java | 5 ++- .../IRCKeyWriteableSerializerTests.java | 6 +-- 20 files changed, 143 insertions(+), 84 deletions(-) diff --git a/modules/cache-common/src/main/java/org/opensearch/cache/common/tier/TieredSpilloverCache.java b/modules/cache-common/src/main/java/org/opensearch/cache/common/tier/TieredSpilloverCache.java index c96e98a2a3be8..e8a3e7985703c 100644 --- a/modules/cache-common/src/main/java/org/opensearch/cache/common/tier/TieredSpilloverCache.java +++ b/modules/cache-common/src/main/java/org/opensearch/cache/common/tier/TieredSpilloverCache.java @@ -48,8 +48,8 @@ public class TieredSpilloverCache implements ICache { private final ICache onHeapCache; // TODO: Listeners for removals from the two tiers - //private final RemovalListener, V> onDiskRemovalListener; - //private final RemovalListener, V> onHeapRemovalListener; + // private final RemovalListener, V> onDiskRemovalListener; + // private final RemovalListener, V> onHeapRemovalListener; // The listener for removals from the spillover cache as a whole private final RemovalListener, V> removalListener; @@ -67,21 +67,20 @@ public class TieredSpilloverCache implements ICache { Objects.requireNonNull(builder.diskCacheFactory, "disk cache builder can't be null"); this.removalListener = Objects.requireNonNull(builder.removalListener, "Removal listener can't be null"); - this.onHeapCache = builder.onHeapCacheFactory.create( - new CacheConfig.Builder().setRemovalListener(new RemovalListener<>() { - @Override - public void onRemoval(RemovalNotification, V> notification) { - try (ReleasableLock ignore = writeLock.acquire()) { - diskCache.put(notification.getKey(), notification.getValue()); - } - removalListener.onRemoval(notification); - } - }) - .setKeyType(builder.cacheConfig.getKeyType()) - .setValueType(builder.cacheConfig.getValueType()) - .setSettings(builder.cacheConfig.getSettings()) - .setWeigher(builder.cacheConfig.getWeigher()) - .build(), + this.onHeapCache = builder.onHeapCacheFactory.create(new CacheConfig.Builder().setRemovalListener(new RemovalListener<>() { + @Override + public void onRemoval(RemovalNotification, V> notification) { + try (ReleasableLock ignore = writeLock.acquire()) { + diskCache.put(notification.getKey(), notification.getValue()); + } + removalListener.onRemoval(notification); + } + }) + .setKeyType(builder.cacheConfig.getKeyType()) + .setValueType(builder.cacheConfig.getValueType()) + .setSettings(builder.cacheConfig.getSettings()) + .setWeigher(builder.cacheConfig.getWeigher()) + .build(), builder.cacheType, builder.cacheFactories diff --git a/modules/cache-common/src/test/java/org/opensearch/cache/common/tier/TieredSpilloverCacheTests.java b/modules/cache-common/src/test/java/org/opensearch/cache/common/tier/TieredSpilloverCacheTests.java index c1f5abe5a2fe1..e1a48ec6fdae9 100644 --- a/modules/cache-common/src/test/java/org/opensearch/cache/common/tier/TieredSpilloverCacheTests.java +++ b/modules/cache-common/src/test/java/org/opensearch/cache/common/tier/TieredSpilloverCacheTests.java @@ -30,7 +30,7 @@ import static org.opensearch.common.cache.store.settings.OpenSearchOnHeapCacheSettings.MAXIMUM_SIZE_IN_BYTES_KEY; public class TieredSpilloverCacheTests extends OpenSearchTestCase { -// TODO: TSC has no stats implementation yet - fix these tests once it does + // TODO: These tests are uncommented in the second stats rework PR, which adds a TSC stats implementation /*public void testComputeIfAbsentWithoutAnyOnHeapCacheEviction() throws Exception { int onHeapCacheSize = randomIntBetween(10, 30); int keyValueSize = 50; diff --git a/plugins/cache-ehcache/src/main/java/org/opensearch/cache/EhcacheCachePlugin.java b/plugins/cache-ehcache/src/main/java/org/opensearch/cache/EhcacheCachePlugin.java index 510130184397a..ceda96e4a7d7d 100644 --- a/plugins/cache-ehcache/src/main/java/org/opensearch/cache/EhcacheCachePlugin.java +++ b/plugins/cache-ehcache/src/main/java/org/opensearch/cache/EhcacheCachePlugin.java @@ -16,7 +16,6 @@ import org.opensearch.plugins.Plugin; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; diff --git a/plugins/cache-ehcache/src/main/java/org/opensearch/cache/store/disk/EhcacheDiskCache.java b/plugins/cache-ehcache/src/main/java/org/opensearch/cache/store/disk/EhcacheDiskCache.java index e769f0e0d7ca1..fcc661fd16676 100644 --- a/plugins/cache-ehcache/src/main/java/org/opensearch/cache/store/disk/EhcacheDiskCache.java +++ b/plugins/cache-ehcache/src/main/java/org/opensearch/cache/store/disk/EhcacheDiskCache.java @@ -10,25 +10,22 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.ehcache.core.spi.service.FileBasedPersistenceContext; -import org.ehcache.spi.serialization.SerializerException; import org.opensearch.OpenSearchException; import org.opensearch.cache.EhcacheDiskCacheSettings; import org.opensearch.common.SuppressForbidden; import org.opensearch.common.annotation.ExperimentalApi; import org.opensearch.common.cache.CacheType; import org.opensearch.common.cache.ICache; +import org.opensearch.common.cache.ICacheKey; import org.opensearch.common.cache.LoadAwareCacheLoader; import org.opensearch.common.cache.RemovalListener; import org.opensearch.common.cache.RemovalNotification; 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.ICacheKey; -import org.opensearch.common.cache.stats.CacheStatsDimension; import org.opensearch.common.cache.stats.MultiDimensionCacheStats; import org.opensearch.common.cache.store.builders.ICacheBuilder; -import org.opensearch.common.cache.serializer.ICacheKeySerializer; -import org.opensearch.common.cache.serializer.Serializer; import org.opensearch.common.cache.store.config.CacheConfig; import org.opensearch.common.collect.Tuple; import org.opensearch.common.settings.Setting; @@ -60,6 +57,7 @@ import org.ehcache.config.builders.PooledExecutionServiceConfigurationBuilder; import org.ehcache.config.builders.ResourcePoolsBuilder; import org.ehcache.config.units.MemoryUnit; +import org.ehcache.core.spi.service.FileBasedPersistenceContext; import org.ehcache.event.CacheEvent; import org.ehcache.event.CacheEventListener; import org.ehcache.event.EventType; @@ -67,6 +65,7 @@ import org.ehcache.impl.config.store.disk.OffHeapDiskStoreConfiguration; import org.ehcache.spi.loaderwriter.CacheLoadingException; import org.ehcache.spi.loaderwriter.CacheWritingException; +import org.ehcache.spi.serialization.SerializerException; import static org.opensearch.cache.EhcacheDiskCacheSettings.DISK_CACHE_ALIAS_KEY; import static org.opensearch.cache.EhcacheDiskCacheSettings.DISK_CACHE_EXPIRE_AFTER_ACCESS_KEY; @@ -153,7 +152,8 @@ private EhcacheDiskCache(Builder builder) { this.ehCacheEventListener = new EhCacheEventListener( Objects.requireNonNull(builder.getRemovalListener(), "Removal listener can't be null"), Objects.requireNonNull(builder.getWeigher(), "Weigher function can't be null"), - this.valueSerializer); + this.valueSerializer + ); this.cache = buildCache(Duration.ofMillis(expireAfterAccess.getMillis()), builder); List dimensionNames = Objects.requireNonNull(builder.dimensionNames, "Dimension names can't be null"); this.stats = new MultiDimensionCacheStats(dimensionNames, TIER_DIMENSION_VALUE); @@ -456,9 +456,11 @@ class EhCacheEventListener implements CacheEventListener, byt private ToLongBiFunction, V> weigher; private Serializer valueSerializer; - EhCacheEventListener(RemovalListener, V> removalListener, - ToLongBiFunction, V> weigher, - Serializer valueSerializer) { + EhCacheEventListener( + RemovalListener, V> removalListener, + ToLongBiFunction, V> weigher, + Serializer valueSerializer + ) { this.removalListener = removalListener; this.weigher = weigher; this.valueSerializer = valueSerializer; @@ -481,20 +483,30 @@ public void onEvent(CacheEvent, ? extends byte[]> event) assert event.getOldValue() == null; break; case EVICTED: - this.removalListener.onRemoval(new RemovalNotification<>(event.getKey(), valueSerializer.deserialize(event.getOldValue()), RemovalReason.EVICTED)); + this.removalListener.onRemoval( + new RemovalNotification<>(event.getKey(), valueSerializer.deserialize(event.getOldValue()), RemovalReason.EVICTED) + ); stats.decrementEntriesByDimensions(event.getKey().dimensions); stats.incrementMemorySizeByDimensions(event.getKey().dimensions, -getOldValuePairSize(event)); stats.incrementEvictionsByDimensions(event.getKey().dimensions); assert event.getNewValue() == null; break; case REMOVED: - this.removalListener.onRemoval(new RemovalNotification<>(event.getKey(), valueSerializer.deserialize(event.getOldValue()), RemovalReason.EXPLICIT)); + this.removalListener.onRemoval( + new RemovalNotification<>(event.getKey(), valueSerializer.deserialize(event.getOldValue()), RemovalReason.EXPLICIT) + ); stats.decrementEntriesByDimensions(event.getKey().dimensions); stats.incrementMemorySizeByDimensions(event.getKey().dimensions, -getOldValuePairSize(event)); assert event.getNewValue() == null; break; case EXPIRED: - this.removalListener.onRemoval(new RemovalNotification<>(event.getKey(), valueSerializer.deserialize(event.getOldValue()), RemovalReason.INVALIDATED)); + this.removalListener.onRemoval( + new RemovalNotification<>( + event.getKey(), + valueSerializer.deserialize(event.getOldValue()), + RemovalReason.INVALIDATED + ) + ); stats.decrementEntriesByDimensions(event.getKey().dimensions); stats.incrementMemorySizeByDimensions(event.getKey().dimensions, -getOldValuePairSize(event)); assert event.getNewValue() == null; @@ -512,6 +524,7 @@ public void onEvent(CacheEvent, ? extends byte[]> event) private class KeySerializerWrapper implements org.ehcache.spi.serialization.Serializer { private ICacheKeySerializer serializer; + public KeySerializerWrapper(Serializer internalKeySerializer) { this.serializer = new ICacheKeySerializer<>(internalKeySerializer); } @@ -520,6 +533,7 @@ public KeySerializerWrapper(Serializer internalKeySerializer) { // cache after a restart. // See https://www.ehcache.org/documentation/3.0/serializers-copiers.html#persistent-vs-transient-caches public KeySerializerWrapper(ClassLoader classLoader, FileBasedPersistenceContext persistenceContext) {} + @Override public ByteBuffer serialize(ICacheKey object) throws SerializerException { return ByteBuffer.wrap(serializer.serialize(object)); @@ -705,7 +719,7 @@ public Builder setValueSerializer(Serializer valueSerializer) { return this; } - //@Override + // @Override public EhcacheDiskCache build() { return new EhcacheDiskCache<>(this); } diff --git a/plugins/cache-ehcache/src/test/java/org/opensearch/cache/store/disk/EhCacheDiskCacheTests.java b/plugins/cache-ehcache/src/test/java/org/opensearch/cache/store/disk/EhCacheDiskCacheTests.java index a25cde40396a6..20230878dbf89 100644 --- a/plugins/cache-ehcache/src/test/java/org/opensearch/cache/store/disk/EhCacheDiskCacheTests.java +++ b/plugins/cache-ehcache/src/test/java/org/opensearch/cache/store/disk/EhCacheDiskCacheTests.java @@ -11,12 +11,12 @@ import org.opensearch.cache.EhcacheDiskCacheSettings; import org.opensearch.common.cache.CacheType; import org.opensearch.common.cache.ICache; +import org.opensearch.common.cache.ICacheKey; import org.opensearch.common.cache.LoadAwareCacheLoader; import org.opensearch.common.cache.RemovalListener; import org.opensearch.common.cache.RemovalNotification; -import org.opensearch.common.cache.stats.CacheStatsDimension; -import org.opensearch.common.cache.ICacheKey; import org.opensearch.common.cache.serializer.Serializer; +import org.opensearch.common.cache.stats.CacheStatsDimension; import org.opensearch.common.cache.store.config.CacheConfig; import org.opensearch.common.settings.Settings; import org.opensearch.common.unit.TimeValue; @@ -39,9 +39,9 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.function.ToLongBiFunction; -import static org.hamcrest.CoreMatchers.instanceOf; import static org.opensearch.cache.EhcacheDiskCacheSettings.DISK_MAX_SIZE_IN_BYTES_KEY; import static org.opensearch.cache.EhcacheDiskCacheSettings.DISK_STORAGE_PATH_KEY; +import static org.hamcrest.CoreMatchers.instanceOf; public class EhCacheDiskCacheTests extends OpenSearchSingleNodeTestCase { @@ -500,7 +500,7 @@ public void testComputeIfAbsentWithNullValueLoading() throws Exception { // Try to hit different request with the same key concurrently. Loader throws exception. for (int i = 0; i < numberOfRequest; i++) { threads[i] = new Thread(() -> { - LoadAwareCacheLoader, String> loadAwareCacheLoader = new LoadAwareCacheLoader<>() { + LoadAwareCacheLoader, String> loadAwareCacheLoader = new LoadAwareCacheLoader<>() { boolean isLoaded; @Override @@ -543,7 +543,10 @@ public void testMemoryTracking() throws Exception { ToLongBiFunction, String> weigher = getWeigher(); int initialKeyLength = 40; int initialValueLength = 40; - long sizeForOneInitialEntry = weigher.applyAsLong(new ICacheKey<>(generateRandomString(initialKeyLength), getMockDimensions()), generateRandomString(initialValueLength)); + long sizeForOneInitialEntry = weigher.applyAsLong( + new ICacheKey<>(generateRandomString(initialKeyLength), getMockDimensions()), + generateRandomString(initialValueLength) + ); int maxEntries = 2000; try (NodeEnvironment env = newNodeEnvironment(settings)) { ICache ehcacheTest = new EhcacheDiskCache.Builder().setDiskCacheAlias("test1") @@ -603,7 +606,7 @@ public void testMemoryTracking() throws Exception { } // TODO: Ehcache incorrectly evicts at 30-40% of max size. Fix this test once we figure out why. // Since the EVICTED and EXPIRED cases use the same code as REMOVED, we should be ok on testing them for now. - //assertEquals(maxEntries * sizeForOneInitialEntry, ehcacheTest.stats().getTotalMemorySize()); + // assertEquals(maxEntries * sizeForOneInitialEntry, ehcacheTest.stats().getTotalMemorySize()); ehcacheTest.close(); } @@ -632,8 +635,18 @@ public void testGetStatsByTierName() throws Exception { for (int i = 0; i < randomKeys; i++) { ehcacheTest.put(getICacheKey(UUID.randomUUID().toString()), UUID.randomUUID().toString()); } - assertEquals(randomKeys, ehcacheTest.stats().getEntriesByDimensions(List.of(new CacheStatsDimension(CacheStatsDimension.TIER_DIMENSION_NAME, EhcacheDiskCache.TIER_DIMENSION_VALUE)))); - assertEquals(0, ehcacheTest.stats().getEntriesByDimensions(List.of(new CacheStatsDimension(CacheStatsDimension.TIER_DIMENSION_NAME, "other_tier_value")))); + assertEquals( + randomKeys, + ehcacheTest.stats() + .getEntriesByDimensions( + List.of(new CacheStatsDimension(CacheStatsDimension.TIER_DIMENSION_NAME, EhcacheDiskCache.TIER_DIMENSION_VALUE)) + ) + ); + assertEquals( + 0, + ehcacheTest.stats() + .getEntriesByDimensions(List.of(new CacheStatsDimension(CacheStatsDimension.TIER_DIMENSION_NAME, "other_tier_value"))) + ); ehcacheTest.close(); } @@ -676,6 +689,7 @@ private ToLongBiFunction, String> getWeigher() { class MockRemovalListener implements RemovalListener, V> { AtomicInteger onRemovalCount = new AtomicInteger(); + @Override public void onRemoval(RemovalNotification, V> notification) { onRemovalCount.incrementAndGet(); @@ -684,6 +698,7 @@ public void onRemoval(RemovalNotification, V> notification) { static class StringSerializer implements Serializer { private final Charset charset = StandardCharsets.UTF_8; + @Override public byte[] serialize(String object) { return object.getBytes(charset); diff --git a/server/src/main/java/org/opensearch/common/cache/ICacheKey.java b/server/src/main/java/org/opensearch/common/cache/ICacheKey.java index 8acf0352f25d2..51cb1712873c1 100644 --- a/server/src/main/java/org/opensearch/common/cache/ICacheKey.java +++ b/server/src/main/java/org/opensearch/common/cache/ICacheKey.java @@ -8,7 +8,6 @@ package org.opensearch.common.cache; - import org.opensearch.common.cache.stats.CacheStatsDimension; import java.util.List; diff --git a/server/src/main/java/org/opensearch/common/cache/stats/CacheStats.java b/server/src/main/java/org/opensearch/common/cache/stats/CacheStats.java index bc5d9a0d1df8c..7b24e3412c1f6 100644 --- a/server/src/main/java/org/opensearch/common/cache/stats/CacheStats.java +++ b/server/src/main/java/org/opensearch/common/cache/stats/CacheStats.java @@ -22,30 +22,43 @@ 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 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 dimensions); + long getMissesByDimensions(List dimensions); + long getEvictionsByDimensions(List dimensions); + long getMemorySizeByDimensions(List dimensions); - long getEntriesByDimensions(List dimensions); + long getEntriesByDimensions(List dimensions); void incrementHitsByDimensions(List dimensions); + void incrementMissesByDimensions(List dimensions); + void incrementEvictionsByDimensions(List dimensions); + // Can also use to decrement, with negative values void incrementMemorySizeByDimensions(List dimensions, long amountBytes); + void incrementEntriesByDimensions(List dimensions); + void decrementEntriesByDimensions(List dimensions); // Resets memory and entries stats but leaves the others; called when the cache clears itself. diff --git a/server/src/main/java/org/opensearch/common/cache/stats/CacheStatsDimension.java b/server/src/main/java/org/opensearch/common/cache/stats/CacheStatsDimension.java index 5ba83122f7fd2..9aee24efb46f0 100644 --- a/server/src/main/java/org/opensearch/common/cache/stats/CacheStatsDimension.java +++ b/server/src/main/java/org/opensearch/common/cache/stats/CacheStatsDimension.java @@ -20,6 +20,7 @@ public class CacheStatsDimension implements Writeable { public static final String TIER_DIMENSION_NAME = "tier"; public final String dimensionName; public final String dimensionValue; + public CacheStatsDimension(String dimensionName, String dimensionValue) { this.dimensionName = dimensionName; this.dimensionValue = dimensionValue; diff --git a/server/src/main/java/org/opensearch/common/cache/stats/CacheStatsResponse.java b/server/src/main/java/org/opensearch/common/cache/stats/CacheStatsResponse.java index fededa326d3e5..520a771510c43 100644 --- a/server/src/main/java/org/opensearch/common/cache/stats/CacheStatsResponse.java +++ b/server/src/main/java/org/opensearch/common/cache/stats/CacheStatsResponse.java @@ -44,7 +44,7 @@ public CacheStatsResponse(StreamInput in) throws IOException { } public CacheStatsResponse() { - this(0,0,0,0,0); + this(0, 0, 0, 0, 0); } public synchronized void add(CacheStatsResponse other) { diff --git a/server/src/main/java/org/opensearch/common/cache/stats/MultiDimensionCacheStats.java b/server/src/main/java/org/opensearch/common/cache/stats/MultiDimensionCacheStats.java index 7546d129973a2..1f977a7c040b3 100644 --- a/server/src/main/java/org/opensearch/common/cache/stats/MultiDimensionCacheStats.java +++ b/server/src/main/java/org/opensearch/common/cache/stats/MultiDimensionCacheStats.java @@ -13,7 +13,6 @@ import java.io.IOException; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -43,7 +42,8 @@ public class MultiDimensionCacheStats implements CacheStats { // Package-private for testing. final String tierDimensionValue; - // A map from a set of cache stats dimensions -> stats for that combination of dimensions. Does not include the tier dimension in its keys. + // A map from a set of cache stats dimensions -> stats for that combination of dimensions. Does not include the tier dimension in its + // keys. final ConcurrentMap map; final int maxDimensionValues; @@ -81,7 +81,7 @@ public void writeTo(StreamOutput out) throws IOException { map, (o, key) -> o.writeArray((o1, dim) -> ((CacheStatsDimension) dim).writeTo(o1), key.dimensions.toArray()), (o, response) -> response.writeTo(o) - ); + ); totalStats.writeTo(out); out.writeVInt(maxDimensionValues); } @@ -103,7 +103,8 @@ public CacheStatsResponse getStatsByDimensions(List dimensi CacheStatsDimension tierDim = getTierDimension(dimensions); if (tierDim == null || tierDim.dimensionValue.equals(tierDimensionValue)) { - // If there is no tier dimension, or if the tier dimension value matches the one for this stats object, return an aggregated response over the non-tier dimensions + // If there is no tier dimension, or if the tier dimension value matches the one for this stats object, return an aggregated + // response over the non-tier dimensions List modifiedDimensions = new ArrayList<>(dimensions); if (tierDim != null) { modifiedDimensions.remove(tierDim); @@ -262,19 +263,24 @@ private void internalIncrement(List dimensions, BiConsumer< */ static class Key { final Set dimensions; + Key(Set dimensions) { this.dimensions = Collections.unmodifiableSet(dimensions); } + Key(List dimensions) { this(new HashSet<>(dimensions)); } + @Override public boolean equals(Object o) { if (o == this) { return true; - } if (o == null) { + } + if (o == null) { return false; - } if (o.getClass() != Key.class) { + } + if (o.getClass() != Key.class) { return false; } Key other = (Key) o; diff --git a/server/src/main/java/org/opensearch/common/cache/store/OpenSearchOnHeapCache.java b/server/src/main/java/org/opensearch/common/cache/store/OpenSearchOnHeapCache.java index 6cb366ae48f33..e242d084ec2a7 100644 --- a/server/src/main/java/org/opensearch/common/cache/store/OpenSearchOnHeapCache.java +++ b/server/src/main/java/org/opensearch/common/cache/store/OpenSearchOnHeapCache.java @@ -12,26 +12,23 @@ import org.opensearch.common.cache.CacheBuilder; import org.opensearch.common.cache.CacheType; import org.opensearch.common.cache.ICache; +import org.opensearch.common.cache.ICacheKey; import org.opensearch.common.cache.LoadAwareCacheLoader; import org.opensearch.common.cache.RemovalListener; import org.opensearch.common.cache.RemovalNotification; import org.opensearch.common.cache.RemovalReason; import org.opensearch.common.cache.stats.CacheStats; -import org.opensearch.common.cache.ICacheKey; import org.opensearch.common.cache.stats.MultiDimensionCacheStats; import org.opensearch.common.cache.store.builders.ICacheBuilder; import org.opensearch.common.cache.store.config.CacheConfig; -import org.opensearch.common.settings.Settings; - -import java.util.List; -import java.util.Objects; - import org.opensearch.common.cache.store.settings.OpenSearchOnHeapCacheSettings; import org.opensearch.common.settings.Setting; +import org.opensearch.common.settings.Settings; import org.opensearch.core.common.unit.ByteSizeValue; +import java.util.List; import java.util.Map; -import java.util.function.ToLongBiFunction; +import java.util.Objects; import static org.opensearch.common.cache.store.settings.OpenSearchOnHeapCacheSettings.MAXIMUM_SIZE_IN_BYTES_KEY; @@ -132,7 +129,10 @@ public CacheStats stats() { public void onRemoval(RemovalNotification, V> notification) { removalListener.onRemoval(notification); stats.decrementEntriesByDimensions(notification.getKey().dimensions); - stats.incrementMemorySizeByDimensions(notification.getKey().dimensions, -cache.getWeigher().applyAsLong(notification.getKey(), notification.getValue())); + stats.incrementMemorySizeByDimensions( + notification.getKey().dimensions, + -cache.getWeigher().applyAsLong(notification.getKey(), notification.getValue()) + ); if (RemovalReason.EVICTED.equals(notification.getRemovalReason()) || RemovalReason.CAPACITY.equals(notification.getRemovalReason())) { @@ -151,8 +151,7 @@ public static class OpenSearchOnHeapCacheFactory implements Factory { public ICache create(CacheConfig config, CacheType cacheType, Map cacheFactories) { Map> settingList = OpenSearchOnHeapCacheSettings.getSettingListForCacheType(cacheType); Settings settings = config.getSettings(); - return new Builder() - .setDimensionNames(config.getDimensionNames()) + return new Builder().setDimensionNames(config.getDimensionNames()) .setMaximumWeightInBytes(((ByteSizeValue) settingList.get(MAXIMUM_SIZE_IN_BYTES_KEY).get(settings)).getBytes()) .setWeigher(config.getWeigher()) .setRemovalListener(config.getRemovalListener()) @@ -178,6 +177,7 @@ public Builder setDimensionNames(List dimensionNames) { this.dimensionNames = dimensionNames; return this; } + @Override public ICache build() { return new OpenSearchOnHeapCache(this); diff --git a/server/src/main/java/org/opensearch/common/cache/store/builders/ICacheBuilder.java b/server/src/main/java/org/opensearch/common/cache/store/builders/ICacheBuilder.java index 02b3c4f802536..3fc43767a03e7 100644 --- a/server/src/main/java/org/opensearch/common/cache/store/builders/ICacheBuilder.java +++ b/server/src/main/java/org/opensearch/common/cache/store/builders/ICacheBuilder.java @@ -10,8 +10,8 @@ import org.opensearch.common.annotation.ExperimentalApi; import org.opensearch.common.cache.ICache; -import org.opensearch.common.cache.RemovalListener; import org.opensearch.common.cache.ICacheKey; +import org.opensearch.common.cache.RemovalListener; import org.opensearch.common.settings.Settings; import org.opensearch.common.unit.TimeValue; diff --git a/server/src/main/java/org/opensearch/common/cache/store/config/CacheConfig.java b/server/src/main/java/org/opensearch/common/cache/store/config/CacheConfig.java index 0bf325cdd5a86..2c2a93ae9ab67 100644 --- a/server/src/main/java/org/opensearch/common/cache/store/config/CacheConfig.java +++ b/server/src/main/java/org/opensearch/common/cache/store/config/CacheConfig.java @@ -9,8 +9,8 @@ package org.opensearch.common.cache.store.config; import org.opensearch.common.annotation.ExperimentalApi; -import org.opensearch.common.cache.RemovalListener; import org.opensearch.common.cache.ICacheKey; +import org.opensearch.common.cache.RemovalListener; import org.opensearch.common.cache.serializer.Serializer; import org.opensearch.common.settings.Settings; diff --git a/server/src/main/java/org/opensearch/indices/IRCKeyWriteableSerializer.java b/server/src/main/java/org/opensearch/indices/IRCKeyWriteableSerializer.java index 56d16e84f3f42..b83957d4a2508 100644 --- a/server/src/main/java/org/opensearch/indices/IRCKeyWriteableSerializer.java +++ b/server/src/main/java/org/opensearch/indices/IRCKeyWriteableSerializer.java @@ -22,9 +22,7 @@ */ public class IRCKeyWriteableSerializer implements Serializer { - - public IRCKeyWriteableSerializer() { - } + public IRCKeyWriteableSerializer() {} @Override public byte[] serialize(IndicesRequestCache.Key object) { diff --git a/server/src/test/java/org/opensearch/common/cache/serializer/BytesReferenceSerializerTests.java b/server/src/test/java/org/opensearch/common/cache/serializer/BytesReferenceSerializerTests.java index d316cfe9630ae..b1d9e762d5df7 100644 --- a/server/src/test/java/org/opensearch/common/cache/serializer/BytesReferenceSerializerTests.java +++ b/server/src/test/java/org/opensearch/common/cache/serializer/BytesReferenceSerializerTests.java @@ -10,7 +10,6 @@ import org.opensearch.common.Randomness; import org.opensearch.common.bytes.ReleasableBytesReference; -import org.opensearch.common.cache.serializer.BytesReferenceSerializer; import org.opensearch.common.util.BigArrays; import org.opensearch.common.util.PageCacheRecycler; import org.opensearch.core.common.bytes.BytesArray; diff --git a/server/src/test/java/org/opensearch/common/cache/serializer/ICacheKeySerializerTests.java b/server/src/test/java/org/opensearch/common/cache/serializer/ICacheKeySerializerTests.java index e3875b64c3818..968d9dd64b01d 100644 --- a/server/src/test/java/org/opensearch/common/cache/serializer/ICacheKeySerializerTests.java +++ b/server/src/test/java/org/opensearch/common/cache/serializer/ICacheKeySerializerTests.java @@ -9,10 +9,8 @@ package org.opensearch.common.cache.serializer; import org.opensearch.common.Randomness; -import org.opensearch.common.cache.serializer.BytesReferenceSerializer; -import org.opensearch.common.cache.serializer.ICacheKeySerializer; -import org.opensearch.common.cache.stats.CacheStatsDimension; import org.opensearch.common.cache.ICacheKey; +import org.opensearch.common.cache.stats.CacheStatsDimension; import org.opensearch.core.common.bytes.BytesArray; import org.opensearch.core.common.bytes.BytesReference; import org.opensearch.test.OpenSearchTestCase; @@ -44,7 +42,7 @@ public void testDimNumbers() throws Exception { BytesReferenceSerializer keySer = new BytesReferenceSerializer(); ICacheKeySerializer serializer = new ICacheKeySerializer<>(keySer); - for (int numDims : new int[]{0, 5, 1000}) { + for (int numDims : new int[] { 0, 5, 1000 }) { List dims = new ArrayList<>(); for (int j = 0; j < numDims; j++) { dims.add(getRandomDim()); @@ -64,6 +62,7 @@ public void testHashCodes() throws Exception { assertEquals(key1, key2); assertEquals(key1.hashCode(), key2.hashCode()); } + public void testNullInputs() throws Exception { BytesReferenceSerializer keySer = new BytesReferenceSerializer(); ICacheKeySerializer serializer = new ICacheKeySerializer<>(keySer); diff --git a/server/src/test/java/org/opensearch/common/cache/stats/CacheStatsDimensionTests.java b/server/src/test/java/org/opensearch/common/cache/stats/CacheStatsDimensionTests.java index 0af171191cdd4..21c0c46991be5 100644 --- a/server/src/test/java/org/opensearch/common/cache/stats/CacheStatsDimensionTests.java +++ b/server/src/test/java/org/opensearch/common/cache/stats/CacheStatsDimensionTests.java @@ -34,7 +34,7 @@ public void testEquality() throws Exception { String value = "dimension_value"; CacheStatsDimension dim = new CacheStatsDimension(name, value); assertEquals(dim, new CacheStatsDimension(name, value)); - assertNotEquals(dim, new CacheStatsDimension("a", "b")); + assertNotEquals(dim, new CacheStatsDimension("a", "b")); assertNotEquals(dim, null); assertNotEquals(dim, new CacheStatsDimension(null, null)); } diff --git a/server/src/test/java/org/opensearch/common/cache/stats/MultiDimensionCacheStatsTests.java b/server/src/test/java/org/opensearch/common/cache/stats/MultiDimensionCacheStatsTests.java index 03cd831de14d4..63f747d63ff08 100644 --- a/server/src/test/java/org/opensearch/common/cache/stats/MultiDimensionCacheStatsTests.java +++ b/server/src/test/java/org/opensearch/common/cache/stats/MultiDimensionCacheStatsTests.java @@ -11,7 +11,6 @@ import org.opensearch.common.Randomness; import org.opensearch.common.io.stream.BytesStreamOutput; import org.opensearch.common.metrics.CounterMetric; -import org.opensearch.common.recycler.Recycler; import org.opensearch.core.common.bytes.BytesReference; import org.opensearch.core.common.io.stream.BytesStreamInput; import org.opensearch.test.OpenSearchTestCase; @@ -66,7 +65,12 @@ public void testAddAndGet() throws Exception { // test gets for aggregations of values: for example, dim1="a", dim2="b", but dim3 and dim4 can be anything // test a random subset of these, there are combinatorially many possibilities for (int i = 0; i < 1000; i++) { - List aggregationDims = getRandomDimList(stats.dimensionNames, usedDimensionValues, false, Randomness.get()); + List aggregationDims = getRandomDimList( + stats.dimensionNames, + usedDimensionValues, + false, + Randomness.get() + ); CacheStatsResponse expectedResponse = new CacheStatsResponse(); for (Set dimSet : expected.keySet()) { if (dimSet.containsAll(aggregationDims)) { @@ -131,7 +135,12 @@ public void testTierLogic() throws Exception { CacheStatsDimension wrongTierDim = new CacheStatsDimension(CacheStatsDimension.TIER_DIMENSION_NAME, "wrong_value"); for (int i = 0; i < 1000; i++) { - List aggregationDims = getRandomDimList(stats.dimensionNames, usedDimensionValues, false, Randomness.get()); + List aggregationDims = getRandomDimList( + stats.dimensionNames, + usedDimensionValues, + false, + Randomness.get() + ); List aggDimsWithTier = new ArrayList<>(aggregationDims); aggDimsWithTier.add(tierDim); @@ -218,7 +227,12 @@ private Map> getUsedDimensionValues(MultiDimensionCacheStat return usedDimensionValues; } - private Map, CacheStatsResponse> populateStats(MultiDimensionCacheStats stats, Map> usedDimensionValues, int numDistinctValuePairs, int numRepetitionsPerValue) { + private Map, CacheStatsResponse> populateStats( + MultiDimensionCacheStats stats, + Map> usedDimensionValues, + int numDistinctValuePairs, + int numRepetitionsPerValue + ) { Map, CacheStatsResponse> expected = new HashMap<>(); Random rand = Randomness.get(); @@ -272,10 +286,16 @@ private Map, CacheStatsResponse> populateStats(MultiDim return expected; } - private List getRandomDimList(List dimensionNames, Map> usedDimensionValues, boolean pickValueForAllDims, Random rand) { + private List getRandomDimList( + List dimensionNames, + Map> usedDimensionValues, + boolean pickValueForAllDims, + Random rand + ) { List result = new ArrayList<>(); for (String dimName : dimensionNames) { - if (pickValueForAllDims || rand.nextBoolean()) { // if pickValueForAllDims, always pick a value for each dimension, otherwise do so 50% of the time + if (pickValueForAllDims || rand.nextBoolean()) { // if pickValueForAllDims, always pick a value for each dimension, otherwise do + // so 50% of the time int index = between(0, usedDimensionValues.get(dimName).size() - 1); result.add(new CacheStatsDimension(dimName, usedDimensionValues.get(dimName).get(index))); } diff --git a/server/src/test/java/org/opensearch/common/cache/store/OpenSearchOnHeapCacheTests.java b/server/src/test/java/org/opensearch/common/cache/store/OpenSearchOnHeapCacheTests.java index 22d8813aede19..b02195b67437d 100644 --- a/server/src/test/java/org/opensearch/common/cache/store/OpenSearchOnHeapCacheTests.java +++ b/server/src/test/java/org/opensearch/common/cache/store/OpenSearchOnHeapCacheTests.java @@ -30,6 +30,7 @@ public class OpenSearchOnHeapCacheTests extends OpenSearchTestCase { private final static long keyValueSize = 50; private final static List dimensionNames = List.of("dim1", "dim2"); + public void testStats() throws Exception { MockRemovalListener listener = new MockRemovalListener<>(); int maxKeys = between(10, 50); @@ -85,8 +86,7 @@ private OpenSearchOnHeapCache getCache(int maxSizeKeys, MockRemo ) .build(); - CacheConfig cacheConfig = new CacheConfig.Builder() - .setKeyType(String.class) + CacheConfig cacheConfig = new CacheConfig.Builder().setKeyType(String.class) .setValueType(String.class) .setWeigher((k, v) -> keyValueSize) .setRemovalListener(listener) @@ -98,6 +98,7 @@ private OpenSearchOnHeapCache getCache(int maxSizeKeys, MockRemo private static class MockRemovalListener implements RemovalListener, V> { CounterMetric numRemovals; + MockRemovalListener() { numRemovals = new CounterMetric(); } diff --git a/server/src/test/java/org/opensearch/indices/IRCKeyWriteableSerializerTests.java b/server/src/test/java/org/opensearch/indices/IRCKeyWriteableSerializerTests.java index a4a97b5c8470f..af657dadd7a1a 100644 --- a/server/src/test/java/org/opensearch/indices/IRCKeyWriteableSerializerTests.java +++ b/server/src/test/java/org/opensearch/indices/IRCKeyWriteableSerializerTests.java @@ -39,11 +39,7 @@ public void testSerializer() throws Exception { } } - private IndicesRequestCache.Key getRandomIRCKey( - int valueLength, - Random random, - ShardId shard - ) { + private IndicesRequestCache.Key getRandomIRCKey(int valueLength, Random random, ShardId shard) { byte[] value = new byte[valueLength]; for (int i = 0; i < valueLength; i++) { value[i] = (byte) (random.nextInt(126 - 32) + 32);