From 179e8d8801a84d317372c3d952e4787f704a57bd Mon Sep 17 00:00:00 2001 From: Craig Perkins Date: Wed, 3 Jul 2024 17:06:09 -0400 Subject: [PATCH] Remove singleton limitation Signed-off-by: Craig Perkins --- .../indices/SystemIndexRegistry.java | 26 ++++++------------- .../org/opensearch/indices/SystemIndices.java | 3 +-- .../indices/SystemIndicesTests.java | 6 ----- 3 files changed, 9 insertions(+), 26 deletions(-) diff --git a/server/src/main/java/org/opensearch/indices/SystemIndexRegistry.java b/server/src/main/java/org/opensearch/indices/SystemIndexRegistry.java index a784adb6e82b5..27973839c6167 100644 --- a/server/src/main/java/org/opensearch/indices/SystemIndexRegistry.java +++ b/server/src/main/java/org/opensearch/indices/SystemIndexRegistry.java @@ -16,6 +16,7 @@ import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; @@ -27,17 +28,17 @@ import static java.util.Collections.unmodifiableMap; import static org.opensearch.tasks.TaskResultsService.TASK_INDEX; -class SystemIndexRegistry { - private static SystemIndexRegistry INSTANCE = null; +public class SystemIndexRegistry { private static final SystemIndexDescriptor TASK_INDEX_DESCRIPTOR = new SystemIndexDescriptor(TASK_INDEX + "*", "Task Result Index"); - static final Map> SERVER_SYSTEM_INDEX_DESCRIPTORS = singletonMap( + private static final Map> SERVER_SYSTEM_INDEX_DESCRIPTORS = singletonMap( TaskResultsService.class.getName(), singletonList(TASK_INDEX_DESCRIPTOR) ); - private static String[] SYSTEM_INDEX_PATTERNS = new String[0]; - static Collection SYSTEM_INDEX_DESCRIPTORS; - private SystemIndexRegistry(Map> pluginAndModulesDescriptors) { + private volatile static String[] SYSTEM_INDEX_PATTERNS = new String[0]; + volatile static Collection SYSTEM_INDEX_DESCRIPTORS = Collections.emptyList(); + + static void register(Map> pluginAndModulesDescriptors) { final Map> descriptorsMap = buildSystemIndexDescriptorMap(pluginAndModulesDescriptors); checkForOverlappingPatterns(descriptorsMap); List descriptors = pluginAndModulesDescriptors.values() @@ -45,17 +46,11 @@ private SystemIndexRegistry(Map> plugi .flatMap(Collection::stream) .collect(Collectors.toList()); descriptors.add(TASK_INDEX_DESCRIPTOR); + SYSTEM_INDEX_DESCRIPTORS = descriptors.stream().collect(Collectors.toUnmodifiableList()); SYSTEM_INDEX_PATTERNS = descriptors.stream().map(SystemIndexDescriptor::getIndexPattern).toArray(String[]::new); } - public static synchronized SystemIndexRegistry initialize(Map> pluginAndModulesDescriptors) { - if (INSTANCE == null) { - INSTANCE = new SystemIndexRegistry(pluginAndModulesDescriptors); - } - return INSTANCE; - } - public static List matchesSystemIndexPattern(String... indexExpressions) { return Arrays.stream(indexExpressions) .filter(pattern -> Regex.simpleMatch(SYSTEM_INDEX_PATTERNS, pattern)) @@ -123,9 +118,4 @@ private static Map> buildSystemIndexDe }); return unmodifiableMap(map); } - - // visible for testing - static void clear() { - INSTANCE = null; - } } diff --git a/server/src/main/java/org/opensearch/indices/SystemIndices.java b/server/src/main/java/org/opensearch/indices/SystemIndices.java index 28e3f50b444a3..bbf58fe91512f 100644 --- a/server/src/main/java/org/opensearch/indices/SystemIndices.java +++ b/server/src/main/java/org/opensearch/indices/SystemIndices.java @@ -62,8 +62,7 @@ public class SystemIndices { private final CharacterRunAutomaton runAutomaton; public SystemIndices(Map> pluginAndModulesDescriptors) { - SystemIndexRegistry.initialize(pluginAndModulesDescriptors); - SystemIndexRegistry.initialize(pluginAndModulesDescriptors); + SystemIndexRegistry.register(pluginAndModulesDescriptors); this.runAutomaton = buildCharacterRunAutomaton(SystemIndexRegistry.SYSTEM_INDEX_DESCRIPTORS); } diff --git a/server/src/test/java/org/opensearch/indices/SystemIndicesTests.java b/server/src/test/java/org/opensearch/indices/SystemIndicesTests.java index 12836ae7892ea..8ac457c32d53a 100644 --- a/server/src/test/java/org/opensearch/indices/SystemIndicesTests.java +++ b/server/src/test/java/org/opensearch/indices/SystemIndicesTests.java @@ -37,7 +37,6 @@ import org.opensearch.plugins.SystemIndexPlugin; import org.opensearch.tasks.TaskResultsService; import org.opensearch.test.OpenSearchTestCase; -import org.junit.Before; import java.util.Arrays; import java.util.Collection; @@ -56,11 +55,6 @@ public class SystemIndicesTests extends OpenSearchTestCase { - @Before - public void setup() { - SystemIndexRegistry.clear(); - } - public void testBasicOverlappingPatterns() { SystemIndexDescriptor broadPattern = new SystemIndexDescriptor(".a*c*", "test"); SystemIndexDescriptor notOverlapping = new SystemIndexDescriptor(".bbbddd*", "test");