Skip to content

Commit

Permalink
Explicitly check disk cache is used in test
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <[email protected]>
  • Loading branch information
Peter Alfonsi committed Dec 6, 2024
1 parent f7b0727 commit a59c3b8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -139,6 +143,19 @@ public void testBasics_WithTSC_WithSmallHeapSize() throws Exception {

testBasicsDummyQuery(cache, s, shard);

// Explicitly check disk cache had items and hits
TieredSpilloverCache<TieredQueryCache.CompositeKey, TieredQueryCache.CacheAndCount> 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);
}
Expand All @@ -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)));
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,7 @@ public void testDropStatsForDimensions() throws Exception {
assertEquals(new ImmutableCacheStats(0, 0, 0, 0, 0), tieredSpilloverCache.stats().getTotalStats());
}

private List<String> getMockDimensions() {
static private List<String> getMockDimensions() {
List<String> dims = new ArrayList<>();
for (String dimensionName : dimensionNames) {
dims.add("0");
Expand Down Expand Up @@ -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<String> levelsList = new ArrayList<>(dimensionNames);
levelsList.add(TIER_DIMENSION_NAME);
String[] levels = levelsList.toArray(new String[0]);
Expand All @@ -2433,6 +2433,26 @@ private ImmutableCacheStats getStatsSnapshotForTier(TieredSpilloverCache<?, ?> t
return snapshot;
}

public static ImmutableCacheStats getStatsSnapshotForTier(
TieredSpilloverCache<?, ?> tsc,
String tierValue,
List<String> dimensionNames,
List<String> dimensionValues
) throws IOException {
List<String> levelsList = new ArrayList<>(dimensionNames);
levelsList.add(TIER_DIMENSION_NAME);
String[] levels = levelsList.toArray(new String[0]);
ImmutableCacheStatsHolder cacheStats = tsc.stats(levels);
List<String> 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)) {
Expand Down

0 comments on commit a59c3b8

Please sign in to comment.