Skip to content

Commit

Permalink
Added createComponent to Extension interface (#146)
Browse files Browse the repository at this point in the history
* Added createComponent to Extension interface and created BaseExtension abstract class

Signed-off-by: Ryan Bogan <[email protected]>

* Fixed minor error

Signed-off-by: Ryan Bogan <[email protected]>

* Changed create component arguments

Signed-off-by: Ryan Bogan <[email protected]>

* Addressed PR Comments

Signed-off-by: Ryan Bogan <[email protected]>

* Fixed minor errors

Signed-off-by: Ryan Bogan <[email protected]>

* Return consumed params and content from extensions (#169)

* Remove duplicate copies of registries in handlers

Signed-off-by: Daniel Widdis <[email protected]>

* Move ExtensionRestResponse to OpenSearch

Signed-off-by: Daniel Widdis <[email protected]>

* Add a POST request to parse content

Signed-off-by: Daniel Widdis <[email protected]>

* Add a DELETE request corresponding to the POST

Signed-off-by: Daniel Widdis <[email protected]>

* Add consumed params and content to Extension Responses

Signed-off-by: Daniel Widdis <[email protected]>

* Update tests and OpenAPI spec

Signed-off-by: Daniel Widdis <[email protected]>

Signed-off-by: Daniel Widdis <[email protected]>

* Fixed minor errors

Signed-off-by: Ryan Bogan <[email protected]>

* Addressed PR Comments

Signed-off-by: Ryan Bogan <[email protected]>

Signed-off-by: Ryan Bogan <[email protected]>
Signed-off-by: Daniel Widdis <[email protected]>
Co-authored-by: Daniel Widdis <[email protected]>
  • Loading branch information
ryanbogan and dbwiddis authored Oct 12, 2022
1 parent 28ad2fb commit fb90df4
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 9 deletions.
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;

/**
* 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;

/**
* 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) {
this.client = client;
this.clusterService = clusterService;
this.threadPool = threadPool;

return Collections.emptyList();
}
}
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();
} 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

0 comments on commit fb90df4

Please sign in to comment.