From 7ef987570d73eb71b1ad7b8bcecad5a8f5955e2f Mon Sep 17 00:00:00 2001 From: David Zane Date: Mon, 27 Jan 2025 12:22:07 -0800 Subject: [PATCH] Fix null indexFieldMap bug & add UTs Signed-off-by: David Zane --- .../categorizer/IndicesFieldTypeCache.java | 8 ++- .../IndicesFieldTypeCacheTests.java | 64 +++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/opensearch/plugin/insights/core/service/categorizer/IndicesFieldTypeCacheTests.java diff --git a/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/IndicesFieldTypeCache.java b/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/IndicesFieldTypeCache.java index cb442d35..4f5d1d10 100644 --- a/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/IndicesFieldTypeCache.java +++ b/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/IndicesFieldTypeCache.java @@ -68,9 +68,11 @@ IndexFieldMap getOrInitialize(Index index) { public void invalidate(Index index) { IndexFieldMap indexFieldMap = cache.get(index); - evictionCount.inc(indexFieldMap.fieldTypeMap.size()); - entryCount.dec(indexFieldMap.fieldTypeMap.size()); - weight.dec(indexFieldMap.weight()); + if (indexFieldMap != null) { + evictionCount.inc(indexFieldMap.fieldTypeMap.size()); + entryCount.dec(indexFieldMap.fieldTypeMap.size()); + weight.dec(indexFieldMap.weight()); + } cache.invalidate(index); } diff --git a/src/test/java/org/opensearch/plugin/insights/core/service/categorizer/IndicesFieldTypeCacheTests.java b/src/test/java/org/opensearch/plugin/insights/core/service/categorizer/IndicesFieldTypeCacheTests.java new file mode 100644 index 00000000..830f66e4 --- /dev/null +++ b/src/test/java/org/opensearch/plugin/insights/core/service/categorizer/IndicesFieldTypeCacheTests.java @@ -0,0 +1,64 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.plugin.insights.core.service.categorizer; + +import java.util.UUID; +import java.util.stream.StreamSupport; +import org.opensearch.common.settings.Settings; +import org.opensearch.core.index.Index; +import org.opensearch.test.OpenSearchTestCase; + +public final class IndicesFieldTypeCacheTests extends OpenSearchTestCase { + final Index index1 = new Index("index1", UUID.randomUUID().toString()); + final Index index2 = new Index("index2", UUID.randomUUID().toString()); + + public IndicesFieldTypeCacheTests() {} + + public void testCache() { + final IndicesFieldTypeCache indicesFieldTypeCache = new IndicesFieldTypeCache(Settings.EMPTY); + + // Assert cache is empty + assertEquals(0, StreamSupport.stream(indicesFieldTypeCache.keySet().spliterator(), false).count()); + assertEquals(0, (long) indicesFieldTypeCache.getEntryCount()); + assertEquals(0, (long) indicesFieldTypeCache.getEvictionCount()); + assertEquals(0, (long) indicesFieldTypeCache.getWeight()); + + // Test invalidate on empty cache + indicesFieldTypeCache.invalidate(index1); + indicesFieldTypeCache.invalidate(index2); + + // Insert some items in the cache + if (indicesFieldTypeCache.getOrInitialize(index1).putIfAbsent("field1", "keyword")) { + indicesFieldTypeCache.incrementCountAndWeight("field1", "keyword"); + } + if (indicesFieldTypeCache.getOrInitialize(index1).putIfAbsent("field2", "boolean")) { + indicesFieldTypeCache.incrementCountAndWeight("field2", "boolean"); + } + if (indicesFieldTypeCache.getOrInitialize(index2).putIfAbsent("field3", "float_range")) { + indicesFieldTypeCache.incrementCountAndWeight("field3", "float_range"); + } + if (indicesFieldTypeCache.getOrInitialize(index2).putIfAbsent("field4", "date")) { + indicesFieldTypeCache.incrementCountAndWeight("field4", "date"); + } + + assertEquals(2, StreamSupport.stream(indicesFieldTypeCache.keySet().spliterator(), false).count()); + assertEquals(4, (long) indicesFieldTypeCache.getEntryCount()); + assertEquals(0, (long) indicesFieldTypeCache.getEvictionCount()); + assertTrue(0 < indicesFieldTypeCache.getWeight()); + + // Invalidate index1 + indicesFieldTypeCache.invalidate(index1); + + assertEquals(1, StreamSupport.stream(indicesFieldTypeCache.keySet().spliterator(), false).count()); + assertEquals(2, (long) indicesFieldTypeCache.getEntryCount()); + assertEquals(2, (long) indicesFieldTypeCache.getEvictionCount()); + assertTrue(0 < indicesFieldTypeCache.getWeight()); + } + +}