Skip to content

Commit 10b187e

Browse files
cwperksreta
authored andcommitted
Add matchesPluginSystemIndexPattern to SystemIndexRegistry (opensearch-project#14750)
* Add matchesPluginSystemIndexPattern to SystemIndexRegistry Signed-off-by: Craig Perkins <[email protected]> * Add to CHANGELOG Signed-off-by: Craig Perkins <[email protected]> * Use single data structure to keep track of system indices Signed-off-by: Craig Perkins <[email protected]> * Address code review comments Signed-off-by: Craig Perkins <[email protected]> * Add test for getAllDescriptors Signed-off-by: Craig Perkins <[email protected]> * Update server/src/main/java/org/opensearch/indices/SystemIndexRegistry.java Co-authored-by: Andriy Redko <[email protected]> Signed-off-by: Craig Perkins <[email protected]> --------- Signed-off-by: Craig Perkins <[email protected]> Signed-off-by: Craig Perkins <[email protected]> Co-authored-by: Andriy Redko <[email protected]> Signed-off-by: Kaushal Kumar <[email protected]>
1 parent 4b56657 commit 10b187e

File tree

5 files changed

+112
-35
lines changed

5 files changed

+112
-35
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1818
- [Workload Management] add queryGroupId header propagator across requests and nodes ([#14614](https://github.com/opensearch-project/OpenSearch/pull/14614))
1919
- Create SystemIndexRegistry with helper method matchesSystemIndex ([#14415](https://github.com/opensearch-project/OpenSearch/pull/14415))
2020
- Print reason why parent task was cancelled ([#14604](https://github.com/opensearch-project/OpenSearch/issues/14604))
21+
- Add matchesPluginSystemIndexPattern to SystemIndexRegistry ([#14750](https://github.com/opensearch-project/OpenSearch/pull/14750))
2122

2223
### Dependencies
2324
- Bump `org.gradle.test-retry` from 1.5.8 to 1.5.9 ([#13442](https://github.com/opensearch-project/OpenSearch/pull/13442))

server/src/main/java/org/opensearch/indices/SystemIndexRegistry.java

+23-13
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
import org.opensearch.common.regex.Regex;
1616
import org.opensearch.tasks.TaskResultsService;
1717

18-
import java.util.Arrays;
1918
import java.util.Collection;
2019
import java.util.Collections;
2120
import java.util.Comparator;
2221
import java.util.HashMap;
2322
import java.util.List;
2423
import java.util.Map;
24+
import java.util.Set;
2525
import java.util.stream.Collectors;
2626

2727
import static java.util.Collections.singletonList;
@@ -45,25 +45,35 @@ public class SystemIndexRegistry {
4545
);
4646

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

5050
static void register(Map<String, Collection<SystemIndexDescriptor>> pluginAndModulesDescriptors) {
5151
final Map<String, Collection<SystemIndexDescriptor>> descriptorsMap = buildSystemIndexDescriptorMap(pluginAndModulesDescriptors);
5252
checkForOverlappingPatterns(descriptorsMap);
53-
List<SystemIndexDescriptor> descriptors = pluginAndModulesDescriptors.values()
54-
.stream()
55-
.flatMap(Collection::stream)
56-
.collect(Collectors.toList());
57-
descriptors.add(TASK_INDEX_DESCRIPTOR);
5853

59-
SYSTEM_INDEX_DESCRIPTORS = descriptors.stream().collect(Collectors.toUnmodifiableList());
60-
SYSTEM_INDEX_PATTERNS = descriptors.stream().map(SystemIndexDescriptor::getIndexPattern).toArray(String[]::new);
54+
SYSTEM_INDEX_DESCRIPTORS_MAP = descriptorsMap;
55+
SYSTEM_INDEX_PATTERNS = getAllDescriptors().stream().map(SystemIndexDescriptor::getIndexPattern).toArray(String[]::new);
6156
}
6257

63-
public static List<String> matchesSystemIndexPattern(String... indexExpressions) {
64-
return Arrays.stream(indexExpressions)
65-
.filter(pattern -> Regex.simpleMatch(SYSTEM_INDEX_PATTERNS, pattern))
66-
.collect(Collectors.toList());
58+
public static Set<String> matchesSystemIndexPattern(Set<String> indexExpressions) {
59+
return indexExpressions.stream().filter(pattern -> Regex.simpleMatch(SYSTEM_INDEX_PATTERNS, pattern)).collect(Collectors.toSet());
60+
}
61+
62+
public static Set<String> matchesPluginSystemIndexPattern(String pluginClassName, Set<String> indexExpressions) {
63+
if (!SYSTEM_INDEX_DESCRIPTORS_MAP.containsKey(pluginClassName)) {
64+
return Collections.emptySet();
65+
}
66+
String[] pluginSystemIndexPatterns = SYSTEM_INDEX_DESCRIPTORS_MAP.get(pluginClassName)
67+
.stream()
68+
.map(SystemIndexDescriptor::getIndexPattern)
69+
.toArray(String[]::new);
70+
return indexExpressions.stream()
71+
.filter(pattern -> Regex.simpleMatch(pluginSystemIndexPatterns, pattern))
72+
.collect(Collectors.toSet());
73+
}
74+
75+
static List<SystemIndexDescriptor> getAllDescriptors() {
76+
return SYSTEM_INDEX_DESCRIPTORS_MAP.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
6777
}
6878

6979
/**

server/src/main/java/org/opensearch/indices/SystemIndices.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class SystemIndices {
6363

6464
public SystemIndices(Map<String, Collection<SystemIndexDescriptor>> pluginAndModulesDescriptors) {
6565
SystemIndexRegistry.register(pluginAndModulesDescriptors);
66-
this.runAutomaton = buildCharacterRunAutomaton(SystemIndexRegistry.SYSTEM_INDEX_DESCRIPTORS);
66+
this.runAutomaton = buildCharacterRunAutomaton(SystemIndexRegistry.getAllDescriptors());
6767
}
6868

6969
/**
@@ -91,7 +91,8 @@ public boolean isSystemIndex(String indexName) {
9191
* @throws IllegalStateException if multiple descriptors match the name
9292
*/
9393
public @Nullable SystemIndexDescriptor findMatchingDescriptor(String name) {
94-
final List<SystemIndexDescriptor> matchingDescriptors = SystemIndexRegistry.SYSTEM_INDEX_DESCRIPTORS.stream()
94+
final List<SystemIndexDescriptor> matchingDescriptors = SystemIndexRegistry.getAllDescriptors()
95+
.stream()
9596
.filter(descriptor -> descriptor.matchesIndexPattern(name))
9697
.collect(Collectors.toList());
9798

server/src/main/java/org/opensearch/node/Node.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,10 @@ protected Node(
699699
pluginsService.filterPlugins(SystemIndexPlugin.class)
700700
.stream()
701701
.collect(
702-
Collectors.toMap(plugin -> plugin.getClass().getSimpleName(), plugin -> plugin.getSystemIndexDescriptors(settings))
702+
Collectors.toMap(
703+
plugin -> plugin.getClass().getCanonicalName(),
704+
plugin -> plugin.getSystemIndexDescriptors(settings)
705+
)
703706
)
704707
);
705708
final SystemIndices systemIndices = new SystemIndices(systemIndexDescriptorMap);

server/src/test/java/org/opensearch/indices/SystemIndicesTests.java

+81-19
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import java.util.HashMap;
4545
import java.util.List;
4646
import java.util.Map;
47+
import java.util.Set;
48+
import java.util.stream.Collectors;
4749

4850
import static java.util.Collections.emptyMap;
4951
import static java.util.Collections.singletonList;
@@ -155,32 +157,61 @@ public void testSystemIndexMatching() {
155157
);
156158

157159
assertThat(
158-
SystemIndexRegistry.matchesSystemIndexPattern(".system-index1", ".system-index2"),
159-
equalTo(List.of(SystemIndexPlugin1.SYSTEM_INDEX_1, SystemIndexPlugin2.SYSTEM_INDEX_2))
160+
SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".system-index1", ".system-index2")),
161+
equalTo(Set.of(SystemIndexPlugin1.SYSTEM_INDEX_1, SystemIndexPlugin2.SYSTEM_INDEX_2))
160162
);
161-
assertThat(SystemIndexRegistry.matchesSystemIndexPattern(".system-index1"), equalTo(List.of(SystemIndexPlugin1.SYSTEM_INDEX_1)));
162-
assertThat(SystemIndexRegistry.matchesSystemIndexPattern(".system-index2"), equalTo(List.of(SystemIndexPlugin2.SYSTEM_INDEX_2)));
163-
assertThat(SystemIndexRegistry.matchesSystemIndexPattern(".system-index-pattern1"), equalTo(List.of(".system-index-pattern1")));
164163
assertThat(
165-
SystemIndexRegistry.matchesSystemIndexPattern(".system-index-pattern-sub*"),
166-
equalTo(List.of(".system-index-pattern-sub*"))
164+
SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".system-index1")),
165+
equalTo(Set.of(SystemIndexPlugin1.SYSTEM_INDEX_1))
167166
);
168167
assertThat(
169-
SystemIndexRegistry.matchesSystemIndexPattern(".system-index-pattern1", ".system-index-pattern2"),
170-
equalTo(List.of(".system-index-pattern1", ".system-index-pattern2"))
168+
SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".system-index2")),
169+
equalTo(Set.of(SystemIndexPlugin2.SYSTEM_INDEX_2))
171170
);
172171
assertThat(
173-
SystemIndexRegistry.matchesSystemIndexPattern(".system-index1", ".system-index-pattern1"),
174-
equalTo(List.of(".system-index1", ".system-index-pattern1"))
172+
SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".system-index-pattern1")),
173+
equalTo(Set.of(".system-index-pattern1"))
175174
);
176175
assertThat(
177-
SystemIndexRegistry.matchesSystemIndexPattern(".system-index1", ".system-index-pattern1", ".not-system"),
178-
equalTo(List.of(".system-index1", ".system-index-pattern1"))
176+
SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".system-index-pattern-sub*")),
177+
equalTo(Set.of(".system-index-pattern-sub*"))
178+
);
179+
assertThat(
180+
SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".system-index-pattern1", ".system-index-pattern2")),
181+
equalTo(Set.of(".system-index-pattern1", ".system-index-pattern2"))
182+
);
183+
assertThat(
184+
SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".system-index1", ".system-index-pattern1")),
185+
equalTo(Set.of(".system-index1", ".system-index-pattern1"))
186+
);
187+
assertThat(
188+
SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".system-index1", ".system-index-pattern1", ".not-system")),
189+
equalTo(Set.of(".system-index1", ".system-index-pattern1"))
190+
);
191+
assertThat(SystemIndexRegistry.matchesSystemIndexPattern(Set.of(".not-system")), equalTo(Collections.emptySet()));
192+
}
193+
194+
public void testRegisteredSystemIndexGetAllDescriptors() {
195+
SystemIndexPlugin plugin1 = new SystemIndexPlugin1();
196+
SystemIndexPlugin plugin2 = new SystemIndexPlugin2();
197+
SystemIndices pluginSystemIndices = new SystemIndices(
198+
Map.of(
199+
SystemIndexPlugin1.class.getCanonicalName(),
200+
plugin1.getSystemIndexDescriptors(Settings.EMPTY),
201+
SystemIndexPlugin2.class.getCanonicalName(),
202+
plugin2.getSystemIndexDescriptors(Settings.EMPTY)
203+
)
204+
);
205+
assertEquals(
206+
SystemIndexRegistry.getAllDescriptors()
207+
.stream()
208+
.map(SystemIndexDescriptor::getIndexPattern)
209+
.collect(Collectors.toUnmodifiableList()),
210+
List.of(SystemIndexPlugin1.SYSTEM_INDEX_1, SystemIndexPlugin2.SYSTEM_INDEX_2, TASK_INDEX + "*")
179211
);
180-
assertThat(SystemIndexRegistry.matchesSystemIndexPattern(".not-system"), equalTo(Collections.emptyList()));
181212
}
182213

183-
public void testRegisteredSystemIndexExpansion() {
214+
public void testRegisteredSystemIndexMatching() {
184215
SystemIndexPlugin plugin1 = new SystemIndexPlugin1();
185216
SystemIndexPlugin plugin2 = new SystemIndexPlugin2();
186217
SystemIndices pluginSystemIndices = new SystemIndices(
@@ -191,12 +222,43 @@ public void testRegisteredSystemIndexExpansion() {
191222
plugin2.getSystemIndexDescriptors(Settings.EMPTY)
192223
)
193224
);
194-
List<String> systemIndices = SystemIndexRegistry.matchesSystemIndexPattern(
195-
SystemIndexPlugin1.SYSTEM_INDEX_1,
196-
SystemIndexPlugin2.SYSTEM_INDEX_2
225+
Set<String> systemIndices = SystemIndexRegistry.matchesSystemIndexPattern(
226+
Set.of(SystemIndexPlugin1.SYSTEM_INDEX_1, SystemIndexPlugin2.SYSTEM_INDEX_2)
197227
);
198228
assertEquals(2, systemIndices.size());
199-
assertTrue(systemIndices.containsAll(List.of(SystemIndexPlugin1.SYSTEM_INDEX_1, SystemIndexPlugin2.SYSTEM_INDEX_2)));
229+
assertTrue(systemIndices.containsAll(Set.of(SystemIndexPlugin1.SYSTEM_INDEX_1, SystemIndexPlugin2.SYSTEM_INDEX_2)));
230+
}
231+
232+
public void testRegisteredSystemIndexMatchingForPlugin() {
233+
SystemIndexPlugin plugin1 = new SystemIndexPlugin1();
234+
SystemIndexPlugin plugin2 = new SystemIndexPlugin2();
235+
SystemIndices pluginSystemIndices = new SystemIndices(
236+
Map.of(
237+
SystemIndexPlugin1.class.getCanonicalName(),
238+
plugin1.getSystemIndexDescriptors(Settings.EMPTY),
239+
SystemIndexPlugin2.class.getCanonicalName(),
240+
plugin2.getSystemIndexDescriptors(Settings.EMPTY)
241+
)
242+
);
243+
Set<String> systemIndicesForPlugin1 = SystemIndexRegistry.matchesPluginSystemIndexPattern(
244+
SystemIndexPlugin1.class.getCanonicalName(),
245+
Set.of(SystemIndexPlugin1.SYSTEM_INDEX_1, SystemIndexPlugin2.SYSTEM_INDEX_2, "other-index")
246+
);
247+
assertEquals(1, systemIndicesForPlugin1.size());
248+
assertTrue(systemIndicesForPlugin1.contains(SystemIndexPlugin1.SYSTEM_INDEX_1));
249+
250+
Set<String> systemIndicesForPlugin2 = SystemIndexRegistry.matchesPluginSystemIndexPattern(
251+
SystemIndexPlugin2.class.getCanonicalName(),
252+
Set.of(SystemIndexPlugin1.SYSTEM_INDEX_1, SystemIndexPlugin2.SYSTEM_INDEX_2, "other-index")
253+
);
254+
assertEquals(1, systemIndicesForPlugin2.size());
255+
assertTrue(systemIndicesForPlugin2.contains(SystemIndexPlugin2.SYSTEM_INDEX_2));
256+
257+
Set<String> noMatchingSystemIndices = SystemIndexRegistry.matchesPluginSystemIndexPattern(
258+
SystemIndexPlugin2.class.getCanonicalName(),
259+
Set.of("other-index")
260+
);
261+
assertEquals(0, noMatchingSystemIndices.size());
200262
}
201263

202264
static final class SystemIndexPlugin1 extends Plugin implements SystemIndexPlugin {

0 commit comments

Comments
 (0)