From bf13c88cf208a57a6ed476e26992ae0aac0fa87b Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Fri, 8 Feb 2019 16:27:41 +0100 Subject: [PATCH 1/3] Adapt ClusterAllocationExplainIT to also test closed indices --- .../cluster.allocation_explain/10_basic.yml | 44 +++++ .../ClusterAllocationExplainIT.java | 168 ++++++++++++++---- 2 files changed, 176 insertions(+), 36 deletions(-) diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/cluster.allocation_explain/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/cluster.allocation_explain/10_basic.yml index 7dbc57dac8b56..510a872f2b5b9 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/cluster.allocation_explain/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/cluster.allocation_explain/10_basic.yml @@ -53,3 +53,47 @@ - match: { primary: false } - is_true: cluster_info - is_true: can_allocate + + +--- +"Cluster shard allocation explanation test with a closed index": + - skip: + version: " - 7.99.99" + reason: closed indices are replicated starting version 8.0.0 + + - do: + indices.create: + index: test_closed + body: { "settings": { "index.number_of_shards": 1, "index.number_of_replicas": 0 } } + + - match: { acknowledged: true } + + - do: + cluster.health: + index: test_closed + wait_for_status: green + + - do: + indices.close: + index: test_closed + + - match: { acknowledged: true } + + - do: + cluster.health: + index: test_closed + wait_for_status: green + + - do: + cluster.allocation_explain: + body: { "index": "test_closed", "shard": 0, "primary": true } + + - match: { current_state: "started" } + - is_true: current_node.id + - match: { index: "test_closed" } + - match: { shard: 0 } + - match: { primary: true } + - is_true: can_remain_on_current_node + - is_true: can_rebalance_cluster + - is_true: can_rebalance_to_other_node + - is_true: rebalance_explanation diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java index e38fc64c8e3ab..2b80a7667424c 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java @@ -19,9 +19,12 @@ package org.elasticsearch.action.admin.cluster.allocation; +import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.cluster.ClusterInfo; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.health.ClusterHealthStatus; +import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.routing.ShardRoutingState; import org.elasticsearch.cluster.routing.UnassignedInfo; @@ -32,8 +35,10 @@ import org.elasticsearch.cluster.routing.allocation.MoveDecision; import org.elasticsearch.cluster.routing.allocation.NodeAllocationResult; import org.elasticsearch.cluster.routing.allocation.decider.Decision; +import org.elasticsearch.common.Priority; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -52,6 +57,7 @@ import java.util.Map; import java.util.Set; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; @@ -66,12 +72,19 @@ @ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0) public final class ClusterAllocationExplainIT extends ESIntegTestCase { - public void testUnassignedPrimaryWithExistingIndex() throws Exception { + public void testUnassignedPrimaryWithExistingOpenedIndex() throws Exception { + runTestUnassignedPrimaryWithExistingIndex(IndexMetaData.State.OPEN); + } + + public void testUnassignedPrimaryWithExistingClosedIndex() throws Exception { + runTestUnassignedPrimaryWithExistingIndex(IndexMetaData.State.CLOSE); + } + + private void runTestUnassignedPrimaryWithExistingIndex(final IndexMetaData.State indexState) throws Exception { logger.info("--> starting 2 nodes"); internalCluster().startNodes(2); - logger.info("--> creating an index with 1 primary, 0 replicas"); - createIndexAndIndexData(1, 0); + prepareIndex(indexState, 1, 0); logger.info("--> stopping the node with the primary"); internalCluster().stopRandomNode(InternalTestCluster.nameFilter(primaryNodeName())); @@ -145,12 +158,19 @@ public void testUnassignedPrimaryWithExistingIndex() throws Exception { } } - public void testUnassignedReplicaDelayedAllocation() throws Exception { + public void testUnassignedReplicaDelayedAllocationForOpenedIndex() throws Exception { + runTestUnassignedReplicaDelayedAllocation(IndexMetaData.State.OPEN); + } + + public void testUnassignedReplicaDelayedAllocationForClosedIndex() throws Exception { + runTestUnassignedReplicaDelayedAllocation(IndexMetaData.State.CLOSE); + } + + private void runTestUnassignedReplicaDelayedAllocation(final IndexMetaData.State indexState) throws Exception { logger.info("--> starting 3 nodes"); internalCluster().startNodes(3); - logger.info("--> creating an index with 1 primary, 1 replica"); - createIndexAndIndexData(1, 1); + prepareIndex(indexState, 1, 1); logger.info("--> stopping the node with the replica"); internalCluster().stopRandomNode(InternalTestCluster.nameFilter(replicaNode().getName())); ensureStableCluster(2); @@ -264,12 +284,19 @@ public void testUnassignedReplicaDelayedAllocation() throws Exception { } } - public void testUnassignedReplicaWithPriorCopy() throws Exception { + public void testUnassignedReplicaWithPriorCopyForOpenedIndex() throws Exception { + runTestUnassignedReplicaWithPriorCopy(IndexMetaData.State.OPEN); + } + + public void testUnassignedReplicaWithPriorCopyForClosedIndex() throws Exception { + runTestUnassignedReplicaWithPriorCopy(IndexMetaData.State.CLOSE); + } + + private void runTestUnassignedReplicaWithPriorCopy(final IndexMetaData.State indexState) throws Exception { logger.info("--> starting 3 nodes"); List nodes = internalCluster().startNodes(3); - logger.info("--> creating an index with 1 primary and 1 replica"); - createIndexAndIndexData(1, 1); + prepareIndex(indexState, 1, 1); String primaryNodeName = primaryNodeName(); nodes.remove(primaryNodeName); @@ -390,8 +417,8 @@ public void testAllocationFilteringOnIndexCreation() throws Exception { internalCluster().startNodes(2); logger.info("--> creating an index with 1 primary, 0 replicas, with allocation filtering so the primary can't be assigned"); - createIndexAndIndexData(1, 0, Settings.builder().put("index.routing.allocation.include._name", "non_existent_node").build(), - ActiveShardCount.NONE); + prepareIndex(IndexMetaData.State.OPEN, 1, 0, + Settings.builder().put("index.routing.allocation.include._name", "non_existent_node").build(), ActiveShardCount.NONE); boolean includeYesDecisions = randomBoolean(); boolean includeDiskInfo = randomBoolean(); @@ -477,12 +504,19 @@ public void testAllocationFilteringOnIndexCreation() throws Exception { } } - public void testAllocationFilteringPreventsShardMove() throws Exception { + public void testAllocationFilteringPreventsShardMoveForOpenedIndex() throws Exception { + runTestAllocationFilteringPreventsShardMove(IndexMetaData.State.OPEN); + } + + public void testAllocationFilteringPreventsShardMoveForClosedIndex() throws Exception { + runTestAllocationFilteringPreventsShardMove(IndexMetaData.State.CLOSE); + } + + private void runTestAllocationFilteringPreventsShardMove(final IndexMetaData.State indexState) throws Exception { logger.info("--> starting 2 nodes"); internalCluster().startNodes(2); - logger.info("--> creating an index with 1 primary and 0 replicas"); - createIndexAndIndexData(1, 0); + prepareIndex(indexState, 1, 0); logger.info("--> setting up allocation filtering to prevent allocation to both nodes"); client().admin().indices().prepareUpdateSettings("idx").setSettings( @@ -586,13 +620,21 @@ public void testAllocationFilteringPreventsShardMove() throws Exception { } } - public void testRebalancingNotAllowed() throws Exception { + public void testRebalancingNotAllowedForOpenedIndex() throws Exception { + runTestRebalancingNotAllowed(IndexMetaData.State.OPEN); + } + + public void testRebalancingNotAllowedForClosedIndex() throws Exception { + runTestRebalancingNotAllowed(IndexMetaData.State.CLOSE); + } + + private void runTestRebalancingNotAllowed(final IndexMetaData.State indexState) throws Exception { logger.info("--> starting a single node"); internalCluster().startNode(); ensureStableCluster(1); logger.info("--> creating an index with 5 shards, all allocated to the single node"); - createIndexAndIndexData(5, 0); + prepareIndex(indexState, 5, 0); logger.info("--> disabling rebalancing on the index"); client().admin().indices().prepareUpdateSettings("idx").setSettings( @@ -699,13 +741,20 @@ public void testRebalancingNotAllowed() throws Exception { } } - public void testWorseBalance() throws Exception { + public void testWorseBalanceForOpenedIndex() throws Exception { + runTestWorseBalance(IndexMetaData.State.OPEN); + } + + public void testWorseBalanceForClosedIndex() throws Exception { + runTestWorseBalance(IndexMetaData.State.CLOSE); + } + + private void runTestWorseBalance(final IndexMetaData.State indexState) throws Exception { logger.info("--> starting a single node"); internalCluster().startNode(); ensureStableCluster(1); - logger.info("--> creating an index with 5 shards, all allocated to the single node"); - createIndexAndIndexData(5, 0); + prepareIndex(indexState, 5, 0); logger.info("--> setting balancing threshold really high, so it won't be met"); client().admin().cluster().prepareUpdateSettings().setTransientSettings( @@ -803,13 +852,20 @@ public void testWorseBalance() throws Exception { } } - public void testBetterBalanceButCannotAllocate() throws Exception { + public void testBetterBalanceButCannotAllocateForOpenedIndex() throws Exception { + runTestBetterBalanceButCannotAllocate(IndexMetaData.State.OPEN); + } + + public void testBetterBalanceButCannotAllocateForClosedIndex() throws Exception { + runTestBetterBalanceButCannotAllocate(IndexMetaData.State.CLOSE); + } + + private void runTestBetterBalanceButCannotAllocate(final IndexMetaData.State indexState) throws Exception { logger.info("--> starting a single node"); String firstNode = internalCluster().startNode(); ensureStableCluster(1); - logger.info("--> creating an index with 5 shards, all allocated to the single node"); - createIndexAndIndexData(5, 0); + prepareIndex(indexState, 5, 0); logger.info("--> setting up allocation filtering to only allow allocation to the current node"); client().admin().indices().prepareUpdateSettings("idx").setSettings( @@ -914,14 +970,21 @@ public void testBetterBalanceButCannotAllocate() throws Exception { } } - public void testAssignedReplicaOnSpecificNode() throws Exception { + public void testAssignedReplicaOnSpecificNodeForOpenedIndex() throws Exception { + runTestAssignedReplicaOnSpecificNode(IndexMetaData.State.OPEN); + } + + public void testAssignedReplicaOnSpecificNodeForClosedIndex() throws Exception { + runTestAssignedReplicaOnSpecificNode(IndexMetaData.State.CLOSE); + } + + private void runTestAssignedReplicaOnSpecificNode(final IndexMetaData.State indexState) throws Exception { logger.info("--> starting 3 nodes"); List nodes = internalCluster().startNodes(3); - logger.info("--> creating an index with 1 primary and 2 replicas"); String excludedNode = nodes.get(randomIntBetween(0, 2)); - createIndexAndIndexData(1, 2, Settings.builder().put("index.routing.allocation.exclude._name", excludedNode).build(), - ActiveShardCount.from(2)); + prepareIndex(indexState, 1, 2, + Settings.builder().put("index.routing.allocation.exclude._name", excludedNode).build(), ActiveShardCount.from(2)); boolean includeYesDecisions = randomBoolean(); boolean includeDiskInfo = randomBoolean(); @@ -1011,7 +1074,15 @@ public void testAssignedReplicaOnSpecificNode() throws Exception { } } - public void testCannotAllocateStaleReplicaExplanation() throws Exception { + public void testCannotAllocateStaleReplicaExplanationForOpenedIndex() throws Exception { + runTestCannotAllocateStaleReplicaExplanation(IndexMetaData.State.OPEN); + } + + public void testCannotAllocateStaleReplicaExplanationForClosedIndex() throws Exception { + runTestCannotAllocateStaleReplicaExplanation(IndexMetaData.State.CLOSE); + } + + private void runTestCannotAllocateStaleReplicaExplanation(final IndexMetaData.State indexState) throws Exception { logger.info("--> starting 3 nodes"); final String masterNode = internalCluster().startNode(); // start replica node first, so it's path will be used first when we start a node after @@ -1019,8 +1090,7 @@ public void testCannotAllocateStaleReplicaExplanation() throws Exception { final String replicaNode = internalCluster().startNode(); final String primaryNode = internalCluster().startNode(); - logger.info("--> creating an index with 1 primary and 1 replica"); - createIndexAndIndexData(1, 1, + prepareIndex(IndexMetaData.State.OPEN, 1, 1, Settings.builder() .put("index.routing.allocation.include._name", primaryNode) .put("index.routing.allocation.exclude._name", masterNode) @@ -1037,8 +1107,20 @@ public void testCannotAllocateStaleReplicaExplanation() throws Exception { logger.info("--> stop node with the replica shard"); internalCluster().stopRandomNode(InternalTestCluster.nameFilter(replicaNode)); - logger.info("--> index more data, now the replica is stale"); - indexData(); + if (indexState == IndexMetaData.State.OPEN) { + logger.info("--> index more data, now the replica is stale"); + indexData(); + } else { + logger.info("--> close the index, now the replica is stale"); + assertAcked(client().admin().indices().prepareClose("idx")); + + final ClusterHealthResponse clusterHealthResponse = client().admin().cluster().prepareHealth("idx") + .setTimeout(TimeValue.timeValueSeconds(30)) + .setWaitForActiveShards(ActiveShardCount.ONE) + .setWaitForEvents(Priority.LANGUID) + .get(); + assertThat(clusterHealthResponse.getStatus().value(), lessThanOrEqualTo(ClusterHealthStatus.YELLOW.value())); + } logger.info("--> stop the node with the primary"); internalCluster().stopRandomNode(InternalTestCluster.nameFilter(primaryNode)); @@ -1147,21 +1229,35 @@ private ClusterAllocationExplanation runExplain(boolean primary, String nodeId, return explanation; } - private void createIndexAndIndexData(int numPrimaries, int numReplicas) { - createIndexAndIndexData(numPrimaries, numReplicas, Settings.EMPTY, ActiveShardCount.ALL); + private void prepareIndex(final IndexMetaData.State state, final int numPrimaries, final int numReplicas) { + prepareIndex(state, numPrimaries, numReplicas, Settings.EMPTY, ActiveShardCount.ALL); } - private void createIndexAndIndexData(int numPrimaries, int numReplicas, Settings settings, ActiveShardCount activeShardCount) { - client().admin().indices().prepareCreate("idx") + private void prepareIndex(final IndexMetaData.State state, final int numPrimaries, final int numReplicas, + final Settings settings, final ActiveShardCount activeShardCount) { + + logger.info("--> creating a {} index with {} primary, {} replicas", state, numPrimaries, numReplicas); + assertAcked(client().admin().indices().prepareCreate("idx") .setSettings(Settings.builder() .put("index.number_of_shards", numPrimaries) .put("index.number_of_replicas", numReplicas) .put(settings)) .setWaitForActiveShards(activeShardCount) - .get(); + .get()); + if (activeShardCount != ActiveShardCount.NONE) { indexData(); } + if (state == IndexMetaData.State.CLOSE) { + assertAcked(client().admin().indices().prepareClose("idx")); + + final ClusterHealthResponse clusterHealthResponse = client().admin().cluster().prepareHealth("idx") + .setTimeout(TimeValue.timeValueSeconds(30)) + .setWaitForActiveShards(activeShardCount) + .setWaitForEvents(Priority.LANGUID) + .get(); + assertThat(clusterHealthResponse.getStatus().value(), lessThanOrEqualTo(ClusterHealthStatus.YELLOW.value())); + } } private void indexData() { From df139d67c539b431488f2649981e9d6ed1954020 Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Sat, 9 Feb 2019 12:25:42 +0100 Subject: [PATCH 2/3] Apply feedback --- .../ClusterAllocationExplainIT.java | 122 ++++-------------- 1 file changed, 28 insertions(+), 94 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java index 2b80a7667424c..32fffacfb4269 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java @@ -72,19 +72,11 @@ @ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0) public final class ClusterAllocationExplainIT extends ESIntegTestCase { - public void testUnassignedPrimaryWithExistingOpenedIndex() throws Exception { - runTestUnassignedPrimaryWithExistingIndex(IndexMetaData.State.OPEN); - } - - public void testUnassignedPrimaryWithExistingClosedIndex() throws Exception { - runTestUnassignedPrimaryWithExistingIndex(IndexMetaData.State.CLOSE); - } - - private void runTestUnassignedPrimaryWithExistingIndex(final IndexMetaData.State indexState) throws Exception { + public void testUnassignedPrimaryWithExistingIndex() throws Exception { logger.info("--> starting 2 nodes"); internalCluster().startNodes(2); - prepareIndex(indexState, 1, 0); + prepareIndex(1, 0); logger.info("--> stopping the node with the primary"); internalCluster().stopRandomNode(InternalTestCluster.nameFilter(primaryNodeName())); @@ -158,19 +150,11 @@ private void runTestUnassignedPrimaryWithExistingIndex(final IndexMetaData.State } } - public void testUnassignedReplicaDelayedAllocationForOpenedIndex() throws Exception { - runTestUnassignedReplicaDelayedAllocation(IndexMetaData.State.OPEN); - } - - public void testUnassignedReplicaDelayedAllocationForClosedIndex() throws Exception { - runTestUnassignedReplicaDelayedAllocation(IndexMetaData.State.CLOSE); - } - - private void runTestUnassignedReplicaDelayedAllocation(final IndexMetaData.State indexState) throws Exception { + public void testUnassignedReplicaDelayedAllocation() throws Exception { logger.info("--> starting 3 nodes"); internalCluster().startNodes(3); - prepareIndex(indexState, 1, 1); + prepareIndex(1, 1); logger.info("--> stopping the node with the replica"); internalCluster().stopRandomNode(InternalTestCluster.nameFilter(replicaNode().getName())); ensureStableCluster(2); @@ -284,19 +268,11 @@ private void runTestUnassignedReplicaDelayedAllocation(final IndexMetaData.State } } - public void testUnassignedReplicaWithPriorCopyForOpenedIndex() throws Exception { - runTestUnassignedReplicaWithPriorCopy(IndexMetaData.State.OPEN); - } - - public void testUnassignedReplicaWithPriorCopyForClosedIndex() throws Exception { - runTestUnassignedReplicaWithPriorCopy(IndexMetaData.State.CLOSE); - } - - private void runTestUnassignedReplicaWithPriorCopy(final IndexMetaData.State indexState) throws Exception { + public void testUnassignedReplicaWithPriorCopy() throws Exception { logger.info("--> starting 3 nodes"); List nodes = internalCluster().startNodes(3); - prepareIndex(indexState, 1, 1); + prepareIndex(1, 1); String primaryNodeName = primaryNodeName(); nodes.remove(primaryNodeName); @@ -418,7 +394,8 @@ public void testAllocationFilteringOnIndexCreation() throws Exception { logger.info("--> creating an index with 1 primary, 0 replicas, with allocation filtering so the primary can't be assigned"); prepareIndex(IndexMetaData.State.OPEN, 1, 0, - Settings.builder().put("index.routing.allocation.include._name", "non_existent_node").build(), ActiveShardCount.NONE); + Settings.builder().put("index.routing.allocation.include._name", "non_existent_node").build(), + ActiveShardCount.NONE); boolean includeYesDecisions = randomBoolean(); boolean includeDiskInfo = randomBoolean(); @@ -504,19 +481,11 @@ public void testAllocationFilteringOnIndexCreation() throws Exception { } } - public void testAllocationFilteringPreventsShardMoveForOpenedIndex() throws Exception { - runTestAllocationFilteringPreventsShardMove(IndexMetaData.State.OPEN); - } - - public void testAllocationFilteringPreventsShardMoveForClosedIndex() throws Exception { - runTestAllocationFilteringPreventsShardMove(IndexMetaData.State.CLOSE); - } - - private void runTestAllocationFilteringPreventsShardMove(final IndexMetaData.State indexState) throws Exception { + public void testAllocationFilteringPreventsShardMove() throws Exception { logger.info("--> starting 2 nodes"); internalCluster().startNodes(2); - prepareIndex(indexState, 1, 0); + prepareIndex(1, 0); logger.info("--> setting up allocation filtering to prevent allocation to both nodes"); client().admin().indices().prepareUpdateSettings("idx").setSettings( @@ -620,21 +589,12 @@ private void runTestAllocationFilteringPreventsShardMove(final IndexMetaData.Sta } } - public void testRebalancingNotAllowedForOpenedIndex() throws Exception { - runTestRebalancingNotAllowed(IndexMetaData.State.OPEN); - } - - public void testRebalancingNotAllowedForClosedIndex() throws Exception { - runTestRebalancingNotAllowed(IndexMetaData.State.CLOSE); - } - - private void runTestRebalancingNotAllowed(final IndexMetaData.State indexState) throws Exception { + public void testRebalancingNotAllowed() throws Exception { logger.info("--> starting a single node"); internalCluster().startNode(); ensureStableCluster(1); - logger.info("--> creating an index with 5 shards, all allocated to the single node"); - prepareIndex(indexState, 5, 0); + prepareIndex(5, 0); logger.info("--> disabling rebalancing on the index"); client().admin().indices().prepareUpdateSettings("idx").setSettings( @@ -741,20 +701,12 @@ private void runTestRebalancingNotAllowed(final IndexMetaData.State indexState) } } - public void testWorseBalanceForOpenedIndex() throws Exception { - runTestWorseBalance(IndexMetaData.State.OPEN); - } - - public void testWorseBalanceForClosedIndex() throws Exception { - runTestWorseBalance(IndexMetaData.State.CLOSE); - } - - private void runTestWorseBalance(final IndexMetaData.State indexState) throws Exception { + public void testWorseBalance() throws Exception { logger.info("--> starting a single node"); internalCluster().startNode(); ensureStableCluster(1); - prepareIndex(indexState, 5, 0); + prepareIndex(5, 0); logger.info("--> setting balancing threshold really high, so it won't be met"); client().admin().cluster().prepareUpdateSettings().setTransientSettings( @@ -852,20 +804,12 @@ private void runTestWorseBalance(final IndexMetaData.State indexState) throws Ex } } - public void testBetterBalanceButCannotAllocateForOpenedIndex() throws Exception { - runTestBetterBalanceButCannotAllocate(IndexMetaData.State.OPEN); - } - - public void testBetterBalanceButCannotAllocateForClosedIndex() throws Exception { - runTestBetterBalanceButCannotAllocate(IndexMetaData.State.CLOSE); - } - - private void runTestBetterBalanceButCannotAllocate(final IndexMetaData.State indexState) throws Exception { + public void testBetterBalanceButCannotAllocate() throws Exception { logger.info("--> starting a single node"); String firstNode = internalCluster().startNode(); ensureStableCluster(1); - prepareIndex(indexState, 5, 0); + prepareIndex(5, 0); logger.info("--> setting up allocation filtering to only allow allocation to the current node"); client().admin().indices().prepareUpdateSettings("idx").setSettings( @@ -970,21 +914,14 @@ private void runTestBetterBalanceButCannotAllocate(final IndexMetaData.State ind } } - public void testAssignedReplicaOnSpecificNodeForOpenedIndex() throws Exception { - runTestAssignedReplicaOnSpecificNode(IndexMetaData.State.OPEN); - } - - public void testAssignedReplicaOnSpecificNodeForClosedIndex() throws Exception { - runTestAssignedReplicaOnSpecificNode(IndexMetaData.State.CLOSE); - } - - private void runTestAssignedReplicaOnSpecificNode(final IndexMetaData.State indexState) throws Exception { + public void testAssignedReplicaOnSpecificNode() throws Exception { logger.info("--> starting 3 nodes"); List nodes = internalCluster().startNodes(3); String excludedNode = nodes.get(randomIntBetween(0, 2)); - prepareIndex(indexState, 1, 2, - Settings.builder().put("index.routing.allocation.exclude._name", excludedNode).build(), ActiveShardCount.from(2)); + prepareIndex(randomIndexState(), 1, 2, + Settings.builder().put("index.routing.allocation.exclude._name", excludedNode).build(), + ActiveShardCount.from(2)); boolean includeYesDecisions = randomBoolean(); boolean includeDiskInfo = randomBoolean(); @@ -1074,15 +1011,7 @@ private void runTestAssignedReplicaOnSpecificNode(final IndexMetaData.State inde } } - public void testCannotAllocateStaleReplicaExplanationForOpenedIndex() throws Exception { - runTestCannotAllocateStaleReplicaExplanation(IndexMetaData.State.OPEN); - } - - public void testCannotAllocateStaleReplicaExplanationForClosedIndex() throws Exception { - runTestCannotAllocateStaleReplicaExplanation(IndexMetaData.State.CLOSE); - } - - private void runTestCannotAllocateStaleReplicaExplanation(final IndexMetaData.State indexState) throws Exception { + public void testCannotAllocateStaleReplicaExplanation() throws Exception { logger.info("--> starting 3 nodes"); final String masterNode = internalCluster().startNode(); // start replica node first, so it's path will be used first when we start a node after @@ -1107,6 +1036,7 @@ private void runTestCannotAllocateStaleReplicaExplanation(final IndexMetaData.St logger.info("--> stop node with the replica shard"); internalCluster().stopRandomNode(InternalTestCluster.nameFilter(replicaNode)); + final IndexMetaData.State indexState = randomIndexState(); if (indexState == IndexMetaData.State.OPEN) { logger.info("--> index more data, now the replica is stale"); indexData(); @@ -1229,8 +1159,8 @@ private ClusterAllocationExplanation runExplain(boolean primary, String nodeId, return explanation; } - private void prepareIndex(final IndexMetaData.State state, final int numPrimaries, final int numReplicas) { - prepareIndex(state, numPrimaries, numReplicas, Settings.EMPTY, ActiveShardCount.ALL); + private void prepareIndex(final int numPrimaries, final int numReplicas) { + prepareIndex(randomIndexState(), numPrimaries, numReplicas, Settings.EMPTY, ActiveShardCount.ALL); } private void prepareIndex(final IndexMetaData.State state, final int numPrimaries, final int numReplicas, @@ -1260,6 +1190,10 @@ private void prepareIndex(final IndexMetaData.State state, final int numPrimarie } } + private static IndexMetaData.State randomIndexState() { + return randomFrom(IndexMetaData.State.values()); + } + private void indexData() { for (int i = 0; i < 10; i++) { index("idx", "t", Integer.toString(i), Collections.singletonMap("f1", Integer.toString(i))); From 5484f6d5f632fda628c3d636dc074556cf300e7a Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Mon, 11 Feb 2019 11:45:49 +0100 Subject: [PATCH 3/3] Add setWaitForNoInitializingShards --- .../admin/cluster/allocation/ClusterAllocationExplainIT.java | 1 + 1 file changed, 1 insertion(+) diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java index 32fffacfb4269..941ad3c658aba 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java @@ -1047,6 +1047,7 @@ public void testCannotAllocateStaleReplicaExplanation() throws Exception { final ClusterHealthResponse clusterHealthResponse = client().admin().cluster().prepareHealth("idx") .setTimeout(TimeValue.timeValueSeconds(30)) .setWaitForActiveShards(ActiveShardCount.ONE) + .setWaitForNoInitializingShards(true) .setWaitForEvents(Priority.LANGUID) .get(); assertThat(clusterHealthResponse.getStatus().value(), lessThanOrEqualTo(ClusterHealthStatus.YELLOW.value()));