From a414e4f2f3ce03c1cd80ca3ef7d01c370e49d5a7 Mon Sep 17 00:00:00 2001
From: javanna
Date: Wed, 23 Apr 2014 16:05:32 +0200
Subject: [PATCH] [TEST] randomly introduced a client node within test cluster
The default number of clients nodes is randomized between 0 and 1, applied to all cluster scopes (global, suite and test). Can be changed through the newly added `@ClusterScope#numClientNodes`.
In our tests we currently refer to nodes in a generic way. All the tests that either stop or start nodes rely on the fact that those nodes hold data though. Made that clearer as that becomes more important when introducing other types of nodes within the test cluster. Reflected this by adapting and renaming the following methods in `TestCluster`:
- ensureAtLeastNumNodes to ensureAtLeastNumDataNodes
- ensureAtMostNumNodes to ensureAtMostNumDataNodes
- stopRandomNode to stopRandomDataNode
and the following ones in `ElasticsearchIntegrationTest`:
- allowNodes to allowDataNodes
- dataNodes to numDataNodes.
- @ClusterScope#numNodes to numDataNodes
- @ClusterScope#minNumNodes to minNumDataNodes
- @ClusterScope#maxNumNodes to maxNumDataNodes
Added facilities to be able to deal with data nodes specifically, like for instance retrieve a client to a data node, or retrieve an instance of a class through guice only from data nodes.
Adapted existing tests to successfully run although there's a node client around.
Fixed _cat/allocation REST tests to make disk.total, disk.avail and disk.percent optional as client nodes won't return that info.
Closes #5949
---
.../test/cat.allocation/10_basic.yaml | 86 ++++----
.../cluster/stats/ClusterStatsTests.java | 7 +-
.../action/bulk/BulkIntegrationTests.java | 9 +-
.../transport/TransportClientTests.java | 4 +-
.../cluster/ClusterServiceTests.java | 3 +-
.../cluster/MinimumMasterNodesTests.java | 5 +-
.../cluster/NoMasterNodeTests.java | 5 +-
.../cluster/SimpleDataNodesTests.java | 3 +-
.../cluster/SpecificMasterNodesTests.java | 3 +-
.../UpdateSettingsValidationTests.java | 3 +-
.../ack/AckClusterUpdateSettingsTests.java | 4 +-
.../elasticsearch/cluster/ack/AckTests.java | 8 +-
.../allocation/AwarenessAllocationTests.java | 2 +-
.../allocation/ClusterRerouteTests.java | 3 +-
.../allocation/FilteringAllocationTests.java | 3 +-
.../ShardsAllocatorModuleTests.java | 6 +-
.../shards/ClusterSearchShardsTests.java | 3 +-
.../DiscoveryWithNetworkFailuresTests.java | 2 +-
.../discovery/ZenUnicastDiscoveryTests.java | 20 +-
...icastDiscoveryTestsMinimumMasterNodes.java | 2 +-
...ZenUnicastDiscoveryTestsSpecificNodes.java | 8 +-
.../org/elasticsearch/document/BulkTests.java | 2 +-
.../local/LocalGatewayIndexStateTests.java | 3 +-
.../local/QuorumLocalGatewayTests.java | 3 +-
.../SimpleRecoveryLocalGatewayTests.java | 3 +-
.../gateway/none/RecoverAfterNodesTests.java | 3 +-
.../internal/InternalEngineMergeTests.java | 13 +-
.../suggest/stats/SuggestStatsTests.java | 4 +-
.../IndexLifecycleActionTests.java | 3 +-
.../IndicesLifecycleListenerTests.java | 2 +-
.../indices/analyze/HunspellServiceTests.java | 3 +-
.../indices/cache/CacheTests.java | 3 +-
.../indices/leaks/IndicesLeaksTests.java | 3 +-
.../DedicatedMasterGetFieldMappingTests.java | 10 +-
.../indices/recovery/IndexRecoveryTests.java | 3 +-
.../settings/UpdateNumberOfReplicasTests.java | 12 +-
.../state/CloseIndexDisableCloseAllTests.java | 3 +-
.../indices/stats/SimpleIndexStatsTests.java | 3 +-
.../indices/store/IndicesStoreTests.java | 3 +-
.../IndexTemplateFileLoadingTests.java | 3 +-
.../LocalGatewayIndicesWarmerTests.java | 3 +-
.../nodesinfo/SimpleNodesInfoTests.java | 3 +-
.../percolator/RecoveryPercolatorTests.java | 10 +-
.../plugin/PluginManagerTests.java | 3 +-
.../plugin/ResponseHeaderPluginTests.java | 5 +-
.../elasticsearch/plugin/SitePluginTests.java | 19 +-
.../recovery/FullRollingRestartTests.java | 11 +-
.../recovery/RelocationTests.java | 3 +-
.../script/ScriptFieldTests.java | 3 +-
.../basic/SearchWhileCreatingIndexTests.java | 4 +-
.../FunctionScorePluginTests.java | 2 +-
.../CustomHighlighterSearchTests.java | 2 +-
.../preference/SearchPreferenceTests.java | 14 +-
.../search/stats/SearchStatsTests.java | 2 +-
.../suggest/CustomSuggesterSearchTests.java | 3 +-
.../snapshots/AbstractSnapshotTests.java | 2 +-
.../DedicatedClusterSnapshotRestoreTests.java | 8 +-
.../SharedClusterSnapshotRestoreTests.java | 2 +-
.../test/ElasticsearchIntegrationTest.java | 65 +++---
.../test/ExternalTestCluster.java | 8 +-
.../test/ImmutableTestCluster.java | 2 +-
.../org/elasticsearch/test/TestCluster.java | 189 +++++++++++++-----
.../threadpool/SimpleThreadPoolTests.java | 5 +-
.../org/elasticsearch/tribe/TribeTests.java | 6 +-
.../org/elasticsearch/ttl/SimpleTTLTests.java | 3 +-
.../update/UpdateByNativeScriptTests.java | 3 +-
66 files changed, 410 insertions(+), 241 deletions(-)
diff --git a/rest-api-spec/test/cat.allocation/10_basic.yaml b/rest-api-spec/test/cat.allocation/10_basic.yaml
index ca9d5aa6e1a..3af10c3c66b 100644
--- a/rest-api-spec/test/cat.allocation/10_basic.yaml
+++ b/rest-api-spec/test/cat.allocation/10_basic.yaml
@@ -25,13 +25,13 @@
- match:
$body: >
/^
- ( 0 \s+
- \d+(\.\d+)?[kmgt]b \s+
- \d+(\.\d+)?[kmgt]b \s+
- \d+(\.\d+)?[kmgt]b \s+
- \d+ \s+
- [-\w.]+ \s+
- \d+(\.\d+){3} \s+
+ ( 0 \s+
+ \d+(\.\d+)?[kmgt]?b \s+
+ (\d+(\.\d+)?[kmgt]b)? \s+ #no value from client nodes
+ (\d+(\.\d+)?[kmgt]b)? \s+ #no value from client nodes
+ (\d+)? \s+ #no value from client nodes
+ [-\w.]+ \s+
+ \d+(\.\d+){3} \s+
\w.*
\n
)+
@@ -55,19 +55,19 @@
- match:
$body: >
/^
- ( \d+ \s+
- \d+(\.\d+)?[kmgt]b \s+
- \d+(\.\d+)?[kmgt]b \s+
- \d+(\.\d+)?[kmgt]b \s+
- \d+ \s+
- [-\w.]+ \s+
- \d+(\.\d+){3} \s+
+ ( \d+ \s+
+ \d+(\.\d+)?[kmgt]?b \s+
+ (\d+(\.\d+)?[kmgt]b)? \s+ #no value from client nodes
+ (\d+(\.\d+)?[kmgt]b)? \s+ #no value from client nodes
+ (\d+)? \s+ #no value from client nodes
+ [-\w.]+ \s+
+ \d+(\.\d+){3} \s+
\w.*
\n
)+
(
- \d+ \s+
- UNASSIGNED \s+
+ \d+ \s+
+ UNASSIGNED \s+
\n
)?
$/
@@ -82,13 +82,13 @@
- match:
$body: >
/^
- ( 0 \s+
- \d+(\.\d+)?[kmgt]b \s+
- \d+(\.\d+)?[kmgt]b \s+
- \d+(\.\d+)?[kmgt]b \s+
- \d+ \s+
- [-\w.]+ \s+
- \d+(\.\d+){3} \s+
+ ( 0 \s+
+ \d+(\.\d+)?[kmgt]?b \s+
+ (\d+(\.\d+)?[kmgt]b)? \s+ #no value from client nodes
+ (\d+(\.\d+)?[kmgt]b)? \s+ #no value from client nodes
+ (\d+)? \s+ #no value from client nodes
+ [-\w.]+ \s+
+ \d+(\.\d+){3} \s+
\w.*
\n
)
@@ -112,23 +112,23 @@
- match:
$body: >
- /^ shards \s+
- disk.used \s+
- disk.avail \s+
- disk.total \s+
- disk.percent \s+
- host \s+
- ip \s+
- node \s+
+ /^ shards \s+
+ disk.used \s+
+ disk.avail \s+
+ disk.total \s+
+ disk.percent \s+
+ host \s+
+ ip \s+
+ node \s+
\n
- ( \s+0 \s+
- \d+(\.\d+)?[kmgt]b \s+
- \d+(\.\d+)?[kmgt]b \s+
- \d+(\.\d+)?[kmgt]b \s+
- \d+ \s+
- [-\w.]+ \s+
- \d+(\.\d+){3} \s+
+ ( \s+0 \s+
+ \d+(\.\d+)?[kmgt]?b \s+
+ (\d+(\.\d+)?[kmgt]b)? \s+ #no value from client nodes
+ (\d+(\.\d+)?[kmgt]b)? \s+ #no value from client nodes
+ (\d+)? \s+ #no value from client nodes
+ [-\w.]+ \s+
+ \d+(\.\d+){3} \s+
\w.*
\n
)+
@@ -144,7 +144,7 @@
- match:
$body: >
/^
- ( \d+ \s+
+ ( \d* \s+
\w.*
\n
)+
@@ -162,7 +162,7 @@
node \s+
\n
(
- \s+\d+ \s+
+ \s+\d* \s+
\w.*
\n
)+
@@ -182,9 +182,9 @@
/^
( 0 \s+
\d+ \s+
- \d+ \s+
- \d+ \s+
- \d+ \s+
+ \d* \s+ #no value from client nodes
+ \d* \s+ #no value from client nodes
+ \d* \s+ #no value from client nodes
[-\w.]+ \s+
\d+(\.\d+){3} \s+
\w.*
diff --git a/src/test/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsTests.java b/src/test/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsTests.java
index f10aa46a180..edc7d532fe7 100644
--- a/src/test/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsTests.java
+++ b/src/test/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsTests.java
@@ -31,9 +31,10 @@ import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
import org.hamcrest.Matchers;
import org.junit.Test;
+import static org.elasticsearch.test.ElasticsearchIntegrationTest.*;
import static org.hamcrest.Matchers.is;
-@ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE, numNodes = 1)
+@ClusterScope(scope = Scope.SUITE, numDataNodes = 1, numClientNodes = 0)
public class ClusterStatsTests extends ElasticsearchIntegrationTest {
private void assertCounts(ClusterStatsNodes.Counts counts, int total, int masterOnly, int dataOnly, int masterData, int client) {
@@ -126,8 +127,8 @@ public class ClusterStatsTests extends ElasticsearchIntegrationTest {
@Test
public void testValuesSmokeScreen() {
- cluster().ensureAtMostNumNodes(5);
- cluster().ensureAtLeastNumNodes(1);
+ cluster().ensureAtMostNumDataNodes(5);
+ cluster().ensureAtLeastNumDataNodes(1);
SigarService sigarService = cluster().getInstance(SigarService.class);
index("test1", "type", "1", "f", "f");
/*
diff --git a/src/test/java/org/elasticsearch/action/bulk/BulkIntegrationTests.java b/src/test/java/org/elasticsearch/action/bulk/BulkIntegrationTests.java
index 5a3c3188fe2..777840d2b9c 100644
--- a/src/test/java/org/elasticsearch/action/bulk/BulkIntegrationTests.java
+++ b/src/test/java/org/elasticsearch/action/bulk/BulkIntegrationTests.java
@@ -25,12 +25,13 @@ import com.google.common.base.Predicate;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
-import static org.hamcrest.Matchers.*;
-import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath;
-
import org.junit.Test;
-@ElasticsearchIntegrationTest.ClusterScope(scope= ElasticsearchIntegrationTest.Scope.SUITE, numNodes=1)
+import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath;
+import static org.elasticsearch.test.ElasticsearchIntegrationTest.*;
+import static org.hamcrest.Matchers.equalTo;
+
+@ClusterScope(scope= Scope.SUITE, numDataNodes =1)
public class BulkIntegrationTests extends ElasticsearchIntegrationTest{
@Test
diff --git a/src/test/java/org/elasticsearch/client/transport/TransportClientTests.java b/src/test/java/org/elasticsearch/client/transport/TransportClientTests.java
index 087b7d7827b..863f231ce5a 100644
--- a/src/test/java/org/elasticsearch/client/transport/TransportClientTests.java
+++ b/src/test/java/org/elasticsearch/client/transport/TransportClientTests.java
@@ -25,7 +25,9 @@ import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
import org.hamcrest.Matchers;
import org.junit.Test;
-@ClusterScope(scope = ElasticsearchIntegrationTest.Scope.TEST, numNodes = 0, transportClientRatio = 1.0)
+import static org.elasticsearch.test.ElasticsearchIntegrationTest.*;
+
+@ClusterScope(scope = Scope.TEST, numDataNodes = 0, transportClientRatio = 1.0)
public class TransportClientTests extends ElasticsearchIntegrationTest {
@Test
diff --git a/src/test/java/org/elasticsearch/cluster/ClusterServiceTests.java b/src/test/java/org/elasticsearch/cluster/ClusterServiceTests.java
index 457a68ead98..3b9f0def993 100644
--- a/src/test/java/org/elasticsearch/cluster/ClusterServiceTests.java
+++ b/src/test/java/org/elasticsearch/cluster/ClusterServiceTests.java
@@ -44,12 +44,13 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
+import static org.elasticsearch.test.ElasticsearchIntegrationTest.*;
import static org.hamcrest.Matchers.*;
/**
*
*/
-@ClusterScope(scope = ElasticsearchIntegrationTest.Scope.TEST, numNodes = 0)
+@ClusterScope(scope = Scope.TEST, numDataNodes = 0)
public class ClusterServiceTests extends ElasticsearchIntegrationTest {
@Test
diff --git a/src/test/java/org/elasticsearch/cluster/MinimumMasterNodesTests.java b/src/test/java/org/elasticsearch/cluster/MinimumMasterNodesTests.java
index ee84546eed7..812b015937c 100644
--- a/src/test/java/org/elasticsearch/cluster/MinimumMasterNodesTests.java
+++ b/src/test/java/org/elasticsearch/cluster/MinimumMasterNodesTests.java
@@ -36,9 +36,10 @@ import java.util.concurrent.TimeUnit;
import static org.elasticsearch.client.Requests.clusterHealthRequest;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
+import static org.elasticsearch.test.ElasticsearchIntegrationTest.*;
import static org.hamcrest.Matchers.equalTo;
-@ClusterScope(scope = ElasticsearchIntegrationTest.Scope.TEST, numNodes=0)
+@ClusterScope(scope = Scope.TEST, numDataNodes =0)
public class MinimumMasterNodesTests extends ElasticsearchIntegrationTest {
@Test
@@ -276,7 +277,7 @@ public class MinimumMasterNodesTests extends ElasticsearchIntegrationTest {
}
logger.info("--> stopping a node");
- cluster().stopRandomNode();
+ cluster().stopRandomDataNode();
logger.info("--> verifying min master node has effect");
assertNoMasterBlockOnAllNodes();
diff --git a/src/test/java/org/elasticsearch/cluster/NoMasterNodeTests.java b/src/test/java/org/elasticsearch/cluster/NoMasterNodeTests.java
index 96e10afb906..9cf9569aae6 100644
--- a/src/test/java/org/elasticsearch/cluster/NoMasterNodeTests.java
+++ b/src/test/java/org/elasticsearch/cluster/NoMasterNodeTests.java
@@ -35,12 +35,13 @@ import org.junit.Test;
import java.util.HashMap;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
+import static org.elasticsearch.test.ElasticsearchIntegrationTest.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
/**
*/
-@ClusterScope(scope= ElasticsearchIntegrationTest.Scope.TEST, numNodes=0)
+@ClusterScope(scope= Scope.TEST, numDataNodes =0)
public class NoMasterNodeTests extends ElasticsearchIntegrationTest {
@Test
@@ -61,7 +62,7 @@ public class NoMasterNodeTests extends ElasticsearchIntegrationTest {
cluster().startNode(settings);
createIndex("test");
client().admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet();
- cluster().stopRandomNode();
+ cluster().stopRandomDataNode();
assertThat(awaitBusy(new Predicate
*/
public CreateIndexRequestBuilder prepareCreate(String index, int numNodes, ImmutableSettings.Builder settingsBuilder) {
- cluster().ensureAtLeastNumNodes(numNodes);
+ cluster().ensureAtLeastNumDataNodes(numNodes);
ImmutableSettings.Builder builder = ImmutableSettings.builder().put(indexSettings()).put(settingsBuilder.build());
@@ -605,7 +612,7 @@ public abstract class ElasticsearchIntegrationTest extends ElasticsearchTestCase
}
private ImmutableSettings.Builder getExcludeSettings(String index, int num, ImmutableSettings.Builder builder) {
- String exclude = Joiner.on(',').join(cluster().allButN(num));
+ String exclude = Joiner.on(',').join(cluster().allDataNodesButN(num));
builder.put("index.routing.allocation.exclude._name", exclude);
return builder;
}
@@ -617,7 +624,7 @@ public abstract class ElasticsearchIntegrationTest extends ElasticsearchTestCase
*/
public void allowNodes(String index, int n) {
assert index != null;
- cluster().ensureAtLeastNumNodes(n);
+ cluster().ensureAtLeastNumDataNodes(n);
ImmutableSettings.Builder builder = ImmutableSettings.builder();
if (n > 0) {
getExcludeSettings(index, n, builder);
@@ -1025,19 +1032,25 @@ public abstract class ElasticsearchIntegrationTest extends ElasticsearchTestCase
* a random number of nodes is used, where the minimum and maximum number of nodes
* are either the specified ones or the default ones if not specified.
*/
- int numNodes() default -1;
+ int numDataNodes() default -1;
/**
- * Returns the minimum number of nodes in the cluster. Default is {@link org.elasticsearch.test.TestCluster#DEFAULT_MIN_NUM_NODES}.
- * Ignored when {@link ClusterScope#numNodes()} is set.
+ * Returns the minimum number of nodes in the cluster. Default is {@link org.elasticsearch.test.TestCluster#DEFAULT_MIN_NUM_DATA_NODES}.
+ * Ignored when {@link ClusterScope#numDataNodes()} is set.
*/
- int minNumNodes() default TestCluster.DEFAULT_MIN_NUM_NODES;
+ int minNumDataNodes() default TestCluster.DEFAULT_MIN_NUM_DATA_NODES;
/**
- * Returns the maximum number of nodes in the cluster. Default is {@link org.elasticsearch.test.TestCluster#DEFAULT_MAX_NUM_NODES}.
- * Ignored when {@link ClusterScope#numNodes()} is set.
+ * Returns the maximum number of nodes in the cluster. Default is {@link org.elasticsearch.test.TestCluster#DEFAULT_MAX_NUM_DATA_NODES}.
+ * Ignored when {@link ClusterScope#numDataNodes()} is set.
*/
- int maxNumNodes() default TestCluster.DEFAULT_MAX_NUM_NODES;
+ int maxNumDataNodes() default TestCluster.DEFAULT_MAX_NUM_DATA_NODES;
+
+ /**
+ * Returns the number of client nodes in the cluster. Default is {@link org.elasticsearch.test.TestCluster#DEFAULT_NUM_CLIENT_NODES}, a
+ * negative value means that the number of client nodes will be randomized.
+ */
+ int numClientNodes() default TestCluster.DEFAULT_NUM_CLIENT_NODES;
/**
* Returns the transport client ratio. By default this returns -1 which means a random
@@ -1115,19 +1128,24 @@ public abstract class ElasticsearchIntegrationTest extends ElasticsearchTestCase
return annotation == null ? Scope.GLOBAL : annotation.scope();
}
- private int getNumNodes() {
+ private int getNumDataNodes() {
ClusterScope annotation = getAnnotation(this.getClass());
- return annotation == null ? -1 : annotation.numNodes();
+ return annotation == null ? -1 : annotation.numDataNodes();
}
- private int getMinNumNodes() {
+ private int getMinNumDataNodes() {
ClusterScope annotation = getAnnotation(this.getClass());
- return annotation == null ? TestCluster.DEFAULT_MIN_NUM_NODES : annotation.minNumNodes();
+ return annotation == null ? TestCluster.DEFAULT_MIN_NUM_DATA_NODES : annotation.minNumDataNodes();
}
- private int getMaxNumNodes() {
+ private int getMaxNumDataNodes() {
ClusterScope annotation = getAnnotation(this.getClass());
- return annotation == null ? TestCluster.DEFAULT_MAX_NUM_NODES : annotation.maxNumNodes();
+ return annotation == null ? TestCluster.DEFAULT_MAX_NUM_DATA_NODES : annotation.maxNumDataNodes();
+ }
+
+ private int getNumClientNodes() {
+ ClusterScope annotation = getAnnotation(this.getClass());
+ return annotation == null ? TestCluster.DEFAULT_NUM_CLIENT_NODES : annotation.numClientNodes();
}
/**
@@ -1143,7 +1161,7 @@ public abstract class ElasticsearchIntegrationTest extends ElasticsearchTestCase
private TestCluster buildTestCluster(Scope scope) {
long currentClusterSeed = randomLong();
- int numNodes = getNumNodes();
+ int numNodes = getNumDataNodes();
NodeSettingsSource nodeSettingsSource;
if (numNodes > 0) {
NodeSettingsSource.Immutable.Builder nodesSettings = NodeSettingsSource.Immutable.builder();
@@ -1164,11 +1182,12 @@ public abstract class ElasticsearchIntegrationTest extends ElasticsearchTestCase
if (numNodes >= 0) {
minNumNodes = maxNumNodes = numNodes;
} else {
- minNumNodes = getMinNumNodes();
- maxNumNodes = getMaxNumNodes();
+ minNumNodes = getMinNumDataNodes();
+ maxNumNodes = getMaxNumDataNodes();
}
- return new TestCluster(currentClusterSeed, minNumNodes, maxNumNodes, clusterName(scope.name(), ElasticsearchTestCase.CHILD_VM_ID, currentClusterSeed), nodeSettingsSource);
+ int numClientNodes = getNumClientNodes();
+ return new TestCluster(currentClusterSeed, minNumNodes, maxNumNodes, clusterName(scope.name(), ElasticsearchTestCase.CHILD_VM_ID, currentClusterSeed), nodeSettingsSource, numClientNodes);
}
/**
diff --git a/src/test/java/org/elasticsearch/test/ExternalTestCluster.java b/src/test/java/org/elasticsearch/test/ExternalTestCluster.java
index f5b4402b41e..e7160d94c1f 100644
--- a/src/test/java/org/elasticsearch/test/ExternalTestCluster.java
+++ b/src/test/java/org/elasticsearch/test/ExternalTestCluster.java
@@ -46,7 +46,7 @@ public final class ExternalTestCluster extends ImmutableTestCluster {
private final InetSocketAddress[] httpAddresses;
- private final int dataNodes;
+ private final int numDataNodes;
public ExternalTestCluster(TransportAddress... transportAddresses) {
this.client = new TransportClient(ImmutableSettings.settingsBuilder().put("client.transport.ignore_cluster_name", true))
@@ -62,7 +62,7 @@ public final class ExternalTestCluster extends ImmutableTestCluster {
dataNodes++;
}
}
- this.dataNodes = dataNodes;
+ this.numDataNodes = dataNodes;
logger.info("Setup ExternalTestCluster [{}] made of [{}] nodes", nodeInfos.getClusterName().value(), size());
}
@@ -82,8 +82,8 @@ public final class ExternalTestCluster extends ImmutableTestCluster {
}
@Override
- public int dataNodes() {
- return dataNodes;
+ public int numDataNodes() {
+ return numDataNodes;
}
@Override
diff --git a/src/test/java/org/elasticsearch/test/ImmutableTestCluster.java b/src/test/java/org/elasticsearch/test/ImmutableTestCluster.java
index d3801528f0a..e75f6cfef22 100644
--- a/src/test/java/org/elasticsearch/test/ImmutableTestCluster.java
+++ b/src/test/java/org/elasticsearch/test/ImmutableTestCluster.java
@@ -98,7 +98,7 @@ public abstract class ImmutableTestCluster implements Iterable {
/**
* Returns the number of data nodes in the cluster.
*/
- public abstract int dataNodes();
+ public abstract int numDataNodes();
/**
* Returns the http addresses of the nodes within the cluster.
diff --git a/src/test/java/org/elasticsearch/test/TestCluster.java b/src/test/java/org/elasticsearch/test/TestCluster.java
index d187626be0d..c700253057b 100644
--- a/src/test/java/org/elasticsearch/test/TestCluster.java
+++ b/src/test/java/org/elasticsearch/test/TestCluster.java
@@ -19,13 +19,11 @@
package org.elasticsearch.test;
import com.carrotsearch.randomizedtesting.SeedUtils;
+import com.carrotsearch.randomizedtesting.generators.RandomInts;
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
-import com.google.common.collect.Collections2;
-import com.google.common.collect.Iterators;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
+import com.google.common.collect.*;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
@@ -121,8 +119,12 @@ public final class TestCluster extends ImmutableTestCluster {
private static final boolean ENABLE_MOCK_MODULES = systemPropertyAsBoolean(TESTS_ENABLE_MOCK_MODULES, true);
- static final int DEFAULT_MIN_NUM_NODES = 2;
- static final int DEFAULT_MAX_NUM_NODES = 6;
+ static final int DEFAULT_MIN_NUM_DATA_NODES = 2;
+ static final int DEFAULT_MAX_NUM_DATA_NODES = 6;
+
+ static final int DEFAULT_NUM_CLIENT_NODES = -1;
+ static final int DEFAULT_MIN_NUM_CLIENT_NODES = 0;
+ static final int DEFAULT_MAX_NUM_CLIENT_NODES = 1;
/* sorted map to make traverse order reproducible, concurrent since we do checks on it not within a sync block */
private final NavigableMap nodes = new TreeMap<>();
@@ -142,39 +144,50 @@ public final class TestCluster extends ImmutableTestCluster {
* fully shared cluster to be more reproducible */
private final long[] sharedNodesSeeds;
+ private final int numSharedDataNodes;
+
+ private final int numSharedClientNodes;
+
private final NodeSettingsSource nodeSettingsSource;
private final ExecutorService executor;
public TestCluster(long clusterSeed, String clusterName) {
- this(clusterSeed, DEFAULT_MIN_NUM_NODES, DEFAULT_MAX_NUM_NODES, clusterName, NodeSettingsSource.EMPTY);
+ this(clusterSeed, DEFAULT_MIN_NUM_DATA_NODES, DEFAULT_MAX_NUM_DATA_NODES, clusterName, NodeSettingsSource.EMPTY, DEFAULT_NUM_CLIENT_NODES);
}
- public TestCluster(long clusterSeed, int minNumNodes, int maxNumNodes, String clusterName) {
- this(clusterSeed, minNumNodes, maxNumNodes, clusterName, NodeSettingsSource.EMPTY);
+ public TestCluster(long clusterSeed, int minNumDataNodes, int maxNumDataNodes, String clusterName, int numClientNodes) {
+ this(clusterSeed, minNumDataNodes, maxNumDataNodes, clusterName, NodeSettingsSource.EMPTY, numClientNodes);
}
- public TestCluster(long clusterSeed, int minNumNodes, int maxNumNodes, String clusterName, NodeSettingsSource nodeSettingsSource) {
+ public TestCluster(long clusterSeed, int minNumDataNodes, int maxNumDataNodes, String clusterName, NodeSettingsSource nodeSettingsSource, int numClientNodes) {
this.clusterName = clusterName;
- if (minNumNodes < 0 || maxNumNodes < 0) {
- throw new IllegalArgumentException("minimum and maximum number of nodes must be >= 0");
+ if (minNumDataNodes < 0 || maxNumDataNodes < 0) {
+ throw new IllegalArgumentException("minimum and maximum number of data nodes must be >= 0");
}
- if (maxNumNodes < minNumNodes) {
- throw new IllegalArgumentException("maximum number of nodes must be >= minimum number of nodes");
+ if (maxNumDataNodes < minNumDataNodes) {
+ throw new IllegalArgumentException("maximum number of data nodes must be >= minimum number of data nodes");
}
Random random = new Random(clusterSeed);
- int numSharedNodes;
- if (minNumNodes == maxNumNodes) {
- numSharedNodes = minNumNodes;
- } else {
- numSharedNodes = minNumNodes + random.nextInt(maxNumNodes - minNumNodes);
- }
+ this.numSharedDataNodes = RandomInts.randomIntBetween(random, minNumDataNodes, maxNumDataNodes);
+ assert this.numSharedDataNodes >= 0;
+
+ //for now all shared data nodes are also master eligible
+ if (numSharedDataNodes == 0) {
+ this.numSharedClientNodes = 0;
+ } else {
+ if (numClientNodes < 0) {
+ this.numSharedClientNodes = RandomInts.randomIntBetween(random, DEFAULT_MIN_NUM_CLIENT_NODES, DEFAULT_MAX_NUM_CLIENT_NODES);
+ } else {
+ this.numSharedClientNodes = numClientNodes;
+ }
+ }
+ assert this.numSharedClientNodes >=0;
- assert numSharedNodes >= 0;
/*
* TODO
* - we might want start some master only nodes?
@@ -182,11 +195,12 @@ public final class TestCluster extends ImmutableTestCluster {
* - we could add a flag that never returns a client to the master
* - along those lines use a dedicated node that is master eligible and let all other nodes be only data nodes
*/
- sharedNodesSeeds = new long[numSharedNodes];
+ sharedNodesSeeds = new long[numSharedDataNodes + numSharedClientNodes];
for (int i = 0; i < sharedNodesSeeds.length; i++) {
sharedNodesSeeds[i] = random.nextLong();
}
- logger.info("Setup TestCluster [{}] with seed [{}] using [{}] nodes", clusterName, SeedUtils.formatSeed(clusterSeed), numSharedNodes);
+
+ logger.info("Setup TestCluster [{}] with seed [{}] using [{}] data nodes and [{}] client nodes", clusterName, SeedUtils.formatSeed(clusterSeed), numSharedDataNodes, numSharedClientNodes);
this.nodeSettingsSource = nodeSettingsSource;
Builder builder = ImmutableSettings.settingsBuilder();
if (random.nextInt(5) == 0) { // sometimes set this
@@ -333,14 +347,14 @@ public final class TestCluster extends ImmutableTestCluster {
}
/**
- * Ensures that at least n nodes are present in the cluster.
+ * Ensures that at least n data nodes are present in the cluster.
* if more nodes than n are present this method will not
* stop any of the running nodes.
*/
- public void ensureAtLeastNumNodes(int n) {
+ public void ensureAtLeastNumDataNodes(int n) {
List> futures = Lists.newArrayList();
synchronized (this) {
- int size = nodes.size();
+ int size = numDataNodes();
for (int i = size; i < n; i++) {
logger.info("increasing cluster size from {} to {}", size, n);
futures.add(startNodeAsync());
@@ -358,14 +372,17 @@ public final class TestCluster extends ImmutableTestCluster {
* If less nodes that n are running this method
* will not start any additional nodes.
*/
- public synchronized void ensureAtMostNumNodes(int n) {
- if (nodes.size() <= n) {
+ public synchronized void ensureAtMostNumDataNodes(int n) {
+ int size = numDataNodes();
+ if (size <= n) {
return;
}
- // prevent killing the master if possible
- final Iterator values = n == 0 ? nodes.values().iterator() : Iterators.filter(nodes.values().iterator(), Predicates.not(new MasterNodePredicate(getMasterName())));
- final Iterator limit = Iterators.limit(values, nodes.size() - n);
- logger.info("changing cluster size from {} to {}", nodes.size() - n, n);
+ // prevent killing the master if possible and client nodes
+ final Iterator values = n == 0 ? nodes.values().iterator() : Iterators.filter(nodes.values().iterator(),
+ Predicates.and(new DataNodePredicate(), Predicates.not(new MasterNodePredicate(getMasterName()))));
+
+ final Iterator limit = Iterators.limit(values, size - n);
+ logger.info("changing cluster size from {} to {}, {} data nodes", size(), n + numSharedClientNodes, n);
Set nodesToRemove = new HashSet<>();
while (limit.hasNext()) {
NodeAndClient next = limit.next();
@@ -414,6 +431,16 @@ public final class TestCluster extends ImmutableTestCluster {
return getOrBuildRandomNode().client(random);
}
+ /**
+ * Returns a node client to a data node in the cluster.
+ * Note: use this with care tests should not rely on a certain nodes client.
+ */
+ public synchronized Client dataNodeClient() {
+ ensureOpen();
+ /* Randomly return a client to one of the nodes in the cluster */
+ return getRandomNodeAndClient(new DataNodePredicate()).client(random);
+ }
+
/**
* Returns a node client to the current master node.
* Note: use this with care tests should not rely on a certain nodes client.
@@ -707,8 +734,9 @@ public final class TestCluster extends ImmutableTestCluster {
Set sharedNodes = new HashSet<>();
+ assert sharedNodesSeeds.length == numSharedDataNodes + numSharedClientNodes;
boolean changed = false;
- for (int i = 0; i < sharedNodesSeeds.length; i++) {
+ for (int i = 0; i < numSharedDataNodes; i++) {
String buildNodeName = buildNodeName(i);
NodeAndClient nodeAndClient = nodes.get(buildNodeName);
if (nodeAndClient == null) {
@@ -719,6 +747,18 @@ public final class TestCluster extends ImmutableTestCluster {
}
sharedNodes.add(nodeAndClient);
}
+ for (int i = numSharedDataNodes; i < numSharedDataNodes + numSharedClientNodes; i++) {
+ String buildNodeName = buildNodeName(i);
+ NodeAndClient nodeAndClient = nodes.get(buildNodeName);
+ if (nodeAndClient == null) {
+ changed = true;
+ Settings clientSettings = ImmutableSettings.builder().put("node.data", "false").put("node.master", "false").build();
+ nodeAndClient = buildNode(i, sharedNodesSeeds[i], clientSettings, Version.CURRENT);
+ nodeAndClient.node.start();
+ logger.info("Start Shared Node [{}] not shared", nodeAndClient.name);
+ }
+ sharedNodes.add(nodeAndClient);
+ }
if (!changed && sharedNodes.size() == nodes.size()) {
logger.debug("Cluster is consistent - moving out - nodes: [{}] nextNodeId: [{}] numSharedNodes: [{}]", nodes.keySet(), nextNodeId.get(), sharedNodesSeeds.length);
if (size() > 0) {
@@ -791,6 +831,22 @@ public final class TestCluster extends ImmutableTestCluster {
return instances;
}
+ /**
+ * Returns an Iterable to all instances for the given class >T< across all data nodes in the cluster.
+ */
+ public synchronized Iterable getDataNodeInstances(Class clazz) {
+ return getInstances(clazz, new DataNodePredicate());
+ }
+
+ private synchronized Iterable getInstances(Class clazz, Predicate predicate) {
+ Iterable filteredNodes = Iterables.filter(nodes.values(), predicate);
+ List instances = new ArrayList<>();
+ for (NodeAndClient nodeAndClient : filteredNodes) {
+ instances.add(getInstanceFromNode(clazz, nodeAndClient.node));
+ }
+ return instances;
+ }
+
/**
* Returns a reference to the given nodes instances of the given class >T<
*/
@@ -805,6 +861,14 @@ public final class TestCluster extends ImmutableTestCluster {
} else {
predicate = Predicates.alwaysTrue();
}
+ return getInstance(clazz, predicate);
+ }
+
+ public synchronized T getDataNodeInstance(Class clazz) {
+ return getInstance(clazz, new DataNodePredicate());
+ }
+
+ private synchronized T getInstance(Class clazz, Predicate predicate) {
NodeAndClient randomNodeAndClient = getRandomNodeAndClient(predicate);
assert randomNodeAndClient != null;
return getInstanceFromNode(clazz, randomNodeAndClient.node);
@@ -814,7 +878,7 @@ public final class TestCluster extends ImmutableTestCluster {
* Returns a reference to a random nodes instances of the given class >T<
*/
public synchronized T getInstance(Class clazz) {
- return getInstance(clazz, null);
+ return getInstance(clazz, Predicates.alwaysTrue());
}
private synchronized T getInstanceFromNode(Class clazz, InternalNode node) {
@@ -836,11 +900,11 @@ public final class TestCluster extends ImmutableTestCluster {
}
/**
- * Stops a random node in the cluster.
+ * Stops a random data node in the cluster.
*/
- public synchronized void stopRandomNode() {
+ public synchronized void stopRandomDataNode() {
ensureOpen();
- NodeAndClient nodeAndClient = getRandomNodeAndClient();
+ NodeAndClient nodeAndClient = getRandomNodeAndClient(new DataNodePredicate());
if (nodeAndClient != null) {
logger.info("Closing random node [{}] ", nodeAndClient.name);
nodes.remove(nodeAndClient.name);
@@ -899,13 +963,33 @@ public final class TestCluster extends ImmutableTestCluster {
restartRandomNode(EMPTY_CALLBACK);
}
-
/**
* Restarts a random node in the cluster and calls the callback during restart.
*/
public void restartRandomNode(RestartCallback callback) throws Exception {
+ restartRandomNode(Predicates.alwaysTrue(), callback);
+ }
+
+ /**
+ * Restarts a random data node in the cluster
+ */
+ public void restartRandomDataNode() throws Exception {
+ restartRandomNode(EMPTY_CALLBACK);
+ }
+
+ /**
+ * Restarts a random data node in the cluster and calls the callback during restart.
+ */
+ public void restartRandomDataNode(RestartCallback callback) throws Exception {
+ restartRandomNode(new DataNodePredicate(), callback);
+ }
+
+ /**
+ * Restarts a random node in the cluster and calls the callback during restart.
+ */
+ private void restartRandomNode(Predicate predicate, RestartCallback callback) throws Exception {
ensureOpen();
- NodeAndClient nodeAndClient = getRandomNodeAndClient();
+ NodeAndClient nodeAndClient = getRandomNodeAndClient(predicate);
if (nodeAndClient != null) {
logger.info("Restarting random node [{}] ", nodeAndClient.name);
nodeAndClient.restart(callback);
@@ -996,13 +1080,14 @@ public final class TestCluster extends ImmutableTestCluster {
}
}
- synchronized Set allButN(int numNodes) {
- return nRandomNodes(size() - numNodes);
+ synchronized Set allDataNodesButN(int numNodes) {
+ return nRandomDataNodes(numDataNodes() - numNodes);
}
- private synchronized Set nRandomNodes(int numNodes) {
+ private synchronized Set nRandomDataNodes(int numNodes) {
assert size() >= numNodes;
- return Sets.newHashSet(Iterators.limit(this.nodes.keySet().iterator(), numNodes));
+ NavigableMap dataNodes = Maps.filterEntries(nodes, new EntryNodePredicate(new DataNodePredicate()));
+ return Sets.newHashSet(Iterators.limit(dataNodes.keySet().iterator(), numNodes));
}
public synchronized void startNodeClient(Settings settings) {
@@ -1154,11 +1239,11 @@ public final class TestCluster extends ImmutableTestCluster {
}
@Override
- public int dataNodes() {
+ public int numDataNodes() {
return dataNodeAndClients().size();
}
- private Collection dataNodeAndClients() {
+ private synchronized Collection dataNodeAndClients() {
return Collections2.filter(nodes.values(), new DataNodePredicate());
}
@@ -1183,13 +1268,25 @@ public final class TestCluster extends ImmutableTestCluster {
}
private static final class ClientNodePredicate implements Predicate {
-
@Override
public boolean apply(NodeAndClient nodeAndClient) {
return nodeAndClient.node.settings().getAsBoolean("node.client", false);
}
}
+ private static final class EntryNodePredicate implements Predicate> {
+ private final Predicate delegateNodePredicate;
+
+ EntryNodePredicate(Predicate delegateNodePredicate) {
+ this.delegateNodePredicate = delegateNodePredicate;
+ }
+
+ @Override
+ public boolean apply(Map.Entry entry) {
+ return delegateNodePredicate.apply(entry.getValue());
+ }
+ }
+
@Override
public synchronized Iterator iterator() {
ensureOpen();
diff --git a/src/test/java/org/elasticsearch/threadpool/SimpleThreadPoolTests.java b/src/test/java/org/elasticsearch/threadpool/SimpleThreadPoolTests.java
index 74eb7b26cf8..3dd2fdf451e 100644
--- a/src/test/java/org/elasticsearch/threadpool/SimpleThreadPoolTests.java
+++ b/src/test/java/org/elasticsearch/threadpool/SimpleThreadPoolTests.java
@@ -38,11 +38,12 @@ import java.util.Map;
import java.util.concurrent.*;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
+import static org.elasticsearch.test.ElasticsearchIntegrationTest.*;
import static org.hamcrest.Matchers.*;
/**
*/
-@ClusterScope(scope= ElasticsearchIntegrationTest.Scope.TEST, numNodes=2)
+@ClusterScope(scope= Scope.TEST, numDataNodes =2)
public class SimpleThreadPoolTests extends ElasticsearchIntegrationTest {
@Override
@@ -52,7 +53,7 @@ public class SimpleThreadPoolTests extends ElasticsearchIntegrationTest {
@Test(timeout = 20000)
public void testUpdatingThreadPoolSettings() throws Exception {
- ThreadPool threadPool = cluster().getInstance(ThreadPool.class);
+ ThreadPool threadPool = cluster().getDataNodeInstance(ThreadPool.class);
// Check that settings are changed
assertThat(((ThreadPoolExecutor) threadPool.executor(Names.SEARCH)).getKeepAliveTime(TimeUnit.MINUTES), equalTo(5L));
client().admin().cluster().prepareUpdateSettings().setTransientSettings(settingsBuilder().put("threadpool.search.keep_alive", "10m").build()).execute().actionGet();
diff --git a/src/test/java/org/elasticsearch/tribe/TribeTests.java b/src/test/java/org/elasticsearch/tribe/TribeTests.java
index dbc0e5de2ec..b35763be8e0 100644
--- a/src/test/java/org/elasticsearch/tribe/TribeTests.java
+++ b/src/test/java/org/elasticsearch/tribe/TribeTests.java
@@ -58,9 +58,9 @@ public class TribeTests extends ElasticsearchIntegrationTest {
public static void setupSecondCluster() throws Exception {
ElasticsearchIntegrationTest.beforeClass();
// create another cluster
- cluster2 = new TestCluster(randomLong(), 2, 2, Strings.randomBase64UUID(getRandom()));
+ cluster2 = new TestCluster(randomLong(), 2, 2, Strings.randomBase64UUID(getRandom()), 0);
cluster2.beforeTest(getRandom(), 0.1);
- cluster2.ensureAtLeastNumNodes(2);
+ cluster2.ensureAtLeastNumDataNodes(2);
}
@AfterClass
@@ -301,7 +301,7 @@ public class TribeTests extends ElasticsearchIntegrationTest {
});
logger.info("stop a node, make sure its reflected");
- cluster2.stopRandomNode();
+ cluster2.stopRandomDataNode();
awaitSameNodeCounts();
}
diff --git a/src/test/java/org/elasticsearch/ttl/SimpleTTLTests.java b/src/test/java/org/elasticsearch/ttl/SimpleTTLTests.java
index d167c147cae..2703ef3bb98 100644
--- a/src/test/java/org/elasticsearch/ttl/SimpleTTLTests.java
+++ b/src/test/java/org/elasticsearch/ttl/SimpleTTLTests.java
@@ -32,10 +32,11 @@ import org.junit.Test;
import java.util.concurrent.TimeUnit;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
+import static org.elasticsearch.test.ElasticsearchIntegrationTest.*;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.*;
-@ClusterScope(scope= ElasticsearchIntegrationTest.Scope.SUITE, numNodes = 1)
+@ClusterScope(scope= Scope.SUITE, numDataNodes = 1)
public class SimpleTTLTests extends ElasticsearchIntegrationTest {
static private final long PURGE_INTERVAL = 200;
diff --git a/src/test/java/org/elasticsearch/update/UpdateByNativeScriptTests.java b/src/test/java/org/elasticsearch/update/UpdateByNativeScriptTests.java
index b80b5468880..7e97b0bde32 100644
--- a/src/test/java/org/elasticsearch/update/UpdateByNativeScriptTests.java
+++ b/src/test/java/org/elasticsearch/update/UpdateByNativeScriptTests.java
@@ -31,13 +31,14 @@ import org.junit.Test;
import java.util.Map;
+import static org.elasticsearch.test.ElasticsearchIntegrationTest.*;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;
/**
*
*/
-@ClusterScope(scope= ElasticsearchIntegrationTest.Scope.SUITE, numNodes=1)
+@ClusterScope(scope= Scope.SUITE, numDataNodes =1)
public class UpdateByNativeScriptTests extends ElasticsearchIntegrationTest {
@Override