Skip to content

Commit 2609cf7

Browse files
author
Peter Alfonsi
committed
Fixes tests using incorrect null levels
Signed-off-by: Peter Alfonsi <petealft@amazon.com>
1 parent 79331c4 commit 2609cf7

File tree

6 files changed

+23
-17
lines changed

6 files changed

+23
-17
lines changed

plugins/cache-ehcache/src/test/java/org/opensearch/cache/store/disk/EhCacheDiskCacheTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,15 +829,16 @@ public void testInvalidateWithDropDimensions() throws Exception {
829829

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

832-
ImmutableCacheStats snapshot = ehCacheDiskCachingTier.stats().getStatsForDimensionValues(keyToDrop.dimensions);
832+
String[] levels = dimensionNames.toArray(new String[0]);
833+
ImmutableCacheStats snapshot = ehCacheDiskCachingTier.stats(levels).getStatsForDimensionValues(keyToDrop.dimensions);
833834
assertNotNull(snapshot);
834835

835836
keyToDrop.setDropStatsForDimensions(true);
836837
ehCacheDiskCachingTier.invalidate(keyToDrop);
837838

838839
// Now assert the stats are gone for any key that has this combination of dimensions, but still there otherwise
839840
for (ICacheKey<String> keyAdded : keysAdded) {
840-
snapshot = ehCacheDiskCachingTier.stats().getStatsForDimensionValues(keyAdded.dimensions);
841+
snapshot = ehCacheDiskCachingTier.stats(levels).getStatsForDimensionValues(keyAdded.dimensions);
841842
if (keyAdded.dimensions.equals(keyToDrop.dimensions)) {
842843
assertNull(snapshot);
843844
} else {

server/src/main/java/org/opensearch/common/cache/ICache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public interface ICache<K, V> extends Closeable {
4545

4646
void refresh();
4747

48-
// Return all stats without aggregation.
48+
// Return total stats only
4949
default ImmutableCacheStatsHolder stats() {
5050
return stats(null);
5151
}
5252

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

5656
/**

server/src/main/java/org/opensearch/indices/IndicesRequestCache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,8 @@ long count() {
826826
/**
827827
* Returns the current cache stats. Pkg-private for testing.
828828
*/
829-
ImmutableCacheStatsHolder stats() {
830-
return cache.stats();
829+
ImmutableCacheStatsHolder stats(String[] levels) {
830+
return cache.stats(levels);
831831
}
832832

833833
int numRegisteredCloseListeners() { // for testing

server/src/test/java/org/opensearch/common/cache/stats/ImmutableCacheStatsHolderTests.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ public class ImmutableCacheStatsHolderTests extends OpenSearchTestCase {
2929

3030
public void testSerialization() throws Exception {
3131
List<String> dimensionNames = List.of("dim1", "dim2", "dim3");
32+
String[] levels = dimensionNames.toArray(new String[0]);
3233
DefaultCacheStatsHolder statsHolder = new DefaultCacheStatsHolder(dimensionNames, storeName);
3334
Map<String, List<String>> usedDimensionValues = DefaultCacheStatsHolderTests.getUsedDimensionValues(statsHolder, 10);
3435
DefaultCacheStatsHolderTests.populateStats(statsHolder, usedDimensionValues, 100, 10);
35-
ImmutableCacheStatsHolder stats = statsHolder.getImmutableCacheStatsHolder(null);
36+
ImmutableCacheStatsHolder stats = statsHolder.getImmutableCacheStatsHolder(levels);
3637
assertNotEquals(0, stats.getStatsRoot().children.size());
3738

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

5859
public void testEquals() throws Exception {
5960
List<String> dimensionNames = List.of("dim1", "dim2", "dim3");
61+
String[] levels = dimensionNames.toArray(new String[0]);
6062
DefaultCacheStatsHolder statsHolder = new DefaultCacheStatsHolder(dimensionNames, storeName);
6163
DefaultCacheStatsHolder differentStoreNameStatsHolder = new DefaultCacheStatsHolder(dimensionNames, "nonMatchingStoreName");
6264
DefaultCacheStatsHolder nonMatchingStatsHolder = new DefaultCacheStatsHolder(dimensionNames, storeName);
6365
Map<String, List<String>> usedDimensionValues = DefaultCacheStatsHolderTests.getUsedDimensionValues(statsHolder, 10);
6466
DefaultCacheStatsHolderTests.populateStats(List.of(statsHolder, differentStoreNameStatsHolder), usedDimensionValues, 100, 10);
6567
DefaultCacheStatsHolderTests.populateStats(nonMatchingStatsHolder, usedDimensionValues, 100, 10);
66-
ImmutableCacheStatsHolder stats = statsHolder.getImmutableCacheStatsHolder(null);
68+
ImmutableCacheStatsHolder stats = statsHolder.getImmutableCacheStatsHolder(levels);
6769

68-
ImmutableCacheStatsHolder secondStats = statsHolder.getImmutableCacheStatsHolder(null);
70+
ImmutableCacheStatsHolder secondStats = statsHolder.getImmutableCacheStatsHolder(levels);
6971
assertEquals(stats, secondStats);
70-
ImmutableCacheStatsHolder nonMatchingStats = nonMatchingStatsHolder.getImmutableCacheStatsHolder(null);
72+
ImmutableCacheStatsHolder nonMatchingStats = nonMatchingStatsHolder.getImmutableCacheStatsHolder(levels);
7173
assertNotEquals(stats, nonMatchingStats);
72-
ImmutableCacheStatsHolder differentStoreNameStats = differentStoreNameStatsHolder.getImmutableCacheStatsHolder(null);
74+
ImmutableCacheStatsHolder differentStoreNameStats = differentStoreNameStatsHolder.getImmutableCacheStatsHolder(levels);
7375
assertNotEquals(stats, differentStoreNameStats);
7476
}
7577

server/src/test/java/org/opensearch/common/cache/store/OpenSearchOnHeapCacheTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,16 @@ public void testInvalidateWithDropDimensions() throws Exception {
145145
}
146146

147147
ICacheKey<String> keyToDrop = keysAdded.get(0);
148-
149-
ImmutableCacheStats snapshot = cache.stats().getStatsForDimensionValues(keyToDrop.dimensions);
148+
String[] levels = dimensionNames.toArray(new String[0]);
149+
ImmutableCacheStats snapshot = cache.stats(levels).getStatsForDimensionValues(keyToDrop.dimensions);
150150
assertNotNull(snapshot);
151151

152152
keyToDrop.setDropStatsForDimensions(true);
153153
cache.invalidate(keyToDrop);
154154

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

server/src/test/java/org/opensearch/indices/IndicesRequestCacheTests.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@
8989
import java.util.concurrent.ConcurrentMap;
9090
import java.util.concurrent.atomic.AtomicInteger;
9191

92+
import static org.opensearch.indices.IndicesRequestCache.INDEX_DIMENSION_NAME;
9293
import static org.opensearch.indices.IndicesRequestCache.INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING;
94+
import static org.opensearch.indices.IndicesRequestCache.SHARD_ID_DIMENSION_NAME;
9395
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
9496
import static org.mockito.Mockito.mock;
9597
import static org.mockito.Mockito.when;
@@ -799,6 +801,7 @@ private String getReaderCacheKeyId(DirectoryReader reader) {
799801

800802
public void testClosingIndexWipesStats() throws Exception {
801803
IndicesService indicesService = getInstanceFromNode(IndicesService.class);
804+
String[] levels = { INDEX_DIMENSION_NAME, SHARD_ID_DIMENSION_NAME };
802805
// Create two indices each with multiple shards
803806
int numShards = 3;
804807
Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numShards).build();
@@ -873,8 +876,8 @@ public void testClosingIndexWipesStats() throws Exception {
873876
ShardId shardId = indexService.getShard(i).shardId();
874877
List<String> dimensionValues = List.of(shardId.getIndexName(), shardId.toString());
875878
initialDimensionValues.add(dimensionValues);
876-
ImmutableCacheStatsHolder holder = cache.stats();
877-
ImmutableCacheStats snapshot = cache.stats().getStatsForDimensionValues(dimensionValues);
879+
ImmutableCacheStatsHolder holder = cache.stats(levels);
880+
ImmutableCacheStats snapshot = cache.stats(levels).getStatsForDimensionValues(dimensionValues);
878881
assertNotNull(snapshot);
879882
// check the values are not empty by confirming entries != 0, this should always be true since the missed value is loaded
880883
// into the cache
@@ -895,7 +898,7 @@ public void testClosingIndexWipesStats() throws Exception {
895898

896899
// Now stats for the closed index should be gone
897900
for (List<String> dimensionValues : initialDimensionValues) {
898-
ImmutableCacheStats snapshot = cache.stats().getStatsForDimensionValues(dimensionValues);
901+
ImmutableCacheStats snapshot = cache.stats(levels).getStatsForDimensionValues(dimensionValues);
899902
if (dimensionValues.get(0).equals(indexToCloseName)) {
900903
assertNull(snapshot);
901904
} else {

0 commit comments

Comments
 (0)