Skip to content

Commit

Permalink
Revert "Add default Opensearch host and port if init comes from local…
Browse files Browse the repository at this point in the history
…host (#763)" (#783)

This reverts commit aa8886d.

Signed-off-by: Joshua Palis <[email protected]>
  • Loading branch information
joshpalis authored May 24, 2023
1 parent 579c302 commit 3518f57
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 95 deletions.
10 changes: 0 additions & 10 deletions src/main/java/org/opensearch/sdk/ExtensionsRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,6 @@ public class ExtensionsRunner {
* The key for the extension runner's node name in its settings.
*/
public static final String NODE_NAME_SETTING = "node.name";
/**
* OpenSearch default host name.
*/
public static final String OPENSEARCH_HOST_SETTING = "opensearch.host";
/**
* OpenSearch default host port.
*/
public static final String OPENSEARCH_PORT_SETTING = "opensearch.port";

// The extension being run
private final Extension extension;
Expand Down Expand Up @@ -189,8 +181,6 @@ protected ExtensionsRunner(Extension extension) throws IOException {
ExtensionSettings extensionSettings = extension.getExtensionSettings();
Settings.Builder settingsBuilder = Settings.builder()
.put(NODE_NAME_SETTING, extensionSettings.getExtensionName())
.put(OPENSEARCH_HOST_SETTING, extensionSettings.getOpensearchAddress())
.put(OPENSEARCH_PORT_SETTING, extensionSettings.getOpensearchPort())
.put(TransportSettings.BIND_HOST.getKey(), extensionSettings.getHostAddress())
.put(TransportSettings.PORT.getKey(), extensionSettings.getHostPort());
boolean sslEnabled = extensionSettings.getSecuritySettings().containsKey(SSL_TRANSPORT_ENABLED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,14 @@
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.common.transport.TransportAddress;
import org.opensearch.discovery.InitializeExtensionRequest;
import org.opensearch.discovery.InitializeExtensionResponse;
import org.opensearch.extensions.DiscoveryExtensionNode;
import org.opensearch.sdk.ExtensionsRunner;
import org.opensearch.sdk.SDKTransportService;
import org.opensearch.transport.TransportService;

import java.net.InetAddress;
import java.net.UnknownHostException;

import static org.opensearch.sdk.ExtensionsRunner.NODE_NAME_SETTING;
import static org.opensearch.sdk.ExtensionsRunner.OPENSEARCH_HOST_SETTING;
import static org.opensearch.sdk.ExtensionsRunner.OPENSEARCH_PORT_SETTING;

/**
* This class handles the request from OpenSearch to a {@link ExtensionsRunner#startTransportService(TransportService transportService)} call.
Expand All @@ -54,33 +46,14 @@ public ExtensionsInitRequestHandler(ExtensionsRunner extensionsRunner) {
* @return A response to OpenSearch validating that this is an extension.
*/
public InitializeExtensionResponse handleExtensionInitRequest(InitializeExtensionRequest extensionInitRequest) {
DiscoveryNode sourceNode = extensionInitRequest.getSourceNode();
DiscoveryExtensionNode extensionNode = extensionInitRequest.getExtension();
logger.info("Registering Extension Request received from OpenSearch node {}", sourceNode);
// In some cases the source node has a localhost IP address which is unreachable from extension.
String sourceHost = sourceNode.getAddress().address().getHostString();
if ("127.0.0.1".equals(sourceHost)) {
// pull from ExtensionSettings instead
sourceHost = extensionsRunner.getSettings().get(OPENSEARCH_HOST_SETTING);
try {
int sourcePort = Integer.valueOf(extensionsRunner.getSettings().get(OPENSEARCH_PORT_SETTING));
sourceNode = new DiscoveryNode(
sourceNode.getName(),
new TransportAddress(InetAddress.getByName(sourceHost), sourcePort),
sourceNode.getVersion()
);
} catch (NumberFormatException | UnknownHostException e) {
// If configured defaults don't work, just keep original source node
}
}
extensionsRunner.opensearchNode = sourceNode;
extensionsRunner.getThreadPool().getThreadContext().putHeader("extension_unique_id", extensionNode.getId());
extensionsRunner.setUniqueId(extensionNode.getId());
// TODO: Remove above lines setting extensionRunner node and ID in favor of the below when refactoring
// per https://github.com/opensearch-project/opensearch-sdk-java/issues/585
logger.info("Registering Extension Request received from OpenSearch");
extensionsRunner.opensearchNode = extensionInitRequest.getSourceNode();
extensionsRunner.getThreadPool().getThreadContext().putHeader("extension_unique_id", extensionInitRequest.getExtension().getId());
extensionsRunner.setUniqueId(extensionInitRequest.getExtension().getId());
// TODO: Remove above two lines in favor of the below when refactoring
SDKTransportService sdkTransportService = extensionsRunner.getSdkTransportService();
sdkTransportService.setOpensearchNode(sourceNode);
sdkTransportService.setUniqueId(extensionNode.getId());
sdkTransportService.setOpensearchNode(extensionInitRequest.getSourceNode());
sdkTransportService.setUniqueId(extensionInitRequest.getExtension().getId());
// Successfully initialized. Send the response.
try {
return new InitializeExtensionResponse(
Expand All @@ -89,7 +62,7 @@ public InitializeExtensionResponse handleExtensionInitRequest(InitializeExtensio
);
} finally {
// After sending successful response to initialization, send the REST API and Settings
extensionsRunner.setOpensearchNode(sourceNode);
extensionsRunner.setOpensearchNode(extensionInitRequest.getSourceNode());
extensionsRunner.setExtensionNode(extensionInitRequest.getExtension());
extensionsRunner.getSdkClient().updateOpenSearchNodeSettings(extensionInitRequest.getSourceNode().getAddress());

Expand Down
6 changes: 1 addition & 5 deletions src/main/resources/sample/helloworld-settings.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# This name is only used as a response string during init and is purely cosmetic
extensionName: hello-world
# The listening port is bound to the interface with this IP. Set to 0.0.0.0 to listen on all interfaces
hostAddress: 0.0.0.0
# The port the Extension listens on
hostAddress: 127.0.0.1
hostPort: 4532
# Default opensearch address and port to connect to if the values received during handshake are wrong
opensearchAddress: 127.0.0.1
opensearchPort: 9200
#ssl.transport.enabled: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ExtensionsRunnerForTest extends ExtensionsRunner {
* @throws IOException if the runner failed to read settings or API.
*/
public ExtensionsRunnerForTest() throws IOException {
super(new BaseExtension(new ExtensionSettings(NODE_NAME, NODE_HOST, NODE_PORT, "10.10.10.10", "9200")) {
super(new BaseExtension(new ExtensionSettings(NODE_NAME, NODE_HOST, NODE_PORT, "127.0.0.1", "9200")) {
});
}

Expand Down
44 changes: 1 addition & 43 deletions src/test/java/org/opensearch/sdk/TestExtensionsRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void testStartTransportService() {
public void testHandleExtensionInitRequest() throws UnknownHostException {
DiscoveryNode sourceNode = new DiscoveryNode(
"test_node",
new TransportAddress(InetAddress.getByName("10.10.10.10"), 9876),
new TransportAddress(InetAddress.getByName("localhost"), 9876),
emptyMap(),
emptySet(),
Version.CURRENT
Expand Down Expand Up @@ -139,48 +139,6 @@ public void testHandleExtensionInitRequest() throws UnknownHostException {
assertEquals(sourceNode, extensionsRunner.getOpensearchNode());
}

@Test
public void testHandleExtensionInitRequestFromLocalHost() throws UnknownHostException {
DiscoveryNode sourceNode = new DiscoveryNode(
"test_node",
new TransportAddress(InetAddress.getByName("127.0.0.1"), 9876),
emptyMap(),
emptySet(),
Version.CURRENT
);
DiscoveryExtensionNode extension = new DiscoveryExtensionNode(
EXTENSION_NAME,
"opensearch-sdk-1",
sourceNode.getAddress(),
new HashMap<String, String>(),
Version.fromString("3.0.0"),
Version.fromString("3.0.0"),
new ArrayList<ExtensionDependency>()
);

// Set mocked transport service
extensionsRunner.setExtensionTransportService(this.transportService);
doNothing().when(this.transportService).connectToNodeAsExtension(sourceNode, "opensearch-sdk-1");

InitializeExtensionRequest extensionInitRequest = new InitializeExtensionRequest(sourceNode, extension);

InitializeExtensionResponse response = extensionsInitRequestHandler.handleExtensionInitRequest(extensionInitRequest);
// Test if name and unique ID are set
assertEquals(EXTENSION_NAME, response.getName());
assertEquals("opensearch-sdk-1", extensionsRunner.getUniqueId());
// Test that the source node is NOT set after handleExtensionInitRequest() is called during OpenSearch bootstrap
assertNotEquals(sourceNode, extensionsRunner.getOpensearchNode());
// It should keep the original opensearch node
assertEquals(
extensionsRunner.getSettings().get(ExtensionsRunner.OPENSEARCH_HOST_SETTING),
extensionsRunner.getOpensearchNode().getAddress().address().getHostName()
);
assertEquals(
extensionsRunner.getSettings().get(ExtensionsRunner.OPENSEARCH_PORT_SETTING),
Integer.toString(extensionsRunner.getOpensearchNode().getAddress().address().getPort())
);
}

@Test
public void testHandleExtensionRestRequest() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void tearDown() throws Exception {
public void testExtensionSettings() {
// This effectively tests the Extension interface helper method
ExtensionSettings extensionSettings = extension.getExtensionSettings();
ExtensionSettings expected = new ExtensionSettings("hello-world", "0.0.0.0", "4532", "127.0.0.1", "9200");
ExtensionSettings expected = new ExtensionSettings("hello-world", "127.0.0.1", "4532", "127.0.0.1", "9200");
assertEquals(expected.getExtensionName(), extensionSettings.getExtensionName());
assertEquals(expected.getHostAddress(), extensionSettings.getHostAddress());
assertEquals(expected.getHostPort(), extensionSettings.getHostPort());
Expand Down

0 comments on commit 3518f57

Please sign in to comment.