From 47a4c0efb9c0468c6fdba4b71abc2b4c150f5211 Mon Sep 17 00:00:00 2001 From: Gordon Brown Date: Tue, 4 Sep 2018 16:19:45 -0600 Subject: [PATCH 1/5] Use custom index metadata for ILM state Using index settings for ILM state is fragile and exposes too much information that doesn't need to be exposed. Using custom index metadata is more resilient and allows more controlled access to internal information. --- .../client/IndexLifecycleIT.java | 8 - .../InitializePolicyContextStep.java | 18 +- .../LifecycleExecutionState.java | 253 +++++++++ .../indexlifecycle/LifecycleSettings.java | 30 - .../core/indexlifecycle/ShrinkAction.java | 11 +- .../ShrinkCopyExecutionState.java | 71 +++ .../xpack/core/indexlifecycle/ShrinkStep.java | 16 +- .../UpdateRolloverLifecycleDateStep.java | 13 +- .../InitializePolicyContextStepTests.java | 9 +- .../LifecycleExecutionStateTests.java | 57 ++ .../indexlifecycle/ShrinkActionTests.java | 21 +- .../ShrinkCopyExecutionStateTests.java | 89 +++ .../core/indexlifecycle/ShrinkStepTests.java | 13 +- .../UpdateRolloverLifecycleDateStepTests.java | 5 +- .../ExecuteStepsUpdateTask.java | 3 +- .../xpack/indexlifecycle/IndexLifecycle.java | 10 - .../indexlifecycle/IndexLifecycleRunner.java | 181 +++--- .../indexlifecycle/IndexLifecycleService.java | 4 +- .../MoveToErrorStepUpdateTask.java | 4 +- .../MoveToNextStepUpdateTask.java | 4 +- .../indexlifecycle/PolicyStepsRegistry.java | 14 +- .../indexlifecycle/SetStepInfoUpdateTask.java | 4 +- .../TransportExplainLifecycleAction.java | 26 +- .../ExecuteStepsUpdateTaskTests.java | 69 ++- .../IndexLifecycleInitialisationIT.java | 36 +- .../IndexLifecycleRunnerTests.java | 535 +++++++++--------- .../IndexLifecycleServiceTests.java | 24 +- .../MoveToErrorStepUpdateTaskTests.java | 31 +- .../MoveToNextStepUpdateTaskTests.java | 41 +- .../PolicyStepsRegistryTests.java | 22 +- .../SetStepInfoUpdateTaskTests.java | 28 +- .../TimeSeriesLifecycleActionsIT.java | 35 +- .../test/ilm/20_move_to_step.yml | 39 +- .../rest-api-spec/test/ilm/30_retry.yml | 12 +- 34 files changed, 1138 insertions(+), 598 deletions(-) create mode 100644 x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionState.java create mode 100644 x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkCopyExecutionState.java create mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionStateTests.java create mode 100644 x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkCopyExecutionStateTests.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 2b363c4a45c9d..504c558ee611b 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 @@ -160,14 +160,6 @@ public void testExplainLifecycle() throws Exception { .put("index.lifecycle.rollover_alias", "baz-alias").build(), "", "\"baz-alias\" : {}"); createIndex("squash", Settings.EMPTY); - assertBusy(() -> { - GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices("foo-01", "baz-01"); - GetSettingsResponse settingsResponse = highLevelClient().indices().getSettings(getSettingsRequest, RequestOptions.DEFAULT); - assertThat(settingsResponse.getSetting("foo-01", "index.lifecycle.name"), equalTo(policy.getName())); - assertThat(settingsResponse.getSetting("baz-01", "index.lifecycle.name"), equalTo(policy.getName())); - assertThat(settingsResponse.getSetting("foo-01", "index.lifecycle.phase"), equalTo("hot")); - assertThat(settingsResponse.getSetting("baz-01", "index.lifecycle.phase"), equalTo("hot")); - }); ExplainLifecycleRequest req = new ExplainLifecycleRequest(); req.indices("foo-01", "baz-01", "squash"); 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 7ec357a3013be..7934c484fdcec 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 @@ -10,9 +10,10 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.MetaData; -import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; +import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; + public final class InitializePolicyContextStep extends ClusterStateActionStep { public static final String INITIALIZATION_PHASE = "new"; public static final StepKey KEY = new StepKey(INITIALIZATION_PHASE, "init", "init"); @@ -30,18 +31,19 @@ public ClusterState performAction(Index index, ClusterState clusterState) { // Index must have been since deleted, ignore it return clusterState; } - Settings settings = indexMetaData.getSettings(); - if (settings.hasValue(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE)) { + LifecycleExecutionState lifecycleState = LifecycleExecutionState + .fromIndexMetadata(indexMetaData); + if (lifecycleState.getIndexCreationDate() != -1) { return clusterState; } ClusterState.Builder newClusterStateBuilder = ClusterState.builder(clusterState); - IndexMetaData idxMeta = clusterState.getMetaData().index(index); - Settings.Builder indexSettings = Settings.builder().put(idxMeta.getSettings()) - .put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, idxMeta.getCreationDate()); + + LifecycleExecutionState.Builder newCustomData = LifecycleExecutionState.builder(lifecycleState); + newCustomData.setIndexCreationDate(indexMetaData.getCreationDate()); newClusterStateBuilder.metaData(MetaData.builder(clusterState.getMetaData()).put(IndexMetaData - .builder(clusterState.getMetaData().index(index)) - .settings(indexSettings))); + .builder(indexMetaData) + .putCustom(ILM_CUSTOM_METADATA_KEY, newCustomData.build().asMap()))); return newClusterStateBuilder.build(); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionState.java new file mode 100644 index 0000000000000..77910b198573f --- /dev/null +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionState.java @@ -0,0 +1,253 @@ +/* + * 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.metadata.IndexMetaData; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +public class LifecycleExecutionState { + public static final String ILM_CUSTOM_METADATA_KEY = "ilm"; + + private static final String PHASE = "phase"; + private static final String ACTION = "action"; + private static final String STEP = "step"; + private static final String INDEX_CREATION_DATE = "creation_date"; + private static final String PHASE_TIME = "phase_time"; + private static final String ACTION_TIME = "action_time"; + private static final String STEP_TIME = "step_time"; + private static final String FAILED_STEP = "failed_step"; + private static final String STEP_INFO = "step_info"; + private static final String PHASE_DEFINITION = "phase_definition"; + + private final String phase; + private final String action; + private final String step; + private final String failedStep; + private final String stepInfo; + private final String phaseDefinition; + private final long indexCreationDate; + private final long phaseTime; + private final long actionTime; + private final long stepTime; + + private LifecycleExecutionState(String phase, String action, String step, String failedStep, + String stepInfo, String phaseDefinition, long indexCreationDate, + long phaseTime, long actionTime, long stepTime) { + this.phase = phase; + this.action = action; + this.step = step; + this.failedStep = failedStep; + this.stepInfo = stepInfo; + this.phaseDefinition = phaseDefinition; + this.indexCreationDate = indexCreationDate; + this.phaseTime = phaseTime; + this.actionTime = actionTime; + this.stepTime = stepTime; + } + + /** + * Retrieves the execution state from an {@link IndexMetaData} based on the + * custom metadata. + * @param indexMetaData The metadata of the index to retrieve the execution + * state from. + * @return The execution state of that index. + */ + public static LifecycleExecutionState fromIndexMetadata(IndexMetaData indexMetaData) { + Map customData = indexMetaData.getCustomData(ILM_CUSTOM_METADATA_KEY); + customData = customData == null ? new HashMap<>() : customData; + return fromCustomMetadata(customData); + } + + public static Builder builder() { + return new Builder(); + } + + public static Builder builder(LifecycleExecutionState state) { + return new Builder() + .setPhase(state.phase) + .setAction(state.action) + .setStep(state.step) + .setFailedStep(state.failedStep) + .setStepInfo(state.stepInfo) + .setPhaseDefinition(state.phaseDefinition) + .setIndexCreationDate(state.indexCreationDate) + .setPhaseTime(state.phaseTime) + .setActionTime(state.actionTime) + .setStepTime(state.stepTime); + } + + static LifecycleExecutionState fromCustomMetadata(Map customData) { + return new Builder() + .setPhase(customData.getOrDefault(PHASE, "")) + .setAction(customData.getOrDefault(ACTION, "")) + .setStep(customData.getOrDefault(STEP, "")) + .setFailedStep(customData.getOrDefault(FAILED_STEP, "")) + .setStepInfo(customData.getOrDefault(STEP_INFO, "")) + .setPhaseDefinition(customData.getOrDefault(PHASE_DEFINITION, "")) + .setIndexCreationDate(Long.parseLong(customData.getOrDefault(INDEX_CREATION_DATE, "-1"))) + .setPhaseTime(Long.parseLong(customData.getOrDefault(PHASE_TIME, "-1"))) + .setActionTime(Long.parseLong(customData.getOrDefault(ACTION_TIME, "-1"))) + .setStepTime(Long.parseLong(customData.getOrDefault(STEP_TIME, "-1"))) + .build(); + } + + /** + * Converts this object to an immutable map representation for use with + * {@link IndexMetaData.Builder#putCustom(String, Map)}. + * @return An immutable Map representation of this execution state. + */ + public Map asMap() { + Map result = new HashMap<>(); + result.put(PHASE, phase); + result.put(ACTION, action); + result.put(STEP, step); + result.put(FAILED_STEP, failedStep); + result.put(STEP_INFO, stepInfo); + result.put(INDEX_CREATION_DATE, String.valueOf(indexCreationDate)); + result.put(PHASE_TIME, String.valueOf(phaseTime)); + result.put(ACTION_TIME, String.valueOf(actionTime)); + result.put(STEP_TIME, String.valueOf(stepTime)); + result.put(PHASE_DEFINITION, String.valueOf(phaseDefinition)); + return Collections.unmodifiableMap(result); + } + + public String getPhase() { + return phase; + } + + public String getAction() { + return action; + } + + public String getStep() { + return step; + } + + public String getFailedStep() { + return failedStep; + } + + public String getStepInfo() { + return stepInfo; + } + + public String getPhaseDefinition() { + return phaseDefinition; + } + + public long getIndexCreationDate() { + return indexCreationDate; + } + + public long getPhaseTime() { + return phaseTime; + } + + public long getActionTime() { + return actionTime; + } + + public long getStepTime() { + return stepTime; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + LifecycleExecutionState that = (LifecycleExecutionState) o; + return getIndexCreationDate() == that.getIndexCreationDate() && + getPhaseTime() == that.getPhaseTime() && + getActionTime() == that.getActionTime() && + getStepTime() == that.getStepTime() && + Objects.equals(getPhase(), that.getPhase()) && + Objects.equals(getAction(), that.getAction()) && + Objects.equals(getStep(), that.getStep()) && + Objects.equals(getFailedStep(), that.getFailedStep()) && + Objects.equals(getStepInfo(), that.getStepInfo()) && + Objects.equals(getPhaseDefinition(), that.getPhaseDefinition()); + } + + @Override + public int hashCode() { + return Objects.hash(getPhase(), getAction(), getStep(), getFailedStep(), getStepInfo(), getPhaseDefinition(), + getIndexCreationDate(), getPhaseTime(), getActionTime(), getStepTime()); + } + + public static class Builder { + private String phase = ""; + private String action = ""; + private String step = ""; + private String failedStep = ""; + private String stepInfo = ""; + private String phaseDefinition = ""; + private long indexCreationDate = -1; + private long phaseTime = -1; + private long actionTime = -1; + private long stepTime = -1; + + public Builder setPhase(String phase) { + this.phase = Objects.requireNonNull(phase); + return this; + } + + public Builder setAction(String action) { + this.action = Objects.requireNonNull(action); + return this; + } + + public Builder setStep(String step) { + this.step = Objects.requireNonNull(step); + return this; + } + + public Builder setFailedStep(String failedStep) { + this.failedStep = Objects.requireNonNull(failedStep); + return this; + } + + public Builder setStepInfo(String stepInfo) { + this.stepInfo = Objects.requireNonNull(stepInfo); + return this; + } + + public Builder setPhaseDefinition(String phaseDefinition) { + this.phaseDefinition = Objects.requireNonNull(phaseDefinition); + return this; + } + + public Builder setIndexCreationDate(long indexCreationDate) { + this.indexCreationDate = indexCreationDate; + return this; + } + + public Builder setPhaseTime(long phaseTime) { + this.phaseTime = phaseTime; + return this; + } + + public Builder setActionTime(long actionTime) { + this.actionTime = actionTime; + return this; + } + + public Builder setStepTime(long stepTime) { + this.stepTime = stepTime; + return this; + } + + public LifecycleExecutionState build() { + return new LifecycleExecutionState(phase, action, step, failedStep, stepInfo, phaseDefinition, indexCreationDate, + phaseTime, actionTime, stepTime); + } + } + +} 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 027904a31bf65..7c9fc4f8e8a3e 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,42 +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_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"; - public static final String LIFECYCLE_STEP_TIME = "index.lifecycle.step_time"; - 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_PHASE_DEFINITION = "index.lifecycle.phase_definition"; 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, - 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, - 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); - public static final Setting LIFECYCLE_INDEX_CREATION_DATE_SETTING = Setting.longSetting(LIFECYCLE_INDEX_CREATION_DATE, - -1L, -1L, Setting.Property.Dynamic, Setting.Property.IndexScope, Setting.Property.InternalIndex); - public static final Setting LIFECYCLE_PHASE_TIME_SETTING = Setting.longSetting(LIFECYCLE_PHASE_TIME, - -1L, -1L, Setting.Property.Dynamic, Setting.Property.IndexScope, Setting.Property.InternalIndex); - public static final Setting LIFECYCLE_ACTION_TIME_SETTING = Setting.longSetting(LIFECYCLE_ACTION_TIME, - -1L, -1L, Setting.Property.Dynamic, Setting.Property.IndexScope, Setting.Property.InternalIndex); - public static final Setting LIFECYCLE_STEP_TIME_SETTING = Setting.longSetting(LIFECYCLE_STEP_TIME, - -1L, -1L, Setting.Property.Dynamic, Setting.Property.IndexScope, Setting.Property.InternalIndex); - public static final Setting LIFECYCLE_STEP_INFO_SETTING = Setting.simpleString(LIFECYCLE_STEP_INFO, Setting.Property.Dynamic, - 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_PHASE_DEFINITION_SETTING = Setting.simpleString(LIFECYCLE_PHASE_DEFINITION, - Setting.Property.Dynamic, Setting.Property.IndexScope); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkAction.java index 5467d4a8a4dc7..249e62cfe10fe 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkAction.java @@ -85,15 +85,18 @@ public List toSteps(Client client, String phase, Step.StepKey nextStepKey) StepKey allocationRoutedKey = new StepKey(phase, NAME, AllocationRoutedStep.NAME); StepKey shrinkKey = new StepKey(phase, NAME, ShrinkStep.NAME); StepKey enoughShardsKey = new StepKey(phase, NAME, ShrunkShardsAllocatedStep.NAME); + StepKey copyMetadataKey = new StepKey(phase, NAME, ShrinkCopyExecutionState.NAME); StepKey aliasKey = new StepKey(phase, NAME, ShrinkSetAliasStep.NAME); StepKey isShrunkIndexKey = new StepKey(phase, NAME, ShrunkenIndexCheckStep.NAME); + SetSingleNodeAllocateStep setSingleNodeStep = new SetSingleNodeAllocateStep(setSingleNodeKey, allocationRoutedKey, client); AllocationRoutedStep allocationStep = new AllocationRoutedStep(allocationRoutedKey, shrinkKey, false); ShrinkStep shrink = new ShrinkStep(shrinkKey, enoughShardsKey, client, numberOfShards, SHRUNKEN_INDEX_PREFIX); - ShrunkShardsAllocatedStep allocated = new ShrunkShardsAllocatedStep(enoughShardsKey, aliasKey, SHRUNKEN_INDEX_PREFIX); + ShrunkShardsAllocatedStep allocated = new ShrunkShardsAllocatedStep(enoughShardsKey, copyMetadataKey, SHRUNKEN_INDEX_PREFIX); + ShrinkCopyExecutionState copyMetadata = new ShrinkCopyExecutionState(copyMetadataKey, aliasKey, SHRUNKEN_INDEX_PREFIX); ShrinkSetAliasStep aliasSwapAndDelete = new ShrinkSetAliasStep(aliasKey, isShrunkIndexKey, client, SHRUNKEN_INDEX_PREFIX); ShrunkenIndexCheckStep waitOnShrinkTakeover = new ShrunkenIndexCheckStep(isShrunkIndexKey, nextStepKey, SHRUNKEN_INDEX_PREFIX); - return Arrays.asList(setSingleNodeStep, allocationStep, shrink, allocated, aliasSwapAndDelete, waitOnShrinkTakeover); + return Arrays.asList(setSingleNodeStep, allocationStep, shrink, allocated, copyMetadata, aliasSwapAndDelete, waitOnShrinkTakeover); } @Override @@ -102,9 +105,11 @@ public List toStepKeys(String phase) { StepKey allocationRoutedKey = new StepKey(phase, NAME, AllocationRoutedStep.NAME); StepKey shrinkKey = new StepKey(phase, NAME, ShrinkStep.NAME); StepKey enoughShardsKey = new StepKey(phase, NAME, ShrunkShardsAllocatedStep.NAME); + StepKey copyMetadataKey = new StepKey(phase, NAME, ShrinkCopyExecutionState.NAME); StepKey aliasKey = new StepKey(phase, NAME, ShrinkSetAliasStep.NAME); StepKey isShrunkIndexKey = new StepKey(phase, NAME, ShrunkenIndexCheckStep.NAME); - return Arrays.asList(setSingleNodeKey, allocationRoutedKey, shrinkKey, enoughShardsKey, aliasKey, isShrunkIndexKey); + return Arrays.asList(setSingleNodeKey, allocationRoutedKey, shrinkKey, enoughShardsKey, + copyMetadataKey, aliasKey, isShrunkIndexKey); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkCopyExecutionState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkCopyExecutionState.java new file mode 100644 index 0000000000000..7501cfe4bce64 --- /dev/null +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkCopyExecutionState.java @@ -0,0 +1,71 @@ +/* + * 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.cluster.metadata.MetaData; +import org.elasticsearch.index.Index; + +import java.util.Objects; + +import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; + +public class ShrinkCopyExecutionState extends ClusterStateActionStep { + public static final String NAME = "copy_metadata"; + private String shrunkIndexPrefix; + + public ShrinkCopyExecutionState(StepKey key, StepKey nextStepKey, String shrunkIndexPrefix) { + super(key, nextStepKey); + this.shrunkIndexPrefix = shrunkIndexPrefix; + } + + String getShrunkIndexPrefix() { + return shrunkIndexPrefix; + } + + @Override + public ClusterState performAction(Index index, ClusterState clusterState) { + IndexMetaData indexMetaData = clusterState.metaData().index(index); + // get source index + String indexName = indexMetaData.getIndex().getName(); + // get target shrink index + String targetIndexName = shrunkIndexPrefix + indexName; + IndexMetaData targetIndexMetaData = clusterState.metaData().index(targetIndexName); + + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetaData); + String phase = lifecycleState.getPhase(); + String action = lifecycleState.getAction(); + long lifecycleDate = lifecycleState.getIndexCreationDate(); + + LifecycleExecutionState.Builder relevantTargetCustomData = LifecycleExecutionState.builder(); + relevantTargetCustomData.setIndexCreationDate(lifecycleDate); + relevantTargetCustomData.setPhase(phase); + relevantTargetCustomData.setAction(action); + relevantTargetCustomData.setStep(ShrunkenIndexCheckStep.NAME); + + MetaData.Builder newMetaData = MetaData.builder(clusterState.getMetaData()) + .put(IndexMetaData.builder(targetIndexMetaData) + .putCustom(ILM_CUSTOM_METADATA_KEY, relevantTargetCustomData.build().asMap())); + + return ClusterState.builder(clusterState).metaData(newMetaData).build(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + ShrinkCopyExecutionState that = (ShrinkCopyExecutionState) o; + return Objects.equals(shrunkIndexPrefix, that.shrunkIndexPrefix); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), shrunkIndexPrefix); + } +} 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..930bc06131fa4 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 @@ -38,27 +38,16 @@ String getShrunkIndexPrefix() { @Override public void performAction(IndexMetaData indexMetaData, ClusterState currentState, Listener listener) { // if operating on the shrunken index, do nothing - - Long lifecycleDate = LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE_SETTING.get(indexMetaData.getSettings()); - if (lifecycleDate == null) { - throw new IllegalStateException("source index[" + indexMetaData.getIndex().getName() + - "] 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()); 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(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(); ResizeRequest resizeRequest = new ResizeRequest(shrunkenIndexName, indexMetaData.getIndex().getName()); indexMetaData.getAliases().values().spliterator().forEachRemaining(aliasMetaDataObjectCursor -> { @@ -70,6 +59,7 @@ public void performAction(IndexMetaData indexMetaData, ClusterState currentState // TODO(talevy): when is this not acknowledged? listener.onResponse(response.isAcknowledged()); }, listener::onFailure)); + } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/UpdateRolloverLifecycleDateStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/UpdateRolloverLifecycleDateStep.java index 2d09bc7750f17..9d1c7701faa5a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/UpdateRolloverLifecycleDateStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/UpdateRolloverLifecycleDateStep.java @@ -10,9 +10,10 @@ import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; +import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; + public class UpdateRolloverLifecycleDateStep extends ClusterStateActionStep { public static final String NAME = "update-rollover-lifecycle-date"; @@ -33,9 +34,15 @@ public ClusterState performAction(Index index, ClusterState currentState) { if (rolloverInfo == null) { throw new IllegalStateException("index [" + indexMetaData.getIndex().getName() + "] has not rolled over yet"); } - Settings settings = Settings.builder().put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, rolloverInfo.getTime()).build(); + + LifecycleExecutionState.Builder newLifecycleState = LifecycleExecutionState + .builder(LifecycleExecutionState.fromIndexMetadata(indexMetaData)); + newLifecycleState.setIndexCreationDate(rolloverInfo.getTime()); + + IndexMetaData.Builder newIndexMetadata = IndexMetaData.builder(indexMetaData); + newIndexMetadata.putCustom(ILM_CUSTOM_METADATA_KEY, newLifecycleState.build().asMap()); return ClusterState.builder(currentState).metaData(MetaData.builder(currentState.metaData()) - .updateSettings(settings, indexMetaData.getIndex().getName())).build(); + .put(newIndexMetadata)).build(); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/InitializePolicyContextStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/InitializePolicyContextStepTests.java index b4096b1054115..bd6aa3e88ead2 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/InitializePolicyContextStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/InitializePolicyContextStepTests.java @@ -14,6 +14,7 @@ import org.elasticsearch.index.Index; import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey; +import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; import static org.hamcrest.Matchers.equalTo; public class InitializePolicyContextStepTests extends AbstractStepTestCase { @@ -69,8 +70,11 @@ public void testAddCreationDate() { public void testDoNothing() { long creationDate = randomNonNegativeLong(); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setIndexCreationDate(creationDate); IndexMetaData indexMetadata = IndexMetaData.builder(randomAlphaOfLength(5)) - .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, creationDate)) + .settings(settings(Version.CURRENT)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) .creationDate(creationDate) .numberOfShards(1).numberOfReplicas(0).build(); MetaData metaData = MetaData.builder() @@ -85,7 +89,6 @@ public void testDoNothing() { } private long getIndexLifecycleDate(Index index, ClusterState clusterState) { - return clusterState.metaData().index(index).getSettings() - .getAsLong(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, -1L); + return LifecycleExecutionState.fromIndexMetadata(clusterState.getMetaData().index(index)).getIndexCreationDate(); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionStateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionStateTests.java new file mode 100644 index 0000000000000..689dbeb3981d7 --- /dev/null +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionStateTests.java @@ -0,0 +1,57 @@ +/* + * 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.test.ESTestCase; + +import java.util.HashMap; +import java.util.Map; + +public class LifecycleExecutionStateTests extends ESTestCase { + + public void testConversion() { + Map customMetadata = createCustomMetadata(); + LifecycleExecutionState parsed = LifecycleExecutionState.fromCustomMetadata(customMetadata); + assertEquals(customMetadata, parsed.asMap()); + } + + public void testInputValidation() { + LifecycleExecutionState.Builder test = LifecycleExecutionState.builder(); + expectThrows(NullPointerException.class, () -> test.setPhase(null)); + expectThrows(NullPointerException.class, () -> test.setAction(null)); + expectThrows(NullPointerException.class, () -> test.setStep(null)); + expectThrows(NullPointerException.class, () -> test.setFailedStep(null)); + expectThrows(NullPointerException.class, () -> test.setStepInfo(null)); + expectThrows(NullPointerException.class, () -> test.setPhaseDefinition(null)); + } + + static Map createCustomMetadata() { + String phase = randomAlphaOfLengthBetween(5,20); + String action = randomAlphaOfLengthBetween(5,20); + String step = randomAlphaOfLengthBetween(5,20); + String failedStep = randomAlphaOfLengthBetween(5,20); + String stepInfo = randomAlphaOfLengthBetween(15,50); + String phaseDefinition = randomAlphaOfLengthBetween(15,50); + long indexCreationDate = randomLong(); + long phaseTime = randomLong(); + long actionTime = randomLong(); + long stepTime = randomLong(); + + Map customMetadata = new HashMap<>(); + customMetadata.put("phase", phase); + customMetadata.put("action", action); + customMetadata.put("step", step); + customMetadata.put("failed_step", failedStep); + customMetadata.put("step_info", stepInfo); + customMetadata.put("phase_definition", phaseDefinition); + customMetadata.put("creation_date", String.valueOf(indexCreationDate)); + customMetadata.put("phase_time", String.valueOf(phaseTime)); + customMetadata.put("action_time", String.valueOf(actionTime)); + customMetadata.put("step_time", String.valueOf(stepTime)); + return customMetadata; + } +} diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkActionTests.java index 5ec140147ada7..2dd7ee624881b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkActionTests.java @@ -51,13 +51,14 @@ public void testToSteps() { StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10)); List steps = action.toSteps(null, phase, nextStepKey); - assertThat(steps.size(), equalTo(6)); + assertThat(steps.size(), equalTo(7)); StepKey expectedFirstKey = new StepKey(phase, ShrinkAction.NAME, SetSingleNodeAllocateStep.NAME); StepKey expectedSecondKey = new StepKey(phase, ShrinkAction.NAME, AllocationRoutedStep.NAME); StepKey expectedThirdKey = new StepKey(phase, ShrinkAction.NAME, ShrinkStep.NAME); StepKey expectedFourthKey = new StepKey(phase, ShrinkAction.NAME, ShrunkShardsAllocatedStep.NAME); - StepKey expectedFifthKey = new StepKey(phase, ShrinkAction.NAME, ShrinkSetAliasStep.NAME); - StepKey expectedSixthKey = new StepKey(phase, ShrinkAction.NAME, ShrunkenIndexCheckStep.NAME); + StepKey expectedFifthKey = new StepKey(phase, ShrinkAction.NAME, ShrinkCopyExecutionState.NAME); + StepKey expectedSixthKey = new StepKey(phase, ShrinkAction.NAME, ShrinkSetAliasStep.NAME); + StepKey expectedSeventhKey = new StepKey(phase, ShrinkAction.NAME, ShrunkenIndexCheckStep.NAME); assertTrue(steps.get(0) instanceof SetSingleNodeAllocateStep); assertThat(steps.get(0).getKey(), equalTo(expectedFirstKey)); assertThat(steps.get(0).getNextStepKey(), equalTo(expectedSecondKey)); @@ -74,14 +75,18 @@ public void testToSteps() { assertThat(steps.get(3).getKey(), equalTo(expectedFourthKey)); assertThat(steps.get(3).getNextStepKey(), equalTo(expectedFifthKey)); assertThat(((ShrunkShardsAllocatedStep) steps.get(3)).getShrunkIndexPrefix(), equalTo(ShrinkAction.SHRUNKEN_INDEX_PREFIX)); - assertTrue(steps.get(4) instanceof ShrinkSetAliasStep); + assertTrue(steps.get(4) instanceof ShrinkCopyExecutionState); assertThat(steps.get(4).getKey(), equalTo(expectedFifthKey)); assertThat(steps.get(4).getNextStepKey(), equalTo(expectedSixthKey)); - assertThat(((ShrinkSetAliasStep) steps.get(4)).getShrunkIndexPrefix(), equalTo(ShrinkAction.SHRUNKEN_INDEX_PREFIX)); - assertTrue(steps.get(5) instanceof ShrunkenIndexCheckStep); + assertThat(((ShrinkCopyExecutionState) steps.get(4)).getShrunkIndexPrefix(), equalTo(ShrinkAction.SHRUNKEN_INDEX_PREFIX)); + assertTrue(steps.get(5) instanceof ShrinkSetAliasStep); assertThat(steps.get(5).getKey(), equalTo(expectedSixthKey)); - assertThat(steps.get(5).getNextStepKey(), equalTo(nextStepKey)); - assertThat(((ShrunkenIndexCheckStep) steps.get(5)).getShrunkIndexPrefix(), equalTo(ShrinkAction.SHRUNKEN_INDEX_PREFIX)); + assertThat(steps.get(5).getNextStepKey(), equalTo(expectedSeventhKey)); + assertThat(((ShrinkSetAliasStep) steps.get(5)).getShrunkIndexPrefix(), equalTo(ShrinkAction.SHRUNKEN_INDEX_PREFIX)); + assertTrue(steps.get(6) instanceof ShrunkenIndexCheckStep); + assertThat(steps.get(6).getKey(), equalTo(expectedSeventhKey)); + assertThat(steps.get(6).getNextStepKey(), equalTo(nextStepKey)); + assertThat(((ShrunkenIndexCheckStep) steps.get(6)).getShrunkIndexPrefix(), equalTo(ShrinkAction.SHRUNKEN_INDEX_PREFIX)); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkCopyExecutionStateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkCopyExecutionStateTests.java new file mode 100644 index 0000000000000..79b5967f316c2 --- /dev/null +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkCopyExecutionStateTests.java @@ -0,0 +1,89 @@ +/* + * 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.xpack.core.indexlifecycle.Step.StepKey; + +import java.util.Map; + +import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; +import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionStateTests.createCustomMetadata; + +public class ShrinkCopyExecutionStateTests extends AbstractStepTestCase { + @Override + protected ShrinkCopyExecutionState createRandomInstance() { + StepKey stepKey = randomStepKey(); + StepKey nextStepKey = randomStepKey(); + String shrunkIndexPrefix = randomAlphaOfLength(10); + return new ShrinkCopyExecutionState(stepKey, nextStepKey, shrunkIndexPrefix); + } + + @Override + protected ShrinkCopyExecutionState mutateInstance(ShrinkCopyExecutionState instance) { + StepKey key = instance.getKey(); + StepKey nextKey = instance.getNextStepKey(); + String shrunkIndexPrefix = instance.getShrunkIndexPrefix(); + + 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: + shrunkIndexPrefix += randomAlphaOfLength(5); + break; + default: + throw new AssertionError("Illegal randomisation branch"); + } + + return new ShrinkCopyExecutionState(key, nextKey, shrunkIndexPrefix); + } + + @Override + protected ShrinkCopyExecutionState copyInstance(ShrinkCopyExecutionState instance) { + return new ShrinkCopyExecutionState(instance.getKey(), instance.getNextStepKey(), instance.getShrunkIndexPrefix()); + } + + public void testPerformAction() { + ShrinkCopyExecutionState step = createRandomInstance(); + String indexName = randomAlphaOfLengthBetween(5, 20); + Map customMetadata = createCustomMetadata(); + + IndexMetaData originalIndexMetaData = IndexMetaData.builder(indexName) + .settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1,5)) + .numberOfReplicas(randomIntBetween(1,5)) + .putCustom(ILM_CUSTOM_METADATA_KEY, customMetadata) + .build(); + IndexMetaData shrunkIndexMetaData = IndexMetaData.builder(step.getShrunkIndexPrefix() + indexName) + .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.getShrunkIndexPrefix() + indexName)); + + assertEquals(oldIndexData.getIndexCreationDate(), newIndexData.getIndexCreationDate()); + assertEquals(oldIndexData.getPhase(), newIndexData.getPhase()); + assertEquals(oldIndexData.getAction(), newIndexData.getAction()); + assertEquals(ShrunkenIndexCheckStep.NAME, newIndexData.getStep()); + } +} 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..51b7f2eab6c7d 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 @@ -84,13 +84,14 @@ public void testPerformAction() throws Exception { String lifecycleName = randomAlphaOfLength(5); long creationDate = randomNonNegativeLong(); ShrinkStep step = createRandomInstance(); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(step.getKey().getPhase()); + lifecycleState.setAction(step.getKey().getAction()); + lifecycleState.setStep(step.getKey().getName()); + lifecycleState.setIndexCreationDate(creationDate); IndexMetaData sourceIndexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)) .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) ) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) .putAlias(AliasMetaData.builder("my_alias")) @@ -114,11 +115,7 @@ public Void answer(InvocationOnMock invocation) throws Throwable { assertThat(request.getTargetIndexRequest().settings(), equalTo(Settings.builder() .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, step.getNumberOfShards()) .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(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_name", (String) null) .build())); assertThat(request.getTargetIndexRequest().settings() diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/UpdateRolloverLifecycleDateStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/UpdateRolloverLifecycleDateStepTests.java index 96cbe1e8a3a13..576d3c39ca1f6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/UpdateRolloverLifecycleDateStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/UpdateRolloverLifecycleDateStepTests.java @@ -67,8 +67,9 @@ public void testPerformAction() { UpdateRolloverLifecycleDateStep step = createRandomInstance(); ClusterState newState = step.performAction(indexMetaData.getIndex(), clusterState); - long actualRolloverTime = LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE_SETTING - .get(newState.metaData().index(indexMetaData.getIndex()).getSettings()); + long actualRolloverTime = LifecycleExecutionState + .fromIndexMetadata(newState.metaData().index(indexMetaData.getIndex())) + .getIndexCreationDate(); assertThat(actualRolloverTime, equalTo(rolloverTime)); } 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 c2e0f4521b7f1..5b1fb2dc7247e 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 @@ -16,6 +16,7 @@ import org.elasticsearch.index.Index; import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateActionStep; import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateWaitStep; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.Step; import java.io.IOException; @@ -71,7 +72,7 @@ public ClusterState execute(ClusterState currentState) throws IOException { return currentState; } Step registeredCurrentStep = IndexLifecycleRunner.getCurrentStep(policyStepsRegistry, policy, index, - indexMetaData.getSettings()); + LifecycleExecutionState.fromIndexMetadata(indexMetaData)); if (currentStep.equals(registeredCurrentStep)) { // We can do cluster state steps all together until we // either get to a step that isn't a cluster state step or a 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 1916095d2c5e2..8179ea0db0432 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 @@ -123,17 +123,7 @@ 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_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_PHASE_DEFINITION_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 e5637b095fe8a..a1d015122af59 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 @@ -27,6 +27,7 @@ import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateActionStep; import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateWaitStep; import org.elasticsearch.xpack.core.indexlifecycle.ErrorStep; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings; @@ -38,9 +39,13 @@ import org.elasticsearch.xpack.core.indexlifecycle.TerminalPolicyStep; import java.io.IOException; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.function.LongSupplier; +import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; + public class IndexLifecycleRunner { private static final Logger logger = LogManager.getLogger(IndexLifecycleRunner.class); private PolicyStepsRegistry stepRegistry; @@ -57,12 +62,12 @@ public IndexLifecycleRunner(PolicyStepsRegistry stepRegistry, ClusterService clu * 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) { + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetaData); + if (lifecycleState.getIndexCreationDate() == -1L) { logger.trace("no index creation date has been set yet"); return true; } - final long lifecycleDate = indexSettings.getAsLong(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, -1L); + final long lifecycleDate = lifecycleState.getIndexCreationDate(); 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(); @@ -79,18 +84,19 @@ boolean isReadyToTransitionToThisPhase(final String policy, final IndexMetaData } public void runPolicy(String policy, IndexMetaData indexMetaData, ClusterState currentState, - boolean fromClusterStateChange) { + boolean fromClusterStateChange) { Settings indexSettings = indexMetaData.getSettings(); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetaData); 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); + Step currentStep = getCurrentStep(stepRegistry, policy, indexMetaData.getIndex(), lifecycleState); 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() + logger.warn("current step [" + getCurrentStepKey(lifecycleState) + "] for index [" + indexMetaData.getIndex().getName() + "] with policy [" + policy + "] is not recognized"); return; } @@ -153,7 +159,7 @@ public void onFailure(Exception e) { } } else { throw new IllegalStateException( - "Step with key [" + currentStep.getKey() + "] is not a recognised type: [" + currentStep.getClass().getName() + "]"); + "Step with key [" + currentStep.getKey() + "] is not a recognised type: [" + currentStep.getClass().getName() + "]"); } } @@ -178,13 +184,12 @@ private void executeClusterStateSteps(Index index, String policy, Step step) { * or for the step to be unset with the phase and/or action set. All three * settings must be either present or missing. * - * @param indexSettings - * the index settings to extract the {@link StepKey} from. + * @param lifecycleState the index custom data 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); + public static StepKey getCurrentStepKey(LifecycleExecutionState lifecycleState) { + String currentPhase = lifecycleState.getPhase(); + String currentAction = lifecycleState.getAction(); + String currentStep = lifecycleState.getStep(); if (Strings.isNullOrEmpty(currentStep)) { assert Strings.isNullOrEmpty(currentPhase) : "Current phase is not empty: " + currentPhase; assert Strings.isNullOrEmpty(currentAction) : "Current action is not empty: " + currentAction; @@ -196,8 +201,8 @@ public static StepKey getCurrentStepKey(Settings indexSettings) { } } - static Step getCurrentStep(PolicyStepsRegistry stepRegistry, String policy, Index index, Settings indexSettings) { - StepKey currentStepKey = getCurrentStepKey(indexSettings); + static Step getCurrentStep(PolicyStepsRegistry stepRegistry, String policy, Index index, LifecycleExecutionState lifecycleState) { + StepKey currentStepKey = getCurrentStepKey(lifecycleState); if (currentStepKey == null) { return stepRegistry.getFirstStep(policy); } else { @@ -208,18 +213,13 @@ static Step getCurrentStep(PolicyStepsRegistry stepRegistry, String policy, Inde /** * This method is intended for handling moving to different steps from {@link TransportAction} executions. * For this reason, it is reasonable to throw {@link IllegalArgumentException} when state is not as expected. - * @param indexName - * The index whose step is to change - * @param currentState - * The current {@link ClusterState} - * @param currentStepKey - * The current {@link StepKey} found for the index in the current cluster state - * @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 indexName The index whose step is to change + * @param currentState The current {@link ClusterState} + * @param currentStepKey The current {@link StepKey} found for the index in the current cluster state + * @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 * @return The updated cluster state where the index moved to nextStepKey */ static ClusterState moveClusterStateToStep(String indexName, ClusterState currentState, StepKey currentStepKey, @@ -234,7 +234,8 @@ static ClusterState moveClusterStateToStep(String indexName, ClusterState curren throw new IllegalArgumentException("index [" + indexName + "] is not associated with an Index Lifecycle Policy"); } - if (currentStepKey.equals(IndexLifecycleRunner.getCurrentStepKey(indexSettings)) == false) { + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(idxMeta); + if (currentStepKey.equals(IndexLifecycleRunner.getCurrentStepKey(lifecycleState)) == false) { throw new IllegalArgumentException("index [" + indexName + "] is not on current step [" + currentStepKey + "]"); } @@ -247,17 +248,20 @@ static ClusterState moveClusterStateToStep(String indexName, ClusterState curren } static ClusterState moveClusterStateToNextStep(Index index, ClusterState clusterState, StepKey currentStep, StepKey nextStep, - LongSupplier nowSupplier) { + LongSupplier nowSupplier) { IndexMetaData idxMeta = clusterState.getMetaData().index(index); IndexLifecycleMetadata ilmMeta = clusterState.metaData().custom(IndexLifecycleMetadata.TYPE); LifecyclePolicy policy = ilmMeta.getPolicies().get(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(idxMeta.getSettings())); - Settings.Builder indexSettings = moveIndexSettingsToNextStep(policy, idxMeta.getSettings(), currentStep, nextStep, nowSupplier); - ClusterState.Builder newClusterStateBuilder = newClusterStateWithIndexSettings(index, clusterState, indexSettings); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(idxMeta); + LifecycleExecutionState newLifecycleState = moveExecutionStateToNextStep(policy, + lifecycleState, currentStep, nextStep, nowSupplier); + ClusterState.Builder newClusterStateBuilder = newClusterStateWithLifecycleState(index, clusterState, newLifecycleState); + return newClusterStateBuilder.build(); } static ClusterState moveClusterStateToErrorStep(Index index, ClusterState clusterState, StepKey currentStep, Exception cause, - LongSupplier nowSupplier) throws IOException { + LongSupplier nowSupplier) throws IOException { IndexMetaData idxMeta = clusterState.getMetaData().index(index); IndexLifecycleMetadata ilmMeta = clusterState.metaData().custom(IndexLifecycleMetadata.TYPE); LifecyclePolicy policy = ilmMeta.getPolicies().get(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(idxMeta.getSettings())); @@ -265,11 +269,13 @@ static ClusterState moveClusterStateToErrorStep(Index index, ClusterState cluste causeXContentBuilder.startObject(); ElasticsearchException.generateThrowableXContent(causeXContentBuilder, ToXContent.EMPTY_PARAMS, cause); causeXContentBuilder.endObject(); - Settings.Builder indexSettings = moveIndexSettingsToNextStep(policy, idxMeta.getSettings(), currentStep, - new StepKey(currentStep.getPhase(), currentStep.getAction(), ErrorStep.NAME), nowSupplier) - .put(LifecycleSettings.LIFECYCLE_FAILED_STEP, currentStep.getName()) - .put(LifecycleSettings.LIFECYCLE_STEP_INFO, BytesReference.bytes(causeXContentBuilder).utf8ToString()); - ClusterState.Builder newClusterStateBuilder = newClusterStateWithIndexSettings(index, clusterState, indexSettings); + LifecycleExecutionState nextStepState = moveExecutionStateToNextStep(policy, + LifecycleExecutionState.fromIndexMetadata(idxMeta), currentStep, new StepKey(currentStep.getPhase(), + currentStep.getAction(), ErrorStep.NAME), nowSupplier); + LifecycleExecutionState.Builder failedState = LifecycleExecutionState.builder(nextStepState); + failedState.setFailedStep(currentStep.getName()); + failedState.setStepInfo(BytesReference.bytes(causeXContentBuilder).utf8ToString()); + ClusterState.Builder newClusterStateBuilder = newClusterStateWithLifecycleState(index, clusterState, failedState.build()); return newClusterStateBuilder.build(); } @@ -280,8 +286,9 @@ ClusterState moveClusterStateToFailedStep(ClusterState currentState, String[] in if (indexMetaData == null) { throw new IllegalArgumentException("index [" + index + "] does not exist"); } - StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(indexMetaData.getSettings()); - String failedStep = LifecycleSettings.LIFECYCLE_FAILED_STEP_SETTING.get(indexMetaData.getSettings()); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetaData); + StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(lifecycleState); + String failedStep = lifecycleState.getFailedStep(); if (currentStepKey != null && ErrorStep.NAME.equals(currentStepKey.getName()) && Strings.isNullOrEmpty(failedStep) == false) { StepKey nextStepKey = new StepKey(currentStepKey.getPhase(), currentStepKey.getAction(), failedStep); @@ -294,15 +301,21 @@ ClusterState moveClusterStateToFailedStep(ClusterState currentState, String[] in return newState; } - private static Settings.Builder moveIndexSettingsToNextStep(LifecyclePolicy policy, Settings existingSettings, - StepKey currentStep, StepKey nextStep, LongSupplier nowSupplier) { + private static LifecycleExecutionState moveExecutionStateToNextStep(LifecyclePolicy policy, + LifecycleExecutionState existingState, + StepKey currentStep, StepKey nextStep, + 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); + LifecycleExecutionState.Builder updatedState = LifecycleExecutionState.builder(existingState); + updatedState.setPhase(nextStep.getPhase()); + updatedState.setAction(nextStep.getAction()); + updatedState.setStep(nextStep.getName()); + updatedState.setStepTime(nowAsMillis); + + // clear any step info or error-related settings from the current step + updatedState.setFailedStep(""); + updatedState.setStepInfo(""); + if (currentStep.getPhase().equals(nextStep.getPhase()) == false) { final String newPhaseDefinition; if ("new".equals(nextStep.getPhase()) || TerminalPolicyStep.KEY.equals(nextStep)) { @@ -310,25 +323,26 @@ private static Settings.Builder moveIndexSettingsToNextStep(LifecyclePolicy poli } else { Phase nextPhase = policy.getPhases().get(nextStep.getPhase()); if (nextPhase == null) { - newPhaseDefinition = null; + newPhaseDefinition = ""; } else { newPhaseDefinition = Strings.toString(nextPhase, false, false); } } - newSettings.put(LifecycleSettings.LIFECYCLE_PHASE_DEFINITION, newPhaseDefinition); - newSettings.put(LifecycleSettings.LIFECYCLE_PHASE_TIME, nowAsMillis); + updatedState.setPhaseDefinition(newPhaseDefinition); + updatedState.setPhaseTime(nowAsMillis); } if (currentStep.getAction().equals(nextStep.getAction()) == false) { - newSettings.put(LifecycleSettings.LIFECYCLE_ACTION_TIME, nowAsMillis); + updatedState.setActionTime(nowAsMillis); } - return newSettings; + return updatedState.build(); } - static ClusterState.Builder newClusterStateWithIndexSettings(Index index, ClusterState clusterState, - Settings.Builder newSettings) { + static ClusterState.Builder newClusterStateWithLifecycleState(Index index, ClusterState clusterState, + LifecycleExecutionState lifecycleState) { ClusterState.Builder newClusterStateBuilder = ClusterState.builder(clusterState); newClusterStateBuilder.metaData(MetaData.builder(clusterState.getMetaData()) - .put(IndexMetaData.builder(clusterState.getMetaData().index(index)).settings(newSettings))); + .put(IndexMetaData.builder(clusterState.getMetaData().index(index)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.asMap()))); return newClusterStateBuilder; } @@ -337,39 +351,44 @@ static ClusterState.Builder newClusterStateWithIndexSettings(Index index, Cluste * built if the step info has changed, otherwise the same old clusterState is * returned * - * @param index the index to modify + * @param index the index to modify * @param clusterState the cluster state to modify - * @param stepInfo the new step info to update + * @param stepInfo the new step info to update * @return Updated cluster state with stepInfo if changed, otherwise the same cluster state - * if no changes to step info exist + * if no changes to step info exist * @throws IOException if parsing step info fails */ static ClusterState addStepInfoToClusterState(Index index, ClusterState clusterState, ToXContentObject stepInfo) throws IOException { - IndexMetaData idxMeta = clusterState.getMetaData().index(index); + IndexMetaData indexMetaData = clusterState.getMetaData().index(index); + if (indexMetaData == null) { + // This index doesn't exist anymore, we can't do anything + return clusterState; + } + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetaData); final String stepInfoString; try (XContentBuilder infoXContentBuilder = JsonXContent.contentBuilder()) { stepInfo.toXContent(infoXContentBuilder, ToXContent.EMPTY_PARAMS); stepInfoString = BytesReference.bytes(infoXContentBuilder).utf8ToString(); } - if (stepInfoString.equals(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.get(idxMeta.getSettings()))) { + if (stepInfoString.equals(lifecycleState.getStepInfo())) { return clusterState; } - Settings.Builder indexSettings = Settings.builder().put(idxMeta.getSettings()) - .put(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.getKey(), stepInfoString); - ClusterState.Builder newClusterStateBuilder = newClusterStateWithIndexSettings(index, clusterState, indexSettings); + LifecycleExecutionState.Builder newState = LifecycleExecutionState.builder(lifecycleState); + newState.setStepInfo(stepInfoString); + ClusterState.Builder newClusterStateBuilder = newClusterStateWithLifecycleState(index, clusterState, newState.build()); return newClusterStateBuilder.build(); } private void moveToStep(Index index, String policy, StepKey currentStepKey, StepKey nextStepKey) { logger.debug("moveToStep[" + policy + "] [" + index.getName() + "]" + currentStepKey + " -> " - + nextStepKey); + + nextStepKey); clusterService.submitStateUpdateTask("ILM", new MoveToNextStepUpdateTask(index, policy, currentStepKey, - nextStepKey, nowSupplier)); + nextStepKey, nowSupplier)); } 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); + + "]. Moving to ERROR step.", e); clusterService.submitStateUpdateTask("ILM", new MoveToErrorStepUpdateTask(index, policy, currentStepKey, e, nowSupplier)); } @@ -378,7 +397,7 @@ private void setStepInfo(Index index, String policy, StepKey currentStepKey, ToX } public static ClusterState setPolicyForIndexes(final String newPolicyName, final Index[] indices, ClusterState currentState, - LifecyclePolicy newPolicy, List failedIndexes, LongSupplier nowSupplier) { + LifecyclePolicy newPolicy, List failedIndexes, LongSupplier nowSupplier) { MetaData.Builder newMetadata = MetaData.builder(currentState.getMetaData()); boolean clusterStateChanged = false; for (Index index : indices) { @@ -406,20 +425,22 @@ public static ClusterState setPolicyForIndexes(final String newPolicyName, final private static IndexMetaData.Builder setPolicyForIndex(final String newPolicyName, LifecyclePolicy newPolicy, IndexMetaData indexMetadata, LongSupplier nowSupplier) { - Settings idxSettings = indexMetadata.getSettings(); - StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(idxSettings); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetadata); + StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(lifecycleState); - Settings.Builder newSettings = Settings.builder().put(idxSettings); + LifecycleExecutionState newState = LifecycleExecutionState.builder(lifecycleState).build(); if (currentStepKey != null) { // Check if current step exists in new policy and if not move to // next available step StepKey nextValidStepKey = newPolicy.getNextValidStep(currentStepKey); if (nextValidStepKey.equals(currentStepKey) == false) { - newSettings = moveIndexSettingsToNextStep(newPolicy, idxSettings, currentStepKey, nextValidStepKey, nowSupplier); + newState = moveExecutionStateToNextStep(newPolicy, lifecycleState, currentStepKey, nextValidStepKey, nowSupplier); } } + + Settings.Builder newSettings = Settings.builder().put(indexMetadata.getSettings()); newSettings.put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), newPolicyName); - return IndexMetaData.builder(indexMetadata).settings(newSettings); + return IndexMetaData.builder(indexMetadata).settings(newSettings).putCustom(ILM_CUSTOM_METADATA_KEY, newState.asMap()); } public static ClusterState removePolicyForIndexes(final Index[] indices, ClusterState currentState, List failedIndexes) { @@ -452,17 +473,13 @@ 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_PHASE_TIME_SETTING.getKey()); - newSettings.remove(LifecycleSettings.LIFECYCLE_ACTION_SETTING.getKey()); - newSettings.remove(LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.getKey()); - newSettings.remove(LifecycleSettings.LIFECYCLE_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(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS_SETTING.getKey()); - return IndexMetaData.builder(indexMetadata).settings(newSettings); + + Map newCustomData = new HashMap<>(); + + return IndexMetaData.builder(indexMetadata) + .putCustom(ILM_CUSTOM_METADATA_KEY, newCustomData) + .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 91d1f8997c4ee..29769eee65038 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 @@ -6,7 +6,6 @@ package org.elasticsearch.xpack.indexlifecycle; import com.carrotsearch.hppc.cursors.ObjectCursor; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.lucene.util.SetOnce; @@ -25,6 +24,7 @@ import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.core.XPackField; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings; @@ -197,7 +197,7 @@ void triggerPolicies(ClusterState clusterState, boolean fromClusterStateChange) IndexMetaData idxMeta = cursor.value; String policyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(idxMeta.getSettings()); if (Strings.isNullOrEmpty(policyName) == false) { - StepKey stepKey = IndexLifecycleRunner.getCurrentStepKey(idxMeta.getSettings()); + StepKey stepKey = IndexLifecycleRunner.getCurrentStepKey(LifecycleExecutionState.fromIndexMetadata(idxMeta)); if (OperationMode.STOPPING == currentMode && stepKey != null && IGNORE_ACTIONS_MAINTENANCE_REQUESTED.contains(stepKey.getAction()) == false) { logger.info("skipping policy [" + policyName + "] for index [" + idxMeta.getIndex().getName() diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/MoveToErrorStepUpdateTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/MoveToErrorStepUpdateTask.java index b4352246182f6..5af1a05309e7e 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/MoveToErrorStepUpdateTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/MoveToErrorStepUpdateTask.java @@ -11,6 +11,7 @@ import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings; import org.elasticsearch.xpack.core.indexlifecycle.Step; @@ -56,8 +57,9 @@ public ClusterState execute(ClusterState currentState) throws IOException { return currentState; } Settings indexSettings = idxMeta.getSettings(); + LifecycleExecutionState indexILMData = LifecycleExecutionState.fromIndexMetadata(idxMeta); if (policy.equals(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexSettings)) - && currentStepKey.equals(IndexLifecycleRunner.getCurrentStepKey(indexSettings))) { + && currentStepKey.equals(IndexLifecycleRunner.getCurrentStepKey(indexILMData))) { return IndexLifecycleRunner.moveClusterStateToErrorStep(index, currentState, currentStepKey, cause, nowSupplier); } else { // either the policy has changed or the step is now diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/MoveToNextStepUpdateTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/MoveToNextStepUpdateTask.java index 96776d4e61c20..f7aabce777810 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/MoveToNextStepUpdateTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/MoveToNextStepUpdateTask.java @@ -11,6 +11,7 @@ import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings; import org.elasticsearch.xpack.core.indexlifecycle.Step; @@ -56,8 +57,9 @@ public ClusterState execute(ClusterState currentState) { return currentState; } Settings indexSettings = indexMetaData.getSettings(); + LifecycleExecutionState indexILMData = LifecycleExecutionState.fromIndexMetadata(currentState.getMetaData().index(index)); if (policy.equals(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexSettings)) - && currentStepKey.equals(IndexLifecycleRunner.getCurrentStepKey(indexSettings))) { + && currentStepKey.equals(IndexLifecycleRunner.getCurrentStepKey(indexILMData))) { return IndexLifecycleRunner.moveClusterStateToNextStep(index, currentState, currentStepKey, nextStepKey, nowSupplier); } else { // either the policy has changed or the step is now 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 ec4ff9d8d9986..c05671c743ff2 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 @@ -14,6 +14,7 @@ import org.elasticsearch.cluster.DiffableUtils; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.unit.TimeValue; @@ -24,6 +25,7 @@ import org.elasticsearch.index.Index; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.indexlifecycle.ErrorStep; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.indexlifecycle.InitializePolicyContextStep; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; @@ -161,15 +163,19 @@ 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, - InitializePolicyContextStep.INITIALIZATION_PHASE); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(imd.value); + final String currentPhase = Strings.isNullOrEmpty(lifecycleState.getPhase()) + ? InitializePolicyContextStep.INITIALIZATION_PHASE + : lifecycleState.getPhase(); + if (existingPhase.equals(currentPhase) == false) { logger.debug("index [{}] has transitioned phases [{} -> {}], rebuilding step list", index, existingPhase, currentPhase); // parse existing phase steps from the phase definition in the index settings - String phaseDef = imd.value.getSettings().get(LifecycleSettings.LIFECYCLE_PHASE_DEFINITION, - InitializePolicyContextStep.INITIALIZATION_PHASE); + String phaseDef = Strings.isNullOrEmpty(lifecycleState.getPhaseDefinition()) ? + InitializePolicyContextStep.INITIALIZATION_PHASE + : lifecycleState.getPhaseDefinition(); final Phase phase; LifecyclePolicy currentPolicy = lifecyclePolicyMap.get(policy).getPolicy(); final LifecyclePolicy policyToExecute; diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/SetStepInfoUpdateTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/SetStepInfoUpdateTask.java index 273da9e5e171d..bbba07fd8522d 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/SetStepInfoUpdateTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/SetStepInfoUpdateTask.java @@ -13,6 +13,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.index.Index; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings; import org.elasticsearch.xpack.core.indexlifecycle.Step; @@ -55,8 +56,9 @@ public ClusterState execute(ClusterState currentState) throws IOException { return currentState; } Settings indexSettings = idxMeta.getSettings(); + LifecycleExecutionState indexILMData = LifecycleExecutionState.fromIndexMetadata(idxMeta); if (policy.equals(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexSettings)) - && currentStepKey.equals(IndexLifecycleRunner.getCurrentStepKey(indexSettings))) { + && currentStepKey.equals(IndexLifecycleRunner.getCurrentStepKey(indexILMData))) { return IndexLifecycleRunner.addStepInfoToClusterState(index, currentState, stepInfo); } else { // either the policy has changed or the step is now 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..4d78953163fcf 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 @@ -19,11 +19,12 @@ import org.elasticsearch.common.bytes.BytesArray; 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.ExplainLifecycleRequest; import org.elasticsearch.xpack.core.indexlifecycle.ExplainLifecycleResponse; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleExplainResponse; -import org.elasticsearch.threadpool.ThreadPool; -import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings; import org.elasticsearch.xpack.core.indexlifecycle.action.ExplainLifecycleAction; @@ -64,20 +65,21 @@ protected void doMasterOperation(ExplainLifecycleRequest request, String[] concr for (String index : concreteIndices) { IndexMetaData idxMetadata = state.metaData().index(index); Settings idxSettings = idxMetadata.getSettings(); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(idxMetadata); String policyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(idxSettings); final IndexLifecycleExplainResponse indexResponse; if (Strings.hasLength(policyName)) { 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_FAILED_STEP_SETTING.get(idxSettings), - LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(idxSettings), - LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.get(idxSettings), - LifecycleSettings.LIFECYCLE_STEP_TIME_SETTING.get(idxSettings), - new BytesArray(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.get(idxSettings))); + LifecycleSettings.LIFECYCLE_SKIP_SETTING.get(idxSettings), + lifecycleState.getIndexCreationDate(), + lifecycleState.getPhase(), + lifecycleState.getAction(), + lifecycleState.getStep(), + lifecycleState.getFailedStep(), + lifecycleState.getPhaseTime(), + lifecycleState.getActionTime(), + lifecycleState.getStepTime(), + new BytesArray(lifecycleState.getStepInfo())); } else { indexResponse = IndexLifecycleExplainResponse.newUnmanagedIndexResponse(index); } 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 bd7b2437b5254..240e6192ed878 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 @@ -22,6 +22,7 @@ import org.elasticsearch.index.Index; import org.elasticsearch.node.Node; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyMetadata; @@ -44,6 +45,7 @@ import java.util.HashMap; import java.util.Map; +import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; import static org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyTestsUtils.newTestLifecyclePolicy; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.sameInstance; @@ -109,12 +111,14 @@ public void prepareState() throws IOException { private void setupIndexPolicy(String policyName) { // Reset the index to use the "allClusterPolicyName" + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase("new"); + lifecycleState.setAction("init"); + lifecycleState.setStep("init"); 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_NAME, policyName)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); index = indexMetadata.getIndex(); MetaData metaData = MetaData.builder() @@ -147,24 +151,28 @@ public void testExecuteUntilFirstNonClusterStateStep() throws IOException { long now = randomNonNegativeLong(); ExecuteStepsUpdateTask task = new ExecuteStepsUpdateTask(mixedPolicyName, index, startStep, policyStepsRegistry, () -> now); ClusterState newState = task.execute(clusterState); - StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(newState.metaData().index(index).getSettings()); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(newState.getMetaData().index(index)); + StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(lifecycleState); 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_ACTION_TIME_SETTING.get(newState.metaData().index(index).getSettings()), equalTo(-1L)); - assertThat(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.get(newState.metaData().index(index).getSettings()), equalTo("")); + assertThat(lifecycleState.getPhaseTime(), equalTo(-1L)); + assertThat(lifecycleState.getActionTime(), equalTo(-1L)); + assertThat(lifecycleState.getStepInfo(), equalTo("")); } public void testExecuteInvalidStartStep() throws IOException { // Unset the index's phase/action/step to simulate starting from scratch + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder( + LifecycleExecutionState.fromIndexMetadata(clusterState.getMetaData().index(index))); + lifecycleState.setPhase(""); + lifecycleState.setAction(""); + lifecycleState.setStep(""); clusterState = ClusterState.builder(clusterState) - .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(); + .metaData(MetaData.builder(clusterState.getMetaData()) + .put(IndexMetaData.builder(clusterState.getMetaData().index(index)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()))).build(); + policyStepsRegistry.update(clusterState); Step invalidStep = new MockClusterStateActionStep(firstStepKey, secondStepKey); @@ -181,13 +189,14 @@ public void testExecuteIncompleteWaitStepNoInfo() throws IOException { long now = randomNonNegativeLong(); ExecuteStepsUpdateTask task = new ExecuteStepsUpdateTask(mixedPolicyName, index, startStep, policyStepsRegistry, () -> now); ClusterState newState = task.execute(clusterState); - StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(newState.metaData().index(index).getSettings()); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(newState.getMetaData().index(index)); + StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(lifecycleState); assertThat(currentStepKey, equalTo(secondStepKey)); 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_ACTION_TIME_SETTING.get(newState.metaData().index(index).getSettings()), equalTo(-1L)); - assertThat(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.get(newState.metaData().index(index).getSettings()), equalTo("")); + assertThat(lifecycleState.getPhaseTime(), equalTo(-1L)); + assertThat(lifecycleState.getActionTime(), equalTo(-1L)); + assertThat(lifecycleState.getStepInfo(), equalTo("")); } public void testExecuteIncompleteWaitStepWithInfo() throws IOException { @@ -199,14 +208,14 @@ public void testExecuteIncompleteWaitStepWithInfo() throws IOException { long now = randomNonNegativeLong(); ExecuteStepsUpdateTask task = new ExecuteStepsUpdateTask(mixedPolicyName, index, startStep, policyStepsRegistry, () -> now); ClusterState newState = task.execute(clusterState); - StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(newState.metaData().index(index).getSettings()); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(newState.getMetaData().index(index)); + StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(lifecycleState); assertThat(currentStepKey, equalTo(secondStepKey)); 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_ACTION_TIME_SETTING.get(newState.metaData().index(index).getSettings()), equalTo(-1L)); - assertThat(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.get(newState.metaData().index(index).getSettings()), - equalTo(stepInfo.toString())); + assertThat(lifecycleState.getPhaseTime(), equalTo(-1L)); + assertThat(lifecycleState.getActionTime(), equalTo(-1L)); + assertThat(lifecycleState.getStepInfo(), equalTo(stepInfo.toString())); } public void testOnFailure() throws IOException { @@ -223,13 +232,15 @@ public void testOnFailure() throws IOException { } private void setStateToKey(StepKey stepKey) throws IOException { + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder( + LifecycleExecutionState.fromIndexMetadata(clusterState.getMetaData().index(index))); + lifecycleState.setPhase(stepKey.getPhase()); + lifecycleState.setAction(stepKey.getAction()); + lifecycleState.setStep(stepKey.getName()); clusterState = ClusterState.builder(clusterState) - .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(); + .metaData(MetaData.builder(clusterState.getMetaData()) + .put(IndexMetaData.builder(clusterState.getMetaData().index(index)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()))).build(); policyStepsRegistry.update(clusterState); } } 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 376e3d2175042..e178c2806cac0 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 @@ -7,7 +7,7 @@ import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; -import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse; +import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.cluster.routing.RoutingNode; @@ -28,6 +28,7 @@ import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateWaitStep; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleAction; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings; @@ -169,9 +170,9 @@ public void testSingleNodeCluster() throws Exception { assertThat(indexLifecycleService.getScheduler().jobCount(), equalTo(1)); assertNotNull(indexLifecycleService.getScheduledJob()); assertBusy(() -> { - GetSettingsResponse settingsResponse = client().admin().indices().prepareGetSettings("test").get(); - String step = settingsResponse.getSetting("test", "index.lifecycle.step"); - assertThat(step, equalTo(TerminalPolicyStep.KEY.getName())); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(client().admin().cluster() + .prepareState().execute().actionGet().getState().getMetaData().index("test")); + assertThat(lifecycleState.getStep(), equalTo(TerminalPolicyStep.KEY.getName())); }); } @@ -214,9 +215,9 @@ public void testMasterDedicatedDataDedicated() throws Exception { assertEquals(true, client().admin().indices().prepareExists("test").get().isExists()); }); assertBusy(() -> { - GetSettingsResponse settingsResponse = client().admin().indices().prepareGetSettings("test").get(); - String step = settingsResponse.getSetting("test", "index.lifecycle.step"); - assertThat(step, equalTo(TerminalPolicyStep.KEY.getName())); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(client().admin().cluster() + .prepareState().execute().actionGet().getState().getMetaData().index("test")); + assertThat(lifecycleState.getStep(), equalTo(TerminalPolicyStep.KEY.getName())); }); } @@ -254,9 +255,9 @@ 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"); - assertThat(step, equalTo(ObservableClusterStateWaitStep.NAME)); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(client().admin().cluster() + .prepareState().execute().actionGet().getState().getMetaData().index("test")); + assertThat(lifecycleState.getStep(), equalTo(ObservableClusterStateWaitStep.NAME)); }); if (randomBoolean()) { @@ -276,19 +277,20 @@ 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"); - assertThat(step, equalTo(ObservableClusterStateWaitStep.NAME)); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(client().admin().cluster() + .prepareState().execute().actionGet().getState().getMetaData().index("test")); + assertThat(lifecycleState.getStep(), equalTo(ObservableClusterStateWaitStep.NAME)); }); + logger.info("new master is operation"); // complete the step - client().admin().indices().prepareUpdateSettings("test") + AcknowledgedResponse repsonse = client().admin().indices().prepareUpdateSettings("test") .setSettings(Collections.singletonMap("index.lifecycle.test.complete", true)).get(); assertBusy(() -> { - GetSettingsResponse settingsResponse = client().admin().indices().prepareGetSettings("test").get(); - String step = settingsResponse.getSetting("test", "index.lifecycle.step"); - assertThat(step, equalTo(TerminalPolicyStep.KEY.getName())); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(client().admin().cluster() + .prepareState().execute().actionGet().getState().getMetaData().index("test")); + assertThat(lifecycleState.getStep(), 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 f8bfeb89681ed..57fc164414608 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 @@ -29,6 +29,7 @@ import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateActionStep; import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateWaitStep; import org.elasticsearch.xpack.core.indexlifecycle.ErrorStep; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleAction; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; @@ -59,6 +60,7 @@ import java.util.function.Function; import java.util.stream.Collectors; +import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; import static org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyTestsUtils.newTestLifecyclePolicy; import static org.hamcrest.Matchers.equalTo; import static org.mockito.Mockito.mock; @@ -106,10 +108,12 @@ public void testRunPolicyErrorStep() { PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); 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)) + LifecycleExecutionState.Builder newState = LifecycleExecutionState.builder(); + newState.setPhase(stepKey.getPhase()); + newState.setAction(stepKey.getAction()); + newState.setStep(ErrorStep.NAME); + IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT)) + .putCustom(ILM_CUSTOM_METADATA_KEY, newState.build().asMap()) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); runner.runPolicy(policyName, indexMetaData, null, false); @@ -371,19 +375,18 @@ public void testRunPolicyUnknownStepType() { } public void testGetCurrentStepKey() { - Settings indexSettings = Settings.EMPTY; - StepKey stepKey = IndexLifecycleRunner.getCurrentStepKey(indexSettings); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + StepKey stepKey = IndexLifecycleRunner.getCurrentStepKey(lifecycleState.build()); assertNull(stepKey); String phase = randomAlphaOfLength(20); 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) - .build(); - stepKey = IndexLifecycleRunner.getCurrentStepKey(indexSettings2); + LifecycleExecutionState.Builder lifecycleState2 = LifecycleExecutionState.builder(); + lifecycleState2.setPhase(phase); + lifecycleState2.setAction(action); + lifecycleState2.setStep(step); + stepKey = IndexLifecycleRunner.getCurrentStepKey(lifecycleState2.build()); assertNotNull(stepKey); assertEquals(phase, stepKey.getPhase()); assertEquals(action, stepKey.getAction()); @@ -391,46 +394,42 @@ public void testGetCurrentStepKey() { phase = randomAlphaOfLength(20); 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) - .build(); - AssertionError error3 = expectThrows(AssertionError.class, () -> IndexLifecycleRunner.getCurrentStepKey(indexSettings3)); + step = ""; + LifecycleExecutionState.Builder lifecycleState3 = LifecycleExecutionState.builder(); + lifecycleState3.setPhase(phase); + lifecycleState3.setAction(action); + lifecycleState3.setStep(step); + AssertionError error3 = expectThrows(AssertionError.class, () -> IndexLifecycleRunner.getCurrentStepKey(lifecycleState3.build())); assertEquals("Current phase is not empty: " + phase, error3.getMessage()); - phase = randomBoolean() ? null : ""; + phase = ""; 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) - .build(); - AssertionError error4 = expectThrows(AssertionError.class, () -> IndexLifecycleRunner.getCurrentStepKey(indexSettings4)); + step = ""; + LifecycleExecutionState.Builder lifecycleState4 = LifecycleExecutionState.builder(); + lifecycleState4.setPhase(phase); + lifecycleState4.setAction(action); + lifecycleState4.setStep(step); + AssertionError error4 = expectThrows(AssertionError.class, () -> IndexLifecycleRunner.getCurrentStepKey(lifecycleState4.build())); assertEquals("Current action is not empty: " + action, error4.getMessage()); - phase = randomBoolean() ? null : ""; + phase = ""; 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) - .build(); - AssertionError error5 = expectThrows(AssertionError.class, () -> IndexLifecycleRunner.getCurrentStepKey(indexSettings5)); + LifecycleExecutionState.Builder lifecycleState5 = LifecycleExecutionState.builder(); + lifecycleState5.setPhase(phase); + lifecycleState5.setAction(action); + lifecycleState5.setStep(step); + AssertionError error5 = expectThrows(AssertionError.class, () -> IndexLifecycleRunner.getCurrentStepKey(lifecycleState5.build())); assertEquals(null, error5.getMessage()); - phase = randomBoolean() ? null : ""; - action = randomBoolean() ? null : ""; + phase = ""; + action = ""; step = randomAlphaOfLength(20); - Settings indexSettings6 = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, phase) - .put(LifecycleSettings.LIFECYCLE_ACTION, action) - .put(LifecycleSettings.LIFECYCLE_STEP, step) - .build(); - AssertionError error6 = expectThrows(AssertionError.class, () -> IndexLifecycleRunner.getCurrentStepKey(indexSettings6)); + LifecycleExecutionState.Builder lifecycleState6 = LifecycleExecutionState.builder(); + lifecycleState6.setPhase(phase); + lifecycleState6.setAction(action); + lifecycleState6.setStep(step); + AssertionError error6 = expectThrows(AssertionError.class, () -> IndexLifecycleRunner.getCurrentStepKey(lifecycleState6.build())); assertEquals(null, error6.getMessage()); } @@ -474,32 +473,26 @@ public void testGetCurrentStep() { PolicyStepsRegistry registry = new PolicyStepsRegistry(lifecyclePolicyMap, firstStepMap, stepMap, indexSteps, NamedXContentRegistry.EMPTY, null); - Settings indexSettings = Settings.EMPTY; - Step actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, indexSettings); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + Step actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, lifecycleState.build()); assertSame(firstStep, actualStep); - indexSettings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, "phase_1") - .put(LifecycleSettings.LIFECYCLE_ACTION, "action_1") - .put(LifecycleSettings.LIFECYCLE_STEP, "step_1") - .build(); - actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, indexSettings); + lifecycleState.setPhase("phase_1"); + lifecycleState.setAction("action_1"); + lifecycleState.setStep("step_1"); + actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, lifecycleState.build()); assertSame(firstStep, actualStep); - indexSettings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, "phase_1") - .put(LifecycleSettings.LIFECYCLE_ACTION, "action_1") - .put(LifecycleSettings.LIFECYCLE_STEP, "step_2") - .build(); - actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, indexSettings); + lifecycleState.setPhase("phase_1"); + lifecycleState.setAction("action_1"); + lifecycleState.setStep("step_2"); + actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, lifecycleState.build()); assertSame(secondStep, actualStep); - indexSettings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, "phase_1") - .put(LifecycleSettings.LIFECYCLE_ACTION, "action_2") - .put(LifecycleSettings.LIFECYCLE_STEP, "step_1") - .build(); - actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, indexSettings); + lifecycleState.setPhase("phase_1"); + lifecycleState.setAction("action_2"); + lifecycleState.setStep("step_1"); + actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, lifecycleState.build()); assertSame(thirdStep, actualStep); // Switch to phase_2 @@ -508,20 +501,16 @@ public void testGetCurrentStep() { indexSteps.put(index, Collections.singletonList(fourthStep)); registry = new PolicyStepsRegistry(lifecyclePolicyMap, firstStepMap, stepMap, indexSteps, NamedXContentRegistry.EMPTY, null); - indexSettings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, "phase_2") - .put(LifecycleSettings.LIFECYCLE_ACTION, "action_1") - .put(LifecycleSettings.LIFECYCLE_STEP, "step_1") - .build(); - actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, indexSettings); + lifecycleState.setPhase("phase_2"); + lifecycleState.setAction("action_1"); + lifecycleState.setStep("step_1"); + actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, lifecycleState.build()); assertSame(fourthStep, actualStep); - indexSettings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, "phase_2") - .put(LifecycleSettings.LIFECYCLE_ACTION, "action_1") - .put(LifecycleSettings.LIFECYCLE_STEP, "step_1") - .build(); - actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, indexSettings); + lifecycleState.setPhase("phase_2"); + lifecycleState.setAction("action_1"); + lifecycleState.setStep("step_1"); + actualStep = IndexLifecycleRunner.getCurrentStep(registry, policyName, index, lifecycleState.build()); assertSame(fourthStep, actualStep); // Back to phase_1 @@ -529,29 +518,25 @@ public void testGetCurrentStep() { indexSteps.put(index, phase1Steps); registry = new PolicyStepsRegistry(lifecyclePolicyMap, firstStepMap, stepMap, indexSteps, NamedXContentRegistry.EMPTY, null); - indexSettings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, "phase_1") - .put(LifecycleSettings.LIFECYCLE_ACTION, "action_1") - .put(LifecycleSettings.LIFECYCLE_STEP, "step_1") - .build(); - actualStep = IndexLifecycleRunner.getCurrentStep(registry, otherPolicyName, index, indexSettings); + lifecycleState.setPhase("phase_1"); + lifecycleState.setAction("action_1"); + lifecycleState.setStep("step_1"); + actualStep = IndexLifecycleRunner.getCurrentStep(registry, otherPolicyName, index, lifecycleState.build()); assertEquals(otherPolicyFirstStep, actualStep); - indexSettings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE, "phase_1") - .put(LifecycleSettings.LIFECYCLE_ACTION, "action_1") - .put(LifecycleSettings.LIFECYCLE_STEP, "step_2") - .build(); - actualStep = IndexLifecycleRunner.getCurrentStep(registry, otherPolicyName, index, indexSettings); + lifecycleState.setPhase("phase_1"); + lifecycleState.setAction("action_1"); + lifecycleState.setStep("step_2"); + actualStep = IndexLifecycleRunner.getCurrentStep(registry, otherPolicyName, index, lifecycleState.build()); 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") - .build(); - assertNull(IndexLifecycleRunner.getCurrentStep(registry, policyName, index, invalidIndexSettings)); - assertNull(IndexLifecycleRunner.getCurrentStep(registry, "policy_does_not_exist", new Index("test","bad"), invalidIndexSettings)); + LifecycleExecutionState.Builder invalidIndexState = LifecycleExecutionState.builder(); + invalidIndexState.setPhase("phase_1"); + invalidIndexState.setAction("action_1"); + invalidIndexState.setStep("step_3"); + assertNull(IndexLifecycleRunner.getCurrentStep(registry, policyName, index, invalidIndexState.build())); + assertNull(IndexLifecycleRunner.getCurrentStep(registry, "policy_does_not_exist", new Index("test","bad"), + invalidIndexState.build())); } public void testMoveClusterStateToNextStep() { @@ -567,22 +552,25 @@ public void testMoveClusterStateToNextStep() { // test going from null lifecycle settings to next step ClusterState clusterState = buildClusterState(indexName, - Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy.getName()), policyMetadatas); + Settings.builder() + .put(LifecycleSettings.LIFECYCLE_NAME, policy.getName()), LifecycleExecutionState.builder().build(), policyMetadatas); Index index = clusterState.metaData().index(indexName).getIndex(); ClusterState newClusterState = IndexLifecycleRunner.moveClusterStateToNextStep(index, clusterState, currentStep, nextStep, () -> now); assertClusterStateOnNextStep(clusterState, index, currentStep, nextStep, newClusterState, now); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(currentStep.getPhase()); + lifecycleState.setAction(currentStep.getAction()); + lifecycleState.setStep(currentStep.getName()); // test going from set currentStep settings to nextStep Builder indexSettingsBuilder = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, policy.getName()) - .put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()); + .put(LifecycleSettings.LIFECYCLE_NAME, policy.getName()); if (randomBoolean()) { - indexSettingsBuilder.put(LifecycleSettings.LIFECYCLE_STEP_INFO, randomAlphaOfLength(20)); + lifecycleState.setStepInfo(randomAlphaOfLength(20)); } - clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); + + clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), policyMetadatas); index = clusterState.metaData().index(indexName).getIndex(); newClusterState = IndexLifecycleRunner.moveClusterStateToNextStep(index, clusterState, currentStep, nextStep, () -> now); assertClusterStateOnNextStep(clusterState, index, currentStep, nextStep, newClusterState, now); @@ -594,20 +582,22 @@ public void testMoveClusterStateToNextStepSamePhase() { StepKey nextStep = new StepKey("current_phase", "next_action", "next_step"); long now = randomNonNegativeLong(); - ClusterState clusterState = buildClusterState(indexName, Settings.builder(), Collections.emptyList()); + ClusterState clusterState = buildClusterState(indexName, Settings.builder(), LifecycleExecutionState.builder().build(), + Collections.emptyList()); 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()); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(currentStep.getPhase()); + lifecycleState.setAction(currentStep.getAction()); + lifecycleState.setStep(currentStep.getName()); if (randomBoolean()) { - indexSettingsBuilder.put(LifecycleSettings.LIFECYCLE_STEP_INFO, randomAlphaOfLength(20)); + lifecycleState.setStepInfo(randomAlphaOfLength(20)); } - clusterState = buildClusterState(indexName, - indexSettingsBuilder, Collections.emptyList()); + + clusterState = buildClusterState(indexName, Settings.builder(), lifecycleState.build(), Collections.emptyList()); index = clusterState.metaData().index(indexName).getIndex(); newClusterState = IndexLifecycleRunner.moveClusterStateToNextStep(index, clusterState, currentStep, nextStep, () -> now); assertClusterStateOnNextStep(clusterState, index, currentStep, nextStep, newClusterState, now); @@ -619,20 +609,21 @@ public void testMoveClusterStateToNextStepSameAction() { StepKey nextStep = new StepKey("current_phase", "current_action", "next_step"); long now = randomNonNegativeLong(); - ClusterState clusterState = buildClusterState(indexName, Settings.builder(), Collections.emptyList()); + ClusterState clusterState = buildClusterState(indexName, Settings.builder(), LifecycleExecutionState.builder().build(), + Collections.emptyList()); 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()); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(currentStep.getPhase()); + lifecycleState.setAction(currentStep.getAction()); + lifecycleState.setStep(currentStep.getName()); if (randomBoolean()) { - indexSettingsBuilder.put(LifecycleSettings.LIFECYCLE_STEP_INFO, randomAlphaOfLength(20)); + lifecycleState.setStepInfo(randomAlphaOfLength(20)); } - clusterState = buildClusterState(indexName, - indexSettingsBuilder, Collections.emptyList()); + clusterState = buildClusterState(indexName, Settings.builder(), lifecycleState.build(), Collections.emptyList()); index = clusterState.metaData().index(indexName).getIndex(); newClusterState = IndexLifecycleRunner.moveClusterStateToNextStep(index, clusterState, currentStep, nextStep, () -> now); assertClusterStateOnNextStep(clusterState, index, currentStep, nextStep, newClusterState, now); @@ -652,11 +643,13 @@ public void testSuccessfulValidatedMoveClusterStateToNextStep() { Step step = new MockStep(nextStepKey, nextStepKey); 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()); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(currentStepKey.getPhase()); + lifecycleState.setAction(currentStepKey.getAction()); + lifecycleState.setStep(currentStepKey.getName()); + + Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyName); + 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); @@ -672,11 +665,13 @@ public void testValidatedMoveClusterStateToNextStepWithoutPolicy() { Step step = new MockStep(nextStepKey, nextStepKey); 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()); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); + Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyName); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(currentStepKey.getPhase()); + lifecycleState.setAction(currentStepKey.getAction()); + lifecycleState.setStep(currentStepKey.getName()); + + ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), Collections.emptyList()); IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> IndexLifecycleRunner.moveClusterStateToStep(indexName, clusterState, currentStepKey, nextStepKey, () -> now, stepRegistry)); @@ -693,11 +688,13 @@ public void testValidatedMoveClusterStateToNextStepInvalidCurrentStep() { Step step = new MockStep(nextStepKey, nextStepKey); 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()); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); + Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyName); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(currentStepKey.getPhase()); + lifecycleState.setAction(currentStepKey.getAction()); + lifecycleState.setStep(currentStepKey.getName()); + + ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), Collections.emptyList()); IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> IndexLifecycleRunner.moveClusterStateToStep(indexName, clusterState, notCurrentStepKey, nextStepKey, () -> now, stepRegistry)); @@ -714,11 +711,13 @@ public void testValidatedMoveClusterStateToNextStepInvalidNextStep() { Step step = new MockStep(currentStepKey, nextStepKey); 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()); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); + Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyName); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(currentStepKey.getPhase()); + lifecycleState.setAction(currentStepKey.getAction()); + lifecycleState.setStep(currentStepKey.getName()); + + ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), Collections.emptyList()); IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> IndexLifecycleRunner.moveClusterStateToStep(indexName, clusterState, currentStepKey, nextStepKey, () -> now, stepRegistry)); @@ -733,11 +732,11 @@ public void testMoveClusterStateToErrorStep() throws IOException { long now = randomNonNegativeLong(); 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()), - Collections.emptyList()); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(currentStep.getPhase()); + lifecycleState.setAction(currentStep.getAction()); + lifecycleState.setStep(currentStep.getName()); + ClusterState clusterState = buildClusterState(indexName, Settings.builder(), lifecycleState.build(), Collections.emptyList()); Index index = clusterState.metaData().index(indexName).getIndex(); ClusterState newClusterState = IndexLifecycleRunner.moveClusterStateToErrorStep(index, clusterState, currentStep, cause, () -> now); @@ -760,12 +759,13 @@ public void testMoveClusterStateToFailedStep() { Step step = new MockStep(failedStepKey, null); 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_FAILED_STEP, failedStepKey.getName()) - .put(LifecycleSettings.LIFECYCLE_STEP, errorStepKey.getName()); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); + .put(LifecycleSettings.LIFECYCLE_NAME, policyName); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(errorStepKey.getPhase()); + lifecycleState.setAction(errorStepKey.getAction()); + lifecycleState.setStep(errorStepKey.getName()); + lifecycleState.setFailedStep(failedStepKey.getName()); + ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), Collections.emptyList()); Index index = clusterState.metaData().index(indexName).getIndex(); IndexLifecycleRunner runner = new IndexLifecycleRunner(policyRegistry, null, () -> now); ClusterState nextClusterState = runner.moveClusterStateToFailedStep(clusterState, indices); @@ -776,7 +776,8 @@ public void testMoveClusterStateToFailedStep() { public void testMoveClusterStateToFailedStepIndexNotFound() { String existingIndexName = "my_index"; String invalidIndexName = "does_not_exist"; - ClusterState clusterState = buildClusterState(existingIndexName, Settings.builder(), Collections.emptyList()); + ClusterState clusterState = buildClusterState(existingIndexName, Settings.builder(), LifecycleExecutionState.builder().build(), + Collections.emptyList()); IndexLifecycleRunner runner = new IndexLifecycleRunner(null, null, () -> 0L); IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> runner.moveClusterStateToFailedStep(clusterState, new String[] { invalidIndexName })); @@ -793,12 +794,13 @@ public void testMoveClusterStateToFailedStepInvalidPolicySetting() { Step step = new MockStep(failedStepKey, null); 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_FAILED_STEP, failedStepKey.getName()) - .put(LifecycleSettings.LIFECYCLE_STEP, errorStepKey.getName()); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); + .put(LifecycleSettings.LIFECYCLE_NAME, (String) null); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(errorStepKey.getPhase()); + lifecycleState.setAction(errorStepKey.getAction()); + lifecycleState.setStep(errorStepKey.getName()); + lifecycleState.setFailedStep(failedStepKey.getName()); + ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), Collections.emptyList()); IndexLifecycleRunner runner = new IndexLifecycleRunner(policyRegistry, null, () -> now); IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> runner.moveClusterStateToFailedStep(clusterState, indices)); @@ -814,11 +816,12 @@ public void testMoveClusterStateToFailedNotOnError() { Step step = new MockStep(failedStepKey, null); 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()); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); + .put(LifecycleSettings.LIFECYCLE_NAME, (String) null); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(failedStepKey.getPhase()); + lifecycleState.setAction(failedStepKey.getAction()); + lifecycleState.setStep(failedStepKey.getName()); + ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), Collections.emptyList()); IndexLifecycleRunner runner = new IndexLifecycleRunner(policyRegistry, null, () -> now); IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> runner.moveClusterStateToFailedStep(clusterState, indices)); @@ -831,11 +834,11 @@ public void testAddStepInfoToClusterState() throws IOException { StepKey currentStep = new StepKey("current_phase", "current_action", "current_step"); 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()), - Collections.emptyList()); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(currentStep.getPhase()); + lifecycleState.setAction(currentStep.getAction()); + lifecycleState.setStep(currentStep.getName()); + ClusterState clusterState = buildClusterState(indexName, Settings.builder(), lifecycleState.build(), Collections.emptyList()); Index index = clusterState.metaData().index(indexName).getIndex(); ClusterState newClusterState = IndexLifecycleRunner.addStepInfoToClusterState(index, clusterState, stepInfo); assertClusterStateStepInfo(clusterState, index, currentStep, newClusterState, stepInfo); @@ -847,13 +850,14 @@ public void testAddStepInfoToClusterState() throws IOException { public void testSkipped() { String policy = randomAlphaOfLength(5); String index = randomAlphaOfLength(10); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(randomAlphaOfLength(5)); + lifecycleState.setAction(randomAlphaOfLength(5)); + lifecycleState.setStep(randomAlphaOfLength(5)); 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_SKIP, true), - Collections.emptyList()); + Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy).put(LifecycleSettings.LIFECYCLE_SKIP, true), + lifecycleState.build(), + Collections.emptyList()); Step step = mock(randomFrom(TerminalPolicyStep.class, ClusterStateActionStep.class, ClusterStateWaitStep.class, AsyncActionStep.class, AsyncWaitStep.class)); PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policy, step); @@ -864,11 +868,14 @@ public void testSkipped() { } private ClusterState buildClusterState(String indexName, Settings.Builder indexSettingsBuilder, - List lifecyclePolicyMetadatas) { + LifecycleExecutionState lifecycleState, + List lifecyclePolicyMetadatas) { Settings indexSettings = indexSettingsBuilder.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build(); - IndexMetaData indexMetadata = IndexMetaData.builder(indexName).settings(indexSettings) - .build(); + IndexMetaData indexMetadata = IndexMetaData.builder(indexName) + .settings(indexSettings) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.asMap()) + .build(); Map lifecyclePolicyMetadatasMap = lifecyclePolicyMetadatas.stream() .collect(Collectors.toMap(LifecyclePolicyMetadata::getName, Function.identity())); @@ -890,15 +897,17 @@ 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_SKIP, true); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(currentStep.getPhase()); + lifecycleState.setAction(currentStep.getAction()); + lifecycleState.setStep(currentStep.getName()); List policyMetadatas = new ArrayList<>(); policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong())); policyMetadatas.add(new LifecyclePolicyMetadata(newPolicy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong())); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); + ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), policyMetadatas); Index index = clusterState.metaData().index(indexName).getIndex(); Index[] indices = new Index[] { index }; List failedIndexes = new ArrayList<>(); @@ -917,7 +926,8 @@ public void testSetPolicyForIndexNoCurrentPolicy() { LifecyclePolicy newPolicy = newTestLifecyclePolicy(newPolicyName, Collections.emptyMap()); StepKey currentStep = new StepKey("", "", ""); Settings.Builder indexSettingsBuilder = Settings.builder(); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); + ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, LifecycleExecutionState.builder().build(), + Collections.emptyList()); Index index = clusterState.metaData().index(indexName).getIndex(); Index[] indices = new Index[] { index }; List failedIndexes = new ArrayList<>(); @@ -938,13 +948,15 @@ 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_SKIP, true); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(currentStep.getPhase()); + lifecycleState.setAction(currentStep.getAction()); + lifecycleState.setStep(currentStep.getName()); List policyMetadatas = new ArrayList<>(); policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong())); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); + ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), policyMetadatas); Index index = new Index("doesnt_exist", "im_not_here"); Index[] indices = new Index[] { index }; List failedIndexes = new ArrayList<>(); @@ -988,13 +1000,15 @@ 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_SKIP, true); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(currentStep.getPhase()); + lifecycleState.setAction(currentStep.getAction()); + lifecycleState.setStep(currentStep.getName()); List policyMetadatas = new ArrayList<>(); policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong())); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); + ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), policyMetadatas); Index index = clusterState.metaData().index(indexName).getIndex(); Index[] indices = new Index[] { index }; List failedIndexes = new ArrayList<>(); @@ -1008,7 +1022,8 @@ public void testRemovePolicyForIndex() { public void testRemovePolicyForIndexNoCurrentPolicy() { String indexName = randomAlphaOfLength(10); Settings.Builder indexSettingsBuilder = Settings.builder(); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, Collections.emptyList()); + ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, LifecycleExecutionState.builder().build(), + Collections.emptyList()); Index index = clusterState.metaData().index(indexName).getIndex(); Index[] indices = new Index[] { index }; List failedIndexes = new ArrayList<>(); @@ -1025,13 +1040,15 @@ 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_SKIP, true); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(currentStep.getPhase()); + lifecycleState.setAction(currentStep.getAction()); + lifecycleState.setStep(currentStep.getName()); List policyMetadatas = new ArrayList<>(); policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong())); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); + ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), policyMetadatas); Index index = new Index("doesnt_exist", "im_not_here"); Index[] indices = new Index[] { index }; List failedIndexes = new ArrayList<>(); @@ -1049,13 +1066,15 @@ 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_SKIP, true); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(currentStep.getPhase()); + lifecycleState.setAction(currentStep.getAction()); + lifecycleState.setStep(currentStep.getName()); List policyMetadatas = new ArrayList<>(); policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong())); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); + ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), policyMetadatas); Index index = clusterState.metaData().index(indexName).getIndex(); Index[] indices = new Index[] { index }; List failedIndexes = new ArrayList<>(); @@ -1091,11 +1110,14 @@ public void testIsReadyToTransition() { // With no time, always transition assertTrue("index should be able to transition with no creation date", runner.isReadyToTransitionToThisPhase(policyName, indexMetaData, "phase")); + + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setIndexCreationDate(10L); indexMetaData = IndexMetaData.builder(indexMetaData) .settings(Settings.builder() .put(indexMetaData.getSettings()) - .put(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE, 10L) .build()) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) .build(); // Index is not old enough to transition assertFalse("index is not able to transition if it isn't old enough", @@ -1116,15 +1138,6 @@ 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_PHASE_TIME_SETTING.exists(indexSettings)); - assertFalse(LifecycleSettings.LIFECYCLE_ACTION_SETTING.exists(indexSettings)); - assertFalse(LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.exists(indexSettings)); - assertFalse(LifecycleSettings.LIFECYCLE_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)); - assertFalse(LifecycleSettings.LIFECYCLE_INDEX_CREATION_DATE_SETTING.exists(indexSettings)); assertFalse(LifecycleSettings.LIFECYCLE_SKIP_SETTING.exists(indexSettings)); assertFalse(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS_SETTING.exists(indexSettings)); } @@ -1136,31 +1149,31 @@ public static void assertClusterStateOnPolicy(ClusterState oldClusterState, Inde assertNotSame(oldClusterState.metaData(), newMetadata); IndexMetaData newIndexMetadata = newMetadata.getIndexSafe(index); 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)); - 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)); + LifecycleExecutionState newLifecycleState = LifecycleExecutionState + .fromIndexMetadata(newClusterState.metaData().index(index)); + LifecycleExecutionState oldLifecycleState = LifecycleExecutionState + .fromIndexMetadata(oldClusterState.metaData().index(index)); + assertNotSame(oldLifecycleState, newLifecycleState); + assertEquals(expectedStep.getPhase(), newLifecycleState.getPhase()); + assertEquals(expectedStep.getAction(), newLifecycleState.getAction()); + assertEquals(expectedStep.getName(), newLifecycleState.getStep()); + if (Objects.equals(previousStep.getPhase(), expectedStep.getPhase())) { + assertEquals(oldLifecycleState.getPhase(), newLifecycleState.getPhase()); } else { - assertEquals(now, (long) LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(newIndexSettings)); + assertEquals(now, newLifecycleState.getPhaseTime()); } - if (previousStep.getAction().equals(expectedStep.getAction())) { - assertEquals(LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.get(oldClusterState.metaData().index(index).getSettings()), - LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.get(newIndexSettings)); + if (Objects.equals(previousStep.getAction(), expectedStep.getAction())) { + assertEquals(oldLifecycleState.getActionTime(), newLifecycleState.getActionTime()); } else { - assertEquals(now, (long) LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.get(newIndexSettings)); + assertEquals(now, newLifecycleState.getActionTime()); } - if (previousStep.getName().equals(expectedStep.getName())) { - assertEquals(LifecycleSettings.LIFECYCLE_STEP_TIME_SETTING.get(oldClusterState.metaData().index(index).getSettings()), - LifecycleSettings.LIFECYCLE_STEP_TIME_SETTING.get(newIndexSettings)); + if (Objects.equals(previousStep.getName(), expectedStep.getName())) { + assertEquals(oldLifecycleState.getStepTime(), newLifecycleState.getStepTime()); } else { - assertEquals(now, (long) LifecycleSettings.LIFECYCLE_STEP_TIME_SETTING.get(newIndexSettings)); + assertEquals(now, newLifecycleState.getStepTime()); } - assertEquals("", LifecycleSettings.LIFECYCLE_FAILED_STEP_SETTING.get(newIndexSettings)); - assertEquals("", LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.get(newIndexSettings)); + assertEquals("", newLifecycleState.getFailedStep()); + assertEquals("", newLifecycleState.getStepInfo()); } public static void assertClusterStateOnNextStep(ClusterState oldClusterState, Index index, StepKey currentStep, StepKey nextStep, @@ -1170,26 +1183,27 @@ public static void assertClusterStateOnNextStep(ClusterState oldClusterState, In assertNotSame(oldClusterState.metaData(), newMetadata); IndexMetaData newIndexMetadata = newMetadata.getIndexSafe(index); 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)); + LifecycleExecutionState newLifecycleState = LifecycleExecutionState + .fromIndexMetadata(newClusterState.metaData().index(index)); + LifecycleExecutionState oldLifecycleState = LifecycleExecutionState + .fromIndexMetadata(oldClusterState.metaData().index(index)); + assertNotSame(oldLifecycleState, newLifecycleState); + assertEquals(nextStep.getPhase(), newLifecycleState.getPhase()); + assertEquals(nextStep.getAction(), newLifecycleState.getAction()); + assertEquals(nextStep.getName(), newLifecycleState.getStep()); 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)); + assertEquals(oldLifecycleState.getPhaseTime(), newLifecycleState.getPhaseTime()); } else { - assertEquals(now, (long) LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(newIndexSettings)); + assertEquals(now, newLifecycleState.getPhaseTime()); } if (currentStep.getAction().equals(nextStep.getAction())) { - assertEquals(LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.get(oldClusterState.metaData().index(index).getSettings()), - LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.get(newIndexSettings)); + assertEquals(oldLifecycleState.getActionTime(), newLifecycleState.getActionTime()); } else { - assertEquals(now, (long) LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.get(newIndexSettings)); + assertEquals(now, newLifecycleState.getActionTime()); } - assertEquals(now, (long) LifecycleSettings.LIFECYCLE_STEP_TIME_SETTING.get(newIndexSettings)); - assertEquals("", LifecycleSettings.LIFECYCLE_FAILED_STEP_SETTING.get(newIndexSettings)); - assertEquals("", LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.get(newIndexSettings)); + assertEquals(now, newLifecycleState.getStepTime()); + assertEquals("", newLifecycleState.getFailedStep()); + assertEquals("", newLifecycleState.getStepInfo()); } private void assertClusterStateOnErrorStep(ClusterState oldClusterState, Index index, StepKey currentStep, @@ -1199,18 +1213,19 @@ private void assertClusterStateOnErrorStep(ClusterState oldClusterState, Index i assertNotSame(oldClusterState.metaData(), newMetadata); IndexMetaData newIndexMetadata = newMetadata.getIndexSafe(index); 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.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()), - LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(newIndexSettings)); - assertEquals(LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.get(oldClusterState.metaData().index(index).getSettings()), - LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.get(newIndexSettings)); - assertEquals(now, (long) LifecycleSettings.LIFECYCLE_STEP_TIME_SETTING.get(newIndexSettings)); + LifecycleExecutionState newLifecycleState = LifecycleExecutionState + .fromIndexMetadata(newClusterState.metaData().index(index)); + LifecycleExecutionState oldLifecycleState = LifecycleExecutionState + .fromIndexMetadata(oldClusterState.metaData().index(index)); + assertNotSame(oldLifecycleState, newLifecycleState); + assertEquals(currentStep.getPhase(), newLifecycleState.getPhase()); + assertEquals(currentStep.getAction(), newLifecycleState.getAction()); + assertEquals(ErrorStep.NAME, newLifecycleState.getStep()); + assertEquals(currentStep.getName(), newLifecycleState.getFailedStep()); + assertEquals(expectedCauseValue, newLifecycleState.getStepInfo()); + assertEquals(oldLifecycleState.getPhaseTime(), newLifecycleState.getPhaseTime()); + assertEquals(oldLifecycleState.getActionTime(), newLifecycleState.getActionTime()); + assertEquals(now, newLifecycleState.getStepTime()); } private void assertClusterStateStepInfo(ClusterState oldClusterState, Index index, StepKey currentStep, ClusterState newClusterState, @@ -1223,18 +1238,18 @@ private void assertClusterStateStepInfo(ClusterState oldClusterState, Index inde assertNotSame(oldClusterState.metaData(), newMetadata); IndexMetaData newIndexMetadata = newMetadata.getIndexSafe(index); 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(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)); - assertEquals(LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.get(oldClusterState.metaData().index(index).getSettings()), - LifecycleSettings.LIFECYCLE_ACTION_TIME_SETTING.get(newIndexSettings)); - assertEquals(LifecycleSettings.LIFECYCLE_STEP_TIME_SETTING.get(oldClusterState.metaData().index(index).getSettings()), - LifecycleSettings.LIFECYCLE_STEP_TIME_SETTING.get(newIndexSettings)); + LifecycleExecutionState newLifecycleState = LifecycleExecutionState + .fromIndexMetadata(newClusterState.metaData().index(index)); + LifecycleExecutionState oldLifecycleState = LifecycleExecutionState + .fromIndexMetadata(oldClusterState.metaData().index(index)); + assertNotSame(oldLifecycleState, newLifecycleState); + assertEquals(currentStep.getPhase(), newLifecycleState.getPhase()); + assertEquals(currentStep.getAction(), newLifecycleState.getAction()); + assertEquals(currentStep.getName(), newLifecycleState.getStep()); + assertEquals(expectedstepInfoValue, newLifecycleState.getStepInfo()); + assertEquals(oldLifecycleState.getPhaseTime(), newLifecycleState.getPhaseTime()); + assertEquals(oldLifecycleState.getActionTime(), newLifecycleState.getActionTime()); + assertEquals(newLifecycleState.getStepTime(), newLifecycleState.getStepTime()); } private static class MockAsyncActionStep extends AsyncActionStep { 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 d7296ffc81622..a1ea3c2cd7f13 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 @@ -24,13 +24,14 @@ import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.Index; -import org.elasticsearch.xpack.core.indexlifecycle.OperationMode; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyMetadata; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings; import org.elasticsearch.xpack.core.indexlifecycle.MockAction; +import org.elasticsearch.xpack.core.indexlifecycle.OperationMode; import org.elasticsearch.xpack.core.indexlifecycle.Phase; import org.elasticsearch.xpack.core.indexlifecycle.ShrinkAction; import org.elasticsearch.xpack.core.indexlifecycle.Step; @@ -49,6 +50,7 @@ import static org.elasticsearch.node.Node.NODE_MASTER_SETTING; import static org.elasticsearch.xpack.core.indexlifecycle.AbstractStepTestCase.randomStepKey; +import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; import static org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyTestsUtils.newTestLifecyclePolicy; import static org.hamcrest.Matchers.equalTo; import static org.mockito.Matchers.any; @@ -148,11 +150,13 @@ public void testRequestedStopOnShrink() { policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong())); Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(mockShrinkStep.getPhase()); + lifecycleState.setAction(mockShrinkStep.getAction()); + lifecycleState.setStep(mockShrinkStep.getName()); 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())) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policyName)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); ImmutableOpenMap.Builder indices = ImmutableOpenMap. builder() .fPut(index.getName(), indexMetadata); @@ -189,11 +193,13 @@ public void testRequestedStopOnSafeAction() { policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong())); Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase(currentStepKey.getPhase()); + lifecycleState.setAction(currentStepKey.getAction()); + lifecycleState.setStep(currentStepKey.getName()); 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())) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policyName)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) .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 40844d0f93d19..f9670e340c398 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 @@ -19,6 +19,7 @@ import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.indexlifecycle.ErrorStep; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyMetadata; @@ -31,6 +32,7 @@ import java.io.IOException; import java.util.Collections; +import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.sameInstance; @@ -70,21 +72,20 @@ public void testExecuteSuccessfullyMoved() throws IOException { MoveToErrorStepUpdateTask task = new MoveToErrorStepUpdateTask(index, policy, currentStepKey, cause, () -> now); ClusterState newState = task.execute(clusterState); - StepKey actualKey = IndexLifecycleRunner.getCurrentStepKey(newState.metaData().index(index).getSettings()); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(newState.getMetaData().index(index)); + StepKey actualKey = IndexLifecycleRunner.getCurrentStepKey(lifecycleState); 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_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)); + assertThat(lifecycleState.getFailedStep(), equalTo(currentStepKey.getName())); + assertThat(lifecycleState.getPhaseTime(), equalTo(-1L)); + assertThat(lifecycleState.getActionTime(), equalTo(-1L)); + assertThat(lifecycleState.getStepTime(), equalTo(now)); XContentBuilder causeXContentBuilder = JsonXContent.contentBuilder(); causeXContentBuilder.startObject(); ElasticsearchException.generateThrowableXContent(causeXContentBuilder, ToXContent.EMPTY_PARAMS, cause); causeXContentBuilder.endObject(); String expectedCauseValue = BytesReference.bytes(causeXContentBuilder).utf8ToString(); - assertThat(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.get(newState.metaData().index(index).getSettings()), - equalTo(expectedCauseValue)); + assertThat(lifecycleState.getStepInfo(), equalTo(expectedCauseValue)); } public void testExecuteNoopDifferentStep() throws IOException { @@ -133,11 +134,15 @@ private void setStatePolicy(String policy) { } private void setStateToKey(StepKey stepKey) { + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder( + LifecycleExecutionState.fromIndexMetadata(clusterState.metaData().index(index))); + lifecycleState.setPhase(stepKey.getPhase()); + lifecycleState.setAction(stepKey.getAction()); + lifecycleState.setStep(stepKey.getName()); + 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(); + .metaData(MetaData.builder(clusterState.getMetaData()) + .put(IndexMetaData.builder(clusterState.getMetaData().index(index)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()))).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 727b957c34953..16c4e332177be 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 @@ -15,6 +15,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyMetadata; @@ -28,6 +29,7 @@ import java.util.Collections; import java.util.List; +import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; import static org.hamcrest.Matchers.equalTo; public class MoveToNextStepUpdateTaskTests extends ESTestCase { @@ -68,11 +70,12 @@ public void testExecuteSuccessfullyMoved() { MoveToNextStepUpdateTask task = new MoveToNextStepUpdateTask(index, policy, currentStepKey, nextStepKey, () -> now); ClusterState newState = task.execute(clusterState); - StepKey actualKey = IndexLifecycleRunner.getCurrentStepKey(newState.metaData().index(index).getSettings()); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(newState.getMetaData().index(index)); + StepKey actualKey = IndexLifecycleRunner.getCurrentStepKey(lifecycleState); assertThat(actualKey, equalTo(nextStepKey)); - 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(now)); - assertThat(LifecycleSettings.LIFECYCLE_STEP_TIME_SETTING.get(newState.metaData().index(index).getSettings()), equalTo(now)); + assertThat(lifecycleState.getPhaseTime(), equalTo(now)); + assertThat(lifecycleState.getActionTime(), equalTo(now)); + assertThat(lifecycleState.getStepTime(), equalTo(now)); task.clusterStateProcessed("source", clusterState, newState); } @@ -107,11 +110,12 @@ public void testExecuteSuccessfulMoveWithInvalidNextStep() { SetOnce changed = new SetOnce<>(); MoveToNextStepUpdateTask task = new MoveToNextStepUpdateTask(index, policy, currentStepKey, invalidNextStep, () -> now); ClusterState newState = task.execute(clusterState); - StepKey actualKey = IndexLifecycleRunner.getCurrentStepKey(newState.metaData().index(index).getSettings()); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(newState.getMetaData().index(index)); + StepKey actualKey = IndexLifecycleRunner.getCurrentStepKey(lifecycleState); assertThat(actualKey, equalTo(invalidNextStep)); - 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(now)); - assertThat(LifecycleSettings.LIFECYCLE_STEP_TIME_SETTING.get(newState.metaData().index(index).getSettings()), equalTo(now)); + assertThat(lifecycleState.getPhaseTime(), equalTo(now)); + assertThat(lifecycleState.getActionTime(), equalTo(now)); + assertThat(lifecycleState.getStepTime(), equalTo(now)); task.clusterStateProcessed("source", clusterState, newState); } @@ -139,15 +143,18 @@ private void setStatePolicy(String policy) { } private void setStateToKey(StepKey stepKey, long now) { + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder( + LifecycleExecutionState.fromIndexMetadata(clusterState.metaData().index(index))); + lifecycleState.setPhase(stepKey.getPhase()); + lifecycleState.setPhaseTime(now); + lifecycleState.setAction(stepKey.getAction()); + lifecycleState.setActionTime(now); + lifecycleState.setStep(stepKey.getName()); + lifecycleState.setStepTime(now); + lifecycleState.setPhaseDefinition("{\"actions\":{\"TEST_ACTION\":{}}}"); clusterState = ClusterState.builder(clusterState) - .metaData(MetaData.builder(clusterState.metaData()) - .updateSettings(Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PHASE_DEFINITION, "{\"actions\":{\"TEST_ACTION\":{}}}") - .put(LifecycleSettings.LIFECYCLE_PHASE, stepKey.getPhase()) - .put(LifecycleSettings.LIFECYCLE_PHASE_TIME, now) - .put(LifecycleSettings.LIFECYCLE_ACTION, stepKey.getAction()) - .put(LifecycleSettings.LIFECYCLE_ACTION_TIME, now) - .put(LifecycleSettings.LIFECYCLE_STEP, stepKey.getName()) - .put(LifecycleSettings.LIFECYCLE_STEP_TIME, now).build(), index.getName())).build(); + .metaData(MetaData.builder(clusterState.getMetaData()) + .put(IndexMetaData.builder(clusterState.getMetaData().index(index)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()))).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 3bfba50ccb62b..5ffcfbe38a31b 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 @@ -23,9 +23,9 @@ import org.elasticsearch.common.xcontent.json.JsonXContent; 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.ErrorStep; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleAction; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; @@ -33,6 +33,7 @@ import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyTests; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings; 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.ShrinkAction; import org.elasticsearch.xpack.core.indexlifecycle.ShrinkStep; @@ -44,6 +45,7 @@ import java.util.List; import java.util.Map; +import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.sameInstance; @@ -118,6 +120,8 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { Map policyMap = Collections.singletonMap(newPolicy.getName(), new LifecyclePolicyMetadata(newPolicy, headers, randomNonNegativeLong(), randomNonNegativeLong())); IndexLifecycleMetadata lifecycleMetadata = new IndexLifecycleMetadata(policyMap, OperationMode.RUNNING); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase("new"); MetaData metaData = MetaData.builder() .persistentSettings(settings(Version.CURRENT).build()) .putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata) @@ -127,8 +131,8 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { .put("index.number_of_shards", 1) .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_NAME, policyName)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap())) .build(); try (XContentBuilder builder = JsonXContent.contentBuilder()) { builder.startObject(); @@ -160,11 +164,13 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { Map registeredStepsForPolicy = registry.getStepMap().get(newPolicy.getName()); assertThat(registeredStepsForPolicy.size(), equalTo(policySteps.size())); for (Step step : policySteps) { + LifecycleExecutionState.Builder newIndexState = LifecycleExecutionState.builder(); + newIndexState.setPhase(step.getKey().getPhase()); currentState = ClusterState.builder(currentState) .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())))) + .settings(Settings.builder().put(currentState.metaData().index("test").getSettings())) + .putCustom(ILM_CUSTOM_METADATA_KEY, newIndexState.build().asMap()))) .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) .build(); registry.update(currentState); @@ -259,6 +265,8 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except Map policyMap = Collections.singletonMap(newPolicy.getName(), new LifecyclePolicyMetadata(newPolicy, headers, randomNonNegativeLong(), randomNonNegativeLong())); IndexLifecycleMetadata lifecycleMetadata = new IndexLifecycleMetadata(policyMap, OperationMode.RUNNING); + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setPhase("warm"); MetaData metaData = MetaData.builder() .persistentSettings(settings(Version.CURRENT).build()) .putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata) @@ -268,8 +276,8 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except .put("index.number_of_shards", 1) .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_NAME, policyName)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap())) .build(); try (XContentBuilder builder = JsonXContent.contentBuilder()) { builder.startObject(); 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..8b8e45e0954fd 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 @@ -20,12 +20,14 @@ import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings; import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey; import org.junit.Before; import java.io.IOException; +import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.sameInstance; @@ -57,17 +59,17 @@ public void testExecuteSuccessfullySet() throws IOException { SetStepInfoUpdateTask task = new SetStepInfoUpdateTask(index, policy, currentStepKey, stepInfo); ClusterState newState = task.execute(clusterState); - StepKey actualKey = IndexLifecycleRunner.getCurrentStepKey(newState.metaData().index(index).getSettings()); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(newState.getMetaData().index(index)); + StepKey actualKey = IndexLifecycleRunner.getCurrentStepKey(lifecycleState); assertThat(actualKey, equalTo(currentStepKey)); - assertThat(LifecycleSettings.LIFECYCLE_PHASE_TIME_SETTING.get(newState.metaData().index(index).getSettings()), equalTo(-1L)); - 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(-1L)); + assertThat(lifecycleState.getPhaseTime(), equalTo(-1L)); + assertThat(lifecycleState.getActionTime(), equalTo(-1L)); + assertThat(lifecycleState.getStepTime(), equalTo(-1L)); XContentBuilder infoXContentBuilder = JsonXContent.contentBuilder(); stepInfo.toXContent(infoXContentBuilder, ToXContent.EMPTY_PARAMS); String expectedCauseValue = BytesReference.bytes(infoXContentBuilder).utf8ToString(); - assertThat(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.get(newState.metaData().index(index).getSettings()), - equalTo(expectedCauseValue)); + assertThat(lifecycleState.getStepInfo(), equalTo(expectedCauseValue)); } private ToXContentObject getRandomStepInfo() { @@ -124,11 +126,15 @@ private void setStatePolicy(String policy) { } private void setStateToKey(StepKey stepKey) { + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder( + LifecycleExecutionState.fromIndexMetadata(clusterState.metaData().index(index))); + lifecycleState.setPhase(stepKey.getPhase()); + lifecycleState.setAction(stepKey.getAction()); + lifecycleState.setStep(stepKey.getName()); + 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(); + .metaData(MetaData.builder(clusterState.getMetaData()) + .put(IndexMetaData.builder(clusterState.getMetaData().index(index)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()))).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..14ae060c89d9a 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 @@ -9,11 +9,14 @@ import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.elasticsearch.client.Request; +import org.elasticsearch.client.Response; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xpack.core.indexlifecycle.AllocateAction; @@ -30,6 +33,7 @@ import org.junit.Before; import java.io.IOException; +import java.io.InputStream; import java.util.Collections; import java.util.List; import java.util.Locale; @@ -75,8 +79,7 @@ public void testAllocateOnlyAllocation() throws Exception { createNewSingletonPolicy(randomFrom("warm", "cold"), allocateAction); updatePolicy(index, policy); assertBusy(() -> { - Map settings = getOnlyIndexSettings(index); - assertThat(getStepKey(settings), equalTo(TerminalPolicyStep.KEY)); + assertThat(getStepKeyForIndex(index), equalTo(TerminalPolicyStep.KEY)); }); ensureGreen(index); } @@ -92,7 +95,7 @@ public void testAllocateActionOnlyReplicas() throws Exception { updatePolicy(index, policy); assertBusy(() -> { Map settings = getOnlyIndexSettings(index); - assertThat(getStepKey(settings), equalTo(TerminalPolicyStep.KEY)); + assertThat(getStepKeyForIndex(index), equalTo(TerminalPolicyStep.KEY)); assertThat(settings.get(IndexMetaData.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey()), equalTo(String.valueOf(finalNumReplicas))); }); } @@ -112,7 +115,7 @@ public void testReadOnly() throws Exception { updatePolicy(index, policy); assertBusy(() -> { Map settings = getOnlyIndexSettings(index); - assertThat(getStepKey(settings), equalTo(TerminalPolicyStep.KEY)); + assertThat(getStepKeyForIndex(index), equalTo(TerminalPolicyStep.KEY)); assertThat(settings.get(IndexMetaData.INDEX_BLOCKS_WRITE_SETTING.getKey()), equalTo("true")); }); } @@ -146,7 +149,7 @@ public void testForceMergeAction() throws Exception { updatePolicy(index, policy); assertBusy(() -> { - assertThat(getStepKey(getOnlyIndexSettings(index)), equalTo(TerminalPolicyStep.KEY)); + assertThat(getStepKeyForIndex(index), equalTo(TerminalPolicyStep.KEY)); assertThat(numSegments.get(), equalTo(1)); }); } @@ -164,7 +167,7 @@ public void testShrinkAction() throws Exception { assertTrue(indexExists(shrunkenIndex)); assertTrue(aliasExists(shrunkenIndex, index)); Map settings = getOnlyIndexSettings(shrunkenIndex); - assertThat(getStepKey(settings), equalTo(TerminalPolicyStep.KEY)); + assertThat(getStepKeyForIndex(shrunkenIndex), equalTo(TerminalPolicyStep.KEY)); assertThat(settings.get(IndexMetaData.SETTING_NUMBER_OF_SHARDS), equalTo(String.valueOf(expectedFinalShards))); }); } @@ -198,10 +201,22 @@ private Map getOnlyIndexSettings(String index) throws IOExceptio return (Map) response.get("settings"); } - 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); + private StepKey getStepKeyForIndex(String indexName) throws IOException { + Request explainRequest = new Request("GET", indexName + "/_ilm/explain"); + Response response = client().performRequest(explainRequest); + Map responseMap; + try (InputStream is = response.getEntity().getContent()) { + responseMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), is, true); + } + + @SuppressWarnings("unchecked") Map indexResponse = ((Map>) responseMap.get("indices")) + .get(indexName); + if (indexResponse == null) { + return new StepKey(null, null, null); + } + String phase = indexResponse.get("phase"); + String action = indexResponse.get("action"); + String step = indexResponse.get("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 94c21926e2387..f102a801c81b5 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 @@ -84,13 +84,13 @@ teardown: name: "forcemerge" - do: - acknowledge: true - 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" } + acknowledge: true + ilm.explain_lifecycle: + index: "my_index" + - match: { indices.my_index.policy: "my_moveable_timeseries_lifecycle" } + - match: { indices.my_index.step: "forcemerge" } + - match: { indices.my_index.action: "forcemerge" } + - match: { indices.my_index.phase: "warm" } --- "Test Invalid Move To Step With Incorrect Current Step": @@ -111,14 +111,15 @@ teardown: - match: { error.root_cause.0.type: "illegal_argument_exception" } - match: { error.root_cause.0.reason: "index [my_index] is not on current step [{\"phase\":\"warm\",\"action\":\"forcemerge\",\"name\":\"forcemerge\"}]" } + - do: acknowledge: true - indices.get: - index: my_index - - match: { my_index.settings.index.lifecycle.name: "my_moveable_timeseries_lifecycle" } - - match: { my_index.settings.index.lifecycle.step: "complete" } - - match: { my_index.settings.index.lifecycle.action: "complete" } - - match: { my_index.settings.index.lifecycle.phase: "new" } + ilm.explain_lifecycle: + index: "my_index" + - match: { indices.my_index.policy: "my_moveable_timeseries_lifecycle" } + - match: { indices.my_index.step: "complete" } + - match: { indices.my_index.action: "complete" } + - match: { indices.my_index.phase: "new" } --- "Test Invalid Move To Step With Invalid Next Step": @@ -141,12 +142,12 @@ teardown: - do: acknowledge: true - indices.get: - index: my_index - - match: { my_index.settings.index.lifecycle.name: "my_moveable_timeseries_lifecycle" } - - match: { my_index.settings.index.lifecycle.step: "complete" } - - match: { my_index.settings.index.lifecycle.action: "complete" } - - match: { my_index.settings.index.lifecycle.phase: "new" } + ilm.explain_lifecycle: + index: "my_index" + - match: { indices.my_index.policy: "my_moveable_timeseries_lifecycle" } + - match: { indices.my_index.step: "complete" } + - match: { indices.my_index.action: "complete" } + - match: { indices.my_index.phase: "new" } --- "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 6e0e0095480a3..0ffcc74a70efb 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 @@ -70,12 +70,12 @@ teardown: - do: acknowledge: true - indices.get: - index: my_index - - match: { my_index.settings.index.lifecycle.name: "my_lifecycle" } - - match: { my_index.settings.index.lifecycle.step: "complete" } - - match: { my_index.settings.index.lifecycle.action: "complete" } - - match: { my_index.settings.index.lifecycle.phase: "new" } + ilm.explain_lifecycle: + index: "my_index" + - match: { indices.my_index.policy: "my_lifecycle" } + - match: { indices.my_index.step: "complete" } + - match: { indices.my_index.action: "complete" } + - match: { indices.my_index.phase: "new" } --- From 327165fc768012328a05a3b008501dddb0736d80 Mon Sep 17 00:00:00 2001 From: Gordon Brown Date: Mon, 17 Sep 2018 15:55:17 -0600 Subject: [PATCH 2/5] Changes per review in #33783 --- .../cluster/metadata/IndexMetaData.java | 5 + .../InitializePolicyContextStep.java | 2 +- .../LifecycleExecutionState.java | 170 ++++++++++++------ .../LifecycleExecutionStateTests.java | 109 ++++++++++- .../indexlifecycle/IndexLifecycleRunner.java | 20 +-- .../TransportExplainLifecycleAction.java | 18 +- .../ExecuteStepsUpdateTaskTests.java | 23 +-- .../IndexLifecycleRunnerTests.java | 36 ++-- .../MoveToErrorStepUpdateTaskTests.java | 5 +- .../SetStepInfoUpdateTaskTests.java | 7 +- 10 files changed, 279 insertions(+), 116 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java index c1e7019141789..3c5ec2f965e53 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java @@ -955,6 +955,11 @@ public Builder putCustom(String type, Map customIndexMetaData) { return this; } + public Builder removeCustom(String type) { + this.customMetaData.remove(type); + return this; + } + public Set getInSyncAllocationIds(int shardId) { return inSyncAllocationIds.get(shardId); } 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 7934c484fdcec..6bbc2921e22ce 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 @@ -33,7 +33,7 @@ public ClusterState performAction(Index index, ClusterState clusterState) { } LifecycleExecutionState lifecycleState = LifecycleExecutionState .fromIndexMetadata(indexMetaData); - if (lifecycleState.getIndexCreationDate() != -1) { + if (lifecycleState.getIndexCreationDate() != null) { return clusterState; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionState.java index 77910b198573f..e570500b431d1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionState.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.core.indexlifecycle; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.cluster.metadata.IndexMetaData; import java.util.Collections; @@ -13,6 +14,11 @@ import java.util.Map; import java.util.Objects; +/** + * Contains information about the execution of a lifecycle policy for a single + * index, and serializes/deserializes this information to and from custom + * index metadata. + */ public class LifecycleExecutionState { public static final String ILM_CUSTOM_METADATA_KEY = "ilm"; @@ -33,14 +39,14 @@ public class LifecycleExecutionState { private final String failedStep; private final String stepInfo; private final String phaseDefinition; - private final long indexCreationDate; - private final long phaseTime; - private final long actionTime; - private final long stepTime; + private final Long indexCreationDate; + private final Long phaseTime; + private final Long actionTime; + private final Long stepTime; private LifecycleExecutionState(String phase, String action, String step, String failedStep, - String stepInfo, String phaseDefinition, long indexCreationDate, - long phaseTime, long actionTime, long stepTime) { + String stepInfo, String phaseDefinition, Long indexCreationDate, + Long phaseTime, Long actionTime, Long stepTime) { this.phase = phase; this.action = action; this.step = step; @@ -85,18 +91,58 @@ public static Builder builder(LifecycleExecutionState state) { } static LifecycleExecutionState fromCustomMetadata(Map customData) { - return new Builder() - .setPhase(customData.getOrDefault(PHASE, "")) - .setAction(customData.getOrDefault(ACTION, "")) - .setStep(customData.getOrDefault(STEP, "")) - .setFailedStep(customData.getOrDefault(FAILED_STEP, "")) - .setStepInfo(customData.getOrDefault(STEP_INFO, "")) - .setPhaseDefinition(customData.getOrDefault(PHASE_DEFINITION, "")) - .setIndexCreationDate(Long.parseLong(customData.getOrDefault(INDEX_CREATION_DATE, "-1"))) - .setPhaseTime(Long.parseLong(customData.getOrDefault(PHASE_TIME, "-1"))) - .setActionTime(Long.parseLong(customData.getOrDefault(ACTION_TIME, "-1"))) - .setStepTime(Long.parseLong(customData.getOrDefault(STEP_TIME, "-1"))) - .build(); + Builder builder = builder(); + if (customData.containsKey(PHASE)) { + builder.setPhase(customData.get(PHASE)); + } + if (customData.containsKey(ACTION)) { + builder.setAction(customData.get(ACTION)); + } + if (customData.containsKey(STEP)) { + builder.setStep(customData.get(STEP)); + } + if (customData.containsKey(FAILED_STEP)) { + builder.setFailedStep(customData.get(FAILED_STEP)); + } + if (customData.containsKey(STEP_INFO)) { + builder.setStepInfo(customData.get(STEP_INFO)); + } + if (customData.containsKey(PHASE_DEFINITION)) { + builder.setPhaseDefinition(customData.get(PHASE_DEFINITION)); + } + if (customData.containsKey(INDEX_CREATION_DATE)) { + try { + builder.setIndexCreationDate(Long.parseLong(customData.get(INDEX_CREATION_DATE))); + } catch (NumberFormatException e) { + throw new ElasticsearchException("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]", + e, INDEX_CREATION_DATE, customData.get(INDEX_CREATION_DATE)); + } + } + if (customData.containsKey(PHASE_TIME)) { + try { + builder.setPhaseTime(Long.parseLong(customData.get(PHASE_TIME))); + } catch (NumberFormatException e) { + throw new ElasticsearchException("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]", + e, PHASE_TIME, customData.get(PHASE_TIME)); + } + } + if (customData.containsKey(ACTION_TIME)) { + try { + builder.setActionTime(Long.parseLong(customData.get(ACTION_TIME))); + } catch (NumberFormatException e) { + throw new ElasticsearchException("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]", + e, ACTION_TIME, customData.get(ACTION_TIME)); + } + } + if (customData.containsKey(STEP_TIME)) { + try { + builder.setStepTime(Long.parseLong(customData.get(STEP_TIME))); + } catch (NumberFormatException e) { + throw new ElasticsearchException("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]", + e, STEP_TIME, customData.get(STEP_TIME)); + } + } + return builder.build(); } /** @@ -106,16 +152,36 @@ static LifecycleExecutionState fromCustomMetadata(Map customData */ public Map asMap() { Map result = new HashMap<>(); - result.put(PHASE, phase); - result.put(ACTION, action); - result.put(STEP, step); - result.put(FAILED_STEP, failedStep); - result.put(STEP_INFO, stepInfo); - result.put(INDEX_CREATION_DATE, String.valueOf(indexCreationDate)); - result.put(PHASE_TIME, String.valueOf(phaseTime)); - result.put(ACTION_TIME, String.valueOf(actionTime)); - result.put(STEP_TIME, String.valueOf(stepTime)); - result.put(PHASE_DEFINITION, String.valueOf(phaseDefinition)); + if (phase != null) { + result.put(PHASE, phase); + } + if (action != null) { + result.put(ACTION, action); + } + if (step != null) { + result.put(STEP, step); + } + if (failedStep != null) { + result.put(FAILED_STEP, failedStep); + } + if (stepInfo != null) { + result.put(STEP_INFO, stepInfo); + } + if (indexCreationDate != null) { + result.put(INDEX_CREATION_DATE, String.valueOf(indexCreationDate)); + } + if (phaseTime != null) { + result.put(PHASE_TIME, String.valueOf(phaseTime)); + } + if (actionTime != null) { + result.put(ACTION_TIME, String.valueOf(actionTime)); + } + if (stepTime != null) { + result.put(STEP_TIME, String.valueOf(stepTime)); + } + if (phaseDefinition != null) { + result.put(PHASE_DEFINITION, String.valueOf(phaseDefinition)); + } return Collections.unmodifiableMap(result); } @@ -143,19 +209,19 @@ public String getPhaseDefinition() { return phaseDefinition; } - public long getIndexCreationDate() { + public Long getIndexCreationDate() { return indexCreationDate; } - public long getPhaseTime() { + public Long getPhaseTime() { return phaseTime; } - public long getActionTime() { + public Long getActionTime() { return actionTime; } - public long getStepTime() { + public Long getStepTime() { return stepTime; } @@ -183,63 +249,63 @@ public int hashCode() { } public static class Builder { - private String phase = ""; - private String action = ""; - private String step = ""; - private String failedStep = ""; - private String stepInfo = ""; - private String phaseDefinition = ""; - private long indexCreationDate = -1; - private long phaseTime = -1; - private long actionTime = -1; - private long stepTime = -1; + private String phase; + private String action; + private String step; + private String failedStep; + private String stepInfo; + private String phaseDefinition; + private Long indexCreationDate; + private Long phaseTime; + private Long actionTime; + private Long stepTime; public Builder setPhase(String phase) { - this.phase = Objects.requireNonNull(phase); + this.phase = phase; return this; } public Builder setAction(String action) { - this.action = Objects.requireNonNull(action); + this.action = action; return this; } public Builder setStep(String step) { - this.step = Objects.requireNonNull(step); + this.step = step; return this; } public Builder setFailedStep(String failedStep) { - this.failedStep = Objects.requireNonNull(failedStep); + this.failedStep = failedStep; return this; } public Builder setStepInfo(String stepInfo) { - this.stepInfo = Objects.requireNonNull(stepInfo); + this.stepInfo = stepInfo; return this; } public Builder setPhaseDefinition(String phaseDefinition) { - this.phaseDefinition = Objects.requireNonNull(phaseDefinition); + this.phaseDefinition = phaseDefinition; return this; } - public Builder setIndexCreationDate(long indexCreationDate) { + public Builder setIndexCreationDate(Long indexCreationDate) { this.indexCreationDate = indexCreationDate; return this; } - public Builder setPhaseTime(long phaseTime) { + public Builder setPhaseTime(Long phaseTime) { this.phaseTime = phaseTime; return this; } - public Builder setActionTime(long actionTime) { + public Builder setActionTime(Long actionTime) { this.actionTime = actionTime; return this; } - public Builder setStepTime(long stepTime) { + public Builder setStepTime(Long stepTime) { this.stepTime = stepTime; return this; } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionStateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionStateTests.java index 689dbeb3981d7..b979d5e276539 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionStateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionStateTests.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.core.indexlifecycle; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.util.HashMap; import java.util.Map; @@ -19,14 +20,106 @@ public void testConversion() { assertEquals(customMetadata, parsed.asMap()); } - public void testInputValidation() { - LifecycleExecutionState.Builder test = LifecycleExecutionState.builder(); - expectThrows(NullPointerException.class, () -> test.setPhase(null)); - expectThrows(NullPointerException.class, () -> test.setAction(null)); - expectThrows(NullPointerException.class, () -> test.setStep(null)); - expectThrows(NullPointerException.class, () -> test.setFailedStep(null)); - expectThrows(NullPointerException.class, () -> test.setStepInfo(null)); - expectThrows(NullPointerException.class, () -> test.setPhaseDefinition(null)); + public void testEmptyValuesAreNotSerialized() { + LifecycleExecutionState empty = LifecycleExecutionState.builder().build(); + assertEquals(new HashMap().entrySet(), empty.asMap().entrySet()); + + Map originalMap = createCustomMetadata(); + LifecycleExecutionState originalState = LifecycleExecutionState.fromCustomMetadata(originalMap); + LifecycleExecutionState.Builder newState = LifecycleExecutionState.builder(originalState); + newState.setPhase(null); + assertFalse(newState.build().asMap().containsKey("phase")); + + newState = LifecycleExecutionState.builder(originalState); + newState.setAction(null); + assertFalse(newState.build().asMap().containsKey("action")); + + newState = LifecycleExecutionState.builder(originalState); + newState.setStep(null); + assertFalse(newState.build().asMap().containsKey("step")); + + newState = LifecycleExecutionState.builder(originalState); + newState.setFailedStep(null); + assertFalse(newState.build().asMap().containsKey("failed_step")); + + newState = LifecycleExecutionState.builder(originalState); + newState.setPhaseDefinition(null); + assertFalse(newState.build().asMap().containsKey("phase_definition")); + + newState = LifecycleExecutionState.builder(originalState); + newState.setStepInfo(null); + assertFalse(newState.build().asMap().containsKey("step_info")); + + newState = LifecycleExecutionState.builder(originalState); + newState.setPhaseTime(null); + assertFalse(newState.build().asMap().containsKey("phase_time")); + + newState = LifecycleExecutionState.builder(originalState); + newState.setActionTime(null); + assertFalse(newState.build().asMap().containsKey("action_time")); + + newState = LifecycleExecutionState.builder(originalState); + newState.setIndexCreationDate(null); + assertFalse(newState.build().asMap().containsKey("creation_date")); + } + + public void testEqualsAndHashcode() { + LifecycleExecutionState original = LifecycleExecutionState.fromCustomMetadata(createCustomMetadata()); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + original, + toCopy -> LifecycleExecutionState.builder(toCopy).build(), + LifecycleExecutionStateTests::mutate); + } + + 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; + } + 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.getIndexCreationDate(), ESTestCase::randomLong)); + changed = true; + } + + if (changed == false) { + return LifecycleExecutionState.builder().build(); + } + + return newState.build(); } static Map createCustomMetadata() { 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 a1d015122af59..30900dff5ef43 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 @@ -27,8 +27,8 @@ import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateActionStep; import org.elasticsearch.xpack.core.indexlifecycle.ClusterStateWaitStep; import org.elasticsearch.xpack.core.indexlifecycle.ErrorStep; -import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata; +import org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings; import org.elasticsearch.xpack.core.indexlifecycle.Phase; @@ -39,9 +39,7 @@ import org.elasticsearch.xpack.core.indexlifecycle.TerminalPolicyStep; import java.io.IOException; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.function.LongSupplier; import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; @@ -63,12 +61,12 @@ public IndexLifecycleRunner(PolicyStepsRegistry stepRegistry, ClusterService clu */ boolean isReadyToTransitionToThisPhase(final String policy, final IndexMetaData indexMetaData, final String phase) { LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetaData); - if (lifecycleState.getIndexCreationDate() == -1L) { + if (lifecycleState.getIndexCreationDate() == null) { logger.trace("no index creation date has been set yet"); return true; } - final long lifecycleDate = lifecycleState.getIndexCreationDate(); - assert lifecycleDate >= 0 : "expected index to have a lifecycle date but it did not"; + final Long lifecycleDate = lifecycleState.getIndexCreationDate(); + assert lifecycleDate != null && 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); @@ -313,8 +311,8 @@ private static LifecycleExecutionState moveExecutionStateToNextStep(LifecyclePol updatedState.setStepTime(nowAsMillis); // clear any step info or error-related settings from the current step - updatedState.setFailedStep(""); - updatedState.setStepInfo(""); + updatedState.setFailedStep(null); + updatedState.setStepInfo(null); if (currentStep.getPhase().equals(nextStep.getPhase()) == false) { final String newPhaseDefinition; @@ -323,7 +321,7 @@ private static LifecycleExecutionState moveExecutionStateToNextStep(LifecyclePol } else { Phase nextPhase = policy.getPhases().get(nextStep.getPhase()); if (nextPhase == null) { - newPhaseDefinition = ""; + newPhaseDefinition = null; } else { newPhaseDefinition = Strings.toString(nextPhase, false, false); } @@ -476,10 +474,8 @@ private static IndexMetaData.Builder removePolicyForIndex(IndexMetaData indexMet newSettings.remove(LifecycleSettings.LIFECYCLE_SKIP_SETTING.getKey()); newSettings.remove(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS_SETTING.getKey()); - Map newCustomData = new HashMap<>(); - return IndexMetaData.builder(indexMetadata) - .putCustom(ILM_CUSTOM_METADATA_KEY, newCustomData) + .removeCustom(ILM_CUSTOM_METADATA_KEY) .settings(newSettings); } } 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 4d78953163fcf..80800e5bdfd8f 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 @@ -71,15 +71,15 @@ protected void doMasterOperation(ExplainLifecycleRequest request, String[] concr if (Strings.hasLength(policyName)) { indexResponse = IndexLifecycleExplainResponse.newManagedIndexResponse(index, policyName, LifecycleSettings.LIFECYCLE_SKIP_SETTING.get(idxSettings), - lifecycleState.getIndexCreationDate(), - lifecycleState.getPhase(), - lifecycleState.getAction(), - lifecycleState.getStep(), - lifecycleState.getFailedStep(), - lifecycleState.getPhaseTime(), - lifecycleState.getActionTime(), - lifecycleState.getStepTime(), - new BytesArray(lifecycleState.getStepInfo())); + lifecycleState.getIndexCreationDate() == null ? -1 : lifecycleState.getIndexCreationDate(), + lifecycleState.getPhase() == null ? "" : lifecycleState.getPhase(), + lifecycleState.getAction() == null ? "" : lifecycleState.getAction(), + lifecycleState.getStep() == null ? "" : lifecycleState.getStep(), + lifecycleState.getFailedStep() == null ? "" : lifecycleState.getFailedStep(), + lifecycleState.getPhaseTime() == null ? -1 : lifecycleState.getPhaseTime(), + lifecycleState.getActionTime() == null ? -1 : lifecycleState.getActionTime(), + lifecycleState.getStepTime() == null ? -1 : lifecycleState.getStepTime(), + new BytesArray(lifecycleState.getStepInfo() == null ? "" : lifecycleState.getStepInfo())); } else { indexResponse = IndexLifecycleExplainResponse.newUnmanagedIndexResponse(index); } 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 240e6192ed878..dbf81cbd4ccfc 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 @@ -48,6 +48,7 @@ import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; import static org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyTestsUtils.newTestLifecyclePolicy; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.sameInstance; public class ExecuteStepsUpdateTaskTests extends ESTestCase { @@ -156,18 +157,18 @@ public void testExecuteUntilFirstNonClusterStateStep() throws IOException { assertThat(currentStepKey, equalTo(thirdStepKey)); assertThat(firstStep.getExecuteCount(), equalTo(0L)); assertThat(secondStep.getExecuteCount(), equalTo(1L)); - assertThat(lifecycleState.getPhaseTime(), equalTo(-1L)); - assertThat(lifecycleState.getActionTime(), equalTo(-1L)); - assertThat(lifecycleState.getStepInfo(), equalTo("")); + assertThat(lifecycleState.getPhaseTime(), nullValue()); + assertThat(lifecycleState.getActionTime(), nullValue()); + assertThat(lifecycleState.getStepInfo(), nullValue()); } public void testExecuteInvalidStartStep() throws IOException { // Unset the index's phase/action/step to simulate starting from scratch LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder( LifecycleExecutionState.fromIndexMetadata(clusterState.getMetaData().index(index))); - lifecycleState.setPhase(""); - lifecycleState.setAction(""); - lifecycleState.setStep(""); + lifecycleState.setPhase(null); + lifecycleState.setAction(null); + lifecycleState.setStep(null); clusterState = ClusterState.builder(clusterState) .metaData(MetaData.builder(clusterState.getMetaData()) .put(IndexMetaData.builder(clusterState.getMetaData().index(index)) @@ -194,9 +195,9 @@ public void testExecuteIncompleteWaitStepNoInfo() throws IOException { assertThat(currentStepKey, equalTo(secondStepKey)); assertThat(firstStep.getExecuteCount(), equalTo(0L)); assertThat(secondStep.getExecuteCount(), equalTo(1L)); - assertThat(lifecycleState.getPhaseTime(), equalTo(-1L)); - assertThat(lifecycleState.getActionTime(), equalTo(-1L)); - assertThat(lifecycleState.getStepInfo(), equalTo("")); + assertThat(lifecycleState.getPhaseTime(), nullValue()); + assertThat(lifecycleState.getActionTime(), nullValue()); + assertThat(lifecycleState.getStepInfo(), nullValue()); } public void testExecuteIncompleteWaitStepWithInfo() throws IOException { @@ -213,8 +214,8 @@ public void testExecuteIncompleteWaitStepWithInfo() throws IOException { assertThat(currentStepKey, equalTo(secondStepKey)); assertThat(firstStep.getExecuteCount(), equalTo(0L)); assertThat(secondStep.getExecuteCount(), equalTo(1L)); - assertThat(lifecycleState.getPhaseTime(), equalTo(-1L)); - assertThat(lifecycleState.getActionTime(), equalTo(-1L)); + assertThat(lifecycleState.getPhaseTime(), nullValue()); + assertThat(lifecycleState.getActionTime(), nullValue()); assertThat(lifecycleState.getStepInfo(), equalTo(stepInfo.toString())); } 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 57fc164414608..3f252c222d05c 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 @@ -394,7 +394,7 @@ public void testGetCurrentStepKey() { phase = randomAlphaOfLength(20); action = randomAlphaOfLength(20); - step = ""; + step = null; LifecycleExecutionState.Builder lifecycleState3 = LifecycleExecutionState.builder(); lifecycleState3.setPhase(phase); lifecycleState3.setAction(action); @@ -402,9 +402,9 @@ public void testGetCurrentStepKey() { AssertionError error3 = expectThrows(AssertionError.class, () -> IndexLifecycleRunner.getCurrentStepKey(lifecycleState3.build())); assertEquals("Current phase is not empty: " + phase, error3.getMessage()); - phase = ""; + phase = null; action = randomAlphaOfLength(20); - step = ""; + step = null; LifecycleExecutionState.Builder lifecycleState4 = LifecycleExecutionState.builder(); lifecycleState4.setPhase(phase); lifecycleState4.setAction(action); @@ -412,7 +412,7 @@ public void testGetCurrentStepKey() { AssertionError error4 = expectThrows(AssertionError.class, () -> IndexLifecycleRunner.getCurrentStepKey(lifecycleState4.build())); assertEquals("Current action is not empty: " + action, error4.getMessage()); - phase = ""; + phase = null; action = randomAlphaOfLength(20); step = randomAlphaOfLength(20); LifecycleExecutionState.Builder lifecycleState5 = LifecycleExecutionState.builder(); @@ -422,8 +422,8 @@ public void testGetCurrentStepKey() { AssertionError error5 = expectThrows(AssertionError.class, () -> IndexLifecycleRunner.getCurrentStepKey(lifecycleState5.build())); assertEquals(null, error5.getMessage()); - phase = ""; - action = ""; + phase = null; + action = null; step = randomAlphaOfLength(20); LifecycleExecutionState.Builder lifecycleState6 = LifecycleExecutionState.builder(); lifecycleState6.setPhase(phase); @@ -924,7 +924,7 @@ public void testSetPolicyForIndexNoCurrentPolicy() { String indexName = randomAlphaOfLength(10); String newPolicyName = "new_policy"; LifecyclePolicy newPolicy = newTestLifecyclePolicy(newPolicyName, Collections.emptyMap()); - StepKey currentStep = new StepKey("", "", ""); + StepKey currentStep = new StepKey(null, null, null); Settings.Builder indexSettingsBuilder = Settings.builder(); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, LifecycleExecutionState.builder().build(), Collections.emptyList()); @@ -1160,20 +1160,20 @@ public static void assertClusterStateOnPolicy(ClusterState oldClusterState, Inde if (Objects.equals(previousStep.getPhase(), expectedStep.getPhase())) { assertEquals(oldLifecycleState.getPhase(), newLifecycleState.getPhase()); } else { - assertEquals(now, newLifecycleState.getPhaseTime()); + assertEquals(now, newLifecycleState.getPhaseTime().longValue()); } if (Objects.equals(previousStep.getAction(), expectedStep.getAction())) { assertEquals(oldLifecycleState.getActionTime(), newLifecycleState.getActionTime()); } else { - assertEquals(now, newLifecycleState.getActionTime()); + assertEquals(now, newLifecycleState.getActionTime().longValue()); } if (Objects.equals(previousStep.getName(), expectedStep.getName())) { assertEquals(oldLifecycleState.getStepTime(), newLifecycleState.getStepTime()); } else { - assertEquals(now, newLifecycleState.getStepTime()); + assertEquals(now, newLifecycleState.getStepTime().longValue()); } - assertEquals("", newLifecycleState.getFailedStep()); - assertEquals("", newLifecycleState.getStepInfo()); + assertEquals(null, newLifecycleState.getFailedStep()); + assertEquals(null, newLifecycleState.getStepInfo()); } public static void assertClusterStateOnNextStep(ClusterState oldClusterState, Index index, StepKey currentStep, StepKey nextStep, @@ -1194,16 +1194,16 @@ public static void assertClusterStateOnNextStep(ClusterState oldClusterState, In if (currentStep.getPhase().equals(nextStep.getPhase())) { assertEquals(oldLifecycleState.getPhaseTime(), newLifecycleState.getPhaseTime()); } else { - assertEquals(now, newLifecycleState.getPhaseTime()); + assertEquals(now, newLifecycleState.getPhaseTime().longValue()); } if (currentStep.getAction().equals(nextStep.getAction())) { assertEquals(oldLifecycleState.getActionTime(), newLifecycleState.getActionTime()); } else { - assertEquals(now, newLifecycleState.getActionTime()); + assertEquals(now, newLifecycleState.getActionTime().longValue()); } - assertEquals(now, newLifecycleState.getStepTime()); - assertEquals("", newLifecycleState.getFailedStep()); - assertEquals("", newLifecycleState.getStepInfo()); + assertEquals(now, newLifecycleState.getStepTime().longValue()); + assertEquals(null, newLifecycleState.getFailedStep()); + assertEquals(null, newLifecycleState.getStepInfo()); } private void assertClusterStateOnErrorStep(ClusterState oldClusterState, Index index, StepKey currentStep, @@ -1225,7 +1225,7 @@ private void assertClusterStateOnErrorStep(ClusterState oldClusterState, Index i assertEquals(expectedCauseValue, newLifecycleState.getStepInfo()); assertEquals(oldLifecycleState.getPhaseTime(), newLifecycleState.getPhaseTime()); assertEquals(oldLifecycleState.getActionTime(), newLifecycleState.getActionTime()); - assertEquals(now, newLifecycleState.getStepTime()); + assertEquals(now, newLifecycleState.getStepTime().longValue()); } private void assertClusterStateStepInfo(ClusterState oldClusterState, Index index, StepKey currentStep, ClusterState newClusterState, 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 f9670e340c398..dc3a6602f39ba 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 @@ -34,6 +34,7 @@ import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.sameInstance; public class MoveToErrorStepUpdateTaskTests extends ESTestCase { @@ -76,8 +77,8 @@ public void testExecuteSuccessfullyMoved() throws IOException { StepKey actualKey = IndexLifecycleRunner.getCurrentStepKey(lifecycleState); assertThat(actualKey, equalTo(new StepKey(currentStepKey.getPhase(), currentStepKey.getAction(), ErrorStep.NAME))); assertThat(lifecycleState.getFailedStep(), equalTo(currentStepKey.getName())); - assertThat(lifecycleState.getPhaseTime(), equalTo(-1L)); - assertThat(lifecycleState.getActionTime(), equalTo(-1L)); + assertThat(lifecycleState.getPhaseTime(), nullValue()); + assertThat(lifecycleState.getActionTime(), nullValue()); assertThat(lifecycleState.getStepTime(), equalTo(now)); XContentBuilder causeXContentBuilder = JsonXContent.contentBuilder(); 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 8b8e45e0954fd..a8b16d3ecfdf9 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 @@ -29,6 +29,7 @@ import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.sameInstance; public class SetStepInfoUpdateTaskTests extends ESTestCase { @@ -62,9 +63,9 @@ public void testExecuteSuccessfullySet() throws IOException { LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(newState.getMetaData().index(index)); StepKey actualKey = IndexLifecycleRunner.getCurrentStepKey(lifecycleState); assertThat(actualKey, equalTo(currentStepKey)); - assertThat(lifecycleState.getPhaseTime(), equalTo(-1L)); - assertThat(lifecycleState.getActionTime(), equalTo(-1L)); - assertThat(lifecycleState.getStepTime(), equalTo(-1L)); + assertThat(lifecycleState.getPhaseTime(), nullValue()); + assertThat(lifecycleState.getActionTime(), nullValue()); + assertThat(lifecycleState.getStepTime(), nullValue()); XContentBuilder infoXContentBuilder = JsonXContent.contentBuilder(); stepInfo.toXContent(infoXContentBuilder, ToXContent.EMPTY_PARAMS); From e4d0bb435a94015bef4fc06c94f064c0c25d6c7d Mon Sep 17 00:00:00 2001 From: Gordon Brown Date: Mon, 17 Sep 2018 17:33:15 -0600 Subject: [PATCH 3/5] More review changes --- ...ionState.java => CopyExecutionStateStep.java} | 12 ++++++++---- .../xpack/core/indexlifecycle/ShrinkAction.java | 6 +++--- .../xpack/core/indexlifecycle/ShrinkStep.java | 7 ++++++- ...sts.java => CopyExecutionStateStepTests.java} | 16 ++++++++-------- .../core/indexlifecycle/ShrinkActionTests.java | 6 +++--- 5 files changed, 28 insertions(+), 19 deletions(-) rename x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/{ShrinkCopyExecutionState.java => CopyExecutionStateStep.java} (85%) rename x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/{ShrinkCopyExecutionStateTests.java => CopyExecutionStateStepTests.java} (83%) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkCopyExecutionState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStep.java similarity index 85% rename from x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkCopyExecutionState.java rename to x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStep.java index 7501cfe4bce64..58afee8b60012 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkCopyExecutionState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStep.java @@ -15,11 +15,15 @@ import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; -public class ShrinkCopyExecutionState extends ClusterStateActionStep { - public static final String NAME = "copy_metadata"; +/** + * Copies the execution state data from one index to another, typically after a + * new index has been created. Useful for actions such as shrink. + */ +public class CopyExecutionStateStep extends ClusterStateActionStep { + public static final String NAME = "copy_execution_state"; private String shrunkIndexPrefix; - public ShrinkCopyExecutionState(StepKey key, StepKey nextStepKey, String shrunkIndexPrefix) { + public CopyExecutionStateStep(StepKey key, StepKey nextStepKey, String shrunkIndexPrefix) { super(key, nextStepKey); this.shrunkIndexPrefix = shrunkIndexPrefix; } @@ -60,7 +64,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (!super.equals(o)) return false; - ShrinkCopyExecutionState that = (ShrinkCopyExecutionState) o; + CopyExecutionStateStep that = (CopyExecutionStateStep) o; return Objects.equals(shrunkIndexPrefix, that.shrunkIndexPrefix); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkAction.java index 249e62cfe10fe..cda0312360686 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkAction.java @@ -85,7 +85,7 @@ public List toSteps(Client client, String phase, Step.StepKey nextStepKey) StepKey allocationRoutedKey = new StepKey(phase, NAME, AllocationRoutedStep.NAME); StepKey shrinkKey = new StepKey(phase, NAME, ShrinkStep.NAME); StepKey enoughShardsKey = new StepKey(phase, NAME, ShrunkShardsAllocatedStep.NAME); - StepKey copyMetadataKey = new StepKey(phase, NAME, ShrinkCopyExecutionState.NAME); + StepKey copyMetadataKey = new StepKey(phase, NAME, CopyExecutionStateStep.NAME); StepKey aliasKey = new StepKey(phase, NAME, ShrinkSetAliasStep.NAME); StepKey isShrunkIndexKey = new StepKey(phase, NAME, ShrunkenIndexCheckStep.NAME); @@ -93,7 +93,7 @@ public List toSteps(Client client, String phase, Step.StepKey nextStepKey) AllocationRoutedStep allocationStep = new AllocationRoutedStep(allocationRoutedKey, shrinkKey, false); ShrinkStep shrink = new ShrinkStep(shrinkKey, enoughShardsKey, client, numberOfShards, SHRUNKEN_INDEX_PREFIX); ShrunkShardsAllocatedStep allocated = new ShrunkShardsAllocatedStep(enoughShardsKey, copyMetadataKey, SHRUNKEN_INDEX_PREFIX); - ShrinkCopyExecutionState copyMetadata = new ShrinkCopyExecutionState(copyMetadataKey, aliasKey, SHRUNKEN_INDEX_PREFIX); + CopyExecutionStateStep copyMetadata = new CopyExecutionStateStep(copyMetadataKey, aliasKey, SHRUNKEN_INDEX_PREFIX); ShrinkSetAliasStep aliasSwapAndDelete = new ShrinkSetAliasStep(aliasKey, isShrunkIndexKey, client, SHRUNKEN_INDEX_PREFIX); ShrunkenIndexCheckStep waitOnShrinkTakeover = new ShrunkenIndexCheckStep(isShrunkIndexKey, nextStepKey, SHRUNKEN_INDEX_PREFIX); return Arrays.asList(setSingleNodeStep, allocationStep, shrink, allocated, copyMetadata, aliasSwapAndDelete, waitOnShrinkTakeover); @@ -105,7 +105,7 @@ public List toStepKeys(String phase) { StepKey allocationRoutedKey = new StepKey(phase, NAME, AllocationRoutedStep.NAME); StepKey shrinkKey = new StepKey(phase, NAME, ShrinkStep.NAME); StepKey enoughShardsKey = new StepKey(phase, NAME, ShrunkShardsAllocatedStep.NAME); - StepKey copyMetadataKey = new StepKey(phase, NAME, ShrinkCopyExecutionState.NAME); + StepKey copyMetadataKey = new StepKey(phase, NAME, CopyExecutionStateStep.NAME); StepKey aliasKey = new StepKey(phase, NAME, ShrinkSetAliasStep.NAME); StepKey isShrunkIndexKey = new StepKey(phase, NAME, ShrunkenIndexCheckStep.NAME); return Arrays.asList(setSingleNodeKey, allocationRoutedKey, shrinkKey, enoughShardsKey, 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 930bc06131fa4..5bf9b7d674def 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 @@ -37,7 +37,12 @@ String getShrunkIndexPrefix() { @Override public void performAction(IndexMetaData indexMetaData, ClusterState currentState, Listener listener) { - // if operating on the shrunken index, do nothing + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetaData); + if (lifecycleState == null) { + throw new IllegalStateException("source index [" + indexMetaData.getIndex().getName() + + "] is missing creation date"); + } + String lifecycle = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetaData.getSettings()); Settings relevantTargetSettings = Settings.builder() diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkCopyExecutionStateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStepTests.java similarity index 83% rename from x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkCopyExecutionStateTests.java rename to x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStepTests.java index 79b5967f316c2..b03871b166eba 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkCopyExecutionStateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStepTests.java @@ -18,17 +18,17 @@ import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionStateTests.createCustomMetadata; -public class ShrinkCopyExecutionStateTests extends AbstractStepTestCase { +public class CopyExecutionStateStepTests extends AbstractStepTestCase { @Override - protected ShrinkCopyExecutionState createRandomInstance() { + protected CopyExecutionStateStep createRandomInstance() { StepKey stepKey = randomStepKey(); StepKey nextStepKey = randomStepKey(); String shrunkIndexPrefix = randomAlphaOfLength(10); - return new ShrinkCopyExecutionState(stepKey, nextStepKey, shrunkIndexPrefix); + return new CopyExecutionStateStep(stepKey, nextStepKey, shrunkIndexPrefix); } @Override - protected ShrinkCopyExecutionState mutateInstance(ShrinkCopyExecutionState instance) { + protected CopyExecutionStateStep mutateInstance(CopyExecutionStateStep instance) { StepKey key = instance.getKey(); StepKey nextKey = instance.getNextStepKey(); String shrunkIndexPrefix = instance.getShrunkIndexPrefix(); @@ -47,16 +47,16 @@ protected ShrinkCopyExecutionState mutateInstance(ShrinkCopyExecutionState insta throw new AssertionError("Illegal randomisation branch"); } - return new ShrinkCopyExecutionState(key, nextKey, shrunkIndexPrefix); + return new CopyExecutionStateStep(key, nextKey, shrunkIndexPrefix); } @Override - protected ShrinkCopyExecutionState copyInstance(ShrinkCopyExecutionState instance) { - return new ShrinkCopyExecutionState(instance.getKey(), instance.getNextStepKey(), instance.getShrunkIndexPrefix()); + protected CopyExecutionStateStep copyInstance(CopyExecutionStateStep instance) { + return new CopyExecutionStateStep(instance.getKey(), instance.getNextStepKey(), instance.getShrunkIndexPrefix()); } public void testPerformAction() { - ShrinkCopyExecutionState step = createRandomInstance(); + CopyExecutionStateStep step = createRandomInstance(); String indexName = randomAlphaOfLengthBetween(5, 20); Map customMetadata = createCustomMetadata(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkActionTests.java index 2dd7ee624881b..4409de2a59d4a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/ShrinkActionTests.java @@ -56,7 +56,7 @@ public void testToSteps() { StepKey expectedSecondKey = new StepKey(phase, ShrinkAction.NAME, AllocationRoutedStep.NAME); StepKey expectedThirdKey = new StepKey(phase, ShrinkAction.NAME, ShrinkStep.NAME); StepKey expectedFourthKey = new StepKey(phase, ShrinkAction.NAME, ShrunkShardsAllocatedStep.NAME); - StepKey expectedFifthKey = new StepKey(phase, ShrinkAction.NAME, ShrinkCopyExecutionState.NAME); + StepKey expectedFifthKey = new StepKey(phase, ShrinkAction.NAME, CopyExecutionStateStep.NAME); StepKey expectedSixthKey = new StepKey(phase, ShrinkAction.NAME, ShrinkSetAliasStep.NAME); StepKey expectedSeventhKey = new StepKey(phase, ShrinkAction.NAME, ShrunkenIndexCheckStep.NAME); assertTrue(steps.get(0) instanceof SetSingleNodeAllocateStep); @@ -75,10 +75,10 @@ public void testToSteps() { assertThat(steps.get(3).getKey(), equalTo(expectedFourthKey)); assertThat(steps.get(3).getNextStepKey(), equalTo(expectedFifthKey)); assertThat(((ShrunkShardsAllocatedStep) steps.get(3)).getShrunkIndexPrefix(), equalTo(ShrinkAction.SHRUNKEN_INDEX_PREFIX)); - assertTrue(steps.get(4) instanceof ShrinkCopyExecutionState); + assertTrue(steps.get(4) instanceof CopyExecutionStateStep); assertThat(steps.get(4).getKey(), equalTo(expectedFifthKey)); assertThat(steps.get(4).getNextStepKey(), equalTo(expectedSixthKey)); - assertThat(((ShrinkCopyExecutionState) steps.get(4)).getShrunkIndexPrefix(), equalTo(ShrinkAction.SHRUNKEN_INDEX_PREFIX)); + assertThat(((CopyExecutionStateStep) steps.get(4)).getShrunkIndexPrefix(), equalTo(ShrinkAction.SHRUNKEN_INDEX_PREFIX)); assertTrue(steps.get(5) instanceof ShrinkSetAliasStep); assertThat(steps.get(5).getKey(), equalTo(expectedSixthKey)); assertThat(steps.get(5).getNextStepKey(), equalTo(expectedSeventhKey)); From 4330513c35109fb3964c67edec3b6d4fb20afc6a Mon Sep 17 00:00:00 2001 From: Gordon Brown Date: Tue, 18 Sep 2018 16:28:48 -0600 Subject: [PATCH 4/5] Finish moving to nullable data instead of defaults --- .../IndexLifecycleExplainResponse.java | 76 +++++++++---------- .../IndexExplainResponseTests.java | 8 +- .../TransportExplainLifecycleAction.java | 23 +++--- 3 files changed, 56 insertions(+), 51 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/IndexLifecycleExplainResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/IndexLifecycleExplainResponse.java index b110697ae07ad..0a451b5c14b51 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/IndexLifecycleExplainResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/IndexLifecycleExplainResponse.java @@ -48,14 +48,14 @@ public class IndexLifecycleExplainResponse implements ToXContentObject, Writeabl (boolean) a[1], (String) a[2], (boolean) (a[3] == null ? false: a[3]), - (long) (a[4] == null ? -1L: a[4]), + (Long) (a[4]), (String) a[5], (String) a[6], (String) a[7], (String) a[8], - (long) (a[9] == null ? -1L: a[9]), - (long) (a[10] == null ? -1L: a[10]), - (long) (a[11] == null ? -1L: a[11]), + (Long) (a[9]), + (Long) (a[10]), + (Long) (a[11]), (BytesReference) a[12], (PhaseExecutionInfo) a[13])); static { @@ -86,36 +86,36 @@ public class IndexLifecycleExplainResponse implements ToXContentObject, Writeabl private final String action; private final String step; private final String failedStep; - private final long lifecycleDate; - private final long phaseTime; - private final long actionTime; - private final long stepTime; + private final Long lifecycleDate; + private final Long phaseTime; + private final Long actionTime; + private final Long stepTime; private final boolean skip; private final boolean managedByILM; private final BytesReference stepInfo; private final PhaseExecutionInfo phaseExecutionInfo; - public static IndexLifecycleExplainResponse newManagedIndexResponse(String index, String policyName, boolean skip, long lifecycleDate, - String phase, String action, String step, String failedStep, long phaseTime, long actionTime, long stepTime, + public static IndexLifecycleExplainResponse newManagedIndexResponse(String index, String policyName, boolean skip, Long lifecycleDate, + String phase, String action, String step, String failedStep, Long phaseTime, Long actionTime, Long stepTime, BytesReference stepInfo, PhaseExecutionInfo phaseExecutionInfo) { return new IndexLifecycleExplainResponse(index, true, policyName, skip, lifecycleDate, phase, action, step, failedStep, phaseTime, actionTime, stepTime, stepInfo, phaseExecutionInfo); } public static IndexLifecycleExplainResponse newUnmanagedIndexResponse(String index) { - return new IndexLifecycleExplainResponse(index, false, null, false, -1L, null, null, null, null, -1L, -1L, -1L, null, null); + return new IndexLifecycleExplainResponse(index, false, null, false, null, null, null, null, null, null, null, null, null, null); } - private IndexLifecycleExplainResponse(String index, boolean managedByILM, String policyName, boolean skip, long lifecycleDate, - String phase, String action, String step, String failedStep, long phaseTime, long actionTime, - long stepTime, BytesReference stepInfo, PhaseExecutionInfo phaseExecutionInfo) { + private IndexLifecycleExplainResponse(String index, boolean managedByILM, String policyName, boolean skip, Long lifecycleDate, + String phase, String action, String step, String failedStep, Long phaseTime, Long actionTime, + Long stepTime, BytesReference stepInfo, PhaseExecutionInfo phaseExecutionInfo) { if (managedByILM) { if (policyName == null) { throw new IllegalArgumentException("[" + POLICY_NAME_FIELD.getPreferredName() + "] cannot be null for managed index"); } } else { - if (policyName != null || lifecycleDate >= 0 || phase != null || action != null || step != null || failedStep != null - || phaseTime >= 0 || actionTime >= 0 || stepTime >= 0 || stepInfo != null || phaseExecutionInfo != null) { + if (policyName != null || lifecycleDate != null || phase != null || action != null || step != null || failedStep != null + || phaseTime != null || actionTime != null || stepTime != null || stepInfo != null || phaseExecutionInfo != null) { throw new IllegalArgumentException( "Unmanaged index response must only contain fields: [" + MANAGED_BY_ILM_FIELD + ", " + INDEX_FIELD + "]"); } @@ -142,27 +142,27 @@ public IndexLifecycleExplainResponse(StreamInput in) throws IOException { if (managedByILM) { policyName = in.readString(); skip = in.readBoolean(); - lifecycleDate = in.readZLong(); - phase = in.readString(); - action = in.readString(); - step = in.readString(); + lifecycleDate = in.readOptionalLong(); + phase = in.readOptionalString(); + action = in.readOptionalString(); + step = in.readOptionalString(); failedStep = in.readOptionalString(); - phaseTime = in.readZLong(); - actionTime = in.readZLong(); - stepTime = in.readZLong(); + phaseTime = in.readOptionalLong(); + actionTime = in.readOptionalLong(); + stepTime = in.readOptionalLong(); stepInfo = in.readOptionalBytesReference(); phaseExecutionInfo = in.readOptionalWriteable(PhaseExecutionInfo::new); } else { policyName = null; skip = false; - lifecycleDate = -1L; + lifecycleDate = null; phase = null; action = null; step = null; failedStep = null; - phaseTime = -1L; - actionTime = -1L; - stepTime = -1L; + phaseTime = null; + actionTime = null; + stepTime = null; stepInfo = null; phaseExecutionInfo = null; } @@ -175,14 +175,14 @@ public void writeTo(StreamOutput out) throws IOException { if (managedByILM) { out.writeString(policyName); out.writeBoolean(skip); - out.writeZLong(lifecycleDate); - out.writeString(phase); - out.writeString(action); - out.writeString(step); + out.writeOptionalLong(lifecycleDate); + out.writeOptionalString(phase); + out.writeOptionalString(action); + out.writeOptionalString(step); out.writeOptionalString(failedStep); - out.writeZLong(phaseTime); - out.writeZLong(actionTime); - out.writeZLong(stepTime); + out.writeOptionalLong(phaseTime); + out.writeOptionalLong(actionTime); + out.writeOptionalLong(stepTime); out.writeOptionalBytesReference(stepInfo); out.writeOptionalWriteable(phaseExecutionInfo); } @@ -204,7 +204,7 @@ public boolean skip() { return skip; } - public long getLifecycleDate() { + public Long getLifecycleDate() { return lifecycleDate; } @@ -212,7 +212,7 @@ public String getPhase() { return phase; } - public long getPhaseTime() { + public Long getPhaseTime() { return phaseTime; } @@ -220,7 +220,7 @@ public String getAction() { return action; } - public long getActionTime() { + public Long getActionTime() { return actionTime; } @@ -228,7 +228,7 @@ public String getStep() { return step; } - public long getStepTime() { + public Long getStepTime() { return stepTime; } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/IndexExplainResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/IndexExplainResponseTests.java index f05def36c4f40..1c9be6daacffe 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/IndexExplainResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/IndexExplainResponseTests.java @@ -71,10 +71,10 @@ protected IndexLifecycleExplainResponse mutateInstance(IndexLifecycleExplainResp String action = instance.getAction(); String step = instance.getStep(); String failedStep = instance.getFailedStep(); - long policyTime = instance.getLifecycleDate(); - long phaseTime = instance.getPhaseTime(); - long actionTime = instance.getActionTime(); - long stepTime = instance.getStepTime(); + Long policyTime = instance.getLifecycleDate(); + Long phaseTime = instance.getPhaseTime(); + Long actionTime = instance.getActionTime(); + Long stepTime = instance.getStepTime(); boolean managed = instance.managedByILM(); boolean skip = instance.skip(); BytesReference stepInfo = instance.getStepInfo(); 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 ef0554dd51512..4981391f8791a 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 @@ -80,6 +80,11 @@ protected void doMasterOperation(ExplainLifecycleRequest request, String[] concr LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(idxMetadata); String policyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(idxSettings); String currentPhase = lifecycleState.getPhase(); + String stepInfo = lifecycleState.getStepInfo(); + BytesArray stepInfoBytes = null; + if (stepInfo != null) { + stepInfoBytes = new BytesArray(stepInfo); + } // parse existing phase steps from the phase definition in the index settings String phaseDef = lifecycleState.getPhaseDefinition(); PhaseExecutionInfo phaseExecutionInfo = null; @@ -97,15 +102,15 @@ protected void doMasterOperation(ExplainLifecycleRequest request, String[] concr if (Strings.hasLength(policyName)) { indexResponse = IndexLifecycleExplainResponse.newManagedIndexResponse(index, policyName, LifecycleSettings.LIFECYCLE_SKIP_SETTING.get(idxSettings), - lifecycleState.getIndexCreationDate() == null ? -1 : lifecycleState.getIndexCreationDate(), - lifecycleState.getPhase() == null ? "" : lifecycleState.getPhase(), - lifecycleState.getAction() == null ? "" : lifecycleState.getAction(), - lifecycleState.getStep() == null ? "" : lifecycleState.getStep(), - lifecycleState.getFailedStep() == null ? "" : lifecycleState.getFailedStep(), - lifecycleState.getPhaseTime() == null ? -1 : lifecycleState.getPhaseTime(), - lifecycleState.getActionTime() == null ? -1 : lifecycleState.getActionTime(), - lifecycleState.getStepTime() == null ? -1 : lifecycleState.getStepTime(), - new BytesArray(lifecycleState.getStepInfo() == null ? "" : lifecycleState.getStepInfo()), + lifecycleState.getIndexCreationDate(), + lifecycleState.getPhase(), + lifecycleState.getAction(), + lifecycleState.getStep(), + lifecycleState.getFailedStep(), + lifecycleState.getPhaseTime(), + lifecycleState.getActionTime(), + lifecycleState.getStepTime(), + stepInfoBytes, phaseExecutionInfo); } else { indexResponse = IndexLifecycleExplainResponse.newUnmanagedIndexResponse(index); From 382d59e81dc409fd9ed020015de177f1bd1538fd Mon Sep 17 00:00:00 2001 From: Gordon Brown Date: Wed, 19 Sep 2018 10:01:09 -0600 Subject: [PATCH 5/5] More review changes --- .../CopyExecutionStateStep.java | 2 +- .../InitializePolicyContextStep.java | 2 +- .../LifecycleExecutionState.java | 20 +++++++++---------- .../xpack/core/indexlifecycle/ShrinkStep.java | 4 ++-- .../CopyExecutionStateStepTests.java | 2 +- .../InitializePolicyContextStepTests.java | 2 +- .../LifecycleExecutionStateTests.java | 2 +- .../core/indexlifecycle/ShrinkStepTests.java | 11 ++++++++-- .../UpdateRolloverLifecycleDateStepTests.java | 2 +- .../indexlifecycle/IndexLifecycleRunner.java | 4 ++-- .../TransportExplainLifecycleAction.java | 4 ++-- 11 files changed, 31 insertions(+), 24 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStep.java index 58afee8b60012..b3bd4235fae71 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStep.java @@ -44,7 +44,7 @@ public ClusterState performAction(Index index, ClusterState clusterState) { LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetaData); String phase = lifecycleState.getPhase(); String action = lifecycleState.getAction(); - long lifecycleDate = lifecycleState.getIndexCreationDate(); + long lifecycleDate = lifecycleState.getLifecycleDate(); LifecycleExecutionState.Builder relevantTargetCustomData = LifecycleExecutionState.builder(); relevantTargetCustomData.setIndexCreationDate(lifecycleDate); 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 6bbc2921e22ce..c9046cb5eb7ec 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 @@ -33,7 +33,7 @@ public ClusterState performAction(Index index, ClusterState clusterState) { } LifecycleExecutionState lifecycleState = LifecycleExecutionState .fromIndexMetadata(indexMetaData); - if (lifecycleState.getIndexCreationDate() != null) { + if (lifecycleState.getLifecycleDate() != null) { return clusterState; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionState.java index e570500b431d1..b2d42bca7338e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionState.java @@ -39,13 +39,13 @@ public class LifecycleExecutionState { private final String failedStep; private final String stepInfo; private final String phaseDefinition; - private final Long indexCreationDate; + private final Long lifecycleDate; private final Long phaseTime; private final Long actionTime; private final Long stepTime; private LifecycleExecutionState(String phase, String action, String step, String failedStep, - String stepInfo, String phaseDefinition, Long indexCreationDate, + String stepInfo, String phaseDefinition, Long lifecycleDate, Long phaseTime, Long actionTime, Long stepTime) { this.phase = phase; this.action = action; @@ -53,7 +53,7 @@ private LifecycleExecutionState(String phase, String action, String step, String this.failedStep = failedStep; this.stepInfo = stepInfo; this.phaseDefinition = phaseDefinition; - this.indexCreationDate = indexCreationDate; + this.lifecycleDate = lifecycleDate; this.phaseTime = phaseTime; this.actionTime = actionTime; this.stepTime = stepTime; @@ -84,7 +84,7 @@ public static Builder builder(LifecycleExecutionState state) { .setFailedStep(state.failedStep) .setStepInfo(state.stepInfo) .setPhaseDefinition(state.phaseDefinition) - .setIndexCreationDate(state.indexCreationDate) + .setIndexCreationDate(state.lifecycleDate) .setPhaseTime(state.phaseTime) .setActionTime(state.actionTime) .setStepTime(state.stepTime); @@ -167,8 +167,8 @@ public Map asMap() { if (stepInfo != null) { result.put(STEP_INFO, stepInfo); } - if (indexCreationDate != null) { - result.put(INDEX_CREATION_DATE, String.valueOf(indexCreationDate)); + if (lifecycleDate != null) { + result.put(INDEX_CREATION_DATE, String.valueOf(lifecycleDate)); } if (phaseTime != null) { result.put(PHASE_TIME, String.valueOf(phaseTime)); @@ -209,8 +209,8 @@ public String getPhaseDefinition() { return phaseDefinition; } - public Long getIndexCreationDate() { - return indexCreationDate; + public Long getLifecycleDate() { + return lifecycleDate; } public Long getPhaseTime() { @@ -230,7 +230,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; LifecycleExecutionState that = (LifecycleExecutionState) o; - return getIndexCreationDate() == that.getIndexCreationDate() && + return getLifecycleDate() == that.getLifecycleDate() && getPhaseTime() == that.getPhaseTime() && getActionTime() == that.getActionTime() && getStepTime() == that.getStepTime() && @@ -245,7 +245,7 @@ public boolean equals(Object o) { @Override public int hashCode() { return Objects.hash(getPhase(), getAction(), getStep(), getFailedStep(), getStepInfo(), getPhaseDefinition(), - getIndexCreationDate(), getPhaseTime(), getActionTime(), getStepTime()); + getLifecycleDate(), getPhaseTime(), getActionTime(), getStepTime()); } public static class Builder { 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 5bf9b7d674def..eb18fe98876c9 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 @@ -38,9 +38,9 @@ String getShrunkIndexPrefix() { @Override public void performAction(IndexMetaData indexMetaData, ClusterState currentState, Listener listener) { LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetaData); - if (lifecycleState == null) { + if (lifecycleState.getLifecycleDate() == null) { throw new IllegalStateException("source index [" + indexMetaData.getIndex().getName() + - "] is missing creation date"); + "] is missing lifecycle date"); } String lifecycle = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetaData.getSettings()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStepTests.java index b03871b166eba..40dd022c05de6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/CopyExecutionStateStepTests.java @@ -81,7 +81,7 @@ public void testPerformAction() { LifecycleExecutionState newIndexData = LifecycleExecutionState .fromIndexMetadata(newClusterState.metaData().index(step.getShrunkIndexPrefix() + indexName)); - assertEquals(oldIndexData.getIndexCreationDate(), newIndexData.getIndexCreationDate()); + assertEquals(oldIndexData.getLifecycleDate(), newIndexData.getLifecycleDate()); assertEquals(oldIndexData.getPhase(), newIndexData.getPhase()); assertEquals(oldIndexData.getAction(), newIndexData.getAction()); assertEquals(ShrunkenIndexCheckStep.NAME, newIndexData.getStep()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/InitializePolicyContextStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/InitializePolicyContextStepTests.java index bd6aa3e88ead2..5dbeee07fe75f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/InitializePolicyContextStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/InitializePolicyContextStepTests.java @@ -89,6 +89,6 @@ public void testDoNothing() { } private long getIndexLifecycleDate(Index index, ClusterState clusterState) { - return LifecycleExecutionState.fromIndexMetadata(clusterState.getMetaData().index(index)).getIndexCreationDate(); + return LifecycleExecutionState.fromIndexMetadata(clusterState.getMetaData().index(index)).getLifecycleDate(); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionStateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionStateTests.java index b979d5e276539..e8276ad06ead2 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionStateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleExecutionStateTests.java @@ -111,7 +111,7 @@ private static LifecycleExecutionState mutate(LifecycleExecutionState toMutate) changed = true; } if (randomBoolean()) { - newState.setIndexCreationDate(randomValueOtherThan(toMutate.getIndexCreationDate(), ESTestCase::randomLong)); + newState.setIndexCreationDate(randomValueOtherThan(toMutate.getLifecycleDate(), ESTestCase::randomLong)); changed = true; } 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 51b7f2eab6c7d..820d1d15ded9d 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 @@ -27,6 +27,7 @@ import java.util.Collections; +import static org.elasticsearch.xpack.core.indexlifecycle.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; import static org.hamcrest.Matchers.equalTo; public class ShrinkStepTests extends AbstractStepTestCase { @@ -82,17 +83,17 @@ public ShrinkStep copyInstance(ShrinkStep instance) { public void testPerformAction() throws Exception { String lifecycleName = randomAlphaOfLength(5); - long creationDate = randomNonNegativeLong(); ShrinkStep step = createRandomInstance(); LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setPhase(step.getKey().getPhase()); lifecycleState.setAction(step.getKey().getAction()); lifecycleState.setStep(step.getKey().getName()); - lifecycleState.setIndexCreationDate(creationDate); + lifecycleState.setIndexCreationDate(randomNonNegativeLong()); IndexMetaData sourceIndexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)) .settings(settings(Version.CURRENT) .put(LifecycleSettings.LIFECYCLE_NAME, lifecycleName) ) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) .putAlias(AliasMetaData.builder("my_alias")) .build(); @@ -148,7 +149,10 @@ public void onFailure(Exception e) { } public void testPerformActionNotComplete() throws Exception { + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setIndexCreationDate(randomNonNegativeLong()); IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); ShrinkStep step = createRandomInstance(); @@ -191,7 +195,10 @@ public void onFailure(Exception e) { } public void testPerformActionFailure() throws Exception { + LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); + lifecycleState.setIndexCreationDate(randomNonNegativeLong()); IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); Exception exception = new RuntimeException(); ShrinkStep step = createRandomInstance(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/UpdateRolloverLifecycleDateStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/UpdateRolloverLifecycleDateStepTests.java index 576d3c39ca1f6..5a4c88eaa6ab2 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/UpdateRolloverLifecycleDateStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/UpdateRolloverLifecycleDateStepTests.java @@ -69,7 +69,7 @@ public void testPerformAction() { ClusterState newState = step.performAction(indexMetaData.getIndex(), clusterState); long actualRolloverTime = LifecycleExecutionState .fromIndexMetadata(newState.metaData().index(indexMetaData.getIndex())) - .getIndexCreationDate(); + .getLifecycleDate(); assertThat(actualRolloverTime, equalTo(rolloverTime)); } 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 d1abc9a7b613c..0d4cc15c395a3 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 @@ -62,11 +62,11 @@ public IndexLifecycleRunner(PolicyStepsRegistry stepRegistry, ClusterService clu */ boolean isReadyToTransitionToThisPhase(final String policy, final IndexMetaData indexMetaData, final String phase) { LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetaData); - if (lifecycleState.getIndexCreationDate() == null) { + if (lifecycleState.getLifecycleDate() == null) { logger.trace("no index creation date has been set yet"); return true; } - final Long lifecycleDate = lifecycleState.getIndexCreationDate(); + final Long lifecycleDate = lifecycleState.getLifecycleDate(); assert lifecycleDate != null && 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(); 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 4981391f8791a..5695c207029cc 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 @@ -94,7 +94,7 @@ protected void doMasterOperation(ExplainLifecycleRequest request, String[] concr phaseExecutionInfo = PhaseExecutionInfo.parse(parser, currentPhase); } catch (IOException e) { listener.onFailure(new ElasticsearchParseException( - "failed to parse [phase_definition] for index [" + index + "]", e)); + "failed to parse phase definition for index [" + index + "]", e)); return; } } @@ -102,7 +102,7 @@ protected void doMasterOperation(ExplainLifecycleRequest request, String[] concr if (Strings.hasLength(policyName)) { indexResponse = IndexLifecycleExplainResponse.newManagedIndexResponse(index, policyName, LifecycleSettings.LIFECYCLE_SKIP_SETTING.get(idxSettings), - lifecycleState.getIndexCreationDate(), + lifecycleState.getLifecycleDate(), lifecycleState.getPhase(), lifecycleState.getAction(), lifecycleState.getStep(),