diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index 97a0d6b46d6b3..33cce5822c361 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -2829,4 +2829,11 @@ public static IndexSearcher newSearcher(IndexReader r, boolean maybeWrap, boolea public static ProjectState projectStateFromProject(ProjectMetadata.Builder project) { return ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(project).build().projectState(project.getId()); } + + /** + * Constructs an empty {@link ProjectState} with one (empty) project. + */ + public static ProjectState projectStateWithEmptyProject() { + return projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault())); + } } 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 6922ee4cef3e4..5998f4cfebfe2 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 @@ -9,7 +9,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.support.ActiveShardCount; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.routing.IndexRoutingTable; import org.elasticsearch.cluster.routing.IndexShardRoutingTable; @@ -45,18 +45,14 @@ public boolean isRetryable() { } @Override - public Result isConditionMet(Index index, ClusterState clusterState) { - IndexMetadata idxMeta = clusterState.metadata().getProject().index(index); + public Result isConditionMet(Index index, ProjectState currentState) { + IndexMetadata idxMeta = currentState.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()); return new Result(false, null); } - if (ActiveShardCount.ALL.enoughShardsActive( - clusterState.metadata().getProject(), - clusterState.routingTable(), - index.getName() - ) == false) { + if (ActiveShardCount.ALL.enoughShardsActive(currentState.metadata(), currentState.routingTable(), index.getName()) == false) { logger.debug( "[{}] lifecycle action for index [{}] cannot make progress because not all shards are active", getKey().action(), @@ -68,12 +64,12 @@ public Result isConditionMet(Index index, ClusterState clusterState) { AllocationDeciders allocationDeciders = new AllocationDeciders( List.of( new FilterAllocationDecider( - clusterState.getMetadata().settings(), + currentState.cluster().metadata().settings(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS) ) ) ); - int allocationPendingAllShards = getPendingAllocations(index, allocationDeciders, clusterState); + int allocationPendingAllShards = getPendingAllocations(index, allocationDeciders, currentState); if (allocationPendingAllShards > 0) { logger.debug( @@ -89,14 +85,14 @@ public Result isConditionMet(Index index, ClusterState clusterState) { } } - static int getPendingAllocations(Index index, AllocationDeciders allocationDeciders, ClusterState clusterState) { + static int getPendingAllocations(Index index, AllocationDeciders allocationDeciders, ProjectState currentState) { // All the allocation attributes are already set so just need to check // if the allocation has happened - RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, clusterState, null, null, System.nanoTime()); + RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, currentState.cluster(), null, null, System.nanoTime()); int allocationPendingAllShards = 0; - final IndexRoutingTable indexRoutingTable = clusterState.getRoutingTable().index(index); + final IndexRoutingTable indexRoutingTable = currentState.routingTable().index(index); for (int shardId = 0; shardId < indexRoutingTable.size(); shardId++) { final IndexShardRoutingTable indexShardRoutingTable = indexRoutingTable.shard(shardId); for (int copy = 0; copy < indexShardRoutingTable.size(); copy++) { @@ -104,7 +100,7 @@ static int getPendingAllocations(Index index, AllocationDeciders allocationDecid String currentNodeId = shardRouting.currentNodeId(); boolean canRemainOnCurrentNode = allocationDeciders.canRemain( shardRouting, - clusterState.getRoutingNodes().node(currentNodeId), + currentState.cluster().getRoutingNodes().node(currentNodeId), allocation ).type() == Decision.Type.YES; if (canRemainOnCurrentNode == false || shardRouting.started() == false) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckNotDataStreamWriteIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckNotDataStreamWriteIndexStep.java index d597ffeefbd5d..5cc7257981679 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckNotDataStreamWriteIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckNotDataStreamWriteIndexStep.java @@ -8,11 +8,10 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.DataStream; import org.elasticsearch.cluster.metadata.IndexAbstraction; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.Strings; import org.elasticsearch.index.Index; import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo; @@ -39,9 +38,8 @@ public boolean isRetryable() { } @Override - public Result isConditionMet(Index index, ClusterState clusterState) { - Metadata metadata = clusterState.metadata(); - IndexMetadata indexMetadata = metadata.getProject().index(index); + public Result isConditionMet(Index index, ProjectState currentState) { + IndexMetadata indexMetadata = currentState.metadata().index(index); String indexName = index.getName(); if (indexMetadata == null) { @@ -56,7 +54,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) { } String policyName = indexMetadata.getLifecyclePolicyName(); - IndexAbstraction indexAbstraction = clusterState.metadata().getProject().getIndicesLookup().get(indexName); + IndexAbstraction indexAbstraction = currentState.metadata().getIndicesLookup().get(indexName); assert indexAbstraction != null : "invalid cluster metadata. index [" + indexName + "] was not found"; DataStream dataStream = indexAbstraction.getParentDataStream(); if (dataStream != null) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckShrinkReadyStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckShrinkReadyStep.java index b1c93bcb29236..3bf6744a93f68 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckShrinkReadyStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckShrinkReadyStep.java @@ -9,7 +9,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.SingleNodeShutdownMetadata; import org.elasticsearch.cluster.routing.IndexRoutingTable; @@ -51,8 +51,8 @@ public boolean isCompletable() { } @Override - public Result isConditionMet(Index index, ClusterState clusterState) { - IndexMetadata idxMeta = clusterState.metadata().getProject().index(index); + public Result isConditionMet(Index index, ProjectState currentState) { + IndexMetadata idxMeta = currentState.metadata().index(index); if (idxMeta == null) { // Index must have been since deleted, ignore it @@ -69,10 +69,10 @@ public Result isConditionMet(Index index, ClusterState clusterState) { throw new IllegalStateException("Cannot check shrink allocation as there are no allocation rules by _id"); } - var shutdown = clusterState.metadata().nodeShutdowns().get(idShardsShouldBeOn); + var shutdown = currentState.cluster().metadata().nodeShutdowns().get(idShardsShouldBeOn); boolean nodeBeingRemoved = shutdown != null && shutdown.getType() != SingleNodeShutdownMetadata.Type.RESTART; - final IndexRoutingTable routingTable = clusterState.getRoutingTable().index(index); + final IndexRoutingTable routingTable = currentState.routingTable().index(index); int foundShards = 0; for (ShardRouting shard : routingTable.shardsWithState(ShardRoutingState.STARTED)) { final String currentNodeId = shard.currentNodeId(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStep.java index 6377e758b7681..dd31084b6a1ac 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStep.java @@ -8,7 +8,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.index.Index; @@ -40,8 +40,8 @@ public Integer getNumberOfShards() { } @Override - public Result isConditionMet(Index index, ClusterState clusterState) { - IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index); + public Result isConditionMet(Index index, ProjectState currentState) { + IndexMetadata indexMetadata = currentState.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()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitStep.java index 4ed83fa170ead..f45fb62d7236f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitStep.java @@ -6,7 +6,7 @@ */ package org.elasticsearch.xpack.core.ilm; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.index.Index; import org.elasticsearch.xcontent.ToXContentObject; @@ -21,7 +21,7 @@ public ClusterStateWaitStep(StepKey key, StepKey nextStepKey) { super(key, nextStepKey); } - public abstract Result isConditionMet(Index index, ClusterState clusterState); + public abstract Result isConditionMet(Index index, ProjectState currentState); /** * Whether the step can be completed at all. This only affects the diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStep.java index eb1d520abad0e..96b0df1448516 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStep.java @@ -9,7 +9,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; import org.elasticsearch.common.Strings; @@ -52,15 +52,15 @@ public boolean isRetryable() { } @Override - public Result isConditionMet(Index index, ClusterState clusterState) { - IndexMetadata idxMeta = clusterState.metadata().getProject().index(index); + public Result isConditionMet(Index index, ProjectState currentState) { + IndexMetadata idxMeta = currentState.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()); return new Result(false, null); } - Result stepResult = stepToExecute.isConditionMet(index, clusterState); + Result stepResult = stepToExecute.isConditionMet(index, currentState); if (stepResult.complete() == false) { // checking the threshold after we execute the step to make sure we execute the wrapped step at least once (because time is a 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 26dd4224e8e3b..7a6d2a29796e1 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 @@ -9,7 +9,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.support.ActiveShardCount; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.DesiredNodes; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders; @@ -45,8 +45,8 @@ public boolean isRetryable() { } @Override - public Result isConditionMet(Index index, ClusterState clusterState) { - IndexMetadata idxMeta = clusterState.metadata().getProject().index(index); + public Result isConditionMet(Index index, ProjectState currentState) { + IndexMetadata idxMeta = currentState.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()); @@ -55,16 +55,12 @@ public Result isConditionMet(Index index, ClusterState clusterState) { List preferredTierConfiguration = idxMeta.getTierPreference(); Optional availableDestinationTier = DataTierAllocationDecider.preferredAvailableTier( preferredTierConfiguration, - clusterState.getNodes(), - DesiredNodes.latestFromClusterState(clusterState), - clusterState.metadata().nodeShutdowns() + currentState.cluster().getNodes(), + DesiredNodes.latestFromClusterState(currentState.cluster()), + currentState.cluster().metadata().nodeShutdowns() ); - if (ActiveShardCount.ALL.enoughShardsActive( - clusterState.metadata().getProject(), - clusterState.routingTable(), - index.getName() - ) == false) { + if (ActiveShardCount.ALL.enoughShardsActive(currentState.metadata(), currentState.routingTable(), index.getName()) == false) { if (preferredTierConfiguration.isEmpty()) { logger.debug( "[{}] lifecycle action for index [{}] cannot make progress because not all shards are active", @@ -103,7 +99,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) { return new Result(true, null); } - int allocationPendingAllShards = getPendingAllocations(index, DECIDERS, clusterState); + int allocationPendingAllShards = getPendingAllocations(index, DECIDERS, currentState); if (allocationPendingAllShards > 0) { String statusMessage = availableDestinationTier.map( diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/NoopStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/NoopStep.java index 00abccca0790a..9be070c35345f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/NoopStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/NoopStep.java @@ -6,7 +6,7 @@ */ package org.elasticsearch.xpack.core.ilm; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.index.Index; /** @@ -27,7 +27,7 @@ public boolean isRetryable() { } @Override - public Result isConditionMet(Index index, ClusterState clusterState) { + public Result isConditionMet(Index index, ProjectState currentState) { // We always want to move forward with this step so this should always be true return new Result(true, null); } 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 763e1c1f5ebfc..3e69ecdee7728 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 @@ -9,7 +9,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.support.ActiveShardCount; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; import org.elasticsearch.common.Strings; @@ -42,8 +42,8 @@ public boolean isRetryable() { } @Override - public Result isConditionMet(Index index, ClusterState clusterState) { - IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index); + public Result isConditionMet(Index index, ProjectState currentState) { + IndexMetadata indexMetadata = currentState.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,16 +55,16 @@ 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().getProject().index(shrunkenIndexName) != null; + boolean indexExists = currentState.metadata().index(shrunkenIndexName) != null; if (indexExists == false) { return new Result(false, new Info(false, -1, false)); } boolean allShardsActive = ActiveShardCount.ALL.enoughShardsActive( - clusterState.metadata().getProject(), - clusterState.routingTable(), + currentState.metadata(), + currentState.routingTable(), shrunkenIndexName ); - int numShrunkIndexShards = clusterState.metadata().getProject().index(shrunkenIndexName).getNumberOfShards(); + int numShrunkIndexShards = currentState.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/ShrunkenIndexCheckStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStep.java index efc106127881c..ad63c98f23f2e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStep.java @@ -8,7 +8,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; import org.elasticsearch.common.Strings; @@ -41,24 +41,21 @@ public boolean isRetryable() { } @Override - public Result isConditionMet(Index index, ClusterState clusterState) { - IndexMetadata idxMeta = clusterState.getMetadata().getProject().index(index); + public Result isConditionMet(Index index, ProjectState currentState) { + IndexMetadata idxMeta = currentState.metadata().index(index); if (idxMeta == null) { logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), index.getName()); // Index must have been since deleted, ignore it return new Result(false, null); } - String shrunkenIndexSource = IndexMetadata.INDEX_RESIZE_SOURCE_NAME.get( - clusterState.metadata().getProject().index(index).getSettings() - ); + String shrunkenIndexSource = IndexMetadata.INDEX_RESIZE_SOURCE_NAME.get(currentState.metadata().index(index).getSettings()); if (Strings.isNullOrEmpty(shrunkenIndexSource)) { throw new IllegalStateException("step[" + NAME + "] is checking an un-shrunken index[" + index.getName() + "]"); } LifecycleExecutionState lifecycleState = idxMeta.getLifecycleExecutionState(); String targetIndexName = getShrinkIndexName(shrunkenIndexSource, lifecycleState); - boolean isConditionMet = index.getName().equals(targetIndexName) - && clusterState.metadata().getProject().index(shrunkenIndexSource) == null; + boolean isConditionMet = index.getName().equals(targetIndexName) && currentState.metadata().index(shrunkenIndexSource) == null; if (isConditionMet) { 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 49f2259ec4162..5735202b11948 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 @@ -9,11 +9,10 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.support.ActiveShardCount; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.DataStream; import org.elasticsearch.cluster.metadata.IndexAbstraction; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.routing.IndexRoutingTable; import org.elasticsearch.common.Strings; import org.elasticsearch.index.Index; @@ -48,9 +47,8 @@ public boolean isRetryable() { } @Override - public Result isConditionMet(Index index, ClusterState clusterState) { - Metadata metadata = clusterState.metadata(); - IndexMetadata originalIndexMeta = metadata.getProject().index(index); + public Result isConditionMet(Index index, ProjectState currentState) { + IndexMetadata originalIndexMeta = currentState.metadata().index(index); if (originalIndexMeta == null) { String errorMessage = Strings.format( @@ -74,12 +72,12 @@ public Result isConditionMet(Index index, ClusterState clusterState) { return new Result(true, new SingleMessageFieldInfo(message)); } - IndexAbstraction indexAbstraction = metadata.getProject().getIndicesLookup().get(index.getName()); + IndexAbstraction indexAbstraction = currentState.metadata().getIndicesLookup().get(index.getName()); final String rolledIndexName; final String waitForActiveShardsSettingValue; DataStream dataStream = indexAbstraction.getParentDataStream(); if (dataStream != null) { - IndexAbstraction dataStreamAbstraction = metadata.getProject().getIndicesLookup().get(dataStream.getName()); + IndexAbstraction dataStreamAbstraction = currentState.metadata().getIndicesLookup().get(dataStream.getName()); assert dataStreamAbstraction != null : dataStream.getName() + " datastream is not present in the metadata indices lookup"; // Determine which write index we care about right now: final Index rolledIndex; @@ -91,7 +89,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) { if (rolledIndex == null) { return getErrorResultOnNullMetadata(getKey(), index); } - IndexMetadata rolledIndexMeta = metadata.getProject().index(rolledIndex); + IndexMetadata rolledIndexMeta = currentState.metadata().index(rolledIndex); rolledIndexName = rolledIndexMeta.getIndex().getName(); waitForActiveShardsSettingValue = rolledIndexMeta.getSettings().get(IndexMetadata.SETTING_WAIT_FOR_ACTIVE_SHARDS.getKey()); } else { @@ -106,12 +104,12 @@ public Result isConditionMet(Index index, ClusterState clusterState) { ); } - IndexAbstraction aliasAbstraction = metadata.getProject().getIndicesLookup().get(rolloverAlias); + IndexAbstraction aliasAbstraction = currentState.metadata().getIndicesLookup().get(rolloverAlias); assert aliasAbstraction.getType() == IndexAbstraction.Type.ALIAS : rolloverAlias + " must be an alias but it is not"; Index aliasWriteIndex = aliasAbstraction.getWriteIndex(); if (aliasWriteIndex != null) { - IndexMetadata writeIndexImd = metadata.getProject().index(aliasWriteIndex); + IndexMetadata writeIndexImd = currentState.metadata().index(aliasWriteIndex); rolledIndexName = writeIndexImd.getIndex().getName(); waitForActiveShardsSettingValue = writeIndexImd.getSettings().get(IndexMetadata.SETTING_WAIT_FOR_ACTIVE_SHARDS.getKey()); } else { @@ -129,7 +127,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) { return getErrorResultOnNullMetadata(getKey(), index); } rolledIndexName = tmpRolledIndex.getName(); - waitForActiveShardsSettingValue = metadata.getProject() + waitForActiveShardsSettingValue = currentState.metadata() .index(rolledIndexName) .getSettings() .get("index.write.wait_for_active_shards"); @@ -138,12 +136,12 @@ public Result isConditionMet(Index index, ClusterState clusterState) { ActiveShardCount activeShardCount = ActiveShardCount.parseString(waitForActiveShardsSettingValue); boolean enoughShardsActive = activeShardCount.enoughShardsActive( - clusterState.metadata().getProject(), - clusterState.routingTable(), + currentState.metadata(), + currentState.routingTable(), rolledIndexName ); - IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(rolledIndexName); + IndexRoutingTable indexRoutingTable = currentState.routingTable().index(rolledIndexName); int currentActiveShards = 0; for (int i = 0; i < indexRoutingTable.size(); i++) { currentActiveShards += indexRoutingTable.shard(i).activeShards().size(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForDataTierStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForDataTierStep.java index 923b57988b415..84bf405abbfbe 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForDataTierStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForDataTierStep.java @@ -7,7 +7,7 @@ package org.elasticsearch.xpack.core.ilm; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.DesiredNodes; import org.elasticsearch.cluster.routing.allocation.DataTier; import org.elasticsearch.index.Index; @@ -33,12 +33,12 @@ public WaitForDataTierStep(StepKey key, StepKey nextStepKey, String tierPreferen } @Override - public Result isConditionMet(Index index, ClusterState clusterState) { + public Result isConditionMet(Index index, ProjectState currentState) { boolean present = DataTierAllocationDecider.preferredAvailableTier( DataTier.parseTierList(tierPreference), - clusterState.nodes(), - DesiredNodes.latestFromClusterState(clusterState), - clusterState.metadata().nodeShutdowns() + currentState.cluster().nodes(), + DesiredNodes.latestFromClusterState(currentState.cluster()), + currentState.cluster().metadata().nodeShutdowns() ).isPresent(); SingleMessageFieldInfo info = present ? null : new SingleMessageFieldInfo("no nodes for tiers [" + tierPreference + "] available"); return new Result(present, info); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStep.java index defe094c7ec63..5e8a383dc4cf9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStep.java @@ -9,7 +9,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; @@ -81,13 +81,10 @@ public boolean equals(Object obj) { } @Override - public Result isConditionMet(Index index, ClusterState clusterState) { - LifecycleExecutionState lifecycleExecutionState = clusterState.metadata() - .getProject() - .index(index.getName()) - .getLifecycleExecutionState(); + public Result isConditionMet(Index index, ProjectState currentState) { + LifecycleExecutionState lifecycleExecutionState = currentState.metadata().index(index.getName()).getLifecycleExecutionState(); String indexName = indexNameSupplier.apply(index.getName(), lifecycleExecutionState); - IndexMetadata indexMetadata = clusterState.metadata().getProject().index(indexName); + IndexMetadata indexMetadata = currentState.metadata().index(indexName); // check if the (potentially) derived index exists if (indexMetadata == null) { String errorMessage = Strings.format( @@ -100,7 +97,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) { return new Result(false, new SingleMessageFieldInfo(errorMessage)); } - IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(indexMetadata.getIndex()); + IndexRoutingTable indexRoutingTable = currentState.routingTable().index(indexMetadata.getIndex()); Result result = switch (this.color) { case GREEN -> waitForGreen(indexRoutingTable); case YELLOW -> waitForYellow(indexRoutingTable); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexingCompleteStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexingCompleteStep.java index c1c86f04eaefd..3a3a6da4f1321 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexingCompleteStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexingCompleteStep.java @@ -8,7 +8,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.index.Index; import org.elasticsearch.xcontent.ParseField; @@ -36,8 +36,8 @@ public boolean isRetryable() { } @Override - public Result isConditionMet(Index index, ClusterState clusterState) { - IndexMetadata followerIndex = clusterState.metadata().getProject().index(index); + public Result isConditionMet(Index index, ProjectState currentState) { + IndexMetadata followerIndex = currentState.metadata().index(index); if (followerIndex == 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()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AllocationRoutedStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AllocationRoutedStepTests.java index 708c3630b8b8a..d9a201d24d061 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AllocationRoutedStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AllocationRoutedStepTests.java @@ -6,9 +6,12 @@ */ package org.elasticsearch.xpack.core.ilm; +import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.routing.IndexRoutingTable; @@ -158,8 +161,9 @@ public void testClusterExcludeFiltersConditionMetOnlyOneCopyAllocated() { Settings clusterSettings = Settings.builder().put("cluster.routing.allocation.exclude._id", "node1").build(); Settings.Builder nodeSettingsBuilder = Settings.builder(); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder().indices(indices).transientSettings(clusterSettings)) + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()).indices(indices).build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata(Metadata.builder().put(project).transientSettings(clusterSettings)) .nodes( DiscoveryNodes.builder() .add( @@ -175,9 +179,10 @@ public void testClusterExcludeFiltersConditionMetOnlyOneCopyAllocated() { .build() ) ) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); - Result actualResult = step.isConditionMet(index, clusterState); + .putRoutingTable(project.id(), RoutingTable.builder().add(indexRoutingTable).build()) + .build() + .projectState(project.id()); + Result actualResult = step.isConditionMet(index, state); Result expectedResult = new ClusterStateWaitStep.Result(false, allShardsActiveAllocationInfo(1, 1)); assertEquals(expectedResult.complete(), actualResult.complete()); @@ -489,11 +494,11 @@ public void testExecuteReplicasNotAllocatedOnSingleNode() { public void testExecuteIndexMissing() throws Exception { Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).build(); + ProjectState state = projectStateWithEmptyProject(); AllocationRoutedStep step = createRandomInstance(); - Result actualResult = step.isConditionMet(index, clusterState); + Result actualResult = step.isConditionMet(index, state); assertFalse(actualResult.complete()); assertNull(actualResult.informationContext()); } @@ -516,8 +521,9 @@ private void assertAllocateStatus( .build(); Map indices = Map.of(index.getName(), indexMetadata); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder().indices(indices)) + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()).indices(indices).build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) .nodes( DiscoveryNodes.builder() .add( @@ -533,9 +539,10 @@ private void assertAllocateStatus( .build() ) ) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); - Result actualResult = step.isConditionMet(index, clusterState); + .putRoutingTable(project.id(), RoutingTable.builder().add(indexRoutingTable).build()) + .build() + .projectState(project.id()); + Result actualResult = step.isConditionMet(index, state); assertEquals(expectedResult.complete(), actualResult.complete()); assertEquals(expectedResult.informationContext(), actualResult.informationContext()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckNoDataStreamWriteIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckNoDataStreamWriteIndexStepTests.java index 54c6ceb814af8..077d59c4a158d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckNoDataStreamWriteIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckNoDataStreamWriteIndexStepTests.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.xpack.core.ilm; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.DataStream; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo; @@ -54,11 +54,9 @@ public void testStepCompleteIfIndexIsNotPartOfDataStream() { .numberOfReplicas(randomIntBetween(0, 5)) .build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomUniqueProjectId()).put(indexMetadata, true)); - ClusterStateWaitStep.Result result = createRandomInstance().isConditionMet(indexMetadata.getIndex(), clusterState); + ClusterStateWaitStep.Result result = createRandomInstance().isConditionMet(indexMetadata.getIndex(), state); assertThat(result.complete(), is(true)); assertThat(result.informationContext(), is(nullValue())); } @@ -80,20 +78,17 @@ public void testStepIncompleteIfIndexIsTheDataStreamWriteIndex() { .numberOfReplicas(randomIntBetween(0, 5)) .build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata( - Metadata.builder() - .put(indexMetadata, true) - .put(failureIndexMetadata, true) - .put(newInstance(dataStreamName, List.of(indexMetadata.getIndex()), List.of(failureIndexMetadata.getIndex()))) - .build() - ) - .build(); + ProjectState state = projectStateFromProject( + ProjectMetadata.builder(randomUniqueProjectId()) + .put(indexMetadata, true) + .put(failureIndexMetadata, true) + .put(newInstance(dataStreamName, List.of(indexMetadata.getIndex()), List.of(failureIndexMetadata.getIndex()))) + ); boolean useFailureStore = randomBoolean(); IndexMetadata indexToOperateOn = useFailureStore ? failureIndexMetadata : indexMetadata; String expectedIndexName = indexToOperateOn.getIndex().getName(); - ClusterStateWaitStep.Result result = createRandomInstance().isConditionMet(indexToOperateOn.getIndex(), clusterState); + ClusterStateWaitStep.Result result = createRandomInstance().isConditionMet(indexToOperateOn.getIndex(), state); assertThat(result.complete(), is(false)); SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.informationContext(); assertThat( @@ -146,21 +141,18 @@ public void testStepCompleteIfPartOfDataStreamButNotWriteIndex() { List backingIndices = List.of(indexMetadata.getIndex(), writeIndexMetadata.getIndex()); List failureIndices = List.of(failureIndexMetadata.getIndex(), failureStoreWriteIndexMetadata.getIndex()); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata( - Metadata.builder() - .put(indexMetadata, true) - .put(writeIndexMetadata, true) - .put(failureIndexMetadata, true) - .put(failureStoreWriteIndexMetadata, true) - .put(newInstance(dataStreamName, backingIndices, failureIndices)) - .build() - ) - .build(); + ProjectState state = projectStateFromProject( + ProjectMetadata.builder(randomUniqueProjectId()) + .put(indexMetadata, true) + .put(writeIndexMetadata, true) + .put(failureIndexMetadata, true) + .put(failureStoreWriteIndexMetadata, true) + .put(newInstance(dataStreamName, backingIndices, failureIndices)) + ); boolean useFailureStore = randomBoolean(); IndexMetadata indexToOperateOn = useFailureStore ? failureIndexMetadata : indexMetadata; - ClusterStateWaitStep.Result result = createRandomInstance().isConditionMet(indexToOperateOn.getIndex(), clusterState); + ClusterStateWaitStep.Result result = createRandomInstance().isConditionMet(indexToOperateOn.getIndex(), state); assertThat(result.complete(), is(true)); assertThat(result.informationContext(), is(nullValue())); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckShrinkReadyStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckShrinkReadyStepTests.java index a427f8c9154cc..42bbdd472fa15 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckShrinkReadyStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckShrinkReadyStepTests.java @@ -7,10 +7,13 @@ package org.elasticsearch.xpack.core.ilm; +import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.metadata.NodesShutdownMetadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.metadata.SingleNodeShutdownMetadata; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; import org.elasticsearch.cluster.node.DiscoveryNodes; @@ -412,11 +415,11 @@ public void testExecuteReplicasButCopiesNotPresent() { public void testExecuteIndexMissing() throws Exception { Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).build(); + ProjectState state = projectStateWithEmptyProject(); CheckShrinkReadyStep step = createRandomInstance(); - ClusterStateWaitStep.Result actualResult = step.isConditionMet(index, clusterState); + ClusterStateWaitStep.Result actualResult = step.isConditionMet(index, state); assertFalse(actualResult.complete()); assertNull(actualResult.informationContext()); } @@ -448,13 +451,14 @@ public void testStepCompletableIfAllShardsActive() { .numberOfReplicas(1) .build(); Map indices = Map.of(index.getName(), indexMetadata); + var project = ProjectMetadata.builder(randomProjectIdOrDefault()).indices(indices).build(); final String targetNodeName = type == SingleNodeShutdownMetadata.Type.REPLACE ? randomAlphaOfLengthBetween(10, 20) : null; final TimeValue grace = type == SIGTERM ? randomTimeValue() : null; - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) .metadata( Metadata.builder() - .indices(indices) + .put(project) .putCustom( NodesShutdownMetadata.TYPE, new NodesShutdownMetadata( @@ -492,10 +496,11 @@ public void testStepCompletableIfAllShardsActive() { .build() ) ) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + .putRoutingTable(project.id(), RoutingTable.builder().add(indexRoutingTable).build()) + .build() + .projectState(project.id()); assertTrue(step.isCompletable()); - ClusterStateWaitStep.Result actualResult = step.isConditionMet(index, clusterState); + ClusterStateWaitStep.Result actualResult = step.isConditionMet(index, state); assertTrue(actualResult.complete()); assertTrue(step.isCompletable()); } @@ -528,13 +533,14 @@ public void testStepBecomesUncompletable() { .numberOfReplicas(1) .build(); Map indices = Map.of(index.getName(), indexMetadata); + var project = ProjectMetadata.builder(randomProjectIdOrDefault()).indices(indices).build(); final String targetNodeName = type == SingleNodeShutdownMetadata.Type.REPLACE ? randomAlphaOfLengthBetween(10, 20) : null; final TimeValue grace = type == SIGTERM ? randomTimeValue() : null; - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) .metadata( Metadata.builder() - .indices(indices) + .put(project) .putCustom( NodesShutdownMetadata.TYPE, new NodesShutdownMetadata( @@ -572,10 +578,11 @@ public void testStepBecomesUncompletable() { .build() ) ) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + .putRoutingTable(project.id(), RoutingTable.builder().add(indexRoutingTable).build()) + .build() + .projectState(project.id()); assertTrue(step.isCompletable()); - ClusterStateWaitStep.Result actualResult = step.isConditionMet(index, clusterState); + ClusterStateWaitStep.Result actualResult = step.isConditionMet(index, state); assertFalse(actualResult.complete()); assertThat( Strings.toString(actualResult.informationContext()), @@ -602,9 +609,10 @@ private void assertAllocateStatus( .numberOfReplicas(replicas) .build(); Map indices = Map.of(index.getName(), indexMetadata); + var project = ProjectMetadata.builder(randomProjectIdOrDefault()).indices(indices).build(); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder().indices(indices)) + ProjectState state = ClusterState.builder(ClusterState.EMPTY_STATE) + .putProjectMetadata(project) .nodes( DiscoveryNodes.builder() .add( @@ -624,9 +632,10 @@ private void assertAllocateStatus( .build() ) ) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); - ClusterStateWaitStep.Result actualResult = step.isConditionMet(index, clusterState); + .putRoutingTable(project.id(), RoutingTable.builder().add(indexRoutingTable).build()) + .build() + .projectState(project.id()); + ClusterStateWaitStep.Result actualResult = step.isConditionMet(index, state); assertEquals(expectedResult.complete(), actualResult.complete()); assertEquals(expectedResult.informationContext(), actualResult.informationContext()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStepTests.java index 23d24fbd28730..791f9ee2bfa90 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStepTests.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.core.ilm; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo; @@ -49,13 +49,11 @@ public void testStepCompleteIfTargetShardsCountIsValid() { .numberOfReplicas(randomIntBetween(0, 5)) .build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, false)); CheckTargetShardsCountStep checkTargetShardsCountStep = new CheckTargetShardsCountStep(randomStepKey(), randomStepKey(), 2); - ClusterStateWaitStep.Result result = checkTargetShardsCountStep.isConditionMet(indexMetadata.getIndex(), clusterState); + ClusterStateWaitStep.Result result = checkTargetShardsCountStep.isConditionMet(indexMetadata.getIndex(), state); assertThat(result.complete(), is(true)); } @@ -68,13 +66,11 @@ public void testStepIncompleteIfTargetShardsCountNotValid() { .numberOfReplicas(randomIntBetween(0, 5)) .build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, false)); CheckTargetShardsCountStep checkTargetShardsCountStep = new CheckTargetShardsCountStep(randomStepKey(), randomStepKey(), 3); - ClusterStateWaitStep.Result result = checkTargetShardsCountStep.isConditionMet(indexMetadata.getIndex(), clusterState); + ClusterStateWaitStep.Result result = checkTargetShardsCountStep.isConditionMet(indexMetadata.getIndex(), state); assertThat(result.complete(), is(false)); SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.informationContext(); assertThat( diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStepTests.java index eeddda4199665..e44308d9d8544 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStepTests.java @@ -6,11 +6,10 @@ */ package org.elasticsearch.xpack.core.ilm; -import org.elasticsearch.cluster.ClusterName; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; -import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexVersion; @@ -67,7 +66,7 @@ public void testIndexIsMissingReturnsIncompleteResult() { ClusterStateWaitUntilThresholdStep underTest = new ClusterStateWaitUntilThresholdStep(stepToExecute, randomStepKey()); ClusterStateWaitStep.Result result = underTest.isConditionMet( new Index("testName", UUID.randomUUID().toString()), - ClusterState.EMPTY_STATE + projectStateFromProject(ProjectMetadata.builder(randomUniqueProjectId())) ); assertThat(result.complete(), is(false)); assertThat(result.informationContext(), nullValue()); @@ -86,14 +85,12 @@ public void testIsConditionMetForUnderlyingStep() { .numberOfShards(1) .numberOfReplicas(0) .build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomUniqueProjectId()).put(indexMetadata, true)); WaitForIndexingCompleteStep stepToExecute = new WaitForIndexingCompleteStep(randomStepKey(), randomStepKey()); ClusterStateWaitUntilThresholdStep underTest = new ClusterStateWaitUntilThresholdStep(stepToExecute, randomStepKey()); - ClusterStateWaitStep.Result result = underTest.isConditionMet(indexMetadata.getIndex(), clusterState); + ClusterStateWaitStep.Result result = underTest.isConditionMet(indexMetadata.getIndex(), state); assertThat(result.complete(), is(true)); assertThat(result.informationContext(), nullValue()); } @@ -111,13 +108,11 @@ public void testIsConditionMetForUnderlyingStep() { .numberOfReplicas(0) .build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomUniqueProjectId()).put(indexMetadata, true)); WaitForIndexingCompleteStep stepToExecute = new WaitForIndexingCompleteStep(randomStepKey(), randomStepKey()); ClusterStateWaitUntilThresholdStep underTest = new ClusterStateWaitUntilThresholdStep(stepToExecute, randomStepKey()); - ClusterStateWaitStep.Result result = underTest.isConditionMet(indexMetadata.getIndex(), clusterState); + ClusterStateWaitStep.Result result = underTest.isConditionMet(indexMetadata.getIndex(), state); assertThat(result.complete(), is(false)); assertThat(result.informationContext(), notNullValue()); @@ -144,15 +139,13 @@ public void testIsConditionMetForUnderlyingStep() { .numberOfShards(1) .numberOfReplicas(0) .build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomUniqueProjectId()).put(indexMetadata, true)); WaitForIndexingCompleteStep stepToExecute = new WaitForIndexingCompleteStep(randomStepKey(), randomStepKey()); StepKey nextKeyOnThresholdBreach = randomStepKey(); ClusterStateWaitUntilThresholdStep underTest = new ClusterStateWaitUntilThresholdStep(stepToExecute, nextKeyOnThresholdBreach); - ClusterStateWaitStep.Result result = underTest.isConditionMet(indexMetadata.getIndex(), clusterState); + ClusterStateWaitStep.Result result = underTest.isConditionMet(indexMetadata.getIndex(), state); assertThat(result.complete(), is(true)); assertThat(result.informationContext(), nullValue()); assertThat(underTest.getNextStepKey(), is(not(nextKeyOnThresholdBreach))); @@ -173,15 +166,13 @@ public void testIsConditionMetForUnderlyingStep() { .numberOfReplicas(0) .build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomUniqueProjectId()).put(indexMetadata, true)); StepKey currentStepKey = randomStepKey(); WaitForIndexingCompleteStep stepToExecute = new WaitForIndexingCompleteStep(currentStepKey, randomStepKey()); StepKey nextKeyOnThresholdBreach = randomStepKey(); ClusterStateWaitUntilThresholdStep underTest = new ClusterStateWaitUntilThresholdStep(stepToExecute, nextKeyOnThresholdBreach); - ClusterStateWaitStep.Result result = underTest.isConditionMet(indexMetadata.getIndex(), clusterState); + ClusterStateWaitStep.Result result = underTest.isConditionMet(indexMetadata.getIndex(), state); assertThat(result.complete(), is(true)); assertThat(result.informationContext(), notNullValue()); @@ -247,14 +238,12 @@ public void testIsCompletableBreaches() { .putCustom(ILM_CUSTOM_METADATA_KEY, Map.of("step_time", String.valueOf(Clock.systemUTC().millis()))) .build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomUniqueProjectId()).put(indexMetadata, true)); ClusterStateWaitUntilThresholdStep step = new ClusterStateWaitUntilThresholdStep( new ClusterStateWaitStep(new StepKey("phase", "action", "key"), new StepKey("phase", "action", "next-key")) { @Override - public Result isConditionMet(Index index, ClusterState clusterState) { + public Result isConditionMet(Index index, ProjectState currentState) { return new Result(false, new SingleMessageFieldInfo("")); } @@ -266,14 +255,14 @@ public boolean isRetryable() { new StepKey("phase", "action", "breached") ); - assertFalse(step.isConditionMet(indexMetadata.getIndex(), clusterState).complete()); + assertFalse(step.isConditionMet(indexMetadata.getIndex(), state).complete()); assertThat(step.getNextStepKey().name(), equalTo("next-key")); step = new ClusterStateWaitUntilThresholdStep( new ClusterStateWaitStep(new StepKey("phase", "action", "key"), new StepKey("phase", "action", "next-key")) { @Override - public Result isConditionMet(Index index, ClusterState clusterState) { + public Result isConditionMet(Index index, ProjectState currentState) { return new Result(false, new SingleMessageFieldInfo("")); } @@ -289,7 +278,7 @@ public boolean isRetryable() { }, new StepKey("phase", "action", "breached") ); - assertTrue(step.isConditionMet(indexMetadata.getIndex(), clusterState).complete()); + assertTrue(step.isConditionMet(indexMetadata.getIndex(), state).complete()); assertThat(step.getNextStepKey().name(), equalTo("breached")); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStepTests.java index 51d1464ed5525..8689d083c5a5a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStepTests.java @@ -6,9 +6,11 @@ */ package org.elasticsearch.xpack.core.ilm; +import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodeRole; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; @@ -29,6 +31,9 @@ import java.util.Collections; import java.util.Set; +import static org.elasticsearch.cluster.node.DiscoveryNodeRole.DATA_HOT_NODE_ROLE; +import static org.elasticsearch.cluster.node.DiscoveryNodeRole.DATA_ROLE; +import static org.elasticsearch.cluster.node.DiscoveryNodeRole.DATA_WARM_NODE_ROLE; import static org.elasticsearch.cluster.routing.TestShardRouting.shardRoutingBuilder; import static org.elasticsearch.cluster.routing.allocation.DataTier.TIER_PREFERENCE; import static org.elasticsearch.xpack.core.ilm.CheckShrinkReadyStepTests.randomUnassignedInfo; @@ -80,15 +85,11 @@ public void testExecuteWithUnassignedShard() { ).build() ); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .nodes(DiscoveryNodes.builder().add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_HOT_NODE_ROLE)))) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + ProjectState state = createState(indexMetadata, indexRoutingTable, DATA_HOT_NODE_ROLE); DataTierMigrationRoutedStep step = createRandomInstance(); Result expectedResult = new Result(false, waitingForActiveShardsAllocationInfo(1)); - Result actualResult = step.isConditionMet(index, clusterState); + Result actualResult = step.isConditionMet(index, state); assertThat(actualResult.complete(), is(false)); assertThat(actualResult.informationContext(), is(expectedResult.informationContext())); } @@ -103,15 +104,7 @@ public void testExecuteWithPendingShards() { IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .nodes( - DiscoveryNodes.builder() - .add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_HOT_NODE_ROLE))) - .add(newNode("node2", Collections.singleton(DiscoveryNodeRole.DATA_WARM_NODE_ROLE))) - ) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + ProjectState state = createState(indexMetadata, indexRoutingTable, DATA_HOT_NODE_ROLE, DATA_WARM_NODE_ROLE); DataTierMigrationRoutedStep step = createRandomInstance(); Result expectedResult = new Result( false, @@ -128,7 +121,7 @@ public void testExecuteWithPendingShards() { ) ); - Result actualResult = step.isConditionMet(index, clusterState); + Result actualResult = step.isConditionMet(index, state); assertThat(actualResult.complete(), is(false)); assertThat(actualResult.informationContext(), is(expectedResult.informationContext())); } @@ -143,11 +136,7 @@ public void testExecuteWithPendingShardsAndTargetRoleNotPresentInCluster() { IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .nodes(DiscoveryNodes.builder().add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_HOT_NODE_ROLE)))) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + ProjectState state = createState(indexMetadata, indexRoutingTable, DATA_HOT_NODE_ROLE); DataTierMigrationRoutedStep step = createRandomInstance(); Result expectedResult = new Result( false, @@ -162,18 +151,18 @@ public void testExecuteWithPendingShardsAndTargetRoleNotPresentInCluster() { ) ); - Result actualResult = step.isConditionMet(index, clusterState); + Result actualResult = step.isConditionMet(index, state); assertThat(actualResult.complete(), is(false)); assertThat(actualResult.informationContext(), is(expectedResult.informationContext())); } public void testExecuteIndexMissing() { Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).build(); + ProjectState state = projectStateWithEmptyProject(); DataTierMigrationRoutedStep step = createRandomInstance(); - Result actualResult = step.isConditionMet(index, clusterState); + Result actualResult = step.isConditionMet(index, state); assertThat(actualResult.complete(), is(false)); assertThat(actualResult.informationContext(), is(nullValue())); } @@ -188,17 +177,9 @@ public void testExecuteIsComplete() { IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", true, ShardRoutingState.STARTED)); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .nodes( - DiscoveryNodes.builder() - .add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_HOT_NODE_ROLE))) - .add(newNode("node2", Collections.singleton(DiscoveryNodeRole.DATA_WARM_NODE_ROLE))) - ) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + ProjectState state = createState(indexMetadata, indexRoutingTable, DATA_HOT_NODE_ROLE, DATA_WARM_NODE_ROLE); DataTierMigrationRoutedStep step = createRandomInstance(); - Result result = step.isConditionMet(index, clusterState); + Result result = step.isConditionMet(index, state); assertThat(result.complete(), is(true)); assertThat(result.informationContext(), is(nullValue())); } @@ -213,13 +194,10 @@ public void testExecuteWithGenericDataNodes() { IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .nodes(DiscoveryNodes.builder().add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_ROLE)))) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + ProjectState state = createState(indexMetadata, indexRoutingTable, DATA_ROLE); + DataTierMigrationRoutedStep step = createRandomInstance(); - Result result = step.isConditionMet(index, clusterState); + Result result = step.isConditionMet(index, state); assertThat(result.complete(), is(true)); assertThat(result.informationContext(), is(nullValue())); } @@ -236,15 +214,11 @@ public void testExecuteForIndexWithoutTierRoutingInformationWaitsForReplicasToBe .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)) .addReplica(ShardRouting.Role.DEFAULT); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .nodes(DiscoveryNodes.builder().add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_HOT_NODE_ROLE)))) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + ProjectState state = createState(indexMetadata, indexRoutingTable, DATA_HOT_NODE_ROLE); DataTierMigrationRoutedStep step = createRandomInstance(); Result expectedResult = new Result(false, waitingForActiveShardsAllocationInfo(1)); - Result result = step.isConditionMet(index, clusterState); + Result result = step.isConditionMet(index, state); assertThat(result.complete(), is(false)); assertThat(result.informationContext(), is(expectedResult.informationContext())); } @@ -254,23 +228,29 @@ public void testExecuteForIndexWithoutTierRoutingInformationWaitsForReplicasToBe .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", false, ShardRoutingState.STARTED)); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .nodes( - DiscoveryNodes.builder() - .add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_HOT_NODE_ROLE))) - .add(newNode("node2", Collections.singleton(DiscoveryNodeRole.DATA_WARM_NODE_ROLE))) - ) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + ProjectState state = createState(indexMetadata, indexRoutingTable, DATA_HOT_NODE_ROLE, DATA_WARM_NODE_ROLE); DataTierMigrationRoutedStep step = createRandomInstance(); - Result result = step.isConditionMet(index, clusterState); + Result result = step.isConditionMet(index, state); assertThat(result.complete(), is(true)); assertThat(result.informationContext(), is(nullValue())); } } + private ProjectState createState(IndexMetadata indexMetadata, IndexRoutingTable.Builder indexRoutingTable, DiscoveryNodeRole... roles) { + var project = ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, false).build(); + final var nodes = DiscoveryNodes.builder(); + for (int i = 0; i < roles.length; i++) { + nodes.add(newNode("node" + (i + 1), Collections.singleton(roles[i]))); + } + return ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) + .nodes(nodes) + .putRoutingTable(project.id(), RoutingTable.builder().add(indexRoutingTable).build()) + .build() + .projectState(project.id()); + } + private DiscoveryNode newNode(String nodeId, Set roles) { return DiscoveryNodeUtils.builder(nodeId).roles(roles).build(); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStepTests.java index 592d259f07069..879ffb3de39a6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStepTests.java @@ -8,8 +8,9 @@ import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; import org.elasticsearch.cluster.node.DiscoveryNodes; @@ -70,8 +71,7 @@ public void testConditionMet() { .numberOfShards(shrinkNumberOfShards) .numberOfReplicas(0) .build(); - Metadata metadata = Metadata.builder() - .persistentSettings(settings(IndexVersion.current()).build()) + ProjectMetadata project = ProjectMetadata.builder(randomProjectIdOrDefault()) .put(IndexMetadata.builder(originalIndexMetadata)) .put(IndexMetadata.builder(shrunkIndexMetadata)) .build(); @@ -87,13 +87,14 @@ public void testConditionMet() { for (int i = 0; i < shrinkNumberOfShards; i++) { builder.addShard(TestShardRouting.newShardRouting(new ShardId(shrinkIndex, i), nodeId, true, ShardRoutingState.STARTED)); } - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(metadata) + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) - .routingTable(RoutingTable.builder().add(builder.build()).build()) - .build(); + .putRoutingTable(project.id(), RoutingTable.builder().add(builder.build()).build()) + .build() + .projectState(project.id()); - Result result = step.isConditionMet(originalIndexMetadata.getIndex(), clusterState); + Result result = step.isConditionMet(originalIndexMetadata.getIndex(), state); assertTrue(result.complete()); assertNull(result.informationContext()); } @@ -113,8 +114,7 @@ public void testConditionNotMetBecauseOfActive() { .numberOfShards(shrinkNumberOfShards) .numberOfReplicas(0) .build(); - Metadata metadata = Metadata.builder() - .persistentSettings(settings(IndexVersion.current()).build()) + ProjectMetadata project = ProjectMetadata.builder(randomProjectIdOrDefault()) .put(IndexMetadata.builder(originalIndexMetadata)) .put(IndexMetadata.builder(shrunkIndexMetadata)) .build(); @@ -130,13 +130,14 @@ public void testConditionNotMetBecauseOfActive() { for (int i = 0; i < shrinkNumberOfShards; i++) { builder.addShard(TestShardRouting.newShardRouting(new ShardId(shrinkIndex, i), nodeId, true, ShardRoutingState.INITIALIZING)); } - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(metadata) + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) - .routingTable(RoutingTable.builder().add(builder.build()).build()) - .build(); + .putRoutingTable(project.id(), RoutingTable.builder().add(builder.build()).build()) + .build() + .projectState(project.id()); - Result result = step.isConditionMet(originalIndexMetadata.getIndex(), clusterState); + Result result = step.isConditionMet(originalIndexMetadata.getIndex(), state); assertFalse(result.complete()); assertEquals(new ShrunkShardsAllocatedStep.Info(true, shrinkNumberOfShards, false), result.informationContext()); } @@ -150,8 +151,7 @@ public void testConditionNotMetBecauseOfShrunkIndexDoesntExistYet() { .numberOfShards(originalNumberOfShards) .numberOfReplicas(0) .build(); - Metadata metadata = Metadata.builder() - .persistentSettings(settings(IndexVersion.current()).build()) + ProjectMetadata project = ProjectMetadata.builder(randomProjectIdOrDefault()) .put(IndexMetadata.builder(originalIndexMetadata)) .build(); @@ -160,12 +160,13 @@ public void testConditionNotMetBecauseOfShrunkIndexDoesntExistYet() { .applySettings(NodeRoles.masterNode(settings(IndexVersion.current()).build())) .address(new TransportAddress(TransportAddress.META_ADDRESS, 9300)) .build(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(metadata) + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) - .build(); + .build() + .projectState(project.id()); - Result result = step.isConditionMet(originalIndexMetadata.getIndex(), clusterState); + Result result = step.isConditionMet(originalIndexMetadata.getIndex(), state); assertFalse(result.complete()); assertEquals(new ShrunkShardsAllocatedStep.Info(false, -1, false), result.informationContext()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStepTests.java index 4eb49df7f89c5..e7e576031b505 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStepTests.java @@ -6,10 +6,9 @@ */ package org.elasticsearch.xpack.core.ilm; -import org.elasticsearch.cluster.ClusterName; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.index.IndexVersion; import org.elasticsearch.xpack.core.ilm.ClusterStateWaitStep.Result; import org.elasticsearch.xpack.core.ilm.Step.StepKey; @@ -52,13 +51,9 @@ public void testConditionMet() { .numberOfShards(1) .numberOfReplicas(0) .build(); - Metadata metadata = Metadata.builder() - .persistentSettings(settings(IndexVersion.current()).build()) - .put(IndexMetadata.builder(indexMetadata)) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomUniqueProjectId()).put(indexMetadata, false)); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).build(); - Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState); + Result result = step.isConditionMet(indexMetadata.getIndex(), state); assertTrue(result.complete()); assertNull(result.informationContext()); } @@ -71,12 +66,8 @@ public void testConditionNotMetBecauseNotSameShrunkenIndex() { .numberOfShards(1) .numberOfReplicas(0) .build(); - Metadata metadata = Metadata.builder() - .persistentSettings(settings(IndexVersion.current()).build()) - .put(IndexMetadata.builder(shrinkIndexMetadata)) - .build(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).build(); - Result result = step.isConditionMet(shrinkIndexMetadata.getIndex(), clusterState); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomUniqueProjectId()).put(shrinkIndexMetadata, false)); + Result result = step.isConditionMet(shrinkIndexMetadata.getIndex(), state); assertFalse(result.complete()); assertEquals(new ShrunkenIndexCheckStep.Info(sourceIndex), result.informationContext()); } @@ -94,13 +85,10 @@ public void testConditionNotMetBecauseSourceIndexExists() { .numberOfShards(1) .numberOfReplicas(0) .build(); - Metadata metadata = Metadata.builder() - .persistentSettings(settings(IndexVersion.current()).build()) - .put(IndexMetadata.builder(originalIndexMetadata)) - .put(IndexMetadata.builder(shrinkIndexMetadata)) - .build(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).build(); - Result result = step.isConditionMet(shrinkIndexMetadata.getIndex(), clusterState); + ProjectState state = projectStateFromProject( + ProjectMetadata.builder(randomUniqueProjectId()).put(originalIndexMetadata, false).put(shrinkIndexMetadata, false) + ); + Result result = step.isConditionMet(shrinkIndexMetadata.getIndex(), state); assertFalse(result.complete()); assertEquals(new ShrunkenIndexCheckStep.Info(sourceIndex), result.informationContext()); } @@ -112,14 +100,10 @@ public void testIllegalState() { .numberOfShards(1) .numberOfReplicas(0) .build(); - Metadata metadata = Metadata.builder() - .persistentSettings(settings(IndexVersion.current()).build()) - .put(IndexMetadata.builder(indexMetadata)) - .build(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomUniqueProjectId()).put(indexMetadata, false)); IllegalStateException exception = expectThrows( IllegalStateException.class, - () -> step.isConditionMet(indexMetadata.getIndex(), clusterState) + () -> step.isConditionMet(indexMetadata.getIndex(), state) ); assertThat( exception.getMessage(), diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsTests.java index 328698254dc76..60454d8294e9e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsTests.java @@ -8,11 +8,12 @@ import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.AliasMetadata; import org.elasticsearch.cluster.metadata.DataStream; import org.elasticsearch.cluster.metadata.DataStreamTestHelper; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.routing.IndexRoutingTable; import org.elasticsearch.cluster.routing.RoutingTable; import org.elasticsearch.cluster.routing.ShardRoutingRoleStrategy; @@ -70,12 +71,10 @@ public void testIsConditionMetThrowsExceptionWhenRolloverAliasIsNotSet() { .numberOfShards(randomIntBetween(1, 5)) .numberOfReplicas(randomIntBetween(0, 5)) .build(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomUniqueProjectId()).put(indexMetadata, false)); try { - createRandomInstance().isConditionMet(indexMetadata.getIndex(), clusterState); + createRandomInstance().isConditionMet(indexMetadata.getIndex(), state); fail("expected the invocation to fail"); } catch (IllegalStateException e) { assertThat( @@ -118,14 +117,16 @@ public void testResultEvaluatedOnWriteIndexAliasWhenExists() { routingTable.addShard( TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node2", null, false, ShardRoutingState.STARTED) ); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(originalIndex, true).put(rolledIndex, true).build()) - .routingTable(RoutingTable.builder().add(routingTable.build()).build()) - .build(); + var project = ProjectMetadata.builder(randomProjectIdOrDefault()).put(originalIndex, false).put(rolledIndex, false).build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) + .putRoutingTable(project.id(), RoutingTable.builder().add(routingTable.build()).build()) + .build() + .projectState(project.id()); assertThat( "the rolled index has both the primary and the replica shards started so the condition should be met", - createRandomInstance().isConditionMet(originalIndex.getIndex(), clusterState).complete(), + createRandomInstance().isConditionMet(originalIndex.getIndex(), state).complete(), is(true) ); } @@ -156,14 +157,16 @@ public void testResultEvaluatedOnOnlyIndexTheAliasPointsToIfWriteIndexIsNull() { routingTable.addShard( TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node2", null, false, ShardRoutingState.STARTED) ); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(originalIndex, true).put(rolledIndex, true).build()) - .routingTable(RoutingTable.builder().add(routingTable.build()).build()) - .build(); + var project = ProjectMetadata.builder(randomProjectIdOrDefault()).put(originalIndex, false).put(rolledIndex, false).build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) + .putRoutingTable(project.id(), RoutingTable.builder().add(routingTable.build()).build()) + .build() + .projectState(project.id()); assertThat( "the index the alias is pointing to has both the primary and the replica shards started so the condition should be" + " met", - createRandomInstance().isConditionMet(originalIndex.getIndex(), clusterState).complete(), + createRandomInstance().isConditionMet(originalIndex.getIndex(), state).complete(), is(true) ); } @@ -221,29 +224,30 @@ public void testResultEvaluatedOnDataStream() throws IOException { ) ); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .put( - DataStreamTestHelper.newInstance( - dataStreamName, - List.of(originalIndexMeta.getIndex(), rolledIndexMeta.getIndex()), - List.of(failureOriginalIndexMeta.getIndex(), failureRolledIndexMeta.getIndex()) - ) - ) - .put(originalIndexMeta, true) - .put(rolledIndexMeta, true) - .put(failureOriginalIndexMeta, true) - .put(failureRolledIndexMeta, true) + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put( + DataStreamTestHelper.newInstance( + dataStreamName, + List.of(originalIndexMeta.getIndex(), rolledIndexMeta.getIndex()), + List.of(failureOriginalIndexMeta.getIndex(), failureRolledIndexMeta.getIndex()) + ) ) - .routingTable(RoutingTable.builder().add(routingTable.build()).add(failureRoutingTable.build()).build()) + .put(originalIndexMeta, true) + .put(rolledIndexMeta, true) + .put(failureOriginalIndexMeta, true) + .put(failureRolledIndexMeta, true) .build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) + .putRoutingTable(project.id(), RoutingTable.builder().add(routingTable.build()).add(failureRoutingTable.build()).build()) + .build() + .projectState(project.id()); WaitForActiveShardsStep waitForActiveShardsStep = createRandomInstance(); boolean useFailureStore = randomBoolean(); IndexMetadata indexToOperateOn = useFailureStore ? failureOriginalIndexMeta : originalIndexMeta; - ClusterStateWaitStep.Result result = waitForActiveShardsStep.isConditionMet(indexToOperateOn.getIndex(), clusterState); + ClusterStateWaitStep.Result result = waitForActiveShardsStep.isConditionMet(indexToOperateOn.getIndex(), state); assertThat(result.complete(), is(false)); XContentBuilder expected = new WaitForActiveShardsStep.ActiveShardsInfo(2, "3", false).toXContent( @@ -282,12 +286,14 @@ public void testResultReportsMeaningfulMessage() throws IOException { routingTable.addShard( TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node2", null, false, ShardRoutingState.STARTED) ); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(originalIndex, true).put(rolledIndex, true).build()) - .routingTable(RoutingTable.builder().add(routingTable.build()).build()) - .build(); + var project = ProjectMetadata.builder(randomProjectIdOrDefault()).put(originalIndex, false).put(rolledIndex, false).build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) + .putRoutingTable(project.id(), RoutingTable.builder().add(routingTable.build()).build()) + .build() + .projectState(project.id()); - ClusterStateWaitStep.Result result = createRandomInstance().isConditionMet(originalIndex.getIndex(), clusterState); + ClusterStateWaitStep.Result result = createRandomInstance().isConditionMet(originalIndex.getIndex(), state); assertThat(result.complete(), is(false)); XContentBuilder expected = new WaitForActiveShardsStep.ActiveShardsInfo(2, "3", false).toXContent( @@ -310,12 +316,10 @@ public void testResultReportsErrorMessage() { .numberOfShards(1) .numberOfReplicas(2) .build(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(rolledIndex, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(rolledIndex, false)); WaitForActiveShardsStep step = createRandomInstance(); - ClusterStateWaitStep.Result result = step.isConditionMet(new Index("index-000000", UUID.randomUUID().toString()), clusterState); + ClusterStateWaitStep.Result result = step.isConditionMet(new Index("index-000000", UUID.randomUUID().toString()), state); assertThat(result.complete(), is(false)); String actualResultAsString = Strings.toString(result.informationContext()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForDataTierStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForDataTierStepTests.java index 2635e14b52eb4..cfd41035a0454 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForDataTierStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForDataTierStepTests.java @@ -9,6 +9,8 @@ import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.node.DiscoveryNodeRole; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; import org.elasticsearch.cluster.node.DiscoveryNodes; @@ -69,13 +71,13 @@ public void testConditionMet() { String tierPreference = String.join(",", includedTiers); WaitForDataTierStep step = new WaitForDataTierStep(randomStepKey(), randomStepKey(), tierPreference); - verify(step, ClusterState.EMPTY_STATE, false, "no nodes for tiers [" + tierPreference + "] available"); + verify(step, projectStateWithEmptyProject(), false, "no nodes for tiers [" + tierPreference + "] available"); verify(step, state(List.of(notIncludedTier)), false, "no nodes for tiers [" + tierPreference + "] available"); verify(step, state(includedTiers), true, null); verify(step, state(List.of(DiscoveryNodeRole.DATA_ROLE.roleName())), true, null); } - private void verify(WaitForDataTierStep step, ClusterState state, boolean complete, String message) { + private void verify(WaitForDataTierStep step, ProjectState state, boolean complete, String message) { ClusterStateWaitStep.Result result = step.isConditionMet(null, state); assertThat(result.complete(), is(complete)); if (message != null) { @@ -85,7 +87,7 @@ private void verify(WaitForDataTierStep step, ClusterState state, boolean comple } } - private ClusterState state(Collection roles) { + private ProjectState state(Collection roles) { DiscoveryNodes.Builder builder = DiscoveryNodes.builder(); IntStream.range(0, between(1, 5)) .mapToObj( @@ -99,6 +101,7 @@ private ClusterState state(Collection roles) { .build() ) .forEach(builder::add); - return ClusterState.builder(ClusterName.DEFAULT).nodes(builder).build(); + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()).build(); + return ClusterState.builder(ClusterName.DEFAULT).nodes(builder).putProjectMetadata(project).build().projectState(project.id()); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java index 1414788f3ff98..dfa53f032cf30 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java @@ -9,10 +9,11 @@ import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; -import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.cluster.routing.IndexRoutingTable; import org.elasticsearch.cluster.routing.RoutingTable; import org.elasticsearch.cluster.routing.ShardRouting; @@ -86,13 +87,15 @@ public void testConditionMetForGreen() { ); IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).addShard(shardRouting).build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, false).build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) + .putRoutingTable(project.id(), RoutingTable.builder().add(indexRoutingTable).build()) + .build() + .projectState(project.id()); WaitForIndexColorStep step = new WaitForIndexColorStep(randomStepKey(), randomStepKey(), ClusterHealthStatus.GREEN); - ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState); + ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), state); assertThat(result.complete(), is(true)); assertThat(result.informationContext(), nullValue()); } @@ -112,13 +115,15 @@ public void testConditionNotMetForGreen() { ); IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).addShard(shardRouting).build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, false).build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) + .putRoutingTable(project.id(), RoutingTable.builder().add(indexRoutingTable).build()) + .build() + .projectState(project.id()); WaitForIndexColorStep step = new WaitForIndexColorStep(randomStepKey(), randomStepKey(), ClusterHealthStatus.GREEN); - ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState); + ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), state); assertThat(result.complete(), is(false)); SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.informationContext(); assertThat(info, notNullValue()); @@ -132,13 +137,10 @@ public void testConditionNotMetNoIndexRoutingTable() { .numberOfReplicas(0) .build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .routingTable(RoutingTable.builder().build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, false)); WaitForIndexColorStep step = new WaitForIndexColorStep(randomStepKey(), randomStepKey(), ClusterHealthStatus.YELLOW); - ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState); + ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), state); assertThat(result.complete(), is(false)); SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.informationContext(); assertThat(info, notNullValue()); @@ -160,13 +162,15 @@ public void testConditionMetForYellow() { ); IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).addShard(shardRouting).build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, false).build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) + .putRoutingTable(project.id(), RoutingTable.builder().add(indexRoutingTable).build()) + .build() + .projectState(project.id()); WaitForIndexColorStep step = new WaitForIndexColorStep(randomStepKey(), randomStepKey(), ClusterHealthStatus.YELLOW); - ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState); + ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), state); assertThat(result.complete(), is(true)); assertThat(result.informationContext(), nullValue()); } @@ -186,13 +190,15 @@ public void testConditionNotMetForYellow() { ); IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).addShard(shardRouting).build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, false).build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) + .putRoutingTable(project.id(), RoutingTable.builder().add(indexRoutingTable).build()) + .build() + .projectState(project.id()); WaitForIndexColorStep step = new WaitForIndexColorStep(randomStepKey(), randomStepKey(), ClusterHealthStatus.YELLOW); - ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState); + ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), state); assertThat(result.complete(), is(false)); SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.informationContext(); assertThat(info, notNullValue()); @@ -206,13 +212,10 @@ public void testConditionNotMetNoIndexRoutingTableForYellow() { .numberOfReplicas(0) .build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .routingTable(RoutingTable.builder().build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, false)); WaitForIndexColorStep step = new WaitForIndexColorStep(randomStepKey(), randomStepKey(), ClusterHealthStatus.YELLOW); - ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState); + ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), state); assertThat(result.complete(), is(false)); SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.informationContext(); assertThat(info, notNullValue()); @@ -236,13 +239,15 @@ public void testStepReturnsFalseIfTargetIndexIsMissing() { ); IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(originalIndex.getIndex()).addShard(shardRouting).build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder().put(originalIndex, true).build()) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()).put(originalIndex, false).build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) + .putRoutingTable(project.id(), RoutingTable.builder().add(indexRoutingTable).build()) + .build() + .projectState(project.id()); WaitForIndexColorStep step = new WaitForIndexColorStep(randomStepKey(), randomStepKey(), ClusterHealthStatus.GREEN, indexPrefix); - ClusterStateWaitStep.Result result = step.isConditionMet(originalIndex.getIndex(), clusterState); + ClusterStateWaitStep.Result result = step.isConditionMet(originalIndex.getIndex(), state); assertThat(result.complete(), is(false)); SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.informationContext(); String targetIndex = indexPrefix + originalIndex.getIndex().getName(); @@ -296,13 +301,18 @@ public void testStepWaitsForTargetIndexHealthWhenPrefixConfigured() { .addShard(targetShardRouting) .build(); - ClusterState clusterTargetInitializing = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder().put(originalIndex, true).put(targetIndex, true).build()) - .routingTable(RoutingTable.builder().add(originalIndexRoutingTable).add(targetIndexRoutingTable).build()) + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(originalIndex, false) + .put(targetIndex, false) .build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) + .putRoutingTable(project.id(), RoutingTable.builder().add(originalIndexRoutingTable).add(targetIndexRoutingTable).build()) + .build() + .projectState(project.id()); WaitForIndexColorStep step = new WaitForIndexColorStep(randomStepKey(), randomStepKey(), ClusterHealthStatus.GREEN); - ClusterStateWaitStep.Result result = step.isConditionMet(originalIndex.getIndex(), clusterTargetInitializing); + ClusterStateWaitStep.Result result = step.isConditionMet(originalIndex.getIndex(), state); assertThat(result.complete(), is(false)); SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.informationContext(); assertThat(info.message(), is("index is not green; not all shards are active")); @@ -319,13 +329,18 @@ public void testStepWaitsForTargetIndexHealthWhenPrefixConfigured() { .addShard(targetShardRouting) .build(); - ClusterState clusterTargetInitializing = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder().put(originalIndex, true).put(targetIndex, true).build()) - .routingTable(RoutingTable.builder().add(originalIndexRoutingTable).add(targetIndexRoutingTable).build()) + final var project = ProjectMetadata.builder(randomProjectIdOrDefault()) + .put(originalIndex, false) + .put(targetIndex, false) .build(); + ProjectState state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) + .putRoutingTable(project.id(), RoutingTable.builder().add(originalIndexRoutingTable).add(targetIndexRoutingTable).build()) + .build() + .projectState(project.id()); WaitForIndexColorStep step = new WaitForIndexColorStep(randomStepKey(), randomStepKey(), ClusterHealthStatus.GREEN); - ClusterStateWaitStep.Result result = step.isConditionMet(originalIndex.getIndex(), clusterTargetInitializing); + ClusterStateWaitStep.Result result = step.isConditionMet(originalIndex.getIndex(), state); assertThat(result.complete(), is(true)); assertThat(result.informationContext(), nullValue()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexingCompleteStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexingCompleteStepTests.java index a0982e72b11af..e14cc1d18c868 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexingCompleteStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexingCompleteStepTests.java @@ -6,10 +6,9 @@ */ package org.elasticsearch.xpack.core.ilm; -import org.elasticsearch.cluster.ClusterName; -import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.ProjectMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexVersion; @@ -59,12 +58,10 @@ public void testConditionMet() { .numberOfReplicas(0) .build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, false)); WaitForIndexingCompleteStep step = createRandomInstance(); - ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState); + ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), state); assertThat(result.complete(), is(true)); assertThat(result.informationContext(), nullValue()); } @@ -76,12 +73,10 @@ public void testConditionMetNotAFollowerIndex() { .numberOfReplicas(0) .build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, false)); WaitForIndexingCompleteStep step = createRandomInstance(); - ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState); + ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), state); assertThat(result.complete(), is(true)); assertThat(result.informationContext(), nullValue()); } @@ -98,12 +93,10 @@ public void testConditionNotMet() { .numberOfReplicas(0) .build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder().put(indexMetadata, true).build()) - .build(); + ProjectState state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, false)); WaitForIndexingCompleteStep step = createRandomInstance(); - ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState); + ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), state); assertThat(result.complete(), is(false)); assertThat(result.informationContext(), notNullValue()); WaitForIndexingCompleteStep.IndexingNotCompleteInfo info = (WaitForIndexingCompleteStep.IndexingNotCompleteInfo) result @@ -118,10 +111,10 @@ public void testConditionNotMet() { } public void testIndexDeleted() { - ClusterState clusterState = ClusterState.builder(new ClusterName("cluster")).metadata(Metadata.builder().build()).build(); + ProjectState state = projectStateWithEmptyProject(); WaitForIndexingCompleteStep step = createRandomInstance(); - ClusterStateWaitStep.Result result = step.isConditionMet(new Index("this-index-doesnt-exist", "uuid"), clusterState); + ClusterStateWaitStep.Result result = step.isConditionMet(new Index("this-index-doesnt-exist", "uuid"), state); assertThat(result.complete(), is(false)); assertThat(result.informationContext(), nullValue()); } diff --git a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleInitialisationTests.java b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleInitialisationTests.java index aa8b2d69dbae3..cdb3716078de2 100644 --- a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleInitialisationTests.java +++ b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleInitialisationTests.java @@ -10,6 +10,7 @@ import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; import org.elasticsearch.cluster.routing.RoutingNode; import org.elasticsearch.common.io.stream.NamedWriteable; @@ -586,12 +587,8 @@ public String getWriteableName() { } @Override - public Result isConditionMet(Index index, ClusterState clusterState) { - boolean complete = clusterState.metadata() - .getProject() - .index("test") - .getSettings() - .getAsBoolean("index.lifecycle.test.complete", false); + public Result isConditionMet(Index index, ProjectState currentState) { + boolean complete = currentState.metadata().index("test").getSettings().getAsBoolean("index.lifecycle.test.complete", false); return new Result(complete, null); } } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTask.java index 29b808547b206..831f38cdceef3 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTask.java @@ -166,7 +166,7 @@ private ClusterState executeWaitStep(ClusterState state, Step currentStep) { currentStep.getClass().getSimpleName(), currentStep.getKey() ); - ClusterStateWaitStep.Result result = ((ClusterStateWaitStep) currentStep).isConditionMet(index, state); + ClusterStateWaitStep.Result result = ((ClusterStateWaitStep) currentStep).isConditionMet(index, state.projectState()); // some steps can decide to change the next step to execute after waiting for some time for the condition // to be met (eg. {@link LifecycleSettings#LIFECYCLE_STEP_WAIT_TIME_THRESHOLD_SETTING}, so it's important we // re-evaluate what the next step is after we evaluate the condition diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java index bd39932890c69..ac4738fb787b2 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java @@ -1094,7 +1094,7 @@ public long getExecuteCount() { } @Override - public Result isConditionMet(Index index, ClusterState clusterState) { + public Result isConditionMet(Index index, ProjectState currentState) { executeCount++; if (exception != null) { throw exception;