diff --git a/server/src/main/java/org/elasticsearch/cluster/ProjectState.java b/server/src/main/java/org/elasticsearch/cluster/ProjectState.java index 3ce3c448c879a..7e78ddf5bcbf1 100644 --- a/server/src/main/java/org/elasticsearch/cluster/ProjectState.java +++ b/server/src/main/java/org/elasticsearch/cluster/ProjectState.java @@ -76,6 +76,13 @@ public ClusterState updatedState(Consumer projectBuilde return ClusterState.builder(cluster).putProjectMetadata(projectBuilder).build(); } + /** + * Build a new {@link ClusterState} with the updated project. + */ + public ClusterState updatedState(ProjectMetadata updatedProject) { + return ClusterState.builder(cluster).putProjectMetadata(updatedProject).build(); + } + /** * Build a new {@link ProjectState} with the updated {@code project}. */ diff --git a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ClusterStateWaitThresholdBreachTests.java b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ClusterStateWaitThresholdBreachTests.java index ab8ed46b9bc04..fc3bfd437e5c3 100644 --- a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ClusterStateWaitThresholdBreachTests.java +++ b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ClusterStateWaitThresholdBreachTests.java @@ -149,7 +149,7 @@ public ClusterState execute(ClusterState currentState) throws Exception { nowWayBackInThePastSupplier, indexLifecycleService.getPolicyRegistry(), state -> {} - ).execute(currentState); + ).execute(currentState.projectState()); } @Override 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 831f38cdceef3..fc40d352483f0 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 @@ -11,9 +11,9 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateUpdateTask; +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.common.Strings; import org.elasticsearch.index.Index; import org.elasticsearch.xcontent.ToXContentObject; @@ -84,22 +84,22 @@ Step.StepKey getNextStepKey() { * @throws IOException if any exceptions occur */ @Override - public ClusterState doExecute(final ClusterState currentState) throws IOException { + public ClusterState doExecute(final ProjectState currentState) throws IOException { Step currentStep = startStep; - IndexMetadata indexMetadata = currentState.metadata().getProject().index(index); + IndexMetadata indexMetadata = currentState.metadata().index(index); if (indexMetadata == null) { logger.debug("lifecycle for index [{}] executed but index no longer exists", index.getName()); // This index doesn't exist any more, there's nothing to execute currently - return currentState; + return currentState.cluster(); } Step registeredCurrentStep = IndexLifecycleRunner.getCurrentStep(policyStepsRegistry, policy, indexMetadata); if (currentStep.equals(registeredCurrentStep) == false) { // either we are no longer the master or the step is now // not the same as when we submitted the update task. In // either case we don't want to do anything now - return currentState; + return currentState.cluster(); } - ClusterState state = currentState; + ProjectState state = currentState; // We can do cluster state steps all together until we // either get to a step that isn't a cluster state step or a // cluster state wait step returns not completed @@ -114,7 +114,7 @@ public ClusterState doExecute(final ClusterState currentState) throws IOExceptio return moveToErrorStep(state, currentStep.getKey(), exception); } if (nextStepKey == null) { - return state; + return state.cluster(); } else { state = moveToNextStep(state); } @@ -123,14 +123,14 @@ public ClusterState doExecute(final ClusterState currentState) throws IOExceptio // loop, if we are about to go into a new phase, return so that // other processing can occur if (currentStep.getKey().phase().equals(currentStep.getNextStepKey().phase()) == false) { - return state; + return state.cluster(); } currentStep = policyStepsRegistry.getStep(indexMetadata, currentStep.getNextStepKey()); } - return state; + return state.cluster(); } - private ClusterState executeActionStep(ClusterState state, Step currentStep) { + private ProjectState executeActionStep(ProjectState state, Step currentStep) { // cluster state action step so do the action and // move the cluster state to the next step logger.trace( @@ -140,7 +140,7 @@ private ClusterState executeActionStep(ClusterState state, Step currentStep) { currentStep.getKey() ); ClusterStateActionStep actionStep = (ClusterStateActionStep) currentStep; - state = actionStep.performAction(index, state.projectState()).cluster(); + state = actionStep.performAction(index, state); // If this step (usually a CopyExecutionStateStep step) has brought the // index to where it needs to have async actions invoked, then add that // index to the list so that when the new cluster state has been @@ -153,7 +153,7 @@ private ClusterState executeActionStep(ClusterState state, Step currentStep) { return state; } - private ClusterState executeWaitStep(ClusterState state, Step currentStep) { + private ProjectState executeWaitStep(ProjectState state, Step currentStep) { // cluster state wait step so evaluate the // condition, if the condition is met move to the // next step, if its not met return the current @@ -166,7 +166,7 @@ private ClusterState executeWaitStep(ClusterState state, Step currentStep) { currentStep.getClass().getSimpleName(), currentStep.getKey() ); - ClusterStateWaitStep.Result result = ((ClusterStateWaitStep) currentStep).isConditionMet(index, state.projectState()); + ClusterStateWaitStep.Result result = ((ClusterStateWaitStep) currentStep).isConditionMet(index, state); // 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 @@ -198,35 +198,23 @@ private ClusterState executeWaitStep(ClusterState state, Step currentStep) { if (stepInfo == null) { return state; } - return ClusterState.builder(state) - .putProjectMetadata(IndexLifecycleTransition.addStepInfoToProject(index, state.metadata().getProject(), stepInfo)) - .build(); + return state.updateProject(IndexLifecycleTransition.addStepInfoToProject(index, state.metadata(), stepInfo)); } } - private ClusterState moveToNextStep(ClusterState state) { + private ProjectState moveToNextStep(ProjectState state) { if (nextStepKey == null) { return state; } logger.trace("[{}] moving cluster state to next step [{}]", index.getName(), nextStepKey); - return ClusterState.builder(state) - .putProjectMetadata( - IndexLifecycleTransition.moveIndexToStep( - index, - state.metadata().getProject(), - nextStepKey, - nowSupplier, - policyStepsRegistry, - false - ) - ) - .build(); + return state.updateProject( + IndexLifecycleTransition.moveIndexToStep(index, state.metadata(), nextStepKey, nowSupplier, policyStepsRegistry, false) + ); } @Override - public void onClusterStateProcessed(ClusterState newState) { - final Metadata metadata = newState.metadata(); - final IndexMetadata indexMetadata = metadata.getProject().index(index); + public void onClusterStateProcessed(ProjectState newState) { + final IndexMetadata indexMetadata = newState.metadata().index(index); if (indexMetadata != null) { LifecycleExecutionState exState = indexMetadata.getLifecycleExecutionState(); @@ -246,16 +234,16 @@ public void onClusterStateProcessed(ClusterState newState) { // After the cluster state has been processed and we have moved // to a new step, we need to conditionally execute the step iff // it is an `AsyncAction` so that it is executed exactly once. - lifecycleRunner.maybeRunAsyncAction(newState, indexMetadata, policy, nextStepKey); + lifecycleRunner.maybeRunAsyncAction(newState.cluster(), indexMetadata, policy, nextStepKey); } } assert indexToStepKeysForAsyncActions.size() <= 1 : "we expect a maximum of one single spawned index currently"; for (Map.Entry indexAndStepKey : indexToStepKeysForAsyncActions.entrySet()) { final String indexName = indexAndStepKey.getKey(); final Step.StepKey nextStep = indexAndStepKey.getValue(); - final IndexMetadata indexMeta = metadata.getProject().index(indexName); + final IndexMetadata indexMeta = newState.metadata().index(indexName); if (indexMeta != null) { - if (newState.metadata().getProject().isIndexManagedByILM(indexMeta)) { + if (newState.metadata().isIndexManagedByILM(indexMeta)) { if (nextStep != null && nextStep != TerminalPolicyStep.KEY) { logger.trace( "[{}] index has been spawed from a different index's ({}) " @@ -265,7 +253,7 @@ public void onClusterStateProcessed(ClusterState newState) { nextStep ); final String policyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMeta.getSettings()); - lifecycleRunner.maybeRunAsyncAction(newState, indexMeta, policyName, nextStep); + lifecycleRunner.maybeRunAsyncAction(newState.cluster(), indexMeta, policyName, nextStep); } } } @@ -277,7 +265,7 @@ public void handleFailure(Exception e) { logger.warn(() -> format("policy [%s] for index [%s] failed on step [%s].", policy, index, startStep.getKey()), e); } - private ClusterState moveToErrorStep(final ClusterState state, Step.StepKey currentStepKey, Exception cause) { + private ClusterState moveToErrorStep(final ProjectState state, Step.StepKey currentStepKey, Exception cause) { this.failure = cause; logger.warn( () -> format( @@ -288,12 +276,9 @@ private ClusterState moveToErrorStep(final ClusterState state, Step.StepKey curr ), cause ); - final var project = state.metadata().getProject(); - return ClusterState.builder(state) - .putProjectMetadata( - IndexLifecycleTransition.moveIndexToErrorStep(index, project, cause, nowSupplier, policyStepsRegistry::getStep) - ) - .build(); + return state.updatedState( + IndexLifecycleTransition.moveIndexToErrorStep(index, state.metadata(), cause, nowSupplier, policyStepsRegistry::getStep) + ); } @Override diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleClusterStateUpdateTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleClusterStateUpdateTask.java index 193a4b110c596..3a024027f6aaa 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleClusterStateUpdateTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleClusterStateUpdateTask.java @@ -11,6 +11,7 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateTaskListener; import org.elasticsearch.cluster.ClusterStateUpdateTask; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.common.util.concurrent.ListenableFuture; import org.elasticsearch.index.Index; import org.elasticsearch.xpack.core.ilm.Step; @@ -42,18 +43,22 @@ final Step.StepKey getCurrentStepKey() { private boolean executed; - public final ClusterState execute(ClusterState currentState) throws Exception { + /** + * Executes the task on the current project state. We return a cluster state instead of a project state because we're only interested + * in the resulting cluster state, so we avoid constructing a project state just to only access the cluster state. + */ + public final ClusterState execute(ProjectState currentState) throws Exception { assert executed == false; final ClusterState updatedState = doExecute(currentState); - if (currentState != updatedState) { + if (currentState.cluster() != updatedState) { executed = true; } return updatedState; } - protected abstract ClusterState doExecute(ClusterState currentState) throws Exception; + protected abstract ClusterState doExecute(ProjectState currentState) throws Exception; - public final void clusterStateProcessed(ClusterState oldState, ClusterState newState) { + public final void clusterStateProcessed(ProjectState newState) { listener.onResponse(null); if (executed) { onClusterStateProcessed(newState); @@ -68,7 +73,7 @@ public final void onFailure(Exception e) { /** * Add a listener that is resolved once this update has been processed or failed and before either the - * {@link #onClusterStateProcessed(ClusterState)} or the {@link #handleFailure(Exception)} hooks are + * {@link #onClusterStateProcessed(ProjectState)} or the {@link #handleFailure(Exception)} hooks are * executed. */ public final void addListener(ActionListener actionListener) { @@ -78,10 +83,10 @@ public final void addListener(ActionListener actionListener) { /** * This method is functionally the same as {@link ClusterStateUpdateTask#clusterStateProcessed} * and implementations can override it as they would override {@code ClusterStateUpdateTask#clusterStateProcessed}. - * The only difference to {@link ClusterStateUpdateTask#clusterStateProcessed} is that if the {@link #execute(ClusterState)} + * The only difference to {@link ClusterStateUpdateTask#clusterStateProcessed} is that if the {@link #execute(ProjectState)} * implementation was a noop and returned the input cluster state, then this method will not be invoked. */ - protected void onClusterStateProcessed(ClusterState newState) {} + protected void onClusterStateProcessed(ProjectState newState) {} @Override public abstract boolean equals(Object other); diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java index 0721f1e1d5574..6e7b19ecf32dc 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java @@ -12,6 +12,7 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; import org.elasticsearch.cluster.ClusterStateTaskExecutor; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; import org.elasticsearch.cluster.metadata.Metadata; @@ -67,12 +68,13 @@ public ClusterState execute(BatchExecutionContext task.clusterStateProcessed(batchExecutionContext.initialState(), publishedState) - ); + taskContext.success(publishedState -> task.clusterStateProcessed(publishedState.projectState(projectId))); } catch (Exception e) { taskContext.onFailure(e); } @@ -477,11 +479,11 @@ private void moveToStep(Index index, String policy, Step.StepKey currentStepKey, currentStepKey, newStepKey ), - new MoveToNextStepUpdateTask(index, policy, currentStepKey, newStepKey, nowSupplier, stepRegistry, clusterState -> { - IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index); + new MoveToNextStepUpdateTask(index, policy, currentStepKey, newStepKey, nowSupplier, stepRegistry, state -> { + IndexMetadata indexMetadata = state.metadata().index(index); registerSuccessfulOperation(indexMetadata); if (newStepKey != null && newStepKey != TerminalPolicyStep.KEY && indexMetadata != null) { - maybeRunAsyncAction(clusterState, indexMetadata, policy, newStepKey); + maybeRunAsyncAction(state.cluster(), indexMetadata, policy, newStepKey); } }) ); @@ -497,8 +499,8 @@ private void moveToErrorStep(Index index, String policy, Step.StepKey currentSte ); submitUnlessAlreadyQueued( Strings.format("ilm-move-to-error-step {policy [%s], index [%s], currentStep [%s]}", policy, index.getName(), currentStepKey), - new MoveToErrorStepUpdateTask(index, policy, currentStepKey, e, nowSupplier, stepRegistry::getStep, clusterState -> { - IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index); + new MoveToErrorStepUpdateTask(index, policy, currentStepKey, e, nowSupplier, stepRegistry::getStep, state -> { + IndexMetadata indexMetadata = state.metadata().index(index); registerFailedOperation(indexMetadata, e); }) ); @@ -656,15 +658,15 @@ private final class MoveToRetryFailedStepUpdateTask extends IndexLifecycleCluste } @Override - protected ClusterState doExecute(ClusterState currentState) { + protected ClusterState doExecute(ProjectState currentState) { final var updatedProject = IndexLifecycleTransition.moveIndexToPreviouslyFailedStep( - currentState.metadata().getProject(), + currentState.metadata(), index.getName(), nowSupplier, stepRegistry, true ); - return ClusterState.builder(currentState).putProjectMetadata(updatedProject).build(); + return currentState.updatedState(updatedProject); } @Override @@ -693,8 +695,8 @@ protected void handleFailure(Exception e) { } @Override - protected void onClusterStateProcessed(ClusterState newState) { - IndexMetadata newIndexMeta = newState.metadata().getProject().index(index); + protected void onClusterStateProcessed(ProjectState newState) { + IndexMetadata newIndexMeta = newState.metadata().index(index); if (newIndexMeta == null) { // index was deleted return; @@ -713,7 +715,7 @@ protected void onClusterStateProcessed(ClusterState newState) { index, stepKey ); - maybeRunAsyncAction(newState, newIndexMeta, policy, stepKey); + maybeRunAsyncAction(newState.cluster(), newIndexMeta, policy, stepKey); } } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTask.java index 3a96252ab80fd..26e5d5e918798 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTask.java @@ -12,6 +12,7 @@ import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.NotMasterException; +import org.elasticsearch.cluster.ProjectState; import org.elasticsearch.cluster.coordination.FailedToCommitClusterStateException; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.LifecycleExecutionState; @@ -32,7 +33,7 @@ public class MoveToErrorStepUpdateTask extends IndexLifecycleClusterStateUpdateT private final String policy; private final Step.StepKey currentStepKey; private final BiFunction stepLookupFunction; - private final Consumer stateChangeConsumer; + private final Consumer stateChangeConsumer; private final LongSupplier nowSupplier; private final Exception cause; @@ -43,7 +44,7 @@ public MoveToErrorStepUpdateTask( Exception cause, LongSupplier nowSupplier, BiFunction stepLookupFunction, - Consumer stateChangeConsumer + Consumer stateChangeConsumer ) { super(index, currentStepKey); this.index = index; @@ -56,28 +57,27 @@ public MoveToErrorStepUpdateTask( } @Override - protected ClusterState doExecute(ClusterState currentState) throws Exception { - final var project = currentState.getMetadata().getProject(); - IndexMetadata idxMeta = project.index(index); + protected ClusterState doExecute(ProjectState currentState) throws Exception { + IndexMetadata idxMeta = currentState.metadata().index(index); if (idxMeta == null) { // Index must have been since deleted, ignore it - return currentState; + return currentState.cluster(); } LifecycleExecutionState lifecycleState = idxMeta.getLifecycleExecutionState(); if (policy.equals(idxMeta.getLifecyclePolicyName()) && currentStepKey.equals(Step.getCurrentStepKey(lifecycleState))) { - return ClusterState.builder(currentState) - .putProjectMetadata(IndexLifecycleTransition.moveIndexToErrorStep(index, project, cause, nowSupplier, stepLookupFunction)) - .build(); + return currentState.updatedState( + IndexLifecycleTransition.moveIndexToErrorStep(index, currentState.metadata(), cause, nowSupplier, stepLookupFunction) + ); } else { // either the policy has changed or the step is now // not the same as when we submitted the update task. In // either case we don't want to do anything now - return currentState; + return currentState.cluster(); } } @Override - public void onClusterStateProcessed(ClusterState newState) { + public void onClusterStateProcessed(ProjectState newState) { stateChangeConsumer.accept(newState); } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTask.java index 9c04c635aab92..b0e03902864b2 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTask.java @@ -9,6 +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.index.Index; @@ -28,7 +29,7 @@ public class MoveToNextStepUpdateTask extends IndexLifecycleClusterStateUpdateTa private final Step.StepKey nextStepKey; private final LongSupplier nowSupplier; private final PolicyStepsRegistry stepRegistry; - private final Consumer stateChangeConsumer; + private final Consumer stateChangeConsumer; public MoveToNextStepUpdateTask( Index index, @@ -37,7 +38,7 @@ public MoveToNextStepUpdateTask( Step.StepKey nextStepKey, LongSupplier nowSupplier, PolicyStepsRegistry stepRegistry, - Consumer stateChangeConsumer + Consumer stateChangeConsumer ) { super(index, currentStepKey); this.policy = policy; @@ -48,29 +49,28 @@ public MoveToNextStepUpdateTask( } @Override - public ClusterState doExecute(ClusterState currentState) { - final var project = currentState.metadata().getProject(); - IndexMetadata idxMeta = project.index(index); + public ClusterState doExecute(ProjectState currentState) { + IndexMetadata idxMeta = currentState.metadata().index(index); if (idxMeta == null) { // Index must have been since deleted, ignore it - return currentState; + return currentState.cluster(); } LifecycleExecutionState lifecycleState = idxMeta.getLifecycleExecutionState(); if (policy.equals(idxMeta.getLifecyclePolicyName()) && currentStepKey.equals(Step.getCurrentStepKey(lifecycleState))) { logger.trace("moving [{}] to next step ({})", index.getName(), nextStepKey); - return ClusterState.builder(currentState) - .putProjectMetadata(IndexLifecycleTransition.moveIndexToStep(index, project, nextStepKey, nowSupplier, stepRegistry, false)) - .build(); + return currentState.updatedState( + IndexLifecycleTransition.moveIndexToStep(index, currentState.metadata(), nextStepKey, nowSupplier, stepRegistry, false) + ); } else { // either the policy has changed or the step is now // not the same as when we submitted the update task. In // either case we don't want to do anything now - return currentState; + return currentState.cluster(); } } @Override - public void onClusterStateProcessed(ClusterState newState) { + public void onClusterStateProcessed(ProjectState newState) { stateChangeConsumer.accept(newState); } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/SetStepInfoUpdateTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/SetStepInfoUpdateTask.java index 0370169a73e41..3f5e794461172 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/SetStepInfoUpdateTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/SetStepInfoUpdateTask.java @@ -11,6 +11,7 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchException; 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.index.Index; @@ -45,23 +46,20 @@ ToXContentObject getStepInfo() { } @Override - protected ClusterState doExecute(ClusterState currentState) throws IOException { - final var project = currentState.metadata().getProject(); - IndexMetadata idxMeta = project.index(index); + protected ClusterState doExecute(ProjectState currentState) throws IOException { + IndexMetadata idxMeta = currentState.metadata().index(index); if (idxMeta == null) { // Index must have been since deleted, ignore it - return currentState; + return currentState.cluster(); } LifecycleExecutionState lifecycleState = idxMeta.getLifecycleExecutionState(); if (policy.equals(idxMeta.getLifecyclePolicyName()) && Objects.equals(currentStepKey, Step.getCurrentStepKey(lifecycleState))) { - return ClusterState.builder(currentState) - .putProjectMetadata(IndexLifecycleTransition.addStepInfoToProject(index, project, stepInfo)) - .build(); + return currentState.updatedState(IndexLifecycleTransition.addStepInfoToProject(index, currentState.metadata(), stepInfo)); } else { // either the policy has changed or the step is now // not the same as when we submitted the update task. In // either case we don't want to do anything now - return currentState; + return currentState.cluster(); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTaskTests.java index 6b3636dff1505..11aa8816e55ee 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTaskTests.java @@ -10,9 +10,10 @@ import org.elasticsearch.client.internal.Client; 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.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; import org.elasticsearch.cluster.node.DiscoveryNodes; @@ -59,7 +60,7 @@ public class ExecuteStepsUpdateTaskTests extends ESTestCase { private static final StepKey secondStepKey = new StepKey("first_phase", "action_1", "step_2"); private static final StepKey thirdStepKey = new StepKey("first_phase", "action_1", "step_3"); private static final StepKey invalidStepKey = new StepKey("invalid", "invalid", "invalid"); - private ClusterState clusterState; + private ProjectState state; private PolicyStepsRegistry policyStepsRegistry; private String mixedPolicyName; private String allClusterPolicyName; @@ -138,8 +139,7 @@ private void setupIndexPolicy(String policyName) { .numberOfReplicas(randomIntBetween(0, 5)) .build(); index = indexMetadata.getIndex(); - Metadata metadata = Metadata.builder() - .persistentSettings(settings(IndexVersion.current()).build()) + ProjectMetadata project = ProjectMetadata.builder(randomProjectIdOrDefault()) .putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata) .put(IndexMetadata.builder(indexMetadata)) .build(); @@ -148,11 +148,12 @@ private void setupIndexPolicy(String policyName) { .applySettings(NodeRoles.masterNode(settings(IndexVersion.current()).build())) .address(new TransportAddress(TransportAddress.META_ADDRESS, 9300)) .build(); - clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(metadata) + state = ClusterState.builder(ClusterName.DEFAULT) + .putProjectMetadata(project) .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) - .build(); - policyStepsRegistry.update(clusterState.metadata().getProject().custom(IndexLifecycleMetadata.TYPE)); + .build() + .projectState(project.id()); + policyStepsRegistry.update(state.metadata().custom(IndexLifecycleMetadata.TYPE)); } public void testNeverExecuteNonClusterStateStep() throws Exception { @@ -160,7 +161,7 @@ public void testNeverExecuteNonClusterStateStep() throws Exception { Step startStep = policyStepsRegistry.getStep(indexMetadata, thirdStepKey); long now = randomNonNegativeLong(); ExecuteStepsUpdateTask task = new ExecuteStepsUpdateTask(mixedPolicyName, index, startStep, policyStepsRegistry, null, () -> now); - assertThat(task.execute(clusterState), sameInstance(clusterState)); + assertThat(task.execute(state), sameInstance(state.cluster())); } public void testSuccessThenFailureUnsetNextKey() throws Exception { @@ -169,8 +170,8 @@ public void testSuccessThenFailureUnsetNextKey() throws Exception { Step startStep = policyStepsRegistry.getStep(indexMetadata, firstStepKey); long now = randomNonNegativeLong(); ExecuteStepsUpdateTask task = new ExecuteStepsUpdateTask(mixedPolicyName, index, startStep, policyStepsRegistry, null, () -> now); - ClusterState newState = task.execute(clusterState); - LifecycleExecutionState lifecycleState = newState.getMetadata().getProject().index(index).getLifecycleExecutionState(); + ClusterState newState = task.execute(state); + LifecycleExecutionState lifecycleState = getLifecycleExecutionState(newState); StepKey currentStepKey = Step.getCurrentStepKey(lifecycleState); assertThat(currentStepKey, equalTo(secondStepKey)); assertThat(firstStep.getExecuteCount(), equalTo(1L)); @@ -186,8 +187,8 @@ public void testExecuteUntilFirstNonClusterStateStep() throws Exception { Step startStep = policyStepsRegistry.getStep(indexMetadata, secondStepKey); long now = randomNonNegativeLong(); ExecuteStepsUpdateTask task = new ExecuteStepsUpdateTask(mixedPolicyName, index, startStep, policyStepsRegistry, null, () -> now); - ClusterState newState = task.execute(clusterState); - LifecycleExecutionState lifecycleState = newState.getMetadata().getProject().index(index).getLifecycleExecutionState(); + ClusterState newState = task.execute(state); + LifecycleExecutionState lifecycleState = getLifecycleExecutionState(newState); StepKey currentStepKey = Step.getCurrentStepKey(lifecycleState); assertThat(currentStepKey, equalTo(thirdStepKey)); assertThat(firstStep.getExecuteCount(), equalTo(0L)); @@ -199,23 +200,19 @@ public void testExecuteUntilFirstNonClusterStateStep() throws Exception { public void testExecuteInvalidStartStep() throws Exception { // Unset the index's phase/action/step to simulate starting from scratch - LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder( - clusterState.getMetadata().getProject().index(index).getLifecycleExecutionState() - ); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(getLifecycleExecutionState(state.cluster())); lifecycleState.setPhase(null); lifecycleState.setAction(null); lifecycleState.setStep(null); - clusterState = ClusterState.builder(clusterState) - .metadata( - Metadata.builder(clusterState.getMetadata()) - .put( - IndexMetadata.builder(clusterState.getMetadata().getProject().index(index)) - .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) - ) - ) - .build(); + state = state.updateProject( + ProjectMetadata.builder(state.metadata()) + .put( + IndexMetadata.builder(state.metadata().index(index)).putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) + ) + .build() + ); - policyStepsRegistry.update(clusterState.metadata().getProject().custom(IndexLifecycleMetadata.TYPE)); + policyStepsRegistry.update(state.metadata().custom(IndexLifecycleMetadata.TYPE)); Step invalidStep = new MockClusterStateActionStep(firstStepKey, secondStepKey); long now = randomNonNegativeLong(); @@ -227,8 +224,8 @@ public void testExecuteInvalidStartStep() throws Exception { null, () -> now ); - ClusterState newState = task.execute(clusterState); - assertSame(newState, clusterState); + ClusterState newState = task.execute(state); + assertSame(newState, state.cluster()); } public void testExecuteIncompleteWaitStepNoInfo() throws Exception { @@ -237,8 +234,8 @@ public void testExecuteIncompleteWaitStepNoInfo() throws Exception { Step startStep = policyStepsRegistry.getStep(indexMetadata, secondStepKey); long now = randomNonNegativeLong(); ExecuteStepsUpdateTask task = new ExecuteStepsUpdateTask(mixedPolicyName, index, startStep, policyStepsRegistry, null, () -> now); - ClusterState newState = task.execute(clusterState); - LifecycleExecutionState lifecycleState = newState.getMetadata().getProject().index(index).getLifecycleExecutionState(); + ClusterState newState = task.execute(state); + LifecycleExecutionState lifecycleState = getLifecycleExecutionState(newState); StepKey currentStepKey = Step.getCurrentStepKey(lifecycleState); assertThat(currentStepKey, equalTo(secondStepKey)); assertThat(firstStep.getExecuteCount(), equalTo(0L)); @@ -256,8 +253,8 @@ public void testExecuteIncompleteWaitStepWithInfo() throws Exception { Step startStep = policyStepsRegistry.getStep(indexMetadata, secondStepKey); long now = randomNonNegativeLong(); ExecuteStepsUpdateTask task = new ExecuteStepsUpdateTask(mixedPolicyName, index, startStep, policyStepsRegistry, null, () -> now); - ClusterState newState = task.execute(clusterState); - LifecycleExecutionState lifecycleState = newState.getMetadata().getProject().index(index).getLifecycleExecutionState(); + ClusterState newState = task.execute(state); + LifecycleExecutionState lifecycleState = getLifecycleExecutionState(newState); StepKey currentStepKey = Step.getCurrentStepKey(lifecycleState); assertThat(currentStepKey, equalTo(secondStepKey)); assertThat(firstStep.getExecuteCount(), equalTo(0L)); @@ -283,8 +280,8 @@ public void testClusterActionStepThrowsException() throws Exception { Step startStep = policyStepsRegistry.getStep(indexMetadata, firstStepKey); long now = randomNonNegativeLong(); ExecuteStepsUpdateTask task = new ExecuteStepsUpdateTask(mixedPolicyName, index, startStep, policyStepsRegistry, null, () -> now); - ClusterState newState = task.execute(clusterState); - LifecycleExecutionState lifecycleState = newState.getMetadata().getProject().index(index).getLifecycleExecutionState(); + ClusterState newState = task.execute(state); + LifecycleExecutionState lifecycleState = getLifecycleExecutionState(newState); StepKey currentStepKey = Step.getCurrentStepKey(lifecycleState); assertThat(currentStepKey, equalTo(new StepKey(firstStepKey.phase(), firstStepKey.action(), ErrorStep.NAME))); assertThat(firstStep.getExecuteCount(), equalTo(1L)); @@ -303,8 +300,8 @@ public void testClusterWaitStepThrowsException() throws Exception { Step startStep = policyStepsRegistry.getStep(indexMetadata, firstStepKey); long now = randomNonNegativeLong(); ExecuteStepsUpdateTask task = new ExecuteStepsUpdateTask(mixedPolicyName, index, startStep, policyStepsRegistry, null, () -> now); - ClusterState newState = task.execute(clusterState); - LifecycleExecutionState lifecycleState = newState.getMetadata().getProject().index(index).getLifecycleExecutionState(); + ClusterState newState = task.execute(state); + LifecycleExecutionState lifecycleState = getLifecycleExecutionState(newState); StepKey currentStepKey = Step.getCurrentStepKey(lifecycleState); assertThat(currentStepKey, equalTo(new StepKey(firstStepKey.phase(), firstStepKey.action(), ErrorStep.NAME))); assertThat(firstStep.getExecuteCount(), equalTo(1L)); @@ -316,21 +313,21 @@ public void testClusterWaitStepThrowsException() throws Exception { } private void setStateToKey(StepKey stepKey) throws IOException { - LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder( - clusterState.getMetadata().getProject().index(index).getLifecycleExecutionState() - ); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(getLifecycleExecutionState(state.cluster())); lifecycleState.setPhase(stepKey.phase()); lifecycleState.setAction(stepKey.action()); lifecycleState.setStep(stepKey.name()); - clusterState = ClusterState.builder(clusterState) - .metadata( - Metadata.builder(clusterState.getMetadata()) - .put( - IndexMetadata.builder(clusterState.getMetadata().getProject().index(index)) - .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) - ) - ) - .build(); - policyStepsRegistry.update(clusterState.metadata().getProject().custom(IndexLifecycleMetadata.TYPE)); + state = state.updateProject( + ProjectMetadata.builder(state.metadata()) + .put( + IndexMetadata.builder(state.metadata().index(index)).putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) + ) + .build() + ); + policyStepsRegistry.update(state.metadata().custom(IndexLifecycleMetadata.TYPE)); + } + + private LifecycleExecutionState getLifecycleExecutionState(ClusterState newState) { + return newState.metadata().getProject(state.projectId()).index(index).getLifecycleExecutionState(); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTaskTests.java index 0f5154b50d43e..bba21d5a3a170 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTaskTests.java @@ -9,9 +9,10 @@ import org.elasticsearch.ElasticsearchException; 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.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexVersion; @@ -39,7 +40,7 @@ public class MoveToErrorStepUpdateTaskTests extends ESTestCase { String policy; - ClusterState clusterState; + ProjectState state; Index index; @Before @@ -56,12 +57,11 @@ public void setupClusterState() { Map.of(policy, new LifecyclePolicyMetadata(lifecyclePolicy, Map.of(), randomNonNegativeLong(), randomNonNegativeLong())), OperationMode.RUNNING ); - Metadata metadata = Metadata.builder() - .persistentSettings(settings(IndexVersion.current()).build()) + ProjectMetadata project = ProjectMetadata.builder(randomProjectIdOrDefault()) .put(IndexMetadata.builder(indexMetadata)) .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) .build(); - clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).build(); + state = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(project).build().projectState(project.id()); } public void testExecuteSuccessfullyMoved() throws Exception { @@ -81,8 +81,11 @@ public void testExecuteSuccessfullyMoved() throws Exception { (idxMeta, stepKey) -> new MockStep(stepKey, nextStepKey), state -> {} ); - ClusterState newState = task.execute(clusterState); - LifecycleExecutionState lifecycleState = newState.getMetadata().getProject().index(index).getLifecycleExecutionState(); + ClusterState newState = task.execute(state); + LifecycleExecutionState lifecycleState = newState.metadata() + .getProject(state.projectId()) + .index(index) + .getLifecycleExecutionState(); StepKey actualKey = Step.getCurrentStepKey(lifecycleState); assertThat(actualKey, equalTo(new StepKey(currentStepKey.phase(), currentStepKey.action(), ErrorStep.NAME))); assertThat(lifecycleState.failedStep(), equalTo(currentStepKey.name())); @@ -109,8 +112,8 @@ public void testExecuteNoopDifferentStep() throws Exception { (idxMeta, stepKey) -> new MockStep(stepKey, new StepKey("next-phase", "action", "step")), state -> {} ); - ClusterState newState = task.doExecute(clusterState); - assertThat(newState, sameInstance(clusterState)); + ClusterState newState = task.doExecute(state); + assertThat(newState, sameInstance(state.cluster())); } public void testExecuteNoopDifferentPolicy() throws Exception { @@ -128,35 +131,32 @@ public void testExecuteNoopDifferentPolicy() throws Exception { (idxMeta, stepKey) -> new MockStep(stepKey, new StepKey("next-phase", "action", "step")), state -> {} ); - ClusterState newState = task.doExecute(clusterState); - assertThat(newState, sameInstance(clusterState)); + ClusterState newState = task.doExecute(state); + assertThat(newState, sameInstance(state.cluster())); } private void setStatePolicy(String policyValue) { - clusterState = ClusterState.builder(clusterState) - .metadata( - Metadata.builder(clusterState.metadata()) - .updateSettings(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyValue).build(), index.getName()) - ) - .build(); + state = state.updateProject( + ProjectMetadata.builder(state.metadata()) + .updateSettings(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyValue).build(), index.getName()) + .build() + ); } private void setStateToKey(StepKey stepKey) { LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder( - clusterState.metadata().getProject().index(index).getLifecycleExecutionState() + state.metadata().index(index).getLifecycleExecutionState() ); lifecycleState.setPhase(stepKey.phase()); lifecycleState.setAction(stepKey.action()); lifecycleState.setStep(stepKey.name()); - clusterState = ClusterState.builder(clusterState) - .metadata( - Metadata.builder(clusterState.getMetadata()) - .put( - IndexMetadata.builder(clusterState.getMetadata().getProject().index(index)) - .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) - ) - ) - .build(); + state = state.updateProject( + ProjectMetadata.builder(state.metadata()) + .put( + IndexMetadata.builder(state.metadata().index(index)).putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) + ) + .build() + ); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTaskTests.java index 0b2cbe30c551c..a830246aaecd6 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTaskTests.java @@ -10,9 +10,10 @@ import org.elasticsearch.client.internal.Client; 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.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexVersion; @@ -44,7 +45,7 @@ public class MoveToNextStepUpdateTaskTests extends ESTestCase { private static final NamedXContentRegistry REGISTRY; String policy; - ClusterState clusterState; + ProjectState state; Index index; LifecyclePolicy lifecyclePolicy; @@ -69,12 +70,11 @@ public void setupClusterState() { Map.of(policy, new LifecyclePolicyMetadata(lifecyclePolicy, Map.of(), randomNonNegativeLong(), randomNonNegativeLong())), OperationMode.RUNNING ); - Metadata metadata = Metadata.builder() - .persistentSettings(settings(IndexVersion.current()).build()) + ProjectMetadata project = ProjectMetadata.builder(randomProjectIdOrDefault()) .put(IndexMetadata.builder(indexMetadata)) .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) .build(); - clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).build(); + state = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(project).build().projectState(project.id()); } public void testExecuteSuccessfullyMoved() throws Exception { @@ -104,14 +104,17 @@ public void testExecuteSuccessfullyMoved() throws Exception { stepRegistry, state -> changed.set(true) ); - ClusterState newState = task.execute(clusterState); - LifecycleExecutionState lifecycleState = newState.getMetadata().getProject().index(index).getLifecycleExecutionState(); + ClusterState newState = task.execute(state); + LifecycleExecutionState lifecycleState = newState.metadata() + .getProject(state.projectId()) + .index(index) + .getLifecycleExecutionState(); StepKey actualKey = Step.getCurrentStepKey(lifecycleState); assertThat(actualKey, equalTo(nextStepKey)); assertThat(lifecycleState.phaseTime(), equalTo(now)); assertThat(lifecycleState.actionTime(), equalTo(now)); assertThat(lifecycleState.stepTime(), equalTo(now)); - task.clusterStateProcessed(clusterState, newState); + task.clusterStateProcessed(newState.projectState(state.projectId())); assertTrue(changed.get()); } @@ -129,8 +132,8 @@ public void testExecuteDifferentCurrentStep() throws Exception { new AlwaysExistingStepRegistry(), null ); - ClusterState newState = task.execute(clusterState); - assertSame(newState, clusterState); + ClusterState newState = task.execute(state); + assertSame(newState, state.cluster()); } public void testExecuteDifferentPolicy() throws Exception { @@ -147,8 +150,8 @@ public void testExecuteDifferentPolicy() throws Exception { new AlwaysExistingStepRegistry(), null ); - ClusterState newState = task.execute(clusterState); - assertSame(newState, clusterState); + ClusterState newState = task.execute(state); + assertSame(newState, state.cluster()); } public void testExecuteSuccessfulMoveWithInvalidNextStep() throws Exception { @@ -178,14 +181,17 @@ public void testExecuteSuccessfulMoveWithInvalidNextStep() throws Exception { stepRegistry, s -> changed.set(true) ); - ClusterState newState = task.execute(clusterState); - LifecycleExecutionState lifecycleState = newState.getMetadata().getProject().index(index).getLifecycleExecutionState(); + ClusterState newState = task.execute(state); + LifecycleExecutionState lifecycleState = newState.metadata() + .getProject(state.projectId()) + .index(index) + .getLifecycleExecutionState(); StepKey actualKey = Step.getCurrentStepKey(lifecycleState); assertThat(actualKey, equalTo(invalidNextStep)); assertThat(lifecycleState.phaseTime(), equalTo(now)); assertThat(lifecycleState.actionTime(), equalTo(now)); assertThat(lifecycleState.stepTime(), equalTo(now)); - task.clusterStateProcessed(clusterState, newState); + task.clusterStateProcessed(newState.projectState(state.projectId())); assertTrue(changed.get()); } @@ -229,18 +235,17 @@ public boolean stepExists(String policy, StepKey stepKey) { } private void setStatePolicy(String policyValue) { - clusterState = ClusterState.builder(clusterState) - .metadata( - Metadata.builder(clusterState.metadata()) - .updateSettings(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyValue).build(), index.getName()) - ) - .build(); + state = state.updateProject( + ProjectMetadata.builder(state.metadata()) + .updateSettings(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyValue).build(), index.getName()) + .build() + ); } private void setStateToKey(StepKey stepKey, long now) { LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder( - clusterState.metadata().getProject().index(index).getLifecycleExecutionState() + state.metadata().index(index).getLifecycleExecutionState() ); lifecycleState.setPhase(stepKey.phase()); lifecycleState.setPhaseTime(now); @@ -260,14 +265,12 @@ private void setStateToKey(StepKey stepKey, long now) { "version" : 1, "modified_date_in_millis" : 1578521007076 }""", policy)); - clusterState = ClusterState.builder(clusterState) - .metadata( - Metadata.builder(clusterState.getMetadata()) - .put( - IndexMetadata.builder(clusterState.getMetadata().getProject().index(index)) - .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) - ) - ) - .build(); + state = state.updateProject( + ProjectMetadata.builder(state.metadata()) + .put( + IndexMetadata.builder(state.metadata().index(index)).putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) + ) + .build() + ); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/SetStepInfoUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/SetStepInfoUpdateTaskTests.java index c78f5034f6722..1ec19a2632b35 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/SetStepInfoUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/SetStepInfoUpdateTaskTests.java @@ -10,9 +10,10 @@ import org.apache.logging.log4j.Level; 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.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; @@ -37,7 +38,7 @@ public class SetStepInfoUpdateTaskTests extends ESTestCase { String policy; - ClusterState clusterState; + ProjectState state; Index index; @Before @@ -49,11 +50,8 @@ public void setupClusterState() { .numberOfReplicas(randomIntBetween(0, 5)) .build(); index = indexMetadata.getIndex(); - Metadata metadata = Metadata.builder() - .persistentSettings(settings(IndexVersion.current()).build()) - .put(IndexMetadata.builder(indexMetadata)) - .build(); - clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).build(); + ProjectMetadata project = ProjectMetadata.builder(randomProjectIdOrDefault()).put(IndexMetadata.builder(indexMetadata)).build(); + state = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(project).build().projectState(project.id()); } public void testExecuteSuccessfullySet() throws Exception { @@ -62,8 +60,11 @@ public void testExecuteSuccessfullySet() throws Exception { setStateToKey(currentStepKey); SetStepInfoUpdateTask task = new SetStepInfoUpdateTask(index, policy, currentStepKey, stepInfo); - ClusterState newState = task.execute(clusterState); - LifecycleExecutionState lifecycleState = newState.getMetadata().getProject().index(index).getLifecycleExecutionState(); + ClusterState newState = task.execute(state); + LifecycleExecutionState lifecycleState = newState.metadata() + .getProject(state.projectId()) + .index(index) + .getLifecycleExecutionState(); StepKey actualKey = Step.getCurrentStepKey(lifecycleState); assertThat(actualKey, equalTo(currentStepKey)); assertThat(lifecycleState.phaseTime(), nullValue()); @@ -93,8 +94,8 @@ public void testExecuteNoopDifferentStep() throws Exception { ToXContentObject stepInfo = getRandomStepInfo(); setStateToKey(notCurrentStepKey); SetStepInfoUpdateTask task = new SetStepInfoUpdateTask(index, policy, currentStepKey, stepInfo); - ClusterState newState = task.execute(clusterState); - assertThat(newState, sameInstance(clusterState)); + ClusterState newState = task.execute(state); + assertThat(newState, sameInstance(state.cluster())); } public void testExecuteNoopDifferentPolicy() throws Exception { @@ -103,8 +104,8 @@ public void testExecuteNoopDifferentPolicy() throws Exception { setStateToKey(currentStepKey); setStatePolicy("not-" + policy); SetStepInfoUpdateTask task = new SetStepInfoUpdateTask(index, policy, currentStepKey, stepInfo); - ClusterState newState = task.execute(clusterState); - assertThat(newState, sameInstance(clusterState)); + ClusterState newState = task.execute(state); + assertThat(newState, sameInstance(state.cluster())); } @TestLogging(reason = "logging test", value = "logger.org.elasticsearch.xpack.ilm.SetStepInfoUpdateTask:WARN") @@ -132,31 +133,27 @@ public void testOnFailure() throws IllegalAccessException { } private void setStatePolicy(String policyValue) { - clusterState = ClusterState.builder(clusterState) - .metadata( - Metadata.builder(clusterState.metadata()) - .updateSettings(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyValue).build(), index.getName()) - ) - .build(); - + state = state.updateProject( + ProjectMetadata.builder(state.metadata()) + .updateSettings(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyValue).build(), index.getName()) + .build() + ); } private void setStateToKey(StepKey stepKey) { LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder( - clusterState.metadata().getProject().index(index).getLifecycleExecutionState() + state.metadata().index(index).getLifecycleExecutionState() ); lifecycleState.setPhase(stepKey.phase()); lifecycleState.setAction(stepKey.action()); lifecycleState.setStep(stepKey.name()); - clusterState = ClusterState.builder(clusterState) - .metadata( - Metadata.builder(clusterState.getMetadata()) - .put( - IndexMetadata.builder(clusterState.getMetadata().getProject().index(index)) - .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) - ) - ) - .build(); + state = state.updateProject( + ProjectMetadata.builder(state.metadata()) + .put( + IndexMetadata.builder(state.metadata().index(index)).putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) + ) + .build() + ); } }