Skip to content

Commit

Permalink
[Tiered Caching] Fix test testComputeIfAbsentWithFactoryBasedCacheCre…
Browse files Browse the repository at this point in the history
…ation (opensearch-project#12700)


---------

Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com>
  • Loading branch information
sgup432 authored and Peter Alfonsi committed Aug 30, 2024
1 parent 0fc1f40 commit bf6488d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public void onRemoval(RemovalNotification<K, V> notification) {
.setValueType(builder.cacheConfig.getValueType())
.setSettings(builder.cacheConfig.getSettings())
.setWeigher(builder.cacheConfig.getWeigher())
.setMaxSizeInBytes(builder.cacheConfig.getMaxSizeInBytes()) // TODO: Part of a workaround for an issue in TSC. Overall fix
// coming soon
.setMaxSizeInBytes(builder.cacheConfig.getMaxSizeInBytes())
.setExpireAfterAccess(builder.cacheConfig.getExpireAfterAccess())
.build(),
builder.cacheType,
builder.cacheFactories
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ public void testComputeIfAbsentWithFactoryBasedCacheCreation() throws Exception
.getKey(),
onHeapCacheSize * keyValueSize + "b"
)
.put(
CacheSettings.getConcreteStoreNameSettingForCacheType(CacheType.INDICES_REQUEST_CACHE).getKey(),
TieredSpilloverCache.TieredSpilloverCacheFactory.TIERED_SPILLOVER_CACHE_NAME
)
.put(FeatureFlags.PLUGGABLE_CACHE, "true")
.build();

ICache<String, String> tieredSpilloverICache = new TieredSpilloverCache.TieredSpilloverCacheFactory().create(
Expand All @@ -127,12 +132,8 @@ public void testComputeIfAbsentWithFactoryBasedCacheCreation() throws Exception
.setWeigher((k, v) -> keyValueSize)
.setRemovalListener(removalListener)
.setSettings(settings)
.setCachedResultParser(new Function<String, CachedQueryResult.PolicyValues>() {
@Override
public CachedQueryResult.PolicyValues apply(String s) {
return new CachedQueryResult.PolicyValues(20_000_000L);
}
}) // Values will always appear to have taken 20_000_000 ns = 20 ms to compute
.setCachedResultParser(s -> new CachedQueryResult.PolicyValues(20_000_000L)) // Values will always appear to have taken
// 20_000_000 ns = 20 ms to compute
.build(),
CacheType.INDICES_REQUEST_CACHE,
Map.of(
Expand All @@ -145,20 +146,16 @@ public CachedQueryResult.PolicyValues apply(String s) {

TieredSpilloverCache<String, String> tieredSpilloverCache = (TieredSpilloverCache<String, String>) tieredSpilloverICache;

// Put values in cache more than it's size and cause evictions from onHeap.
int numOfItems1 = randomIntBetween(onHeapCacheSize + 1, totalSize);
List<String> onHeapKeys = new ArrayList<>();
List<String> diskTierKeys = new ArrayList<>();
for (int iter = 0; iter < numOfItems1; iter++) {
String key = UUID.randomUUID().toString();
LoadAwareCacheLoader<String, String> tieredCacheLoader = getLoadAwareCacheLoader();
tieredSpilloverCache.computeIfAbsent(key, tieredCacheLoader);
}
tieredSpilloverCache.getOnHeapCache().keys().forEach(onHeapKeys::add);
tieredSpilloverCache.getDiskCache().keys().forEach(diskTierKeys::add);

assertEquals(tieredSpilloverCache.getOnHeapCache().count(), onHeapKeys.size());
assertEquals(tieredSpilloverCache.getDiskCache().count(), diskTierKeys.size());
// Verify on heap cache size.
assertEquals(onHeapCacheSize, tieredSpilloverCache.getOnHeapCache().count());
// Verify disk cache size.
assertEquals(numOfItems1 - onHeapCacheSize, tieredSpilloverCache.getDiskCache().count());
}

public void testWithFactoryCreationWithOnHeapCacheNotPresent() {
Expand All @@ -180,6 +177,11 @@ public void testWithFactoryCreationWithOnHeapCacheNotPresent() {
.getKey(),
onHeapCacheSize * keyValueSize + "b"
)
.put(
CacheSettings.getConcreteStoreNameSettingForCacheType(CacheType.INDICES_REQUEST_CACHE).getKey(),
TieredSpilloverCache.TieredSpilloverCacheFactory.TIERED_SPILLOVER_CACHE_NAME
)
.put(FeatureFlags.PLUGGABLE_CACHE, "true")
.build();

IllegalArgumentException ex = assertThrows(
Expand Down

0 comments on commit bf6488d

Please sign in to comment.