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 new extension point for a single plugin to obtain registered system indices #177

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.http;

import org.opensearch.client.Request;
import org.opensearch.client.Response;
import org.opensearch.client.ResponseException;
import org.opensearch.plugins.Plugin;
import org.opensearch.test.OpenSearchIntegTestCase.ClusterScope;
import org.opensearch.test.OpenSearchIntegTestCase.Scope;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

/**
* Test a rest action that sets special response headers
*/
@ClusterScope(scope = Scope.SUITE, supportsDedicatedMasters = false, numDataNodes = 1)
public class ExecutionContextPluginGetIT extends HttpSmokeTestCase {

@Override
protected boolean addMockHttpTransport() {
return false; // enable http
}

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
ArrayList<Class<? extends Plugin>> plugins = new ArrayList<>(super.nodePlugins());
plugins.add(TestExecutionContextPlugin.class);
return plugins;
}

public void testGetExecutionContext() throws IOException {
ensureGreen();
Response response = getRestClient().performRequest(new Request("GET", "/_get_execution_context"));
String responseBody = new String(response.getEntity().getContent().readAllBytes(), StandardCharsets.UTF_8);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
assertThat(responseBody, containsString(TestExecutionContextPlugin.class.getName()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.http;

import org.opensearch.client.Request;
import org.opensearch.client.Response;
import org.opensearch.client.ResponseException;
import org.opensearch.plugins.Plugin;
import org.opensearch.test.OpenSearchIntegTestCase.ClusterScope;
import org.opensearch.test.OpenSearchIntegTestCase.Scope;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

/**
* Test a rest action that sets special response headers
*/
@ClusterScope(scope = Scope.SUITE, supportsDedicatedMasters = false, numDataNodes = 1)
public class ExecutionContextPluginSetIT extends HttpSmokeTestCase {

@Override
protected boolean addMockHttpTransport() {
return false; // enable http
}

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
ArrayList<Class<? extends Plugin>> plugins = new ArrayList<>(super.nodePlugins());
plugins.add(TestExecutionContextPlugin.class);
return plugins;
}

public void testThatPluginCannotOverrideExecutionContext() throws IOException {
ensureGreen();
try {
Response response = getRestClient().performRequest(new Request("POST", "/_set_execution_context"));
fail("request should have failed");
} catch(ResponseException e) {
Response response = e.getResponse();
String responseBody = new String(response.getEntity().getContent().readAllBytes(), StandardCharsets.UTF_8);
assertThat(response.getStatusLine().getStatusCode(), equalTo(400));
assertThat(responseBody, containsString("ExecutionContext already present"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.http;

import org.opensearch.client.Client;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.IndexScopedSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.settings.SettingsFilter;
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.env.Environment;
import org.opensearch.env.NodeEnvironment;
import org.opensearch.plugins.ActionPlugin;
import org.opensearch.plugins.Plugin;
import org.opensearch.repositories.RepositoriesService;
import org.opensearch.rest.RestController;
import org.opensearch.rest.RestHandler;
import org.opensearch.script.ScriptService;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.watcher.ResourceWatcherService;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

import static java.util.Collections.singletonList;

public class TestExecutionContextPlugin extends Plugin implements ActionPlugin {

private ThreadPool threadPool;

@Override
public Collection<Object> createComponents(
Client client,
ClusterService clusterService,
ThreadPool threadPool,
ResourceWatcherService resourceWatcherService,
ScriptService scriptService,
NamedXContentRegistry xContentRegistry,
Environment environment,
NodeEnvironment nodeEnvironment,
NamedWriteableRegistry namedWriteableRegistry,
IndexNameExpressionResolver expressionResolver,
Supplier<RepositoriesService> repositoriesServiceSupplier
) {
this.threadPool = threadPool;
return Collections.emptyList();
}

@Override
public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings,
IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<DiscoveryNodes> nodesInCluster) {
return List.of(new TestSetExecutionContextRestAction(threadPool), new TestGetExecutionContextRestAction(threadPool));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.http;

import org.opensearch.client.node.NodeClient;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestResponse;
import org.opensearch.threadpool.ThreadPool;

import java.util.List;

import static java.util.Collections.singletonList;
import static org.opensearch.rest.RestRequest.Method.GET;
import static org.opensearch.rest.RestRequest.Method.POST;

public class TestGetExecutionContextRestAction extends BaseRestHandler {

private final ThreadPool threadPool;

public TestGetExecutionContextRestAction(ThreadPool threadPool) {
this.threadPool = threadPool;
}

@Override
public List<Route> routes() {
return singletonList(new Route(GET, "/_get_execution_context"));
}

@Override
public String getName() {
return "test_get_execution_context_action";
}

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
String pluginMainClass = threadPool.getThreadContext().getExecutionContext();
RestResponse response = new BytesRestResponse(RestStatus.OK, pluginMainClass);
return channel -> channel.sendResponse(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.http;

import org.opensearch.client.node.NodeClient;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestResponse;
import org.opensearch.threadpool.ThreadPool;

import java.util.List;

import static java.util.Collections.singletonList;
import static org.opensearch.rest.RestRequest.Method.POST;

public class TestSetExecutionContextRestAction extends BaseRestHandler {

private final ThreadPool threadPool;

public TestSetExecutionContextRestAction(ThreadPool threadPool) {
this.threadPool = threadPool;
}

@Override
public List<Route> routes() {
return singletonList(new Route(POST, "/_set_execution_context"));
}

@Override
public String getName() {
return "test_set_execution_context_action";
}

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
threadPool.getThreadContext().setExecutionContext("should-not-allow-plugin-to-set-execution-context");
RestResponse response = new BytesRestResponse(RestStatus.OK, "Should not happen");
return channel -> channel.sendResponse(response);
}
}
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; };
}
}
4 changes: 3 additions & 1 deletion server/src/main/java/org/opensearch/action/ActionModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@
import org.opensearch.rest.NamedRoute;
import org.opensearch.rest.RestController;
import org.opensearch.rest.RestHandler;
import org.opensearch.rest.RestHandlerProxy;
import org.opensearch.rest.RestHeaderDefinition;
import org.opensearch.rest.action.RestFieldCapabilitiesAction;
import org.opensearch.rest.action.RestMainAction;
Expand Down Expand Up @@ -995,7 +996,8 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
indexNameExpressionResolver,
nodesInCluster
)) {
registerHandler.accept(handler);
RestHandler handlerProxy = RestHandlerProxy.newInstance(handler, threadPool, plugin);
registerHandler.accept(handlerProxy);
}
}
registerHandler.accept(new RestCatAction(catActions));
Expand Down
Loading
Loading