Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,18 @@ public ClusterState doExecute(final ClusterState currentState) throws IOExceptio
return state;
} else {
logger.trace("[{}] moving cluster state to next step [{}]", index.getName(), nextStepKey);
state = IndexLifecycleTransition.moveClusterStateToStep(
index,
state,
nextStepKey,
nowSupplier,
policyStepsRegistry,
false
);
state = ClusterState.builder(state)
.putProjectMetadata(
IndexLifecycleTransition.moveProjectToStep(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit clunky for now, but it'll get improved in a future change. Same goes for some other placesin this PR.

index,
state.metadata().getProject(),
nextStepKey,
nowSupplier,
policyStepsRegistry,
false
)
)
.build();
}
} else {
// cluster state wait step so evaluate the
Expand Down Expand Up @@ -170,14 +174,18 @@ public ClusterState doExecute(final ClusterState currentState) throws IOExceptio
if (nextStepKey == null) {
return state;
} else {
state = IndexLifecycleTransition.moveClusterStateToStep(
index,
state,
nextStepKey,
nowSupplier,
policyStepsRegistry,
false
);
state = ClusterState.builder(state)
.putProjectMetadata(
IndexLifecycleTransition.moveProjectToStep(
index,
state.metadata().getProject(),
nextStepKey,
nowSupplier,
policyStepsRegistry,
false
)
)
.build();
}
} else {
final ToXContentObject stepInfo = result.informationContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,26 +153,21 @@ public StepKey resolveStepKey(ClusterState state, Index index, String phase, @Nu
}

/**
* Move the cluster state to an arbitrary step for the provided index.
* Move the project to an arbitrary step for the provided index.
*
* In order to avoid a check-then-set race condition, the current step key
* is required in order to validate that the index is currently on the
* provided step. If it is not, an {@link IllegalArgumentException} is
* thrown.
* @throws IllegalArgumentException if the step movement cannot be validated
*/
public ClusterState moveClusterStateToStep(ClusterState currentState, Index index, StepKey currentStepKey, StepKey newStepKey) {
public ProjectMetadata moveProjectToStep(ProjectMetadata project, Index index, StepKey currentStepKey, StepKey newStepKey) {
// We manually validate here, because any API must correctly specify the current step key
// when moving to an arbitrary step key (to avoid race conditions between the
// check-and-set). moveClusterStateToStep also does its own validation, but doesn't take
// check-and-set). moveProjectToStep also does its own validation, but doesn't take
// the user-input for the current step (which is why we validate here for a passed in step)
IndexLifecycleTransition.validateTransition(
currentState.getMetadata().getProject().index(index),
currentStepKey,
newStepKey,
policyRegistry
);
return IndexLifecycleTransition.moveClusterStateToStep(index, currentState, newStepKey, nowSupplier, policyRegistry, true);
IndexLifecycleTransition.validateTransition(project.index(index), currentStepKey, newStepKey, policyRegistry);
return IndexLifecycleTransition.moveProjectToStep(index, project, newStepKey, nowSupplier, policyRegistry, true);
}

public ClusterState moveClusterStateToPreviouslyFailedStep(ClusterState currentState, String[] indices) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,29 +108,29 @@ public static void validateTransition(
* For this reason, it is reasonable to throw {@link IllegalArgumentException} when state is not as expected.
*
* @param index The index whose step is to change
* @param state The current {@link ClusterState}
* @param project The current {@link ProjectMetadata}
* @param newStepKey The new step to move the index into
* @param nowSupplier The current-time supplier for updating when steps changed
* @param stepRegistry The steps registry to check a step-key's existence in the index's current policy
* @param forcePhaseDefinitionRefresh Whether to force the phase JSON to be reread or not
* @return The updated cluster state where the index moved to <code>newStepKey</code>
*/
static ClusterState moveClusterStateToStep(
static ProjectMetadata moveProjectToStep(
Index index,
ClusterState state,
ProjectMetadata project,
Step.StepKey newStepKey,
LongSupplier nowSupplier,
PolicyStepsRegistry stepRegistry,
boolean forcePhaseDefinitionRefresh
) {
IndexMetadata idxMeta = state.getMetadata().getProject().index(index);
IndexMetadata idxMeta = project.index(index);
Step.StepKey currentStepKey = Step.getCurrentStepKey(idxMeta.getLifecycleExecutionState());
validateTransition(idxMeta, currentStepKey, newStepKey, stepRegistry);

String policyName = idxMeta.getLifecyclePolicyName();
logger.info("moving index [{}] from [{}] to [{}] in policy [{}]", index.getName(), currentStepKey, newStepKey, policyName);

IndexLifecycleMetadata ilmMeta = state.metadata().getProject().custom(IndexLifecycleMetadata.TYPE);
IndexLifecycleMetadata ilmMeta = project.custom(IndexLifecycleMetadata.TYPE);
LifecyclePolicyMetadata policyMetadata = ilmMeta.getPolicyMetadatas().get(idxMeta.getLifecyclePolicyName());
LifecycleExecutionState lifecycleState = idxMeta.getLifecycleExecutionState();
LifecycleExecutionState newLifecycleState = updateExecutionStateToStep(
Expand All @@ -142,7 +142,7 @@ static ClusterState moveClusterStateToStep(
true
);

return LifecycleExecutionStateUtils.newClusterStateWithLifecycleState(state, idxMeta.getIndex(), newLifecycleState);
return project.withLifecycleState(idxMeta.getIndex(), newLifecycleState);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,20 @@ public MoveToNextStepUpdateTask(

@Override
public ClusterState doExecute(ClusterState currentState) {
IndexMetadata idxMeta = currentState.getMetadata().getProject().index(index);
final var project = currentState.metadata().getProject();
IndexMetadata idxMeta = project.index(index);
if (idxMeta == null) {
// Index must have been since deleted, ignore it
return currentState;
}
LifecycleExecutionState lifecycleState = idxMeta.getLifecycleExecutionState();
if (policy.equals(idxMeta.getLifecyclePolicyName()) && currentStepKey.equals(Step.getCurrentStepKey(lifecycleState))) {
logger.trace("moving [{}] to next step ({})", index.getName(), nextStepKey);
return IndexLifecycleTransition.moveClusterStateToStep(index, currentState, nextStepKey, nowSupplier, stepRegistry, false);
return ClusterState.builder(currentState)
.putProjectMetadata(
IndexLifecycleTransition.moveProjectToStep(index, project, nextStepKey, nowSupplier, stepRegistry, false)
)
.build();
} else {
// either the policy has changed or the step is now
// not the same as when we submitted the update task. In
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,13 @@ public ClusterState execute(ClusterState currentState) {
}

concreteTargetKey.set(concreteTargetStepKey);
return indexLifecycleService.moveClusterStateToStep(
currentState,
final var updatedProject = indexLifecycleService.moveProjectToStep(
currentState.metadata().getProject(),
indexMetadata.getIndex(),
request.getCurrentStepKey(),
concreteTargetKey.get()
);
return ClusterState.builder(currentState).putProjectMetadata(updatedProject).build();
}

@Override
Expand Down
Loading