From 9101dd6ab2b261015e4f5bd9fc7ab5cb8c65c2cc Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Tue, 21 Aug 2018 13:35:57 -0600 Subject: [PATCH] Remove PhaseAfterStep This commit removes PhaseAfterStep and all the plumbing associated with it. Instead, we rely on the LifecyclePolicyRunner to police itself for advancing phases. This also makes a modification to the settings that are exposed related to the current phase, instead of returning the current phase/step/action as-is in the `index.lifecycle.phase` (etc) setting, these are now split into: `index.lifecycle.current_phase|action|step` - the currently executing phase/action/step which may or may not have completed `index.lifecycle.next_phase|action|step` - the next phase/action/step to which we will be proceeding While I don't think these will cause much issue (especially since nothing is being broken for users here), these changes were required to have the `phase_time` correctly updated now that we don't have a "shim" step between phases. Without these it would be confusing as the index would advance to have an `index.lifecycle.phase` setting that was potentially one phase in the future. Relates to #29823 --- .../client/IndexLifecycleIT.java | 4 +- .../InitializePolicyContextStep.java | 3 +- .../core/indexlifecycle/LifecyclePolicy.java | 63 +--- .../indexlifecycle/LifecycleSettings.java | 26 +- .../core/indexlifecycle/PhaseAfterStep.java | 60 ---- .../xpack/core/indexlifecycle/ShrinkStep.java | 18 +- .../indexlifecycle/TerminalPolicyStep.java | 3 +- .../indexlifecycle/LifecyclePolicyTests.java | 86 +++-- .../indexlifecycle/PhaseAfterStepTests.java | 103 ------ .../core/indexlifecycle/ShrinkStepTests.java | 12 +- .../ExecuteStepsUpdateTask.java | 3 +- .../xpack/indexlifecycle/IndexLifecycle.java | 10 +- .../indexlifecycle/IndexLifecycleRunner.java | 112 +++++-- .../indexlifecycle/IndexLifecycleService.java | 2 +- .../indexlifecycle/PolicyStepsRegistry.java | 30 +- .../TransportExplainLifecycleAction.java | 6 +- .../action/TransportMoveToStepAction.java | 19 +- .../ExecuteStepsUpdateTaskTests.java | 42 +-- .../IndexLifecycleInitialisationIT.java | 10 +- .../IndexLifecycleRunnerTests.java | 305 +++++++++++------- .../IndexLifecycleServiceTests.java | 12 +- .../MoveToErrorStepUpdateTaskTests.java | 8 +- .../MoveToNextStepUpdateTaskTests.java | 6 +- .../PolicyStepsRegistryTests.java | 26 +- .../SetStepInfoUpdateTaskTests.java | 6 +- .../TimeSeriesLifecycleActionsIT.java | 6 +- .../test/ilm/20_move_to_step.yml | 30 +- .../rest-api-spec/test/ilm/30_retry.yml | 6 +- .../test/ilm/40_explain_lifecycle.yml | 89 +++-- 29 files changed, 558 insertions(+), 548 deletions(-) delete mode 100644 x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/PhaseAfterStep.java delete mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/PhaseAfterStepTests.java diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java index ad9064cf160ad..64e847d0cdc9e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java @@ -257,8 +257,8 @@ public void testExplainLifecycle() throws Exception { GetSettingsResponse settingsResponse = highLevelClient().indices().getSettings(getSettingsRequest, RequestOptions.DEFAULT); assertThat(settingsResponse.getSetting("foo", "index.lifecycle.name"), equalTo(policy)); assertThat(settingsResponse.getSetting("baz", "index.lifecycle.name"), equalTo(policy)); - assertThat(settingsResponse.getSetting("foo", "index.lifecycle.phase"), equalTo("hot")); - assertThat(settingsResponse.getSetting("baz", "index.lifecycle.phase"), equalTo("hot")); + assertThat(settingsResponse.getSetting("foo", "index.lifecycle.next_phase"), equalTo("hot")); + assertThat(settingsResponse.getSetting("baz", "index.lifecycle.next_phase"), equalTo("hot")); }); ExplainLifecycleRequest req = new ExplainLifecycleRequest(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/InitializePolicyContextStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/InitializePolicyContextStep.java index 157488bb5e3c8..d22ed2ef2e688 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/InitializePolicyContextStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/InitializePolicyContextStep.java @@ -12,7 +12,8 @@ import org.elasticsearch.index.Index; public final class InitializePolicyContextStep extends ClusterStateActionStep { - public static final StepKey KEY = new StepKey("new", "init", "init"); + public static final String INITIALIZATION_PHASE = "new"; + public static final StepKey KEY = new StepKey(INITIALIZATION_PHASE, "init", "init"); public InitializePolicyContextStep(Step.StepKey key, StepKey nextStepKey) { super(key, nextStepKey); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicy.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicy.java index abf46df9a161b..c31bf835de036 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicy.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicy.java @@ -28,7 +28,6 @@ import java.util.Map; import java.util.Objects; import java.util.function.Function; -import java.util.function.LongSupplier; import java.util.stream.Collectors; /** @@ -165,10 +164,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws * * @param client The Elasticsearch Client to use during execution of {@link AsyncActionStep} * and {@link AsyncWaitStep} steps. - * @param nowSupplier The supplier of the current time for {@link PhaseAfterStep} steps. * @return The list of {@link Step} objects in order of their execution. */ - public List toSteps(Client client, LongSupplier nowSupplier) { + public List toSteps(Client client) { List steps = new ArrayList<>(); List orderedPhases = type.getOrderedPhases(phases); ListIterator phaseIterator = orderedPhases.listIterator(orderedPhases.size()); @@ -183,16 +181,6 @@ public List toSteps(Client client, LongSupplier nowSupplier) { Phase previousPhase = phaseIterator.previous(); - // add `after` step for phase before next - if (phase != null) { - // after step should have the name of the previous phase since the index is still in the - // previous phase until the after condition is reached - Step.StepKey afterStepKey = new Step.StepKey(previousPhase.getName(), PhaseAfterStep.NAME, PhaseAfterStep.NAME); - Step phaseAfterStep = new PhaseAfterStep(nowSupplier, phase.getAfter(), afterStepKey, lastStepKey); - steps.add(phaseAfterStep); - lastStepKey = phaseAfterStep.getKey(); - } - phase = previousPhase; List orderedActions = type.getOrderedActions(phase); ListIterator actionIterator = orderedActions.listIterator(orderedActions.size()); @@ -209,14 +197,6 @@ public List toSteps(Client client, LongSupplier nowSupplier) { } } - if (phase != null) { - // The very first after step is in a phase before the hot phase so call this "new" - Step.StepKey afterStepKey = new Step.StepKey("new", PhaseAfterStep.NAME, PhaseAfterStep.NAME); - Step phaseAfterStep = new PhaseAfterStep(nowSupplier, phase.getAfter(), afterStepKey, lastStepKey); - steps.add(phaseAfterStep); - lastStepKey = phaseAfterStep.getKey(); - } - // init step so that policy is guaranteed to have steps.add(new InitializePolicyContextStep(InitializePolicyContextStep.KEY, lastStepKey)); @@ -240,10 +220,10 @@ public boolean isActionSafe(StepKey stepKey) { return action.isSafeAction(); } else { throw new IllegalArgumentException("Action [" + stepKey.getAction() + "] in phase [" + stepKey.getPhase() - + "] does not exist in policy [" + name + "]"); + + "] does not exist in policy [" + name + "]"); } } else { - throw new IllegalArgumentException("Phase [" + stepKey.getPhase() + "] does not exist in policy [" + name + "]"); + throw new IllegalArgumentException("Phase [" + stepKey.getPhase() + "] does not exist in policy [" + name + "]"); } } @@ -256,9 +236,7 @@ public boolean isActionSafe(StepKey stepKey) { public StepKey getNextValidStep(StepKey stepKey) { Phase phase = phases.get(stepKey.getPhase()); if (phase == null) { - // Phase doesn't exist so find the after step for the previous - // available phase - return getAfterStepBeforePhase(stepKey.getPhase()); + throw new IllegalArgumentException("unable to find next valid step after " + stepKey + " for policy " + name); } else { // Phase exists so check if the action exists LifecycleAction action = phase.getActions().get(stepKey.getAction()); @@ -281,41 +259,10 @@ public StepKey getNextValidStep(StepKey stepKey) { } } - private StepKey getNextAfterStep(String currentPhaseName) { - String nextPhaseName = type.getNextPhaseName(currentPhaseName, phases); - if (nextPhaseName == null) { - // We don't have a next phase after this one so there is no after - // step to move to. Instead we need to go to the terminal step as - // there are no more steps we should execute - return TerminalPolicyStep.KEY; - } else { - return new StepKey(currentPhaseName, PhaseAfterStep.NAME, PhaseAfterStep.NAME); - } - } - - private StepKey getAfterStepBeforePhase(String currentPhaseName) { - String nextPhaseName = type.getNextPhaseName(currentPhaseName, phases); - if (nextPhaseName == null) { - // We don't have a next phase after this one so the next step is the - // TerminalPolicyStep - return TerminalPolicyStep.KEY; - } else { - String prevPhaseName = type.getPreviousPhaseName(currentPhaseName, phases); - if (prevPhaseName == null) { - // no previous phase available so go to the - // InitializePolicyContextStep - return InitializePolicyContextStep.KEY; - } - return new StepKey(prevPhaseName, PhaseAfterStep.NAME, PhaseAfterStep.NAME); - } - } - private StepKey getFirstStepInNextAction(String currentActionName, Phase phase) { String nextActionName = type.getNextActionName(currentActionName, phase); if (nextActionName == null) { - // The current action is the last in this phase so we need to find - // the next after step - return getNextAfterStep(phase.getName()); + return TerminalPolicyStep.KEY; } else { LifecycleAction nextAction = phase.getActions().get(nextActionName); // Return the first stepKey for nextAction diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleSettings.java index f865b970339d7..8229e6c2bad97 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleSettings.java @@ -14,9 +14,12 @@ public class LifecycleSettings { public static final String LIFECYCLE_POLL_INTERVAL = "indices.lifecycle.poll_interval"; public static final String LIFECYCLE_NAME = "index.lifecycle.name"; - public static final String LIFECYCLE_PHASE = "index.lifecycle.phase"; - public static final String LIFECYCLE_ACTION = "index.lifecycle.action"; - public static final String LIFECYCLE_STEP = "index.lifecycle.step"; + public static final String LIFECYCLE_NEXT_PHASE = "index.lifecycle.next_phase"; + public static final String LIFECYCLE_NEXT_ACTION = "index.lifecycle.next_action"; + public static final String LIFECYCLE_NEXT_STEP = "index.lifecycle.next_step"; + public static final String LIFECYCLE_CURRENT_PHASE = "index.lifecycle.current_phase"; + public static final String LIFECYCLE_CURRENT_ACTION = "index.lifecycle.current_action"; + public static final String LIFECYCLE_CURRENT_STEP = "index.lifecycle.current_step"; public static final String LIFECYCLE_INDEX_CREATION_DATE = "index.lifecycle.date"; public static final String LIFECYCLE_PHASE_TIME = "index.lifecycle.phase_time"; public static final String LIFECYCLE_ACTION_TIME = "index.lifecycle.action_time"; @@ -24,16 +27,23 @@ public class LifecycleSettings { public static final String LIFECYCLE_FAILED_STEP = "index.lifecycle.failed_step"; public static final String LIFECYCLE_STEP_INFO = "index.lifecycle.step_info"; public static final String LIFECYCLE_SKIP = "index.lifecycle.skip"; + public static final String LIFECYCLE_FORCED_PHASE = "index.lifecycle.forced_phase"; public static final Setting LIFECYCLE_POLL_INTERVAL_SETTING = Setting.positiveTimeSetting(LIFECYCLE_POLL_INTERVAL, TimeValue.timeValueMinutes(10), Setting.Property.Dynamic, Setting.Property.NodeScope); public static final Setting LIFECYCLE_NAME_SETTING = Setting.simpleString(LIFECYCLE_NAME, Setting.Property.Dynamic, Setting.Property.IndexScope, Setting.Property.InternalIndex); - public static final Setting LIFECYCLE_PHASE_SETTING = Setting.simpleString(LIFECYCLE_PHASE, + public static final Setting LIFECYCLE_NEXT_PHASE_SETTING = Setting.simpleString(LIFECYCLE_NEXT_PHASE, Setting.Property.Dynamic, Setting.Property.IndexScope, Setting.Property.InternalIndex); - public static final Setting LIFECYCLE_ACTION_SETTING = Setting.simpleString(LIFECYCLE_ACTION, - Setting.Property.Dynamic, Setting.Property.IndexScope, Setting.Property.InternalIndex); - public static final Setting LIFECYCLE_STEP_SETTING = Setting.simpleString(LIFECYCLE_STEP, + public static final Setting LIFECYCLE_NEXT_ACTION_SETTING = Setting.simpleString(LIFECYCLE_NEXT_ACTION, + Setting.Property.Dynamic, Setting.Property.IndexScope, Setting.Property.InternalIndex); + public static final Setting LIFECYCLE_NEXT_STEP_SETTING = Setting.simpleString(LIFECYCLE_NEXT_STEP, + Setting.Property.Dynamic, Setting.Property.IndexScope, Setting.Property.InternalIndex); + public static final Setting LIFECYCLE_CURRENT_PHASE_SETTING = Setting.simpleString(LIFECYCLE_CURRENT_PHASE, + Setting.Property.Dynamic, Setting.Property.IndexScope, Setting.Property.InternalIndex); + public static final Setting LIFECYCLE_CURRENT_ACTION_SETTING = Setting.simpleString(LIFECYCLE_CURRENT_ACTION, + Setting.Property.Dynamic, Setting.Property.IndexScope, Setting.Property.InternalIndex); + public static final Setting LIFECYCLE_CURRENT_STEP_SETTING = Setting.simpleString(LIFECYCLE_CURRENT_STEP, Setting.Property.Dynamic, Setting.Property.IndexScope, Setting.Property.InternalIndex); public static final Setting LIFECYCLE_FAILED_STEP_SETTING = Setting.simpleString(LIFECYCLE_FAILED_STEP, Setting.Property.Dynamic, Setting.Property.IndexScope, Setting.Property.InternalIndex); @@ -49,4 +59,6 @@ public class LifecycleSettings { Setting.Property.IndexScope, Setting.Property.NotCopyableOnResize, Setting.Property.InternalIndex); public static final Setting LIFECYCLE_SKIP_SETTING = Setting.boolSetting(LIFECYCLE_SKIP, false, Setting.Property.Dynamic, Setting.Property.IndexScope); + public static final Setting LIFECYCLE_FORCED_PHASE_SETTING = Setting.simpleString(LIFECYCLE_FORCED_PHASE, + Setting.Property.Dynamic, Setting.Property.IndexScope, Setting.Property.InternalIndex); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/PhaseAfterStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/PhaseAfterStep.java deleted file mode 100644 index 3cc090b106f4f..0000000000000 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/PhaseAfterStep.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.core.indexlifecycle; - -import org.elasticsearch.cluster.ClusterState; -import org.elasticsearch.cluster.metadata.IndexMetaData; -import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.index.Index; - -import java.util.Objects; -import java.util.function.LongSupplier; - -public class PhaseAfterStep extends ClusterStateWaitStep { - public static final String NAME = "after"; - private final TimeValue after; - private final LongSupplier nowSupplier; - - PhaseAfterStep(LongSupplier nowSupplier, TimeValue after, StepKey key, StepKey nextStepKey) { - super(key, nextStepKey); - this.nowSupplier = nowSupplier; - this.after = after; - } - - @Override - public Result isConditionMet(Index index, ClusterState clusterState) { - IndexMetaData indexMetaData = clusterState.metaData().index(index); - long lifecycleDate = indexMetaData.getSettings() - .getAsLong(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, -1L); - return new Result(nowSupplier.getAsLong() >= lifecycleDate + after.getMillis(), null); - } - - TimeValue getAfter() { - return after; - } - - LongSupplier getNowSupplier() { - return nowSupplier; - } - - @Override - public int hashCode() { - return Objects.hash(super.hashCode(), after); - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - PhaseAfterStep other = (PhaseAfterStep) obj; - return super.equals(obj) && - Objects.equals(after, other.after); - } -} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkStep.java index 16fbf7dbc31ed..d1215b15521c4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkStep.java @@ -45,18 +45,18 @@ public void performAction(IndexMetaData indexMetaData, ClusterState currentState "] is missing setting[" + LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE); } String lifecycle = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetaData.getSettings()); - String phase = LifecycleSettings.LIFECYCLE_PHASE_SETTING.get(indexMetaData.getSettings()); - String action = LifecycleSettings.LIFECYCLE_ACTION_SETTING.get(indexMetaData.getSettings()); + String phase = LifecycleSettings.LIFECYCLE_NEXT_PHASE_SETTING.get(indexMetaData.getSettings()); + String action = LifecycleSettings.LIFECYCLE_NEXT_ACTION_SETTING.get(indexMetaData.getSettings()); Settings relevantTargetSettings = Settings.builder() .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, numberOfShards) .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, indexMetaData.getNumberOfReplicas()) .put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, lifecycleDate) .put(LifecycleSettings.LIFECYCLE_NAME, lifecycle) - .put(LifecycleSettings.LIFECYCLE_PHASE, phase) - .put(LifecycleSettings.LIFECYCLE_ACTION, action) - .put(LifecycleSettings.LIFECYCLE_STEP, ShrunkenIndexCheckStep.NAME) // skip source-index steps - .put(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_name", (String) null) // need to remove the single shard + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, phase) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, action) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, ShrunkenIndexCheckStep.NAME) // skip source-index steps + .put(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_name", (String) null) // need to remove the single shard // allocation so replicas can be allocated .build(); String shrunkenIndexName = shrunkIndexPrefix + indexMetaData.getIndex().getName(); @@ -76,7 +76,7 @@ public void performAction(IndexMetaData indexMetaData, ClusterState currentState public int hashCode() { return Objects.hash(super.hashCode(), numberOfShards, shrunkIndexPrefix); } - + @Override public boolean equals(Object obj) { if (obj == null) { @@ -86,8 +86,8 @@ public boolean equals(Object obj) { return false; } ShrinkStep other = (ShrinkStep) obj; - return super.equals(obj) && - Objects.equals(numberOfShards, other.numberOfShards) && + return super.equals(obj) && + Objects.equals(numberOfShards, other.numberOfShards) && Objects.equals(shrunkIndexPrefix, other.shrunkIndexPrefix); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/TerminalPolicyStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/TerminalPolicyStep.java index dfff7cd760e43..56db205dc94da 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/TerminalPolicyStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/TerminalPolicyStep.java @@ -6,7 +6,8 @@ package org.elasticsearch.xpack.core.indexlifecycle; public class TerminalPolicyStep extends Step { - public static final StepKey KEY = new StepKey("completed", "completed", "completed"); + public static final String TERMINAL_PHASE = "completed"; + public static final StepKey KEY = new StepKey(TERMINAL_PHASE, "completed", "completed"); public static final TerminalPolicyStep INSTANCE = new TerminalPolicyStep(KEY, null); TerminalPolicyStep(StepKey key, StepKey nextStepKey) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyTests.java index 1b4bbe0f4e5a3..06916ab17e1cf 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecyclePolicyTests.java @@ -30,9 +30,9 @@ import java.util.Objects; import java.util.Set; import java.util.function.Function; -import java.util.function.LongSupplier; import java.util.stream.Collectors; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; import static org.mockito.Mockito.mock; @@ -170,11 +170,10 @@ protected Reader instanceReader() { public void testFirstAndLastSteps() { Client client = mock(Client.class); - LongSupplier nowSupplier = () -> 0L; lifecycleName = randomAlphaOfLengthBetween(1, 20); Map phases = new LinkedHashMap<>(); LifecyclePolicy policy = new LifecyclePolicy(TestLifecycleType.INSTANCE, lifecycleName, phases); - List steps = policy.toSteps(client, nowSupplier); + List steps = policy.toSteps(client); assertThat(steps.size(), equalTo(2)); assertThat(steps.get(0), instanceOf(InitializePolicyContextStep.class)); assertThat(steps.get(0).getKey(), equalTo(new StepKey("new", "init", "init"))); @@ -184,7 +183,6 @@ public void testFirstAndLastSteps() { public void testToStepsWithOneStep() { Client client = mock(Client.class); - LongSupplier nowSupplier = () -> 0L; MockStep mockStep = new MockStep( new Step.StepKey("test", "test", "test"), TerminalPolicyStep.KEY); @@ -196,28 +194,21 @@ public void testToStepsWithOneStep() { phases.put(firstPhase.getName(), firstPhase); LifecyclePolicy policy = new LifecyclePolicy(TestLifecycleType.INSTANCE, lifecycleName, phases); StepKey firstStepKey = InitializePolicyContextStep.KEY; - StepKey secondStepKey = new StepKey("new", PhaseAfterStep.NAME, PhaseAfterStep.NAME); - List steps = policy.toSteps(client, nowSupplier); - assertThat(steps.size(), equalTo(4)); + List steps = policy.toSteps(client); + assertThat(steps.size(), equalTo(3)); assertSame(steps.get(0).getKey(), firstStepKey); - assertThat(steps.get(0).getNextStepKey(), equalTo(secondStepKey)); - assertThat(steps.get(1).getKey(), equalTo(secondStepKey)); - assertThat(steps.get(1).getNextStepKey(), equalTo(mockStep.getKey())); - assertThat(steps.get(2).getKey(), equalTo(mockStep.getKey())); - assertThat(steps.get(2).getNextStepKey(), equalTo(TerminalPolicyStep.KEY)); - assertSame(steps.get(3), TerminalPolicyStep.INSTANCE); + assertThat(steps.get(0).getNextStepKey(), equalTo(mockStep.getKey())); + assertThat(steps.get(1).getKey(), equalTo(mockStep.getKey())); + assertThat(steps.get(1).getNextStepKey(), equalTo(TerminalPolicyStep.KEY)); + assertSame(steps.get(2), TerminalPolicyStep.INSTANCE); } public void testToStepsWithTwoPhases() { Client client = mock(Client.class); - LongSupplier nowSupplier = () -> 0L; MockStep secondActionStep = new MockStep(new StepKey("second_phase", "test2", "test"), TerminalPolicyStep.KEY); - MockStep secondAfter = new MockStep(new StepKey("first_phase", PhaseAfterStep.NAME, PhaseAfterStep.NAME), - secondActionStep.getKey()); - MockStep firstActionAnotherStep = new MockStep(new StepKey("first_phase", "test", "bar"), secondAfter.getKey()); + MockStep firstActionAnotherStep = new MockStep(new StepKey("first_phase", "test", "bar"), secondActionStep.getKey()); MockStep firstActionStep = new MockStep(new StepKey("first_phase", "test", "foo"), firstActionAnotherStep.getKey()); - MockStep firstAfter = new MockStep(new StepKey("new", PhaseAfterStep.NAME, PhaseAfterStep.NAME), firstActionStep.getKey()); - MockStep init = new MockStep(InitializePolicyContextStep.KEY, firstAfter.getKey()); + MockStep init = new MockStep(InitializePolicyContextStep.KEY, firstActionStep.getKey()); lifecycleName = randomAlphaOfLengthBetween(1, 20); Map phases = new LinkedHashMap<>(); @@ -231,21 +222,15 @@ public void testToStepsWithTwoPhases() { phases.put(secondPhase.getName(), secondPhase); LifecyclePolicy policy = new LifecyclePolicy(TestLifecycleType.INSTANCE, lifecycleName, phases); - List steps = policy.toSteps(client, nowSupplier); - assertThat(steps.size(), equalTo(7)); + List steps = policy.toSteps(client); + assertThat(steps.size(), equalTo(5)); assertThat(steps.get(0).getClass(), equalTo(InitializePolicyContextStep.class)); assertThat(steps.get(0).getKey(), equalTo(init.getKey())); assertThat(steps.get(0).getNextStepKey(), equalTo(init.getNextStepKey())); - assertThat(steps.get(1).getClass(), equalTo(PhaseAfterStep.class)); - assertThat(steps.get(1).getKey(), equalTo(firstAfter.getKey())); - assertThat(steps.get(1).getNextStepKey(), equalTo(firstAfter.getNextStepKey())); - assertThat(steps.get(2), equalTo(firstActionStep)); - assertThat(steps.get(3), equalTo(firstActionAnotherStep)); - assertThat(steps.get(4).getClass(), equalTo(PhaseAfterStep.class)); - assertThat(steps.get(4).getKey(), equalTo(secondAfter.getKey())); - assertThat(steps.get(4).getNextStepKey(), equalTo(secondAfter.getNextStepKey())); - assertThat(steps.get(5), equalTo(secondActionStep)); - assertSame(steps.get(6), TerminalPolicyStep.INSTANCE); + assertThat(steps.get(1), equalTo(firstActionStep)); + assertThat(steps.get(2), equalTo(firstActionAnotherStep)); + assertThat(steps.get(3), equalTo(secondActionStep)); + assertSame(steps.get(4), TerminalPolicyStep.INSTANCE); } public void testIsActionSafe() { @@ -266,11 +251,11 @@ public void testIsActionSafe() { IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> policy.isActionSafe(new StepKey("non_existant_phase", MockAction.NAME, randomAlphaOfLength(10)))); - assertEquals("Phase [non_existant_phase] does not exist in policy [" + policy.getName() + "]", exception.getMessage()); + assertEquals("Phase [non_existant_phase] does not exist in policy [" + policy.getName() + "]", exception.getMessage()); exception = expectThrows(IllegalArgumentException.class, () -> policy.isActionSafe(new StepKey("first_phase", "non_existant_action", randomAlphaOfLength(10)))); - assertEquals("Action [non_existant_action] in phase [first_phase] does not exist in policy [" + policy.getName() + "]", + assertEquals("Action [non_existant_action] in phase [first_phase] does not exist in policy [" + policy.getName() + "]", exception.getMessage()); assertTrue(policy.isActionSafe(new StepKey("new", randomAlphaOfLength(10), randomAlphaOfLength(10)))); @@ -318,7 +303,7 @@ public void testGetNextValidStep() { Phase phase = new Phase(phaseName, TimeValue.ZERO, actions); phases.put(phase.getName(), phase); } - LifecyclePolicy policy = new LifecyclePolicy(lifecycleType, lifecycleName, phases); + final LifecyclePolicy policy = new LifecyclePolicy(lifecycleType, lifecycleName, phases); // step still exists StepKey currentStep = new StepKey(randomFrom("phase_1", "phase_2", "phase_4"), randomFrom("action_1", "action_3"), @@ -337,7 +322,7 @@ public void testGetNextValidStep() { currentStep = new StepKey("phase_1", "action_3", "step_missing"); nextStep = policy.getNextValidStep(currentStep); assertNotNull(nextStep); - assertEquals(new StepKey("phase_1", PhaseAfterStep.NAME, PhaseAfterStep.NAME), nextStep); + assertEquals(TerminalPolicyStep.KEY, nextStep); // current action exists but step does not and action is last in the // last phase @@ -356,7 +341,7 @@ public void testGetNextValidStep() { currentStep = new StepKey("phase_1", "action_4", "step_2"); nextStep = policy.getNextValidStep(currentStep); assertNotNull(nextStep); - assertEquals(new StepKey("phase_1", PhaseAfterStep.NAME, PhaseAfterStep.NAME), nextStep); + assertEquals(TerminalPolicyStep.KEY, nextStep); // current action no longer exists and action was last in the last phase currentStep = new StepKey("phase_4", "action_4", "step_2"); @@ -365,16 +350,18 @@ public void testGetNextValidStep() { assertEquals(TerminalPolicyStep.KEY, nextStep); // current phase no longer exists - currentStep = new StepKey("phase_3", "action_2", "step_2"); - nextStep = policy.getNextValidStep(currentStep); - assertNotNull(nextStep); - assertEquals(new StepKey("phase_2", PhaseAfterStep.NAME, PhaseAfterStep.NAME), nextStep); + final StepKey badStep = new StepKey("phase_3", "action_2", "step_2"); + Exception e = expectThrows(IllegalArgumentException.class, () -> { policy.getNextValidStep(badStep); }); + assertThat(e.getMessage(), + containsString("unable to find next valid step after " + + "{\"phase\":\"phase_3\",\"action\":\"action_2\",\"name\":\"step_2\"} for policy null")); // current phase no longer exists and was last phase - currentStep = new StepKey("phase_5", "action_2", "step_2"); - nextStep = policy.getNextValidStep(currentStep); - assertNotNull(nextStep); - assertEquals(TerminalPolicyStep.KEY, nextStep); + final StepKey badStep2 = new StepKey("phase_5", "action_2", "step_2"); + e = expectThrows(IllegalArgumentException.class, () -> { policy.getNextValidStep(badStep2); }); + assertThat(e.getMessage(), + containsString("unable to find next valid step after " + + "{\"phase\":\"phase_5\",\"action\":\"action_2\",\"name\":\"step_2\"} for policy null")); // create a new policy where only phase 2 exists and within it has // actions 1 and 3 which both contain steps 1, 2, and 3. @@ -398,13 +385,14 @@ public void testGetNextValidStep() { Phase phase = new Phase(phaseName, TimeValue.ZERO, actions); phases.put(phase.getName(), phase); - policy = new LifecyclePolicy(lifecycleType, lifecycleName, phases); + final LifecyclePolicy newPolicy = new LifecyclePolicy(lifecycleType, lifecycleName, phases); // current phase no longer exists and was first phase - currentStep = new StepKey("phase_1", "action_2", "step_2"); - nextStep = policy.getNextValidStep(currentStep); - assertNotNull(nextStep); - assertEquals(InitializePolicyContextStep.KEY, nextStep); + final StepKey badStep3 = new StepKey("phase_1", "action_2", "step_2"); + e = expectThrows(IllegalArgumentException.class, () -> { newPolicy.getNextValidStep(badStep3); }); + assertThat(e.getMessage(), + containsString("unable to find next valid step after " + + "{\"phase\":\"phase_1\",\"action\":\"action_2\",\"name\":\"step_2\"} for policy null")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/PhaseAfterStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/PhaseAfterStepTests.java deleted file mode 100644 index d6fb6575affa9..0000000000000 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/PhaseAfterStepTests.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.core.indexlifecycle; - - -import org.elasticsearch.Version; -import org.elasticsearch.cluster.ClusterName; -import org.elasticsearch.cluster.ClusterState; -import org.elasticsearch.cluster.metadata.IndexMetaData; -import org.elasticsearch.cluster.metadata.MetaData; -import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.index.Index; -import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateWaitStep.Result; -import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey; - -import java.util.concurrent.TimeUnit; - -public class PhaseAfterStepTests extends AbstractStepTestCase { - - @Override - public PhaseAfterStep createRandomInstance() { - StepKey stepKey = randomStepKey(); - StepKey nextStepKey = randomStepKey(); - TimeValue after = createRandomTimeValue(); - return new PhaseAfterStep(null, after, stepKey, nextStepKey); - } - - private TimeValue createRandomTimeValue() { - return new TimeValue(randomLongBetween(1, 10000), randomFrom(TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS, TimeUnit.DAYS)); - } - - @Override - public PhaseAfterStep mutateInstance(PhaseAfterStep instance) { - StepKey key = instance.getKey(); - StepKey nextKey = instance.getNextStepKey(); - TimeValue after = instance.getAfter(); - - switch (between(0, 2)) { - case 0: - key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 1: - nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 2: - after = randomValueOtherThan(after, this::createRandomTimeValue); - break; - default: - throw new AssertionError("Illegal randomisation branch"); - } - - return new PhaseAfterStep(instance.getNowSupplier(), after, key, nextKey); - } - - @Override - public PhaseAfterStep copyInstance(PhaseAfterStep instance) { - return new PhaseAfterStep(instance.getNowSupplier(), instance.getAfter(), - instance.getKey(), instance.getNextStepKey()); - } - - public void testConditionMet() { - long creationDate = randomNonNegativeLong(); - IndexMetaData indexMetadata = IndexMetaData.builder(randomAlphaOfLength(5)) - .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, creationDate)) - .creationDate(creationDate) - .numberOfShards(1).numberOfReplicas(0).build(); - MetaData metaData = MetaData.builder() - .persistentSettings(settings(Version.CURRENT).build()) - .put(IndexMetaData.builder(indexMetadata)) - .build(); - Index index = indexMetadata.getIndex(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).build(); - long after = randomNonNegativeLong(); - long now = creationDate + after + randomIntBetween(0, 2); - PhaseAfterStep step = new PhaseAfterStep(() -> now, TimeValue.timeValueMillis(after), null, null); - Result result = step.isConditionMet(index, clusterState); - assertTrue(result.isComplete()); - assertNull(result.getInfomationContext()); - } - - public void testConditionNotMet() { - long creationDate = randomNonNegativeLong(); - IndexMetaData indexMetadata = IndexMetaData.builder(randomAlphaOfLength(5)) - .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, creationDate)) - .creationDate(creationDate) - .numberOfShards(1).numberOfReplicas(0).build(); - MetaData metaData = MetaData.builder() - .persistentSettings(settings(Version.CURRENT).build()) - .put(IndexMetaData.builder(indexMetadata)) - .build(); - Index index = indexMetadata.getIndex(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).build(); - long after = randomNonNegativeLong(); - long now = creationDate + after - randomIntBetween(1, 1000); - PhaseAfterStep step = new PhaseAfterStep(() -> now, TimeValue.timeValueMillis(after), null, null); - Result result = step.isConditionMet(index, clusterState); - assertFalse(result.isComplete()); - assertNull(result.getInfomationContext()); - } -} diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkStepTests.java index c66a29cb8b3ad..f99cadede5e0c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkStepTests.java @@ -88,9 +88,9 @@ public void testPerformAction() throws Exception { .settings(settings(Version.CURRENT) .put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, creationDate) .put(LifecycleSettings.LIFECYCLE_NAME, lifecycleName) - .put(LifecycleSettings.LIFECYCLE_PHASE, step.getKey().getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, step.getKey().getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, ShrunkenIndexCheckStep.NAME) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, step.getKey().getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, step.getKey().getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, ShrunkenIndexCheckStep.NAME) ) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) .putAlias(AliasMetaData.builder("my_alias")) @@ -116,9 +116,9 @@ public Void answer(InvocationOnMock invocation) throws Throwable { .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, sourceIndexMetaData.getNumberOfReplicas()) .put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, creationDate) .put(LifecycleSettings.LIFECYCLE_NAME, lifecycleName) - .put(LifecycleSettings.LIFECYCLE_PHASE, step.getKey().getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, step.getKey().getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, ShrunkenIndexCheckStep.NAME) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, step.getKey().getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, step.getKey().getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, ShrunkenIndexCheckStep.NAME) .put(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_name", (String) null) .build())); assertThat(request.getTargetIndexRequest().settings() diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/ExecuteStepsUpdateTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/ExecuteStepsUpdateTask.java index e9d35da543467..8c5cd6b62163c 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/ExecuteStepsUpdateTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/ExecuteStepsUpdateTask.java @@ -50,7 +50,6 @@ Step getStartStep() { return startStep; } - /** * {@link Step}s for the current index and policy are executed in succession until the next step to be * executed is not a {@link ClusterStateActionStep}, or not a {@link ClusterStateWaitStep}, or does not @@ -64,6 +63,8 @@ Step getStartStep() { @Override public ClusterState execute(ClusterState currentState) throws IOException { Step currentStep = startStep; + logger.trace("[{}] executing steps update task for ({}) {}", + index.getName(), currentStep.getClass().getSimpleName(), currentStep); IndexMetaData indexMetaData = currentState.metaData().index(index); if (indexMetaData == null) { // This index doesn't exist any more, there's nothing to execute currently diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycle.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycle.java index 99b86c52e63e5..724c2f26bbbe4 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycle.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycle.java @@ -124,16 +124,20 @@ public List> getSettings() { return Arrays.asList( LifecycleSettings.LIFECYCLE_POLL_INTERVAL_SETTING, LifecycleSettings.LIFECYCLE_NAME_SETTING, - LifecycleSettings.LIFECYCLE_PHASE_SETTING, LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE_SETTING, + LifecycleSettings.LIFECYCLE_NEXT_PHASE_SETTING, + LifecycleSettings.LIFECYCLE_NEXT_ACTION_SETTING, + LifecycleSettings.LIFECYCLE_NEXT_STEP_SETTING, + LifecycleSettings.LIFECYCLE_CURRENT_PHASE_SETTING, + LifecycleSettings.LIFECYCLE_CURRENT_ACTION_SETTING, + LifecycleSettings.LIFECYCLE_CURRENT_STEP_SETTING, LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING, LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING, - LifecycleSettings.LIFECYCLE_ACTION_SETTING, LifecycleSettings.LIFECYCLE_STEP_TIME_SETTING, - LifecycleSettings.LIFECYCLE_STEP_SETTING, LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING, LifecycleSettings.LIFECYCLE_FAILED_STEP_SETTING, LifecycleSettings.LIFECYCLE_SKIP_SETTING, + LifecycleSettings.LIFECYCLE_FORCED_PHASE_SETTING, RolloverAction.LIFECYCLE_ROLLOVER_ALIAS_SETTING); } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunner.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunner.java index 379b903ab8ceb..1a86f347dc964 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunner.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunner.java @@ -16,6 +16,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -49,29 +50,75 @@ public IndexLifecycleRunner(PolicyStepsRegistry stepRegistry, ClusterService clu this.nowSupplier = nowSupplier; } + /** + * Return true or false depending on whether the index is ready to be in {@code phase} + */ + boolean isReadyToTransitionToThisPhase(final String policy, final IndexMetaData indexMetaData, final String phase) { + final Settings indexSettings = indexMetaData.getSettings(); + if (indexSettings.hasValue(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE) == false) { + logger.trace("no index creation date has been set yet"); + return true; + } + if (indexSettings.get(LifecycleSettings.LIFECYCLE_FORCED_PHASE, "_none_").equals(phase)) { + // We have been forced into this phase by a move-to-action request, + // therefore, we should skip the check for index age since the user + // specifically asked us to be here + logger.trace("skipping phase transition check for \"{}\" phase due to user initiated move-to-action", phase); + return true; + } + + final long lifecycleDate = indexSettings.getAsLong(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, -1L); + assert lifecycleDate >= 0 : "expected index to have a lifecycle date but it did not"; + + final TimeValue after = stepRegistry.getIndexAgeForPhase(policy, phase); + final long now = nowSupplier.getAsLong(); + final TimeValue age = new TimeValue(now - lifecycleDate); + if (logger.isTraceEnabled()) { + logger.trace("[{}] checking for index age to be at least [{}] before performing actions in " + + "the \"{}\" phase. Now: {}, lifecycle date: {}, age: [{}/{}s]", + indexMetaData.getIndex().getName(), after, phase, + new TimeValue(now).seconds(), + new TimeValue(lifecycleDate).seconds(), + age, age.seconds()); + } + return now >= lifecycleDate + after.getMillis(); + } + public void runPolicy(String policy, IndexMetaData indexMetaData, ClusterState currentState, boolean fromClusterStateChange) { - Settings indexSettings = indexMetaData.getSettings(); + final Settings indexSettings = indexMetaData.getSettings(); if (LifecycleSettings.LIFECYCLE_SKIP_SETTING.get(indexSettings)) { logger.info("skipping policy [" + policy + "] for index [" + indexMetaData.getIndex().getName() + "]." + LifecycleSettings.LIFECYCLE_SKIP + "== true"); return; } - Step currentStep = getCurrentStep(stepRegistry, policy, indexMetaData.getIndex(), indexSettings); + + final Step currentStep = getCurrentStep(stepRegistry, policy, indexMetaData.getIndex(), indexSettings); + if (currentStep == null) { // This may happen in the case that there is invalid ilm-step index settings or the stepRegistry is out of // sync with the current cluster state logger.warn("current step [" + getCurrentStepKey(indexSettings) + "] for index [" + indexMetaData.getIndex().getName() + "] with policy [" + policy + "] is not recognized"); return; - } - logger.debug("running policy with current-step[" + currentStep.getKey() + "]"); - if (currentStep instanceof TerminalPolicyStep) { + } else if (currentStep instanceof TerminalPolicyStep) { logger.debug("policy [" + policy + "] for index [" + indexMetaData.getIndex().getName() + "] complete, skipping execution"); + return; } else if (currentStep instanceof ErrorStep) { logger.debug( "policy [" + policy + "] for index [" + indexMetaData.getIndex().getName() + "] on an error step, skipping execution"); - } else if (currentStep instanceof ClusterStateActionStep || currentStep instanceof ClusterStateWaitStep) { + return; + } + + // Before executing this step, we need to check whether the index is + // ready to transition to whatever phase this step is in. If it's + // not, then we need to return and wait until the future when it will be + if (isReadyToTransitionToThisPhase(policy, indexMetaData, currentStep.getKey().getPhase()) == false) { + return; + } + logger.debug("running policy with current-step [" + currentStep.getKey() + "]"); + + if (currentStep instanceof ClusterStateActionStep || currentStep instanceof ClusterStateWaitStep) { executeClusterStateSteps(indexMetaData.getIndex(), policy, currentStep); } else if (currentStep instanceof AsyncWaitStep) { if (fromClusterStateChange == false) { @@ -126,7 +173,8 @@ private void runPolicy(IndexMetaData indexMetaData, ClusterState currentState) { private void executeClusterStateSteps(Index index, String policy, Step step) { assert step instanceof ClusterStateActionStep || step instanceof ClusterStateWaitStep; - clusterService.submitStateUpdateTask("ILM", new ExecuteStepsUpdateTask(policy, index, step, stepRegistry, nowSupplier)); + clusterService.submitStateUpdateTask("ILM-execute-steps", + new ExecuteStepsUpdateTask(policy, index, step, stepRegistry, nowSupplier)); } /** @@ -139,9 +187,9 @@ private void executeClusterStateSteps(Index index, String policy, Step step) { * the index settings to extract the {@link StepKey} from. */ public static StepKey getCurrentStepKey(Settings indexSettings) { - String currentPhase = LifecycleSettings.LIFECYCLE_PHASE_SETTING.get(indexSettings); - String currentAction = LifecycleSettings.LIFECYCLE_ACTION_SETTING.get(indexSettings); - String currentStep = LifecycleSettings.LIFECYCLE_STEP_SETTING.get(indexSettings); + String currentPhase = LifecycleSettings.LIFECYCLE_NEXT_PHASE_SETTING.get(indexSettings); + String currentAction = LifecycleSettings.LIFECYCLE_NEXT_ACTION_SETTING.get(indexSettings); + String currentStep = LifecycleSettings.LIFECYCLE_NEXT_STEP_SETTING.get(indexSettings); if (Strings.isNullOrEmpty(currentStep)) { assert Strings.isNullOrEmpty(currentPhase) : "Current phase is not empty: " + currentPhase; assert Strings.isNullOrEmpty(currentAction) : "Current action is not empty: " + currentAction; @@ -248,20 +296,27 @@ ClusterState moveClusterStateToFailedStep(ClusterState currentState, String[] in } private static Settings.Builder moveIndexSettingsToNextStep(Settings existingSettings, StepKey currentStep, StepKey nextStep, - LongSupplier nowSupplier) { + LongSupplier nowSupplier) { long nowAsMillis = nowSupplier.getAsLong(); - Settings.Builder newSettings = Settings.builder().put(existingSettings).put(LifecycleSettings.LIFECYCLE_PHASE, nextStep.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, nextStep.getAction()).put(LifecycleSettings.LIFECYCLE_STEP, nextStep.getName()) - .put(LifecycleSettings.LIFECYCLE_STEP_TIME, nowAsMillis) - // clear any step info or error-related settings from the current step - .put(LifecycleSettings.LIFECYCLE_FAILED_STEP, (String) null) - .put(LifecycleSettings.LIFECYCLE_STEP_INFO, (String) null); - if (currentStep.getPhase().equals(nextStep.getPhase()) == false) { - newSettings.put(LifecycleSettings.LIFECYCLE_PHASE_TIME, nowAsMillis); - } + final Settings.Builder newSettings = Settings.builder(); + final String previousPhase = existingSettings.get(LifecycleSettings.LIFECYCLE_CURRENT_PHASE); + newSettings.put(existingSettings) + .put(LifecycleSettings.LIFECYCLE_CURRENT_PHASE, currentStep.getPhase()) + .put(LifecycleSettings.LIFECYCLE_CURRENT_ACTION, currentStep.getAction()) + .put(LifecycleSettings.LIFECYCLE_CURRENT_STEP, currentStep.getName()) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, nextStep.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, nextStep.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, nextStep.getName()) + .put(LifecycleSettings.LIFECYCLE_STEP_TIME, nowAsMillis) + // clear any step info or error-related settings from the current step + .put(LifecycleSettings.LIFECYCLE_FAILED_STEP, (String) null) + .put(LifecycleSettings.LIFECYCLE_STEP_INFO, (String) null); if (currentStep.getAction().equals(nextStep.getAction()) == false) { newSettings.put(LifecycleSettings.LIFECYCLE_ACTION_TIME, nowAsMillis); } + if (previousPhase == null || previousPhase.equals(currentStep.getPhase()) == false) { + newSettings.put(LifecycleSettings.LIFECYCLE_PHASE_TIME, nowAsMillis); + } return newSettings; } @@ -304,18 +359,20 @@ static ClusterState addStepInfoToClusterState(Index index, ClusterState clusterS private void moveToStep(Index index, String policy, StepKey currentStepKey, StepKey nextStepKey) { logger.debug("moveToStep[" + policy + "] [" + index.getName() + "]" + currentStepKey + " -> " + nextStepKey); - clusterService.submitStateUpdateTask("ILM", new MoveToNextStepUpdateTask(index, policy, currentStepKey, + clusterService.submitStateUpdateTask("ILM-move-to-next-step", new MoveToNextStepUpdateTask(index, policy, currentStepKey, nextStepKey, nowSupplier, newState -> runPolicy(newState.getMetaData().index(index), newState))); } private void moveToErrorStep(Index index, String policy, StepKey currentStepKey, Exception e) { logger.error("policy [" + policy + "] for index [" + index.getName() + "] failed on step [" + currentStepKey + "]. Moving to ERROR step.", e); - clusterService.submitStateUpdateTask("ILM", new MoveToErrorStepUpdateTask(index, policy, currentStepKey, e, nowSupplier)); + clusterService.submitStateUpdateTask("ILM-move-to-error", + new MoveToErrorStepUpdateTask(index, policy, currentStepKey, e, nowSupplier)); } private void setStepInfo(Index index, String policy, StepKey currentStepKey, ToXContentObject stepInfo) { - clusterService.submitStateUpdateTask("ILM", new SetStepInfoUpdateTask(index, policy, currentStepKey, stepInfo)); + clusterService.submitStateUpdateTask("ILM-set-step-info", + new SetStepInfoUpdateTask(index, policy, currentStepKey, stepInfo)); } public static ClusterState setPolicyForIndexes(final String newPolicyName, final Index[] indices, ClusterState currentState, @@ -393,16 +450,19 @@ private static IndexMetaData.Builder removePolicyForIndex(IndexMetaData indexMet Settings.Builder newSettings = Settings.builder().put(idxSettings); newSettings.remove(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey()); - newSettings.remove(LifecycleSettings.LIFECYCLE_PHASE_SETTING.getKey()); + newSettings.remove(LifecycleSettings.LIFECYCLE_NEXT_PHASE_SETTING.getKey()); newSettings.remove(LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.getKey()); - newSettings.remove(LifecycleSettings.LIFECYCLE_ACTION_SETTING.getKey()); + newSettings.remove(LifecycleSettings.LIFECYCLE_NEXT_ACTION_SETTING.getKey()); newSettings.remove(LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.getKey()); - newSettings.remove(LifecycleSettings.LIFECYCLE_STEP_SETTING.getKey()); + newSettings.remove(LifecycleSettings.LIFECYCLE_NEXT_STEP_SETTING.getKey()); newSettings.remove(LifecycleSettings.LIFECYCLE_STEP_TIME_SETTING.getKey()); newSettings.remove(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.getKey()); newSettings.remove(LifecycleSettings.LIFECYCLE_FAILED_STEP_SETTING.getKey()); newSettings.remove(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE_SETTING.getKey()); newSettings.remove(LifecycleSettings.LIFECYCLE_SKIP_SETTING.getKey()); + newSettings.remove(LifecycleSettings.LIFECYCLE_CURRENT_PHASE_SETTING.getKey()); + newSettings.remove(LifecycleSettings.LIFECYCLE_CURRENT_STEP_SETTING.getKey()); + newSettings.remove(LifecycleSettings.LIFECYCLE_CURRENT_ACTION_SETTING.getKey()); newSettings.remove(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS_SETTING.getKey()); return IndexMetaData.builder(indexMetadata).settings(newSettings); } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleService.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleService.java index 3602a0a511c8c..cf6798a6ed597 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleService.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleService.java @@ -128,7 +128,7 @@ public void applyClusterState(ClusterChangedEvent event) { } if (event.state().metaData().custom(IndexLifecycleMetadata.TYPE) != null) { // update policy steps registry - policyRegistry.update(event.state(), client, nowSupplier); + policyRegistry.update(event.state(), client); } } } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/PolicyStepsRegistry.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/PolicyStepsRegistry.java index 2715b9ae09bc1..45993547493a7 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/PolicyStepsRegistry.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/PolicyStepsRegistry.java @@ -16,13 +16,17 @@ import org.elasticsearch.common.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.Index; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.indexlifecycle.ErrorStep; import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata; +import org.elasticsearch.xpack.core.indexlifecycle.InitializePolicyContextStep; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyMetadata; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings; +import org.elasticsearch.xpack.core.indexlifecycle.Phase; import org.elasticsearch.xpack.core.indexlifecycle.Step; +import org.elasticsearch.xpack.core.indexlifecycle.TerminalPolicyStep; import java.util.ArrayList; import java.util.HashMap; @@ -30,7 +34,6 @@ import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; -import java.util.function.LongSupplier; import java.util.stream.Collectors; public class PolicyStepsRegistry { @@ -85,7 +88,7 @@ public void removeIndices(List indices) { } @SuppressWarnings({ "unchecked", "rawtypes" }) - public void update(ClusterState clusterState, Client client, LongSupplier nowSupplier) { + public void update(ClusterState clusterState, Client client) { final IndexLifecycleMetadata meta = clusterState.metaData().custom(IndexLifecycleMetadata.TYPE); assert meta != null : "IndexLifecycleMetadata cannot be null when updating the policy steps registry"; @@ -122,7 +125,7 @@ public LifecyclePolicyMetadata read(StreamInput in, String key) { LifecyclePolicySecurityClient policyClient = new LifecyclePolicySecurityClient(client, ClientHelper.INDEX_LIFECYCLE_ORIGIN, policyMetadata.getHeaders()); lifecyclePolicyMap.put(policyMetadata.getName(), policyMetadata); - List policyAsSteps = policyMetadata.getPolicy().toSteps(policyClient, nowSupplier); + List policyAsSteps = policyMetadata.getPolicy().toSteps(policyClient); if (policyAsSteps.isEmpty() == false) { firstStepMap.put(policyMetadata.getName(), policyAsSteps.get(0)); final Map stepMapForPolicy = new HashMap<>(); @@ -146,7 +149,7 @@ public LifecyclePolicyMetadata read(StreamInput in, String key) { final String existingPhase = (currentSteps == null || currentSteps.size() == 0) ? "_none_" : currentSteps.get(0).getKey().getPhase(); // Retrieve the current phase, defaulting to "new" if no phase is set - final String currentPhase = imd.value.getSettings().get(LifecycleSettings.LIFECYCLE_PHASE, "new"); + final String currentPhase = imd.value.getSettings().get(LifecycleSettings.LIFECYCLE_NEXT_PHASE, "new"); if (existingPhase.equals(currentPhase) == false) { logger.debug("index [{}] has transitioned phases [{} -> {}], rebuilding step list", @@ -219,4 +222,23 @@ public Step getFirstStep(String policy) { return firstStepMap.get(policy); } + public TimeValue getIndexAgeForPhase(final String policy, final String phase) { + // These built in phases should never wait + if (InitializePolicyContextStep.INITIALIZATION_PHASE.equals(phase) || TerminalPolicyStep.TERMINAL_PHASE.equals(phase)) { + return TimeValue.ZERO; + } + + final LifecyclePolicyMetadata meta = lifecyclePolicyMap.get(policy); + if (meta == null) { + throw new IllegalArgumentException("no policy found with name \"" + policy + "\""); + } else { + final Phase retrievedPhase = meta.getPolicy().getPhases().get(phase); + if (retrievedPhase == null) { + // We don't have that phase registered, proceed right through it + return TimeValue.ZERO; + } else { + return retrievedPhase.getAfter(); + } + } + } } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportExplainLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportExplainLifecycleAction.java index 310d18ab1cef0..622691cf22a7c 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportExplainLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportExplainLifecycleAction.java @@ -70,9 +70,9 @@ protected void doMasterOperation(ExplainLifecycleRequest request, String[] concr indexResponse = IndexLifecycleExplainResponse.newManagedIndexResponse(index, policyName, LifecycleSettings.LIFECYCLE_SKIP_SETTING.get(idxSettings), LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE_SETTING.get(idxSettings), - LifecycleSettings.LIFECYCLE_PHASE_SETTING.get(idxSettings), - LifecycleSettings.LIFECYCLE_ACTION_SETTING.get(idxSettings), - LifecycleSettings.LIFECYCLE_STEP_SETTING.get(idxSettings), + LifecycleSettings.LIFECYCLE_NEXT_PHASE_SETTING.get(idxSettings), + LifecycleSettings.LIFECYCLE_NEXT_ACTION_SETTING.get(idxSettings), + LifecycleSettings.LIFECYCLE_NEXT_STEP_SETTING.get(idxSettings), LifecycleSettings.LIFECYCLE_FAILED_STEP_SETTING.get(idxSettings), LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(idxSettings), LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.get(idxSettings), diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportMoveToStepAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportMoveToStepAction.java index 1c9a05dbfc73e..40a59af0c11b9 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportMoveToStepAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportMoveToStepAction.java @@ -15,11 +15,13 @@ import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; +import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings; import org.elasticsearch.xpack.core.indexlifecycle.action.MoveToStepAction; import org.elasticsearch.xpack.core.indexlifecycle.action.MoveToStepAction.Request; import org.elasticsearch.xpack.core.indexlifecycle.action.MoveToStepAction.Response; @@ -58,8 +60,21 @@ protected void masterOperation(Request request, ClusterState state, ActionListen new AckedClusterStateUpdateTask(request, listener) { @Override public ClusterState execute(ClusterState currentState) { - return indexLifecycleService.moveClusterStateToStep(currentState, request.getIndex(), request.getCurrentStepKey(), - request.getNextStepKey()); + final ClusterState movedState = indexLifecycleService.moveClusterStateToStep(currentState, request.getIndex(), + request.getCurrentStepKey(), request.getNextStepKey()); + final IndexMetaData indexMeta = movedState.metaData().index(request.getIndex()); + final IndexMetaData newIndexMeta = IndexMetaData.builder(indexMeta) + .settings(Settings.builder() + .put(indexMeta.getSettings()) + // Indicate that we have forced the index into this phase, therefore it + // should not have to wait until the phase's "after" time + .put(LifecycleSettings.LIFECYCLE_FORCED_PHASE, request.getNextStepKey().getPhase())) + .build(); + final ClusterState finalState = ClusterState.builder(movedState) + .metaData(MetaData.builder(movedState.metaData()) + .put(newIndexMeta, true)) + .build(); + return finalState; } @Override diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/ExecuteStepsUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/ExecuteStepsUpdateTaskTests.java index 70bb5cdec9aa1..e6b95cd6dc4ee 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/ExecuteStepsUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/ExecuteStepsUpdateTaskTests.java @@ -20,7 +20,6 @@ import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.Index; import org.elasticsearch.node.Node; -import org.elasticsearch.xpack.core.indexlifecycle.OperationMode; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; @@ -28,6 +27,7 @@ import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings; import org.elasticsearch.xpack.core.indexlifecycle.MockAction; import org.elasticsearch.xpack.core.indexlifecycle.MockStep; +import org.elasticsearch.xpack.core.indexlifecycle.OperationMode; import org.elasticsearch.xpack.core.indexlifecycle.Phase; import org.elasticsearch.xpack.core.indexlifecycle.Step; import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey; @@ -80,11 +80,11 @@ public void prepareState() { mixedPolicyName = randomAlphaOfLengthBetween(5, 10); allClusterPolicyName = randomAlphaOfLengthBetween(1, 4); invalidPolicyName = randomAlphaOfLength(11); - Phase mixedPhase = new Phase("first_phase", TimeValue.ZERO, Collections.singletonMap(MockAction.NAME, + Phase mixedPhase = new Phase("phase_1", TimeValue.ZERO, Collections.singletonMap(MockAction.NAME, new MockAction(Arrays.asList(firstStep, secondStep, thirdStep)))); - Phase allClusterPhase = new Phase("first_phase", TimeValue.ZERO, Collections.singletonMap(MockAction.NAME, + Phase allClusterPhase = new Phase("phase_1", TimeValue.ZERO, Collections.singletonMap(MockAction.NAME, new MockAction(Arrays.asList(firstStep, allClusterSecondStep)))); - Phase invalidPhase = new Phase("invalid_phase", TimeValue.ZERO, Collections.singletonMap(MockAction.NAME, + Phase invalidPhase = new Phase("invalid", TimeValue.ZERO, Collections.singletonMap(MockAction.NAME, new MockAction(Arrays.asList(new MockClusterStateActionStep(firstStepKey, invalidStepKey))))); LifecyclePolicy mixedPolicy = newTestLifecyclePolicy(mixedPolicyName, Collections.singletonMap(mixedPhase.getName(), mixedPhase)); @@ -104,13 +104,14 @@ public void prepareState() { } private void setupIndexPolicy(String policyName) { - // Reset the index to use the "allClusterPolicyName" + // Reset the index to use the provided policy IndexMetaData indexMetadata = IndexMetaData.builder(indexName) .settings(settings(Version.CURRENT) .put(LifecycleSettings.LIFECYCLE_NAME, policyName) - .put(LifecycleSettings.LIFECYCLE_PHASE, "new") - .put(LifecycleSettings.LIFECYCLE_ACTION, "init") - .put(LifecycleSettings.LIFECYCLE_STEP, "init")) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, "new") + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, "init") + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, "init") + .put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, 0L)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); index = indexMetadata.getIndex(); MetaData metaData = MetaData.builder() @@ -126,21 +127,20 @@ private void setupIndexPolicy(String policyName) { .metaData(metaData) .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) .build(); - policyStepsRegistry.update(clusterState, client, () -> 0L); + policyStepsRegistry.update(clusterState, client); } public void testExecuteAllUntilEndOfPhase() throws IOException { setupIndexPolicy(allClusterPolicyName); Step startStep = policyStepsRegistry.getFirstStep(allClusterPolicyName); - Step afterStep = policyStepsRegistry.getStep(index, startStep.getNextStepKey()); long now = randomNonNegativeLong(); // test execute start till end of phase `new` ExecuteStepsUpdateTask task = new ExecuteStepsUpdateTask(allClusterPolicyName, index, startStep, policyStepsRegistry, () -> now); ClusterState newState = task.execute(clusterState); // Update the registry so the next phase's steps are loaded - policyStepsRegistry.update(newState, client, () -> now); + policyStepsRegistry.update(newState, client); // verify that both the `new` phase was executed and the next phase is to begin StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(newState.metaData().index(index).getSettings()); @@ -148,7 +148,7 @@ public void testExecuteAllUntilEndOfPhase() throws IOException { // test execute all actions in same phase task = new ExecuteStepsUpdateTask(allClusterPolicyName, index, firstStep, policyStepsRegistry, () -> now); newState = task.execute(newState); - policyStepsRegistry.update(newState, client, () -> now); + policyStepsRegistry.update(newState, client); assertThat(firstStep.getExecuteCount(), equalTo(1L)); assertThat(allClusterSecondStep.getExecuteCount(), equalTo(1L)); @@ -178,7 +178,7 @@ public void testExecuteUntilFirstNonClusterStateStep() throws IOException { assertThat(currentStepKey, equalTo(thirdStepKey)); assertThat(firstStep.getExecuteCount(), equalTo(0L)); assertThat(secondStep.getExecuteCount(), equalTo(1L)); - assertThat(LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(newState.metaData().index(index).getSettings()), equalTo(-1L)); + assertThat(LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(newState.metaData().index(index).getSettings()), equalTo(now)); assertThat(LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.get(newState.metaData().index(index).getSettings()), equalTo(-1L)); assertThat(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.get(newState.metaData().index(index).getSettings()), equalTo("")); } @@ -189,10 +189,10 @@ public void testExecuteInvalidStartStep() throws IOException { .metaData(MetaData.builder(clusterState.metaData()) .put(IndexMetaData.builder(clusterState.metaData().index(indexName)) .settings(Settings.builder().put(clusterState.metaData().index(indexName).getSettings()) - .put(LifecycleSettings.LIFECYCLE_PHASE, (String) null) - .put(LifecycleSettings.LIFECYCLE_ACTION, (String) null) - .put(LifecycleSettings.LIFECYCLE_STEP, (String) null).build()))).build(); - policyStepsRegistry.update(clusterState, client, () -> 0); + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, (String) null) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, (String) null) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, (String) null).build()))).build(); + policyStepsRegistry.update(clusterState, client); Step invalidStep = new MockClusterStateActionStep(firstStepKey, secondStepKey); long now = randomNonNegativeLong(); @@ -254,9 +254,9 @@ private void setStateToKey(StepKey stepKey) { .metaData(MetaData.builder(clusterState.metaData()) .put(IndexMetaData.builder(clusterState.metaData().index(indexName)) .settings(Settings.builder().put(clusterState.metaData().index(indexName).getSettings()) - .put(LifecycleSettings.LIFECYCLE_PHASE, stepKey.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, stepKey.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, stepKey.getName()).build()))).build(); - policyStepsRegistry.update(clusterState, client, () -> 0); + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, stepKey.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, stepKey.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, stepKey.getName()).build()))).build(); + policyStepsRegistry.update(clusterState, client); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationIT.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationIT.java index 74b9e27ada394..727cb8424bf94 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationIT.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationIT.java @@ -138,7 +138,7 @@ public void testSingleNodeCluster() throws Exception { }); assertBusy(() -> { GetSettingsResponse settingsResponse = client().admin().indices().prepareGetSettings("test").get(); - String step = settingsResponse.getSetting("test", "index.lifecycle.step"); + String step = settingsResponse.getSetting("test", LifecycleSettings.LIFECYCLE_NEXT_STEP); assertThat(step, equalTo(TerminalPolicyStep.KEY.getName())); }); } @@ -171,7 +171,7 @@ public void testMasterDedicatedDataDedicated() throws Exception { }); assertBusy(() -> { GetSettingsResponse settingsResponse = client().admin().indices().prepareGetSettings("test").get(); - String step = settingsResponse.getSetting("test", "index.lifecycle.step"); + String step = settingsResponse.getSetting("test", LifecycleSettings.LIFECYCLE_NEXT_STEP); assertThat(step, equalTo(TerminalPolicyStep.KEY.getName())); }); } @@ -211,7 +211,7 @@ public void testMasterFailover() throws Exception { // check step in progress in lifecycle assertBusy(() -> { GetSettingsResponse settingsResponse = client().admin().indices().prepareGetSettings("test").get(); - String step = settingsResponse.getSetting("test", "index.lifecycle.step"); + String step = settingsResponse.getSetting("test", LifecycleSettings.LIFECYCLE_NEXT_STEP); assertThat(step, equalTo(ObservableClusterStateWaitStep.NAME)); }); @@ -223,7 +223,7 @@ public void testMasterFailover() throws Exception { // check that index lifecycle picked back up where it assertBusy(() -> { GetSettingsResponse settingsResponse = client().admin().indices().prepareGetSettings("test").get(); - String step = settingsResponse.getSetting("test", "index.lifecycle.step"); + String step = settingsResponse.getSetting("test", LifecycleSettings.LIFECYCLE_NEXT_STEP); assertThat(step, equalTo(ObservableClusterStateWaitStep.NAME)); }); @@ -233,7 +233,7 @@ public void testMasterFailover() throws Exception { assertBusy(() -> { GetSettingsResponse settingsResponse = client().admin().indices().prepareGetSettings("test").get(); - String step = settingsResponse.getSetting("test", "index.lifecycle.step"); + String step = settingsResponse.getSetting("test", LifecycleSettings.LIFECYCLE_NEXT_STEP); assertThat(step, equalTo(TerminalPolicyStep.KEY.getName())); }); } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java index a9cc9eb3e2ffc..142cd1e67acc2 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java @@ -52,6 +52,7 @@ import java.util.Map; import java.util.Objects; import java.util.SortedMap; +import java.util.TreeMap; import java.util.function.Function; import java.util.stream.Collectors; @@ -103,9 +104,9 @@ public void testRunPolicyErrorStep() { ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_PHASE, stepKey.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, stepKey.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, ErrorStep.NAME)) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, stepKey.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, stepKey.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, ErrorStep.NAME)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); runner.runPolicy(policyName, indexMetaData, null, false); @@ -125,7 +126,7 @@ public void testRunPolicyClusterStateActionStep() { runner.runPolicy(policyName, indexMetaData, null, randomBoolean()); - Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM"), + Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM-execute-steps"), Mockito.argThat(new ExecuteStepsUpdateTaskMatcher(indexMetaData.getIndex(), policyName, step))); Mockito.verifyNoMoreInteractions(clusterService); } @@ -143,7 +144,7 @@ public void testRunPolicyClusterStateWaitStep() { runner.runPolicy(policyName, indexMetaData, null, randomBoolean()); - Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM"), + Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM-execute-steps"), Mockito.argThat(new ExecuteStepsUpdateTaskMatcher(indexMetaData.getIndex(), policyName, step))); Mockito.verifyNoMoreInteractions(clusterService); } @@ -162,7 +163,7 @@ public void testRunPolicyAsyncActionStepCompletes() { runner.runPolicy(policyName, indexMetaData, null, false); assertEquals(1, step.getExecuteCount()); - Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM"), + Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM-move-to-next-step"), Mockito.argThat(new MoveToNextStepUpdateTaskMatcher(indexMetaData.getIndex(), policyName, stepKey, null))); Mockito.verifyNoMoreInteractions(clusterService); } @@ -217,7 +218,7 @@ public void testRunPolicyAsyncActionStepFails() { runner.runPolicy(policyName, indexMetaData, null, false); assertEquals(1, step.getExecuteCount()); - Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM"), + Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM-move-to-error"), Mockito.argThat(new MoveToErrorStepUpdateTaskMatcher(indexMetaData.getIndex(), policyName, stepKey, expectedException))); Mockito.verifyNoMoreInteractions(clusterService); } @@ -254,7 +255,7 @@ public void testRunPolicyAsyncWaitStepCompletes() { runner.runPolicy(policyName, indexMetaData, null, false); assertEquals(1, step.getExecuteCount()); - Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM"), + Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM-move-to-next-step"), Mockito.argThat(new MoveToNextStepUpdateTaskMatcher(indexMetaData.getIndex(), policyName, stepKey, null))); Mockito.verifyNoMoreInteractions(clusterService); } @@ -275,7 +276,7 @@ public void testRunPolicyAsyncWaitStepNotComplete() { runner.runPolicy(policyName, indexMetaData, null, false); assertEquals(1, step.getExecuteCount()); - Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM"), + Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM-set-step-info"), Mockito.argThat(new SetStepInfoUpdateTaskMatcher(indexMetaData.getIndex(), policyName, stepKey, stepInfo))); Mockito.verifyNoMoreInteractions(clusterService); } @@ -314,7 +315,7 @@ public void testRunPolicyAsyncWaitStepFails() { runner.runPolicy(policyName, indexMetaData, null, false); assertEquals(1, step.getExecuteCount()); - Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM"), + Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask(Mockito.matches("ILM-move-to-error"), Mockito.argThat(new MoveToErrorStepUpdateTaskMatcher(indexMetaData.getIndex(), policyName, stepKey, expectedException))); Mockito.verifyNoMoreInteractions(clusterService); } @@ -374,9 +375,9 @@ public void testGetCurrentStepKey() { String action = randomAlphaOfLength(20); String step = randomAlphaOfLength(20); Settings indexSettings2 = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, phase) - .put(LifecycleSettings.LIFECYCLE_ACTION, action) - .put(LifecycleSettings.LIFECYCLE_STEP, step) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, phase) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, action) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, step) .build(); stepKey = IndexLifecycleRunner.getCurrentStepKey(indexSettings2); assertNotNull(stepKey); @@ -388,9 +389,9 @@ public void testGetCurrentStepKey() { action = randomAlphaOfLength(20); step = randomBoolean() ? null : ""; Settings indexSettings3 = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, phase) - .put(LifecycleSettings.LIFECYCLE_ACTION, action) - .put(LifecycleSettings.LIFECYCLE_STEP, step) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, phase) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, action) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, step) .build(); AssertionError error3 = expectThrows(AssertionError.class, () -> IndexLifecycleRunner.getCurrentStepKey(indexSettings3)); assertEquals("Current phase is not empty: " + phase, error3.getMessage()); @@ -399,9 +400,9 @@ public void testGetCurrentStepKey() { action = randomAlphaOfLength(20); step = randomBoolean() ? null : ""; Settings indexSettings4 = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, phase) - .put(LifecycleSettings.LIFECYCLE_ACTION, action) - .put(LifecycleSettings.LIFECYCLE_STEP, step) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, phase) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, action) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, step) .build(); AssertionError error4 = expectThrows(AssertionError.class, () -> IndexLifecycleRunner.getCurrentStepKey(indexSettings4)); assertEquals("Current action is not empty: " + action, error4.getMessage()); @@ -410,9 +411,9 @@ public void testGetCurrentStepKey() { action = randomAlphaOfLength(20); step = randomAlphaOfLength(20); Settings indexSettings5 = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, phase) - .put(LifecycleSettings.LIFECYCLE_ACTION, action) - .put(LifecycleSettings.LIFECYCLE_STEP, step) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, phase) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, action) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, step) .build(); AssertionError error5 = expectThrows(AssertionError.class, () -> IndexLifecycleRunner.getCurrentStepKey(indexSettings5)); assertEquals(null, error5.getMessage()); @@ -421,9 +422,9 @@ public void testGetCurrentStepKey() { action = randomBoolean() ? null : ""; step = randomAlphaOfLength(20); Settings indexSettings6 = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, phase) - .put(LifecycleSettings.LIFECYCLE_ACTION, action) - .put(LifecycleSettings.LIFECYCLE_STEP, step) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, phase) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, action) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, step) .build(); AssertionError error6 = expectThrows(AssertionError.class, () -> IndexLifecycleRunner.getCurrentStepKey(indexSettings6)); assertEquals(null, error6.getMessage()); @@ -473,25 +474,25 @@ public void testGetCurrentStep() { assertSame(firstStep, actualStep); indexSettings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, "phase_1") - .put(LifecycleSettings.LIFECYCLE_ACTION, "action_1") - .put(LifecycleSettings.LIFECYCLE_STEP, "step_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, "phase_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, "action_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, "step_1") .build(); actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, indexSettings); assertSame(firstStep, actualStep); indexSettings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, "phase_1") - .put(LifecycleSettings.LIFECYCLE_ACTION, "action_1") - .put(LifecycleSettings.LIFECYCLE_STEP, "step_2") + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, "phase_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, "action_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, "step_2") .build(); actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, indexSettings); assertSame(secondStep, actualStep); indexSettings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, "phase_1") - .put(LifecycleSettings.LIFECYCLE_ACTION, "action_2") - .put(LifecycleSettings.LIFECYCLE_STEP, "step_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, "phase_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, "action_2") + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, "step_1") .build(); actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, indexSettings); assertSame(thirdStep, actualStep); @@ -503,17 +504,17 @@ public void testGetCurrentStep() { registry = new PolicyStepsRegistry(lifecyclePolicyMap, firstStepMap, stepMap, indexSteps); indexSettings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, "phase_2") - .put(LifecycleSettings.LIFECYCLE_ACTION, "action_1") - .put(LifecycleSettings.LIFECYCLE_STEP, "step_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, "phase_2") + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, "action_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, "step_1") .build(); actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, indexSettings); assertSame(fourthStep, actualStep); indexSettings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, "phase_2") - .put(LifecycleSettings.LIFECYCLE_ACTION, "action_1") - .put(LifecycleSettings.LIFECYCLE_STEP, "step_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, "phase_2") + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, "action_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, "step_1") .build(); actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, indexSettings); assertSame(fourthStep, actualStep); @@ -524,25 +525,25 @@ public void testGetCurrentStep() { registry = new PolicyStepsRegistry(lifecyclePolicyMap, firstStepMap, stepMap, indexSteps); indexSettings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, "phase_1") - .put(LifecycleSettings.LIFECYCLE_ACTION, "action_1") - .put(LifecycleSettings.LIFECYCLE_STEP, "step_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, "phase_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, "action_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, "step_1") .build(); actualStep = IndexLifecycleRunner.getCurrentStep(registry, otherPolicyName, index, indexSettings); assertEquals(otherPolicyFirstStep, actualStep); indexSettings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, "phase_1") - .put(LifecycleSettings.LIFECYCLE_ACTION, "action_1") - .put(LifecycleSettings.LIFECYCLE_STEP, "step_2") + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, "phase_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, "action_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, "step_2") .build(); actualStep = IndexLifecycleRunner.getCurrentStep(registry, otherPolicyName, index, indexSettings); assertEquals(otherPolicySecondStep, actualStep); Settings invalidIndexSettings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, "phase_1") - .put(LifecycleSettings.LIFECYCLE_ACTION, "action_1") - .put(LifecycleSettings.LIFECYCLE_STEP, "step_3") + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, "phase_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, "action_1") + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, "step_3") .build(); assertNull(IndexLifecycleRunner.getCurrentStep(registry, policyName, index, invalidIndexSettings)); assertNull(IndexLifecycleRunner.getCurrentStep(registry, "policy_does_not_exist", new Index("test","bad"), invalidIndexSettings)); @@ -560,9 +561,9 @@ public void testMoveClusterStateToNextStep() { () -> now); assertClusterStateOnNextStep(clusterState, index, currentStep, nextStep, newClusterState, now); - Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()); + Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, currentStep.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, currentStep.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, currentStep.getName()); if (randomBoolean()) { indexSettingsBuilder.put(LifecycleSettings.LIFECYCLE_STEP_INFO, randomAlphaOfLength(20)); } @@ -573,6 +574,20 @@ public void testMoveClusterStateToNextStep() { assertClusterStateOnNextStep(clusterState, index, currentStep, nextStep, newClusterState, now); } + private static ClusterState updatePreviousPhase(ClusterState clusterState, String index, String previousPhase) { + IndexMetaData indexMeta = clusterState.metaData().index(index); + ClusterState newState = ClusterState.builder(clusterState) + .metaData(MetaData.builder(clusterState.metaData()) + .put(IndexMetaData.builder(clusterState.metaData().index(index)) + .settings(Settings.builder() + .put(indexMeta.getSettings()) + .put(LifecycleSettings.LIFECYCLE_CURRENT_PHASE, previousPhase)) + .build(), true) + .build()) + .build(); + return newState; + } + public void testMoveClusterStateToNextStepSamePhase() { String indexName = "my_index"; StepKey currentStep = new StepKey("current_phase", "current_action", "current_step"); @@ -580,19 +595,21 @@ public void testMoveClusterStateToNextStepSamePhase() { long now = randomNonNegativeLong(); ClusterState clusterState = buildClusterState(indexName, Settings.builder(), Collections.emptyList()); + clusterState = updatePreviousPhase(clusterState, indexName, "current_phase"); Index index = clusterState.metaData().index(indexName).getIndex(); ClusterState newClusterState = IndexLifecycleRunner.moveClusterStateToNextStep(index, clusterState, currentStep, nextStep, () -> now); assertClusterStateOnNextStep(clusterState, index, currentStep, nextStep, newClusterState, now); - Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()); + Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, currentStep.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, currentStep.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, currentStep.getName()); if (randomBoolean()) { indexSettingsBuilder.put(LifecycleSettings.LIFECYCLE_STEP_INFO, randomAlphaOfLength(20)); } clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); + clusterState = updatePreviousPhase(clusterState, indexName, "current_phase"); index = clusterState.metaData().index(indexName).getIndex(); newClusterState = IndexLifecycleRunner.moveClusterStateToNextStep(index, clusterState, currentStep, nextStep, () -> now); assertClusterStateOnNextStep(clusterState, index, currentStep, nextStep, newClusterState, now); @@ -605,19 +622,21 @@ public void testMoveClusterStateToNextStepSameAction() { long now = randomNonNegativeLong(); ClusterState clusterState = buildClusterState(indexName, Settings.builder(), Collections.emptyList()); + clusterState = updatePreviousPhase(clusterState, indexName, "current_phase"); Index index = clusterState.metaData().index(indexName).getIndex(); ClusterState newClusterState = IndexLifecycleRunner.moveClusterStateToNextStep(index, clusterState, currentStep, nextStep, () -> now); assertClusterStateOnNextStep(clusterState, index, currentStep, nextStep, newClusterState, now); - Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()); + Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, currentStep.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, currentStep.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, currentStep.getName()); if (randomBoolean()) { indexSettingsBuilder.put(LifecycleSettings.LIFECYCLE_STEP_INFO, randomAlphaOfLength(20)); } clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); + clusterState = updatePreviousPhase(clusterState, indexName, "current_phase"); index = clusterState.metaData().index(indexName).getIndex(); newClusterState = IndexLifecycleRunner.moveClusterStateToNextStep(index, clusterState, currentStep, nextStep, () -> now); assertClusterStateOnNextStep(clusterState, index, currentStep, nextStep, newClusterState, now); @@ -633,9 +652,9 @@ public void testSuccessfulValidatedMoveClusterStateToNextStep() { PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step, indexName); Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyName) - .put(LifecycleSettings.LIFECYCLE_PHASE, currentStepKey.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStepKey.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStepKey.getName()); + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, currentStepKey.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, currentStepKey.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, currentStepKey.getName()); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); Index index = clusterState.metaData().index(indexName).getIndex(); ClusterState newClusterState = IndexLifecycleRunner.moveClusterStateToStep(indexName, clusterState, currentStepKey, @@ -653,9 +672,9 @@ public void testValidatedMoveClusterStateToNextStepWithoutPolicy() { PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyName) - .put(LifecycleSettings.LIFECYCLE_PHASE, currentStepKey.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStepKey.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStepKey.getName()); + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, currentStepKey.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, currentStepKey.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, currentStepKey.getName()); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> IndexLifecycleRunner.moveClusterStateToStep(indexName, clusterState, currentStepKey, @@ -674,9 +693,9 @@ public void testValidatedMoveClusterStateToNextStepInvalidCurrentStep() { PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyName) - .put(LifecycleSettings.LIFECYCLE_PHASE, currentStepKey.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStepKey.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStepKey.getName()); + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, currentStepKey.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, currentStepKey.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, currentStepKey.getName()); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> IndexLifecycleRunner.moveClusterStateToStep(indexName, clusterState, notCurrentStepKey, @@ -695,9 +714,9 @@ public void testValidatedMoveClusterStateToNextStepInvalidNextStep() { PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyName) - .put(LifecycleSettings.LIFECYCLE_PHASE, currentStepKey.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStepKey.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStepKey.getName()); + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, currentStepKey.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, currentStepKey.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, currentStepKey.getName()); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> IndexLifecycleRunner.moveClusterStateToStep(indexName, clusterState, currentStepKey, @@ -714,9 +733,10 @@ public void testMoveClusterStateToErrorStep() throws IOException { Exception cause = new ElasticsearchException("THIS IS AN EXPECTED CAUSE"); ClusterState clusterState = buildClusterState(indexName, - Settings.builder().put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()), + Settings.builder().put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, currentStep.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, currentStep.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, currentStep.getName()) + .put(LifecycleSettings.LIFECYCLE_CURRENT_PHASE, currentStep.getPhase()), Collections.emptyList()); Index index = clusterState.metaData().index(indexName).getIndex(); @@ -741,11 +761,12 @@ public void testMoveClusterStateToFailedStep() { PolicyStepsRegistry policyRegistry = createOneStepPolicyStepRegistry(policyName, step, indexName); Settings.Builder indexSettingsBuilder = Settings.builder() .put(LifecycleSettings.LIFECYCLE_NAME, policyName) - .put(LifecycleSettings.LIFECYCLE_PHASE, errorStepKey.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, errorStepKey.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, errorStepKey.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, errorStepKey.getAction()) .put(LifecycleSettings.LIFECYCLE_FAILED_STEP, failedStepKey.getName()) - .put(LifecycleSettings.LIFECYCLE_STEP, errorStepKey.getName()); + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, errorStepKey.getName()); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); + clusterState = updatePreviousPhase(clusterState, indexName, "current_phase"); Index index = clusterState.metaData().index(indexName).getIndex(); IndexLifecycleRunner runner = new IndexLifecycleRunner(policyRegistry, null, () -> now); ClusterState nextClusterState = runner.moveClusterStateToFailedStep(clusterState, indices); @@ -774,10 +795,10 @@ public void testMoveClusterStateToFailedStepInvalidPolicySetting() { PolicyStepsRegistry policyRegistry = createOneStepPolicyStepRegistry(policyName, step); Settings.Builder indexSettingsBuilder = Settings.builder() .put(LifecycleSettings.LIFECYCLE_NAME, (String) null) - .put(LifecycleSettings.LIFECYCLE_PHASE, errorStepKey.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, errorStepKey.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, errorStepKey.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, errorStepKey.getAction()) .put(LifecycleSettings.LIFECYCLE_FAILED_STEP, failedStepKey.getName()) - .put(LifecycleSettings.LIFECYCLE_STEP, errorStepKey.getName()); + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, errorStepKey.getName()); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); IndexLifecycleRunner runner = new IndexLifecycleRunner(policyRegistry, null, () -> now); IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, @@ -795,9 +816,9 @@ public void testMoveClusterStateToFailedNotOnError() { PolicyStepsRegistry policyRegistry = createOneStepPolicyStepRegistry(policyName, step); Settings.Builder indexSettingsBuilder = Settings.builder() .put(LifecycleSettings.LIFECYCLE_NAME, (String) null) - .put(LifecycleSettings.LIFECYCLE_PHASE, failedStepKey.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, failedStepKey.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, failedStepKey.getName()); + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, failedStepKey.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, failedStepKey.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, failedStepKey.getName()); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); IndexLifecycleRunner runner = new IndexLifecycleRunner(policyRegistry, null, () -> now); IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, @@ -812,9 +833,9 @@ public void testAddStepInfoToClusterState() throws IOException { RandomStepInfo stepInfo = new RandomStepInfo(() -> randomAlphaOfLength(10)); ClusterState clusterState = buildClusterState(indexName, - Settings.builder().put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()), + Settings.builder().put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, currentStep.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, currentStep.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, currentStep.getName()), Collections.emptyList()); Index index = clusterState.metaData().index(indexName).getIndex(); ClusterState newClusterState = IndexLifecycleRunner.addStepInfoToClusterState(index, clusterState, stepInfo); @@ -829,9 +850,9 @@ public void testSkipped() { String index = randomAlphaOfLength(10); ClusterState clusterState = buildClusterState(index, Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy) - .put(LifecycleSettings.LIFECYCLE_PHASE, randomAlphaOfLength(5)) - .put(LifecycleSettings.LIFECYCLE_ACTION, randomAlphaOfLength(5)) - .put(LifecycleSettings.LIFECYCLE_STEP, randomAlphaOfLength(5)) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, randomAlphaOfLength(5)) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, randomAlphaOfLength(5)) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, randomAlphaOfLength(5)) .put(LifecycleSettings.LIFECYCLE_SKIP, true), Collections.emptyList()); Step step = mock(randomFrom(TerminalPolicyStep.class, ClusterStateActionStep.class, @@ -870,9 +891,9 @@ public void testSetPolicyForIndex() { new StepKey(phaseName, MockAction.NAME, randomAlphaOfLength(9)), null); LifecyclePolicy oldPolicy = createPolicy(oldPolicyName, currentStep, null); Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, oldPolicyName) - .put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, currentStep.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, currentStep.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); List policyMetadatas = new ArrayList<>(); policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); @@ -915,9 +936,9 @@ public void testSetPolicyForIndexIndexDoesntExist() { LifecyclePolicy newPolicy = newTestLifecyclePolicy(newPolicyName, Collections.emptyMap()); StepKey currentStep = AbstractStepTestCase.randomStepKey(); Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, oldPolicyName) - .put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, currentStep.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, currentStep.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); List policyMetadatas = new ArrayList<>(); policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); @@ -964,9 +985,9 @@ public void testRemovePolicyForIndex() { StepKey currentStep = new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)); LifecyclePolicy oldPolicy = createPolicy(oldPolicyName, currentStep, null); Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, oldPolicyName) - .put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, currentStep.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, currentStep.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); List policyMetadatas = new ArrayList<>(); policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); @@ -1000,9 +1021,9 @@ public void testRemovePolicyForIndexIndexDoesntExist() { LifecyclePolicy oldPolicy = newTestLifecyclePolicy(oldPolicyName, Collections.emptyMap()); StepKey currentStep = AbstractStepTestCase.randomStepKey(); Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, oldPolicyName) - .put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, currentStep.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, currentStep.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); List policyMetadatas = new ArrayList<>(); policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); @@ -1023,9 +1044,9 @@ public void testRemovePolicyForIndexIndexInUnsafe() { StepKey currentStep = new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)); LifecyclePolicy oldPolicy = createPolicy(oldPolicyName, null, currentStep); Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, oldPolicyName) - .put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, currentStep.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, currentStep.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); List policyMetadatas = new ArrayList<>(); policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); @@ -1039,6 +1060,54 @@ public void testRemovePolicyForIndexIndexInUnsafe() { assertIndexNotManagedByILM(newClusterState, index); } + public void testIsReadyToTransition() { + String policyName = "async_action_policy"; + StepKey stepKey = new StepKey("phase", MockAction.NAME, MockAction.NAME); + MockAsyncActionStep step = new MockAsyncActionStep(stepKey, null); + step.setWillComplete(true); + + SortedMap lifecyclePolicyMap = new TreeMap<>(Collections.singletonMap(policyName, + new LifecyclePolicyMetadata(createPolicy(policyName, null, step.getKey()), new HashMap<>()))); + Index index = new Index("my_index", "uuid"); + Map firstStepMap = Collections.singletonMap(policyName, step); + Map policySteps = Collections.singletonMap(step.getKey(), step); + Map> stepMap = Collections.singletonMap(policyName, policySteps); + Map> indexSteps = Collections.singletonMap(index, Collections.singletonList(step)); + PolicyStepsRegistry policyStepsRegistry = new PolicyStepsRegistry(lifecyclePolicyMap, firstStepMap, stepMap, indexSteps); + ClusterService clusterService = mock(ClusterService.class); + long now = 5; + IndexLifecycleRunner runner = new IndexLifecycleRunner(policyStepsRegistry, clusterService, () -> now); + + IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); + + // With no time, always transition + assertTrue("index should be able to transition with no creation date", + runner.isReadyToTransitionToThisPhase(policyName, indexMetaData, "phase")); + + indexMetaData = IndexMetaData.builder(indexMetaData) + .settings(Settings.builder() + .put(indexMetaData.getSettings()) + .put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, 10L) + .build()) + .build(); + // Index is not old enough to transition + assertFalse("index is not able to transition if it isn't old enough", + runner.isReadyToTransitionToThisPhase(policyName, indexMetaData, "phase")); + + indexMetaData = IndexMetaData.builder(indexMetaData) + .settings(Settings.builder() + .put(indexMetaData.getSettings()) + .put(LifecycleSettings.LIFECYCLE_FORCED_PHASE, "phase") + .build()) + .build(); + // It was forced into the phase, so transition + assertTrue("index should be able to transition if forced into the phase", + runner.isReadyToTransitionToThisPhase(policyName, indexMetaData, "phase")); + } + public static void assertIndexNotManagedByILM(ClusterState clusterState, Index index) { MetaData metadata = clusterState.metaData(); assertNotNull(metadata); @@ -1047,11 +1116,11 @@ public static void assertIndexNotManagedByILM(ClusterState clusterState, Index i Settings indexSettings = indexMetadata.getSettings(); assertNotNull(indexSettings); assertFalse(LifecycleSettings.LIFECYCLE_NAME_SETTING.exists(indexSettings)); - assertFalse(LifecycleSettings.LIFECYCLE_PHASE_SETTING.exists(indexSettings)); + assertFalse(LifecycleSettings.LIFECYCLE_NEXT_PHASE_SETTING.exists(indexSettings)); assertFalse(LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.exists(indexSettings)); - assertFalse(LifecycleSettings.LIFECYCLE_ACTION_SETTING.exists(indexSettings)); + assertFalse(LifecycleSettings.LIFECYCLE_NEXT_ACTION_SETTING.exists(indexSettings)); assertFalse(LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.exists(indexSettings)); - assertFalse(LifecycleSettings.LIFECYCLE_STEP_SETTING.exists(indexSettings)); + assertFalse(LifecycleSettings.LIFECYCLE_NEXT_STEP_SETTING.exists(indexSettings)); assertFalse(LifecycleSettings.LIFECYCLE_STEP_TIME_SETTING.exists(indexSettings)); assertFalse(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.exists(indexSettings)); assertFalse(LifecycleSettings.LIFECYCLE_FAILED_STEP_SETTING.exists(indexSettings)); @@ -1069,9 +1138,9 @@ public static void assertClusterStateOnPolicy(ClusterState oldClusterState, Inde assertNotSame(oldClusterState.metaData().index(index), newIndexMetadata); Settings newIndexSettings = newIndexMetadata.getSettings(); assertNotSame(oldClusterState.metaData().index(index).getSettings(), newIndexSettings); - assertEquals(expectedStep.getPhase(), LifecycleSettings.LIFECYCLE_PHASE_SETTING.get(newIndexSettings)); - assertEquals(expectedStep.getAction(), LifecycleSettings.LIFECYCLE_ACTION_SETTING.get(newIndexSettings)); - assertEquals(expectedStep.getName(), LifecycleSettings.LIFECYCLE_STEP_SETTING.get(newIndexSettings)); + assertEquals(expectedStep.getPhase(), LifecycleSettings.LIFECYCLE_NEXT_PHASE_SETTING.get(newIndexSettings)); + assertEquals(expectedStep.getAction(), LifecycleSettings.LIFECYCLE_NEXT_ACTION_SETTING.get(newIndexSettings)); + assertEquals(expectedStep.getName(), LifecycleSettings.LIFECYCLE_NEXT_STEP_SETTING.get(newIndexSettings)); if (previousStep.getPhase().equals(expectedStep.getPhase())) { assertEquals(LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(oldClusterState.metaData().index(index).getSettings()), LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(newIndexSettings)); @@ -1103,9 +1172,9 @@ public static void assertClusterStateOnNextStep(ClusterState oldClusterState, In assertNotSame(oldClusterState.metaData().index(index), newIndexMetadata); Settings newIndexSettings = newIndexMetadata.getSettings(); assertNotSame(oldClusterState.metaData().index(index).getSettings(), newIndexSettings); - assertEquals(nextStep.getPhase(), LifecycleSettings.LIFECYCLE_PHASE_SETTING.get(newIndexSettings)); - assertEquals(nextStep.getAction(), LifecycleSettings.LIFECYCLE_ACTION_SETTING.get(newIndexSettings)); - assertEquals(nextStep.getName(), LifecycleSettings.LIFECYCLE_STEP_SETTING.get(newIndexSettings)); + assertEquals(nextStep.getPhase(), LifecycleSettings.LIFECYCLE_NEXT_PHASE_SETTING.get(newIndexSettings)); + assertEquals(nextStep.getAction(), LifecycleSettings.LIFECYCLE_NEXT_ACTION_SETTING.get(newIndexSettings)); + assertEquals(nextStep.getName(), LifecycleSettings.LIFECYCLE_NEXT_STEP_SETTING.get(newIndexSettings)); if (currentStep.getPhase().equals(nextStep.getPhase())) { assertEquals(LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(oldClusterState.metaData().index(index).getSettings()), LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(newIndexSettings)); @@ -1132,9 +1201,9 @@ private void assertClusterStateOnErrorStep(ClusterState oldClusterState, Index i assertNotSame(oldClusterState.metaData().index(index), newIndexMetadata); Settings newIndexSettings = newIndexMetadata.getSettings(); assertNotSame(oldClusterState.metaData().index(index).getSettings(), newIndexSettings); - assertEquals(currentStep.getPhase(), LifecycleSettings.LIFECYCLE_PHASE_SETTING.get(newIndexSettings)); - assertEquals(currentStep.getAction(), LifecycleSettings.LIFECYCLE_ACTION_SETTING.get(newIndexSettings)); - assertEquals(ErrorStep.NAME, LifecycleSettings.LIFECYCLE_STEP_SETTING.get(newIndexSettings)); + assertEquals(currentStep.getPhase(), LifecycleSettings.LIFECYCLE_NEXT_PHASE_SETTING.get(newIndexSettings)); + assertEquals(currentStep.getAction(), LifecycleSettings.LIFECYCLE_NEXT_ACTION_SETTING.get(newIndexSettings)); + assertEquals(ErrorStep.NAME, LifecycleSettings.LIFECYCLE_NEXT_STEP_SETTING.get(newIndexSettings)); assertEquals(currentStep.getName(), LifecycleSettings.LIFECYCLE_FAILED_STEP_SETTING.get(newIndexSettings)); assertEquals(expectedCauseValue, LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.get(newIndexSettings)); assertEquals(LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(oldClusterState.metaData().index(index).getSettings()), @@ -1156,9 +1225,9 @@ private void assertClusterStateStepInfo(ClusterState oldClusterState, Index inde assertNotSame(oldClusterState.metaData().index(index), newIndexMetadata); Settings newIndexSettings = newIndexMetadata.getSettings(); assertNotSame(oldClusterState.metaData().index(index).getSettings(), newIndexSettings); - assertEquals(currentStep.getPhase(), LifecycleSettings.LIFECYCLE_PHASE_SETTING.get(newIndexSettings)); - assertEquals(currentStep.getAction(), LifecycleSettings.LIFECYCLE_ACTION_SETTING.get(newIndexSettings)); - assertEquals(currentStep.getName(), LifecycleSettings.LIFECYCLE_STEP_SETTING.get(newIndexSettings)); + assertEquals(currentStep.getPhase(), LifecycleSettings.LIFECYCLE_NEXT_PHASE_SETTING.get(newIndexSettings)); + assertEquals(currentStep.getAction(), LifecycleSettings.LIFECYCLE_NEXT_ACTION_SETTING.get(newIndexSettings)); + assertEquals(currentStep.getName(), LifecycleSettings.LIFECYCLE_NEXT_STEP_SETTING.get(newIndexSettings)); assertEquals(expectedstepInfoValue, LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.get(newIndexSettings)); assertEquals(LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(oldClusterState.metaData().index(index).getSettings()), LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(newIndexSettings)); diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleServiceTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleServiceTests.java index 09ffb32f107df..5d94141cace40 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleServiceTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleServiceTests.java @@ -319,9 +319,9 @@ public void testRequestedStopOnShrink() { Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); IndexMetaData indexMetadata = IndexMetaData.builder(index.getName()) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policyName) - .put(LifecycleSettings.LIFECYCLE_PHASE, mockShrinkStep.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, mockShrinkStep.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, mockShrinkStep.getName())) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, mockShrinkStep.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, mockShrinkStep.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, mockShrinkStep.getName())) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); ImmutableOpenMap.Builder indices = ImmutableOpenMap. builder() .fPut(index.getName(), indexMetadata); @@ -359,9 +359,9 @@ public void testRequestedStopOnSafeAction() { Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); IndexMetaData indexMetadata = IndexMetaData.builder(index.getName()) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policyName) - .put(LifecycleSettings.LIFECYCLE_PHASE, currentStepKey.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStepKey.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStepKey.getName())) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, currentStepKey.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, currentStepKey.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, currentStepKey.getName())) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); ImmutableOpenMap.Builder indices = ImmutableOpenMap. builder() .fPut(index.getName(), indexMetadata); diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/MoveToErrorStepUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/MoveToErrorStepUpdateTaskTests.java index ab037647c579a..d953bd1388d16 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/MoveToErrorStepUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/MoveToErrorStepUpdateTaskTests.java @@ -62,7 +62,7 @@ public void testExecuteSuccessfullyMoved() throws IOException { assertThat(actualKey, equalTo(new StepKey(currentStepKey.getPhase(), currentStepKey.getAction(), ErrorStep.NAME))); assertThat(LifecycleSettings.LIFECYCLE_FAILED_STEP_SETTING.get(newState.metaData().index(index).getSettings()), equalTo(currentStepKey.getName())); - assertThat(LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(newState.metaData().index(index).getSettings()), equalTo(-1L)); + assertThat(LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(newState.metaData().index(index).getSettings()), equalTo(now)); assertThat(LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.get(newState.metaData().index(index).getSettings()), equalTo(-1L)); assertThat(LifecycleSettings.LIFECYCLE_STEP_TIME_SETTING.get(newState.metaData().index(index).getSettings()), equalTo(now)); @@ -124,8 +124,8 @@ private void setStateToKey(StepKey stepKey) { clusterState = ClusterState.builder(clusterState) .metaData(MetaData.builder(clusterState.metaData()) .updateSettings(Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, stepKey.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, stepKey.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, stepKey.getName()).build(), index.getName())).build(); + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, stepKey.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, stepKey.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, stepKey.getName()).build(), index.getName())).build(); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/MoveToNextStepUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/MoveToNextStepUpdateTaskTests.java index 677e049886aa1..55c6b9c1cca1c 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/MoveToNextStepUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/MoveToNextStepUpdateTaskTests.java @@ -145,8 +145,8 @@ private void setStateToKey(StepKey stepKey) { clusterState = ClusterState.builder(clusterState) .metaData(MetaData.builder(clusterState.metaData()) .updateSettings(Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, stepKey.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, stepKey.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, stepKey.getName()).build(), index.getName())).build(); + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, stepKey.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, stepKey.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, stepKey.getName()).build(), index.getName())).build(); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/PolicyStepsRegistryTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/PolicyStepsRegistryTests.java index db768495d5937..dcb9860bccb82 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/PolicyStepsRegistryTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/PolicyStepsRegistryTests.java @@ -108,7 +108,7 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { String policyName = randomAlphaOfLength(5); LifecyclePolicy newPolicy = LifecyclePolicyTests.randomTestLifecyclePolicy(policyName); logger.info("--> policy: {}", newPolicy); - List policySteps = newPolicy.toSteps(client, () -> 0L); + List policySteps = newPolicy.toSteps(client); Map headers = new HashMap<>(); if (randomBoolean()) { headers.put(randomAlphaOfLength(10), randomAlphaOfLength(10)); @@ -127,7 +127,7 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { .put("index.number_of_replicas", 0) .put("index.version.created", Version.CURRENT.id) .put(LifecycleSettings.LIFECYCLE_NAME, policyName) - .put(LifecycleSettings.LIFECYCLE_PHASE, "new"))) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, "new"))) .build(); try (XContentBuilder builder = JsonXContent.contentBuilder()) { builder.startObject(); @@ -148,7 +148,7 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { PolicyStepsRegistry registry = new PolicyStepsRegistry(); // add new policy - registry.update(currentState, client, () -> 0L); + registry.update(currentState, client); assertThat(registry.getFirstStep(newPolicy.getName()), equalTo(policySteps.get(0))); assertThat(registry.getLifecyclePolicyMap().size(), equalTo(1)); @@ -163,10 +163,10 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { .metaData(MetaData.builder(currentState.metaData()) .put(IndexMetaData.builder(currentState.metaData().index("test")) .settings(Settings.builder().put(currentState.metaData().index("test").getSettings()) - .put(LifecycleSettings.LIFECYCLE_PHASE, step.getKey().getPhase())))) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, step.getKey().getPhase())))) .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) .build(); - registry.update(currentState, client, () -> 0L); + registry.update(currentState, client); assertThat(registeredStepsForPolicy.get(step.getKey()), equalTo(step)); assertThat(registry.getStep(index, step.getKey()), equalTo(step)); } @@ -174,7 +174,7 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { Map registryPolicyMap = registry.getLifecyclePolicyMap(); Map registryFirstStepMap = registry.getFirstStepMap(); Map> registryStepMap = registry.getStepMap(); - registry.update(currentState, client, () -> 0L); + registry.update(currentState, client); assertThat(registry.getLifecyclePolicyMap(), equalTo(registryPolicyMap)); assertThat(registry.getFirstStepMap(), equalTo(registryFirstStepMap)); assertThat(registry.getStepMap(), equalTo(registryStepMap)); @@ -185,7 +185,7 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { .metaData( MetaData.builder(metaData) .putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata)).build(); - registry.update(currentState, client, () -> 0L); + registry.update(currentState, client); assertTrue(registry.getLifecyclePolicyMap().isEmpty()); assertTrue(registry.getFirstStepMap().isEmpty()); assertTrue(registry.getStepMap().isEmpty()); @@ -218,7 +218,7 @@ public void testUpdateChangedPolicy() { .build(); PolicyStepsRegistry registry = new PolicyStepsRegistry(); // add new policy - registry.update(currentState, client, () -> 0L); + registry.update(currentState, client); // swap out policy newPolicy = LifecyclePolicyTests.randomTestLifecyclePolicy(policyName); @@ -226,7 +226,7 @@ public void testUpdateChangedPolicy() { new LifecyclePolicyMetadata(newPolicy, Collections.emptyMap())), OperationMode.RUNNING); currentState = ClusterState.builder(currentState) .metaData(MetaData.builder(metaData).putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata)).build(); - registry.update(currentState, client, () -> 0L); + registry.update(currentState, client); // TODO(talevy): assert changes... right now we do not support updates to policies. will require internal cleanup } @@ -248,7 +248,7 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except LifecyclePolicy updatedPolicy = new LifecyclePolicy(policyName, phases); logger.info("--> policy: {}", newPolicy); logger.info("--> updated policy: {}", updatedPolicy); - List policySteps = newPolicy.toSteps(client, () -> 0L); + List policySteps = newPolicy.toSteps(client); Map headers = new HashMap<>(); if (randomBoolean()) { headers.put(randomAlphaOfLength(10), randomAlphaOfLength(10)); @@ -267,7 +267,7 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except .put("index.number_of_replicas", 0) .put("index.version.created", Version.CURRENT.id) .put(LifecycleSettings.LIFECYCLE_NAME, policyName) - .put(LifecycleSettings.LIFECYCLE_PHASE, "warm"))) + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, "warm"))) .build(); try (XContentBuilder builder = JsonXContent.contentBuilder()) { builder.startObject(); @@ -288,7 +288,7 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except PolicyStepsRegistry registry = new PolicyStepsRegistry(); // add new policy - registry.update(currentState, client, () -> 0L); + registry.update(currentState, client); Map registeredStepsForPolicy = registry.getStepMap().get(newPolicy.getName()); Step shrinkStep = registeredStepsForPolicy.entrySet().stream() @@ -313,7 +313,7 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except currentState = ClusterState.builder(ClusterName.DEFAULT).metaData(metaData).build(); // Update the policies - registry.update(currentState, client, () -> 0L); + registry.update(currentState, client); registeredStepsForPolicy = registry.getStepMap().get(newPolicy.getName()); shrinkStep = registeredStepsForPolicy.entrySet().stream() diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/SetStepInfoUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/SetStepInfoUpdateTaskTests.java index df2c0fa0e69da..25d98d8cfcb7d 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/SetStepInfoUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/SetStepInfoUpdateTaskTests.java @@ -127,8 +127,8 @@ private void setStateToKey(StepKey stepKey) { clusterState = ClusterState.builder(clusterState) .metaData(MetaData.builder(clusterState.metaData()) .updateSettings(Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, stepKey.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, stepKey.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, stepKey.getName()).build(), index.getName())).build(); + .put(LifecycleSettings.LIFECYCLE_NEXT_PHASE, stepKey.getPhase()) + .put(LifecycleSettings.LIFECYCLE_NEXT_ACTION, stepKey.getAction()) + .put(LifecycleSettings.LIFECYCLE_NEXT_STEP, stepKey.getName()).build(), index.getName())).build(); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/TimeSeriesLifecycleActionsIT.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/TimeSeriesLifecycleActionsIT.java index 30737168c1fbe..75777276b161a 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/TimeSeriesLifecycleActionsIT.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/TimeSeriesLifecycleActionsIT.java @@ -199,9 +199,9 @@ private Map getOnlyIndexSettings(String index) throws IOExceptio } private StepKey getStepKey(Map settings) { - String phase = (String) settings.get(LifecycleSettings.LIFECYCLE_PHASE); - String action = (String) settings.get(LifecycleSettings.LIFECYCLE_ACTION); - String step = (String) settings.get(LifecycleSettings.LIFECYCLE_STEP); + String phase = (String) settings.get(LifecycleSettings.LIFECYCLE_NEXT_PHASE); + String action = (String) settings.get(LifecycleSettings.LIFECYCLE_NEXT_ACTION); + String step = (String) settings.get(LifecycleSettings.LIFECYCLE_NEXT_STEP); return new StepKey(phase, action, step); } } diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/20_move_to_step.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/20_move_to_step.yml index bb57a45b13d4d..efcdefad55056 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/20_move_to_step.yml +++ b/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/20_move_to_step.yml @@ -75,9 +75,9 @@ teardown: index: "my_index" body: current_step: - phase: "new" - action: "after" - name: "after" + phase: "warm" + action: "readonly" + name: "readonly" next_step: phase: "warm" action: "forcemerge" @@ -88,9 +88,9 @@ teardown: indices.get: index: my_index - match: { my_index.settings.index.lifecycle.name: "my_moveable_timeseries_lifecycle" } - - match: { my_index.settings.index.lifecycle.step: "forcemerge" } - - match: { my_index.settings.index.lifecycle.action: "forcemerge" } - - match: { my_index.settings.index.lifecycle.phase: "warm" } + - match: { my_index.settings.index.lifecycle.next_step: "forcemerge" } + - match: { my_index.settings.index.lifecycle.next_action: "forcemerge" } + - match: { my_index.settings.index.lifecycle.next_phase: "warm" } --- "Test Invalid Move To Step With Incorrect Current Step": @@ -116,9 +116,9 @@ teardown: indices.get: index: my_index - match: { my_index.settings.index.lifecycle.name: "my_moveable_timeseries_lifecycle" } - - match: { my_index.settings.index.lifecycle.step: "after" } - - match: { my_index.settings.index.lifecycle.action: "after" } - - match: { my_index.settings.index.lifecycle.phase: "new" } + - match: { my_index.settings.index.lifecycle.next_step: "readonly" } + - match: { my_index.settings.index.lifecycle.next_action: "readonly" } + - match: { my_index.settings.index.lifecycle.next_phase: "warm" } --- "Test Invalid Move To Step With Invalid Next Step": @@ -129,9 +129,9 @@ teardown: index: "my_index" body: current_step: - phase: "new" - action: "after" - name: "after" + phase: "warm" + action: "readonly" + name: "readonly" next_step: phase: "invalid" action: "invalid" @@ -144,9 +144,9 @@ teardown: indices.get: index: my_index - match: { my_index.settings.index.lifecycle.name: "my_moveable_timeseries_lifecycle" } - - match: { my_index.settings.index.lifecycle.step: "after" } - - match: { my_index.settings.index.lifecycle.action: "after" } - - match: { my_index.settings.index.lifecycle.phase: "new" } + - match: { my_index.settings.index.lifecycle.next_step: "readonly" } + - match: { my_index.settings.index.lifecycle.next_action: "readonly" } + - match: { my_index.settings.index.lifecycle.next_phase: "warm" } --- "Test Invalid Move To Step With Invalid Policy": diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/30_retry.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/30_retry.yml index eeae6b05b04dc..e7a483606d61a 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/30_retry.yml +++ b/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/30_retry.yml @@ -73,9 +73,9 @@ teardown: indices.get: index: my_index - match: { my_index.settings.index.lifecycle.name: "my_lifecycle" } - - match: { my_index.settings.index.lifecycle.step: "after" } - - match: { my_index.settings.index.lifecycle.action: "after" } - - match: { my_index.settings.index.lifecycle.phase: "new" } + - match: { my_index.settings.index.lifecycle.next_step: "readonly" } + - match: { my_index.settings.index.lifecycle.next_action: "readonly" } + - match: { my_index.settings.index.lifecycle.next_phase: "warm" } --- diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/40_explain_lifecycle.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/40_explain_lifecycle.yml index 269dee133275c..8cc06937a5ae8 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/40_explain_lifecycle.yml +++ b/x-pack/plugin/src/test/resources/rest-api-spec/test/ilm/40_explain_lifecycle.yml @@ -109,9 +109,9 @@ teardown: - is_true: indices.my_index.managed - match: { indices.my_index.index: "my_index" } - match: { indices.my_index.policy: "my_moveable_timeseries_lifecycle" } - - match: { indices.my_index.phase: "new" } - - match: { indices.my_index.action: "after" } - - match: { indices.my_index.step: "after" } + - match: { indices.my_index.phase: "warm" } + - match: { indices.my_index.action: "readonly" } + - match: { indices.my_index.step: "readonly" } - is_false: indices.my_index.failed_step - is_false: indices.my_index.step_info @@ -130,18 +130,18 @@ teardown: - is_true: indices.my_index.managed - match: { indices.my_index.index: "my_index" } - match: { indices.my_index.policy: "my_moveable_timeseries_lifecycle" } - - match: { indices.my_index.phase: "new" } - - match: { indices.my_index.action: "after" } - - match: { indices.my_index.step: "after" } + - match: { indices.my_index.phase: "warm" } + - match: { indices.my_index.action: "readonly" } + - match: { indices.my_index.step: "readonly" } - is_false: indices.my_index.failed_step - is_false: indices.my_index.step_info - is_true: indices.my_index2.managed - match: { indices.my_index2.index: "my_index2" } - match: { indices.my_index2.policy: "my_moveable_timeseries_lifecycle" } - - match: { indices.my_index2.phase: "new" } - - match: { indices.my_index2.action: "after" } - - match: { indices.my_index2.step: "after" } + - match: { indices.my_index2.phase: "warm" } + - match: { indices.my_index2.action: "readonly" } + - match: { indices.my_index2.step: "readonly" } - is_false: indices.my_index2.failed_step - is_false: indices.my_index2.step_info @@ -160,27 +160,27 @@ teardown: - is_true: indices.my_index.managed - match: { indices.my_index.index: "my_index" } - match: { indices.my_index.policy: "my_moveable_timeseries_lifecycle" } - - match: { indices.my_index.phase: "new" } - - match: { indices.my_index.action: "after" } - - match: { indices.my_index.step: "after" } + - match: { indices.my_index.phase: "warm" } + - match: { indices.my_index.action: "readonly" } + - match: { indices.my_index.step: "readonly" } - is_false: indices.my_index.failed_step - is_false: indices.my_index.step_info - is_true: indices.my_index2.managed - match: { indices.my_index2.index: "my_index2" } - match: { indices.my_index2.policy: "my_moveable_timeseries_lifecycle" } - - match: { indices.my_index2.phase: "new" } - - match: { indices.my_index2.action: "after" } - - match: { indices.my_index2.step: "after" } + - match: { indices.my_index2.phase: "warm" } + - match: { indices.my_index2.action: "readonly" } + - match: { indices.my_index2.step: "readonly" } - is_false: indices.my_index2.failed_step - is_false: indices.my_index2.step_info - is_true: indices.another_index.managed - match: { indices.another_index.index: "another_index" } - match: { indices.another_index.policy: "my_moveable_timeseries_lifecycle" } - - match: { indices.another_index.phase: "new" } - - match: { indices.another_index.action: "after" } - - match: { indices.another_index.step: "after" } + - match: { indices.another_index.phase: "warm" } + - match: { indices.another_index.action: "readonly" } + - match: { indices.another_index.step: "readonly" } - is_false: indices.another_index.failed_step - is_false: indices.another_index.step_info @@ -212,3 +212,56 @@ teardown: - is_false: indices.my_index - is_false: indices.my_index2 - is_false: indices.another_index + +--- +"Test correct phase and phase transition time": + + - do: + acknowlege: true + ilm.put_lifecycle: + lifecycle: "quick_warm_slow_cold" + body: | + { + "policy": { + "phases": { + "warm": { + "after": "0s", + "actions": { + "forcemerge": { + "max_num_segments": 1 + } + } + }, + "cold": { + "after": "6000s", + "actions": { + "allocate": { + "number_of_replicas": 1 + } + } + } + } + } + } + + - do: + indices.create: + index: foo + body: + settings: + index.lifecycle.name: "quick_warm_slow_cold" + + - do: + acknowledge: true + ilm.explain_lifecycle: + index: "foo" + + - is_true: indices.foo.managed + - match: { indices.foo.index: "foo" } + - match: { indices.foo.policy: "quick_warm_slow_cold" } + - match: { indices.foo.phase: "warm" } + - match: { indices.foo.action: "readonly" } + - match: { indices.foo.step: "readonly" } + - is_true: indices.foo.phase_time + - is_false: indices.foo.failed_step + - is_false: indices.foo.step_info