Skip to content

Commit 7d62ea6

Browse files
authored
Make ILM move to step method project-aware (elastic#128812)
This is part of an iterative process to make ILM project-aware.
1 parent 269fbbc commit 7d62ea6

File tree

6 files changed

+102
-110
lines changed

6 files changed

+102
-110
lines changed

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTask.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,18 @@ public ClusterState doExecute(final ClusterState currentState) throws IOExceptio
127127
return state;
128128
} else {
129129
logger.trace("[{}] moving cluster state to next step [{}]", index.getName(), nextStepKey);
130-
state = IndexLifecycleTransition.moveClusterStateToStep(
131-
index,
132-
state,
133-
nextStepKey,
134-
nowSupplier,
135-
policyStepsRegistry,
136-
false
137-
);
130+
state = ClusterState.builder(state)
131+
.putProjectMetadata(
132+
IndexLifecycleTransition.moveIndexToStep(
133+
index,
134+
state.metadata().getProject(),
135+
nextStepKey,
136+
nowSupplier,
137+
policyStepsRegistry,
138+
false
139+
)
140+
)
141+
.build();
138142
}
139143
} else {
140144
// cluster state wait step so evaluate the
@@ -170,14 +174,18 @@ public ClusterState doExecute(final ClusterState currentState) throws IOExceptio
170174
if (nextStepKey == null) {
171175
return state;
172176
} else {
173-
state = IndexLifecycleTransition.moveClusterStateToStep(
174-
index,
175-
state,
176-
nextStepKey,
177-
nowSupplier,
178-
policyStepsRegistry,
179-
false
180-
);
177+
state = ClusterState.builder(state)
178+
.putProjectMetadata(
179+
IndexLifecycleTransition.moveIndexToStep(
180+
index,
181+
state.metadata().getProject(),
182+
nextStepKey,
183+
nowSupplier,
184+
policyStepsRegistry,
185+
false
186+
)
187+
)
188+
.build();
181189
}
182190
} else {
183191
final ToXContentObject stepInfo = result.informationContext();

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleService.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,26 +153,21 @@ public StepKey resolveStepKey(ClusterState state, Index index, String phase, @Nu
153153
}
154154

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

178173
public ClusterState moveClusterStateToPreviouslyFailedStep(ClusterState currentState, String[] indices) {

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleTransition.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,29 +108,29 @@ public static void validateTransition(
108108
* For this reason, it is reasonable to throw {@link IllegalArgumentException} when state is not as expected.
109109
*
110110
* @param index The index whose step is to change
111-
* @param state The current {@link ClusterState}
111+
* @param project The current {@link ProjectMetadata}
112112
* @param newStepKey The new step to move the index into
113113
* @param nowSupplier The current-time supplier for updating when steps changed
114114
* @param stepRegistry The steps registry to check a step-key's existence in the index's current policy
115115
* @param forcePhaseDefinitionRefresh Whether to force the phase JSON to be reread or not
116116
* @return The updated cluster state where the index moved to <code>newStepKey</code>
117117
*/
118-
static ClusterState moveClusterStateToStep(
118+
static ProjectMetadata moveIndexToStep(
119119
Index index,
120-
ClusterState state,
120+
ProjectMetadata project,
121121
Step.StepKey newStepKey,
122122
LongSupplier nowSupplier,
123123
PolicyStepsRegistry stepRegistry,
124124
boolean forcePhaseDefinitionRefresh
125125
) {
126-
IndexMetadata idxMeta = state.getMetadata().getProject().index(index);
126+
IndexMetadata idxMeta = project.index(index);
127127
Step.StepKey currentStepKey = Step.getCurrentStepKey(idxMeta.getLifecycleExecutionState());
128128
validateTransition(idxMeta, currentStepKey, newStepKey, stepRegistry);
129129

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

133-
IndexLifecycleMetadata ilmMeta = state.metadata().getProject().custom(IndexLifecycleMetadata.TYPE);
133+
IndexLifecycleMetadata ilmMeta = project.custom(IndexLifecycleMetadata.TYPE);
134134
LifecyclePolicyMetadata policyMetadata = ilmMeta.getPolicyMetadatas().get(idxMeta.getLifecyclePolicyName());
135135
LifecycleExecutionState lifecycleState = idxMeta.getLifecycleExecutionState();
136136
LifecycleExecutionState newLifecycleState = updateExecutionStateToStep(
@@ -142,7 +142,7 @@ static ClusterState moveClusterStateToStep(
142142
true
143143
);
144144

145-
return LifecycleExecutionStateUtils.newClusterStateWithLifecycleState(state, idxMeta.getIndex(), newLifecycleState);
145+
return project.withLifecycleState(idxMeta.getIndex(), newLifecycleState);
146146
}
147147

148148
/**

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTask.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,18 @@ public MoveToNextStepUpdateTask(
4949

5050
@Override
5151
public ClusterState doExecute(ClusterState currentState) {
52-
IndexMetadata idxMeta = currentState.getMetadata().getProject().index(index);
52+
final var project = currentState.metadata().getProject();
53+
IndexMetadata idxMeta = project.index(index);
5354
if (idxMeta == null) {
5455
// Index must have been since deleted, ignore it
5556
return currentState;
5657
}
5758
LifecycleExecutionState lifecycleState = idxMeta.getLifecycleExecutionState();
5859
if (policy.equals(idxMeta.getLifecyclePolicyName()) && currentStepKey.equals(Step.getCurrentStepKey(lifecycleState))) {
5960
logger.trace("moving [{}] to next step ({})", index.getName(), nextStepKey);
60-
return IndexLifecycleTransition.moveClusterStateToStep(index, currentState, nextStepKey, nowSupplier, stepRegistry, false);
61+
return ClusterState.builder(currentState)
62+
.putProjectMetadata(IndexLifecycleTransition.moveIndexToStep(index, project, nextStepKey, nowSupplier, stepRegistry, false))
63+
.build();
6164
} else {
6265
// either the policy has changed or the step is now
6366
// not the same as when we submitted the update task. In

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportMoveToStepAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,13 @@ public ClusterState execute(ClusterState currentState) {
147147
}
148148

149149
concreteTargetKey.set(concreteTargetStepKey);
150-
return indexLifecycleService.moveClusterStateToStep(
151-
currentState,
150+
final var updatedProject = indexLifecycleService.moveIndexToStep(
151+
currentState.metadata().getProject(),
152152
indexMetadata.getIndex(),
153153
request.getCurrentStepKey(),
154154
concreteTargetKey.get()
155155
);
156+
return ClusterState.builder(currentState).putProjectMetadata(updatedProject).build();
156157
}
157158

158159
@Override

0 commit comments

Comments
 (0)