forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create concept of pluginNodeClient that can be used for executing tra…
…nsport actions as the plugin Signed-off-by: Craig Perkins <[email protected]>
- Loading branch information
Showing
15 changed files
with
311 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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 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 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
29 changes: 29 additions & 0 deletions
29
...c/test/java/org/opensearch/http/executioncontextplugin/TestGetExecutionContextAction.java
This file contains 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,29 @@ | ||
/* | ||
* 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.executioncontextplugin; | ||
|
||
import org.opensearch.action.ActionType; | ||
|
||
/** | ||
* Test action to get the name of the plugin executing transport actions | ||
*/ | ||
public class TestGetExecutionContextAction extends ActionType<TestGetExecutionContextResponse> { | ||
/** | ||
* Get execution context action instance | ||
*/ | ||
public static final TestGetExecutionContextAction INSTANCE = new TestGetExecutionContextAction(); | ||
/** | ||
* Get execution context action name | ||
*/ | ||
public static final String NAME = "cluster:admin/executioncontext"; | ||
|
||
private TestGetExecutionContextAction() { | ||
super(NAME, TestGetExecutionContextResponse::new); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
.../test/java/org/opensearch/http/executioncontextplugin/TestGetExecutionContextRequest.java
This file contains 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,46 @@ | ||
/* | ||
* 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.executioncontextplugin; | ||
|
||
import org.opensearch.action.ActionRequest; | ||
import org.opensearch.action.ActionRequestValidationException; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Request object for GetExecutionContext transport action | ||
*/ | ||
public class TestGetExecutionContextRequest extends ActionRequest { | ||
|
||
/** | ||
* Default constructor | ||
*/ | ||
public TestGetExecutionContextRequest() {} | ||
|
||
/** | ||
* Constructor with stream input | ||
* @param in the stream input | ||
* @throws IOException IOException | ||
*/ | ||
public TestGetExecutionContextRequest(final StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
|
||
@Override | ||
public void writeTo(final StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...test/java/org/opensearch/http/executioncontextplugin/TestGetExecutionContextResponse.java
This file contains 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,55 @@ | ||
/* | ||
* 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.executioncontextplugin; | ||
|
||
import org.opensearch.core.action.ActionResponse; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.xcontent.ToXContentObject; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Response object containing the name of the plugin executing the transport action | ||
*/ | ||
public class TestGetExecutionContextResponse extends ActionResponse implements ToXContentObject { | ||
private final String pluginClassName; | ||
|
||
/** | ||
* Default constructor | ||
* | ||
* @param pluginClassName the canonical class name of the plugin executing the transport action | ||
*/ | ||
public TestGetExecutionContextResponse(String pluginClassName) { | ||
this.pluginClassName = pluginClassName; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(pluginClassName); | ||
} | ||
|
||
/** | ||
* Constructor with StreamInput | ||
* | ||
* @param in the stream input | ||
*/ | ||
public TestGetExecutionContextResponse(final StreamInput in) throws IOException { | ||
pluginClassName = in.readString(); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field("plugin_execution_context", pluginClassName); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
} |
This file contains 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
38 changes: 38 additions & 0 deletions
38
...va/org/opensearch/http/executioncontextplugin/TestGetExecutionContextTransportAction.java
This file contains 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,38 @@ | ||
/* | ||
* 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.executioncontextplugin; | ||
|
||
import org.opensearch.action.support.ActionFilters; | ||
import org.opensearch.action.support.HandledTransportAction; | ||
import org.opensearch.common.inject.Inject; | ||
import org.opensearch.common.util.concurrent.ThreadContext; | ||
import org.opensearch.core.action.ActionListener; | ||
import org.opensearch.tasks.Task; | ||
import org.opensearch.transport.TransportService; | ||
|
||
/** | ||
* Transport action for GetExecutionContext. | ||
* | ||
* Returns the canonical class name of the plugin that is currently executing the transport action. | ||
*/ | ||
public class TestGetExecutionContextTransportAction extends HandledTransportAction<TestGetExecutionContextRequest, TestGetExecutionContextResponse> { | ||
private final TransportService transportService; | ||
|
||
@Inject | ||
public TestGetExecutionContextTransportAction(TransportService transportService, ActionFilters actionFilters) { | ||
super(TestGetExecutionContextAction.NAME, transportService, actionFilters, TestGetExecutionContextRequest::new); | ||
this.transportService = transportService; | ||
} | ||
|
||
@Override | ||
protected void doExecute(Task task, TestGetExecutionContextRequest request, ActionListener<TestGetExecutionContextResponse> listener) { | ||
String pluginClassName = transportService.getThreadPool().getThreadContext().getHeader(ThreadContext.PLUGIN_EXECUTION_CONTEXT); | ||
listener.onResponse(new TestGetExecutionContextResponse(pluginClassName)); | ||
} | ||
} |
This file contains 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
89 changes: 89 additions & 0 deletions
89
server/src/main/java/org/opensearch/client/node/PluginNodeClient.java
This file contains 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,89 @@ | ||
/* | ||
* 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.client.node; | ||
|
||
import org.opensearch.action.ActionRequest; | ||
import org.opensearch.action.ActionType; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.common.util.concurrent.PluginContextSwitcher; | ||
import org.opensearch.common.util.concurrent.ThreadContext; | ||
import org.opensearch.core.action.ActionListener; | ||
import org.opensearch.core.action.ActionResponse; | ||
import org.opensearch.plugins.Plugin; | ||
import org.opensearch.tasks.Task; | ||
import org.opensearch.tasks.TaskListener; | ||
import org.opensearch.threadpool.ThreadPool; | ||
|
||
/** | ||
* Client that executes actions on the local node with contextual information about the plugin executing | ||
* transport actions. | ||
* | ||
* @opensearch.api | ||
*/ | ||
public class PluginNodeClient extends NodeClient { | ||
private final PluginContextSwitcher contextSwitcher; | ||
|
||
public PluginNodeClient(Settings settings, ThreadPool threadPool, Plugin plugin) { | ||
super(settings, threadPool); | ||
contextSwitcher = new PluginContextSwitcher(threadPool, plugin); | ||
} | ||
|
||
@Override | ||
public <Request extends ActionRequest, Response extends ActionResponse> void doExecute( | ||
ActionType<Response> action, | ||
Request request, | ||
ActionListener<Response> listener | ||
) { | ||
try (ThreadContext.StoredContext storedContext = contextSwitcher.switchContext()) { | ||
// Discard the task because the Client interface doesn't use it. | ||
executeLocally(action, request, listener); | ||
} | ||
} | ||
|
||
/** | ||
* Execute an {@link ActionType} locally, returning that {@link Task} used to track it, and linking an {@link ActionListener}. | ||
* Prefer this method if you don't need access to the task when listening for the response. This is the method used to implement | ||
* the {@link Client} interface. | ||
* | ||
* This client will execute the transport action in the context of the plugin executing this action | ||
*/ | ||
@Override | ||
public <Request extends ActionRequest, Response extends ActionResponse> Task executeLocally( | ||
ActionType<Response> action, | ||
Request request, | ||
ActionListener<Response> listener | ||
) { | ||
Task task; | ||
try (ThreadContext.StoredContext storedContext = contextSwitcher.switchContext()) { | ||
// Discard the task because the Client interface doesn't use it. | ||
task = transportAction(action).execute(request, listener); | ||
} | ||
return task; | ||
} | ||
|
||
/** | ||
* Execute an {@link ActionType} locally, returning that {@link Task} used to track it, and linking an {@link TaskListener}. Prefer this | ||
* method if you need access to the task when listening for the response. | ||
* | ||
* This client will execute the transport action in the context of the plugin executing this action | ||
*/ | ||
@Override | ||
public <Request extends ActionRequest, Response extends ActionResponse> Task executeLocally( | ||
ActionType<Response> action, | ||
Request request, | ||
TaskListener<Response> listener | ||
) { | ||
Task task; | ||
try (ThreadContext.StoredContext storedContext = contextSwitcher.switchContext()) { | ||
task = transportAction(action).execute(request, listener); | ||
} | ||
return task; | ||
} | ||
} |
This file contains 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.