Skip to content

Commit 936247b

Browse files
colings86jasontedor
authored andcommitted
Skips to next available action on missing step (#32283)
if policy update on index means current step no longer exists This change only updates the setPolicy for index to add this functionality. The update policy API will be changed in a follow up PR.
1 parent 4ed5095 commit 936247b

File tree

20 files changed

+924
-20
lines changed

20 files changed

+924
-20
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/AllocateAction.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ public List<Step> toSteps(Client client, String phase, StepKey nextStepKey) {
131131
return Arrays.asList(allocateStep, routedCheckStep);
132132
}
133133

134+
@Override
135+
public List<StepKey> toStepKeys(String phase) {
136+
StepKey allocateKey = new StepKey(phase, NAME, NAME);
137+
StepKey allocationRoutedKey = new StepKey(phase, NAME, AllocationRoutedStep.NAME);
138+
return Arrays.asList(allocateKey, allocationRoutedKey);
139+
}
140+
134141
@Override
135142
public int hashCode() {
136143
return Objects.hash(include, exclude, require);

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/DeleteAction.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.common.xcontent.ObjectParser;
1313
import org.elasticsearch.common.xcontent.XContentBuilder;
1414
import org.elasticsearch.common.xcontent.XContentParser;
15+
import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey;
1516

1617
import java.io.IOException;
1718
import java.util.Collections;
@@ -62,6 +63,11 @@ public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey)
6263
return Collections.singletonList(new DeleteStep(deleteStepKey, nextStepKey, client));
6364
}
6465

66+
@Override
67+
public List<StepKey> toStepKeys(String phase) {
68+
return Collections.singletonList(new Step.StepKey(phase, NAME, DeleteStep.NAME));
69+
}
70+
6571
@Override
6672
public int hashCode() {
6773
return 1;

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ForceMergeAction.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,20 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
8484

8585
@Override
8686
public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey) {
87-
StepKey updateCompressionKey = new StepKey(phase, NAME, "best_compression");
8887
StepKey forceMergeKey = new StepKey(phase, NAME, ForceMergeStep.NAME);
8988
StepKey countKey = new StepKey(phase, NAME, SegmentCountStep.NAME);
9089
ForceMergeStep forceMergeStep = new ForceMergeStep(forceMergeKey, countKey, client, maxNumSegments);
9190
SegmentCountStep segmentCountStep = new SegmentCountStep(countKey, nextStepKey, client, maxNumSegments);
9291
return Arrays.asList(forceMergeStep, segmentCountStep);
9392
}
9493

94+
@Override
95+
public List<StepKey> toStepKeys(String phase) {
96+
StepKey forceMergeKey = new StepKey(phase, NAME, ForceMergeStep.NAME);
97+
StepKey countKey = new StepKey(phase, NAME, SegmentCountStep.NAME);
98+
return Arrays.asList(forceMergeKey, countKey);
99+
}
100+
95101
@Override
96102
public int hashCode() {
97103
return Objects.hash(maxNumSegments);

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleAction.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.elasticsearch.common.Nullable;
1010
import org.elasticsearch.common.io.stream.NamedWriteable;
1111
import org.elasticsearch.common.xcontent.ToXContentObject;
12+
import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey;
1213

1314
import java.util.List;
1415

@@ -29,6 +30,15 @@ public interface LifecycleAction extends ToXContentObject, NamedWriteable {
2930
*/
3031
List<Step> toSteps(Client client, String phase, @Nullable Step.StepKey nextStepKey);
3132

33+
/**
34+
*
35+
* @param phase
36+
* the name of the phase this action is being executed within
37+
* @return the {@link StepKey}s for the steps which will be executed in this
38+
* action
39+
*/
40+
List<StepKey> toStepKeys(String phase);
41+
3242
/**
3343
* @return true if this action is considered safe. An action is not safe if
3444
* it will produce unwanted side effects or will get stuck when the

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicy.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,82 @@ public boolean isActionSafe(StepKey stepKey) {
245245
}
246246
}
247247

248+
/**
249+
* Finds the next valid {@link StepKey} on or after the provided
250+
* {@link StepKey}. If the provided {@link StepKey} is valid in this policy
251+
* it will be returned. If its not valid the next available {@link StepKey}
252+
* will be returned.
253+
*/
254+
public StepKey getNextValidStep(StepKey stepKey) {
255+
Phase phase = phases.get(stepKey.getPhase());
256+
if (phase == null) {
257+
// Phase doesn't exist so find the after step for the previous
258+
// available phase
259+
return getAfterStepBeforePhase(stepKey.getPhase());
260+
} else {
261+
// Phase exists so check if the action exists
262+
LifecycleAction action = phase.getActions().get(stepKey.getAction());
263+
if (action == null) {
264+
// if action doesn't exist find the first step in the next
265+
// available action
266+
return getFirstStepInNextAction(stepKey.getAction(), phase);
267+
} else {
268+
// if the action exists check if the step itself exists
269+
if (action.toStepKeys(phase.getName()).contains(stepKey)) {
270+
// stepKey is valid still so return it
271+
return stepKey;
272+
} else {
273+
// stepKey no longer exists in the action so we need to move
274+
// to the first step in the next action since skipping steps
275+
// in an action is not safe
276+
return getFirstStepInNextAction(stepKey.getAction(), phase);
277+
}
278+
}
279+
}
280+
}
281+
282+
private StepKey getNextAfterStep(String currentPhaseName) {
283+
String nextPhaseName = type.getNextPhaseName(currentPhaseName, phases);
284+
if (nextPhaseName == null) {
285+
// We don't have a next phase after this one so there is no after
286+
// step to move to. Instead we need to go to the terminal step as
287+
// there are no more steps we should execute
288+
return TerminalPolicyStep.KEY;
289+
} else {
290+
return new StepKey(currentPhaseName, PhaseAfterStep.NAME, PhaseAfterStep.NAME);
291+
}
292+
}
293+
294+
private StepKey getAfterStepBeforePhase(String currentPhaseName) {
295+
String nextPhaseName = type.getNextPhaseName(currentPhaseName, phases);
296+
if (nextPhaseName == null) {
297+
// We don't have a next phase after this one so the next step is the
298+
// TerminalPolicyStep
299+
return TerminalPolicyStep.KEY;
300+
} else {
301+
String prevPhaseName = type.getPreviousPhaseName(currentPhaseName, phases);
302+
if (prevPhaseName == null) {
303+
// no previous phase available so go to the
304+
// InitializePolicyContextStep
305+
return InitializePolicyContextStep.KEY;
306+
}
307+
return new StepKey(prevPhaseName, PhaseAfterStep.NAME, PhaseAfterStep.NAME);
308+
}
309+
}
310+
311+
private StepKey getFirstStepInNextAction(String currentActionName, Phase phase) {
312+
String nextActionName = type.getNextActionName(currentActionName, phase);
313+
if (nextActionName == null) {
314+
// The current action is the last in this phase so we need to find
315+
// the next after step
316+
return getNextAfterStep(phase.getName());
317+
} else {
318+
LifecycleAction nextAction = phase.getActions().get(nextActionName);
319+
// Return the first stepKey for nextAction
320+
return nextAction.toStepKeys(phase.getName()).get(0);
321+
}
322+
}
323+
248324
@Override
249325
public int hashCode() {
250326
return Objects.hash(name, phases);

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleType.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import org.elasticsearch.common.io.stream.NamedWriteable;
99

10+
import java.security.Policy;
1011
import java.util.Collection;
1112
import java.util.List;
1213
import java.util.Map;
@@ -18,8 +19,47 @@ public interface LifecycleType extends NamedWriteable {
1819
*/
1920
List<Phase> getOrderedPhases(Map<String, Phase> phases);
2021

22+
/**
23+
* Returns the next phase thats available after
24+
* <code>currentPhaseName</code>. Note that <code>currentPhaseName</code>
25+
* does not need to exist in <code>phases</code>.
26+
*
27+
* If the current {@link Phase} is the last phase in the {@link Policy} this
28+
* method will return <code>null</code>.
29+
*
30+
* If the phase is not valid for the lifecycle type an
31+
* {@link IllegalArgumentException} will be thrown.
32+
*/
33+
String getNextPhaseName(String currentPhaseName, Map<String, Phase> phases);
34+
35+
/**
36+
* Returns the previous phase thats available before
37+
* <code>currentPhaseName</code>. Note that <code>currentPhaseName</code>
38+
* does not need to exist in <code>phases</code>.
39+
*
40+
* If the current {@link Phase} is the first phase in the {@link Policy}
41+
* this method will return <code>null</code>.
42+
*
43+
* If the phase is not valid for the lifecycle type an
44+
* {@link IllegalArgumentException} will be thrown.
45+
*/
46+
String getPreviousPhaseName(String currentPhaseName, Map<String, Phase> phases);
47+
2148
List<LifecycleAction> getOrderedActions(Phase phase);
2249

50+
/**
51+
* Returns the name of the next phase that is available in the phases after
52+
* <code>currentActionName</code>. Note that <code>currentActionName</code>
53+
* does not need to exist in the {@link Phase}.
54+
*
55+
* If the current action is the last action in the phase this method will
56+
* return <code>null</code>.
57+
*
58+
* If the action is not valid for the phase an
59+
* {@link IllegalArgumentException} will be thrown.
60+
*/
61+
String getNextActionName(String currentActionName, Phase phase);
62+
2363

2464
/**
2565
* validates whether the specified <code>phases</code> are valid for this

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ReadOnlyAction.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.common.xcontent.ObjectParser;
1515
import org.elasticsearch.common.xcontent.XContentBuilder;
1616
import org.elasticsearch.common.xcontent.XContentParser;
17+
import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey;
1718

1819
import java.io.IOException;
1920
import java.util.Collections;
@@ -65,6 +66,11 @@ public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey)
6566
Settings readOnlySettings = Settings.builder().put(IndexMetaData.SETTING_BLOCKS_WRITE, true).build();
6667
return Collections.singletonList(new UpdateSettingsStep(key, nextStepKey, client, readOnlySettings));
6768
}
69+
70+
@Override
71+
public List<StepKey> toStepKeys(String phase) {
72+
return Collections.singletonList(new Step.StepKey(phase, NAME, NAME));
73+
}
6874

6975
@Override
7076
public int hashCode() {

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ReplicasAction.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey)
8787
new ReplicasAllocatedStep(enoughKey, nextStepKey));
8888
}
8989

90+
@Override
91+
public List<StepKey> toStepKeys(String phase) {
92+
StepKey updateReplicasKey = new StepKey(phase, NAME, UpdateSettingsStep.NAME);
93+
StepKey enoughKey = new StepKey(phase, NAME, ReplicasAllocatedStep.NAME);
94+
return Arrays.asList(updateReplicasKey, enoughKey);
95+
}
96+
9097
public int getNumberOfReplicas() {
9198
return numberOfReplicas;
9299
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/RolloverAction.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey)
139139
return Arrays.asList(rolloverStep, updateDateStep);
140140
}
141141

142+
@Override
143+
public List<StepKey> toStepKeys(String phase) {
144+
StepKey rolloverStepKey = new StepKey(phase, NAME, RolloverStep.NAME);
145+
StepKey updateDateStepKey = new StepKey(phase, NAME, UpdateRolloverLifecycleDateStep.NAME);
146+
return Arrays.asList(rolloverStepKey, updateDateStepKey);
147+
}
148+
142149
@Override
143150
public int hashCode() {
144151
return Objects.hash(maxSize, maxAge, maxDocs);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey)
9797
return Arrays.asList(setSingleNodeStep, allocationStep, shrink, allocated, aliasSwapAndDelete, waitOnShrinkTakeover);
9898
}
9999

100+
@Override
101+
public List<StepKey> toStepKeys(String phase) {
102+
StepKey setSingleNodeKey = new StepKey(phase, NAME, SetSingleNodeAllocateStep.NAME);
103+
StepKey allocationRoutedKey = new StepKey(phase, NAME, AllocationRoutedStep.NAME);
104+
StepKey shrinkKey = new StepKey(phase, NAME, ShrinkStep.NAME);
105+
StepKey enoughShardsKey = new StepKey(phase, NAME, ShrunkShardsAllocatedStep.NAME);
106+
StepKey aliasKey = new StepKey(phase, NAME, ShrinkSetAliasStep.NAME);
107+
StepKey isShrunkIndexKey = new StepKey(phase, NAME, ShrunkenIndexCheckStep.NAME);
108+
return Arrays.asList(setSingleNodeKey, allocationRoutedKey, shrinkKey, enoughShardsKey, aliasKey, isShrunkIndexKey);
109+
}
110+
100111
@Override
101112
public boolean equals(Object o) {
102113
if (this == o) return true;

0 commit comments

Comments
 (0)