Skip to content

Commit 257e45d

Browse files
authored
Re-read policy phase JSON when using ILM's move-to-step API (#48827)
When using the move-to-step API, we should reread the phase JSON from the latest version of the ILM policy. This allows a user to move to the same step while re-reading the policy's latest version. For example, when changing rollover criteria. While manually messing around with some other things I discovered that we only reread the policy when using the retry API, not the move-to-step API. This commit changes the move-to-step API to always read the latest version of the policy.
1 parent 7d500ba commit 257e45d

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.elasticsearch.xpack.core.ilm;
77

88
import org.elasticsearch.client.Client;
9+
import org.elasticsearch.common.Nullable;
910
import org.elasticsearch.common.ParseField;
1011
import org.elasticsearch.common.Strings;
1112
import org.elasticsearch.common.io.stream.StreamInput;
@@ -56,7 +57,7 @@ public static RolloverAction parse(XContentParser parser) {
5657
return PARSER.apply(parser, null);
5758
}
5859

59-
public RolloverAction(ByteSizeValue maxSize, TimeValue maxAge, Long maxDocs) {
60+
public RolloverAction(@Nullable ByteSizeValue maxSize, @Nullable TimeValue maxAge, @Nullable Long maxDocs) {
6061
if (maxSize == null && maxAge == null && maxDocs == null) {
6162
throw new IllegalArgumentException("At least one rollover condition must be set.");
6263
}

x-pack/plugin/ilm/qa/multi-node/src/test/java/org/elasticsearch/xpack/ilm/TimeSeriesLifecycleActionsIT.java

+36
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,42 @@ public void testMoveToInjectedStep() throws Exception {
808808
});
809809
}
810810

811+
public void testMoveToStepRereadsPolicy() throws Exception {
812+
createNewSingletonPolicy("hot", new RolloverAction(null, TimeValue.timeValueHours(1), null), TimeValue.ZERO);
813+
814+
createIndexWithSettings("test-1", Settings.builder()
815+
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
816+
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
817+
.put(LifecycleSettings.LIFECYCLE_NAME, policy)
818+
.put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, "alias"),
819+
true);
820+
821+
assertBusy(() -> assertThat(getStepKeyForIndex("test-1"), equalTo(new StepKey("hot", "rollover", "check-rollover-ready"))));
822+
823+
createNewSingletonPolicy("hot", new RolloverAction(null, TimeValue.timeValueSeconds(1), null), TimeValue.ZERO);
824+
825+
// Move to the same step, which should re-read the policy
826+
Request moveToStepRequest = new Request("POST", "_ilm/move/test-1");
827+
moveToStepRequest.setJsonEntity("{\n" +
828+
" \"current_step\": { \n" +
829+
" \"phase\": \"hot\",\n" +
830+
" \"action\": \"rollover\",\n" +
831+
" \"name\": \"check-rollover-ready\"\n" +
832+
" },\n" +
833+
" \"next_step\": { \n" +
834+
" \"phase\": \"hot\",\n" +
835+
" \"action\": \"rollover\",\n" +
836+
" \"name\": \"check-rollover-ready\"\n" +
837+
" }\n" +
838+
"}");
839+
assertOK(client().performRequest(moveToStepRequest));
840+
841+
// Make sure we actually rolled over
842+
assertBusy(() -> {
843+
indexExists("test-000002");
844+
});
845+
}
846+
811847
public void testCanStopILMWithPolicyUsingNonexistentPolicy() throws Exception {
812848
createIndexWithSettings(index, Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
813849
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,11 @@ static Step getCurrentStep(PolicyStepsRegistry stepRegistry, String policy, Inde
317317
* @param nextStepKey The next step to move the index into
318318
* @param nowSupplier The current-time supplier for updating when steps changed
319319
* @param stepRegistry The steps registry to check a step-key's existence in the index's current policy
320-
* @param forcePhaseDefinitionRefresh When true, step information will be recompiled from the latest version of the
321-
* policy. Otherwise, existing phase definition is used.
322320
* @return The updated cluster state where the index moved to <code>nextStepKey</code>
323321
*/
324322
static ClusterState moveClusterStateToStep(String indexName, ClusterState currentState, StepKey currentStepKey,
325323
StepKey nextStepKey, LongSupplier nowSupplier,
326-
PolicyStepsRegistry stepRegistry, boolean forcePhaseDefinitionRefresh) {
324+
PolicyStepsRegistry stepRegistry) {
327325
IndexMetaData idxMeta = currentState.getMetaData().index(indexName);
328326
validateTransition(idxMeta, currentStepKey, nextStepKey, stepRegistry);
329327

@@ -333,7 +331,7 @@ static ClusterState moveClusterStateToStep(String indexName, ClusterState curren
333331
indexName, currentStepKey, nextStepKey, policy);
334332

335333
return IndexLifecycleRunner.moveClusterStateToNextStep(idxMeta.getIndex(), currentState, currentStepKey,
336-
nextStepKey, nowSupplier, forcePhaseDefinitionRefresh);
334+
nextStepKey, nowSupplier, true);
337335
}
338336

339337
static void validateTransition(IndexMetaData idxMeta, StepKey currentStepKey, StepKey nextStepKey, PolicyStepsRegistry stepRegistry) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void maybeRunAsyncAction(ClusterState clusterState, IndexMetaData indexMe
8888

8989
public ClusterState moveClusterStateToStep(ClusterState currentState, String indexName, StepKey currentStepKey, StepKey nextStepKey) {
9090
return IndexLifecycleRunner.moveClusterStateToStep(indexName, currentState, currentStepKey, nextStepKey,
91-
nowSupplier, policyRegistry, false);
91+
nowSupplier, policyRegistry);
9292
}
9393

9494
public ClusterState moveClusterStateToPreviouslyFailedStep(ClusterState currentState, String[] indices) {

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ public void testSuccessfulValidatedMoveClusterStateToNextStep() {
839839
ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), policyMetadatas);
840840
Index index = clusterState.metaData().index(indexName).getIndex();
841841
ClusterState newClusterState = IndexLifecycleRunner.moveClusterStateToStep(indexName, clusterState, currentStepKey,
842-
nextStepKey, () -> now, stepRegistry, false);
842+
nextStepKey, () -> now, stepRegistry);
843843
assertClusterStateOnNextStep(clusterState, index, currentStepKey, nextStepKey, newClusterState, now);
844844
}
845845

@@ -861,7 +861,7 @@ public void testValidatedMoveClusterStateToNextStepWithoutPolicy() {
861861
ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), Collections.emptyList());
862862
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class,
863863
() -> IndexLifecycleRunner.moveClusterStateToStep(indexName, clusterState, currentStepKey,
864-
nextStepKey, () -> now, stepRegistry, false));
864+
nextStepKey, () -> now, stepRegistry));
865865
assertThat(exception.getMessage(), equalTo("index [my_index] is not associated with an Index Lifecycle Policy"));
866866
}
867867

@@ -884,7 +884,7 @@ public void testValidatedMoveClusterStateToNextStepInvalidCurrentStep() {
884884
ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), Collections.emptyList());
885885
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class,
886886
() -> IndexLifecycleRunner.moveClusterStateToStep(indexName, clusterState, notCurrentStepKey,
887-
nextStepKey, () -> now, stepRegistry, false));
887+
nextStepKey, () -> now, stepRegistry));
888888
assertThat(exception.getMessage(), equalTo("index [my_index] is not on current step " +
889889
"[{\"phase\":\"not_current_phase\",\"action\":\"not_current_action\",\"name\":\"not_current_step\"}]"));
890890
}
@@ -907,7 +907,7 @@ public void testValidatedMoveClusterStateToNextStepInvalidNextStep() {
907907
ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), Collections.emptyList());
908908
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class,
909909
() -> IndexLifecycleRunner.moveClusterStateToStep(indexName, clusterState, currentStepKey,
910-
nextStepKey, () -> now, stepRegistry, false));
910+
nextStepKey, () -> now, stepRegistry));
911911
assertThat(exception.getMessage(),
912912
equalTo("step [{\"phase\":\"next_phase\",\"action\":\"next_action\",\"name\":\"next_step\"}] " +
913913
"for index [my_index] with policy [my_policy] does not exist"));

0 commit comments

Comments
 (0)