diff --git a/modules/cache-common/src/test/java/org/opensearch/cache/common/query/TieredQueryCacheTests.java b/modules/cache-common/src/test/java/org/opensearch/cache/common/query/TieredQueryCacheTests.java index acbaede9e8b79..2684776af8508 100644 --- a/modules/cache-common/src/test/java/org/opensearch/cache/common/query/TieredQueryCacheTests.java +++ b/modules/cache-common/src/test/java/org/opensearch/cache/common/query/TieredQueryCacheTests.java @@ -20,11 +20,13 @@ import org.opensearch.cache.common.tier.TieredSpilloverCache; import org.opensearch.cache.common.tier.TieredSpilloverCachePlugin; import org.opensearch.cache.common.tier.TieredSpilloverCacheSettings; +import org.opensearch.cache.common.tier.TieredSpilloverCacheTests; import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.cache.CacheType; import org.opensearch.common.cache.ICache; import org.opensearch.common.cache.module.CacheModule; import org.opensearch.common.cache.settings.CacheSettings; +import org.opensearch.common.cache.stats.ImmutableCacheStats; import org.opensearch.common.cache.store.OpenSearchOnHeapCache; import org.opensearch.common.lucene.index.OpenSearchDirectoryReader; import org.opensearch.common.settings.Settings; @@ -45,7 +47,9 @@ import java.util.List; import java.util.Map; +import static org.opensearch.cache.common.query.TieredQueryCache.SHARD_ID_DIMENSION_NAME; import static org.opensearch.cache.common.tier.TieredSpilloverCacheSettings.DISK_CACHE_ENABLED_SETTING_MAP; +import static org.opensearch.cache.common.tier.TieredSpilloverCacheStatsHolder.TIER_DIMENSION_VALUE_DISK; public class TieredQueryCacheTests extends OpenSearchSingleNodeTestCase { @@ -139,6 +143,19 @@ public void testBasics_WithTSC_WithSmallHeapSize() throws Exception { testBasicsDummyQuery(cache, s, shard); + // Explicitly check disk cache had items and hits + TieredSpilloverCache tsc = (TieredSpilloverCache< + TieredQueryCache.CompositeKey, + TieredQueryCache.CacheAndCount>) cache.getInnerCache(); + ImmutableCacheStats diskTierStats = TieredSpilloverCacheTests.getStatsSnapshotForTier( + tsc, + TIER_DIMENSION_VALUE_DISK, + List.of(SHARD_ID_DIMENSION_NAME), + List.of(shard.toString()) + ); + assertTrue(diskTierStats.getItems() > 0); + assertTrue(diskTierStats.getHits() > 0); + cache.close(); IOUtils.close(r, dir); } @@ -160,7 +177,7 @@ private void testBasicsDummyQuery(TieredQueryCache cache, IndexSearcher s, Shard assertEquals(2L, stats.getMissCount()); assertTrue(stats.getMemorySizeInBytes() > 0L && stats.getMemorySizeInBytes() < Long.MAX_VALUE); - int numEntries = 3; + int numEntries = 20; for (int i = 1; i < numEntries; ++i) { assertEquals(1, s.count(new DummyQuery(i))); @@ -173,7 +190,7 @@ private void testBasicsDummyQuery(TieredQueryCache cache, IndexSearcher s, Shard assertEquals(2 * numEntries, stats.getMissCount()); assertTrue(stats.getMemorySizeInBytes() > 0L && stats.getMemorySizeInBytes() < Long.MAX_VALUE); - s.count(new DummyQuery(numEntries - 1)); + s.count(new DummyQuery(1)); // Pick 1 so the hit comes from disk stats = cache.getStats(shard); // assertEquals(10L, stats.getCacheSize()); 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 3bb1321f9faf2..4989518f41506 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 @@ -2166,7 +2166,7 @@ public void testDropStatsForDimensions() throws Exception { assertEquals(new ImmutableCacheStats(0, 0, 0, 0, 0), tieredSpilloverCache.stats().getTotalStats()); } - private List getMockDimensions() { + static private List getMockDimensions() { List dims = new ArrayList<>(); for (String dimensionName : dimensionNames) { dims.add("0"); @@ -2416,7 +2416,7 @@ private long getItemsForTier(TieredSpilloverCache tsc, String tierValue) t return getStatsSnapshotForTier(tsc, tierValue).getItems(); } - private ImmutableCacheStats getStatsSnapshotForTier(TieredSpilloverCache tsc, String tierValue) throws IOException { + private ImmutableCacheStats getStatsSnapshotForTier(TieredSpilloverCache tsc, String tierValue) { List levelsList = new ArrayList<>(dimensionNames); levelsList.add(TIER_DIMENSION_NAME); String[] levels = levelsList.toArray(new String[0]); @@ -2433,6 +2433,26 @@ private ImmutableCacheStats getStatsSnapshotForTier(TieredSpilloverCache t return snapshot; } + public static ImmutableCacheStats getStatsSnapshotForTier( + TieredSpilloverCache tsc, + String tierValue, + List dimensionNames, + List dimensionValues + ) throws IOException { + List levelsList = new ArrayList<>(dimensionNames); + levelsList.add(TIER_DIMENSION_NAME); + String[] levels = levelsList.toArray(new String[0]); + ImmutableCacheStatsHolder cacheStats = tsc.stats(levels); + List finalValues = new ArrayList<>(dimensionValues); + finalValues.add(tierValue); + ImmutableCacheStats snapshot = cacheStats.getStatsForDimensionValues(finalValues); + if (snapshot == null) { + return new ImmutableCacheStats(0, 0, 0, 0, 0); // This can happen if no cache actions have happened for this set of + // dimensions yet + } + return snapshot; + } + private String getStoragePath(Settings settings) { String storagePath; try (NodeEnvironment environment = newNodeEnvironment(settings)) {