Skip to content

Commit

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

---------


(cherry picked from commit 902a10b)

Signed-off-by: Sagar Upadhyaya <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent e1403a2 commit a9ff805
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 a9ff805

Please sign in to comment.