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
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ publishing {

repositories {
mavenLocal()
// Remove the commented code below once TransportService is published to maven
//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 @@ -69,6 +68,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
57 changes: 57 additions & 0 deletions src/main/java/org/opensearch/sdk/BaseExtension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.threadpool.ThreadPool;

/**
* An abstract class that provides sample methods required by extensions
*/
public abstract class BaseExtension implements Extension {
/**
* A client to make requests to the system
*/
protected SDKClient client;
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved

/**
* A service to allow watching and updating cluster state
*/
protected ClusterService clusterService;

/**
* A service to allow retrieving an executor to run an async action
*/
protected ThreadPool threadPool;
owaiskazi19 marked this conversation as resolved.
Show resolved Hide resolved

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

}

/**
* 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
* @return A collection of objects
*/
public Collection<Object> createComponents(SDKClient client, ClusterService clusterService, ThreadPool threadPool) {
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
this.client = client;
this.clusterService = clusterService;
this.threadPool = threadPool;

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

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

import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionResponse;
import org.opensearch.action.support.TransportAction;
Expand Down Expand Up @@ -52,6 +56,16 @@ 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
* @return A collection of objects
*/
public Collection<Object> createComponents(SDKClient client, ClusterService clusterService, ThreadPool threadPool);

/**
* Gets an optional list of custom {@link TransportAction} for the extension to register with OpenSearch.
*
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/opensearch/sdk/ExtensionsRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public class ExtensionsRunner {
new ExtensionsIndicesModuleNameRequestHandler();
private ExtensionsRestRequestHandler extensionsRestRequestHandler = new ExtensionsRestRequestHandler(extensionRestPathRegistry);
private NettyTransport nettyTransport = new NettyTransport();
private SDKClient client = new SDKClient();

/*
* TODO: expose an interface for extension to register actions
Expand Down Expand Up @@ -132,7 +133,10 @@ protected ExtensionsRunner(Extension extension) throws IOException {
// save custom transport actions
this.transportActions = new TransportActions(extension.getActions());
// initialize the transport service
nettyTransport.initializeExtensionTransportService(this.getSettings(), this);
ThreadPool threadPool = new ThreadPool(this.getSettings());
nettyTransport.initializeExtensionTransportService(this.getSettings(), threadPool, this);
// create components
extension.createComponents(client, null, threadPool);
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/opensearch/sdk/NettyTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,15 @@ public Netty4Transport getNetty4Transport(Settings settings, ThreadPool threadPo
* Initializes the TransportService object for this extension. This object will control communication between the extension and OpenSearch.
*
* @param settings The transport settings to configure.
* @param threadPool The thread pool to use to start transport service.
* @param extensionsRunner method to call
* @return The initialized TransportService object.
*/
public TransportService initializeExtensionTransportService(Settings settings, ExtensionsRunner extensionsRunner) {

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

Netty4Transport transport = getNetty4Transport(settings, threadPool);

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 @@ -41,6 +42,7 @@ public class HelloWorldExtension implements Extension {
* Instantiate this extension, initializing the connection settings and REST actions.
*/
public HelloWorldExtension() {
super();
try {
this.settings = initializeSettings();
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ExtensionsRunnerForTest extends ExtensionsRunner {
* @throws IOException if the runner failed to read settings or API.
*/
public ExtensionsRunnerForTest() throws IOException {
super(new Extension() {
super(new BaseExtension() {
@Override
public ExtensionSettings getExtensionSettings() {
return new ExtensionSettings("sample-extension", "127.0.0.1", "4532");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ private void startTransportandClient(Settings settings, Thread client) throws IO
// retrieve transport service
ExtensionsRunner extensionsRunner = new ExtensionsRunnerForTest();
// start transport service
TransportService transportService = nettyTransport.initializeExtensionTransportService(settings, extensionsRunner);
ThreadPool threadPool = new ThreadPool(settings);
TransportService transportService = nettyTransport.initializeExtensionTransportService(settings, threadPool, extensionsRunner);

assertEquals(Lifecycle.State.STARTED, transportService.lifecycleState());

Expand Down