Skip to content

Commit

Permalink
Add test to verify that ExecutionContext is being populated during Re…
Browse files Browse the repository at this point in the history
…stHandling

Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Jun 11, 2024
1 parent 7e603e1 commit 5b989d6
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 172 deletions.
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
Expand Up @@ -6,33 +6,9 @@
* compatible open source license.
*/

/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.http;

import org.opensearch.client.Request;
import org.opensearch.client.RequestOptions;
import org.opensearch.client.Response;
import org.opensearch.client.ResponseException;
import org.opensearch.plugins.Plugin;
Expand All @@ -52,7 +28,7 @@
* Test a rest action that sets special response headers
*/
@ClusterScope(scope = Scope.SUITE, supportsDedicatedMasters = false, numDataNodes = 1)
public class ExecutionContextPluginIT extends HttpSmokeTestCase {
public class ExecutionContextPluginSetIT extends HttpSmokeTestCase {

@Override
protected boolean addMockHttpTransport() {
Expand All @@ -69,7 +45,7 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
public void testThatPluginCannotOverrideExecutionContext() throws IOException {
ensureGreen();
try {
Response response = getRestClient().performRequest(new Request("GET", "/_execution_context"));
Response response = getRestClient().performRequest(new Request("POST", "/_set_execution_context"));
fail("request should have failed");
} catch(ResponseException e) {
Response response = e.getResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,6 @@
* compatible open source license.
*/

/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.http;

import org.opensearch.client.Client;
Expand Down Expand Up @@ -86,6 +62,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 singletonList(new TestExecutionContextRestAction(threadPool));
return List.of(new TestSetExecutionContextRestAction(threadPool), new TestGetExecutionContextRestAction(threadPool));
}
}

This file was deleted.

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
Expand Up @@ -8,6 +8,10 @@

package org.opensearch.common.util.concurrent;

/**
* An ExecutionContext is a singular header within ThreadLocal that contains the identity of a plugin that is on
* the path of execution.
*/
public class ExecutionContext {
private final ThreadLocal<String> context = new ThreadLocal<>();

Expand Down
49 changes: 0 additions & 49 deletions server/src/main/java/org/opensearch/plugins/ActionPluginProxy.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
* RestHandlerProxy is a wrapper around {@link RestHandler} that populates the ExecutionContext prior
* to delegating execution to a plugin for handling a REST Request
*/
public class RestHandlerProxy implements InvocationHandler {
private final RestHandler restHandler;
private final ThreadPool threadPool;
Expand Down

0 comments on commit 5b989d6

Please sign in to comment.