Skip to content

Commit 819b73c

Browse files
colings86jasontedor
authored andcommitted
Changes PhaseAfterStep to take the name of the previous phase (#30756)
* Changes PhaseAfterStep to take the name of the previous phase This changes the way the phase after step is built so its key has the phase name of the phase that preceeds it rather than the phase that follows it. This is more intuitive to the user since the index is in the warm phase until the after condition for the cold phase is met. * Fixes REST tests x-pack/plugin/src/test/resources/rest-api-spec/test/index_lifecycle/20_m ove_to_step.yml x-pack/plugin/src/test/resources/rest-api-spec/test/index_lifecycle/20_m ove_to_step.yml
1 parent e491d89 commit 819b73c

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,19 @@ public List<Step> toSteps(Client client, LongSupplier nowSupplier) {
178178
// add steps for each phase, in reverse
179179
while (phaseIterator.hasPrevious()) {
180180

181+
Phase previousPhase = phaseIterator.previous();
182+
181183
// add `after` step for phase before next
182184
if (phase != null) {
183-
Step.StepKey afterStepKey = new Step.StepKey(phase.getName(), "pre-" + lastStepKey.getAction(), "after");
185+
// after step should have the name of the previous phase since the index is still in the
186+
// previous phase until the after condition is reached
187+
Step.StepKey afterStepKey = new Step.StepKey(previousPhase.getName(), PhaseAfterStep.NAME, PhaseAfterStep.NAME);
184188
Step phaseAfterStep = new PhaseAfterStep(nowSupplier, phase.getAfter(), afterStepKey, lastStepKey);
185189
steps.add(phaseAfterStep);
186190
lastStepKey = phaseAfterStep.getKey();
187191
}
188192

189-
phase = phaseIterator.previous();
193+
phase = previousPhase;
190194
List<LifecycleAction> orderedActions = type.getOrderedActions(phase);
191195
ListIterator<LifecycleAction> actionIterator = orderedActions.listIterator(orderedActions.size());
192196
// add steps for each action, in reverse
@@ -203,7 +207,8 @@ public List<Step> toSteps(Client client, LongSupplier nowSupplier) {
203207
}
204208

205209
if (phase != null) {
206-
Step.StepKey afterStepKey = new Step.StepKey(phase.getName(), "pre-" + lastStepKey.getAction(), "after");
210+
// The very first after step is in a phase before the hot phase so call this "new"
211+
Step.StepKey afterStepKey = new Step.StepKey("new", PhaseAfterStep.NAME, PhaseAfterStep.NAME);
207212
Step phaseAfterStep = new PhaseAfterStep(nowSupplier, phase.getAfter(), afterStepKey, lastStepKey);
208213
steps.add(phaseAfterStep);
209214
lastStepKey = phaseAfterStep.getKey();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.function.LongSupplier;
1515

1616
public class PhaseAfterStep extends ClusterStateWaitStep {
17+
public static final String NAME = "after";
1718
private final TimeValue after;
1819
private final LongSupplier nowSupplier;
1920

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void testToStepsWithOneStep() {
135135
phases.put(firstPhase.getName(), firstPhase);
136136
LifecyclePolicy policy = new LifecyclePolicy(TestLifecycleType.INSTANCE, lifecycleName, phases);
137137
StepKey firstStepKey = InitializePolicyContextStep.KEY;
138-
StepKey secondStepKey = new StepKey("test", "pre-test", "after");
138+
StepKey secondStepKey = new StepKey("new", PhaseAfterStep.NAME, PhaseAfterStep.NAME);
139139
List<Step> steps = policy.toSteps(client, nowSupplier);
140140
assertThat(steps.size(), equalTo(4));
141141
assertSame(steps.get(0).getKey(), firstStepKey);
@@ -151,10 +151,11 @@ public void testToStepsWithTwoPhases() {
151151
Client client = mock(Client.class);
152152
LongSupplier nowSupplier = () -> 0L;
153153
MockStep secondActionStep = new MockStep(new StepKey("second_phase", "test2", "test"), TerminalPolicyStep.KEY);
154-
MockStep secondAfter = new MockStep(new StepKey("second_phase", "pre-test2", "after"), secondActionStep.getKey());
154+
MockStep secondAfter = new MockStep(new StepKey("first_phase", PhaseAfterStep.NAME, PhaseAfterStep.NAME),
155+
secondActionStep.getKey());
155156
MockStep firstActionAnotherStep = new MockStep(new StepKey("first_phase", "test", "bar"), secondAfter.getKey());
156157
MockStep firstActionStep = new MockStep(new StepKey("first_phase", "test", "foo"), firstActionAnotherStep.getKey());
157-
MockStep firstAfter = new MockStep(new StepKey("first_phase", "pre-test", "after"), firstActionStep.getKey());
158+
MockStep firstAfter = new MockStep(new StepKey("new", PhaseAfterStep.NAME, PhaseAfterStep.NAME), firstActionStep.getKey());
158159
MockStep init = new MockStep(InitializePolicyContextStep.KEY, firstAfter.getKey());
159160

160161
lifecycleName = randomAlphaOfLengthBetween(1, 20);

x-pack/plugin/src/test/resources/rest-api-spec/test/index_lifecycle/20_move_to_step.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ teardown:
7676
index: "my_index"
7777
body:
7878
current_step:
79-
phase: "hot"
80-
action: "pre-pre-readonly"
79+
phase: "new"
80+
action: "after"
8181
name: "after"
8282
next_step:
8383
phase: "warm"
@@ -118,8 +118,8 @@ teardown:
118118
index: my_index
119119
- match: { my_index.settings.index.lifecycle.name: "my_moveable_timeseries_lifecycle" }
120120
- match: { my_index.settings.index.lifecycle.step: "after" }
121-
- match: { my_index.settings.index.lifecycle.action: "pre-pre-readonly" }
122-
- match: { my_index.settings.index.lifecycle.phase: "hot" }
121+
- match: { my_index.settings.index.lifecycle.action: "after" }
122+
- match: { my_index.settings.index.lifecycle.phase: "new" }
123123

124124
---
125125
"Test Invalid Move To Step With Invalid Next Step":
@@ -130,8 +130,8 @@ teardown:
130130
index: "my_index"
131131
body:
132132
current_step:
133-
phase: "hot"
134-
action: "pre-pre-readonly"
133+
phase: "new"
134+
action: "after"
135135
name: "after"
136136
next_step:
137137
phase: "invalid"
@@ -146,8 +146,8 @@ teardown:
146146
index: my_index
147147
- match: { my_index.settings.index.lifecycle.name: "my_moveable_timeseries_lifecycle" }
148148
- match: { my_index.settings.index.lifecycle.step: "after" }
149-
- match: { my_index.settings.index.lifecycle.action: "pre-pre-readonly" }
150-
- match: { my_index.settings.index.lifecycle.phase: "hot" }
149+
- match: { my_index.settings.index.lifecycle.action: "after" }
150+
- match: { my_index.settings.index.lifecycle.phase: "new" }
151151

152152
---
153153
"Test Invalid Move To Step With Invalid Policy":

0 commit comments

Comments
 (0)