Use DiscoveryModule instead of ClusterService to obtain local node id

The ClusterService might not see the latest cluster state and therefore
might not contain the local node id. Discovery will always see the local
node id since it's set on startup.
This commit is contained in:
Simon Willnauer 2013-08-08 12:35:48 +02:00
parent d450d3b016
commit ef365098e7
1 changed files with 16 additions and 15 deletions

View File

@ -24,13 +24,13 @@ import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus; import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.routing.RoutingNode; import org.elasticsearch.cluster.routing.RoutingNode;
import org.elasticsearch.common.Priority; import org.elasticsearch.common.Priority;
import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.discovery.Discovery;
import org.elasticsearch.node.internal.InternalNode; import org.elasticsearch.node.internal.InternalNode;
import org.elasticsearch.test.integration.AbstractNodesTests; import org.elasticsearch.test.integration.AbstractNodesTests;
import org.junit.After; import org.junit.After;
@ -72,8 +72,7 @@ public class IndexLifecycleActionTests extends AbstractNodesTests {
logger.info("Starting sever1"); logger.info("Starting sever1");
startNode("server1", settings); startNode("server1", settings);
ClusterService clusterService1 = ((InternalNode) node("server1")).injector().getInstance(ClusterService.class); final String node1 = getLocalNodeId("server1");
String node1 = clusterService1.state().nodes().localNodeId();
wipeIndices(client()); wipeIndices(client());
@ -102,8 +101,8 @@ public class IndexLifecycleActionTests extends AbstractNodesTests {
assertThat(clusterHealth.isTimedOut(), equalTo(false)); assertThat(clusterHealth.isTimedOut(), equalTo(false));
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN)); assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
ClusterService clusterService2 = ((InternalNode) node("server2")).injector().getInstance(ClusterService.class); final String node2 = getLocalNodeId("server2");
String node2 = clusterService2.state().nodes().localNodeId();
// explicitly call reroute, so shards will get relocated to the new node (we delay it in ES in case other nodes join) // explicitly call reroute, so shards will get relocated to the new node (we delay it in ES in case other nodes join)
client("server1").admin().cluster().prepareReroute().execute().actionGet(); client("server1").admin().cluster().prepareReroute().execute().actionGet();
@ -138,8 +137,8 @@ public class IndexLifecycleActionTests extends AbstractNodesTests {
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN)); assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
ClusterService clusterService3 = ((InternalNode) node("server3")).injector().getInstance(ClusterService.class); final String node3 = getLocalNodeId("server3");
String node3 = clusterService3.state().nodes().localNodeId();
// explicitly call reroute, so shards will get relocated to the new node (we delay it in ES in case other nodes join) // explicitly call reroute, so shards will get relocated to the new node (we delay it in ES in case other nodes join)
client("server1").admin().cluster().prepareReroute().execute().actionGet(); client("server1").admin().cluster().prepareReroute().execute().actionGet();
@ -221,6 +220,13 @@ public class IndexLifecycleActionTests extends AbstractNodesTests {
assertThat(routingNodeEntry3.shards().isEmpty(), equalTo(true)); assertThat(routingNodeEntry3.shards().isEmpty(), equalTo(true));
} }
private String getLocalNodeId(String name) {
assert node(name) != null : "no node for name: " + name;
Discovery discovery = ((InternalNode) node(name)).injector().getInstance(Discovery.class);
String nodeId = discovery.localNode().getId();
assertThat(nodeId, not(nullValue()));
return nodeId;
}
@Slow @Slow
@Test @Test
public void testIndexLifecycleActionsWith11Shards0Backup() throws Exception { public void testIndexLifecycleActionsWith11Shards0Backup() throws Exception {
@ -235,9 +241,7 @@ public class IndexLifecycleActionTests extends AbstractNodesTests {
logger.info("Starting server1"); logger.info("Starting server1");
startNode("server1", settings); startNode("server1", settings);
ClusterService clusterService1 = ((InternalNode) node("server1")).injector().getInstance(ClusterService.class); final String node1 = getLocalNodeId("server1");
String node1 = clusterService1.state().nodes().localNodeId();
wipeIndices(client()); wipeIndices(client());
logger.info("Creating index [test]"); logger.info("Creating index [test]");
@ -267,8 +271,7 @@ public class IndexLifecycleActionTests extends AbstractNodesTests {
assertThat(clusterHealth.isTimedOut(), equalTo(false)); assertThat(clusterHealth.isTimedOut(), equalTo(false));
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN)); assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
ClusterService clusterService2 = ((InternalNode) node("server2")).injector().getInstance(ClusterService.class); final String node2 = getLocalNodeId("server2");
String node2 = clusterService2.state().nodes().localNodeId();
// explicitly call reroute, so shards will get relocated to the new node (we delay it in ES in case other nodes join) // explicitly call reroute, so shards will get relocated to the new node (we delay it in ES in case other nodes join)
client("server1").admin().cluster().prepareReroute().execute().actionGet(); client("server1").admin().cluster().prepareReroute().execute().actionGet();
@ -302,9 +305,7 @@ public class IndexLifecycleActionTests extends AbstractNodesTests {
assertThat(clusterHealth.isTimedOut(), equalTo(false)); assertThat(clusterHealth.isTimedOut(), equalTo(false));
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN)); assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
ClusterService clusterService3 = ((InternalNode) node("server3")).injector().getInstance(ClusterService.class); final String node3 = getLocalNodeId("server3");
String node3 = clusterService3.state().nodes().localNodeId();
// explicitly call reroute, so shards will get relocated to the new node (we delay it in ES in case other nodes join) // explicitly call reroute, so shards will get relocated to the new node (we delay it in ES in case other nodes join)
client("server1").admin().cluster().prepareReroute().execute().actionGet(); client("server1").admin().cluster().prepareReroute().execute().actionGet();