From 86d0d67036f29f4c7f2f37773e2cdaeb37b9ebf3 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 11 Jul 2016 23:16:23 -0700 Subject: [PATCH 1/2] Plugins: Add some basic services to createComponents This adds the first few basic services needed for any plugin to create its own components that interact with the rest of the system. --- .../java/org/elasticsearch/node/Node.java | 4 +- .../org/elasticsearch/plugins/Plugin.java | 20 ++++++--- .../elasticsearch/plugins/PluginsService.java | 45 +++++++++---------- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/node/Node.java b/core/src/main/java/org/elasticsearch/node/Node.java index 454a49291d8..a528de6d082 100644 --- a/core/src/main/java/org/elasticsearch/node/Node.java +++ b/core/src/main/java/org/elasticsearch/node/Node.java @@ -303,7 +303,9 @@ public class Node implements Closeable { resourcesToClose.add(bigArrays); modules.add(settingsModule); client = new NodeClient(settings, threadPool); - Collection pluginComponents = pluginsService.createComponenents(); + Collection pluginComponents = pluginsService.filterPlugins(Plugin.class).stream() + .flatMap(p -> p.createComponents(client, clusterService, threadPool).stream()) + .collect(Collectors.toList()); modules.add(b -> { b.bind(PluginsService.class).toInstance(pluginsService); b.bind(Client.class).toInstance(client); diff --git a/core/src/main/java/org/elasticsearch/plugins/Plugin.java b/core/src/main/java/org/elasticsearch/plugins/Plugin.java index 3c9cf675a63..57ccd876eef 100644 --- a/core/src/main/java/org/elasticsearch/plugins/Plugin.java +++ b/core/src/main/java/org/elasticsearch/plugins/Plugin.java @@ -19,8 +19,13 @@ package org.elasticsearch.plugins; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + import org.elasticsearch.action.ActionModule; -import org.elasticsearch.common.component.AbstractComponent; +import org.elasticsearch.client.Client; +import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.component.LifecycleComponent; import org.elasticsearch.common.inject.Module; import org.elasticsearch.common.settings.Setting; @@ -30,10 +35,7 @@ import org.elasticsearch.index.IndexModule; import org.elasticsearch.indices.analysis.AnalysisModule; import org.elasticsearch.script.ScriptModule; import org.elasticsearch.threadpool.ExecutorBuilder; - -import java.util.Collection; -import java.util.Collections; -import java.util.List; +import org.elasticsearch.threadpool.ThreadPool; /** * An extension point allowing to plug in custom functionality. @@ -65,13 +67,17 @@ public abstract class Plugin { } /** - * Returns components maintained by this plugin. + * Returns components added by this plugin. * * 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 * 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 */ - public Collection createComponents() { + public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool) { return Collections.emptyList(); } diff --git a/core/src/main/java/org/elasticsearch/plugins/PluginsService.java b/core/src/main/java/org/elasticsearch/plugins/PluginsService.java index a03b7c30b7f..ecb51da1aae 100644 --- a/core/src/main/java/org/elasticsearch/plugins/PluginsService.java +++ b/core/src/main/java/org/elasticsearch/plugins/PluginsService.java @@ -19,6 +19,26 @@ package org.elasticsearch.plugins; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; + import org.apache.lucene.analysis.util.CharFilterFactory; import org.apache.lucene.analysis.util.TokenFilterFactory; import org.apache.lucene.analysis.util.TokenizerFactory; @@ -42,26 +62,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.IndexModule; import org.elasticsearch.threadpool.ExecutorBuilder; -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.net.URL; -import java.net.URLClassLoader; -import java.nio.file.DirectoryStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.function.Function; -import java.util.stream.Collectors; - import static org.elasticsearch.common.io.FileSystemUtils.isAccessibleDirectory; /** @@ -293,11 +293,6 @@ public class PluginsService extends AbstractComponent { return services; } - /** Gets components from each plugin. This method should be called exactly once. */ - public Collection createComponenents() { - return plugins.stream().flatMap(p -> p.v2().createComponents().stream()).collect(Collectors.toList()); - } - public void onIndexModule(IndexModule indexModule) { for (Tuple plugin : plugins) { plugin.v2().onIndexModule(indexModule); From 81429aed8c2bfb3145e9fcbcca5ddbb7bce2a22d Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 11 Jul 2016 23:33:49 -0700 Subject: [PATCH 2/2] Remove createComponents from transport client --- .../org/elasticsearch/client/transport/TransportClient.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/client/transport/TransportClient.java b/core/src/main/java/org/elasticsearch/client/transport/TransportClient.java index 9cdbef39cc0..ee3884cac4e 100644 --- a/core/src/main/java/org/elasticsearch/client/transport/TransportClient.java +++ b/core/src/main/java/org/elasticsearch/client/transport/TransportClient.java @@ -157,13 +157,11 @@ public class TransportClient extends AbstractClient { resourcesToClose.add(circuitBreakerService); BigArrays bigArrays = new BigArrays(settings, circuitBreakerService); resourcesToClose.add(bigArrays); - Collection pluginComponents = pluginsService.createComponenents(); modules.add(settingsModule); modules.add((b -> { b.bind(BigArrays.class).toInstance(bigArrays); b.bind(PluginsService.class).toInstance(pluginsService); b.bind(CircuitBreakerService.class).toInstance(circuitBreakerService); - pluginComponents.stream().forEach(p -> b.bind((Class)p.getClass()).toInstance(p)); })); Injector injector = modules.createInjector(); @@ -173,9 +171,7 @@ public class TransportClient extends AbstractClient { final TransportProxyClient proxy = new TransportProxyClient(settings, transportService, nodesService, actionModule.getActions().values().stream().map(x -> x.getAction()).collect(Collectors.toList())); - List pluginLifecycleComponents = pluginComponents.stream() - .filter(p -> p instanceof LifecycleComponent) - .map(p -> (LifecycleComponent)p).collect(Collectors.toList()); + List pluginLifecycleComponents = new ArrayList<>(); pluginLifecycleComponents.addAll(pluginsService.getGuiceServiceClasses().stream() .map(injector::getInstance).collect(Collectors.toList())); resourcesToClose.addAll(pluginLifecycleComponents);