Skip to content

Commit

Permalink
Remove singleton limitation
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Jul 3, 2024
1 parent f7365b5 commit 179e8d8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,35 +28,29 @@
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<String, Collection<SystemIndexDescriptor>> SERVER_SYSTEM_INDEX_DESCRIPTORS = singletonMap(
private static final Map<String, Collection<SystemIndexDescriptor>> SERVER_SYSTEM_INDEX_DESCRIPTORS = singletonMap(
TaskResultsService.class.getName(),
singletonList(TASK_INDEX_DESCRIPTOR)
);
private static String[] SYSTEM_INDEX_PATTERNS = new String[0];
static Collection<SystemIndexDescriptor> SYSTEM_INDEX_DESCRIPTORS;

private SystemIndexRegistry(Map<String, Collection<SystemIndexDescriptor>> pluginAndModulesDescriptors) {
private volatile static String[] SYSTEM_INDEX_PATTERNS = new String[0];
volatile static Collection<SystemIndexDescriptor> SYSTEM_INDEX_DESCRIPTORS = Collections.emptyList();

static void register(Map<String, Collection<SystemIndexDescriptor>> pluginAndModulesDescriptors) {
final Map<String, Collection<SystemIndexDescriptor>> descriptorsMap = buildSystemIndexDescriptorMap(pluginAndModulesDescriptors);
checkForOverlappingPatterns(descriptorsMap);
List<SystemIndexDescriptor> descriptors = pluginAndModulesDescriptors.values()
.stream()
.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<String, Collection<SystemIndexDescriptor>> pluginAndModulesDescriptors) {
if (INSTANCE == null) {
INSTANCE = new SystemIndexRegistry(pluginAndModulesDescriptors);
}
return INSTANCE;
}

public static List<String> matchesSystemIndexPattern(String... indexExpressions) {
return Arrays.stream(indexExpressions)
.filter(pattern -> Regex.simpleMatch(SYSTEM_INDEX_PATTERNS, pattern))
Expand Down Expand Up @@ -123,9 +118,4 @@ private static Map<String, Collection<SystemIndexDescriptor>> buildSystemIndexDe
});
return unmodifiableMap(map);
}

// visible for testing
static void clear() {
INSTANCE = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public class SystemIndices {
private final CharacterRunAutomaton runAutomaton;

public SystemIndices(Map<String, Collection<SystemIndexDescriptor>> pluginAndModulesDescriptors) {
SystemIndexRegistry.initialize(pluginAndModulesDescriptors);
SystemIndexRegistry.initialize(pluginAndModulesDescriptors);
SystemIndexRegistry.register(pluginAndModulesDescriptors);
this.runAutomaton = buildCharacterRunAutomaton(SystemIndexRegistry.SYSTEM_INDEX_DESCRIPTORS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down

0 comments on commit 179e8d8

Please sign in to comment.