diff --git a/server/src/main/java/org/elasticsearch/action/support/ActiveShardCount.java b/server/src/main/java/org/elasticsearch/action/support/ActiveShardCount.java index f6934e8d20ae6..b5195cf001751 100644 --- a/server/src/main/java/org/elasticsearch/action/support/ActiveShardCount.java +++ b/server/src/main/java/org/elasticsearch/action/support/ActiveShardCount.java @@ -8,10 +8,11 @@ package org.elasticsearch.action.support; -import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.routing.IndexRoutingTable; import org.elasticsearch.cluster.routing.IndexShardRoutingTable; +import org.elasticsearch.cluster.routing.RoutingTable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; @@ -122,21 +123,21 @@ public boolean enoughShardsActive(final int activeShardCount) { * Returns true iff the given cluster state's routing table contains enough active * shards for the given indices to meet the required shard count represented by this instance. */ - public boolean enoughShardsActive(final ClusterState clusterState, final String... indices) { + public boolean enoughShardsActive(final Metadata metadata, final RoutingTable routingTable, final String... indices) { if (this == ActiveShardCount.NONE) { // not waiting for any active shards return true; } for (final String indexName : indices) { - final IndexMetadata indexMetadata = clusterState.metadata().index(indexName); + final IndexMetadata indexMetadata = metadata.index(indexName); if (indexMetadata == null) { // its possible the index was deleted while waiting for active shard copies, // in this case, we'll just consider it that we have enough active shard copies // and we can stop waiting continue; } - final IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(indexName); + final IndexRoutingTable indexRoutingTable = routingTable.index(indexName); if (indexRoutingTable == null && indexMetadata.getState() == IndexMetadata.State.CLOSE) { // its possible the index was closed while waiting for active shard copies, // in this case, we'll just consider it that we have enough active shard copies diff --git a/server/src/main/java/org/elasticsearch/action/support/ActiveShardsObserver.java b/server/src/main/java/org/elasticsearch/action/support/ActiveShardsObserver.java index ffe8bde231f22..edaf0d67443b5 100644 --- a/server/src/main/java/org/elasticsearch/action/support/ActiveShardsObserver.java +++ b/server/src/main/java/org/elasticsearch/action/support/ActiveShardsObserver.java @@ -54,7 +54,7 @@ public static void waitForActiveShards( } final ClusterState state = clusterService.state(); - if (activeShardCount.enoughShardsActive(state, indexNames)) { + if (activeShardCount.enoughShardsActive(state.metadata(), state.routingTable(), indexNames)) { listener.onResponse(true); return; } @@ -82,7 +82,7 @@ public void onTimeout(TimeValue timeout) { listener.onResponse(false); } }, - newState -> activeShardCount.enoughShardsActive(newState, indexNames), + newState -> activeShardCount.enoughShardsActive(newState.metadata(), newState.routingTable(), indexNames), timeout ); } diff --git a/server/src/test/java/org/elasticsearch/action/support/ActiveShardCountTests.java b/server/src/test/java/org/elasticsearch/action/support/ActiveShardCountTests.java index e9fe24c53d729..627ad629b9527 100644 --- a/server/src/test/java/org/elasticsearch/action/support/ActiveShardCountTests.java +++ b/server/src/test/java/org/elasticsearch/action/support/ActiveShardCountTests.java @@ -87,11 +87,11 @@ public void testEnoughShardsActiveZero() { final int numberOfReplicas = randomIntBetween(4, 7); final ActiveShardCount waitForActiveShards = ActiveShardCount.NONE; ClusterState clusterState = initializeWithNewIndex(indexName, numberOfShards, numberOfReplicas); - assertTrue(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertTrue(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startPrimaries(clusterState, indexName); - assertTrue(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertTrue(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startAllShards(clusterState, indexName); - assertTrue(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertTrue(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); } public void testEnoughShardsActiveLevelOne() { @@ -109,13 +109,13 @@ public void testEnoughShardsActiveLevelDefaultWithSearchOnlyRole() { final int numberOfReplicas = randomIntBetween(4, 7); final ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT; ClusterState clusterState = initializeWithNewIndex(indexName, numberOfShards, numberOfReplicas, createCustomRoleStrategy(1)); - assertFalse(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertFalse(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startPrimaries(clusterState, indexName); - assertFalse(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertFalse(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startLessThanWaitOnShards(clusterState, indexName, 1); - assertTrue(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertTrue(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startAllShards(clusterState, indexName); - assertTrue(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertTrue(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); } public void testEnoughShardsActiveCustomLevelWithSearchOnlyRole() { @@ -125,15 +125,15 @@ public void testEnoughShardsActiveCustomLevelWithSearchOnlyRole() { final int activeShardCount = randomIntBetween(2, numberOfReplicas); final ActiveShardCount waitForActiveShards = ActiveShardCount.from(activeShardCount); ClusterState clusterState = initializeWithNewIndex(indexName, numberOfShards, numberOfReplicas, createCustomRoleStrategy(1)); - assertFalse(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertFalse(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startPrimaries(clusterState, indexName); - assertFalse(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertFalse(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startLessThanWaitOnShards(clusterState, indexName, activeShardCount - 2); - assertFalse(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertFalse(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startWaitOnShards(clusterState, indexName, activeShardCount); - assertTrue(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertTrue(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startAllShards(clusterState, indexName); - assertTrue(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertTrue(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); } public void testEnoughShardsActiveWithNoSearchOnlyRoles() { @@ -147,13 +147,13 @@ public void testEnoughShardsActiveWithNoSearchOnlyRoles() { numberOfReplicas, createCustomRoleStrategy(numberOfReplicas + 1) ); - assertFalse(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertFalse(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startPrimaries(clusterState, indexName); - assertTrue(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertTrue(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startLessThanWaitOnShards(clusterState, indexName, 1); - assertTrue(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertTrue(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startAllShards(clusterState, indexName); - assertTrue(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertTrue(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); } private static ShardRoutingRoleStrategy createCustomRoleStrategy(int indexShardCount) { @@ -177,15 +177,15 @@ public void testEnoughShardsActiveRandom() { final int activeShardCount = randomIntBetween(2, numberOfReplicas); final ActiveShardCount waitForActiveShards = ActiveShardCount.from(activeShardCount); ClusterState clusterState = initializeWithNewIndex(indexName, numberOfShards, numberOfReplicas); - assertFalse(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertFalse(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startPrimaries(clusterState, indexName); - assertFalse(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertFalse(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startLessThanWaitOnShards(clusterState, indexName, activeShardCount - 2); - assertFalse(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertFalse(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startWaitOnShards(clusterState, indexName, activeShardCount - 1); - assertTrue(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertTrue(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startAllShards(clusterState, indexName); - assertTrue(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertTrue(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); } public void testEnoughShardsActiveLevelAll() { @@ -195,13 +195,13 @@ public void testEnoughShardsActiveLevelAll() { // both values should represent "all" final ActiveShardCount waitForActiveShards = randomBoolean() ? ActiveShardCount.from(numberOfReplicas + 1) : ActiveShardCount.ALL; ClusterState clusterState = initializeWithNewIndex(indexName, numberOfShards, numberOfReplicas); - assertFalse(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertFalse(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startPrimaries(clusterState, indexName); - assertFalse(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertFalse(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startLessThanWaitOnShards(clusterState, indexName, numberOfReplicas - randomIntBetween(1, numberOfReplicas)); - assertFalse(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertFalse(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startAllShards(clusterState, indexName); - assertTrue(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertTrue(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); } public void testEnoughShardsActiveValueBased() { @@ -230,7 +230,7 @@ public void testEnoughShardsActiveWithClosedIndex() { final ClusterState clusterState = initializeWithClosedIndex(indexName, numberOfShards, numberOfReplicas); for (ActiveShardCount waitForActiveShards : Arrays.asList(ActiveShardCount.DEFAULT, ActiveShardCount.ALL, ActiveShardCount.ONE)) { - assertTrue(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertTrue(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); } } @@ -240,11 +240,11 @@ private void runTestForOneActiveShard(final ActiveShardCount waitForActiveShards final int numberOfReplicas = randomIntBetween(4, 7); assert waitForActiveShards == ActiveShardCount.ONE || waitForActiveShards == ActiveShardCount.DEFAULT; ClusterState clusterState = initializeWithNewIndex(indexName, numberOfShards, numberOfReplicas); - assertFalse(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertFalse(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startPrimaries(clusterState, indexName); - assertTrue(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertTrue(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); clusterState = startAllShards(clusterState, indexName); - assertTrue(waitForActiveShards.enoughShardsActive(clusterState, indexName)); + assertTrue(waitForActiveShards.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), indexName)); } private ClusterState initializeWithNewIndex(String indexName, int numShards, int numReplicas) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AllocationRoutedStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AllocationRoutedStep.java index 7cdef6207c487..ba24756c9ed41 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AllocationRoutedStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AllocationRoutedStep.java @@ -52,7 +52,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) { logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), index.getName()); return new Result(false, null); } - if (ActiveShardCount.ALL.enoughShardsActive(clusterState, index.getName()) == false) { + if (ActiveShardCount.ALL.enoughShardsActive(clusterState.metadata(), clusterState.routingTable(), index.getName()) == false) { logger.debug( "[{}] lifecycle action for index [{}] cannot make progress because not all shards are active", getKey().action(), diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStep.java index 855b579e8843b..54c955ba10793 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStep.java @@ -12,6 +12,7 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.DesiredNodes; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders; import org.elasticsearch.index.Index; import org.elasticsearch.xpack.cluster.routing.allocation.DataTierAllocationDecider; @@ -46,7 +47,8 @@ public boolean isRetryable() { @Override public Result isConditionMet(Index index, ClusterState clusterState) { - IndexMetadata idxMeta = clusterState.metadata().index(index); + Metadata metadata = clusterState.metadata(); + IndexMetadata idxMeta = metadata.index(index); if (idxMeta == null) { // Index must have been since deleted, ignore it logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), index.getName()); @@ -57,10 +59,10 @@ public Result isConditionMet(Index index, ClusterState clusterState) { preferredTierConfiguration, clusterState.getNodes(), DesiredNodes.latestFromClusterState(clusterState), - clusterState.metadata().nodeShutdowns() + metadata.nodeShutdowns() ); - if (ActiveShardCount.ALL.enoughShardsActive(clusterState, index.getName()) == false) { + if (ActiveShardCount.ALL.enoughShardsActive(metadata, clusterState.routingTable(), index.getName()) == false) { if (preferredTierConfiguration.isEmpty()) { logger.debug( "[{}] lifecycle action for index [{}] cannot make progress because not all shards are active", diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStep.java index 4a29a9e310061..705d3d8d2105a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStep.java @@ -12,6 +12,8 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; +import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.routing.RoutingTable; import org.elasticsearch.common.Strings; import org.elasticsearch.index.Index; import org.elasticsearch.xcontent.ConstructingObjectParser; @@ -43,7 +45,9 @@ public boolean isRetryable() { @Override public Result isConditionMet(Index index, ClusterState clusterState) { - IndexMetadata indexMetadata = clusterState.metadata().index(index); + Metadata metadata = clusterState.metadata(); + RoutingTable routingTable = clusterState.routingTable(); + IndexMetadata indexMetadata = metadata.index(index); if (indexMetadata == null) { // Index must have been since deleted, ignore it logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), index.getName()); @@ -55,12 +59,12 @@ public Result isConditionMet(Index index, ClusterState clusterState) { // We only want to make progress if all shards of the shrunk index are // active - boolean indexExists = clusterState.metadata().index(shrunkenIndexName) != null; + boolean indexExists = metadata.index(shrunkenIndexName) != null; if (indexExists == false) { return new Result(false, new Info(false, -1, false)); } - boolean allShardsActive = ActiveShardCount.ALL.enoughShardsActive(clusterState, shrunkenIndexName); - int numShrunkIndexShards = clusterState.metadata().index(shrunkenIndexName).getNumberOfShards(); + boolean allShardsActive = ActiveShardCount.ALL.enoughShardsActive(metadata, routingTable, shrunkenIndexName); + int numShrunkIndexShards = metadata.index(shrunkenIndexName).getNumberOfShards(); if (allShardsActive) { return new Result(true, null); } else { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsStep.java index 71c99d7f21848..394856e4ddc12 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsStep.java @@ -15,6 +15,7 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.routing.IndexRoutingTable; +import org.elasticsearch.cluster.routing.RoutingTable; import org.elasticsearch.common.Strings; import org.elasticsearch.index.Index; import org.elasticsearch.xcontent.ParseField; @@ -51,6 +52,7 @@ public boolean isRetryable() { @Override public Result isConditionMet(Index index, ClusterState clusterState) { Metadata metadata = clusterState.metadata(); + RoutingTable routingTable = clusterState.routingTable(); IndexMetadata originalIndexMeta = metadata.index(index); if (originalIndexMeta == null) { @@ -137,9 +139,9 @@ public Result isConditionMet(Index index, ClusterState clusterState) { } ActiveShardCount activeShardCount = ActiveShardCount.parseString(waitForActiveShardsSettingValue); - boolean enoughShardsActive = activeShardCount.enoughShardsActive(clusterState, rolledIndexName); + boolean enoughShardsActive = activeShardCount.enoughShardsActive(metadata, routingTable, rolledIndexName); - IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(rolledIndexName); + IndexRoutingTable indexRoutingTable = routingTable.index(rolledIndexName); int currentActiveShards = 0; for (int i = 0; i < indexRoutingTable.size(); i++) { currentActiveShards += indexRoutingTable.shard(i).activeShards().size();