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
48 changes: 48 additions & 0 deletions src/main/java/org/opensearch/sdk/BaseExtension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.common.xcontent.NamedXContentRegistry;
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 NamedXContentRegistry xContentRegistry;
protected Environment environment;

/**
* Empty constructor to fulfill abstract class requirements
*/
public BaseExtension() {
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved

}

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

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

import org.opensearch.client.Client;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.io.stream.NamedWriteableRegistry;
import org.opensearch.common.xcontent.NamedXContentRegistry;
import org.opensearch.env.Environment;
import org.opensearch.env.NodeEnvironment;
import org.opensearch.repositories.RepositoriesService;
import org.opensearch.script.ScriptService;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.watcher.ResourceWatcherService;

import org.opensearch.common.settings.Setting;

Expand Down Expand Up @@ -48,6 +62,27 @@ default List<Setting<?>> getSettings() {
return Collections.emptyList();
}

/**
* Returns components added by this plugin.
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
*
* Any components returned that implement {@link LifecycleComponent} will have their lifecycle managed.
* Note: To aid in the migration away from guice, all objects returned as components will be bound in guice
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
* to themselves.
*
* @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 xContentRegistry the registry for extensible xContent parsing
* @param environment the environment for path and setting configurations
*/
public Collection<Object> createComponents(
SDKClient client,
ClusterService clusterService,
ThreadPool threadPool,
NamedXContentRegistry xContentRegistry,
Environment environment
);

/**
* Helper method to read extension settings from a YAML file.
*
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/org/opensearch/sdk/ExtensionsRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,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
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,34 @@ public void testExtensionRestHandlers() {
assertEquals(2, routes.size());
}

/**
@Test
public void testCreateComponents() throws IOException {
Settings settings = Settings.builder().put("node.name", Property.NodeScope).build();
ThreadPool threadPool = new ThreadPool(settings, new AtomicReference<>(), Collections.emptyList().toArray(new ExecutorBuilder[0]));
Client client = new NodeClient(settings, threadPool);
ClusterService clusterService = new ClusterService(settings, new ClusterSettings(settings, new HashSet<Setting<?>>()), threadPool);
ResourceWatcherService resourceWatcherService = new ResourceWatcherService(settings, threadPool);
ScriptService scriptService = new ScriptService(settings, new HashMap<String, ScriptEngine>(), new HashMap<String, ScriptContext<?>>());
NamedXContentRegistry xContentRegistry = new NamedXContentRegistry(new ArrayList<NamedXContentRegistry.Entry>());
Environment environment = new Environment(settings, Paths.get(""));
NodeEnvironment nodeEnvironment = new NodeEnvironment(settings, environment);
NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistryAPI().getRegistry();
IndexNameExpressionResolver indexNameExpressionResolver = new IndexNameExpressionResolver(threadPool.getThreadContext());
final SetOnce<RepositoriesService> repositoriesServiceReference = new SetOnce<>();
Supplier<RepositoriesService> repositoriesServiceSupplier = repositoriesServiceReference::get;
assertEquals(threadPool, extension.getThreadPool());
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(client, extension.getClient());
assertEquals(clusterService, extension.getClusterService());
assertEquals(resourceWatcherService, extension.getResourceWatcherService());
assertEquals(scriptService, extension.getScriptService());
assertEquals(xContentRegistry, extension.getNamedXContentRegistry());
assertEquals(environment, extension.getEnvironment());
assertEquals(nodeEnvironment, extension.getNodeEnvironment());
assertEquals(namedWriteableRegistry, extension.getNamedWriteableRegistry());
assertEquals(indexNameExpressionResolver, extension.getIndexNameExpressionResolver());
assertEquals(repositoriesServiceSupplier, extension.getRepositoriesServiceSupplier());
}
*/

}