Skip to content

Commit

Permalink
Fixes tests using incorrect null levels
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <[email protected]>
  • Loading branch information
Peter Alfonsi committed Apr 30, 2024
1 parent 79331c4 commit 917cf0c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -829,15 +829,16 @@ public void testInvalidateWithDropDimensions() throws Exception {

ICacheKey<String> keyToDrop = keysAdded.get(0);

ImmutableCacheStats snapshot = ehCacheDiskCachingTier.stats().getStatsForDimensionValues(keyToDrop.dimensions);
String[] levels = dimensionNames.toArray(new String[0]);
ImmutableCacheStats snapshot = ehCacheDiskCachingTier.stats(levels).getStatsForDimensionValues(keyToDrop.dimensions);
assertNotNull(snapshot);

keyToDrop.setDropStatsForDimensions(true);
ehCacheDiskCachingTier.invalidate(keyToDrop);

// Now assert the stats are gone for any key that has this combination of dimensions, but still there otherwise
for (ICacheKey<String> keyAdded : keysAdded) {
snapshot = ehCacheDiskCachingTier.stats().getStatsForDimensionValues(keyAdded.dimensions);
snapshot = ehCacheDiskCachingTier.stats(levels).getStatsForDimensionValues(keyAdded.dimensions);
if (keyAdded.dimensions.equals(keyToDrop.dimensions)) {
assertNull(snapshot);
} else {
Expand Down
4 changes: 2 additions & 2 deletions server/src/main/java/org/opensearch/common/cache/ICache.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public interface ICache<K, V> extends Closeable {

void refresh();

// Return all stats without aggregation.
// Return total stats only
default ImmutableCacheStatsHolder stats() {
return stats(null);
}

// Return stats aggregated by the provided levels. If levels is null, do not aggregate and return all stats.
// Return stats aggregated by the provided levels. If levels is null or an empty array, return total stats only.
ImmutableCacheStatsHolder stats(String[] levels);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ public class ImmutableCacheStatsHolderTests extends OpenSearchTestCase {

public void testSerialization() throws Exception {
List<String> dimensionNames = List.of("dim1", "dim2", "dim3");
String[] levels = dimensionNames.toArray(new String[0]);
DefaultCacheStatsHolder statsHolder = new DefaultCacheStatsHolder(dimensionNames, storeName);
Map<String, List<String>> usedDimensionValues = DefaultCacheStatsHolderTests.getUsedDimensionValues(statsHolder, 10);
DefaultCacheStatsHolderTests.populateStats(statsHolder, usedDimensionValues, 100, 10);
ImmutableCacheStatsHolder stats = statsHolder.getImmutableCacheStatsHolder(null);
ImmutableCacheStatsHolder stats = statsHolder.getImmutableCacheStatsHolder(levels);
assertNotEquals(0, stats.getStatsRoot().children.size());

BytesStreamOutput os = new BytesStreamOutput();
Expand All @@ -57,19 +58,20 @@ public void testSerialization() throws Exception {

public void testEquals() throws Exception {
List<String> dimensionNames = List.of("dim1", "dim2", "dim3");
String[] levels = dimensionNames.toArray(new String[0]);
DefaultCacheStatsHolder statsHolder = new DefaultCacheStatsHolder(dimensionNames, storeName);
DefaultCacheStatsHolder differentStoreNameStatsHolder = new DefaultCacheStatsHolder(dimensionNames, "nonMatchingStoreName");
DefaultCacheStatsHolder nonMatchingStatsHolder = new DefaultCacheStatsHolder(dimensionNames, storeName);
Map<String, List<String>> usedDimensionValues = DefaultCacheStatsHolderTests.getUsedDimensionValues(statsHolder, 10);
DefaultCacheStatsHolderTests.populateStats(List.of(statsHolder, differentStoreNameStatsHolder), usedDimensionValues, 100, 10);
DefaultCacheStatsHolderTests.populateStats(nonMatchingStatsHolder, usedDimensionValues, 100, 10);
ImmutableCacheStatsHolder stats = statsHolder.getImmutableCacheStatsHolder(null);
ImmutableCacheStatsHolder stats = statsHolder.getImmutableCacheStatsHolder(levels);

ImmutableCacheStatsHolder secondStats = statsHolder.getImmutableCacheStatsHolder(null);
ImmutableCacheStatsHolder secondStats = statsHolder.getImmutableCacheStatsHolder(levels);
assertEquals(stats, secondStats);
ImmutableCacheStatsHolder nonMatchingStats = nonMatchingStatsHolder.getImmutableCacheStatsHolder(null);
ImmutableCacheStatsHolder nonMatchingStats = nonMatchingStatsHolder.getImmutableCacheStatsHolder(levels);
assertNotEquals(stats, nonMatchingStats);
ImmutableCacheStatsHolder differentStoreNameStats = differentStoreNameStatsHolder.getImmutableCacheStatsHolder(null);
ImmutableCacheStatsHolder differentStoreNameStats = differentStoreNameStatsHolder.getImmutableCacheStatsHolder(levels);
assertNotEquals(stats, differentStoreNameStats);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,16 @@ public void testInvalidateWithDropDimensions() throws Exception {
}

ICacheKey<String> keyToDrop = keysAdded.get(0);

ImmutableCacheStats snapshot = cache.stats().getStatsForDimensionValues(keyToDrop.dimensions);
String[] levels = dimensionNames.toArray(new String[0]);
ImmutableCacheStats snapshot = cache.stats(levels).getStatsForDimensionValues(keyToDrop.dimensions);
assertNotNull(snapshot);

keyToDrop.setDropStatsForDimensions(true);
cache.invalidate(keyToDrop);

// Now assert the stats are gone for any key that has this combination of dimensions, but still there otherwise
for (ICacheKey<String> keyAdded : keysAdded) {
snapshot = cache.stats().getStatsForDimensionValues(keyAdded.dimensions);
snapshot = cache.stats(levels).getStatsForDimensionValues(keyAdded.dimensions);
if (keyAdded.dimensions.equals(keyToDrop.dimensions)) {
assertNull(snapshot);
} else {
Expand Down

0 comments on commit 917cf0c

Please sign in to comment.