Skip to content

Commit 8612084

Browse files
nielsbaumanvaleriy42
authored andcommitted
Make utility methods in IndexLifecycleTransition project-aware (elastic#128930)
Modifies the methods to work with a project scope rather than a cluster scope. This is part of an iterative process to make ILM project-aware.
1 parent 53dfd00 commit 8612084

11 files changed

+163
-215
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ private ClusterState executeWaitStep(ClusterState state, Step currentStep) {
198198
if (stepInfo == null) {
199199
return state;
200200
}
201-
return IndexLifecycleTransition.addStepInfoToClusterState(index, state, stepInfo);
201+
return ClusterState.builder(state)
202+
.putProjectMetadata(IndexLifecycleTransition.addStepInfoToProject(index, state.metadata().getProject(), stepInfo))
203+
.build();
202204
}
203205
}
204206

@@ -286,7 +288,12 @@ private ClusterState moveToErrorStep(final ClusterState state, Step.StepKey curr
286288
),
287289
cause
288290
);
289-
return IndexLifecycleTransition.moveClusterStateToErrorStep(index, state, cause, nowSupplier, policyStepsRegistry::getStep);
291+
final var project = state.metadata().getProject();
292+
return ClusterState.builder(state)
293+
.putProjectMetadata(
294+
IndexLifecycleTransition.moveIndexToErrorStep(index, project, cause, nowSupplier, policyStepsRegistry::getStep)
295+
)
296+
.build();
290297
}
291298

292299
@Override

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,13 +657,14 @@ private final class MoveToRetryFailedStepUpdateTask extends IndexLifecycleCluste
657657

658658
@Override
659659
protected ClusterState doExecute(ClusterState currentState) {
660-
return IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep(
661-
currentState,
660+
final var updatedProject = IndexLifecycleTransition.moveIndexToPreviouslyFailedStep(
661+
currentState.metadata().getProject(),
662662
index.getName(),
663663
nowSupplier,
664664
stepRegistry,
665665
true
666666
);
667+
return ClusterState.builder(currentState).putProjectMetadata(updatedProject).build();
667668
}
668669

669670
@Override

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,12 @@ public ProjectMetadata moveIndexToStep(ProjectMetadata project, Index index, Ste
170170
return IndexLifecycleTransition.moveIndexToStep(index, project, newStepKey, nowSupplier, policyRegistry, true);
171171
}
172172

173-
public ClusterState moveClusterStateToPreviouslyFailedStep(ClusterState currentState, String[] indices) {
174-
ClusterState newState = currentState;
173+
public ProjectMetadata moveIndicesToPreviouslyFailedStep(ProjectMetadata currentProject, String[] indices) {
174+
ProjectMetadata newProject = currentProject;
175175
for (String index : indices) {
176-
newState = IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep(newState, index, nowSupplier, policyRegistry, false);
176+
newProject = IndexLifecycleTransition.moveIndexToPreviouslyFailedStep(newProject, index, nowSupplier, policyRegistry, false);
177177
}
178-
return newState;
178+
return newProject;
179179
}
180180

181181
// package private for testing

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

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,18 @@
1212
import org.elasticsearch.ElasticsearchException;
1313
import org.elasticsearch.action.support.TransportAction;
1414
import org.elasticsearch.client.internal.Client;
15-
import org.elasticsearch.cluster.ClusterState;
1615
import org.elasticsearch.cluster.metadata.IndexMetadata;
1716
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
1817
import org.elasticsearch.cluster.metadata.ProjectMetadata;
1918
import org.elasticsearch.common.Strings;
2019
import org.elasticsearch.common.settings.Settings;
21-
import org.elasticsearch.core.Nullable;
2220
import org.elasticsearch.index.Index;
2321
import org.elasticsearch.license.XPackLicenseState;
2422
import org.elasticsearch.xcontent.ToXContentObject;
2523
import org.elasticsearch.xpack.core.ilm.ErrorStep;
2624
import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata;
2725
import org.elasticsearch.xpack.core.ilm.InitializePolicyContextStep;
2826
import org.elasticsearch.xpack.core.ilm.InitializePolicyException;
29-
import org.elasticsearch.xpack.core.ilm.LifecycleExecutionStateUtils;
3027
import org.elasticsearch.xpack.core.ilm.LifecyclePolicy;
3128
import org.elasticsearch.xpack.core.ilm.LifecyclePolicyMetadata;
3229
import org.elasticsearch.xpack.core.ilm.LifecycleSettings;
@@ -113,7 +110,7 @@ public static void validateTransition(
113110
* @param nowSupplier The current-time supplier for updating when steps changed
114111
* @param stepRegistry The steps registry to check a step-key's existence in the index's current policy
115112
* @param forcePhaseDefinitionRefresh Whether to force the phase JSON to be reread or not
116-
* @return The updated cluster state where the index moved to <code>newStepKey</code>
113+
* @return The updated project metadata where the index moved to <code>newStepKey</code>
117114
*/
118115
static ProjectMetadata moveIndexToStep(
119116
Index index,
@@ -149,15 +146,15 @@ static ProjectMetadata moveIndexToStep(
149146
* Moves the given index into the ERROR step. The ERROR step will have the same phase and
150147
* action, but use the {@link ErrorStep#NAME} as the name in the lifecycle execution state.
151148
*/
152-
static ClusterState moveClusterStateToErrorStep(
149+
static ProjectMetadata moveIndexToErrorStep(
153150
Index index,
154-
ClusterState clusterState,
151+
ProjectMetadata project,
155152
Exception cause,
156153
LongSupplier nowSupplier,
157154
BiFunction<IndexMetadata, Step.StepKey, Step> stepLookupFunction
158155
) {
159-
IndexMetadata idxMeta = clusterState.getMetadata().getProject().index(index);
160-
IndexLifecycleMetadata ilmMeta = clusterState.metadata().getProject().custom(IndexLifecycleMetadata.TYPE);
156+
IndexMetadata idxMeta = project.index(index);
157+
IndexLifecycleMetadata ilmMeta = project.custom(IndexLifecycleMetadata.TYPE);
161158
LifecyclePolicyMetadata policyMetadata = ilmMeta.getPolicyMetadatas().get(idxMeta.getLifecyclePolicyName());
162159
LifecycleExecutionState currentState = idxMeta.getLifecycleExecutionState();
163160
Step.StepKey currentStep;
@@ -204,22 +201,21 @@ static ClusterState moveClusterStateToErrorStep(
204201
);
205202
}
206203

207-
return LifecycleExecutionStateUtils.newClusterStateWithLifecycleState(clusterState, idxMeta.getIndex(), failedState.build());
204+
return project.withLifecycleState(idxMeta.getIndex(), failedState.build());
208205
}
209206

210207
/**
211208
* Move the given index's execution state back to a step that had previously failed. If this is
212209
* an automatic retry ({@code isAutomaticRetry}), the retry count is incremented.
213210
*/
214-
static ClusterState moveClusterStateToPreviouslyFailedStep(
215-
ClusterState currentState,
211+
static ProjectMetadata moveIndexToPreviouslyFailedStep(
212+
ProjectMetadata project,
216213
String index,
217214
LongSupplier nowSupplier,
218215
PolicyStepsRegistry stepRegistry,
219216
boolean isAutomaticRetry
220217
) {
221-
ClusterState newState;
222-
IndexMetadata indexMetadata = currentState.metadata().getProject().index(index);
218+
IndexMetadata indexMetadata = project.index(index);
223219
if (indexMetadata == null) {
224220
throw new IllegalArgumentException("index [" + index + "] does not exist");
225221
}
@@ -229,7 +225,7 @@ static ClusterState moveClusterStateToPreviouslyFailedStep(
229225
if (currentStepKey != null && ErrorStep.NAME.equals(currentStepKey.name()) && Strings.isNullOrEmpty(failedStep) == false) {
230226
Step.StepKey nextStepKey = new Step.StepKey(currentStepKey.phase(), currentStepKey.action(), failedStep);
231227
validateTransition(indexMetadata, currentStepKey, nextStepKey, stepRegistry);
232-
IndexLifecycleMetadata ilmMeta = currentState.metadata().getProject().custom(IndexLifecycleMetadata.TYPE);
228+
IndexLifecycleMetadata ilmMeta = project.custom(IndexLifecycleMetadata.TYPE);
233229

234230
LifecyclePolicyMetadata policyMetadata = ilmMeta.getPolicyMetadatas().get(indexMetadata.getLifecyclePolicyName());
235231

@@ -259,17 +255,12 @@ static ClusterState moveClusterStateToPreviouslyFailedStep(
259255
// manual retries don't update the retry count
260256
retryStepState.setFailedStepRetryCount(lifecycleState.failedStepRetryCount());
261257
}
262-
newState = LifecycleExecutionStateUtils.newClusterStateWithLifecycleState(
263-
currentState,
264-
indexMetadata.getIndex(),
265-
retryStepState.build()
266-
);
258+
return project.withLifecycleState(indexMetadata.getIndex(), retryStepState.build());
267259
} else {
268260
throw new IllegalArgumentException(
269261
"cannot retry an action for an index [" + index + "] that has not encountered an error when running a Lifecycle Policy"
270262
);
271263
}
272-
return newState;
273264
}
274265

275266
/**
@@ -413,53 +404,31 @@ public static LifecycleExecutionState moveStateToNextActionAndUpdateCachedPhase(
413404
}
414405

415406
/**
416-
* Conditionally updates cluster state with new step info. The new cluster state is only
417-
* built if the step info has changed, otherwise the same old <code>clusterState</code> is
407+
* Conditionally updates project metadata with new step info. The new project metadata is only
408+
* built if the step info has changed, otherwise the same old <code>project</code> is
418409
* returned
419-
*
420-
* @param index the index to modify
421-
* @param clusterState the cluster state to modify
422-
* @param stepInfo the new step info to update
423-
* @return Updated cluster state with <code>stepInfo</code> if changed, otherwise the same cluster state
424-
* if no changes to step info exist
425410
*/
426-
static ClusterState addStepInfoToClusterState(Index index, ClusterState clusterState, ToXContentObject stepInfo) {
427-
IndexMetadata indexMetadata = clusterState.getMetadata().getProject().index(index);
411+
static ProjectMetadata addStepInfoToProject(Index index, ProjectMetadata project, ToXContentObject stepInfo) {
412+
IndexMetadata indexMetadata = project.index(index);
428413
if (indexMetadata == null) {
429414
// This index doesn't exist anymore, we can't do anything
430-
return clusterState;
415+
return project;
431416
}
432417
LifecycleExecutionState lifecycleState = indexMetadata.getLifecycleExecutionState();
433418
final String stepInfoString = Strings.toString(stepInfo);
434419
if (stepInfoString.equals(lifecycleState.stepInfo())) {
435-
return clusterState;
420+
return project;
436421
}
437422
LifecycleExecutionState.Builder newState = LifecycleExecutionState.builder(lifecycleState);
438423
newState.setStepInfo(stepInfoString);
439-
return LifecycleExecutionStateUtils.newClusterStateWithLifecycleState(clusterState, indexMetadata.getIndex(), newState.build());
424+
return project.withLifecycleState(index, newState.build());
440425
}
441426

442427
/**
443428
* Remove the ILM policy from the given indices, this removes the lifecycle setting as well as
444429
* any lifecycle execution state that may be present in the index metadata
445430
*/
446-
public static ClusterState removePolicyForIndexes(final Index[] indices, ClusterState currentState, List<String> failedIndexes) {
447-
final ProjectMetadata currentProject = currentState.metadata().getProject();
448-
final ProjectMetadata.Builder updatedProject = removePolicyForIndexes(indices, currentProject, failedIndexes);
449-
450-
if (updatedProject == null) {
451-
return currentState;
452-
} else {
453-
return ClusterState.builder(currentState).putProjectMetadata(updatedProject).build();
454-
}
455-
}
456-
457-
/**
458-
* @return If one or more policies were removed, then a new builder representing the changed project state.
459-
* Otherwise {@code null} (if no changes were made)
460-
*/
461-
@Nullable
462-
private static ProjectMetadata.Builder removePolicyForIndexes(
431+
public static ProjectMetadata removePolicyForIndexes(
463432
final Index[] indices,
464433
ProjectMetadata currentProject,
465434
List<String> failedIndexes
@@ -481,7 +450,7 @@ private static ProjectMetadata.Builder removePolicyForIndexes(
481450
}
482451
}
483452

484-
return newProject;
453+
return newProject == null ? currentProject : newProject.build();
485454
}
486455

487456
/**

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,17 @@ public MoveToErrorStepUpdateTask(
5757

5858
@Override
5959
protected ClusterState doExecute(ClusterState currentState) throws Exception {
60-
IndexMetadata idxMeta = currentState.getMetadata().getProject().index(index);
60+
final var project = currentState.getMetadata().getProject();
61+
IndexMetadata idxMeta = project.index(index);
6162
if (idxMeta == null) {
6263
// Index must have been since deleted, ignore it
6364
return currentState;
6465
}
6566
LifecycleExecutionState lifecycleState = idxMeta.getLifecycleExecutionState();
6667
if (policy.equals(idxMeta.getLifecyclePolicyName()) && currentStepKey.equals(Step.getCurrentStepKey(lifecycleState))) {
67-
return IndexLifecycleTransition.moveClusterStateToErrorStep(index, currentState, cause, nowSupplier, stepLookupFunction);
68+
return ClusterState.builder(currentState)
69+
.putProjectMetadata(IndexLifecycleTransition.moveIndexToErrorStep(index, project, cause, nowSupplier, stepLookupFunction))
70+
.build();
6871
} else {
6972
// either the policy has changed or the step is now
7073
// not the same as when we submitted the update task. In

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,17 @@ ToXContentObject getStepInfo() {
4646

4747
@Override
4848
protected ClusterState doExecute(ClusterState currentState) throws IOException {
49-
IndexMetadata idxMeta = currentState.getMetadata().getProject().index(index);
49+
final var project = currentState.metadata().getProject();
50+
IndexMetadata idxMeta = project.index(index);
5051
if (idxMeta == null) {
5152
// Index must have been since deleted, ignore it
5253
return currentState;
5354
}
5455
LifecycleExecutionState lifecycleState = idxMeta.getLifecycleExecutionState();
5556
if (policy.equals(idxMeta.getLifecyclePolicyName()) && Objects.equals(currentStepKey, Step.getCurrentStepKey(lifecycleState))) {
56-
return IndexLifecycleTransition.addStepInfoToClusterState(index, currentState, stepInfo);
57+
return ClusterState.builder(currentState)
58+
.putProjectMetadata(IndexLifecycleTransition.addStepInfoToProject(index, project, stepInfo))
59+
.build();
5760
} else {
5861
// either the policy has changed or the step is now
5962
// not the same as when we submitted the update task. In

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ protected void masterOperation(Task task, Request request, ClusterState state, A
7070

7171
@Override
7272
public ClusterState execute(ClusterState currentState) throws Exception {
73-
return IndexLifecycleTransition.removePolicyForIndexes(indices, currentState, failedIndexes);
73+
final var project = currentState.metadata().getProject();
74+
final var updatedProject = IndexLifecycleTransition.removePolicyForIndexes(indices, project, failedIndexes);
75+
return ClusterState.builder(currentState).putProjectMetadata(updatedProject).build();
7476
}
7577

7678
@Override

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ protected void masterOperation(
7474
submitUnbatchedTask("ilm-re-run", new AckedClusterStateUpdateTask(request, listener) {
7575
@Override
7676
public ClusterState execute(ClusterState currentState) {
77-
return indexLifecycleService.moveClusterStateToPreviouslyFailedStep(currentState, request.indices());
77+
final var project = state.metadata().getProject();
78+
final var updatedProject = indexLifecycleService.moveIndicesToPreviouslyFailedStep(project, request.indices());
79+
return ClusterState.builder(currentState).putProjectMetadata(updatedProject).build();
7880
}
7981

8082
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
* successfully (see {@link org.elasticsearch.xpack.ilm.IndexLifecycleRunner#onErrorMaybeRetryFailedStep}). In order to see all retryable
9191
* steps see {@link org.elasticsearch.xpack.core.ilm.Step#isRetryable()}.
9292
* For steps that are not retryable the failed step can manually be retried using
93-
* {@link org.elasticsearch.xpack.ilm.IndexLifecycleService#moveClusterStateToPreviouslyFailedStep}.
93+
* {@link org.elasticsearch.xpack.ilm.IndexLifecycleService#moveIndicesToPreviouslyFailedStep}.
9494
*
9595
*/
9696
package org.elasticsearch.xpack.ilm;

x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -917,40 +917,6 @@ private static LifecyclePolicy createPolicy(String policyName, StepKey safeStep,
917917
return newTestLifecyclePolicy(policyName, phases);
918918
}
919919

920-
public static void assertClusterStateOnNextStep(
921-
ClusterState oldClusterState,
922-
Index index,
923-
StepKey currentStep,
924-
StepKey nextStep,
925-
ClusterState newClusterState,
926-
long now
927-
) {
928-
assertNotSame(oldClusterState, newClusterState);
929-
Metadata newMetadata = newClusterState.metadata();
930-
assertNotSame(oldClusterState.metadata(), newMetadata);
931-
IndexMetadata newIndexMetadata = newMetadata.getProject().getIndexSafe(index);
932-
assertNotSame(oldClusterState.metadata().getProject().index(index), newIndexMetadata);
933-
LifecycleExecutionState newLifecycleState = newClusterState.metadata().getProject().index(index).getLifecycleExecutionState();
934-
LifecycleExecutionState oldLifecycleState = oldClusterState.metadata().getProject().index(index).getLifecycleExecutionState();
935-
assertNotSame(oldLifecycleState, newLifecycleState);
936-
assertEquals(nextStep.phase(), newLifecycleState.phase());
937-
assertEquals(nextStep.action(), newLifecycleState.action());
938-
assertEquals(nextStep.name(), newLifecycleState.step());
939-
if (currentStep.phase().equals(nextStep.phase())) {
940-
assertEquals(oldLifecycleState.phaseTime(), newLifecycleState.phaseTime());
941-
} else {
942-
assertEquals(now, newLifecycleState.phaseTime().longValue());
943-
}
944-
if (currentStep.action().equals(nextStep.action())) {
945-
assertEquals(oldLifecycleState.actionTime(), newLifecycleState.actionTime());
946-
} else {
947-
assertEquals(now, newLifecycleState.actionTime().longValue());
948-
}
949-
assertEquals(now, newLifecycleState.stepTime().longValue());
950-
assertEquals(null, newLifecycleState.failedStep());
951-
assertEquals(null, newLifecycleState.stepInfo());
952-
}
953-
954920
private static IndexMetadata createIndex(String name) {
955921
return IndexMetadata.builder(name).settings(randomIndexSettings()).build();
956922
}

0 commit comments

Comments
 (0)