From 8def6f59b33883e6288b867a875f2bfb4e23b432 Mon Sep 17 00:00:00 2001 From: kimchy Date: Sun, 19 Sep 2010 18:48:48 +0200 Subject: [PATCH] simple specific master nodes test --- .../master/MasterNodeOperationRequest.java | 7 ++ .../state/ClusterStateRequestBuilder.java | 17 +++++ .../create/CreateIndexRequestBuilder.java | 8 ++ .../delete/DeleteIndexRequestBuilder.java | 7 ++ .../discovery/zen/ZenDiscovery.java | 3 + .../test/integration/AbstractNodesTests.java | 4 + .../masternode/SpecificMasterNodesTests.java | 73 +++++++++++++++++++ 7 files changed, 119 insertions(+) create mode 100644 modules/test/integration/src/test/java/org/elasticsearch/test/integration/masternode/SpecificMasterNodesTests.java diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/support/master/MasterNodeOperationRequest.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/support/master/MasterNodeOperationRequest.java index 49b51b78cda..74e1cce1e30 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/support/master/MasterNodeOperationRequest.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/support/master/MasterNodeOperationRequest.java @@ -53,6 +53,13 @@ public abstract class MasterNodeOperationRequest implements ActionRequest { return this; } + /** + * A timeout value in case the master has not been discovered yet or disconnected. + */ + public MasterNodeOperationRequest masterNodeTimeout(String timeout) { + return masterNodeTimeout(TimeValue.parseTimeValue(timeout, null)); + } + public TimeValue masterNodeTimeout() { return this.masterNodeTimeout; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/cluster/state/ClusterStateRequestBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/cluster/state/ClusterStateRequestBuilder.java index 49b62921a87..d4d9b85e0a5 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/cluster/state/ClusterStateRequestBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/cluster/state/ClusterStateRequestBuilder.java @@ -24,6 +24,7 @@ import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest; import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; import org.elasticsearch.client.ClusterAdminClient; import org.elasticsearch.client.action.admin.cluster.support.BaseClusterRequestBuilder; +import org.elasticsearch.common.unit.TimeValue; /** * @author kimchy (shay.banon) @@ -70,6 +71,22 @@ public class ClusterStateRequestBuilder extends BaseClusterRequestBuilder listener) { client.state(request, listener); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/create/CreateIndexRequestBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/create/CreateIndexRequestBuilder.java index dc60ffc32db..9e1acab5af0 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/create/CreateIndexRequestBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/create/CreateIndexRequestBuilder.java @@ -138,6 +138,14 @@ public class CreateIndexRequestBuilder extends BaseIndicesRequestBuilder listener) { client.create(request, listener); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/delete/DeleteIndexRequestBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/delete/DeleteIndexRequestBuilder.java index f73d01ced45..d7d5f45fedf 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/delete/DeleteIndexRequestBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/delete/DeleteIndexRequestBuilder.java @@ -61,6 +61,13 @@ public class DeleteIndexRequestBuilder extends BaseIndicesRequestBuilder listener) { client.delete(request, listener); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java b/modules/elasticsearch/src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java index e5c36f9a470..cef0afb46f1 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java @@ -474,6 +474,9 @@ public class ZenDiscovery extends AbstractLifecycleComponent implemen private DiscoveryNode findMaster() { ZenPing.PingResponse[] pingResponses = pingService.pingAndWait(initialPingTimeout); + if (pingResponses == null) { + return null; + } if (logger.isDebugEnabled()) { StringBuilder sb = new StringBuilder("ping responses:"); if (pingResponses.length == 0) { diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/AbstractNodesTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/AbstractNodesTests.java index a49ecb747a1..88ec3926cc4 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/AbstractNodesTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/AbstractNodesTests.java @@ -44,6 +44,10 @@ public abstract class AbstractNodesTests { return buildNode(id).start(); } + public Node startNode(String id, Settings.Builder settings) { + return startNode(id, settings.build()); + } + public Node startNode(String id, Settings settings) { return buildNode(id, settings).start(); } diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/masternode/SpecificMasterNodesTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/masternode/SpecificMasterNodesTests.java new file mode 100644 index 00000000000..fca46a5c9ac --- /dev/null +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/masternode/SpecificMasterNodesTests.java @@ -0,0 +1,73 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.test.integration.masternode; + +import org.elasticsearch.discovery.MasterNotDiscoveredException; +import org.elasticsearch.test.integration.AbstractNodesTests; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.Test; + +import static org.elasticsearch.common.settings.ImmutableSettings.*; +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; + +/** + * @author kimchy (shay.banon) + */ +public class SpecificMasterNodesTests extends AbstractNodesTests { + + @AfterMethod public void closeNodes() { + closeAllNodes(); + } + + @Test public void simpleOnlyMasterNodesElection() { + logger.info("--> start data node / non master node"); + startNode("data1", settingsBuilder().put("node.data", true).put("node.master", false).put("discovery.initial_state_timeout", "1s")); + try { + assertThat(client("data1").admin().cluster().prepareState().setMasterNodeTimeout("100ms").execute().actionGet().state().nodes().masterNodeId(), nullValue()); + assert false : "should not be able to find master"; + } catch (MasterNotDiscoveredException e) { + // all is well, no master elected + } + logger.info("--> start master node"); + startNode("master1", settingsBuilder().put("node.data", false).put("node.master", true)); + assertThat(client("data1").admin().cluster().prepareState().execute().actionGet().state().nodes().masterNode().name(), equalTo("master1")); + assertThat(client("master1").admin().cluster().prepareState().execute().actionGet().state().nodes().masterNode().name(), equalTo("master1")); + + logger.info("--> stop master node"); + closeNode("master1"); + + try { + assertThat(client("data1").admin().cluster().prepareState().setMasterNodeTimeout("100ms").execute().actionGet().state().nodes().masterNodeId(), nullValue()); + assert false : "should not be able to find master"; + } catch (MasterNotDiscoveredException e) { + // all is well, no master elected + } + + logger.info("--> start master node"); + startNode("master1", settingsBuilder().put("node.data", false).put("node.master", true)); + assertThat(client("data1").admin().cluster().prepareState().execute().actionGet().state().nodes().masterNode().name(), equalTo("master1")); + assertThat(client("master1").admin().cluster().prepareState().execute().actionGet().state().nodes().masterNode().name(), equalTo("master1")); + + logger.info("--> stop all nodes"); + closeNode("data1"); + closeNode("master1"); + } +}