Skip to content

Commit 77f3667

Browse files
committed
Make utility methods in IndexLifecycleTransition project-aware
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 234e7ed commit 77f3667

File tree

10 files changed

+169
-211
lines changed

10 files changed

+169
-211
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,11 @@ public ClusterState doExecute(final ClusterState currentState) throws IOExceptio
206206
if (stepInfo == null) {
207207
return state;
208208
} else {
209-
return IndexLifecycleTransition.addStepInfoToClusterState(index, state, stepInfo);
209+
return ClusterState.builder(state)
210+
.putProjectMetadata(
211+
IndexLifecycleTransition.addStepInfoToClusterState(index, state.metadata().getProject(), stepInfo)
212+
)
213+
.build();
210214
}
211215
}
212216
}
@@ -293,7 +297,12 @@ private ClusterState moveToErrorStep(final ClusterState state, Step.StepKey curr
293297
),
294298
cause
295299
);
296-
return IndexLifecycleTransition.moveClusterStateToErrorStep(index, state, cause, nowSupplier, policyStepsRegistry::getStep);
300+
final var project = state.metadata().getProject();
301+
return ClusterState.builder(state)
302+
.putProjectMetadata(
303+
IndexLifecycleTransition.moveIndexToErrorStep(index, project, cause, nowSupplier, policyStepsRegistry::getStep)
304+
)
305+
.build();
297306
}
298307

299308
@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.moveClusterStateToPreviouslyFailedStep(
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: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,18 @@ 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 moveClusterStateToPreviouslyFailedStep(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.moveClusterStateToPreviouslyFailedStep(
177+
newProject,
178+
index,
179+
nowSupplier,
180+
policyRegistry,
181+
false
182+
);
177183
}
178-
return newState;
184+
return newProject;
179185
}
180186

181187
// package private for testing

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

Lines changed: 19 additions & 50 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;
@@ -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 moveClusterStateToPreviouslyFailedStep(
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 addStepInfoToClusterState(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.addStepInfoToClusterState(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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ 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 updatedProject = indexLifecycleService.moveClusterStateToPreviouslyFailedStep(
78+
state.metadata().getProject(),
79+
request.indices()
80+
);
81+
return ClusterState.builder(currentState).putProjectMetadata(updatedProject).build();
7882
}
7983

8084
@Override

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
@@ -916,40 +916,6 @@ private static LifecyclePolicy createPolicy(String policyName, StepKey safeStep,
916916
return newTestLifecyclePolicy(policyName, phases);
917917
}
918918

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

0 commit comments

Comments
 (0)