Skip to content

Commit ea33af8

Browse files
author
Peter Alfonsi
committed
spotlessApply
Signed-off-by: Peter Alfonsi <[email protected]>
1 parent 60df761 commit ea33af8

File tree

20 files changed

+143
-84
lines changed

20 files changed

+143
-84
lines changed

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public class TieredSpilloverCache<K, V> implements ICache<K, V> {
4848
private final ICache<K, V> onHeapCache;
4949

5050
// TODO: Listeners for removals from the two tiers
51-
//private final RemovalListener<ICacheKey<K>, V> onDiskRemovalListener;
52-
//private final RemovalListener<ICacheKey<K>, V> onHeapRemovalListener;
51+
// private final RemovalListener<ICacheKey<K>, V> onDiskRemovalListener;
52+
// private final RemovalListener<ICacheKey<K>, V> onHeapRemovalListener;
5353

5454
// The listener for removals from the spillover cache as a whole
5555
private final RemovalListener<ICacheKey<K>, V> removalListener;
@@ -67,21 +67,20 @@ public class TieredSpilloverCache<K, V> implements ICache<K, V> {
6767
Objects.requireNonNull(builder.diskCacheFactory, "disk cache builder can't be null");
6868
this.removalListener = Objects.requireNonNull(builder.removalListener, "Removal listener can't be null");
6969

70-
this.onHeapCache = builder.onHeapCacheFactory.create(
71-
new CacheConfig.Builder<K, V>().setRemovalListener(new RemovalListener<>() {
72-
@Override
73-
public void onRemoval(RemovalNotification<ICacheKey<K>, V> notification) {
74-
try (ReleasableLock ignore = writeLock.acquire()) {
75-
diskCache.put(notification.getKey(), notification.getValue());
76-
}
77-
removalListener.onRemoval(notification);
78-
}
79-
})
80-
.setKeyType(builder.cacheConfig.getKeyType())
81-
.setValueType(builder.cacheConfig.getValueType())
82-
.setSettings(builder.cacheConfig.getSettings())
83-
.setWeigher(builder.cacheConfig.getWeigher())
84-
.build(),
70+
this.onHeapCache = builder.onHeapCacheFactory.create(new CacheConfig.Builder<K, V>().setRemovalListener(new RemovalListener<>() {
71+
@Override
72+
public void onRemoval(RemovalNotification<ICacheKey<K>, V> notification) {
73+
try (ReleasableLock ignore = writeLock.acquire()) {
74+
diskCache.put(notification.getKey(), notification.getValue());
75+
}
76+
removalListener.onRemoval(notification);
77+
}
78+
})
79+
.setKeyType(builder.cacheConfig.getKeyType())
80+
.setValueType(builder.cacheConfig.getValueType())
81+
.setSettings(builder.cacheConfig.getSettings())
82+
.setWeigher(builder.cacheConfig.getWeigher())
83+
.build(),
8584
builder.cacheType,
8685
builder.cacheFactories
8786

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import static org.opensearch.common.cache.store.settings.OpenSearchOnHeapCacheSettings.MAXIMUM_SIZE_IN_BYTES_KEY;
3131

3232
public class TieredSpilloverCacheTests extends OpenSearchTestCase {
33-
// TODO: TSC has no stats implementation yet - fix these tests once it does
33+
// TODO: These tests are uncommented in the second stats rework PR, which adds a TSC stats implementation
3434
/*public void testComputeIfAbsentWithoutAnyOnHeapCacheEviction() throws Exception {
3535
int onHeapCacheSize = randomIntBetween(10, 30);
3636
int keyValueSize = 50;

plugins/cache-ehcache/src/main/java/org/opensearch/cache/EhcacheCachePlugin.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.opensearch.plugins.Plugin;
1717

1818
import java.util.ArrayList;
19-
import java.util.HashMap;
2019
import java.util.List;
2120
import java.util.Map;
2221

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,22 @@
1010

1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
13-
import org.ehcache.core.spi.service.FileBasedPersistenceContext;
14-
import org.ehcache.spi.serialization.SerializerException;
1513
import org.opensearch.OpenSearchException;
1614
import org.opensearch.cache.EhcacheDiskCacheSettings;
1715
import org.opensearch.common.SuppressForbidden;
1816
import org.opensearch.common.annotation.ExperimentalApi;
1917
import org.opensearch.common.cache.CacheType;
2018
import org.opensearch.common.cache.ICache;
19+
import org.opensearch.common.cache.ICacheKey;
2120
import org.opensearch.common.cache.LoadAwareCacheLoader;
2221
import org.opensearch.common.cache.RemovalListener;
2322
import org.opensearch.common.cache.RemovalNotification;
2423
import org.opensearch.common.cache.RemovalReason;
24+
import org.opensearch.common.cache.serializer.ICacheKeySerializer;
25+
import org.opensearch.common.cache.serializer.Serializer;
2526
import org.opensearch.common.cache.stats.CacheStats;
26-
import org.opensearch.common.cache.ICacheKey;
27-
import org.opensearch.common.cache.stats.CacheStatsDimension;
2827
import org.opensearch.common.cache.stats.MultiDimensionCacheStats;
2928
import org.opensearch.common.cache.store.builders.ICacheBuilder;
30-
import org.opensearch.common.cache.serializer.ICacheKeySerializer;
31-
import org.opensearch.common.cache.serializer.Serializer;
3229
import org.opensearch.common.cache.store.config.CacheConfig;
3330
import org.opensearch.common.collect.Tuple;
3431
import org.opensearch.common.settings.Setting;
@@ -60,13 +57,15 @@
6057
import org.ehcache.config.builders.PooledExecutionServiceConfigurationBuilder;
6158
import org.ehcache.config.builders.ResourcePoolsBuilder;
6259
import org.ehcache.config.units.MemoryUnit;
60+
import org.ehcache.core.spi.service.FileBasedPersistenceContext;
6361
import org.ehcache.event.CacheEvent;
6462
import org.ehcache.event.CacheEventListener;
6563
import org.ehcache.event.EventType;
6664
import org.ehcache.expiry.ExpiryPolicy;
6765
import org.ehcache.impl.config.store.disk.OffHeapDiskStoreConfiguration;
6866
import org.ehcache.spi.loaderwriter.CacheLoadingException;
6967
import org.ehcache.spi.loaderwriter.CacheWritingException;
68+
import org.ehcache.spi.serialization.SerializerException;
7069

7170
import static org.opensearch.cache.EhcacheDiskCacheSettings.DISK_CACHE_ALIAS_KEY;
7271
import static org.opensearch.cache.EhcacheDiskCacheSettings.DISK_CACHE_EXPIRE_AFTER_ACCESS_KEY;
@@ -153,7 +152,8 @@ private EhcacheDiskCache(Builder<K, V> builder) {
153152
this.ehCacheEventListener = new EhCacheEventListener<K, V>(
154153
Objects.requireNonNull(builder.getRemovalListener(), "Removal listener can't be null"),
155154
Objects.requireNonNull(builder.getWeigher(), "Weigher function can't be null"),
156-
this.valueSerializer);
155+
this.valueSerializer
156+
);
157157
this.cache = buildCache(Duration.ofMillis(expireAfterAccess.getMillis()), builder);
158158
List<String> dimensionNames = Objects.requireNonNull(builder.dimensionNames, "Dimension names can't be null");
159159
this.stats = new MultiDimensionCacheStats(dimensionNames, TIER_DIMENSION_VALUE);
@@ -456,9 +456,11 @@ class EhCacheEventListener<K, V> implements CacheEventListener<ICacheKey<K>, byt
456456
private ToLongBiFunction<ICacheKey<K>, V> weigher;
457457
private Serializer<V, byte[]> valueSerializer;
458458

459-
EhCacheEventListener(RemovalListener<ICacheKey<K>, V> removalListener,
460-
ToLongBiFunction<ICacheKey<K>, V> weigher,
461-
Serializer<V, byte[]> valueSerializer) {
459+
EhCacheEventListener(
460+
RemovalListener<ICacheKey<K>, V> removalListener,
461+
ToLongBiFunction<ICacheKey<K>, V> weigher,
462+
Serializer<V, byte[]> valueSerializer
463+
) {
462464
this.removalListener = removalListener;
463465
this.weigher = weigher;
464466
this.valueSerializer = valueSerializer;
@@ -481,20 +483,30 @@ public void onEvent(CacheEvent<? extends ICacheKey<K>, ? extends byte[]> event)
481483
assert event.getOldValue() == null;
482484
break;
483485
case EVICTED:
484-
this.removalListener.onRemoval(new RemovalNotification<>(event.getKey(), valueSerializer.deserialize(event.getOldValue()), RemovalReason.EVICTED));
486+
this.removalListener.onRemoval(
487+
new RemovalNotification<>(event.getKey(), valueSerializer.deserialize(event.getOldValue()), RemovalReason.EVICTED)
488+
);
485489
stats.decrementEntriesByDimensions(event.getKey().dimensions);
486490
stats.incrementMemorySizeByDimensions(event.getKey().dimensions, -getOldValuePairSize(event));
487491
stats.incrementEvictionsByDimensions(event.getKey().dimensions);
488492
assert event.getNewValue() == null;
489493
break;
490494
case REMOVED:
491-
this.removalListener.onRemoval(new RemovalNotification<>(event.getKey(), valueSerializer.deserialize(event.getOldValue()), RemovalReason.EXPLICIT));
495+
this.removalListener.onRemoval(
496+
new RemovalNotification<>(event.getKey(), valueSerializer.deserialize(event.getOldValue()), RemovalReason.EXPLICIT)
497+
);
492498
stats.decrementEntriesByDimensions(event.getKey().dimensions);
493499
stats.incrementMemorySizeByDimensions(event.getKey().dimensions, -getOldValuePairSize(event));
494500
assert event.getNewValue() == null;
495501
break;
496502
case EXPIRED:
497-
this.removalListener.onRemoval(new RemovalNotification<>(event.getKey(), valueSerializer.deserialize(event.getOldValue()), RemovalReason.INVALIDATED));
503+
this.removalListener.onRemoval(
504+
new RemovalNotification<>(
505+
event.getKey(),
506+
valueSerializer.deserialize(event.getOldValue()),
507+
RemovalReason.INVALIDATED
508+
)
509+
);
498510
stats.decrementEntriesByDimensions(event.getKey().dimensions);
499511
stats.incrementMemorySizeByDimensions(event.getKey().dimensions, -getOldValuePairSize(event));
500512
assert event.getNewValue() == null;
@@ -512,6 +524,7 @@ public void onEvent(CacheEvent<? extends ICacheKey<K>, ? extends byte[]> event)
512524

513525
private class KeySerializerWrapper implements org.ehcache.spi.serialization.Serializer<ICacheKey> {
514526
private ICacheKeySerializer<K> serializer;
527+
515528
public KeySerializerWrapper(Serializer<K, byte[]> internalKeySerializer) {
516529
this.serializer = new ICacheKeySerializer<>(internalKeySerializer);
517530
}
@@ -520,6 +533,7 @@ public KeySerializerWrapper(Serializer<K, byte[]> internalKeySerializer) {
520533
// cache after a restart.
521534
// See https://www.ehcache.org/documentation/3.0/serializers-copiers.html#persistent-vs-transient-caches
522535
public KeySerializerWrapper(ClassLoader classLoader, FileBasedPersistenceContext persistenceContext) {}
536+
523537
@Override
524538
public ByteBuffer serialize(ICacheKey object) throws SerializerException {
525539
return ByteBuffer.wrap(serializer.serialize(object));
@@ -705,7 +719,7 @@ public Builder<K, V> setValueSerializer(Serializer<V, byte[]> valueSerializer) {
705719
return this;
706720
}
707721

708-
//@Override
722+
// @Override
709723
public EhcacheDiskCache<K, V> build() {
710724
return new EhcacheDiskCache<>(this);
711725
}

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
import org.opensearch.cache.EhcacheDiskCacheSettings;
1212
import org.opensearch.common.cache.CacheType;
1313
import org.opensearch.common.cache.ICache;
14+
import org.opensearch.common.cache.ICacheKey;
1415
import org.opensearch.common.cache.LoadAwareCacheLoader;
1516
import org.opensearch.common.cache.RemovalListener;
1617
import org.opensearch.common.cache.RemovalNotification;
17-
import org.opensearch.common.cache.stats.CacheStatsDimension;
18-
import org.opensearch.common.cache.ICacheKey;
1918
import org.opensearch.common.cache.serializer.Serializer;
19+
import org.opensearch.common.cache.stats.CacheStatsDimension;
2020
import org.opensearch.common.cache.store.config.CacheConfig;
2121
import org.opensearch.common.settings.Settings;
2222
import org.opensearch.common.unit.TimeValue;
@@ -39,9 +39,9 @@
3939
import java.util.concurrent.atomic.AtomicInteger;
4040
import java.util.function.ToLongBiFunction;
4141

42-
import static org.hamcrest.CoreMatchers.instanceOf;
4342
import static org.opensearch.cache.EhcacheDiskCacheSettings.DISK_MAX_SIZE_IN_BYTES_KEY;
4443
import static org.opensearch.cache.EhcacheDiskCacheSettings.DISK_STORAGE_PATH_KEY;
44+
import static org.hamcrest.CoreMatchers.instanceOf;
4545

4646
public class EhCacheDiskCacheTests extends OpenSearchSingleNodeTestCase {
4747

@@ -500,7 +500,7 @@ public void testComputeIfAbsentWithNullValueLoading() throws Exception {
500500
// Try to hit different request with the same key concurrently. Loader throws exception.
501501
for (int i = 0; i < numberOfRequest; i++) {
502502
threads[i] = new Thread(() -> {
503-
LoadAwareCacheLoader<ICacheKey<String >, String> loadAwareCacheLoader = new LoadAwareCacheLoader<>() {
503+
LoadAwareCacheLoader<ICacheKey<String>, String> loadAwareCacheLoader = new LoadAwareCacheLoader<>() {
504504
boolean isLoaded;
505505

506506
@Override
@@ -543,7 +543,10 @@ public void testMemoryTracking() throws Exception {
543543
ToLongBiFunction<ICacheKey<String>, String> weigher = getWeigher();
544544
int initialKeyLength = 40;
545545
int initialValueLength = 40;
546-
long sizeForOneInitialEntry = weigher.applyAsLong(new ICacheKey<>(generateRandomString(initialKeyLength), getMockDimensions()), generateRandomString(initialValueLength));
546+
long sizeForOneInitialEntry = weigher.applyAsLong(
547+
new ICacheKey<>(generateRandomString(initialKeyLength), getMockDimensions()),
548+
generateRandomString(initialValueLength)
549+
);
547550
int maxEntries = 2000;
548551
try (NodeEnvironment env = newNodeEnvironment(settings)) {
549552
ICache<String, String> ehcacheTest = new EhcacheDiskCache.Builder<String, String>().setDiskCacheAlias("test1")
@@ -603,7 +606,7 @@ public void testMemoryTracking() throws Exception {
603606
}
604607
// TODO: Ehcache incorrectly evicts at 30-40% of max size. Fix this test once we figure out why.
605608
// Since the EVICTED and EXPIRED cases use the same code as REMOVED, we should be ok on testing them for now.
606-
//assertEquals(maxEntries * sizeForOneInitialEntry, ehcacheTest.stats().getTotalMemorySize());
609+
// assertEquals(maxEntries * sizeForOneInitialEntry, ehcacheTest.stats().getTotalMemorySize());
607610

608611
ehcacheTest.close();
609612
}
@@ -632,8 +635,18 @@ public void testGetStatsByTierName() throws Exception {
632635
for (int i = 0; i < randomKeys; i++) {
633636
ehcacheTest.put(getICacheKey(UUID.randomUUID().toString()), UUID.randomUUID().toString());
634637
}
635-
assertEquals(randomKeys, ehcacheTest.stats().getEntriesByDimensions(List.of(new CacheStatsDimension(CacheStatsDimension.TIER_DIMENSION_NAME, EhcacheDiskCache.TIER_DIMENSION_VALUE))));
636-
assertEquals(0, ehcacheTest.stats().getEntriesByDimensions(List.of(new CacheStatsDimension(CacheStatsDimension.TIER_DIMENSION_NAME, "other_tier_value"))));
638+
assertEquals(
639+
randomKeys,
640+
ehcacheTest.stats()
641+
.getEntriesByDimensions(
642+
List.of(new CacheStatsDimension(CacheStatsDimension.TIER_DIMENSION_NAME, EhcacheDiskCache.TIER_DIMENSION_VALUE))
643+
)
644+
);
645+
assertEquals(
646+
0,
647+
ehcacheTest.stats()
648+
.getEntriesByDimensions(List.of(new CacheStatsDimension(CacheStatsDimension.TIER_DIMENSION_NAME, "other_tier_value")))
649+
);
637650

638651
ehcacheTest.close();
639652
}
@@ -676,6 +689,7 @@ private ToLongBiFunction<ICacheKey<String>, String> getWeigher() {
676689

677690
class MockRemovalListener<K, V> implements RemovalListener<ICacheKey<K>, V> {
678691
AtomicInteger onRemovalCount = new AtomicInteger();
692+
679693
@Override
680694
public void onRemoval(RemovalNotification<ICacheKey<K>, V> notification) {
681695
onRemovalCount.incrementAndGet();
@@ -684,6 +698,7 @@ public void onRemoval(RemovalNotification<ICacheKey<K>, V> notification) {
684698

685699
static class StringSerializer implements Serializer<String, byte[]> {
686700
private final Charset charset = StandardCharsets.UTF_8;
701+
687702
@Override
688703
public byte[] serialize(String object) {
689704
return object.getBytes(charset);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
package org.opensearch.common.cache;
1010

11-
1211
import org.opensearch.common.cache.stats.CacheStatsDimension;
1312

1413
import java.util.List;

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,43 @@ public interface CacheStats extends Writeable {
2222

2323
// Methods to get all 5 values at once, either in total or for a specific set of dimensions.
2424
CacheStatsResponse getTotalStats();
25+
2526
CacheStatsResponse getStatsByDimensions(List<CacheStatsDimension> dimensions);
2627

2728
// Methods to get total values.
2829
long getTotalHits();
30+
2931
long getTotalMisses();
32+
3033
long getTotalEvictions();
34+
3135
long getTotalMemorySize();
36+
3237
long getTotalEntries();
3338

3439
// Methods to get values for a specific set of dimensions.
3540
// Returns the sum of values for cache entries that match all dimensions in the list.
3641
long getHitsByDimensions(List<CacheStatsDimension> dimensions);
42+
3743
long getMissesByDimensions(List<CacheStatsDimension> dimensions);
44+
3845
long getEvictionsByDimensions(List<CacheStatsDimension> dimensions);
46+
3947
long getMemorySizeByDimensions(List<CacheStatsDimension> dimensions);
40-
long getEntriesByDimensions(List<CacheStatsDimension> dimensions);
4148

49+
long getEntriesByDimensions(List<CacheStatsDimension> dimensions);
4250

4351
void incrementHitsByDimensions(List<CacheStatsDimension> dimensions);
52+
4453
void incrementMissesByDimensions(List<CacheStatsDimension> dimensions);
54+
4555
void incrementEvictionsByDimensions(List<CacheStatsDimension> dimensions);
56+
4657
// Can also use to decrement, with negative values
4758
void incrementMemorySizeByDimensions(List<CacheStatsDimension> dimensions, long amountBytes);
59+
4860
void incrementEntriesByDimensions(List<CacheStatsDimension> dimensions);
61+
4962
void decrementEntriesByDimensions(List<CacheStatsDimension> dimensions);
5063

5164
// Resets memory and entries stats but leaves the others; called when the cache clears itself.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class CacheStatsDimension implements Writeable {
2020
public static final String TIER_DIMENSION_NAME = "tier";
2121
public final String dimensionName;
2222
public final String dimensionValue;
23+
2324
public CacheStatsDimension(String dimensionName, String dimensionValue) {
2425
this.dimensionName = dimensionName;
2526
this.dimensionValue = dimensionValue;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public CacheStatsResponse(StreamInput in) throws IOException {
4444
}
4545

4646
public CacheStatsResponse() {
47-
this(0,0,0,0,0);
47+
this(0, 0, 0, 0, 0);
4848
}
4949

5050
public synchronized void add(CacheStatsResponse other) {

0 commit comments

Comments
 (0)