Skip to content

Commit a64c3c9

Browse files
Fix null indexFieldMap bug & add UTs (#214) (#216)
(cherry picked from commit 24a8bbc) Signed-off-by: David Zane <davizane@amazon.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 521c4be commit a64c3c9

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

src/main/java/org/opensearch/plugin/insights/core/service/categorizer/IndicesFieldTypeCache.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ IndexFieldMap getOrInitialize(Index index) {
6868

6969
public void invalidate(Index index) {
7070
IndexFieldMap indexFieldMap = cache.get(index);
71-
evictionCount.inc(indexFieldMap.fieldTypeMap.size());
72-
entryCount.dec(indexFieldMap.fieldTypeMap.size());
73-
weight.dec(indexFieldMap.weight());
71+
if (indexFieldMap != null) {
72+
evictionCount.inc(indexFieldMap.fieldTypeMap.size());
73+
entryCount.dec(indexFieldMap.fieldTypeMap.size());
74+
weight.dec(indexFieldMap.weight());
75+
}
7476
cache.invalidate(index);
7577
}
7678

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.plugin.insights.core.service.categorizer;
10+
11+
import java.util.UUID;
12+
import java.util.stream.StreamSupport;
13+
import org.opensearch.common.settings.Settings;
14+
import org.opensearch.core.index.Index;
15+
import org.opensearch.test.OpenSearchTestCase;
16+
17+
public final class IndicesFieldTypeCacheTests extends OpenSearchTestCase {
18+
final Index index1 = new Index("index1", UUID.randomUUID().toString());
19+
final Index index2 = new Index("index2", UUID.randomUUID().toString());
20+
21+
public IndicesFieldTypeCacheTests() {}
22+
23+
public void testCache() {
24+
final IndicesFieldTypeCache indicesFieldTypeCache = new IndicesFieldTypeCache(Settings.EMPTY);
25+
26+
// Assert cache is empty
27+
assertEquals(0, StreamSupport.stream(indicesFieldTypeCache.keySet().spliterator(), false).count());
28+
assertEquals(0, (long) indicesFieldTypeCache.getEntryCount());
29+
assertEquals(0, (long) indicesFieldTypeCache.getEvictionCount());
30+
assertEquals(0, (long) indicesFieldTypeCache.getWeight());
31+
32+
// Test invalidate on empty cache
33+
indicesFieldTypeCache.invalidate(index1);
34+
indicesFieldTypeCache.invalidate(index2);
35+
36+
// Insert some items in the cache
37+
if (indicesFieldTypeCache.getOrInitialize(index1).putIfAbsent("field1", "keyword")) {
38+
indicesFieldTypeCache.incrementCountAndWeight("field1", "keyword");
39+
}
40+
if (indicesFieldTypeCache.getOrInitialize(index1).putIfAbsent("field2", "boolean")) {
41+
indicesFieldTypeCache.incrementCountAndWeight("field2", "boolean");
42+
}
43+
if (indicesFieldTypeCache.getOrInitialize(index2).putIfAbsent("field3", "float_range")) {
44+
indicesFieldTypeCache.incrementCountAndWeight("field3", "float_range");
45+
}
46+
if (indicesFieldTypeCache.getOrInitialize(index2).putIfAbsent("field4", "date")) {
47+
indicesFieldTypeCache.incrementCountAndWeight("field4", "date");
48+
}
49+
50+
assertEquals(2, StreamSupport.stream(indicesFieldTypeCache.keySet().spliterator(), false).count());
51+
assertEquals(4, (long) indicesFieldTypeCache.getEntryCount());
52+
assertEquals(0, (long) indicesFieldTypeCache.getEvictionCount());
53+
assertTrue(0 < indicesFieldTypeCache.getWeight());
54+
55+
// Invalidate index1
56+
indicesFieldTypeCache.invalidate(index1);
57+
58+
assertEquals(1, StreamSupport.stream(indicesFieldTypeCache.keySet().spliterator(), false).count());
59+
assertEquals(2, (long) indicesFieldTypeCache.getEntryCount());
60+
assertEquals(2, (long) indicesFieldTypeCache.getEvictionCount());
61+
assertTrue(0 < indicesFieldTypeCache.getWeight());
62+
}
63+
64+
}

0 commit comments

Comments
 (0)