Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,11 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
String step = targetNextStepKey.getName();
long lifecycleDate = lifecycleState.getLifecycleDate();

LifecycleExecutionState.Builder relevantTargetCustomData = LifecycleExecutionState.builder();
relevantTargetCustomData.setIndexCreationDate(lifecycleDate);
relevantTargetCustomData.setAction(action);
LifecycleExecutionState.Builder relevantTargetCustomData = LifecycleExecutionState.builder(lifecycleState);
// Override the phase, action, and step for the target next StepKey
relevantTargetCustomData.setPhase(phase);
relevantTargetCustomData.setAction(action);
relevantTargetCustomData.setStep(step);
relevantTargetCustomData.setSnapshotRepository(lifecycleState.getSnapshotRepository());
relevantTargetCustomData.setSnapshotName(lifecycleState.getSnapshotName());
relevantTargetCustomData.setSnapshotIndexName(lifecycleState.getSnapshotIndexName());
relevantTargetCustomData.setShrinkIndexName(lifecycleState.getShrinkIndexName());

Metadata.Builder newMetadata = Metadata.builder(clusterState.getMetadata())
.put(IndexMetadata.builder(targetIndexMetadata)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,14 +371,16 @@ public boolean equals(Object o) {
Objects.equals(getSnapshotRepository(), that.getSnapshotRepository()) &&
Objects.equals(getSnapshotName(), that.getSnapshotName()) &&
Objects.equals(getSnapshotIndexName(), that.getSnapshotIndexName()) &&
Objects.equals(getShrinkIndexName(), that.getShrinkIndexName()) &&
Objects.equals(getRollupIndexName(), that.getRollupIndexName()) &&
Objects.equals(getPhaseDefinition(), that.getPhaseDefinition());
}

@Override
public int hashCode() {
return Objects.hash(getPhase(), getAction(), getStep(), getFailedStep(), isAutoRetryableError(), getFailedStepRetryCount(),
getStepInfo(), getPhaseDefinition(), getLifecycleDate(), getPhaseTime(), getActionTime(), getStepTime(),
getSnapshotRepository(), getSnapshotName(), getSnapshotIndexName());
getSnapshotRepository(), getSnapshotName(), getSnapshotIndexName(), getShrinkIndexName(), getRollupIndexName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ public LifecyclePolicy(LifecycleType type, String name, Map<String, Phase> phase
this.phases = phases;
this.type = type;
this.metadata = metadata;
}

public void validate() {
this.type.validate(phases.values());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public static LifecyclePolicy loadPolicy(String name, String resource, NamedXCon

try (XContentParser parser = XContentType.JSON.xContent()
.createParser(xContentRegistry, LoggingDeprecationHandler.THROW_UNSUPPORTED_OPERATION, source.utf8ToString())) {
return LifecyclePolicy.parse(parser, name);
LifecyclePolicy policy = LifecyclePolicy.parse(parser, name);
policy.validate();
return policy;
}
} catch (Exception e) {
throw new IllegalArgumentException("unable to load policy [" + name + "] from [" + resource + "]", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public LifecyclePolicy getPolicy() {

@Override
public ActionRequestValidationException validate() {
this.policy.validate();
ActionRequestValidationException err = null;
String phaseTimingErr = TimeseriesLifecycleType.validateMonotonicallyIncreasingPhaseTimings(this.policy.getPhases().values());
if (Strings.hasText(phaseTimingErr)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.xpack.core.ilm.Step.StepKey;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

Expand Down Expand Up @@ -102,6 +103,43 @@ public void testPerformAction() {
assertEquals(newIndexData.getSnapshotName(), oldIndexData.getSnapshotName());
}

public void testAllStateCopied() {
CopyExecutionStateStep step = createRandomInstance();
String indexName = randomAlphaOfLengthBetween(5, 20);

IndexMetadata originalIndexMetadata = IndexMetadata.builder(indexName)
.settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1,5))
.numberOfReplicas(randomIntBetween(1,5))
.putCustom(ILM_CUSTOM_METADATA_KEY, createCustomMetadata())
.build();
IndexMetadata shrunkIndexMetadata =
IndexMetadata.builder(step.getTargetIndexNameSupplier().apply(indexName, LifecycleExecutionState.builder().build()))
.settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1,5))
.numberOfReplicas(randomIntBetween(1,5))
.build();

ClusterState originalClusterState = ClusterState.builder(ClusterName.DEFAULT)
.metadata(Metadata.builder()
.put(originalIndexMetadata, false)
.put(shrunkIndexMetadata, false))
.build();

ClusterState newClusterState = step.performAction(originalIndexMetadata.getIndex(), originalClusterState);

LifecycleExecutionState oldIndexData = LifecycleExecutionState.fromIndexMetadata(originalIndexMetadata);
LifecycleExecutionState newIndexData = LifecycleExecutionState
.fromIndexMetadata(newClusterState.metadata().index(step.getTargetIndexNameSupplier().apply(indexName,
LifecycleExecutionState.builder().build())));

Map<String, String> beforeMap = new HashMap<>(oldIndexData.asMap());
// The target step key's StepKey is used in the new metadata, so update the "before" map with the new info so it can be compared
beforeMap.put("phase", step.getTargetNextStepKey().getPhase());
beforeMap.put("action", step.getTargetNextStepKey().getAction());
beforeMap.put("step", step.getTargetNextStepKey().getName());
Map<String, String> newMap = newIndexData.asMap();
assertThat(beforeMap, equalTo(newMap));
}

public void testPerformActionWithNoTarget() {
CopyExecutionStateStep step = createRandomInstance();
String indexName = randomAlphaOfLengthBetween(5, 20);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,52 +137,65 @@ public void testGetCurrentStepKey() {

private static LifecycleExecutionState mutate(LifecycleExecutionState toMutate) {
LifecycleExecutionState.Builder newState = LifecycleExecutionState.builder(toMutate);
boolean changed = false;
if (randomBoolean()) {
newState.setPhase(randomValueOtherThan(toMutate.getPhase(), () -> randomAlphaOfLengthBetween(5, 20)));
changed = true;
switch (randomIntBetween(0, 17)) {
case 0:
newState.setPhase(randomValueOtherThan(toMutate.getPhase(), () -> randomAlphaOfLengthBetween(5, 20)));
break;
case 1:
newState.setAction(randomValueOtherThan(toMutate.getAction(), () -> randomAlphaOfLengthBetween(5, 20)));
break;
case 2:
newState.setStep(randomValueOtherThan(toMutate.getStep(), () -> randomAlphaOfLengthBetween(5, 20)));
break;
case 3:
newState.setPhaseDefinition(randomValueOtherThan(toMutate.getPhaseDefinition(), () -> randomAlphaOfLengthBetween(5, 20)));
break;
case 4:
newState.setFailedStep(randomValueOtherThan(toMutate.getFailedStep(), () -> randomAlphaOfLengthBetween(5, 20)));
break;
case 5:
newState.setStepInfo(randomValueOtherThan(toMutate.getStepInfo(), () -> randomAlphaOfLengthBetween(5, 20)));
break;
case 6:
newState.setPhaseTime(randomValueOtherThan(toMutate.getPhaseTime(), ESTestCase::randomLong));
break;
case 7:
newState.setActionTime(randomValueOtherThan(toMutate.getActionTime(), ESTestCase::randomLong));
break;
case 8:
newState.setStepTime(randomValueOtherThan(toMutate.getStepTime(), ESTestCase::randomLong));
break;
case 9:
newState.setIndexCreationDate(randomValueOtherThan(toMutate.getLifecycleDate(), ESTestCase::randomLong));
break;
case 10:
newState.setShrinkIndexName(randomValueOtherThan(toMutate.getShrinkIndexName(), () -> randomAlphaOfLengthBetween(5, 20)));
break;
case 11:
newState.setSnapshotRepository(randomValueOtherThan(toMutate.getSnapshotRepository(),
() -> randomAlphaOfLengthBetween(5, 20)));
break;
case 12:
newState.setSnapshotIndexName(randomValueOtherThan(toMutate.getSnapshotIndexName(),
() -> randomAlphaOfLengthBetween(5, 20)));
break;
case 13:
newState.setSnapshotName(randomValueOtherThan(toMutate.getSnapshotName(), () -> randomAlphaOfLengthBetween(5, 20)));
break;
case 14:
newState.setRollupIndexName(randomValueOtherThan(toMutate.getRollupIndexName(), () -> randomAlphaOfLengthBetween(5, 20)));
break;
case 15:
newState.setIsAutoRetryableError(randomValueOtherThan(toMutate.isAutoRetryableError(), ESTestCase::randomBoolean));
break;
case 16:
newState.setFailedStepRetryCount(randomValueOtherThan(toMutate.getFailedStepRetryCount(), ESTestCase::randomInt));
break;
case 17:
return LifecycleExecutionState.builder().build();
default:
throw new IllegalStateException("unknown randomization branch");
}
if (randomBoolean()) {
newState.setAction(randomValueOtherThan(toMutate.getAction(), () -> randomAlphaOfLengthBetween(5, 20)));
changed = true;
}
if (randomBoolean()) {
newState.setStep(randomValueOtherThan(toMutate.getStep(), () -> randomAlphaOfLengthBetween(5, 20)));
changed = true;
}
if (randomBoolean()) {
newState.setPhaseDefinition(randomValueOtherThan(toMutate.getPhaseDefinition(), () -> randomAlphaOfLengthBetween(5, 20)));
changed = true;
}
if (randomBoolean()) {
newState.setFailedStep(randomValueOtherThan(toMutate.getFailedStep(), () -> randomAlphaOfLengthBetween(5, 20)));
changed = true;
}
if (randomBoolean()) {
newState.setStepInfo(randomValueOtherThan(toMutate.getStepInfo(), () -> randomAlphaOfLengthBetween(5, 20)));
changed = true;
}
if (randomBoolean()) {
newState.setPhaseTime(randomValueOtherThan(toMutate.getPhaseTime(), ESTestCase::randomLong));
changed = true;
}
if (randomBoolean()) {
newState.setActionTime(randomValueOtherThan(toMutate.getActionTime(), ESTestCase::randomLong));
changed = true;
}
if (randomBoolean()) {
newState.setStepTime(randomValueOtherThan(toMutate.getStepTime(), ESTestCase::randomLong));
changed = true;
}
if (randomBoolean()) {
newState.setIndexCreationDate(randomValueOtherThan(toMutate.getLifecycleDate(), ESTestCase::randomLong));
changed = true;
}

if (changed == false) {
return LifecycleExecutionState.builder().build();
}

return newState.build();
}

Expand Down Expand Up @@ -215,6 +228,10 @@ static Map<String, String> createCustomMetadata() {
customMetadata.put("snapshot_repository", repositoryName);
customMetadata.put("snapshot_name", snapshotName);
customMetadata.put("snapshot_index_name", snapshotIndexName);
customMetadata.put("shrink_index_name", randomAlphaOfLengthBetween(5, 20));
customMetadata.put("rollup_index_name", randomAlphaOfLengthBetween(5, 20));
customMetadata.put("is_auto_retryable_error", String.valueOf(randomBoolean()));
customMetadata.put("failed_step_retry_count", String.valueOf(randomInt()));
return customMetadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.cluster.metadata.DataStream;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Template;
Expand Down Expand Up @@ -207,7 +208,7 @@ public void testDeleteActionDeletesSearchableSnapshot() throws Exception {
}

public void testCreateInvalidPolicy() {
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> createPolicy(client(), policy,
ResponseException exception = expectThrows(ResponseException.class, () -> createPolicy(client(), policy,
new Phase("hot", TimeValue.ZERO, org.elasticsearch.common.collect.Map.of(RolloverAction.NAME,
new RolloverAction(null, null, null, 1L), SearchableSnapshotAction.NAME,
new SearchableSnapshotAction(randomAlphaOfLengthBetween(4, 10)))),
Expand All @@ -218,7 +219,7 @@ public void testCreateInvalidPolicy() {
)
);

assertThat(exception.getMessage(), is("phases [warm,cold] define one or more of [forcemerge, freeze, shrink, rollup]" +
assertThat(exception.getMessage(), containsString("phases [warm,cold] define one or more of [forcemerge, freeze, shrink, rollup]" +
" actions which are not allowed after a managed index is mounted as a searchable snapshot"));
}

Expand Down Expand Up @@ -458,7 +459,7 @@ public void testSecondSearchableSnapshotUsingDifferentRepoThrows() throws Except
String secondRepo = randomAlphaOfLengthBetween(10, 20);
createSnapshotRepo(client(), snapshotRepo, randomBoolean());
createSnapshotRepo(client(), secondRepo, randomBoolean());
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () ->
ResponseException e = expectThrows(ResponseException.class, () ->
createPolicy(client(), policy, null, null,
new Phase("cold", TimeValue.ZERO,
singletonMap(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo, randomBoolean()))),
Expand Down