From 75023538d8c536a90ccf476b5374c101903fa2d7 Mon Sep 17 00:00:00 2001 From: mloufra Date: Wed, 20 Jul 2022 05:11:36 +0000 Subject: [PATCH 01/15] issue #28 Signed-off-by: mloufra --- .../org/opensearch/sdk/ExtensionsRunner.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java index 7c6cf402..e2876820 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java +++ b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java @@ -22,8 +22,11 @@ import org.opensearch.common.network.NetworkService; import org.opensearch.common.settings.Settings; import org.opensearch.common.util.PageCacheRecycler; +<<<<<<< HEAD import org.opensearch.extensions.DiscoveryExtension; import org.opensearch.extensions.ExtensionBooleanResponse; +======= +>>>>>>> issue #28 import org.opensearch.discovery.InitializeExtensionsRequest; import org.opensearch.discovery.InitializeExtensionsResponse; import org.opensearch.extensions.ExtensionRequest; @@ -123,6 +126,7 @@ DiscoveryNode getOpensearchNode() { * @param extensionInitRequest The request to handle. * @return A response to OpenSearch validating that this is an extension. */ +<<<<<<< HEAD InitializeExtensionsResponse handleExtensionInitRequest(InitializeExtensionsRequest extensionInitRequest) { logger.info("Registering Extension Request received from OpenSearch"); InitializeExtensionsResponse initializeExtensionsResponse = new InitializeExtensionsResponse(extensionSettings.getExtensionName()); @@ -151,6 +155,14 @@ InitializeExtensionsResponse handleExtensionInitRequest(InitializeExtensionsRequ extensionTransportService.connectToNode(opensearchNode); sendRegisterRestActionsRequest(extensionTransportService); } +======= + InitializeExtensionsResponse handlePluginsRequest(InitializeExtensionsRequest initializeExtensionsRequest) { + logger.info("Registering Plugin Request received from OpenSearch"); + InitializeExtensionsResponse initializeExtensionsResponse = new InitializeExtensionsResponse("RealExtension"); + opensearchNode = initializeExtensionsRequest.getSourceNode(); + setOpensearchNode(opensearchNode); + return initializeExtensionsResponse; +>>>>>>> issue #28 } /** @@ -292,7 +304,11 @@ public void startTransportService(TransportService transportService) { false, false, InitializeExtensionsRequest::new, +<<<<<<< HEAD (request, channel, task) -> channel.sendResponse(handleExtensionInitRequest(request)) +======= + (request, channel, task) -> channel.sendResponse(handlePluginsRequest(request)) +>>>>>>> issue #28 ); transportService.registerRequestHandler( From dbb47f5eda557c4858d71b975da1f2e3f8e15f99 Mon Sep 17 00:00:00 2001 From: mloufra Date: Thu, 11 Aug 2022 17:09:48 +0000 Subject: [PATCH 02/15] Update the lastest coomit Signed-off-by: mloufra --- src/main/java/org/opensearch/sdk/ExtensionsRunner.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java index e2876820..65433a9c 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java +++ b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java @@ -127,10 +127,14 @@ DiscoveryNode getOpensearchNode() { * @return A response to OpenSearch validating that this is an extension. */ <<<<<<< HEAD +<<<<<<< HEAD +======= +>>>>>>> Update the lastest coomit InitializeExtensionsResponse handleExtensionInitRequest(InitializeExtensionsRequest extensionInitRequest) { logger.info("Registering Extension Request received from OpenSearch"); InitializeExtensionsResponse initializeExtensionsResponse = new InitializeExtensionsResponse(extensionSettings.getExtensionName()); opensearchNode = extensionInitRequest.getSourceNode(); +<<<<<<< HEAD // Fetch the unique ID for (DiscoveryExtension de : extensionInitRequest.getExtensions()) { if (de.getName().equals(extensionSettings.getExtensionName())) { @@ -160,6 +164,8 @@ InitializeExtensionsResponse handlePluginsRequest(InitializeExtensionsRequest in logger.info("Registering Plugin Request received from OpenSearch"); InitializeExtensionsResponse initializeExtensionsResponse = new InitializeExtensionsResponse("RealExtension"); opensearchNode = initializeExtensionsRequest.getSourceNode(); +======= +>>>>>>> Update the lastest coomit setOpensearchNode(opensearchNode); return initializeExtensionsResponse; >>>>>>> issue #28 From bea6873b2cc4b78974df84b9c6f25e5b1cf3b447 Mon Sep 17 00:00:00 2001 From: mloufra Date: Mon, 15 Aug 2022 17:23:31 +0000 Subject: [PATCH 03/15] Rename the method and fix the conflict Signed-off-by: mloufra --- src/main/java/org/opensearch/sdk/ExtensionsRunner.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java index 65433a9c..7f4c413a 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java +++ b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java @@ -310,11 +310,15 @@ public void startTransportService(TransportService transportService) { false, false, InitializeExtensionsRequest::new, +<<<<<<< HEAD <<<<<<< HEAD (request, channel, task) -> channel.sendResponse(handleExtensionInitRequest(request)) ======= (request, channel, task) -> channel.sendResponse(handlePluginsRequest(request)) >>>>>>> issue #28 +======= + (request, channel, task) -> channel.sendResponse(handleExtensionInitRequest(request)) +>>>>>>> Rename the method and fix the conflict ); transportService.registerRequestHandler( From 0e5dd7febddf209843c9c85bd2935dcfe6b181c1 Mon Sep 17 00:00:00 2001 From: mloufra Date: Mon, 15 Aug 2022 17:13:22 +0000 Subject: [PATCH 04/15] fix merge conflict Signed-off-by: mloufra --- src/main/java/org/opensearch/sdk/ExtensionsRunner.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java index 7f4c413a..4226a3e6 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java +++ b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java @@ -311,6 +311,7 @@ public void startTransportService(TransportService transportService) { false, InitializeExtensionsRequest::new, <<<<<<< HEAD +<<<<<<< HEAD <<<<<<< HEAD (request, channel, task) -> channel.sendResponse(handleExtensionInitRequest(request)) ======= @@ -319,6 +320,9 @@ public void startTransportService(TransportService transportService) { ======= (request, channel, task) -> channel.sendResponse(handleExtensionInitRequest(request)) >>>>>>> Rename the method and fix the conflict +======= + (request, channel, task) -> channel.sendResponse(handleExtensionInitRequest(request)) +>>>>>>> fix merge conflict ); transportService.registerRequestHandler( From 11bd14902f2d96e3e360193c6c549840d2f75092 Mon Sep 17 00:00:00 2001 From: mloufra Date: Fri, 26 Aug 2022 16:27:58 +0000 Subject: [PATCH 05/15] Add code coverage report Signed-off-by: mloufra --- .codecov.yml | 13 +++++++++++++ .github/workflows/build.yml | 6 ++++++ README.md | 2 ++ build.gradle | 11 +++++++++++ 4 files changed, 32 insertions(+) create mode 100644 .codecov.yml diff --git a/.codecov.yml b/.codecov.yml new file mode 100644 index 00000000..976e6096 --- /dev/null +++ b/.codecov.yml @@ -0,0 +1,13 @@ +codecov: + require_ci_to_pass: yes + +coverage: + precision: 2 + round: down + range: "70...100" + status: + project: + default: + target: 70% # the required coverage value + threshold: 1% # the leniency in hitting the target + \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f25b91f6..266e863c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -21,3 +21,9 @@ jobs: - name: Run Gradle Check run: | ./gradlew check + - name: Upload Coverage Report + uses: codecov/codecov-action@v3.1.0 + with: + file: ./build/reports/jacoco/test/jacocoTestReport.xml + flags: plugin + diff --git a/README.md b/README.md index a66c2646..7764a325 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![codecov](https://codecov.io/gh/opensearch-project/opensearch-sdk-java/branch/main/graph/badge.svg)](https://codecov.io/gh/opensearch-project/opensearch-sdk-java) + # OpenSearch SDK - [Introduction](#introduction) - [Contributing](#contributing) diff --git a/build.gradle b/build.gradle index 450c1ce5..a2365240 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,7 @@ plugins { id 'java' id "com.diffplug.spotless" version "6.3.0" apply false + id 'jacoco' } @@ -54,6 +55,16 @@ configurations { requireJavadoc } +jacoco { + toolVersion = "0.8.8" +} + +jacocoTestReport { + reports { + xml.required = true + } +} + dependencies { implementation "org.opensearch:opensearch:3.0.0-SNAPSHOT" implementation "org.opensearch.plugin:transport-netty4-client:3.0.0-SNAPSHOT" From bbccd262768cf32ef4d77a6c2085bf8c57e88b3d Mon Sep 17 00:00:00 2001 From: mloufra Date: Fri, 26 Aug 2022 16:50:32 +0000 Subject: [PATCH 06/15] Rebase the lastest commit Signed-off-by: mloufra --- .../org/opensearch/sdk/ExtensionsRunner.java | 30 ------------------- 1 file changed, 30 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java index 4226a3e6..7c6cf402 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java +++ b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java @@ -22,11 +22,8 @@ import org.opensearch.common.network.NetworkService; import org.opensearch.common.settings.Settings; import org.opensearch.common.util.PageCacheRecycler; -<<<<<<< HEAD import org.opensearch.extensions.DiscoveryExtension; import org.opensearch.extensions.ExtensionBooleanResponse; -======= ->>>>>>> issue #28 import org.opensearch.discovery.InitializeExtensionsRequest; import org.opensearch.discovery.InitializeExtensionsResponse; import org.opensearch.extensions.ExtensionRequest; @@ -126,15 +123,10 @@ DiscoveryNode getOpensearchNode() { * @param extensionInitRequest The request to handle. * @return A response to OpenSearch validating that this is an extension. */ -<<<<<<< HEAD -<<<<<<< HEAD -======= ->>>>>>> Update the lastest coomit InitializeExtensionsResponse handleExtensionInitRequest(InitializeExtensionsRequest extensionInitRequest) { logger.info("Registering Extension Request received from OpenSearch"); InitializeExtensionsResponse initializeExtensionsResponse = new InitializeExtensionsResponse(extensionSettings.getExtensionName()); opensearchNode = extensionInitRequest.getSourceNode(); -<<<<<<< HEAD // Fetch the unique ID for (DiscoveryExtension de : extensionInitRequest.getExtensions()) { if (de.getName().equals(extensionSettings.getExtensionName())) { @@ -159,16 +151,6 @@ InitializeExtensionsResponse handleExtensionInitRequest(InitializeExtensionsRequ extensionTransportService.connectToNode(opensearchNode); sendRegisterRestActionsRequest(extensionTransportService); } -======= - InitializeExtensionsResponse handlePluginsRequest(InitializeExtensionsRequest initializeExtensionsRequest) { - logger.info("Registering Plugin Request received from OpenSearch"); - InitializeExtensionsResponse initializeExtensionsResponse = new InitializeExtensionsResponse("RealExtension"); - opensearchNode = initializeExtensionsRequest.getSourceNode(); -======= ->>>>>>> Update the lastest coomit - setOpensearchNode(opensearchNode); - return initializeExtensionsResponse; ->>>>>>> issue #28 } /** @@ -310,19 +292,7 @@ public void startTransportService(TransportService transportService) { false, false, InitializeExtensionsRequest::new, -<<<<<<< HEAD -<<<<<<< HEAD -<<<<<<< HEAD (request, channel, task) -> channel.sendResponse(handleExtensionInitRequest(request)) -======= - (request, channel, task) -> channel.sendResponse(handlePluginsRequest(request)) ->>>>>>> issue #28 -======= - (request, channel, task) -> channel.sendResponse(handleExtensionInitRequest(request)) ->>>>>>> Rename the method and fix the conflict -======= - (request, channel, task) -> channel.sendResponse(handleExtensionInitRequest(request)) ->>>>>>> fix merge conflict ); transportService.registerRequestHandler( From 1bcb46cf678e3d4c55c282383a7252d49b71fd0b Mon Sep 17 00:00:00 2001 From: mloufra Date: Tue, 6 Sep 2022 22:10:16 +0000 Subject: [PATCH 07/15] update the lastest commit Signed-off-by: mloufra --- build.gradle | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/build.gradle b/build.gradle index a0ef2946..767863bc 100644 --- a/build.gradle +++ b/build.gradle @@ -55,16 +55,6 @@ configurations { requireJavadoc } -jacoco { - toolVersion = "0.8.8" -} - -jacocoTestReport { - reports { - xml.required = true - } -} - dependencies { implementation "org.opensearch:opensearch:3.0.0-SNAPSHOT" implementation "org.opensearch.plugin:transport-netty4-client:3.0.0-SNAPSHOT" From 8c57094cdc749220ea153fbb613288a03c77110c Mon Sep 17 00:00:00 2001 From: mloufra Date: Wed, 21 Sep 2022 21:09:37 +0000 Subject: [PATCH 08/15] refactor class for handler method in ExtensionsRunner Signed-off-by: mloufra --- .../org/opensearch/sdk/ExtensionsRunner.java | 63 ++++++++++------ ...nsionsIndicesModuleNameRequestHandler.java | 36 ++++++++++ ...ExtensionsIndicesModuleRequestHandler.java | 37 ++++++++++ .../ExtensionsInitRequestHandler.java | 72 +++++++++++++++++++ .../ExtensionsRestRequestHandler.java | 63 ++++++++++++++++ .../handlers/OpensearchRequestHandler.java | 40 +++++++++++ .../opensearch/sdk/TestExtensionsRunner.java | 13 ++-- 7 files changed, 296 insertions(+), 28 deletions(-) create mode 100644 src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleNameRequestHandler.java create mode 100644 src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleRequestHandler.java create mode 100644 src/main/java/org/opensearch/sdk/handlers/ExtensionsInitRequestHandler.java create mode 100644 src/main/java/org/opensearch/sdk/handlers/ExtensionsRestRequestHandler.java create mode 100644 src/main/java/org/opensearch/sdk/handlers/OpensearchRequestHandler.java diff --git a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java index 30b8ea59..035dd812 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java +++ b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java @@ -15,47 +15,52 @@ import org.opensearch.Version; import org.opensearch.cluster.ClusterModule; import org.opensearch.cluster.node.DiscoveryNode; -import org.opensearch.common.bytes.BytesReference; import org.opensearch.common.io.stream.NamedWriteableRegistry; import org.opensearch.common.io.stream.NamedWriteableRegistryParseRequest; import org.opensearch.extensions.OpenSearchRequest; import org.opensearch.extensions.rest.RegisterRestActionsRequest; import org.opensearch.extensions.rest.RestExecuteOnExtensionRequest; -import org.opensearch.extensions.rest.RestExecuteOnExtensionResponse; import org.opensearch.extensions.settings.RegisterCustomSettingsRequest; import org.opensearch.common.network.NetworkModule; import org.opensearch.common.network.NetworkService; import org.opensearch.common.settings.Setting; import org.opensearch.common.settings.Settings; import org.opensearch.common.util.PageCacheRecycler; -import org.opensearch.extensions.ExtensionBooleanResponse; import org.opensearch.discovery.InitializeExtensionsRequest; +<<<<<<< HEAD import org.opensearch.discovery.InitializeExtensionsResponse; import org.opensearch.extensions.ExtensionActionListenerOnFailureRequest; import org.opensearch.extensions.DiscoveryExtension; import org.opensearch.extensions.EnvironmentSettingsRequest; import org.opensearch.extensions.AddSettingsUpdateConsumerRequest; import org.opensearch.extensions.UpdateSettingsRequest; +======= +>>>>>>> refactor class for handler method in ExtensionsRunner import org.opensearch.extensions.ExtensionRequest; import org.opensearch.extensions.ExtensionsOrchestrator; import org.opensearch.index.IndicesModuleRequest; -import org.opensearch.index.IndicesModuleResponse; import org.opensearch.indices.IndicesModule; import org.opensearch.indices.breaker.CircuitBreakerService; import org.opensearch.indices.breaker.NoneCircuitBreakerService; -import org.opensearch.rest.RestStatus; import org.opensearch.rest.RestHandler.Route; -import org.opensearch.rest.RestResponse; import org.opensearch.transport.netty4.Netty4Transport; import org.opensearch.transport.SharedGroupFactory; import org.opensearch.sdk.handlers.ActionListenerOnFailureResponseHandler; import org.opensearch.sdk.handlers.ClusterSettingsResponseHandler; import org.opensearch.sdk.handlers.ClusterStateResponseHandler; +<<<<<<< HEAD import org.opensearch.sdk.handlers.EnvironmentSettingsResponseHandler; import org.opensearch.sdk.handlers.ExtensionBooleanResponseHandler; +======= +import org.opensearch.sdk.handlers.ExtensionsIndicesModuleNameRequestHandler; +import org.opensearch.sdk.handlers.ExtensionsIndicesModuleRequestHandler; +import org.opensearch.sdk.handlers.ExtensionsInitRequestHandler; +import org.opensearch.sdk.handlers.ExtensionsRestRequestHandler; +>>>>>>> refactor class for handler method in ExtensionsRunner import org.opensearch.sdk.handlers.LocalNodeResponseHandler; import org.opensearch.sdk.handlers.UpdateSettingsRequestHandler; import org.opensearch.sdk.handlers.ExtensionStringResponseHandler; +import org.opensearch.sdk.handlers.OpensearchRequestHandler; import org.opensearch.search.SearchModule; import org.opensearch.threadpool.ThreadPool; import org.opensearch.transport.ClusterConnectionManager; @@ -63,13 +68,11 @@ import org.opensearch.transport.TransportInterceptor; import org.opensearch.transport.TransportService; import org.opensearch.transport.TransportSettings; -import org.opensearch.transport.TransportResponse; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; @@ -90,12 +93,17 @@ public class ExtensionsRunner { private static final Logger logger = LogManager.getLogger(ExtensionsRunner.class); private static final String NODE_NAME_SETTING = "node.name"; +<<<<<<< HEAD private String uniqueId; private DiscoveryNode opensearchNode; private DiscoveryExtension extensionNode; +======= + private static String uniqueId; + private static DiscoveryNode opensearchNode; +>>>>>>> refactor class for handler method in ExtensionsRunner private TransportService extensionTransportService = null; // The routes and classes which handle the REST requests - private final ExtensionRestPathRegistry extensionRestPathRegistry = new ExtensionRestPathRegistry(); + private static ExtensionRestPathRegistry extensionRestPathRegistry = new ExtensionRestPathRegistry(); // Custom settings from the extension's getSettings private final List> customSettings; // Node name, host, and port @@ -103,6 +111,7 @@ public class ExtensionsRunner { private final TransportInterceptor NOOP_TRANSPORT_INTERCEPTOR = new TransportInterceptor() { }; private NamedWriteableRegistryAPI namedWriteableRegistryApi = new NamedWriteableRegistryAPI(); +<<<<<<< HEAD /* * TODO: expose an interface for extension to register actions * https://github.com/opensearch-project/opensearch-sdk-java/issues/119 @@ -112,6 +121,8 @@ public class ExtensionsRunner { * Instantiates a new update settings request handler */ UpdateSettingsRequestHandler updateSettingsRequestHandler = new UpdateSettingsRequestHandler(); +======= +>>>>>>> refactor class for handler method in ExtensionsRunner /** * Instantiates a new Extensions Runner using test settings. @@ -152,7 +163,7 @@ private ExtensionsRunner(Extension extension) throws IOException { // initialize the transport service this.initializeExtensionTransportService(this.getSettings()); // start listening on configured port and wait for connection from OpenSearch - this.startActionListener(0); + ExtensionsRunner.startActionListener(0); } private static ExtensionSettings readExtensionSettings() throws IOException { @@ -165,14 +176,11 @@ void setExtensionTransportService(TransportService extensionTransportService) { this.extensionTransportService = extensionTransportService; } - private void setUniqueId(String id) { - this.uniqueId = id; - } - - String getUniqueId() { + static String getUniqueId() { return uniqueId; } +<<<<<<< HEAD private void setOpensearchNode(DiscoveryNode opensearchNode) { this.opensearchNode = opensearchNode; } @@ -181,11 +189,14 @@ private void setExtensionNode(DiscoveryExtension extensionNode) { this.extensionNode = extensionNode; } +======= +>>>>>>> refactor class for handler method in ExtensionsRunner DiscoveryNode getOpensearchNode() { return opensearchNode; } /** +<<<<<<< HEAD * Handles a extension request from OpenSearch. This is the first request for the transport communication and will initialize the extension and will be a part of OpenSearch bootstrap. * * @param extensionInitRequest The request to handle. @@ -287,6 +298,8 @@ RestExecuteOnExtensionResponse handleRestExecuteOnExtensionRequest(RestExecuteOn } /** +======= +>>>>>>> refactor class for handler method in ExtensionsRunner * Initializes a Netty4Transport object. This object will be wrapped in a {@link TransportService} object. * * @param settings The transport settings to configure. @@ -381,7 +394,7 @@ public void startTransportService(TransportService transportService) { false, false, InitializeExtensionsRequest::new, - (request, channel, task) -> channel.sendResponse(handleExtensionInitRequest(request)) + (request, channel, task) -> channel.sendResponse(ExtensionsInitRequestHandler.handleExtensionInitRequest(request)) ); transportService.registerRequestHandler( @@ -390,7 +403,7 @@ public void startTransportService(TransportService transportService) { false, false, OpenSearchRequest::new, - (request, channel, task) -> channel.sendResponse(handleOpenSearchRequest(request)) + (request, channel, task) -> channel.sendResponse(OpensearchRequestHandler.handleOpenSearchRequest(request)) ); transportService.registerRequestHandler( @@ -408,7 +421,9 @@ public void startTransportService(TransportService transportService) { false, false, IndicesModuleRequest::new, - ((request, channel, task) -> channel.sendResponse(handleIndicesModuleRequest(request, transportService))) + ((request, channel, task) -> channel.sendResponse( + ExtensionsIndicesModuleRequestHandler.handleIndicesModuleRequest(request, transportService) + )) ); @@ -418,7 +433,9 @@ public void startTransportService(TransportService transportService) { false, false, IndicesModuleRequest::new, - ((request, channel, task) -> channel.sendResponse(handleIndicesModuleNameRequest(request))) + ((request, channel, task) -> channel.sendResponse( + ExtensionsIndicesModuleNameRequestHandler.handleIndicesModuleNameRequest(request) + )) ); transportService.registerRequestHandler( @@ -427,7 +444,7 @@ public void startTransportService(TransportService transportService) { false, false, RestExecuteOnExtensionRequest::new, - ((request, channel, task) -> channel.sendResponse(handleRestExecuteOnExtensionRequest(request))) + ((request, channel, task) -> channel.sendResponse(ExtensionsRestRequestHandler.handleRestExecuteOnExtensionRequest(request))) ); transportService.registerRequestHandler( @@ -446,7 +463,7 @@ public void startTransportService(TransportService transportService) { * * @param transportService The TransportService defining the connection to OpenSearch. */ - public void sendRegisterRestActionsRequest(TransportService transportService) { + public static void sendRegisterRestActionsRequest(TransportService transportService) { List extensionRestPaths = extensionRestPathRegistry.getRegisteredPaths(); logger.info("Sending Register REST Actions request to OpenSearch for " + extensionRestPaths); ExtensionStringResponseHandler registerActionsResponseHandler = new ExtensionStringResponseHandler(); @@ -632,7 +649,7 @@ private Settings getSettings() { * * @param timeout The timeout for the listener in milliseconds. A timeout of 0 means no timeout. */ - public void startActionListener(int timeout) { + public static void startActionListener(int timeout) { final ActionListener actionListener = new ActionListener(); actionListener.runActionListener(true, timeout); } @@ -662,6 +679,6 @@ public static void main(String[] args) throws IOException { // initialize the transport service extensionsRunner.initializeExtensionTransportService(extensionsRunner.getSettings()); // start listening on configured port and wait for connection from OpenSearch - extensionsRunner.startActionListener(0); + ExtensionsRunner.startActionListener(0); } } diff --git a/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleNameRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleNameRequestHandler.java new file mode 100644 index 00000000..34a754b6 --- /dev/null +++ b/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleNameRequestHandler.java @@ -0,0 +1,36 @@ +/* + * 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.sdk.handlers; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.opensearch.extensions.ExtensionBooleanResponse; +import org.opensearch.sdk.ExtensionsRunner; +import org.opensearch.index.IndicesModuleRequest; + +/** + * This class handles the request from OpenSearch to a {@link ExtensionsRunner#startTransportService(TransportService transportService)} call. + */ + +public class ExtensionsIndicesModuleNameRequestHandler { + private static final Logger logger = LogManager.getLogger(ExtensionsIndicesModuleNameRequestHandler.class); + + /** + * Handles a request for extension name from OpenSearch. The {@link #handleExtensionInitRequest(InitializeExtensionsRequest)} method must have been called first to initialize the extension. + * + * @param indicesModuleRequest The request to handle. + * @return A response acknowledging the request. + */ + ExtensionBooleanResponse handleIndicesModuleNameRequest(IndicesModuleRequest indicesModuleRequest) { + // Works as beforeIndexRemoved + logger.info("Registering Indices Module Name Request received from OpenSearch"); + ExtensionBooleanResponse indicesModuleNameResponse = new ExtensionBooleanResponse(true); + return indicesModuleNameResponse; + } + +} diff --git a/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleRequestHandler.java new file mode 100644 index 00000000..3b6611c0 --- /dev/null +++ b/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleRequestHandler.java @@ -0,0 +1,37 @@ +/* + * 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.sdk.handlers; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.opensearch.index.IndicesModuleRequest; +import org.opensearch.index.IndicesModuleResponse; +import org.opensearch.sdk.ExtensionsRunner; +import org.opensearch.transport.TransportService; + +/** + * This class handles the request from OpenSearch to a {@link ExtensionsRunner#startTransportService(TransportService transportService)} call. + */ + +public class ExtensionsIndicesModuleRequestHandler { + private static final Logger logger = LogManager.getLogger(ExtensionsIndicesModuleRequestHandler.class); + + /** + * Handles a request for extension point indices from OpenSearch. The {@link #handleExtensionInitRequest(InitializeExtensionsRequest)} method must have been called first to initialize the extension. + * + * @param indicesModuleRequest The request to handle. + * @param transportService The transport service communicating with OpenSearch. + * @return A response to OpenSearch with this extension's index and search listeners. + */ + IndicesModuleResponse handleIndicesModuleRequest(IndicesModuleRequest indicesModuleRequest, TransportService transportService) { + logger.info("Registering Indices Module Request received from OpenSearch"); + IndicesModuleResponse indicesModuleResponse = new IndicesModuleResponse(true, true, true); + return indicesModuleResponse; + } + +} diff --git a/src/main/java/org/opensearch/sdk/handlers/ExtensionsInitRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/ExtensionsInitRequestHandler.java new file mode 100644 index 00000000..c6a97984 --- /dev/null +++ b/src/main/java/org/opensearch/sdk/handlers/ExtensionsInitRequestHandler.java @@ -0,0 +1,72 @@ +/* + * 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.sdk.handlers; + +import java.util.HashMap; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.opensearch.cluster.node.DiscoveryNode; +import org.opensearch.common.settings.Settings; +import org.opensearch.discovery.InitializeExtensionsRequest; +import org.opensearch.discovery.InitializeExtensionsResponse; +import org.opensearch.sdk.ExtensionsRunner; +import org.opensearch.sdk.TransportActions; +import org.opensearch.transport.TransportService; + +/** + * This class handles the request from OpenSearch to a {@link ExtensionsRunner#startTransportService(TransportService transportService)} call. + */ + +public class ExtensionsInitRequestHandler { + private static final Logger logger = LogManager.getLogger(ExtensionsInitRequestHandler.class); + private static final String NODE_NAME_SETTING = "node.name"; + private static DiscoveryNode opensearchNode; + private static Settings settings; + private static String uniqueId; + private static TransportService extensionTransportService = null; + private static TransportActions transportActions = new TransportActions(new HashMap<>()); + + private static void setUniqueId(String id) { + ExtensionsInitRequestHandler.uniqueId = id; + } + + String getUniqueId() { + return uniqueId; + } + + private static void setOpensearchNode(DiscoveryNode opensearchNode) { + ExtensionsInitRequestHandler.opensearchNode = opensearchNode; + } + + DiscoveryNode getOpensearchNode() { + return opensearchNode; + } + + /** + * Handles a extension request from OpenSearch. This is the first request for the transport communication and will initialize the extension and will be a part of OpenSearch bootstrap. + * + * @param extensionInitRequest The request to handle. + * @return A response to OpenSearch validating that this is an extension. + */ + public static InitializeExtensionsResponse handleExtensionInitRequest(InitializeExtensionsRequest extensionInitRequest) { + logger.info("Registering Extension Request received from OpenSearch"); + opensearchNode = extensionInitRequest.getSourceNode(); + setUniqueId(extensionInitRequest.getExtension().getId()); + // Successfully initialized. Send the response. + try { + return new InitializeExtensionsResponse(settings.get(NODE_NAME_SETTING)); + } finally { + // After sending successful response to initialization, send the REST API + setOpensearchNode(opensearchNode); + extensionTransportService.connectToNode(opensearchNode); + ExtensionsRunner.sendRegisterRestActionsRequest(extensionTransportService); + transportActions.sendRegisterTransportActionsRequest(extensionTransportService, opensearchNode); + } + } +} diff --git a/src/main/java/org/opensearch/sdk/handlers/ExtensionsRestRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/ExtensionsRestRequestHandler.java new file mode 100644 index 00000000..52876bcc --- /dev/null +++ b/src/main/java/org/opensearch/sdk/handlers/ExtensionsRestRequestHandler.java @@ -0,0 +1,63 @@ +/* + * 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.sdk.handlers; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.opensearch.common.bytes.BytesReference; +import org.opensearch.extensions.rest.RestExecuteOnExtensionRequest; +import org.opensearch.extensions.rest.RestExecuteOnExtensionResponse; +import org.opensearch.rest.RestResponse; +import org.opensearch.rest.RestStatus; +import org.opensearch.sdk.ExtensionRestHandler; +import org.opensearch.sdk.ExtensionsRunner; +import org.opensearch.sdk.ExtensionRestPathRegistry; +import org.opensearch.sdk.ExtensionRestRequest; + +/** + * This class handles the request from OpenSearch to a {@link ExtensionsRunner#startTransportService(TransportService transportService)} call. + */ + +public class ExtensionsRestRequestHandler { + private static final Logger logger = LogManager.getLogger(ExtensionsRestRequestHandler.class); + private static ExtensionRestPathRegistry extensionRestPathRegistry = new ExtensionRestPathRegistry(); + + /** + * Handles a request from OpenSearch to execute a REST request on the extension. + * + * @param request The REST request to execute. + * @return A response acknowledging the request. + */ + RestExecuteOnExtensionResponse handleRestExecuteOnExtensionRequest(RestExecuteOnExtensionRequest request) { + + ExtensionRestHandler restHandler = extensionRestPathRegistry.getHandler(request.getMethod(), request.getUri()); + if (restHandler == null) { + return new RestExecuteOnExtensionResponse( + RestStatus.NOT_FOUND, + "No handler for " + ExtensionRestPathRegistry.restPathToString(request.getMethod(), request.getUri()) + ); + } + // ExtensionRestRequest restRequest = new ExtensionRestRequest(request); + ExtensionRestRequest restRequest = new ExtensionRestRequest( + request.getMethod(), + request.getUri(), + request.getRequestIssuerIdentity() + ); + + // Get response from extension + RestResponse response = restHandler.handleRequest(restRequest); + logger.info("Sending extension response to OpenSearch: " + response.status()); + return new RestExecuteOnExtensionResponse( + response.status(), + response.contentType(), + BytesReference.toBytes(response.content()), + response.getHeaders() + ); + } + +} diff --git a/src/main/java/org/opensearch/sdk/handlers/OpensearchRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/OpensearchRequestHandler.java new file mode 100644 index 00000000..e5e6ebc9 --- /dev/null +++ b/src/main/java/org/opensearch/sdk/handlers/OpensearchRequestHandler.java @@ -0,0 +1,40 @@ +/* + * 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.sdk.handlers; + +import org.opensearch.extensions.OpenSearchRequest; +import org.opensearch.sdk.NamedWriteableRegistryAPI; +import org.opensearch.sdk.ExtensionsRunner; +import org.opensearch.transport.TransportResponse; + +/** + * This class handles the request from OpenSearch to a {@link ExtensionsRunner#startTransportService(TransportService transportService)} call. + */ + +public class OpensearchRequestHandler { + private static NamedWriteableRegistryAPI namedWriteableRegistryApi = new NamedWriteableRegistryAPI(); + + /** + * Handles a request from OpenSearch and invokes the extension point API corresponding with the request type + * + * @param request The request to handle. + * @return A response to OpenSearch for the corresponding API + * @throws Exception if the corresponding handler for the request is not present + */ + TransportResponse handleOpenSearchRequest(OpenSearchRequest request) throws Exception { + // Read enum + switch (request.getRequestType()) { + case REQUEST_OPENSEARCH_NAMED_WRITEABLE_REGISTRY: + return namedWriteableRegistryApi.handleNamedWriteableRegistryRequest(request); + // Add additional request handlers here + default: + throw new Exception("Handler not present for the provided request"); + } + } + +} diff --git a/src/test/java/org/opensearch/sdk/TestExtensionsRunner.java b/src/test/java/org/opensearch/sdk/TestExtensionsRunner.java index 66e3a7ea..4cdef114 100644 --- a/src/test/java/org/opensearch/sdk/TestExtensionsRunner.java +++ b/src/test/java/org/opensearch/sdk/TestExtensionsRunner.java @@ -57,8 +57,11 @@ import org.opensearch.sdk.handlers.ClusterSettingsResponseHandler; import org.opensearch.sdk.handlers.ClusterStateResponseHandler; import org.opensearch.sdk.handlers.EnvironmentSettingsResponseHandler; +import org.opensearch.sdk.handlers.ExtensionsInitRequestHandler; +import org.opensearch.sdk.handlers.ExtensionsRestRequestHandler; import org.opensearch.sdk.handlers.LocalNodeResponseHandler; import org.opensearch.sdk.handlers.ExtensionStringResponseHandler; +import org.opensearch.sdk.handlers.OpensearchRequestHandler; import org.opensearch.test.OpenSearchTestCase; import org.opensearch.transport.Transport; import org.opensearch.transport.TransportService; @@ -141,10 +144,10 @@ public void testHandleExtensionInitRequest() throws UnknownHostException { InitializeExtensionsRequest extensionInitRequest = new InitializeExtensionsRequest(sourceNode, extension); - InitializeExtensionsResponse response = extensionsRunner.handleExtensionInitRequest(extensionInitRequest); + InitializeExtensionsResponse response = ExtensionsInitRequestHandler.handleExtensionInitRequest(extensionInitRequest); // Test if name and unique ID are set assertEquals(EXTENSION_NAME, response.getName()); - assertEquals("opensearch-sdk-1", extensionsRunner.getUniqueId()); + assertEquals("opensearch-sdk-1", ExtensionsRunner.getUniqueId()); // Test if the source node is set after handleExtensionInitRequest() is called during OpenSearch bootstrap assertEquals(sourceNode, extensionsRunner.getOpensearchNode()); } @@ -153,7 +156,7 @@ public void testHandleExtensionInitRequest() throws UnknownHostException { public void testHandleOpenSearchRequest() throws Exception { OpenSearchRequest request = new OpenSearchRequest(OpenSearchRequestType.REQUEST_OPENSEARCH_NAMED_WRITEABLE_REGISTRY); - assertEquals(NamedWriteableRegistryResponse.class, extensionsRunner.handleOpenSearchRequest(request).getClass()); + assertEquals(NamedWriteableRegistryResponse.class, OpensearchRequestHandler.handleOpenSearchRequest(request).getClass()); // Add additional OpenSearch request handler tests here for each default extension point } @@ -164,7 +167,7 @@ public void testHandleRestExecuteOnExtensionRequest() throws Exception { ExtensionTokenProcessor ext = new ExtensionTokenProcessor(EXTENSION_NAME); Principal userPrincipal = () -> "user1"; RestExecuteOnExtensionRequest request = new RestExecuteOnExtensionRequest(Method.GET, "/foo", ext.generateToken(userPrincipal)); - RestExecuteOnExtensionResponse response = extensionsRunner.handleRestExecuteOnExtensionRequest(request); + RestExecuteOnExtensionResponse response = ExtensionsRestRequestHandler.handleRestExecuteOnExtensionRequest(request); // this will fail in test environment with no registered actions assertEquals(RestStatus.NOT_FOUND, response.getStatus()); assertEquals(BytesRestResponse.TEXT_CONTENT_TYPE, response.getContentType()); @@ -229,7 +232,7 @@ public void testEnvironmentSettingsRequest() { @Test public void testRegisterRestActionsRequest() { - extensionsRunner.sendRegisterRestActionsRequest(transportService); + ExtensionsRunner.sendRegisterRestActionsRequest(transportService); verify(transportService, times(1)).sendRequest(any(), anyString(), any(), any(ExtensionStringResponseHandler.class)); } From 5b459027c36f60f71a068a51facfa210dbff1c70 Mon Sep 17 00:00:00 2001 From: mloufra Date: Thu, 22 Sep 2022 07:22:21 +0000 Subject: [PATCH 09/15] add documentation for handler Signed-off-by: mloufra --- .../org/opensearch/sdk/ExtensionsRunner.java | 152 +++--------------- ...nsionsIndicesModuleNameRequestHandler.java | 6 +- ...ExtensionsIndicesModuleRequestHandler.java | 6 +- .../ExtensionsInitRequestHandler.java | 41 +++-- .../ExtensionsRestRequestHandler.java | 4 +- .../handlers/OpensearchRequestHandler.java | 6 +- .../opensearch/sdk/TestExtensionsRunner.java | 16 +- 7 files changed, 71 insertions(+), 160 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java index 035dd812..3cec388b 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java +++ b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java @@ -27,15 +27,11 @@ import org.opensearch.common.settings.Settings; import org.opensearch.common.util.PageCacheRecycler; import org.opensearch.discovery.InitializeExtensionsRequest; -<<<<<<< HEAD -import org.opensearch.discovery.InitializeExtensionsResponse; import org.opensearch.extensions.ExtensionActionListenerOnFailureRequest; import org.opensearch.extensions.DiscoveryExtension; import org.opensearch.extensions.EnvironmentSettingsRequest; import org.opensearch.extensions.AddSettingsUpdateConsumerRequest; import org.opensearch.extensions.UpdateSettingsRequest; -======= ->>>>>>> refactor class for handler method in ExtensionsRunner import org.opensearch.extensions.ExtensionRequest; import org.opensearch.extensions.ExtensionsOrchestrator; import org.opensearch.index.IndicesModuleRequest; @@ -48,15 +44,12 @@ import org.opensearch.sdk.handlers.ActionListenerOnFailureResponseHandler; import org.opensearch.sdk.handlers.ClusterSettingsResponseHandler; import org.opensearch.sdk.handlers.ClusterStateResponseHandler; -<<<<<<< HEAD import org.opensearch.sdk.handlers.EnvironmentSettingsResponseHandler; import org.opensearch.sdk.handlers.ExtensionBooleanResponseHandler; -======= import org.opensearch.sdk.handlers.ExtensionsIndicesModuleNameRequestHandler; import org.opensearch.sdk.handlers.ExtensionsIndicesModuleRequestHandler; import org.opensearch.sdk.handlers.ExtensionsInitRequestHandler; import org.opensearch.sdk.handlers.ExtensionsRestRequestHandler; ->>>>>>> refactor class for handler method in ExtensionsRunner import org.opensearch.sdk.handlers.LocalNodeResponseHandler; import org.opensearch.sdk.handlers.UpdateSettingsRequestHandler; import org.opensearch.sdk.handlers.ExtensionStringResponseHandler; @@ -93,14 +86,9 @@ public class ExtensionsRunner { private static final Logger logger = LogManager.getLogger(ExtensionsRunner.class); private static final String NODE_NAME_SETTING = "node.name"; -<<<<<<< HEAD private String uniqueId; private DiscoveryNode opensearchNode; private DiscoveryExtension extensionNode; -======= - private static String uniqueId; - private static DiscoveryNode opensearchNode; ->>>>>>> refactor class for handler method in ExtensionsRunner private TransportService extensionTransportService = null; // The routes and classes which handle the REST requests private static ExtensionRestPathRegistry extensionRestPathRegistry = new ExtensionRestPathRegistry(); @@ -111,18 +99,17 @@ public class ExtensionsRunner { private final TransportInterceptor NOOP_TRANSPORT_INTERCEPTOR = new TransportInterceptor() { }; private NamedWriteableRegistryAPI namedWriteableRegistryApi = new NamedWriteableRegistryAPI(); -<<<<<<< HEAD - /* - * TODO: expose an interface for extension to register actions - * https://github.com/opensearch-project/opensearch-sdk-java/issues/119 - */ - private TransportActions transportActions = new TransportActions(new HashMap<>()); + private ExtensionsInitRequestHandler extensionsInitRequestHandler = new ExtensionsInitRequestHandler(); + private OpensearchRequestHandler opensearchRequestHandler = new OpensearchRequestHandler(); + private ExtensionsIndicesModuleRequestHandler extensionsIndicesModuleRequestHandler = new ExtensionsIndicesModuleRequestHandler(); + private ExtensionsIndicesModuleNameRequestHandler extensionsIndicesModuleNameRequestHandler = + new ExtensionsIndicesModuleNameRequestHandler(); + private ExtensionsRestRequestHandler extensionsRestRequestHandler = new ExtensionsRestRequestHandler(); + /** * Instantiates a new update settings request handler */ UpdateSettingsRequestHandler updateSettingsRequestHandler = new UpdateSettingsRequestHandler(); -======= ->>>>>>> refactor class for handler method in ExtensionsRunner /** * Instantiates a new Extensions Runner using test settings. @@ -176,11 +163,14 @@ void setExtensionTransportService(TransportService extensionTransportService) { this.extensionTransportService = extensionTransportService; } - static String getUniqueId() { + private void setUniqueId(String id) { + this.uniqueId = id; + } + + String getUniqueId() { return uniqueId; } -<<<<<<< HEAD private void setOpensearchNode(DiscoveryNode opensearchNode) { this.opensearchNode = opensearchNode; } @@ -189,117 +179,11 @@ private void setExtensionNode(DiscoveryExtension extensionNode) { this.extensionNode = extensionNode; } -======= ->>>>>>> refactor class for handler method in ExtensionsRunner DiscoveryNode getOpensearchNode() { return opensearchNode; } /** -<<<<<<< HEAD - * Handles a extension request from OpenSearch. This is the first request for the transport communication and will initialize the extension and will be a part of OpenSearch bootstrap. - * - * @param extensionInitRequest The request to handle. - * @return A response to OpenSearch validating that this is an extension. - */ - InitializeExtensionsResponse handleExtensionInitRequest(InitializeExtensionsRequest extensionInitRequest) { - logger.info("Registering Extension Request received from OpenSearch"); - opensearchNode = extensionInitRequest.getSourceNode(); - setUniqueId(extensionInitRequest.getExtension().getId()); - // Successfully initialized. Send the response. - try { - return new InitializeExtensionsResponse(settings.get(NODE_NAME_SETTING)); - } finally { - // After sending successful response to initialization, send the REST API and Settings - setOpensearchNode(opensearchNode); - setExtensionNode(extensionInitRequest.getExtension()); - extensionTransportService.connectToNode(opensearchNode); - sendRegisterRestActionsRequest(extensionTransportService); - sendRegisterCustomSettingsRequest(extensionTransportService); - transportActions.sendRegisterTransportActionsRequest(extensionTransportService, opensearchNode); - } - } - - /** - * Handles a request from OpenSearch and invokes the extension point API corresponding with the request type - * - * @param request The request to handle. - * @return A response to OpenSearch for the corresponding API - * @throws Exception if the corresponding handler for the request is not present - */ - TransportResponse handleOpenSearchRequest(OpenSearchRequest request) throws Exception { - // Read enum - switch (request.getRequestType()) { - case REQUEST_OPENSEARCH_NAMED_WRITEABLE_REGISTRY: - return namedWriteableRegistryApi.handleNamedWriteableRegistryRequest(request); - // Add additional request handlers here - default: - throw new Exception("Handler not present for the provided request"); - } - } - - /** - * Handles a request for extension point indices from OpenSearch. The {@link #handleExtensionInitRequest(InitializeExtensionsRequest)} method must have been called first to initialize the extension. - * - * @param indicesModuleRequest The request to handle. - * @param transportService The transport service communicating with OpenSearch. - * @return A response to OpenSearch with this extension's index and search listeners. - */ - IndicesModuleResponse handleIndicesModuleRequest(IndicesModuleRequest indicesModuleRequest, TransportService transportService) { - logger.info("Registering Indices Module Request received from OpenSearch"); - IndicesModuleResponse indicesModuleResponse = new IndicesModuleResponse(true, true, true); - return indicesModuleResponse; - } - - /** - * Handles a request for extension name from OpenSearch. The {@link #handleExtensionInitRequest(InitializeExtensionsRequest)} method must have been called first to initialize the extension. - * - * @param indicesModuleRequest The request to handle. - * @return A response acknowledging the request. - */ - ExtensionBooleanResponse handleIndicesModuleNameRequest(IndicesModuleRequest indicesModuleRequest) { - // Works as beforeIndexRemoved - logger.info("Registering Indices Module Name Request received from OpenSearch"); - ExtensionBooleanResponse indicesModuleNameResponse = new ExtensionBooleanResponse(true); - return indicesModuleNameResponse; - } - - /** - * Handles a request from OpenSearch to execute a REST request on the extension. - * - * @param request The REST request to execute. - * @return A response acknowledging the request. - */ - RestExecuteOnExtensionResponse handleRestExecuteOnExtensionRequest(RestExecuteOnExtensionRequest request) { - - ExtensionRestHandler restHandler = extensionRestPathRegistry.getHandler(request.getMethod(), request.getUri()); - if (restHandler == null) { - return new RestExecuteOnExtensionResponse( - RestStatus.NOT_FOUND, - "No handler for " + ExtensionRestPathRegistry.restPathToString(request.getMethod(), request.getUri()) - ); - } - // ExtensionRestRequest restRequest = new ExtensionRestRequest(request); - ExtensionRestRequest restRequest = new ExtensionRestRequest( - request.getMethod(), - request.getUri(), - request.getRequestIssuerIdentity() - ); - - // Get response from extension - RestResponse response = restHandler.handleRequest(restRequest); - logger.info("Sending extension response to OpenSearch: " + response.status()); - return new RestExecuteOnExtensionResponse( - response.status(), - response.contentType(), - BytesReference.toBytes(response.content()), - response.getHeaders() - ); - } - - /** -======= ->>>>>>> refactor class for handler method in ExtensionsRunner * Initializes a Netty4Transport object. This object will be wrapped in a {@link TransportService} object. * * @param settings The transport settings to configure. @@ -394,7 +278,7 @@ public void startTransportService(TransportService transportService) { false, false, InitializeExtensionsRequest::new, - (request, channel, task) -> channel.sendResponse(ExtensionsInitRequestHandler.handleExtensionInitRequest(request)) + (request, channel, task) -> channel.sendResponse(extensionsInitRequestHandler.handleExtensionInitRequest(request, null)) ); transportService.registerRequestHandler( @@ -403,7 +287,7 @@ public void startTransportService(TransportService transportService) { false, false, OpenSearchRequest::new, - (request, channel, task) -> channel.sendResponse(OpensearchRequestHandler.handleOpenSearchRequest(request)) + (request, channel, task) -> channel.sendResponse(opensearchRequestHandler.handleOpenSearchRequest(request)) ); transportService.registerRequestHandler( @@ -422,7 +306,7 @@ public void startTransportService(TransportService transportService) { false, IndicesModuleRequest::new, ((request, channel, task) -> channel.sendResponse( - ExtensionsIndicesModuleRequestHandler.handleIndicesModuleRequest(request, transportService) + extensionsIndicesModuleRequestHandler.handleIndicesModuleRequest(request, transportService) )) ); @@ -434,7 +318,7 @@ public void startTransportService(TransportService transportService) { false, IndicesModuleRequest::new, ((request, channel, task) -> channel.sendResponse( - ExtensionsIndicesModuleNameRequestHandler.handleIndicesModuleNameRequest(request) + extensionsIndicesModuleNameRequestHandler.handleIndicesModuleNameRequest(request) )) ); @@ -444,7 +328,7 @@ public void startTransportService(TransportService transportService) { false, false, RestExecuteOnExtensionRequest::new, - ((request, channel, task) -> channel.sendResponse(ExtensionsRestRequestHandler.handleRestExecuteOnExtensionRequest(request))) + ((request, channel, task) -> channel.sendResponse(extensionsRestRequestHandler.handleRestExecuteOnExtensionRequest(request))) ); transportService.registerRequestHandler( @@ -463,7 +347,7 @@ public void startTransportService(TransportService transportService) { * * @param transportService The TransportService defining the connection to OpenSearch. */ - public static void sendRegisterRestActionsRequest(TransportService transportService) { + public void sendRegisterRestActionsRequest(TransportService transportService) { List extensionRestPaths = extensionRestPathRegistry.getRegisteredPaths(); logger.info("Sending Register REST Actions request to OpenSearch for " + extensionRestPaths); ExtensionStringResponseHandler registerActionsResponseHandler = new ExtensionStringResponseHandler(); diff --git a/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleNameRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleNameRequestHandler.java index 34a754b6..68cbe6b7 100644 --- a/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleNameRequestHandler.java +++ b/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleNameRequestHandler.java @@ -12,6 +12,8 @@ import org.opensearch.extensions.ExtensionBooleanResponse; import org.opensearch.sdk.ExtensionsRunner; import org.opensearch.index.IndicesModuleRequest; +import org.opensearch.discovery.InitializeExtensionsRequest; +import org.opensearch.sdk.handlers.ExtensionsInitRequestHandler; /** * This class handles the request from OpenSearch to a {@link ExtensionsRunner#startTransportService(TransportService transportService)} call. @@ -21,12 +23,12 @@ public class ExtensionsIndicesModuleNameRequestHandler { private static final Logger logger = LogManager.getLogger(ExtensionsIndicesModuleNameRequestHandler.class); /** - * Handles a request for extension name from OpenSearch. The {@link #handleExtensionInitRequest(InitializeExtensionsRequest)} method must have been called first to initialize the extension. + * Handles a request for extension name from OpenSearch. The {@link ExtensionsInitRequestHandler#handleExtensionInitRequest(InitializeExtensionsRequest)} method must have been called first to initialize the extension. * * @param indicesModuleRequest The request to handle. * @return A response acknowledging the request. */ - ExtensionBooleanResponse handleIndicesModuleNameRequest(IndicesModuleRequest indicesModuleRequest) { + public ExtensionBooleanResponse handleIndicesModuleNameRequest(IndicesModuleRequest indicesModuleRequest) { // Works as beforeIndexRemoved logger.info("Registering Indices Module Name Request received from OpenSearch"); ExtensionBooleanResponse indicesModuleNameResponse = new ExtensionBooleanResponse(true); diff --git a/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleRequestHandler.java index 3b6611c0..684477ea 100644 --- a/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleRequestHandler.java +++ b/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleRequestHandler.java @@ -13,6 +13,8 @@ import org.opensearch.index.IndicesModuleResponse; import org.opensearch.sdk.ExtensionsRunner; import org.opensearch.transport.TransportService; +import org.opensearch.discovery.InitializeExtensionsRequest; +import org.opensearch.sdk.handlers.ExtensionsInitRequestHandler; /** * This class handles the request from OpenSearch to a {@link ExtensionsRunner#startTransportService(TransportService transportService)} call. @@ -22,13 +24,13 @@ public class ExtensionsIndicesModuleRequestHandler { private static final Logger logger = LogManager.getLogger(ExtensionsIndicesModuleRequestHandler.class); /** - * Handles a request for extension point indices from OpenSearch. The {@link #handleExtensionInitRequest(InitializeExtensionsRequest)} method must have been called first to initialize the extension. + * Handles a request for extension point indices from OpenSearch. The {@link ExtensionsInitRequestHandler#handleExtensionInitRequest(InitializeExtensionsRequest)} method must have been called first to initialize the extension. * * @param indicesModuleRequest The request to handle. * @param transportService The transport service communicating with OpenSearch. * @return A response to OpenSearch with this extension's index and search listeners. */ - IndicesModuleResponse handleIndicesModuleRequest(IndicesModuleRequest indicesModuleRequest, TransportService transportService) { + public IndicesModuleResponse handleIndicesModuleRequest(IndicesModuleRequest indicesModuleRequest, TransportService transportService) { logger.info("Registering Indices Module Request received from OpenSearch"); IndicesModuleResponse indicesModuleResponse = new IndicesModuleResponse(true, true, true); return indicesModuleResponse; diff --git a/src/main/java/org/opensearch/sdk/handlers/ExtensionsInitRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/ExtensionsInitRequestHandler.java index c6a97984..10a1fe08 100644 --- a/src/main/java/org/opensearch/sdk/handlers/ExtensionsInitRequestHandler.java +++ b/src/main/java/org/opensearch/sdk/handlers/ExtensionsInitRequestHandler.java @@ -15,6 +15,7 @@ import org.opensearch.common.settings.Settings; import org.opensearch.discovery.InitializeExtensionsRequest; import org.opensearch.discovery.InitializeExtensionsResponse; +import org.opensearch.extensions.DiscoveryExtension; import org.opensearch.sdk.ExtensionsRunner; import org.opensearch.sdk.TransportActions; import org.opensearch.transport.TransportService; @@ -26,35 +27,49 @@ public class ExtensionsInitRequestHandler { private static final Logger logger = LogManager.getLogger(ExtensionsInitRequestHandler.class); private static final String NODE_NAME_SETTING = "node.name"; - private static DiscoveryNode opensearchNode; - private static Settings settings; - private static String uniqueId; - private static TransportService extensionTransportService = null; - private static TransportActions transportActions = new TransportActions(new HashMap<>()); + private DiscoveryNode opensearchNode; + private DiscoveryExtension extensionNode; + private Settings settings; + private String uniqueId; + private TransportService extensionTransportService = null; - private static void setUniqueId(String id) { - ExtensionsInitRequestHandler.uniqueId = id; + private void setUniqueId(String id) { + this.uniqueId = id; } String getUniqueId() { return uniqueId; } - private static void setOpensearchNode(DiscoveryNode opensearchNode) { - ExtensionsInitRequestHandler.opensearchNode = opensearchNode; + private void setOpensearchNode(DiscoveryNode opensearchNode) { + this.opensearchNode = opensearchNode; + } + + private void setExtensionNode(DiscoveryExtension extensionNode) { + this.extensionNode = extensionNode; } DiscoveryNode getOpensearchNode() { return opensearchNode; } + /* + * TODO: expose an interface for extension to register actions + * https://github.com/opensearch-project/opensearch-sdk-java/issues/119 + */ + private TransportActions transportActions = new TransportActions(new HashMap<>()); + /** * Handles a extension request from OpenSearch. This is the first request for the transport communication and will initialize the extension and will be a part of OpenSearch bootstrap. * * @param extensionInitRequest The request to handle. + * @param extensionsRunner The method call from handler. * @return A response to OpenSearch validating that this is an extension. */ - public static InitializeExtensionsResponse handleExtensionInitRequest(InitializeExtensionsRequest extensionInitRequest) { + public InitializeExtensionsResponse handleExtensionInitRequest( + InitializeExtensionsRequest extensionInitRequest, + ExtensionsRunner extensionsRunner + ) { logger.info("Registering Extension Request received from OpenSearch"); opensearchNode = extensionInitRequest.getSourceNode(); setUniqueId(extensionInitRequest.getExtension().getId()); @@ -62,10 +77,12 @@ public static InitializeExtensionsResponse handleExtensionInitRequest(Initialize try { return new InitializeExtensionsResponse(settings.get(NODE_NAME_SETTING)); } finally { - // After sending successful response to initialization, send the REST API + // After sending successful response to initialization, send the REST API and Settings setOpensearchNode(opensearchNode); + setExtensionNode(extensionInitRequest.getExtension()); extensionTransportService.connectToNode(opensearchNode); - ExtensionsRunner.sendRegisterRestActionsRequest(extensionTransportService); + extensionsRunner.sendRegisterRestActionsRequest(extensionTransportService); + extensionsRunner.sendRegisterCustomSettingsRequest(extensionTransportService); transportActions.sendRegisterTransportActionsRequest(extensionTransportService, opensearchNode); } } diff --git a/src/main/java/org/opensearch/sdk/handlers/ExtensionsRestRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/ExtensionsRestRequestHandler.java index 52876bcc..447306de 100644 --- a/src/main/java/org/opensearch/sdk/handlers/ExtensionsRestRequestHandler.java +++ b/src/main/java/org/opensearch/sdk/handlers/ExtensionsRestRequestHandler.java @@ -25,7 +25,7 @@ public class ExtensionsRestRequestHandler { private static final Logger logger = LogManager.getLogger(ExtensionsRestRequestHandler.class); - private static ExtensionRestPathRegistry extensionRestPathRegistry = new ExtensionRestPathRegistry(); + private ExtensionRestPathRegistry extensionRestPathRegistry = new ExtensionRestPathRegistry(); /** * Handles a request from OpenSearch to execute a REST request on the extension. @@ -33,7 +33,7 @@ public class ExtensionsRestRequestHandler { * @param request The REST request to execute. * @return A response acknowledging the request. */ - RestExecuteOnExtensionResponse handleRestExecuteOnExtensionRequest(RestExecuteOnExtensionRequest request) { + public RestExecuteOnExtensionResponse handleRestExecuteOnExtensionRequest(RestExecuteOnExtensionRequest request) { ExtensionRestHandler restHandler = extensionRestPathRegistry.getHandler(request.getMethod(), request.getUri()); if (restHandler == null) { diff --git a/src/main/java/org/opensearch/sdk/handlers/OpensearchRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/OpensearchRequestHandler.java index e5e6ebc9..04b0ee30 100644 --- a/src/main/java/org/opensearch/sdk/handlers/OpensearchRequestHandler.java +++ b/src/main/java/org/opensearch/sdk/handlers/OpensearchRequestHandler.java @@ -17,7 +17,7 @@ */ public class OpensearchRequestHandler { - private static NamedWriteableRegistryAPI namedWriteableRegistryApi = new NamedWriteableRegistryAPI(); + private NamedWriteableRegistryAPI namedWriteableRegistryApi = new NamedWriteableRegistryAPI(); /** * Handles a request from OpenSearch and invokes the extension point API corresponding with the request type @@ -26,14 +26,14 @@ public class OpensearchRequestHandler { * @return A response to OpenSearch for the corresponding API * @throws Exception if the corresponding handler for the request is not present */ - TransportResponse handleOpenSearchRequest(OpenSearchRequest request) throws Exception { + public TransportResponse handleOpenSearchRequest(OpenSearchRequest request) throws Exception { // Read enum switch (request.getRequestType()) { case REQUEST_OPENSEARCH_NAMED_WRITEABLE_REGISTRY: return namedWriteableRegistryApi.handleNamedWriteableRegistryRequest(request); // Add additional request handlers here default: - throw new Exception("Handler not present for the provided request"); + throw new IllegalArgumentException("Handler not present for the provided request"); } } diff --git a/src/test/java/org/opensearch/sdk/TestExtensionsRunner.java b/src/test/java/org/opensearch/sdk/TestExtensionsRunner.java index 4cdef114..54688a88 100644 --- a/src/test/java/org/opensearch/sdk/TestExtensionsRunner.java +++ b/src/test/java/org/opensearch/sdk/TestExtensionsRunner.java @@ -70,6 +70,9 @@ public class TestExtensionsRunner extends OpenSearchTestCase { private static final String EXTENSION_NAME = "sample-extension"; + private ExtensionsInitRequestHandler extensionsInitRequestHandler = new ExtensionsInitRequestHandler(); + private OpensearchRequestHandler opensearchRequestHandler = new OpensearchRequestHandler(); + private ExtensionsRestRequestHandler extensionsRestRequestHandler = new ExtensionsRestRequestHandler(); private ExtensionsRunner extensionsRunner; private TransportService transportService; @@ -144,10 +147,13 @@ public void testHandleExtensionInitRequest() throws UnknownHostException { InitializeExtensionsRequest extensionInitRequest = new InitializeExtensionsRequest(sourceNode, extension); - InitializeExtensionsResponse response = ExtensionsInitRequestHandler.handleExtensionInitRequest(extensionInitRequest); + InitializeExtensionsResponse response = extensionsInitRequestHandler.handleExtensionInitRequest( + extensionInitRequest, + extensionsRunner + ); // Test if name and unique ID are set assertEquals(EXTENSION_NAME, response.getName()); - assertEquals("opensearch-sdk-1", ExtensionsRunner.getUniqueId()); + assertEquals("opensearch-sdk-1", extensionsRunner.getUniqueId()); // Test if the source node is set after handleExtensionInitRequest() is called during OpenSearch bootstrap assertEquals(sourceNode, extensionsRunner.getOpensearchNode()); } @@ -156,7 +162,7 @@ public void testHandleExtensionInitRequest() throws UnknownHostException { public void testHandleOpenSearchRequest() throws Exception { OpenSearchRequest request = new OpenSearchRequest(OpenSearchRequestType.REQUEST_OPENSEARCH_NAMED_WRITEABLE_REGISTRY); - assertEquals(NamedWriteableRegistryResponse.class, OpensearchRequestHandler.handleOpenSearchRequest(request).getClass()); + assertEquals(NamedWriteableRegistryResponse.class, opensearchRequestHandler.handleOpenSearchRequest(request).getClass()); // Add additional OpenSearch request handler tests here for each default extension point } @@ -167,7 +173,7 @@ public void testHandleRestExecuteOnExtensionRequest() throws Exception { ExtensionTokenProcessor ext = new ExtensionTokenProcessor(EXTENSION_NAME); Principal userPrincipal = () -> "user1"; RestExecuteOnExtensionRequest request = new RestExecuteOnExtensionRequest(Method.GET, "/foo", ext.generateToken(userPrincipal)); - RestExecuteOnExtensionResponse response = ExtensionsRestRequestHandler.handleRestExecuteOnExtensionRequest(request); + RestExecuteOnExtensionResponse response = extensionsRestRequestHandler.handleRestExecuteOnExtensionRequest(request); // this will fail in test environment with no registered actions assertEquals(RestStatus.NOT_FOUND, response.getStatus()); assertEquals(BytesRestResponse.TEXT_CONTENT_TYPE, response.getContentType()); @@ -232,7 +238,7 @@ public void testEnvironmentSettingsRequest() { @Test public void testRegisterRestActionsRequest() { - ExtensionsRunner.sendRegisterRestActionsRequest(transportService); + extensionsRunner.sendRegisterRestActionsRequest(transportService); verify(transportService, times(1)).sendRequest(any(), anyString(), any(), any(ExtensionStringResponseHandler.class)); } From 8ab9c52859d7e99985b00a27b00c1b7cb6b5d68b Mon Sep 17 00:00:00 2001 From: mloufra Date: Mon, 26 Sep 2022 22:48:15 +0000 Subject: [PATCH 10/15] fix merge conflict Signed-off-by: mloufra --- .../java/org/opensearch/sdk/ExtensionsRunner.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java index 3cec388b..bbce7aa8 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java +++ b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java @@ -93,12 +93,13 @@ public class ExtensionsRunner { // The routes and classes which handle the REST requests private static ExtensionRestPathRegistry extensionRestPathRegistry = new ExtensionRestPathRegistry(); // Custom settings from the extension's getSettings - private final List> customSettings; + private static List> customSettings; // Node name, host, and port private final Settings settings; private final TransportInterceptor NOOP_TRANSPORT_INTERCEPTOR = new TransportInterceptor() { }; private NamedWriteableRegistryAPI namedWriteableRegistryApi = new NamedWriteableRegistryAPI(); +<<<<<<< HEAD private ExtensionsInitRequestHandler extensionsInitRequestHandler = new ExtensionsInitRequestHandler(); private OpensearchRequestHandler opensearchRequestHandler = new OpensearchRequestHandler(); private ExtensionsIndicesModuleRequestHandler extensionsIndicesModuleRequestHandler = new ExtensionsIndicesModuleRequestHandler(); @@ -110,6 +111,8 @@ public class ExtensionsRunner { * Instantiates a new update settings request handler */ UpdateSettingsRequestHandler updateSettingsRequestHandler = new UpdateSettingsRequestHandler(); +======= +>>>>>>> fix merge conflict /** * Instantiates a new Extensions Runner using test settings. @@ -123,7 +126,7 @@ public ExtensionsRunner() throws IOException { .put(TransportSettings.BIND_HOST.getKey(), extensionSettings.getHostAddress()) .put(TransportSettings.PORT.getKey(), extensionSettings.getHostPort()) .build(); - this.customSettings = Collections.emptyList(); + ExtensionsRunner.customSettings = Collections.emptyList(); } /** @@ -146,7 +149,7 @@ private ExtensionsRunner(Extension extension) throws IOException { } } // save custom settings - this.customSettings = extension.getSettings(); + ExtensionsRunner.customSettings = extension.getSettings(); // initialize the transport service this.initializeExtensionTransportService(this.getSettings()); // start listening on configured port and wait for connection from OpenSearch @@ -159,6 +162,9 @@ private static ExtensionSettings readExtensionSettings() throws IOException { return objectMapper.readValue(file, ExtensionSettings.class); } + /** + * @param extensionTransportService + */ void setExtensionTransportService(TransportService extensionTransportService) { this.extensionTransportService = extensionTransportService; } @@ -368,7 +374,7 @@ public void sendRegisterRestActionsRequest(TransportService transportService) { * * @param transportService The TransportService defining the connection to OpenSearch. */ - public void sendRegisterCustomSettingsRequest(TransportService transportService) { + public static void sendRegisterCustomSettingsRequest(TransportService transportService) { logger.info("Sending Settings request to OpenSearch"); ExtensionStringResponseHandler registerCustomSettingsResponseHandler = new ExtensionStringResponseHandler(); try { From d33bec5f80bbbb46b5784b0d94b76e6cc6cc93c1 Mon Sep 17 00:00:00 2001 From: mloufra Date: Tue, 27 Sep 2022 21:58:33 +0000 Subject: [PATCH 11/15] delete all the static Signed-off-by: mloufra --- .../org/opensearch/sdk/ExtensionsRunner.java | 22 +++++++------------ ...nsionsIndicesModuleNameRequestHandler.java | 3 +-- ...ExtensionsIndicesModuleRequestHandler.java | 3 +-- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java index bbce7aa8..d1554651 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java +++ b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java @@ -91,15 +91,14 @@ public class ExtensionsRunner { private DiscoveryExtension extensionNode; private TransportService extensionTransportService = null; // The routes and classes which handle the REST requests - private static ExtensionRestPathRegistry extensionRestPathRegistry = new ExtensionRestPathRegistry(); + private final ExtensionRestPathRegistry extensionRestPathRegistry = new ExtensionRestPathRegistry(); // Custom settings from the extension's getSettings - private static List> customSettings; + private final List> customSettings; // Node name, host, and port private final Settings settings; private final TransportInterceptor NOOP_TRANSPORT_INTERCEPTOR = new TransportInterceptor() { }; private NamedWriteableRegistryAPI namedWriteableRegistryApi = new NamedWriteableRegistryAPI(); -<<<<<<< HEAD private ExtensionsInitRequestHandler extensionsInitRequestHandler = new ExtensionsInitRequestHandler(); private OpensearchRequestHandler opensearchRequestHandler = new OpensearchRequestHandler(); private ExtensionsIndicesModuleRequestHandler extensionsIndicesModuleRequestHandler = new ExtensionsIndicesModuleRequestHandler(); @@ -111,8 +110,6 @@ public class ExtensionsRunner { * Instantiates a new update settings request handler */ UpdateSettingsRequestHandler updateSettingsRequestHandler = new UpdateSettingsRequestHandler(); -======= ->>>>>>> fix merge conflict /** * Instantiates a new Extensions Runner using test settings. @@ -126,7 +123,7 @@ public ExtensionsRunner() throws IOException { .put(TransportSettings.BIND_HOST.getKey(), extensionSettings.getHostAddress()) .put(TransportSettings.PORT.getKey(), extensionSettings.getHostPort()) .build(); - ExtensionsRunner.customSettings = Collections.emptyList(); + this.customSettings = Collections.emptyList(); } /** @@ -149,11 +146,11 @@ private ExtensionsRunner(Extension extension) throws IOException { } } // save custom settings - ExtensionsRunner.customSettings = extension.getSettings(); + this.customSettings = extension.getSettings(); // initialize the transport service this.initializeExtensionTransportService(this.getSettings()); // start listening on configured port and wait for connection from OpenSearch - ExtensionsRunner.startActionListener(0); + this.startActionListener(0); } private static ExtensionSettings readExtensionSettings() throws IOException { @@ -162,9 +159,6 @@ private static ExtensionSettings readExtensionSettings() throws IOException { return objectMapper.readValue(file, ExtensionSettings.class); } - /** - * @param extensionTransportService - */ void setExtensionTransportService(TransportService extensionTransportService) { this.extensionTransportService = extensionTransportService; } @@ -374,7 +368,7 @@ public void sendRegisterRestActionsRequest(TransportService transportService) { * * @param transportService The TransportService defining the connection to OpenSearch. */ - public static void sendRegisterCustomSettingsRequest(TransportService transportService) { + public void sendRegisterCustomSettingsRequest(TransportService transportService) { logger.info("Sending Settings request to OpenSearch"); ExtensionStringResponseHandler registerCustomSettingsResponseHandler = new ExtensionStringResponseHandler(); try { @@ -539,7 +533,7 @@ private Settings getSettings() { * * @param timeout The timeout for the listener in milliseconds. A timeout of 0 means no timeout. */ - public static void startActionListener(int timeout) { + public void startActionListener(int timeout) { final ActionListener actionListener = new ActionListener(); actionListener.runActionListener(true, timeout); } @@ -569,6 +563,6 @@ public static void main(String[] args) throws IOException { // initialize the transport service extensionsRunner.initializeExtensionTransportService(extensionsRunner.getSettings()); // start listening on configured port and wait for connection from OpenSearch - ExtensionsRunner.startActionListener(0); + extensionsRunner.startActionListener(0); } } diff --git a/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleNameRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleNameRequestHandler.java index 68cbe6b7..6ed20d05 100644 --- a/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleNameRequestHandler.java +++ b/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleNameRequestHandler.java @@ -13,7 +13,6 @@ import org.opensearch.sdk.ExtensionsRunner; import org.opensearch.index.IndicesModuleRequest; import org.opensearch.discovery.InitializeExtensionsRequest; -import org.opensearch.sdk.handlers.ExtensionsInitRequestHandler; /** * This class handles the request from OpenSearch to a {@link ExtensionsRunner#startTransportService(TransportService transportService)} call. @@ -23,7 +22,7 @@ public class ExtensionsIndicesModuleNameRequestHandler { private static final Logger logger = LogManager.getLogger(ExtensionsIndicesModuleNameRequestHandler.class); /** - * Handles a request for extension name from OpenSearch. The {@link ExtensionsInitRequestHandler#handleExtensionInitRequest(InitializeExtensionsRequest)} method must have been called first to initialize the extension. + * Handles a request for extension name from OpenSearch. The {@link #handleExtensionInitRequest(InitializeExtensionsRequest)} method must have been called first to initialize the extension. * * @param indicesModuleRequest The request to handle. * @return A response acknowledging the request. diff --git a/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleRequestHandler.java index 684477ea..37b2a60d 100644 --- a/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleRequestHandler.java +++ b/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleRequestHandler.java @@ -14,7 +14,6 @@ import org.opensearch.sdk.ExtensionsRunner; import org.opensearch.transport.TransportService; import org.opensearch.discovery.InitializeExtensionsRequest; -import org.opensearch.sdk.handlers.ExtensionsInitRequestHandler; /** * This class handles the request from OpenSearch to a {@link ExtensionsRunner#startTransportService(TransportService transportService)} call. @@ -24,7 +23,7 @@ public class ExtensionsIndicesModuleRequestHandler { private static final Logger logger = LogManager.getLogger(ExtensionsIndicesModuleRequestHandler.class); /** - * Handles a request for extension point indices from OpenSearch. The {@link ExtensionsInitRequestHandler#handleExtensionInitRequest(InitializeExtensionsRequest)} method must have been called first to initialize the extension. + * Handles a request for extension point indices from OpenSearch. The {@link #handleExtensionInitRequest(InitializeExtensionsRequest)} method must have been called first to initialize the extension. * * @param indicesModuleRequest The request to handle. * @param transportService The transport service communicating with OpenSearch. From c977ac4eb2de038a5f1ac3edc5d5979405a57e50 Mon Sep 17 00:00:00 2001 From: mloufra Date: Wed, 28 Sep 2022 00:06:26 +0000 Subject: [PATCH 12/15] fix documentation problem Signed-off-by: mloufra --- src/main/java/org/opensearch/sdk/ExtensionRestRequest.java | 4 ++-- .../handlers/ExtensionsIndicesModuleNameRequestHandler.java | 3 +-- .../sdk/handlers/ExtensionsIndicesModuleRequestHandler.java | 3 +-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/ExtensionRestRequest.java b/src/main/java/org/opensearch/sdk/ExtensionRestRequest.java index 978c9f49..18a0b78a 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionRestRequest.java +++ b/src/main/java/org/opensearch/sdk/ExtensionRestRequest.java @@ -56,7 +56,7 @@ protected ExtensionRestRequest(RestExecuteOnExtensionRequest request) throws Ill /** * Object generated from input stream * @param in Input stream - * @throws IOException + * @throws IOException if there a error generating object from input stream */ public ExtensionRestRequest(StreamInput in) throws IOException { super(in); @@ -68,7 +68,7 @@ public ExtensionRestRequest(StreamInput in) throws IOException { /** * Write this object to output stream * @param out the writeable output stream - * @throws IOException + * @throws IOException if there is an error writing object to output stream */ @Override public void writeTo(StreamOutput out) throws IOException { diff --git a/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleNameRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleNameRequestHandler.java index 6ed20d05..c929ba71 100644 --- a/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleNameRequestHandler.java +++ b/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleNameRequestHandler.java @@ -12,7 +12,6 @@ import org.opensearch.extensions.ExtensionBooleanResponse; import org.opensearch.sdk.ExtensionsRunner; import org.opensearch.index.IndicesModuleRequest; -import org.opensearch.discovery.InitializeExtensionsRequest; /** * This class handles the request from OpenSearch to a {@link ExtensionsRunner#startTransportService(TransportService transportService)} call. @@ -22,7 +21,7 @@ public class ExtensionsIndicesModuleNameRequestHandler { private static final Logger logger = LogManager.getLogger(ExtensionsIndicesModuleNameRequestHandler.class); /** - * Handles a request for extension name from OpenSearch. The {@link #handleExtensionInitRequest(InitializeExtensionsRequest)} method must have been called first to initialize the extension. + * Handles a request for extension name from OpenSearch. The {@link ExtensionsInitRequestHandler} method must have been called first to initialize the extension. * * @param indicesModuleRequest The request to handle. * @return A response acknowledging the request. diff --git a/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleRequestHandler.java index 37b2a60d..a923b25f 100644 --- a/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleRequestHandler.java +++ b/src/main/java/org/opensearch/sdk/handlers/ExtensionsIndicesModuleRequestHandler.java @@ -13,7 +13,6 @@ import org.opensearch.index.IndicesModuleResponse; import org.opensearch.sdk.ExtensionsRunner; import org.opensearch.transport.TransportService; -import org.opensearch.discovery.InitializeExtensionsRequest; /** * This class handles the request from OpenSearch to a {@link ExtensionsRunner#startTransportService(TransportService transportService)} call. @@ -23,7 +22,7 @@ public class ExtensionsIndicesModuleRequestHandler { private static final Logger logger = LogManager.getLogger(ExtensionsIndicesModuleRequestHandler.class); /** - * Handles a request for extension point indices from OpenSearch. The {@link #handleExtensionInitRequest(InitializeExtensionsRequest)} method must have been called first to initialize the extension. + * Handles a request for extension point indices from OpenSearch. The {@link ExtensionsInitRequestHandler} class must have been called first to initialize the extension. * * @param indicesModuleRequest The request to handle. * @param transportService The transport service communicating with OpenSearch. From d3bd29c3efe37555463204ad8adbc1d688314bb3 Mon Sep 17 00:00:00 2001 From: mloufra Date: Wed, 28 Sep 2022 21:41:12 +0000 Subject: [PATCH 13/15] fix NullPointerException bug Signed-off-by: mloufra --- .../org/opensearch/sdk/ExtensionsRunner.java | 44 +++++++++++--- .../ExtensionsInitRequestHandler.java | 58 ++++--------------- 2 files changed, 49 insertions(+), 53 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java index d1554651..164a18e5 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java +++ b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java @@ -66,6 +66,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; @@ -87,15 +88,27 @@ public class ExtensionsRunner { private static final String NODE_NAME_SETTING = "node.name"; private String uniqueId; - private DiscoveryNode opensearchNode; + /** + * This method is call from {@link ExtensionsInitRequestHandler}. + */ + public DiscoveryNode opensearchNode; private DiscoveryExtension extensionNode; - private TransportService extensionTransportService = null; + /** + * This method is call from {@link ExtensionsInitRequestHandler}. + */ + public TransportService extensionTransportService = null; // The routes and classes which handle the REST requests private final ExtensionRestPathRegistry extensionRestPathRegistry = new ExtensionRestPathRegistry(); // Custom settings from the extension's getSettings + /** + * This method is call from {@link ExtensionsInitRequestHandler}. + */ private final List> customSettings; // Node name, host, and port - private final Settings settings; + /** + * This method is call from {@link ExtensionsInitRequestHandler}. + */ + public final Settings settings; private final TransportInterceptor NOOP_TRANSPORT_INTERCEPTOR = new TransportInterceptor() { }; private NamedWriteableRegistryAPI namedWriteableRegistryApi = new NamedWriteableRegistryAPI(); @@ -106,6 +119,15 @@ public class ExtensionsRunner { new ExtensionsIndicesModuleNameRequestHandler(); private ExtensionsRestRequestHandler extensionsRestRequestHandler = new ExtensionsRestRequestHandler(); + /* + * TODO: expose an interface for extension to register actions + * https://github.com/opensearch-project/opensearch-sdk-java/issues/119 + */ + /** + * This method is call from {@link ExtensionsInitRequestHandler}. + */ + public TransportActions transportActions = new TransportActions(new HashMap<>()); + /** * Instantiates a new update settings request handler */ @@ -159,11 +181,19 @@ private static ExtensionSettings readExtensionSettings() throws IOException { return objectMapper.readValue(file, ExtensionSettings.class); } + /** + * This method is call from {@link ExtensionsInitRequestHandler}. + * @param extensionTransportService assign value for extensionTransportService + */ void setExtensionTransportService(TransportService extensionTransportService) { this.extensionTransportService = extensionTransportService; } - private void setUniqueId(String id) { + /** + * This method is call from {@link ExtensionsInitRequestHandler}. + * @param id assign value for id + */ + public void setUniqueId(String id) { this.uniqueId = id; } @@ -171,11 +201,11 @@ String getUniqueId() { return uniqueId; } - private void setOpensearchNode(DiscoveryNode opensearchNode) { + public void setOpensearchNode(DiscoveryNode opensearchNode) { this.opensearchNode = opensearchNode; } - private void setExtensionNode(DiscoveryExtension extensionNode) { + public void setExtensionNode(DiscoveryExtension extensionNode) { this.extensionNode = extensionNode; } @@ -278,7 +308,7 @@ public void startTransportService(TransportService transportService) { false, false, InitializeExtensionsRequest::new, - (request, channel, task) -> channel.sendResponse(extensionsInitRequestHandler.handleExtensionInitRequest(request, null)) + (request, channel, task) -> channel.sendResponse(extensionsInitRequestHandler.handleExtensionInitRequest(request, this)) ); transportService.registerRequestHandler( diff --git a/src/main/java/org/opensearch/sdk/handlers/ExtensionsInitRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/ExtensionsInitRequestHandler.java index 10a1fe08..bd7f7c90 100644 --- a/src/main/java/org/opensearch/sdk/handlers/ExtensionsInitRequestHandler.java +++ b/src/main/java/org/opensearch/sdk/handlers/ExtensionsInitRequestHandler.java @@ -7,17 +7,11 @@ */ package org.opensearch.sdk.handlers; -import java.util.HashMap; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.opensearch.cluster.node.DiscoveryNode; -import org.opensearch.common.settings.Settings; import org.opensearch.discovery.InitializeExtensionsRequest; import org.opensearch.discovery.InitializeExtensionsResponse; -import org.opensearch.extensions.DiscoveryExtension; import org.opensearch.sdk.ExtensionsRunner; -import org.opensearch.sdk.TransportActions; import org.opensearch.transport.TransportService; /** @@ -27,37 +21,6 @@ public class ExtensionsInitRequestHandler { private static final Logger logger = LogManager.getLogger(ExtensionsInitRequestHandler.class); private static final String NODE_NAME_SETTING = "node.name"; - private DiscoveryNode opensearchNode; - private DiscoveryExtension extensionNode; - private Settings settings; - private String uniqueId; - private TransportService extensionTransportService = null; - - private void setUniqueId(String id) { - this.uniqueId = id; - } - - String getUniqueId() { - return uniqueId; - } - - private void setOpensearchNode(DiscoveryNode opensearchNode) { - this.opensearchNode = opensearchNode; - } - - private void setExtensionNode(DiscoveryExtension extensionNode) { - this.extensionNode = extensionNode; - } - - DiscoveryNode getOpensearchNode() { - return opensearchNode; - } - - /* - * TODO: expose an interface for extension to register actions - * https://github.com/opensearch-project/opensearch-sdk-java/issues/119 - */ - private TransportActions transportActions = new TransportActions(new HashMap<>()); /** * Handles a extension request from OpenSearch. This is the first request for the transport communication and will initialize the extension and will be a part of OpenSearch bootstrap. @@ -71,19 +34,22 @@ public InitializeExtensionsResponse handleExtensionInitRequest( ExtensionsRunner extensionsRunner ) { logger.info("Registering Extension Request received from OpenSearch"); - opensearchNode = extensionInitRequest.getSourceNode(); - setUniqueId(extensionInitRequest.getExtension().getId()); + extensionsRunner.opensearchNode = extensionInitRequest.getSourceNode(); + extensionsRunner.setUniqueId(extensionInitRequest.getExtension().getId()); // Successfully initialized. Send the response. try { - return new InitializeExtensionsResponse(settings.get(NODE_NAME_SETTING)); + return new InitializeExtensionsResponse(extensionsRunner.settings.get(NODE_NAME_SETTING)); } finally { // After sending successful response to initialization, send the REST API and Settings - setOpensearchNode(opensearchNode); - setExtensionNode(extensionInitRequest.getExtension()); - extensionTransportService.connectToNode(opensearchNode); - extensionsRunner.sendRegisterRestActionsRequest(extensionTransportService); - extensionsRunner.sendRegisterCustomSettingsRequest(extensionTransportService); - transportActions.sendRegisterTransportActionsRequest(extensionTransportService, opensearchNode); + extensionsRunner.setOpensearchNode(extensionsRunner.opensearchNode); + extensionsRunner.setExtensionNode(extensionInitRequest.getExtension()); + extensionsRunner.extensionTransportService.connectToNode(extensionsRunner.opensearchNode); + extensionsRunner.sendRegisterRestActionsRequest(extensionsRunner.extensionTransportService); + extensionsRunner.sendRegisterCustomSettingsRequest(extensionsRunner.extensionTransportService); + extensionsRunner.transportActions.sendRegisterTransportActionsRequest( + extensionsRunner.extensionTransportService, + extensionsRunner.opensearchNode + ); } } } From 6d690fb47837a21399e957c219947a25cab98d2f Mon Sep 17 00:00:00 2001 From: mloufra Date: Wed, 28 Sep 2022 21:59:46 +0000 Subject: [PATCH 14/15] change RestResponse to ExtensionRestResponse in ExtensionsRestRequestHandler Signed-off-by: mloufra --- .../opensearch/sdk/handlers/ExtensionsRestRequestHandler.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/opensearch/sdk/handlers/ExtensionsRestRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/ExtensionsRestRequestHandler.java index 447306de..5dbe0792 100644 --- a/src/main/java/org/opensearch/sdk/handlers/ExtensionsRestRequestHandler.java +++ b/src/main/java/org/opensearch/sdk/handlers/ExtensionsRestRequestHandler.java @@ -12,12 +12,12 @@ import org.opensearch.common.bytes.BytesReference; import org.opensearch.extensions.rest.RestExecuteOnExtensionRequest; import org.opensearch.extensions.rest.RestExecuteOnExtensionResponse; -import org.opensearch.rest.RestResponse; import org.opensearch.rest.RestStatus; import org.opensearch.sdk.ExtensionRestHandler; import org.opensearch.sdk.ExtensionsRunner; import org.opensearch.sdk.ExtensionRestPathRegistry; import org.opensearch.sdk.ExtensionRestRequest; +import org.opensearch.sdk.ExtensionRestResponse; /** * This class handles the request from OpenSearch to a {@link ExtensionsRunner#startTransportService(TransportService transportService)} call. @@ -50,7 +50,7 @@ public RestExecuteOnExtensionResponse handleRestExecuteOnExtensionRequest(RestEx ); // Get response from extension - RestResponse response = restHandler.handleRequest(restRequest); + ExtensionRestResponse response = restHandler.handleRequest(restRequest); logger.info("Sending extension response to OpenSearch: " + response.status()); return new RestExecuteOnExtensionResponse( response.status(), From d3388ef0b6aa941e918f281fbe1bd2a7211d4130 Mon Sep 17 00:00:00 2001 From: mloufra Date: Thu, 29 Sep 2022 21:31:59 +0000 Subject: [PATCH 15/15] change documentation for ExtensionsRunner Signed-off-by: mloufra --- ...a => ExtensionNamedWriteableRegistry.java} | 12 +++++------ .../opensearch/sdk/ExtensionRestRequest.java | 4 ++-- .../org/opensearch/sdk/ExtensionsRunner.java | 14 ++++++------- .../handlers/OpensearchRequestHandler.java | 4 ++-- .../sdk/TestNamedWriteableRegistryAPI.java | 20 ++++++++++++------- 5 files changed, 30 insertions(+), 24 deletions(-) rename src/main/java/org/opensearch/sdk/{NamedWriteableRegistryAPI.java => ExtensionNamedWriteableRegistry.java} (90%) diff --git a/src/main/java/org/opensearch/sdk/NamedWriteableRegistryAPI.java b/src/main/java/org/opensearch/sdk/ExtensionNamedWriteableRegistry.java similarity index 90% rename from src/main/java/org/opensearch/sdk/NamedWriteableRegistryAPI.java rename to src/main/java/org/opensearch/sdk/ExtensionNamedWriteableRegistry.java index fca1e655..2f000ebe 100644 --- a/src/main/java/org/opensearch/sdk/NamedWriteableRegistryAPI.java +++ b/src/main/java/org/opensearch/sdk/ExtensionNamedWriteableRegistry.java @@ -29,25 +29,25 @@ /** * API used to handle named writeable registry requests from OpenSearch */ -public class NamedWriteableRegistryAPI { - private final Logger logger = LogManager.getLogger(NamedWriteableRegistryAPI.class); +public class ExtensionNamedWriteableRegistry { + private final Logger logger = LogManager.getLogger(ExtensionNamedWriteableRegistry.class); private List namedWriteables; private final NamedWriteableRegistry namedWriteableRegistry; /** - * Constructor for NamedWriteableRegistryAPI. Creates a NamedWriteableRegistry for this extension + * Constructor for ExtensionNamedWriteableRegistry. Creates a NamedWriteableRegistry for this extension */ - public NamedWriteableRegistryAPI() { + public ExtensionNamedWriteableRegistry() { this.namedWriteables = getNamedWriteables(); this.namedWriteableRegistry = new NamedWriteableRegistry(namedWriteables); } /** - * Constructor for NamedWriteableRegistryAPI. Creates and populates a NamedWriteableRegistry with the given NamedWriteableRegistry entries for this extension + * Constructor for ExtensionNamedWriteableRegistry. Creates and populates a NamedWriteableRegistry with the given NamedWriteableRegistry entries for this extension * * @param extensionNamedWriteables List of NamedWriteableRegistry.Entry to be registered */ - public NamedWriteableRegistryAPI(List extensionNamedWriteables) { + public ExtensionNamedWriteableRegistry(List extensionNamedWriteables) { this.namedWriteables = extensionNamedWriteables; this.namedWriteableRegistry = new NamedWriteableRegistry(namedWriteables); } diff --git a/src/main/java/org/opensearch/sdk/ExtensionRestRequest.java b/src/main/java/org/opensearch/sdk/ExtensionRestRequest.java index 18a0b78a..5f170a59 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionRestRequest.java +++ b/src/main/java/org/opensearch/sdk/ExtensionRestRequest.java @@ -56,7 +56,7 @@ protected ExtensionRestRequest(RestExecuteOnExtensionRequest request) throws Ill /** * Object generated from input stream * @param in Input stream - * @throws IOException if there a error generating object from input stream + * @throws IOException if there's an error in generating object from input stream */ public ExtensionRestRequest(StreamInput in) throws IOException { super(in); @@ -68,7 +68,7 @@ public ExtensionRestRequest(StreamInput in) throws IOException { /** * Write this object to output stream * @param out the writeable output stream - * @throws IOException if there is an error writing object to output stream + * @throws IOException if there's an error in generating object from output stream */ @Override public void writeTo(StreamOutput out) throws IOException { diff --git a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java index 164a18e5..6ad1d1e9 100644 --- a/src/main/java/org/opensearch/sdk/ExtensionsRunner.java +++ b/src/main/java/org/opensearch/sdk/ExtensionsRunner.java @@ -89,29 +89,29 @@ public class ExtensionsRunner { private String uniqueId; /** - * This method is call from {@link ExtensionsInitRequestHandler}. + * This field is initialized by a call from {@link ExtensionsInitRequestHandler}. */ public DiscoveryNode opensearchNode; private DiscoveryExtension extensionNode; /** - * This method is call from {@link ExtensionsInitRequestHandler}. + * This field is initialized by a call from {@link ExtensionsInitRequestHandler}. */ public TransportService extensionTransportService = null; // The routes and classes which handle the REST requests private final ExtensionRestPathRegistry extensionRestPathRegistry = new ExtensionRestPathRegistry(); // Custom settings from the extension's getSettings /** - * This method is call from {@link ExtensionsInitRequestHandler}. + * This field is initialized by a call from {@link ExtensionsInitRequestHandler}. */ private final List> customSettings; // Node name, host, and port /** - * This method is call from {@link ExtensionsInitRequestHandler}. + * This field is initialized by a call from {@link ExtensionsInitRequestHandler}. */ public final Settings settings; private final TransportInterceptor NOOP_TRANSPORT_INTERCEPTOR = new TransportInterceptor() { }; - private NamedWriteableRegistryAPI namedWriteableRegistryApi = new NamedWriteableRegistryAPI(); + private ExtensionNamedWriteableRegistry namedWriteableRegistryApi = new ExtensionNamedWriteableRegistry(); private ExtensionsInitRequestHandler extensionsInitRequestHandler = new ExtensionsInitRequestHandler(); private OpensearchRequestHandler opensearchRequestHandler = new OpensearchRequestHandler(); private ExtensionsIndicesModuleRequestHandler extensionsIndicesModuleRequestHandler = new ExtensionsIndicesModuleRequestHandler(); @@ -124,7 +124,7 @@ public class ExtensionsRunner { * https://github.com/opensearch-project/opensearch-sdk-java/issues/119 */ /** - * This method is call from {@link ExtensionsInitRequestHandler}. + * Instantiates a new transportActions */ public TransportActions transportActions = new TransportActions(new HashMap<>()); @@ -190,7 +190,7 @@ void setExtensionTransportService(TransportService extensionTransportService) { } /** - * This method is call from {@link ExtensionsInitRequestHandler}. + * Sets the Unique ID, used in REST requests to uniquely identify this extension * @param id assign value for id */ public void setUniqueId(String id) { diff --git a/src/main/java/org/opensearch/sdk/handlers/OpensearchRequestHandler.java b/src/main/java/org/opensearch/sdk/handlers/OpensearchRequestHandler.java index 04b0ee30..49cec0b4 100644 --- a/src/main/java/org/opensearch/sdk/handlers/OpensearchRequestHandler.java +++ b/src/main/java/org/opensearch/sdk/handlers/OpensearchRequestHandler.java @@ -8,7 +8,7 @@ package org.opensearch.sdk.handlers; import org.opensearch.extensions.OpenSearchRequest; -import org.opensearch.sdk.NamedWriteableRegistryAPI; +import org.opensearch.sdk.ExtensionNamedWriteableRegistry; import org.opensearch.sdk.ExtensionsRunner; import org.opensearch.transport.TransportResponse; @@ -17,7 +17,7 @@ */ public class OpensearchRequestHandler { - private NamedWriteableRegistryAPI namedWriteableRegistryApi = new NamedWriteableRegistryAPI(); + private ExtensionNamedWriteableRegistry namedWriteableRegistryApi = new ExtensionNamedWriteableRegistry(); /** * Handles a request from OpenSearch and invokes the extension point API corresponding with the request type diff --git a/src/test/java/org/opensearch/sdk/TestNamedWriteableRegistryAPI.java b/src/test/java/org/opensearch/sdk/TestNamedWriteableRegistryAPI.java index 5a322d15..b29211bf 100644 --- a/src/test/java/org/opensearch/sdk/TestNamedWriteableRegistryAPI.java +++ b/src/test/java/org/opensearch/sdk/TestNamedWriteableRegistryAPI.java @@ -38,7 +38,7 @@ public class TestNamedWriteableRegistryAPI extends OpenSearchTestCase { private List namedWriteables; - private NamedWriteableRegistryAPI namedWriteableRegistryAPI; + private ExtensionNamedWriteableRegistry extensionNamedWriteableRegistry; private static class Example implements NamedWriteable { public static final String INVALID_NAME = "invalid_name"; @@ -77,18 +77,18 @@ public int hashCode() { @BeforeEach public void setUp() throws Exception { this.namedWriteables = Collections.singletonList(new NamedWriteableRegistry.Entry(Example.class, Example.NAME, Example::new)); - this.namedWriteableRegistryAPI = new NamedWriteableRegistryAPI(namedWriteables); + this.extensionNamedWriteableRegistry = new ExtensionNamedWriteableRegistry(namedWriteables); } @Test public void testNamedWriteableRegistryCreation() { - assert (namedWriteableRegistryAPI.getRegistry() instanceof NamedWriteableRegistry); + assert (extensionNamedWriteableRegistry.getRegistry() instanceof NamedWriteableRegistry); } @Test public void testNamedWriteableRegistryRequest() throws UnknownHostException { OpenSearchRequest request = new OpenSearchRequest(OpenSearchRequestType.REQUEST_OPENSEARCH_NAMED_WRITEABLE_REGISTRY); - NamedWriteableRegistryResponse response = namedWriteableRegistryAPI.handleNamedWriteableRegistryRequest(request); + NamedWriteableRegistryResponse response = extensionNamedWriteableRegistry.handleNamedWriteableRegistryRequest(request); // Verify that the api processes named writeable registry entries successfully within the response assertEquals(response.getRegistry().size(), 1); @@ -112,7 +112,7 @@ public void testNamedWriteableRegistryParseRequest() throws UnknownHostException InputStream input = new ByteArrayInputStream(context); StreamInput in = new InputStreamStreamInput(input); NamedWriteableRegistryParseRequest request = new NamedWriteableRegistryParseRequest(Example.class, in); - ExtensionBooleanResponse response = namedWriteableRegistryAPI.handleNamedWriteableRegistryParseRequest(request); + ExtensionBooleanResponse response = extensionNamedWriteableRegistry.handleNamedWriteableRegistryParseRequest(request); // verify that byte array deserialization was successful assertEquals(response.getStatus(), true); @@ -136,7 +136,10 @@ public void testInvalidCategoryClass() throws UnknownHostException, IOException // Category Class ExtensionRunner is not registered NamedWriteableRegistryParseRequest request = new NamedWriteableRegistryParseRequest(ExtensionsRunner.class, in); - Exception e = expectThrows(Exception.class, () -> namedWriteableRegistryAPI.handleNamedWriteableRegistryParseRequest(request)); + Exception e = expectThrows( + Exception.class, + () -> extensionNamedWriteableRegistry.handleNamedWriteableRegistryParseRequest(request) + ); assertEquals(e.getMessage(), "Unknown NamedWriteable category [" + ExtensionsRunner.class.getName() + "]"); } @@ -157,7 +160,10 @@ public void testInvalidWriteableName() throws UnknownHostException, IOException InputStream input = new ByteArrayInputStream(context); StreamInput in = new InputStreamStreamInput(input); NamedWriteableRegistryParseRequest request = new NamedWriteableRegistryParseRequest(Example.class, in); - Exception e = expectThrows(Exception.class, () -> namedWriteableRegistryAPI.handleNamedWriteableRegistryParseRequest(request)); + Exception e = expectThrows( + Exception.class, + () -> extensionNamedWriteableRegistry.handleNamedWriteableRegistryParseRequest(request) + ); assertEquals(e.getMessage(), "Unknown NamedWriteable [" + Example.class.getName() + "][" + Example.INVALID_NAME + "]"); } }