Skip to content

Commit

Permalink
Add method in SystemIndexRegistry to determine if passed system indic…
Browse files Browse the repository at this point in the history
…es match system indices registered by plugin

Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Jul 15, 2024
1 parent c2e0a64 commit 55fe24c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

public class TestExecutionContextPlugin extends Plugin implements ActionPlugin {

private Client client;
private PluginAwareNodeClient client;

@Override
public Collection<Object> createComponents(
Expand All @@ -63,6 +63,6 @@ public Collection<Object> createComponents(
public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings,
IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<DiscoveryNodes> nodesInCluster) {
return List.of(new TestGetExecutionContextRestAction((PluginAwareNodeClient) client));
return List.of(new TestGetExecutionContextRestAction(client));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
import org.opensearch.common.regex.Regex;
import org.opensearch.tasks.TaskResultsService;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.Collections.singletonList;
Expand All @@ -46,6 +46,7 @@ public class SystemIndexRegistry {

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

static void register(Map<String, Collection<SystemIndexDescriptor>> pluginAndModulesDescriptors) {
final Map<String, Collection<SystemIndexDescriptor>> descriptorsMap = buildSystemIndexDescriptorMap(pluginAndModulesDescriptors);
Expand All @@ -56,14 +57,26 @@ static void register(Map<String, Collection<SystemIndexDescriptor>> pluginAndMod
.collect(Collectors.toList());
descriptors.add(TASK_INDEX_DESCRIPTOR);

SYSTEM_INDEX_DESCRIPTORS_MAP = descriptorsMap;
SYSTEM_INDEX_DESCRIPTORS = descriptors.stream().collect(Collectors.toUnmodifiableList());
SYSTEM_INDEX_PATTERNS = descriptors.stream().map(SystemIndexDescriptor::getIndexPattern).toArray(String[]::new);
}

public static List<String> matchesSystemIndexPattern(String... indexExpressions) {
return Arrays.stream(indexExpressions)
.filter(pattern -> Regex.simpleMatch(SYSTEM_INDEX_PATTERNS, pattern))
.collect(Collectors.toList());
public static Set<String> matchesSystemIndexPattern(Set<String> indexExpressions) {
return indexExpressions.stream().filter(pattern -> Regex.simpleMatch(SYSTEM_INDEX_PATTERNS, pattern)).collect(Collectors.toSet());
}

public static Set<String> matchesPluginSystemIndexPattern(String pluginClassName, Set<String> indexExpressions) {
if (!SYSTEM_INDEX_DESCRIPTORS_MAP.containsKey(pluginClassName)) {
return Collections.emptySet();
}
String[] pluginSystemIndexPatterns = SYSTEM_INDEX_DESCRIPTORS_MAP.get(pluginClassName)
.stream()
.map(SystemIndexDescriptor::getIndexPattern)
.toArray(String[]::new);
return indexExpressions.stream()
.filter(pattern -> Regex.simpleMatch(pluginSystemIndexPatterns, pattern))
.collect(Collectors.toSet());
}

/**
Expand Down
33 changes: 21 additions & 12 deletions server/src/test/java/org/opensearch/indices/SystemIndicesTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
Expand Down Expand Up @@ -155,29 +156,38 @@ public void testSystemIndexMatching() {
);

assertThat(
SystemIndexRegistry.matchesSystemIndexPattern(".system-index1", ".system-index2"),
SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".system-index1", ".system-index2")),
equalTo(List.of(SystemIndexPlugin1.SYSTEM_INDEX_1, SystemIndexPlugin2.SYSTEM_INDEX_2))
);
assertThat(SystemIndexRegistry.matchesSystemIndexPattern(".system-index1"), equalTo(List.of(SystemIndexPlugin1.SYSTEM_INDEX_1)));
assertThat(SystemIndexRegistry.matchesSystemIndexPattern(".system-index2"), equalTo(List.of(SystemIndexPlugin2.SYSTEM_INDEX_2)));
assertThat(SystemIndexRegistry.matchesSystemIndexPattern(".system-index-pattern1"), equalTo(List.of(".system-index-pattern1")));
assertThat(
SystemIndexRegistry.matchesSystemIndexPattern(".system-index-pattern-sub*"),
SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".system-index1")),
equalTo(List.of(SystemIndexPlugin1.SYSTEM_INDEX_1))
);
assertThat(
SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".system-index2")),
equalTo(List.of(SystemIndexPlugin2.SYSTEM_INDEX_2))
);
assertThat(
SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".system-index-pattern1")),
equalTo(List.of(".system-index-pattern1"))
);
assertThat(
SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".system-index-pattern-sub*")),
equalTo(List.of(".system-index-pattern-sub*"))
);
assertThat(
SystemIndexRegistry.matchesSystemIndexPattern(".system-index-pattern1", ".system-index-pattern2"),
SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".system-index-pattern1", ".system-index-pattern2")),
equalTo(List.of(".system-index-pattern1", ".system-index-pattern2"))
);
assertThat(
SystemIndexRegistry.matchesSystemIndexPattern(".system-index1", ".system-index-pattern1"),
SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".system-index1", ".system-index-pattern1")),
equalTo(List.of(".system-index1", ".system-index-pattern1"))
);
assertThat(
SystemIndexRegistry.matchesSystemIndexPattern(".system-index1", ".system-index-pattern1", ".not-system"),
SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".system-index1", ".system-index-pattern1", ".not-system")),
equalTo(List.of(".system-index1", ".system-index-pattern1"))
);
assertThat(SystemIndexRegistry.matchesSystemIndexPattern(".not-system"), equalTo(Collections.emptyList()));
assertThat(SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".not-system")), equalTo(Collections.emptyList()));
}

public void testRegisteredSystemIndexExpansion() {
Expand All @@ -191,9 +201,8 @@ public void testRegisteredSystemIndexExpansion() {
plugin2.getSystemIndexDescriptors(Settings.EMPTY)
)
);
List<String> systemIndices = SystemIndexRegistry.matchesSystemIndexPattern(
SystemIndexPlugin1.SYSTEM_INDEX_1,
SystemIndexPlugin2.SYSTEM_INDEX_2
Set<String> systemIndices = SystemIndexRegistry.matchesSystemIndexPattern(
Set.of(SystemIndexPlugin1.SYSTEM_INDEX_1, SystemIndexPlugin2.SYSTEM_INDEX_2)
);
assertEquals(2, systemIndices.size());
assertTrue(systemIndices.containsAll(List.of(SystemIndexPlugin1.SYSTEM_INDEX_1, SystemIndexPlugin2.SYSTEM_INDEX_2)));
Expand Down

0 comments on commit 55fe24c

Please sign in to comment.