Skip to content

Commit 2025150

Browse files
committed
unguice
1 parent e86a5cf commit 2025150

File tree

9 files changed

+76
-52
lines changed

9 files changed

+76
-52
lines changed

core/src/main/java/org/elasticsearch/cluster/ClusterModule.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public class ClusterModule extends AbstractModule {
9494
private final IndexNameExpressionResolver indexNameExpressionResolver;
9595
private final AllocationDeciders allocationDeciders;
9696
private final AllocationService allocationService;
97+
private final Runnable onStarted;
9798
// pkg private for tests
9899
final Collection<AllocationDecider> deciderList;
99100
final ShardsAllocator shardsAllocator;
@@ -106,6 +107,7 @@ public ClusterModule(Settings settings, ClusterService clusterService, List<Clus
106107
this.clusterService = clusterService;
107108
this.indexNameExpressionResolver = new IndexNameExpressionResolver(settings);
108109
this.allocationService = new AllocationService(settings, allocationDeciders, shardsAllocator, clusterInfoService);
110+
this.onStarted = () -> clusterPlugins.forEach(plugin -> plugin.onNodeStarted());
109111
}
110112

111113

@@ -241,4 +243,8 @@ protected void configure() {
241243
bind(AllocationDeciders.class).toInstance(allocationDeciders);
242244
bind(ShardsAllocator.class).toInstance(shardsAllocator);
243245
}
246+
247+
public Runnable onStarted() {
248+
return onStarted;
249+
}
244250
}

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@
152152
import java.util.Collections;
153153
import java.util.List;
154154
import java.util.Map;
155-
import java.util.concurrent.CopyOnWriteArrayList;
156155
import java.util.concurrent.CountDownLatch;
157156
import java.util.concurrent.TimeUnit;
158157
import java.util.function.Consumer;
@@ -229,7 +228,6 @@ public static final Settings addNodeNameIfNeeded(Settings settings, final String
229228
private final Collection<LifecycleComponent> pluginLifecycleComponents;
230229
private final LocalNodeFactory localNodeFactory;
231230
private final NodeService nodeService;
232-
private final List<Runnable> onStartedListeners = new CopyOnWriteArrayList<>();
233231

234232
/**
235233
* Constructs a node with the given settings.
@@ -394,7 +392,9 @@ protected Node(final Environment environment, Collection<Class<? extends Plugin>
394392

395393
Collection<Object> pluginComponents = pluginsService.filterPlugins(Plugin.class).stream()
396394
.flatMap(p -> p.createComponents(client, clusterService, threadPool, resourceWatcherService,
397-
scriptModule.getScriptService(), xContentRegistry).stream())
395+
scriptModule.getScriptService(), xContentRegistry, environment, nodeEnvironment,
396+
namedWriteableRegistry,
397+
(settings, configPath) -> newNode(settings, classpathPlugins, configPath)).stream())
398398
.collect(Collectors.toList());
399399
final RestController restController = actionModule.getRestController();
400400
final NetworkModule networkModule = new NetworkModule(settings, false, pluginsService.filterPlugins(NetworkPlugin.class),
@@ -439,7 +439,6 @@ protected Node(final Environment environment, Collection<Class<? extends Plugin>
439439
transportService, indicesService, pluginsService, circuitBreakerService, scriptModule.getScriptService(),
440440
httpServerTransport, ingestService, clusterService, settingsModule.getSettingsFilter());
441441
modules.add(b -> {
442-
b.bind(NodeBuilder.class).toInstance(new NodeBuilder(this, classpathPlugins));
443442
b.bind(Node.class).toInstance(this);
444443
b.bind(NodeService.class).toInstance(nodeService);
445444
b.bind(NamedXContentRegistry.class).toInstance(xContentRegistry);
@@ -517,10 +516,6 @@ protected Node(final Environment environment, Collection<Class<? extends Plugin>
517516
}
518517
}
519518

520-
public void addOnStartedListener(Runnable runnable) {
521-
onStartedListeners.add(runnable);
522-
}
523-
524519
// visible for testing
525520
static void warnIfPreRelease(final Version version, final boolean isSnapshot, final Logger logger) {
526521
if (!version.isRelease() || isSnapshot) {
@@ -674,7 +669,7 @@ public void onTimeout(TimeValue timeout) {
674669

675670
logger.info("started");
676671

677-
onStartedListeners.forEach(Runnable::run);
672+
pluginsService.filterPlugins(ClusterPlugin.class).forEach(ClusterPlugin::onNodeStarted);
678673

679674
return this;
680675
}
@@ -908,21 +903,6 @@ private List<NetworkService.CustomNameResolver> getCustomNameResolvers(List<Disc
908903
return customNameResolvers;
909904
}
910905

911-
public static class NodeBuilder {
912-
913-
private final Node node;
914-
private final Collection<Class<? extends Plugin>> classpathPlugins;
915-
916-
public NodeBuilder(Node node, Collection<Class<? extends Plugin>> classpathPlugins) {
917-
this.node = node;
918-
this.classpathPlugins = classpathPlugins;
919-
}
920-
921-
public Node newNode(Settings settings, Path configPath) {
922-
return node.newNode(settings, classpathPlugins, configPath);
923-
}
924-
}
925-
926906
/** Constructs a new node based on the following settings. Overridden by tests */
927907
protected Node newNode(Settings settings, Collection<Class<? extends Plugin>> classpathPlugins, Path configPath) {
928908
return new Node(new Environment(settings, configPath), classpathPlugins);

core/src/main/java/org/elasticsearch/plugins/ClusterPlugin.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,11 @@ default Collection<AllocationDecider> createAllocationDeciders(Settings settings
5858
default Map<String, Supplier<ShardsAllocator>> getShardsAllocators(Settings settings, ClusterSettings clusterSettings) {
5959
return Collections.emptyMap();
6060
}
61+
62+
/**
63+
* Called when the node is started
64+
*/
65+
default void onNodeStarted() {
66+
67+
}
6168
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@
3838
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
3939
import org.elasticsearch.common.xcontent.XContentParser;
4040
import org.elasticsearch.discovery.DiscoveryModule;
41+
import org.elasticsearch.env.Environment;
42+
import org.elasticsearch.env.NodeEnvironment;
4143
import org.elasticsearch.index.IndexModule;
4244
import org.elasticsearch.indices.analysis.AnalysisModule;
45+
import org.elasticsearch.node.Node;
4346
import org.elasticsearch.repositories.RepositoriesModule;
4447
import org.elasticsearch.script.ScriptModule;
4548
import org.elasticsearch.script.ScriptService;
@@ -50,10 +53,12 @@
5053

5154
import java.io.Closeable;
5255
import java.io.IOException;
56+
import java.nio.file.Path;
5357
import java.util.Collection;
5458
import java.util.Collections;
5559
import java.util.List;
5660
import java.util.Map;
61+
import java.util.function.BiFunction;
5762
import java.util.function.UnaryOperator;
5863

5964
/**
@@ -107,7 +112,9 @@ public Collection<Class<? extends LifecycleComponent>> getGuiceServiceClasses()
107112
*/
108113
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool,
109114
ResourceWatcherService resourceWatcherService, ScriptService scriptService,
110-
NamedXContentRegistry xContentRegistry) {
115+
NamedXContentRegistry xContentRegistry, Environment environment,
116+
NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry,
117+
BiFunction<Settings, Path, Node> clientNodeBuilder) {
111118
return Collections.emptyList();
112119
}
113120

core/src/test/java/org/elasticsearch/cluster/metadata/TemplateUpgradeServiceIT.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,27 @@
2222
import org.apache.logging.log4j.Logger;
2323
import org.elasticsearch.client.Client;
2424
import org.elasticsearch.cluster.service.ClusterService;
25+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
2526
import org.elasticsearch.common.logging.Loggers;
2627
import org.elasticsearch.common.settings.Setting;
2728
import org.elasticsearch.common.settings.Settings;
2829
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
30+
import org.elasticsearch.env.Environment;
31+
import org.elasticsearch.env.NodeEnvironment;
32+
import org.elasticsearch.node.Node;
2933
import org.elasticsearch.plugins.Plugin;
3034
import org.elasticsearch.script.ScriptService;
3135
import org.elasticsearch.test.ESIntegTestCase;
3236
import org.elasticsearch.threadpool.ThreadPool;
3337
import org.elasticsearch.watcher.ResourceWatcherService;
3438

39+
import java.nio.file.Path;
3540
import java.util.Collection;
3641
import java.util.Collections;
3742
import java.util.List;
3843
import java.util.Map;
3944
import java.util.concurrent.atomic.AtomicInteger;
45+
import java.util.function.BiFunction;
4046
import java.util.function.UnaryOperator;
4147

4248
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
@@ -67,11 +73,14 @@ public TestPlugin(Settings settings) {
6773
@Override
6874
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool,
6975
ResourceWatcherService resourceWatcherService, ScriptService scriptService,
70-
NamedXContentRegistry xContentRegistry) {
76+
NamedXContentRegistry xContentRegistry, Environment environment,
77+
NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry,
78+
BiFunction<Settings, Path, Node> clientNodeBuilder) {
7179
clusterService.getClusterSettings().addSettingsUpdateConsumer(UPDATE_TEMPLATE_DUMMY_SETTING, integer -> {
7280
logger.debug("the template dummy setting was updated to {}", integer);
7381
});
74-
return super.createComponents(client, clusterService, threadPool, resourceWatcherService, scriptService, xContentRegistry);
82+
return super.createComponents(client, clusterService, threadPool, resourceWatcherService, scriptService, xContentRegistry,
83+
environment, nodeEnvironment, namedWriteableRegistry, clientNodeBuilder);
7584
}
7685

7786
@Override

modules/tribe/src/main/java/org/elasticsearch/tribe/TribePlugin.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,52 @@
2020
package org.elasticsearch.tribe;
2121

2222
import org.elasticsearch.action.support.master.TransportMasterNodeReadAction;
23+
import org.elasticsearch.client.Client;
2324
import org.elasticsearch.cluster.routing.allocation.AllocationService;
2425
import org.elasticsearch.cluster.service.ClusterApplier;
26+
import org.elasticsearch.cluster.service.ClusterService;
2527
import org.elasticsearch.cluster.service.MasterService;
2628
import org.elasticsearch.common.UUIDs;
27-
import org.elasticsearch.common.component.LifecycleComponent;
2829
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
2930
import org.elasticsearch.common.settings.ClusterSettings;
3031
import org.elasticsearch.common.settings.Setting;
3132
import org.elasticsearch.common.settings.Settings;
33+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
3234
import org.elasticsearch.discovery.Discovery;
3335
import org.elasticsearch.discovery.DiscoveryModule;
3436
import org.elasticsearch.discovery.DiscoverySettings;
3537
import org.elasticsearch.discovery.zen.UnicastHostsProvider;
38+
import org.elasticsearch.env.Environment;
3639
import org.elasticsearch.env.NodeEnvironment;
3740
import org.elasticsearch.node.Node;
41+
import org.elasticsearch.plugins.ClusterPlugin;
3842
import org.elasticsearch.plugins.DiscoveryPlugin;
3943
import org.elasticsearch.plugins.Plugin;
44+
import org.elasticsearch.script.ScriptService;
4045
import org.elasticsearch.threadpool.ThreadPool;
4146
import org.elasticsearch.transport.TransportService;
47+
import org.elasticsearch.watcher.ResourceWatcherService;
4248

49+
import java.nio.file.Path;
4350
import java.util.ArrayList;
4451
import java.util.Arrays;
4552
import java.util.Collection;
4653
import java.util.Collections;
4754
import java.util.List;
4855
import java.util.Map;
56+
import java.util.function.BiFunction;
4957
import java.util.function.Function;
5058
import java.util.function.Supplier;
5159

52-
public class TribePlugin extends Plugin implements DiscoveryPlugin {
60+
public class TribePlugin extends Plugin implements DiscoveryPlugin, ClusterPlugin {
5361

5462
private final Settings settings;
63+
private TribeService tribeService;
5564

5665
public TribePlugin(Settings settings) {
5766
this.settings = settings;
5867
}
5968

60-
6169
@Override
6270
public Map<String, Supplier<Discovery>> getDiscoveryTypes(ThreadPool threadPool, TransportService transportService,
6371
NamedWriteableRegistry namedWriteableRegistry,
@@ -68,8 +76,18 @@ public Map<String, Supplier<Discovery>> getDiscoveryTypes(ThreadPool threadPool,
6876
}
6977

7078
@Override
71-
public Collection<Class<? extends LifecycleComponent>> getGuiceServiceClasses() {
72-
return Collections.singleton(TribeService.class);
79+
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool,
80+
ResourceWatcherService resourceWatcherService, ScriptService scriptService,
81+
NamedXContentRegistry xContentRegistry, Environment environment,
82+
NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry,
83+
BiFunction<Settings, Path, Node> clientNodeBuilder) {
84+
tribeService = new TribeService(settings, environment, nodeEnvironment, clusterService, namedWriteableRegistry, clientNodeBuilder);
85+
return Collections.singleton(tribeService);
86+
}
87+
88+
@Override
89+
public void onNodeStarted() {
90+
tribeService.startNodes();
7391
}
7492

7593
@Override

modules/tribe/src/main/java/org/elasticsearch/tribe/TribeService.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.apache.logging.log4j.message.ParameterizedMessage;
2323
import org.apache.logging.log4j.util.Supplier;
2424
import org.apache.lucene.util.BytesRef;
25-
import org.elasticsearch.ElasticsearchException;
2625
import org.elasticsearch.ExceptionsHelper;
2726
import org.elasticsearch.cluster.ClusterChangedEvent;
2827
import org.elasticsearch.cluster.ClusterState;
@@ -45,7 +44,6 @@
4544
import org.elasticsearch.common.component.AbstractLifecycleComponent;
4645
import org.elasticsearch.common.component.Lifecycle;
4746
import org.elasticsearch.common.hash.MurmurHash3;
48-
import org.elasticsearch.common.inject.Inject;
4947
import org.elasticsearch.common.io.stream.BytesStreamOutput;
5048
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
5149
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
@@ -67,6 +65,7 @@
6765
import org.elasticsearch.transport.TcpTransport;
6866

6967
import java.io.IOException;
68+
import java.nio.file.Path;
7069
import java.util.Arrays;
7170
import java.util.Collection;
7271
import java.util.Collections;
@@ -76,6 +75,7 @@
7675
import java.util.Map;
7776
import java.util.Set;
7877
import java.util.concurrent.CopyOnWriteArrayList;
78+
import java.util.function.BiFunction;
7979
import java.util.function.Function;
8080
import java.util.stream.Collectors;
8181

@@ -155,9 +155,8 @@ public class TribeService extends AbstractLifecycleComponent {
155155

156156
private final NamedWriteableRegistry namedWriteableRegistry;
157157

158-
@Inject
159158
public TribeService(Settings settings, Environment environment, NodeEnvironment nodeEnvironment, ClusterService clusterService,
160-
Node node, NamedWriteableRegistry namedWriteableRegistry, Node.NodeBuilder clientNodeBuilder) {
159+
NamedWriteableRegistry namedWriteableRegistry, BiFunction<Settings, Path, Node> clientNodeBuilder) {
161160
super(settings);
162161
this.clusterService = clusterService;
163162
this.namedWriteableRegistry = namedWriteableRegistry;
@@ -167,7 +166,7 @@ public TribeService(Settings settings, Environment environment, NodeEnvironment
167166
for (Map.Entry<String, Settings> entry : nodesSettings.entrySet()) {
168167
Settings clientSettings = buildClientSettings(entry.getKey(), nodeEnvironment.nodeId(), settings, entry.getValue());
169168
try {
170-
nodes.add(clientNodeBuilder.newNode(clientSettings, environment.configFile()));
169+
nodes.add(clientNodeBuilder.apply(clientSettings, environment.configFile()));
171170
} catch (Exception e) {
172171
// calling close is safe for non started nodes, we can just iterate over all
173172
for (Node otherNode : nodes) {
@@ -190,7 +189,6 @@ public TribeService(Settings settings, Environment environment, NodeEnvironment
190189
.deprecated("tribe nodes are deprecated in favor of cross-cluster search and will be removed in Elasticsearch 7.0.0");
191190
}
192191
this.onConflict = ON_CONFLICT_SETTING.get(settings);
193-
node.addOnStartedListener(this::startNodes);
194192
}
195193

196194
// pkg private for testing

modules/tribe/src/test/java/org/elasticsearch/tribe/TribeIntegrationTests.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import org.elasticsearch.action.support.DestructiveOperations;
2424
import org.elasticsearch.client.Client;
2525
import org.elasticsearch.cluster.ClusterName;
26-
import org.elasticsearch.cluster.ClusterStateUpdateTask;
2726
import org.elasticsearch.cluster.ClusterState;
27+
import org.elasticsearch.cluster.ClusterStateUpdateTask;
2828
import org.elasticsearch.cluster.NamedDiff;
2929
import org.elasticsearch.cluster.block.ClusterBlockException;
3030
import org.elasticsearch.cluster.health.ClusterHealthStatus;
@@ -33,14 +33,12 @@
3333
import org.elasticsearch.cluster.service.ClusterService;
3434
import org.elasticsearch.common.Priority;
3535
import org.elasticsearch.common.UUIDs;
36-
import org.elasticsearch.common.inject.ProvisionException;
3736
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
3837
import org.elasticsearch.common.io.stream.Writeable;
3938
import org.elasticsearch.common.lease.Releasable;
4039
import org.elasticsearch.common.network.NetworkModule;
4140
import org.elasticsearch.common.settings.Settings;
4241
import org.elasticsearch.common.util.set.Sets;
43-
import org.elasticsearch.discovery.DiscoveryModule;
4442
import org.elasticsearch.discovery.DiscoverySettings;
4543
import org.elasticsearch.discovery.MasterNotDiscoveredException;
4644
import org.elasticsearch.node.Node;
@@ -79,7 +77,6 @@
7977
import static org.hamcrest.Matchers.containsInAnyOrder;
8078
import static org.hamcrest.Matchers.containsString;
8179
import static org.hamcrest.Matchers.equalTo;
82-
import static org.hamcrest.Matchers.instanceOf;
8380
import static org.hamcrest.Matchers.notNullValue;
8481
import static org.hamcrest.core.Is.is;
8582

@@ -298,9 +295,8 @@ public void testTribeNodeWithBadSettings() throws Exception {
298295
.put("tribe.some.setting.that.does.not.exist", true)
299296
.build();
300297

301-
ProvisionException e = expectThrows(ProvisionException.class, () -> startTribeNode(ALL, brokenSettings)); // <3 Guice
302-
assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
303-
assertThat(e.getCause().getMessage(), containsString("unknown setting [setting.that.does.not.exist]"));
298+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> startTribeNode(ALL, brokenSettings));
299+
assertThat(e.getMessage(), containsString("unknown setting [setting.that.does.not.exist]"));
304300
}
305301

306302
public void testGlobalReadWriteBlocks() throws Exception {

0 commit comments

Comments
 (0)