Skip to content
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

Create SystemIndexRegistry with helper method matchesSystemIndex #14415

Merged
merged 27 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 Jun 17, 2024
0a54f0a
Add to CHANGELOG
cwperks Jun 17, 2024
8f2a60b
WIP on system indices from IndexNameExpressionResolver
cwperks Jun 20, 2024
12fd927
Merge branch 'system-indices-index-name-expression-resolver' into on-…
cwperks Jun 24, 2024
c8312ad
Add test in IndexNameExpressionResolverTests
cwperks Jun 24, 2024
a9cffb3
Remove changes in SystemIndexPlugin
cwperks Jun 24, 2024
54eecd8
Merge branch 'main' into on-system-index-only
cwperks Jun 24, 2024
8cc647e
Add method in IndexNameExpressionResolver to get matching system indices
cwperks Jun 26, 2024
81dff6a
Show how resolver can be chained to get system indices
cwperks Jun 26, 2024
5e8e9af
Fix forbiddenApis check
cwperks Jun 26, 2024
a81f2db
Update CHANGELOG
cwperks Jun 26, 2024
aa87d7f
Make SystemIndices internal
cwperks Jun 26, 2024
766368e
Remove unneeded changes
cwperks Jun 26, 2024
439cc64
Fix CI failures
cwperks Jun 26, 2024
c6a7679
Fix precommit errors
cwperks Jun 26, 2024
8c1583b
Merge branch 'main' into on-system-index-only
cwperks Jun 27, 2024
532f8a4
Merge branch 'main' into on-system-index-only
cwperks Jun 27, 2024
14fbb9a
Merge branch 'on-system-index-only' of https://github.com/cwperks/Ope…
cwperks Jun 27, 2024
8b4196e
Use Regex instead of WildcardMatcher
cwperks Jul 1, 2024
cc4dfb7
Merge branch 'main' into on-system-index-only
cwperks Jul 1, 2024
11192dc
Address code review feedback
cwperks Jul 1, 2024
884fa4c
Allow caller to pass index expressions
cwperks Jul 2, 2024
a75e167
Create SystemIndexRegistry
cwperks Jul 3, 2024
f7365b5
Update CHANGELOG
cwperks Jul 3, 2024
179e8d8
Remove singleton limitation
cwperks Jul 3, 2024
e2ef9c4
Add javadoc
cwperks Jul 3, 2024
e71aa09
Add @ExperimentalApi annotation
cwperks Jul 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased 2.x]
### Added
- Add fingerprint ingest processor ([#13724](https://github.com/opensearch-project/OpenSearch/pull/13724))
- Create new extension point in SystemIndexPlugin for a single plugin to get registered system indices ([#14415](https://github.com/opensearch-project/OpenSearch/pull/14415))

### Dependencies
- Bump `org.gradle.test-retry` from 1.5.8 to 1.5.9 ([#13442](https://github.com/opensearch-project/OpenSearch/pull/13442))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.plugins;

import org.opensearch.common.settings.Settings;
import org.opensearch.indices.SystemIndexDescriptor;
import org.opensearch.test.OpenSearchIntegTestCase;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

import static org.hamcrest.Matchers.containsString;

@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0)
public class SystemIndexPluginIT extends OpenSearchIntegTestCase {

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return List.of(SystemIndexPlugin1.class, SystemIndexPlugin2.class);
}

public void test2SystemIndexPluginsImplementOnSystemIndices_shouldFail() throws Exception {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> internalCluster().startNode());
assertThat(e.getMessage(), containsString("Cannot have more than one plugin implementing onSystemIndex"));
}

}

final class SystemIndexPlugin1 extends Plugin implements SystemIndexPlugin {

private Map<String, Set<String>> systemIndices;

public SystemIndexPlugin1() {}

@Override
public Collection<SystemIndexDescriptor> getSystemIndexDescriptors(Settings settings) {
final SystemIndexDescriptor systemIndexDescriptor = new SystemIndexDescriptor(".system-index1", "System index 1");
return Collections.singletonList(systemIndexDescriptor);
}

@Override
public Consumer<Map<String, Set<String>>> onSystemIndices() {
return (systemIndices) -> { this.systemIndices = systemIndices; };
}
}

class SystemIndexPlugin2 extends Plugin implements SystemIndexPlugin {

private Map<String, Set<String>> systemIndices;

public SystemIndexPlugin2() {}

@Override
public Collection<SystemIndexDescriptor> getSystemIndexDescriptors(Settings settings) {
final SystemIndexDescriptor systemIndexDescriptor = new SystemIndexDescriptor(".system-index2", "System index 2");
return Collections.singletonList(systemIndexDescriptor);
}

@Override
public Consumer<Map<String, Set<String>>> onSystemIndices() {
return (systemIndices) -> { this.systemIndices = systemIndices; };
}
}
34 changes: 32 additions & 2 deletions server/src/main/java/org/opensearch/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -814,15 +815,44 @@ protected Node(
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

List<SystemIndexPlugin> systemIndexPlugins = pluginsService.filterPlugins(SystemIndexPlugin.class);
final Map<String, Collection<SystemIndexDescriptor>> systemIndexDescriptorMap = Collections.unmodifiableMap(
pluginsService.filterPlugins(SystemIndexPlugin.class)
.stream()
systemIndexPlugins.stream()
.collect(
Collectors.toMap(plugin -> plugin.getClass().getSimpleName(), plugin -> plugin.getSystemIndexDescriptors(settings))
)
);
final SystemIndices systemIndices = new SystemIndices(systemIndexDescriptorMap);

final Map<String, Set<String>> systemIndexMap = Collections.unmodifiableMap(
systemIndexPlugins.stream()
.collect(
Collectors.toMap(
plugin -> plugin.getClass().getCanonicalName(),
plugin -> plugin.getSystemIndexDescriptors(settings)
.stream()
.map(SystemIndexDescriptor::getIndexPattern)
.collect(Collectors.toSet())
)
)
);

Consumer<Map<String, Set<String>>> onSystemIndex = null;
for (SystemIndexPlugin plugin : systemIndexPlugins) {
Consumer<Map<String, Set<String>>> newOnSystemIndex = plugin.onSystemIndices();
if (newOnSystemIndex != null) {
logger.debug("Using onSystemIndex from plugin " + plugin.getClass().getCanonicalName());
if (onSystemIndex != null) {
throw new IllegalArgumentException("Cannot have more than one plugin implementing onSystemIndex");
}
onSystemIndex = newOnSystemIndex;
}
}

if (onSystemIndex != null) {
onSystemIndex.accept(systemIndexMap);
}

final RerouteService rerouteService = new BatchedRerouteService(clusterService, clusterModule.getAllocationService()::reroute);
rerouteServiceReference.set(rerouteService);
clusterService.setRerouteService(rerouteService);
Expand Down
12 changes: 12 additions & 0 deletions server/src/main/java/org/opensearch/plugins/SystemIndexPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

/**
* Plugin for defining system indices. Extends {@link ActionPlugin} because system indices must be accessed via APIs
Expand All @@ -55,4 +58,13 @@ public interface SystemIndexPlugin extends ActionPlugin {
default Collection<SystemIndexDescriptor> getSystemIndexDescriptors(Settings settings) {
return Collections.emptyList();
}

/**
* This function passes the registered system indices to a plugin.
*
* Note: Only one installed plugin may implement onSystemIndices.
*/
default Consumer<Map<String, Set<String>>> onSystemIndices() {
cwperks marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
}
Loading