Skip to content

Commit d579f89

Browse files
authored
Remove guice modules from plugins (#43555)
Guice modules provide a way to supply injected parameters to other injected constructors. The last remiaing use in plugins is to provide constructor arguments to transport actions. This commit removes the ability for plugins to provide guice modules. Any transport action parameters should be concretely typed and returned from createComponents, which are still injected. While this does not remove guice completely, it removes it from all remaining plugin apis.
1 parent 1a77301 commit d579f89

File tree

28 files changed

+170
-196
lines changed

28 files changed

+170
-196
lines changed

modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessPlugin.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@
2323
import org.apache.lucene.util.SetOnce;
2424
import org.elasticsearch.action.ActionRequest;
2525
import org.elasticsearch.action.ActionResponse;
26+
import org.elasticsearch.client.Client;
2627
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
2728
import org.elasticsearch.cluster.node.DiscoveryNodes;
28-
import org.elasticsearch.common.inject.Module;
29+
import org.elasticsearch.cluster.service.ClusterService;
30+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
2931
import org.elasticsearch.common.settings.ClusterSettings;
3032
import org.elasticsearch.common.settings.IndexScopedSettings;
3133
import org.elasticsearch.common.settings.Setting;
3234
import org.elasticsearch.common.settings.Settings;
3335
import org.elasticsearch.common.settings.SettingsFilter;
36+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
37+
import org.elasticsearch.env.Environment;
38+
import org.elasticsearch.env.NodeEnvironment;
3439
import org.elasticsearch.painless.action.PainlessContextAction;
3540
import org.elasticsearch.painless.action.PainlessExecuteAction;
3641
import org.elasticsearch.painless.spi.PainlessExtension;
@@ -45,7 +50,10 @@
4550
import org.elasticsearch.script.ScoreScript;
4651
import org.elasticsearch.script.ScriptContext;
4752
import org.elasticsearch.script.ScriptEngine;
53+
import org.elasticsearch.script.ScriptService;
4854
import org.elasticsearch.search.aggregations.pipeline.MovingFunctionScript;
55+
import org.elasticsearch.threadpool.ThreadPool;
56+
import org.elasticsearch.watcher.ResourceWatcherService;
4957

5058
import java.util.ArrayList;
5159
import java.util.Arrays;
@@ -103,8 +111,13 @@ public ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext<
103111
}
104112

105113
@Override
106-
public Collection<Module> createGuiceModules() {
107-
return Collections.singleton(b -> b.bind(PainlessScriptEngine.class).toInstance(painlessScriptEngine.get()));
114+
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool,
115+
ResourceWatcherService resourceWatcherService, ScriptService scriptService,
116+
NamedXContentRegistry xContentRegistry, Environment environment,
117+
NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry) {
118+
// this is a hack to bind the painless script engine in guice (all components are added to guice), so that
119+
// the painless context api. this is a temporary measure until transport actions do no require guice
120+
return Collections.singletonList(painlessScriptEngine.get());
108121
}
109122

110123
@Override

server/src/main/java/org/elasticsearch/node/Node.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
import org.elasticsearch.common.component.LifecycleComponent;
6666
import org.elasticsearch.common.inject.Injector;
6767
import org.elasticsearch.common.inject.Key;
68-
import org.elasticsearch.common.inject.Module;
6968
import org.elasticsearch.common.inject.ModulesBuilder;
7069
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
7170
import org.elasticsearch.common.lease.Releasables;
@@ -156,7 +155,6 @@
156155
import org.elasticsearch.watcher.ResourceWatcherService;
157156

158157
import javax.net.ssl.SNIHostName;
159-
160158
import java.io.BufferedWriter;
161159
import java.io.Closeable;
162160
import java.io.IOException;
@@ -374,10 +372,6 @@ protected Node(
374372
final UsageService usageService = new UsageService();
375373

376374
ModulesBuilder modules = new ModulesBuilder();
377-
// plugin modules must be added here, before others or we can get crazy injection errors...
378-
for (Module pluginModule : pluginsService.createGuiceModules()) {
379-
modules.add(pluginModule);
380-
}
381375
final MonitorService monitorService = new MonitorService(settings, nodeEnvironment, threadPool, clusterInfoService);
382376
ClusterModule clusterModule = new ClusterModule(settings, clusterService, clusterPlugins, clusterInfoService);
383377
modules.add(clusterModule);
@@ -595,8 +589,6 @@ protected Node(
595589
List<LifecycleComponent> pluginLifecycleComponents = pluginComponents.stream()
596590
.filter(p -> p instanceof LifecycleComponent)
597591
.map(p -> (LifecycleComponent) p).collect(Collectors.toList());
598-
pluginLifecycleComponents.addAll(pluginsService.getGuiceServiceClasses().stream()
599-
.map(injector::getInstance).collect(Collectors.toList()));
600592
resourcesToClose.addAll(pluginLifecycleComponents);
601593
this.pluginLifecycleComponents = Collections.unmodifiableList(pluginLifecycleComponents);
602594
client.initialize(injector.getInstance(new Key<Map<Action, TransportAction>>() {}),

server/src/main/java/org/elasticsearch/plugins/Plugin.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
2929
import org.elasticsearch.cluster.service.ClusterService;
3030
import org.elasticsearch.common.component.LifecycleComponent;
31-
import org.elasticsearch.common.inject.Module;
3231
import org.elasticsearch.common.io.stream.NamedWriteable;
3332
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
3433
import org.elasticsearch.common.settings.Setting;
@@ -84,21 +83,6 @@ protected Optional<String> getFeature() {
8483
return Optional.empty();
8584
}
8685

87-
/**
88-
* Node level guice modules.
89-
*/
90-
public Collection<Module> createGuiceModules() {
91-
return Collections.emptyList();
92-
}
93-
94-
/**
95-
* Node level services that will be automatically started/stopped/closed. This classes must be constructed
96-
* by injection with guice.
97-
*/
98-
public Collection<Class<? extends LifecycleComponent>> getGuiceServiceClasses() {
99-
return Collections.emptyList();
100-
}
101-
10286
/**
10387
* Returns components added by this plugin.
10488
*

server/src/main/java/org/elasticsearch/plugins/PluginsService.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
import org.elasticsearch.bootstrap.JarHell;
3434
import org.elasticsearch.common.Strings;
3535
import org.elasticsearch.common.collect.Tuple;
36-
import org.elasticsearch.common.component.LifecycleComponent;
37-
import org.elasticsearch.common.inject.Module;
3836
import org.elasticsearch.common.io.FileSystemUtils;
3937
import org.elasticsearch.common.settings.Setting;
4038
import org.elasticsearch.common.settings.Setting.Property;
@@ -237,14 +235,6 @@ public Settings updatedSettings() {
237235
return builder.put(this.settings).build();
238236
}
239237

240-
public Collection<Module> createGuiceModules() {
241-
List<Module> modules = new ArrayList<>();
242-
for (Tuple<PluginInfo, Plugin> plugin : plugins) {
243-
modules.addAll(plugin.v2().createGuiceModules());
244-
}
245-
return modules;
246-
}
247-
248238
public List<ExecutorBuilder<?>> getExecutorBuilders(Settings settings) {
249239
final ArrayList<ExecutorBuilder<?>> builders = new ArrayList<>();
250240
for (final Tuple<PluginInfo, Plugin> plugin : plugins) {
@@ -253,15 +243,6 @@ public List<ExecutorBuilder<?>> getExecutorBuilders(Settings settings) {
253243
return builders;
254244
}
255245

256-
/** Returns all classes injected into guice by plugins which extend {@link LifecycleComponent}. */
257-
public Collection<Class<? extends LifecycleComponent>> getGuiceServiceClasses() {
258-
List<Class<? extends LifecycleComponent>> services = new ArrayList<>();
259-
for (Tuple<PluginInfo, Plugin> plugin : plugins) {
260-
services.addAll(plugin.v2().getGuiceServiceClasses());
261-
}
262-
return services;
263-
}
264-
265246
public void onIndexModule(IndexModule indexModule) {
266247
for (Tuple<PluginInfo, Plugin> plugin : plugins) {
267248
plugin.v2().onIndexModule(indexModule);

server/src/test/java/org/elasticsearch/index/SettingsListenerIT.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@
1818
*/
1919
package org.elasticsearch.index;
2020

21+
import org.elasticsearch.client.Client;
22+
import org.elasticsearch.cluster.service.ClusterService;
2123
import org.elasticsearch.common.inject.AbstractModule;
22-
import org.elasticsearch.common.inject.Module;
24+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
2325
import org.elasticsearch.common.settings.Setting;
2426
import org.elasticsearch.common.settings.Setting.Property;
2527
import org.elasticsearch.common.settings.Settings;
28+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
29+
import org.elasticsearch.env.Environment;
30+
import org.elasticsearch.env.NodeEnvironment;
2631
import org.elasticsearch.plugins.Plugin;
32+
import org.elasticsearch.script.ScriptService;
2733
import org.elasticsearch.test.ESIntegTestCase;
2834
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
35+
import org.elasticsearch.threadpool.ThreadPool;
36+
import org.elasticsearch.watcher.ResourceWatcherService;
2937

3038
import java.util.Arrays;
3139
import java.util.Collection;
@@ -60,8 +68,11 @@ public void onIndexModule(IndexModule module) {
6068
}
6169

6270
@Override
63-
public Collection<Module> createGuiceModules() {
64-
return Collections.<Module>singletonList(new SettingsListenerModule(service));
71+
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool,
72+
ResourceWatcherService resourceWatcherService, ScriptService scriptService,
73+
NamedXContentRegistry xContentRegistry, Environment environment,
74+
NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry) {
75+
return Collections.singletonList(service);
6576
}
6677
}
6778

test/framework/src/main/java/org/elasticsearch/test/MockIndexEventListener.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818
*/
1919
package org.elasticsearch.test;
2020

21+
import org.elasticsearch.client.Client;
2122
import org.elasticsearch.cluster.routing.ShardRouting;
23+
import org.elasticsearch.cluster.service.ClusterService;
2224
import org.elasticsearch.common.Nullable;
23-
import org.elasticsearch.common.inject.Module;
25+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
2426
import org.elasticsearch.common.settings.Setting;
2527
import org.elasticsearch.common.settings.Setting.Property;
2628
import org.elasticsearch.common.settings.Settings;
29+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
30+
import org.elasticsearch.env.Environment;
31+
import org.elasticsearch.env.NodeEnvironment;
2732
import org.elasticsearch.index.Index;
2833
import org.elasticsearch.index.IndexModule;
2934
import org.elasticsearch.index.IndexService;
@@ -34,6 +39,9 @@
3439
import org.elasticsearch.index.shard.ShardId;
3540
import org.elasticsearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason;
3641
import org.elasticsearch.plugins.Plugin;
42+
import org.elasticsearch.script.ScriptService;
43+
import org.elasticsearch.threadpool.ThreadPool;
44+
import org.elasticsearch.watcher.ResourceWatcherService;
3745

3846
import java.util.Arrays;
3947
import java.util.Collection;
@@ -72,8 +80,11 @@ public void onIndexModule(IndexModule module) {
7280
}
7381

7482
@Override
75-
public Collection<Module> createGuiceModules() {
76-
return Collections.singleton(binder -> binder.bind(TestEventListener.class).toInstance(listener));
83+
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool,
84+
ResourceWatcherService resourceWatcherService, ScriptService scriptService,
85+
NamedXContentRegistry xContentRegistry, Environment environment,
86+
NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry) {
87+
return Collections.singletonList(listener);
7788
}
7889
}
7990

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/EmptyXPackFeatureSet.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackPlugin.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.elasticsearch.cluster.service.ClusterService;
2424
import org.elasticsearch.common.Booleans;
2525
import org.elasticsearch.common.inject.Binder;
26-
import org.elasticsearch.common.inject.Module;
2726
import org.elasticsearch.common.inject.multibindings.Multibinder;
2827
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
2928
import org.elasticsearch.common.logging.DeprecationLogger;
@@ -221,15 +220,6 @@ public Settings additionalSettings() {
221220
return Settings.builder().put(super.additionalSettings()).put(xpackInstalledNodeAttrSetting, "true").build();
222221
}
223222

224-
@Override
225-
public Collection<Module> createGuiceModules() {
226-
ArrayList<Module> modules = new ArrayList<>();
227-
//modules.add(b -> b.bind(Clock.class).toInstance(getClock()));
228-
// used to get core up and running, we do not bind the actual feature set here
229-
modules.add(b -> XPackPlugin.createFeatureSetMultiBinder(b, EmptyXPackFeatureSet.class));
230-
return modules;
231-
}
232-
233223
@Override
234224
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool,
235225
ResourceWatcherService resourceWatcherService, ScriptService scriptService,

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/LocalStateCompositeXPackPlugin.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
2222
import org.elasticsearch.cluster.node.DiscoveryNodes;
2323
import org.elasticsearch.cluster.service.ClusterService;
24-
import org.elasticsearch.common.inject.Module;
2524
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
2625
import org.elasticsearch.common.network.NetworkService;
2726
import org.elasticsearch.common.settings.ClusterSettings;
@@ -136,16 +135,6 @@ protected void setLicenseState(XPackLicenseState licenseState) {
136135
this.licenseState = licenseState;
137136
}
138137

139-
@Override
140-
public Collection<Module> createGuiceModules() {
141-
ArrayList<Module> modules = new ArrayList<>();
142-
modules.addAll(super.createGuiceModules());
143-
filterPlugins(Plugin.class).stream().forEach(p ->
144-
modules.addAll(p.createGuiceModules())
145-
);
146-
return modules;
147-
}
148-
149138
@Override
150139
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool,
151140
ResourceWatcherService resourceWatcherService, ScriptService scriptService,

x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/Monitoring.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
1212
import org.elasticsearch.cluster.node.DiscoveryNodes;
1313
import org.elasticsearch.cluster.service.ClusterService;
14-
import org.elasticsearch.common.inject.Module;
15-
import org.elasticsearch.common.inject.util.Providers;
1614
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
1715
import org.elasticsearch.common.settings.ClusterSettings;
1816
import org.elasticsearch.common.settings.IndexScopedSettings;
@@ -95,25 +93,13 @@ boolean isEnabled() {
9593
return enabled;
9694
}
9795

98-
@Override
99-
public Collection<Module> createGuiceModules() {
100-
List<Module> modules = new ArrayList<>();
101-
modules.add(b -> {
102-
if (enabled == false) {
103-
b.bind(MonitoringService.class).toProvider(Providers.of(null));
104-
b.bind(Exporters.class).toProvider(Providers.of(null));
105-
}
106-
});
107-
return modules;
108-
}
109-
11096
@Override
11197
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool,
11298
ResourceWatcherService resourceWatcherService, ScriptService scriptService,
11399
NamedXContentRegistry xContentRegistry, Environment environment,
114100
NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry) {
115101
if (enabled == false) {
116-
return Collections.emptyList();
102+
return Collections.singletonList(new MonitoringUsageServices(null, null));
117103
}
118104

119105
final ClusterSettings clusterSettings = clusterService.getClusterSettings();
@@ -137,7 +123,8 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
137123

138124
final MonitoringService monitoringService = new MonitoringService(settings, clusterService, threadPool, collectors, exporters);
139125

140-
return Arrays.asList(monitoringService, exporters, cleanerService);
126+
var usageServices = new MonitoringUsageServices(monitoringService, exporters);
127+
return Arrays.asList(monitoringService, exporters, cleanerService, usageServices);
141128
}
142129

143130
@Override
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.monitoring;
7+
8+
import org.elasticsearch.xpack.monitoring.exporter.Exporters;
9+
10+
/**
11+
* A wrapper around the services needed to produce usage information for the monitoring feature.
12+
*
13+
* This class is temporary until actions can be constructed directly by plugins.
14+
*/
15+
class MonitoringUsageServices {
16+
final MonitoringService monitoringService;
17+
final Exporters exporters;
18+
19+
MonitoringUsageServices(MonitoringService monitoringService, Exporters exporters) {
20+
this.monitoringService = monitoringService;
21+
this.exporters = exporters;
22+
}
23+
}

0 commit comments

Comments
 (0)