Skip to content

Commit

Permalink
unguice
Browse files Browse the repository at this point in the history
  • Loading branch information
ywelsch committed Jul 25, 2017
1 parent e86a5cf commit 2025150
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public class ClusterModule extends AbstractModule {
private final IndexNameExpressionResolver indexNameExpressionResolver;
private final AllocationDeciders allocationDeciders;
private final AllocationService allocationService;
private final Runnable onStarted;
// pkg private for tests
final Collection<AllocationDecider> deciderList;
final ShardsAllocator shardsAllocator;
Expand All @@ -106,6 +107,7 @@ public ClusterModule(Settings settings, ClusterService clusterService, List<Clus
this.clusterService = clusterService;
this.indexNameExpressionResolver = new IndexNameExpressionResolver(settings);
this.allocationService = new AllocationService(settings, allocationDeciders, shardsAllocator, clusterInfoService);
this.onStarted = () -> clusterPlugins.forEach(plugin -> plugin.onNodeStarted());
}


Expand Down Expand Up @@ -241,4 +243,8 @@ protected void configure() {
bind(AllocationDeciders.class).toInstance(allocationDeciders);
bind(ShardsAllocator.class).toInstance(shardsAllocator);
}

public Runnable onStarted() {
return onStarted;
}
}
28 changes: 4 additions & 24 deletions core/src/main/java/org/elasticsearch/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
Expand Down Expand Up @@ -229,7 +228,6 @@ public static final Settings addNodeNameIfNeeded(Settings settings, final String
private final Collection<LifecycleComponent> pluginLifecycleComponents;
private final LocalNodeFactory localNodeFactory;
private final NodeService nodeService;
private final List<Runnable> onStartedListeners = new CopyOnWriteArrayList<>();

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

Collection<Object> pluginComponents = pluginsService.filterPlugins(Plugin.class).stream()
.flatMap(p -> p.createComponents(client, clusterService, threadPool, resourceWatcherService,
scriptModule.getScriptService(), xContentRegistry).stream())
scriptModule.getScriptService(), xContentRegistry, environment, nodeEnvironment,
namedWriteableRegistry,
(settings, configPath) -> newNode(settings, classpathPlugins, configPath)).stream())
.collect(Collectors.toList());
final RestController restController = actionModule.getRestController();
final NetworkModule networkModule = new NetworkModule(settings, false, pluginsService.filterPlugins(NetworkPlugin.class),
Expand Down Expand Up @@ -439,7 +439,6 @@ protected Node(final Environment environment, Collection<Class<? extends Plugin>
transportService, indicesService, pluginsService, circuitBreakerService, scriptModule.getScriptService(),
httpServerTransport, ingestService, clusterService, settingsModule.getSettingsFilter());
modules.add(b -> {
b.bind(NodeBuilder.class).toInstance(new NodeBuilder(this, classpathPlugins));
b.bind(Node.class).toInstance(this);
b.bind(NodeService.class).toInstance(nodeService);
b.bind(NamedXContentRegistry.class).toInstance(xContentRegistry);
Expand Down Expand Up @@ -517,10 +516,6 @@ protected Node(final Environment environment, Collection<Class<? extends Plugin>
}
}

public void addOnStartedListener(Runnable runnable) {
onStartedListeners.add(runnable);
}

// visible for testing
static void warnIfPreRelease(final Version version, final boolean isSnapshot, final Logger logger) {
if (!version.isRelease() || isSnapshot) {
Expand Down Expand Up @@ -674,7 +669,7 @@ public void onTimeout(TimeValue timeout) {

logger.info("started");

onStartedListeners.forEach(Runnable::run);
pluginsService.filterPlugins(ClusterPlugin.class).forEach(ClusterPlugin::onNodeStarted);

return this;
}
Expand Down Expand Up @@ -908,21 +903,6 @@ private List<NetworkService.CustomNameResolver> getCustomNameResolvers(List<Disc
return customNameResolvers;
}

public static class NodeBuilder {

private final Node node;
private final Collection<Class<? extends Plugin>> classpathPlugins;

public NodeBuilder(Node node, Collection<Class<? extends Plugin>> classpathPlugins) {
this.node = node;
this.classpathPlugins = classpathPlugins;
}

public Node newNode(Settings settings, Path configPath) {
return node.newNode(settings, classpathPlugins, configPath);
}
}

/** Constructs a new node based on the following settings. Overridden by tests */
protected Node newNode(Settings settings, Collection<Class<? extends Plugin>> classpathPlugins, Path configPath) {
return new Node(new Environment(settings, configPath), classpathPlugins);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@ default Collection<AllocationDecider> createAllocationDeciders(Settings settings
default Map<String, Supplier<ShardsAllocator>> getShardsAllocators(Settings settings, ClusterSettings clusterSettings) {
return Collections.emptyMap();
}

/**
* Called when the node is started
*/
default void onNodeStarted() {

}
}
9 changes: 8 additions & 1 deletion core/src/main/java/org/elasticsearch/plugins/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.discovery.DiscoveryModule;
import org.elasticsearch.env.Environment;
import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.index.IndexModule;
import org.elasticsearch.indices.analysis.AnalysisModule;
import org.elasticsearch.node.Node;
import org.elasticsearch.repositories.RepositoriesModule;
import org.elasticsearch.script.ScriptModule;
import org.elasticsearch.script.ScriptService;
Expand All @@ -50,10 +53,12 @@

import java.io.Closeable;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;

/**
Expand Down Expand Up @@ -107,7 +112,9 @@ public Collection<Class<? extends LifecycleComponent>> getGuiceServiceClasses()
*/
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool,
ResourceWatcherService resourceWatcherService, ScriptService scriptService,
NamedXContentRegistry xContentRegistry) {
NamedXContentRegistry xContentRegistry, Environment environment,
NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry,
BiFunction<Settings, Path, Node> clientNodeBuilder) {
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,27 @@
import org.apache.logging.log4j.Logger;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.env.Environment;
import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.node.Node;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.watcher.ResourceWatcherService;

import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
Expand Down Expand Up @@ -67,11 +73,14 @@ public TestPlugin(Settings settings) {
@Override
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool,
ResourceWatcherService resourceWatcherService, ScriptService scriptService,
NamedXContentRegistry xContentRegistry) {
NamedXContentRegistry xContentRegistry, Environment environment,
NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry,
BiFunction<Settings, Path, Node> clientNodeBuilder) {
clusterService.getClusterSettings().addSettingsUpdateConsumer(UPDATE_TEMPLATE_DUMMY_SETTING, integer -> {
logger.debug("the template dummy setting was updated to {}", integer);
});
return super.createComponents(client, clusterService, threadPool, resourceWatcherService, scriptService, xContentRegistry);
return super.createComponents(client, clusterService, threadPool, resourceWatcherService, scriptService, xContentRegistry,
environment, nodeEnvironment, namedWriteableRegistry, clientNodeBuilder);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,52 @@
package org.elasticsearch.tribe;

import org.elasticsearch.action.support.master.TransportMasterNodeReadAction;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.routing.allocation.AllocationService;
import org.elasticsearch.cluster.service.ClusterApplier;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.cluster.service.MasterService;
import org.elasticsearch.common.UUIDs;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.discovery.Discovery;
import org.elasticsearch.discovery.DiscoveryModule;
import org.elasticsearch.discovery.DiscoverySettings;
import org.elasticsearch.discovery.zen.UnicastHostsProvider;
import org.elasticsearch.env.Environment;
import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.node.Node;
import org.elasticsearch.plugins.ClusterPlugin;
import org.elasticsearch.plugins.DiscoveryPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.watcher.ResourceWatcherService;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

public class TribePlugin extends Plugin implements DiscoveryPlugin {
public class TribePlugin extends Plugin implements DiscoveryPlugin, ClusterPlugin {

private final Settings settings;
private TribeService tribeService;

public TribePlugin(Settings settings) {
this.settings = settings;
}


@Override
public Map<String, Supplier<Discovery>> getDiscoveryTypes(ThreadPool threadPool, TransportService transportService,
NamedWriteableRegistry namedWriteableRegistry,
Expand All @@ -68,8 +76,18 @@ public Map<String, Supplier<Discovery>> getDiscoveryTypes(ThreadPool threadPool,
}

@Override
public Collection<Class<? extends LifecycleComponent>> getGuiceServiceClasses() {
return Collections.singleton(TribeService.class);
public Collection<Object> createComponents(Client client, ClusterService clusterService, ThreadPool threadPool,
ResourceWatcherService resourceWatcherService, ScriptService scriptService,
NamedXContentRegistry xContentRegistry, Environment environment,
NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry,
BiFunction<Settings, Path, Node> clientNodeBuilder) {
tribeService = new TribeService(settings, environment, nodeEnvironment, clusterService, namedWriteableRegistry, clientNodeBuilder);
return Collections.singleton(tribeService);
}

@Override
public void onNodeStarted() {
tribeService.startNodes();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.cluster.ClusterChangedEvent;
import org.elasticsearch.cluster.ClusterState;
Expand All @@ -45,7 +44,6 @@
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.component.Lifecycle;
import org.elasticsearch.common.hash.MurmurHash3;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
Expand All @@ -67,6 +65,7 @@
import org.elasticsearch.transport.TcpTransport;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -76,6 +75,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -155,9 +155,8 @@ public class TribeService extends AbstractLifecycleComponent {

private final NamedWriteableRegistry namedWriteableRegistry;

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

// pkg private for testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.elasticsearch.action.support.DestructiveOperations;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterStateUpdateTask;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateUpdateTask;
import org.elasticsearch.cluster.NamedDiff;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
Expand All @@ -33,14 +33,12 @@
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.UUIDs;
import org.elasticsearch.common.inject.ProvisionException;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.lease.Releasable;
import org.elasticsearch.common.network.NetworkModule;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.discovery.DiscoveryModule;
import org.elasticsearch.discovery.DiscoverySettings;
import org.elasticsearch.discovery.MasterNotDiscoveredException;
import org.elasticsearch.node.Node;
Expand Down Expand Up @@ -79,7 +77,6 @@
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.core.Is.is;

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

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

public void testGlobalReadWriteBlocks() throws Exception {
Expand Down
Loading

0 comments on commit 2025150

Please sign in to comment.