Skip to content

Commit

Permalink
Re-read policy phase JSON when using ILM's move-to-step API (elastic#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
dakrone committed Nov 13, 2019
1 parent 0e10352 commit aeb3a93
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.elasticsearch.xpack.core.ilm;

import org.elasticsearch.client.Client;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
Expand Down Expand Up @@ -56,7 +57,7 @@ public static RolloverAction parse(XContentParser parser) {
return PARSER.apply(parser, null);
}

public RolloverAction(ByteSizeValue maxSize, TimeValue maxAge, Long maxDocs) {
public RolloverAction(@Nullable ByteSizeValue maxSize, @Nullable TimeValue maxAge, @Nullable Long maxDocs) {
if (maxSize == null && maxAge == null && maxDocs == null) {
throw new IllegalArgumentException("At least one rollover condition must be set.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,42 @@ public void testMoveToInjectedStep() throws Exception {
});
}

public void testMoveToStepRereadsPolicy() throws Exception {
createNewSingletonPolicy("hot", new RolloverAction(null, TimeValue.timeValueHours(1), null), TimeValue.ZERO);

createIndexWithSettings("test-1", Settings.builder()
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
.put(LifecycleSettings.LIFECYCLE_NAME, policy)
.put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, "alias"),
true);

assertBusy(() -> assertThat(getStepKeyForIndex("test-1"), equalTo(new StepKey("hot", "rollover", "check-rollover-ready"))));

createNewSingletonPolicy("hot", new RolloverAction(null, TimeValue.timeValueSeconds(1), null), TimeValue.ZERO);

// Move to the same step, which should re-read the policy
Request moveToStepRequest = new Request("POST", "_ilm/move/test-1");
moveToStepRequest.setJsonEntity("{\n" +
" \"current_step\": { \n" +
" \"phase\": \"hot\",\n" +
" \"action\": \"rollover\",\n" +
" \"name\": \"check-rollover-ready\"\n" +
" },\n" +
" \"next_step\": { \n" +
" \"phase\": \"hot\",\n" +
" \"action\": \"rollover\",\n" +
" \"name\": \"check-rollover-ready\"\n" +
" }\n" +
"}");
assertOK(client().performRequest(moveToStepRequest));

// Make sure we actually rolled over
assertBusy(() -> {
indexExists("test-000002");
});
}

public void testCanStopILMWithPolicyUsingNonexistentPolicy() throws Exception {
createIndexWithSettings(index, Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,11 @@ static Step getCurrentStep(PolicyStepsRegistry stepRegistry, String policy, Inde
* @param nextStepKey The next step to move the index into
* @param nowSupplier The current-time supplier for updating when steps changed
* @param stepRegistry The steps registry to check a step-key's existence in the index's current policy
* @param forcePhaseDefinitionRefresh When true, step information will be recompiled from the latest version of the
* policy. Otherwise, existing phase definition is used.
* @return The updated cluster state where the index moved to <code>nextStepKey</code>
*/
static ClusterState moveClusterStateToStep(String indexName, ClusterState currentState, StepKey currentStepKey,
StepKey nextStepKey, LongSupplier nowSupplier,
PolicyStepsRegistry stepRegistry, boolean forcePhaseDefinitionRefresh) {
PolicyStepsRegistry stepRegistry) {
IndexMetaData idxMeta = currentState.getMetaData().index(indexName);
validateTransition(idxMeta, currentStepKey, nextStepKey, stepRegistry);

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

return IndexLifecycleRunner.moveClusterStateToNextStep(idxMeta.getIndex(), currentState, currentStepKey,
nextStepKey, nowSupplier, forcePhaseDefinitionRefresh);
nextStepKey, nowSupplier, true);
}

static void validateTransition(IndexMetaData idxMeta, StepKey currentStepKey, StepKey nextStepKey, PolicyStepsRegistry stepRegistry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void maybeRunAsyncAction(ClusterState clusterState, IndexMetaData indexMe

public ClusterState moveClusterStateToStep(ClusterState currentState, String indexName, StepKey currentStepKey, StepKey nextStepKey) {
return IndexLifecycleRunner.moveClusterStateToStep(indexName, currentState, currentStepKey, nextStepKey,
nowSupplier, policyRegistry, false);
nowSupplier, policyRegistry);
}

public ClusterState moveClusterStateToPreviouslyFailedStep(ClusterState currentState, String[] indices) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ public void testSuccessfulValidatedMoveClusterStateToNextStep() {
ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), policyMetadatas);
Index index = clusterState.metaData().index(indexName).getIndex();
ClusterState newClusterState = IndexLifecycleRunner.moveClusterStateToStep(indexName, clusterState, currentStepKey,
nextStepKey, () -> now, stepRegistry, false);
nextStepKey, () -> now, stepRegistry);
assertClusterStateOnNextStep(clusterState, index, currentStepKey, nextStepKey, newClusterState, now);
}

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

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

0 comments on commit aeb3a93

Please sign in to comment.