Skip to content

Commit 5300b2f

Browse files
committed
Fix transition for mounted index from searchable_snapshot to another action
Not that searchable_snapshot can precede other actions in the same phase (eg. in frozen it is followed by `migrate`) we need to allow the mounted index to resume executing the ILM policy starting with a step that's part of a new action (ie. migrate). This adds support to resume the execution of the mounted index from another action. With older versions the execution would resume from the PhaseCompleteStep as it was the last action in a phase, which was handled as a special case in the `CopyExecutionStateStep`. This commit generalises the `CopyExecutionStateStep` to be able to resume from any `StepKey`.
1 parent 89a4724 commit 5300b2f

File tree

4 files changed

+26
-30
lines changed

4 files changed

+26
-30
lines changed

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

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
/**
2222
* Copies the execution state data from one index to another, typically after a
2323
* new index has been created. As part of the execution state copy it will set the target index
24-
* "current step" to the provided step name (part of the same phase and action as the current step's, unless
25-
* the "complete" step is configured in which case the action will be changed to "complete" as well)
24+
* "current step" to the provided target next step {@link org.elasticsearch.xpack.core.ilm.Step.StepKey}.
2625
*
2726
* Useful for actions such as shrink.
2827
*/
@@ -32,20 +31,20 @@ public class CopyExecutionStateStep extends ClusterStateActionStep {
3231
private static final Logger logger = LogManager.getLogger(CopyExecutionStateStep.class);
3332

3433
private final String targetIndexPrefix;
35-
private final String targetNextStepName;
34+
private final StepKey targetNextStepKey;
3635

37-
public CopyExecutionStateStep(StepKey key, StepKey nextStepKey, String targetIndexPrefix, String targetNextStepName) {
36+
public CopyExecutionStateStep(StepKey key, StepKey nextStepKey, String targetIndexPrefix, StepKey targetNextStepKey) {
3837
super(key, nextStepKey);
3938
this.targetIndexPrefix = targetIndexPrefix;
40-
this.targetNextStepName = targetNextStepName;
39+
this.targetNextStepKey = targetNextStepKey;
4140
}
4241

4342
String getTargetIndexPrefix() {
4443
return targetIndexPrefix;
4544
}
4645

47-
String getTargetNextStepName() {
48-
return targetNextStepName;
46+
StepKey getTargetNextStepKey() {
47+
return targetNextStepKey;
4948
}
5049

5150
@Override
@@ -69,20 +68,17 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
6968
"] to [" + targetIndexName + "] as target index does not exist");
7069
}
7170

71+
String phase = targetNextStepKey.getPhase();
72+
String action = targetNextStepKey.getAction();
73+
String step = targetNextStepKey.getName();
7274
LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetadata);
73-
String phase = lifecycleState.getPhase();
74-
String action = lifecycleState.getAction();
7575
long lifecycleDate = lifecycleState.getLifecycleDate();
7676

7777
LifecycleExecutionState.Builder relevantTargetCustomData = LifecycleExecutionState.builder();
7878
relevantTargetCustomData.setIndexCreationDate(lifecycleDate);
79+
relevantTargetCustomData.setAction(action);
7980
relevantTargetCustomData.setPhase(phase);
80-
relevantTargetCustomData.setStep(targetNextStepName);
81-
if (targetNextStepName.equals(PhaseCompleteStep.NAME)) {
82-
relevantTargetCustomData.setAction(PhaseCompleteStep.NAME);
83-
} else {
84-
relevantTargetCustomData.setAction(action);
85-
}
81+
relevantTargetCustomData.setStep(step);
8682
relevantTargetCustomData.setSnapshotRepository(lifecycleState.getSnapshotRepository());
8783
relevantTargetCustomData.setSnapshotName(lifecycleState.getSnapshotName());
8884
relevantTargetCustomData.setSnapshotIndexName(lifecycleState.getSnapshotIndexName());
@@ -107,11 +103,11 @@ public boolean equals(Object o) {
107103
}
108104
CopyExecutionStateStep that = (CopyExecutionStateStep) o;
109105
return Objects.equals(targetIndexPrefix, that.targetIndexPrefix) &&
110-
Objects.equals(targetNextStepName, that.targetNextStepName);
106+
Objects.equals(targetNextStepKey, that.targetNextStepKey);
111107
}
112108

113109
@Override
114110
public int hashCode() {
115-
return Objects.hash(super.hashCode(), targetIndexPrefix, targetNextStepName);
111+
return Objects.hash(super.hashCode(), targetIndexPrefix, targetNextStepKey);
116112
}
117113
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,8 @@ public List<Step> toSteps(Client client, String phase, StepKey nextStepKey) {
245245
client, getRestoredIndexPrefix(mountSnapshotKey), getConcreteStorageType(mountSnapshotKey));
246246
WaitForIndexColorStep waitForGreenIndexHealthStep = new WaitForIndexColorStep(waitForGreenRestoredIndexKey,
247247
copyMetadataKey, ClusterHealthStatus.GREEN, getRestoredIndexPrefix(waitForGreenRestoredIndexKey));
248-
// a policy with only the cold phase will have a null "nextStepKey", hence the "null" nextStepKey passed in below when that's the
249-
// case
250248
CopyExecutionStateStep copyMetadataStep = new CopyExecutionStateStep(copyMetadataKey, copyLifecyclePolicySettingKey,
251-
getRestoredIndexPrefix(copyMetadataKey), nextStepKey != null ? nextStepKey.getName() : "null");
249+
getRestoredIndexPrefix(copyMetadataKey), nextStepKey);
252250
CopySettingsStep copySettingsStep = new CopySettingsStep(copyLifecyclePolicySettingKey, dataStreamCheckBranchingKey,
253251
getRestoredIndexPrefix(copyLifecyclePolicySettingKey), LifecycleSettings.LIFECYCLE_NAME);
254252
BranchingStep isDataStreamBranchingStep = new BranchingStep(dataStreamCheckBranchingKey, swapAliasesKey, replaceDataStreamIndexKey,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey)
184184
SHRUNKEN_INDEX_PREFIX);
185185
ShrunkShardsAllocatedStep allocated = new ShrunkShardsAllocatedStep(enoughShardsKey, copyMetadataKey, SHRUNKEN_INDEX_PREFIX);
186186
CopyExecutionStateStep copyMetadata = new CopyExecutionStateStep(copyMetadataKey, dataStreamCheckBranchingKey,
187-
SHRUNKEN_INDEX_PREFIX, ShrunkenIndexCheckStep.NAME);
187+
SHRUNKEN_INDEX_PREFIX, isShrunkIndexKey);
188188
// by the time we get to this step we have 2 indices, the source and the shrunken one. we now need to choose an index
189189
// swapping strategy such that the shrunken index takes the place of the source index (which is also deleted).
190190
// if the source index is part of a data stream it's a matter of replacing it with the shrunken index one in the data stream and

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ protected CopyExecutionStateStep createRandomInstance() {
2626
StepKey stepKey = randomStepKey();
2727
StepKey nextStepKey = randomStepKey();
2828
String shrunkIndexPrefix = randomAlphaOfLength(10);
29-
String nextStepName = randomStepKey().getName();
30-
return new CopyExecutionStateStep(stepKey, nextStepKey, shrunkIndexPrefix, nextStepName);
29+
StepKey targetNextStepKey = randomStepKey();
30+
return new CopyExecutionStateStep(stepKey, nextStepKey, shrunkIndexPrefix, targetNextStepKey);
3131
}
3232

3333
@Override
3434
protected CopyExecutionStateStep mutateInstance(CopyExecutionStateStep instance) {
3535
StepKey key = instance.getKey();
3636
StepKey nextKey = instance.getNextStepKey();
3737
String shrunkIndexPrefix = instance.getTargetIndexPrefix();
38-
String nextStepName = instance.getTargetNextStepName();
38+
StepKey targetNextStepKey = instance.getTargetNextStepKey();
3939

4040
switch (between(0, 2)) {
4141
case 0:
@@ -48,19 +48,20 @@ protected CopyExecutionStateStep mutateInstance(CopyExecutionStateStep instance)
4848
shrunkIndexPrefix += randomAlphaOfLength(5);
4949
break;
5050
case 3:
51-
nextStepName = randomAlphaOfLengthBetween(1, 10);
51+
targetNextStepKey = new StepKey(targetNextStepKey.getPhase(), targetNextStepKey.getAction(),
52+
targetNextStepKey.getName() + randomAlphaOfLength(5));
5253
break;
5354
default:
5455
throw new AssertionError("Illegal randomisation branch");
5556
}
5657

57-
return new CopyExecutionStateStep(key, nextKey, shrunkIndexPrefix, nextStepName);
58+
return new CopyExecutionStateStep(key, nextKey, shrunkIndexPrefix, targetNextStepKey);
5859
}
5960

6061
@Override
6162
protected CopyExecutionStateStep copyInstance(CopyExecutionStateStep instance) {
6263
return new CopyExecutionStateStep(instance.getKey(), instance.getNextStepKey(), instance.getTargetIndexPrefix(),
63-
instance.getTargetNextStepName());
64+
instance.getTargetNextStepKey());
6465
}
6566

6667
public void testPerformAction() {
@@ -89,10 +90,11 @@ public void testPerformAction() {
8990
LifecycleExecutionState newIndexData = LifecycleExecutionState
9091
.fromIndexMetadata(newClusterState.metadata().index(step.getTargetIndexPrefix() + indexName));
9192

93+
StepKey targetNextStepKey = step.getTargetNextStepKey();
9294
assertEquals(newIndexData.getLifecycleDate(), oldIndexData.getLifecycleDate());
93-
assertEquals(newIndexData.getPhase(), oldIndexData.getPhase());
94-
assertEquals(newIndexData.getAction(), oldIndexData.getAction());
95-
assertEquals(newIndexData.getStep(), step.getTargetNextStepName());
95+
assertEquals(newIndexData.getPhase(), targetNextStepKey.getPhase());
96+
assertEquals(newIndexData.getAction(), targetNextStepKey.getAction());
97+
assertEquals(newIndexData.getStep(), targetNextStepKey.getName());
9698
assertEquals(newIndexData.getSnapshotRepository(), oldIndexData.getSnapshotRepository());
9799
assertEquals(newIndexData.getSnapshotName(), oldIndexData.getSnapshotName());
98100
}

0 commit comments

Comments
 (0)