-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Create SystemIndexRegistry with helper method matchesSystemIndex #14415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
3666858
Create new extension point in SystemIndexPlugin for a single plugin t…
cwperks 0a54f0a
Add to CHANGELOG
cwperks 8f2a60b
WIP on system indices from IndexNameExpressionResolver
cwperks 12fd927
Merge branch 'system-indices-index-name-expression-resolver' into on-…
cwperks c8312ad
Add test in IndexNameExpressionResolverTests
cwperks a9cffb3
Remove changes in SystemIndexPlugin
cwperks 54eecd8
Merge branch 'main' into on-system-index-only
cwperks 8cc647e
Add method in IndexNameExpressionResolver to get matching system indices
cwperks 81dff6a
Show how resolver can be chained to get system indices
cwperks 5e8e9af
Fix forbiddenApis check
cwperks a81f2db
Update CHANGELOG
cwperks aa87d7f
Make SystemIndices internal
cwperks 766368e
Remove unneeded changes
cwperks 439cc64
Fix CI failures
cwperks c6a7679
Fix precommit errors
cwperks 8c1583b
Merge branch 'main' into on-system-index-only
cwperks 532f8a4
Merge branch 'main' into on-system-index-only
cwperks 14fbb9a
Merge branch 'on-system-index-only' of https://github.com/cwperks/Ope…
cwperks 8b4196e
Use Regex instead of WildcardMatcher
cwperks cc4dfb7
Merge branch 'main' into on-system-index-only
cwperks 11192dc
Address code review feedback
cwperks 884fa4c
Allow caller to pass index expressions
cwperks a75e167
Create SystemIndexRegistry
cwperks f7365b5
Update CHANGELOG
cwperks 179e8d8
Remove singleton limitation
cwperks e2ef9c4
Add javadoc
cwperks e71aa09
Add @ExperimentalApi annotation
cwperks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
server/src/main/java/org/opensearch/indices/SystemIndexRegistry.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.indices; | ||
|
|
||
| import org.apache.lucene.util.automaton.Automaton; | ||
| import org.apache.lucene.util.automaton.Operations; | ||
| import org.opensearch.common.annotation.ExperimentalApi; | ||
| import org.opensearch.common.collect.Tuple; | ||
| 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.stream.Collectors; | ||
|
|
||
| import static java.util.Collections.singletonList; | ||
| import static java.util.Collections.singletonMap; | ||
| import static java.util.Collections.unmodifiableMap; | ||
| import static org.opensearch.tasks.TaskResultsService.TASK_INDEX; | ||
|
|
||
| /** | ||
| * This class holds the {@link SystemIndexDescriptor} objects that represent system indices the | ||
| * node knows about. This class also contains static methods that identify if index expressions match | ||
| * registered system index patterns | ||
| * | ||
| * @opensearch.api | ||
| */ | ||
| @ExperimentalApi | ||
| public class SystemIndexRegistry { | ||
reta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private static final SystemIndexDescriptor TASK_INDEX_DESCRIPTOR = new SystemIndexDescriptor(TASK_INDEX + "*", "Task Result Index"); | ||
| private static final Map<String, Collection<SystemIndexDescriptor>> SERVER_SYSTEM_INDEX_DESCRIPTORS = singletonMap( | ||
| TaskResultsService.class.getName(), | ||
| singletonList(TASK_INDEX_DESCRIPTOR) | ||
| ); | ||
|
|
||
| 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 List<String> matchesSystemIndexPattern(String... indexExpressions) { | ||
| return Arrays.stream(indexExpressions) | ||
| .filter(pattern -> Regex.simpleMatch(SYSTEM_INDEX_PATTERNS, pattern)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| /** | ||
| * Given a collection of {@link SystemIndexDescriptor}s and their sources, checks to see if the index patterns of the listed | ||
| * descriptors overlap with any of the other patterns. If any do, throws an exception. | ||
| * | ||
| * @param sourceToDescriptors A map of source (plugin) names to the SystemIndexDescriptors they provide. | ||
| * @throws IllegalStateException Thrown if any of the index patterns overlaps with another. | ||
| */ | ||
| static void checkForOverlappingPatterns(Map<String, Collection<SystemIndexDescriptor>> sourceToDescriptors) { | ||
| List<Tuple<String, SystemIndexDescriptor>> sourceDescriptorPair = sourceToDescriptors.entrySet() | ||
| .stream() | ||
| .flatMap(entry -> entry.getValue().stream().map(descriptor -> new Tuple<>(entry.getKey(), descriptor))) | ||
| .sorted(Comparator.comparing(d -> d.v1() + ":" + d.v2().getIndexPattern())) // Consistent ordering -> consistent error message | ||
| .collect(Collectors.toList()); | ||
|
|
||
| // This is O(n^2) with the number of system index descriptors, and each check is quadratic with the number of states in the | ||
| // automaton, but the absolute number of system index descriptors should be quite small (~10s at most), and the number of states | ||
| // per pattern should be low as well. If these assumptions change, this might need to be reworked. | ||
| sourceDescriptorPair.forEach(descriptorToCheck -> { | ||
| List<Tuple<String, SystemIndexDescriptor>> descriptorsMatchingThisPattern = sourceDescriptorPair.stream() | ||
|
|
||
| .filter(d -> descriptorToCheck.v2() != d.v2()) // Exclude the pattern currently being checked | ||
| .filter(d -> overlaps(descriptorToCheck.v2(), d.v2())) | ||
| .collect(Collectors.toList()); | ||
| if (descriptorsMatchingThisPattern.isEmpty() == false) { | ||
| throw new IllegalStateException( | ||
| "a system index descriptor [" | ||
| + descriptorToCheck.v2() | ||
| + "] from [" | ||
| + descriptorToCheck.v1() | ||
| + "] overlaps with other system index descriptors: [" | ||
| + descriptorsMatchingThisPattern.stream() | ||
| .map(descriptor -> descriptor.v2() + " from [" + descriptor.v1() + "]") | ||
| .collect(Collectors.joining(", ")) | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private static boolean overlaps(SystemIndexDescriptor a1, SystemIndexDescriptor a2) { | ||
| Automaton a1Automaton = Regex.simpleMatchToAutomaton(a1.getIndexPattern()); | ||
| Automaton a2Automaton = Regex.simpleMatchToAutomaton(a2.getIndexPattern()); | ||
| return Operations.isEmpty(Operations.intersection(a1Automaton, a2Automaton)) == false; | ||
| } | ||
|
|
||
| private static Map<String, Collection<SystemIndexDescriptor>> buildSystemIndexDescriptorMap( | ||
| Map<String, Collection<SystemIndexDescriptor>> pluginAndModulesMap | ||
| ) { | ||
| final Map<String, Collection<SystemIndexDescriptor>> map = new HashMap<>( | ||
| pluginAndModulesMap.size() + SERVER_SYSTEM_INDEX_DESCRIPTORS.size() | ||
| ); | ||
| map.putAll(pluginAndModulesMap); | ||
| // put the server items last since we expect less of them | ||
| SERVER_SYSTEM_INDEX_DESCRIPTORS.forEach((source, descriptors) -> { | ||
| if (map.putIfAbsent(source, descriptors) != null) { | ||
| throw new IllegalArgumentException( | ||
| "plugin or module attempted to define the same source [" + source + "] as a built-in system index" | ||
| ); | ||
| } | ||
| }); | ||
| return unmodifiableMap(map); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.