Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added createComponent to Extension interface #146

Merged
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ publishing {
repositories {
mavenLocal()
// Remove the commented code below once TransportService is published to maven
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
//maven { url "https://aws.oss.sonatype.org/content/repositories/snapshots" }
maven { url "https://aws.oss.sonatype.org/content/repositories/snapshots" }
maven { url "https://d1nvenhzbhpy0q.cloudfront.net/snapshots/lucene/"}
mavenCentral()
}
Expand All @@ -65,6 +65,7 @@ dependencies {
implementation 'org.opensearch.client:opensearch-rest-client:2.0.0'
implementation 'org.opensearch.client:opensearch-java:2.0.0'
implementation "io.netty:netty-all:4.1.73.Final"
implementation "org.apache.lucene:lucene-core:9.4.0-snapshot-ddf0d0a"
testCompileOnly ("junit:junit:4.13.2") {
exclude module : 'hamcrest'
exclude module : 'hamcrest-core'
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/org/opensearch/sdk/BaseExtension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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;

import java.util.Collection;
import java.util.Collections;

import org.opensearch.cluster.service.ClusterService;
import org.opensearch.env.Environment;
import org.opensearch.threadpool.ThreadPool;

public abstract class BaseExtension implements Extension {
protected SDKClient client;
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
protected ClusterService clusterService;
protected ThreadPool threadPool;
owaiskazi19 marked this conversation as resolved.
Show resolved Hide resolved
protected Environment environment;

/**
* Empty constructor to fulfill abstract class requirements
*/
protected BaseExtension() {

}

public Collection<Object> createComponents(
SDKClient client,
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
ClusterService clusterService,
ThreadPool threadPool,
Environment environment
) {
this.client = client;
this.clusterService = clusterService;
this.threadPool = threadPool;
this.environment = environment;

return Collections.emptyList();
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
}
}
21 changes: 21 additions & 0 deletions src/main/java/org/opensearch/sdk/Extension.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.Collection;
import java.util.List;

import org.opensearch.client.OpenSearchClient;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.env.Environment;
import org.opensearch.threadpool.ThreadPool;

import org.opensearch.common.settings.Setting;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -48,6 +54,21 @@ default List<Setting<?>> getSettings() {
return Collections.emptyList();
}

/**
* Returns components added by this extension.
*
* @param client A client to make requests to the system
* @param clusterService A service to allow watching and updating cluster state
* @param threadPool A service to allow retrieving an executor to run an async action
* @param environment the environment for path and setting configurations
*/
public Collection<Object> createComponents(
OpenSearchClient client,
ClusterService clusterService,
ThreadPool threadPool,
Environment environment
);

/**
* Helper method to read extension settings from a YAML file.
*
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/org/opensearch/sdk/ExtensionsRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ private ExtensionsRunner(Extension extension) throws IOException {
// save custom settings
this.customSettings = extension.getSettings();
// initialize the transport service
this.initializeExtensionTransportService(this.getSettings());
ThreadPool threadPool = new ThreadPool(this.getSettings());
this.initializeExtensionTransportService(this.getSettings(), threadPool);
// Create components
extension.createComponents(new SDKClient(), null, threadPool, null);
// start listening on configured port and wait for connection from OpenSearch
this.startActionListener(0);
}
Expand Down Expand Up @@ -330,9 +333,7 @@ public Netty4Transport getNetty4Transport(Settings settings, ThreadPool threadPo
* @param settings The transport settings to configure.
* @return The initialized TransportService object.
*/
public TransportService initializeExtensionTransportService(Settings settings) {

ThreadPool threadPool = new ThreadPool(settings);
public TransportService initializeExtensionTransportService(Settings settings, ThreadPool threadPool) {

Netty4Transport transport = getNetty4Transport(settings, threadPool);

Expand Down Expand Up @@ -554,10 +555,7 @@ public void sendActionListenerOnFailureRequest(TransportService transportService
transportService.sendRequest(
opensearchNode,
ExtensionsOrchestrator.REQUEST_EXTENSION_ACTION_LISTENER_ON_FAILURE,
new ExtensionRequest(
ExtensionsOrchestrator.RequestType.REQUEST_EXTENSION_ACTION_LISTENER_ON_FAILURE,
failureException.toString()
),
new ExtensionActionListenerOnFailureRequest(failureException.toString()),
listenerHandler
);
} catch (Exception e) {
Expand Down Expand Up @@ -662,7 +660,8 @@ public static void main(String[] args) throws IOException {
ExtensionsRunner extensionsRunner = new ExtensionsRunner();

// initialize the transport service
extensionsRunner.initializeExtensionTransportService(extensionsRunner.getSettings());
ThreadPool threadPool = new ThreadPool(extensionsRunner.getSettings());
extensionsRunner.initializeExtensionTransportService(extensionsRunner.getSettings(), threadPool);
// start listening on configured port and wait for connection from OpenSearch
extensionsRunner.startActionListener(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.util.List;

import org.opensearch.sdk.BaseExtension;
import org.opensearch.sdk.Extension;
import org.opensearch.sdk.ExtensionRestHandler;
import org.opensearch.sdk.ExtensionSettings;
Expand All @@ -25,7 +26,7 @@
* <p>
* To execute, pass an instatiated object of this class to {@link ExtensionsRunner#run(Extension)}.
*/
public class HelloWorldExtension implements Extension {
public class HelloWorldExtension extends BaseExtension {

/**
* Optional classpath-relative path to a yml file containing extension settings.
Expand All @@ -40,7 +41,8 @@ public class HelloWorldExtension implements Extension {
/**
* Instantiate this extension, initializing the connection settings and REST actions.
*/
public HelloWorldExtension() {
public HelloWorldExtension() throws IOException {
super();
try {
this.settings = initializeSettings();
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
} catch (IOException e) {
Expand Down