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 076a25c7d259d..379b903ab8ceb 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 @@ -5,7 +5,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.elasticsearch.ElasticsearchException; @@ -27,11 +26,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.IndexLifecycleMetadata; -import org.elasticsearch.xpack.core.indexlifecycle.LifecycleAction; import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicy; import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings; -import org.elasticsearch.xpack.core.indexlifecycle.Phase; import org.elasticsearch.xpack.core.indexlifecycle.RolloverAction; import org.elasticsearch.xpack.core.indexlifecycle.Step; import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey; @@ -39,7 +35,6 @@ import java.io.IOException; import java.util.List; -import java.util.Map; import java.util.function.LongSupplier; public class IndexLifecycleRunner { @@ -325,8 +320,6 @@ 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) { - Map policiesMap = ((IndexLifecycleMetadata) currentState.metaData().custom(IndexLifecycleMetadata.TYPE)) - .getPolicies(); MetaData.Builder newMetadata = MetaData.builder(currentState.getMetaData()); boolean clusterStateChanged = false; for (Index index : indices) { @@ -335,8 +328,8 @@ public static ClusterState setPolicyForIndexes(final String newPolicyName, final // Index doesn't exist so fail it failedIndexes.add(index.getName()); } else { - IndexMetaData.Builder newIdxMetadata = IndexLifecycleRunner.setPolicyForIndex(newPolicyName, newPolicy, policiesMap, - failedIndexes, index, indexMetadata, nowSupplier); + IndexMetaData.Builder newIdxMetadata = IndexLifecycleRunner.setPolicyForIndex(newPolicyName, + newPolicy, indexMetadata, nowSupplier); if (newIdxMetadata != null) { newMetadata.put(newIdxMetadata); clusterStateChanged = true; @@ -353,106 +346,24 @@ public static ClusterState setPolicyForIndexes(final String newPolicyName, final } private static IndexMetaData.Builder setPolicyForIndex(final String newPolicyName, LifecyclePolicy newPolicy, - Map policiesMap, List failedIndexes, Index index, IndexMetaData indexMetadata, - LongSupplier nowSupplier) { + IndexMetaData indexMetadata, LongSupplier nowSupplier) { Settings idxSettings = indexMetadata.getSettings(); - String currentPolicyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(idxSettings); StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(idxSettings); - LifecyclePolicy currentPolicy = null; - if (Strings.hasLength(currentPolicyName)) { - currentPolicy = policiesMap.get(currentPolicyName); - } - - if (canSetPolicy(currentStepKey, currentPolicy, newPolicy)) { - Settings.Builder newSettings = Settings.builder().put(idxSettings); - 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(idxSettings, currentStepKey, nextValidStepKey, nowSupplier); - } - } - newSettings.put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), newPolicyName); - return IndexMetaData.builder(indexMetadata).settings(newSettings); - } else { - failedIndexes.add(index.getName()); - return null; - } - } - - private static boolean canSetPolicy(StepKey currentStepKey, LifecyclePolicy currentPolicy, LifecyclePolicy newPolicy) { - if (currentPolicy != null) { - if (currentPolicy.isActionSafe(currentStepKey)) { - return true; - } else { - // Index is in an unsafe action so fail it if the action has changed between oldPolicy and newPolicy - return isActionChanged(currentStepKey, currentPolicy, newPolicy) == false; - } - } else { - // Index not previously managed by ILM so safe to change policy - return true; - } - } - - private static boolean isActionChanged(StepKey stepKey, LifecyclePolicy currentPolicy, LifecyclePolicy newPolicy) { - LifecycleAction currentAction = getActionFromPolicy(currentPolicy, stepKey.getPhase(), stepKey.getAction()); - LifecycleAction newAction = getActionFromPolicy(newPolicy, stepKey.getPhase(), stepKey.getAction()); - if (newAction == null) { - return true; - } else { - return currentAction.equals(newAction) == false; - } - } - - private static LifecycleAction getActionFromPolicy(LifecyclePolicy policy, String phaseName, String actionName) { - Phase phase = policy.getPhases().get(phaseName); - if (phase != null) { - return phase.getActions().get(actionName); - } else { - return null; - } - } - /** - * Returns true if the provided policy is allowed to be updated - * given the current {@link ClusterState}. In practice this method checks - * that all the indexes using the provided policyName is in a - * state where it is able to deal with the policy being updated to - * newPolicy. If any of these indexes is not in a state wheree - * it can deal with the update the method will return false. - * - * @param policyName - * the name of the policy being updated - * @param newPolicy - * the new version of the {@link LifecyclePolicy} - * @param currentState - * the current {@link ClusterState} - */ - public static boolean canUpdatePolicy(String policyName, LifecyclePolicy newPolicy, ClusterState currentState) { - Map policiesMap = ((IndexLifecycleMetadata) currentState.metaData().custom(IndexLifecycleMetadata.TYPE)) - .getPolicies(); - for (ObjectCursor cursor : currentState.getMetaData().indices().values()) { - IndexMetaData idxMetadata = cursor.value; - Settings idxSettings = idxMetadata.getSettings(); - String currentPolicyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(idxSettings); - LifecyclePolicy currentPolicy = null; - if (Strings.hasLength(currentPolicyName)) { - currentPolicy = policiesMap.get(currentPolicyName); - } - if (policyName.equals(currentPolicyName)) { - StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(idxSettings); - if (canSetPolicy(currentStepKey, currentPolicy, newPolicy) == false) { - return false; - } + Settings.Builder newSettings = Settings.builder().put(idxSettings); + 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(idxSettings, currentStepKey, nextValidStepKey, nowSupplier); } } - return true; + newSettings.put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), newPolicyName); + return IndexMetaData.builder(indexMetadata).settings(newSettings); } public static ClusterState removePolicyForIndexes(final Index[] indices, ClusterState currentState, List failedIndexes) { - Map policiesMap = ((IndexLifecycleMetadata) currentState.metaData().custom(IndexLifecycleMetadata.TYPE)) - .getPolicies(); MetaData.Builder newMetadata = MetaData.builder(currentState.getMetaData()); boolean clusterStateChanged = false; for (Index index : indices) { @@ -461,8 +372,7 @@ public static ClusterState removePolicyForIndexes(final Index[] indices, Cluster // Index doesn't exist so fail it failedIndexes.add(index.getName()); } else { - IndexMetaData.Builder newIdxMetadata = IndexLifecycleRunner.removePolicyForIndex(index, indexMetadata, policiesMap, - failedIndexes); + IndexMetaData.Builder newIdxMetadata = IndexLifecycleRunner.removePolicyForIndex(indexMetadata); if (newIdxMetadata != null) { newMetadata.put(newIdxMetadata); clusterStateChanged = true; @@ -478,44 +388,22 @@ public static ClusterState removePolicyForIndexes(final Index[] indices, Cluster } } - private static IndexMetaData.Builder removePolicyForIndex(Index index, IndexMetaData indexMetadata, - Map policiesMap, List failedIndexes) { + private static IndexMetaData.Builder removePolicyForIndex(IndexMetaData indexMetadata) { Settings idxSettings = indexMetadata.getSettings(); Settings.Builder newSettings = Settings.builder().put(idxSettings); - String currentPolicyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(idxSettings); - StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(idxSettings); - LifecyclePolicy currentPolicy = null; - if (Strings.hasLength(currentPolicyName)) { - currentPolicy = policiesMap.get(currentPolicyName); - } - - if (canRemovePolicy(currentStepKey, currentPolicy)) { - 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); - } else { - failedIndexes.add(index.getName()); - return null; - } - } - private static boolean canRemovePolicy(StepKey currentStepKey, LifecyclePolicy currentPolicy) { - if (currentPolicy != null) { - // Can't remove policy if the index is currently in an unsafe action - return currentPolicy.isActionSafe(currentStepKey); - } else { - // Index not previously managed by ILM - return true; - } + 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); } } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportPutLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportPutLifecycleAction.java index 79e907bb28cb8..63c4e0cc1666d 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportPutLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportPutLifecycleAction.java @@ -6,7 +6,6 @@ package org.elasticsearch.xpack.indexlifecycle.action; -import org.elasticsearch.ResourceAlreadyExistsException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.master.TransportMasterNodeAction; @@ -28,7 +27,6 @@ import org.elasticsearch.xpack.core.indexlifecycle.action.PutLifecycleAction; import org.elasticsearch.xpack.core.indexlifecycle.action.PutLifecycleAction.Request; import org.elasticsearch.xpack.core.indexlifecycle.action.PutLifecycleAction.Response; -import org.elasticsearch.xpack.indexlifecycle.IndexLifecycleRunner; import java.util.Map; import java.util.SortedMap; @@ -81,11 +79,6 @@ public ClusterState execute(ClusterState currentState) throws Exception { if (currentMetadata == null) { // first time using index-lifecycle feature, bootstrap metadata currentMetadata = IndexLifecycleMetadata.EMPTY; } - if (currentMetadata.getPolicyMetadatas().containsKey(request.getPolicy().getName()) && IndexLifecycleRunner - .canUpdatePolicy(request.getPolicy().getName(), request.getPolicy(), currentState) == false) { - throw new ResourceAlreadyExistsException("Lifecycle policy already exists: {}", - request.getPolicy().getName()); - } // NORELEASE Check if current step exists in new policy and if not move to next available step SortedMap newPolicies = new TreeMap<>(currentMetadata.getPolicyMetadatas()); LifecyclePolicyMetadata lifecyclePolicyMetadata = new LifecyclePolicyMetadata(request.getPolicy(), filteredHeaders); 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 be37809f2f0af..cf60d4f96707a 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 @@ -7,7 +7,6 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.Version; -import org.elasticsearch.action.admin.indices.shrink.ShrinkAction; import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetaData; @@ -934,98 +933,6 @@ public void testSetPolicyForIndexIndexDoesntExist() { assertSame(clusterState, newClusterState); } - public void testSetPolicyForIndexIndexInUnsafe() { - long now = randomNonNegativeLong(); - String indexName = randomAlphaOfLength(10); - String oldPolicyName = "old_policy"; - String newPolicyName = "new_policy"; - LifecyclePolicy newPolicy = new LifecyclePolicy(newPolicyName, Collections.emptyMap()); - 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); - List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); - Index index = clusterState.metaData().index(indexName).getIndex(); - Index[] indices = new Index[] { index }; - List failedIndexes = new ArrayList<>(); - - ClusterState newClusterState = IndexLifecycleRunner.setPolicyForIndexes(newPolicyName, indices, clusterState, newPolicy, - failedIndexes, () -> now); - - assertEquals(1, failedIndexes.size()); - assertEquals(index.getName(), failedIndexes.get(0)); - assertSame(clusterState, newClusterState); - } - - public void testSetPolicyForIndexIndexInUnsafeActionUnchanged() { - long now = randomNonNegativeLong(); - String indexName = randomAlphaOfLength(10); - String oldPolicyName = "old_policy"; - String newPolicyName = "new_policy"; - StepKey currentStep = new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)); - LifecyclePolicy oldPolicy = createPolicy(oldPolicyName, null, currentStep); - LifecyclePolicy newPolicy = createPolicy(newPolicyName, 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); - List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); - Index index = clusterState.metaData().index(indexName).getIndex(); - Index[] indices = new Index[] { index }; - List failedIndexes = new ArrayList<>(); - - ClusterState newClusterState = IndexLifecycleRunner.setPolicyForIndexes(newPolicyName, indices, clusterState, newPolicy, - failedIndexes, () -> now); - - assertTrue(failedIndexes.isEmpty()); - assertClusterStateOnPolicy(clusterState, index, newPolicyName, currentStep, currentStep, newClusterState, now); - } - - public void testSetPolicyForIndexIndexInUnsafeActionChanged() { - long now = randomNonNegativeLong(); - String indexName = randomAlphaOfLength(10); - String oldPolicyName = "old_policy"; - String newPolicyName = "new_policy"; - StepKey currentStep = new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)); - LifecyclePolicy oldPolicy = createPolicy(oldPolicyName, null, currentStep); - - // change the current action so its not equal to the old one by adding a step - Map phases = new HashMap<>(); - Map actions = new HashMap<>(); - List steps = new ArrayList<>(); - steps.add(new MockStep(currentStep, null)); - steps.add(new MockStep(new StepKey(currentStep.getPhase(), currentStep.getAction(), randomAlphaOfLength(5)), null)); - MockAction unsafeAction = new MockAction(steps, false); - actions.put(unsafeAction.getWriteableName(), unsafeAction); - Phase phase = new Phase(currentStep.getPhase(), TimeValue.timeValueMillis(0), actions); - phases.put(phase.getName(), phase); - LifecyclePolicy newPolicy = newTestLifecyclePolicy(newPolicyName, phases); - - 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); - List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); - Index index = clusterState.metaData().index(indexName).getIndex(); - Index[] indices = new Index[] { index }; - List failedIndexes = new ArrayList<>(); - - ClusterState newClusterState = IndexLifecycleRunner.setPolicyForIndexes(newPolicyName, indices, clusterState, newPolicy, - failedIndexes, () -> now); - - assertEquals(1, failedIndexes.size()); - assertEquals(index.getName(), failedIndexes.get(0)); - assertSame(clusterState, newClusterState); - } - private static LifecyclePolicy createPolicy(String policyName, StepKey safeStep, StepKey unsafeStep) { Map phases = new HashMap<>(); if (safeStep != null) { @@ -1051,241 +958,6 @@ private static LifecyclePolicy createPolicy(String policyName, StepKey safeStep, return newTestLifecyclePolicy(policyName, phases); } - public void testCanUpdatePolicy() { - String indexName = randomAlphaOfLength(10); - String oldPolicyName = "old_policy"; - String newPolicyName = "new_policy"; - LifecyclePolicy newPolicy = newTestLifecyclePolicy(newPolicyName, Collections.emptyMap()); - 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); - List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); - - boolean canUpdatePolicy = IndexLifecycleRunner.canUpdatePolicy(oldPolicyName, newPolicy, clusterState); - - assertTrue(canUpdatePolicy); - } - - public void testCanUpdatePolicyIndexInUnsafe() { - String indexName = randomAlphaOfLength(10); - String oldPolicyName = "old_policy"; - String newPolicyName = "new_policy"; - LifecyclePolicy newPolicy = newTestLifecyclePolicy(newPolicyName, Collections.emptyMap()); - 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); - List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); - - boolean canUpdatePolicy = IndexLifecycleRunner.canUpdatePolicy(oldPolicyName, newPolicy, clusterState); - - assertFalse(canUpdatePolicy); - } - - public void testCanUpdatePolicyIndexInUnsafeActionUnchanged() { - String indexName = randomAlphaOfLength(10); - String oldPolicyName = "old_policy"; - String newPolicyName = "new_policy"; - StepKey currentStep = new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)); - LifecyclePolicy oldPolicy = createPolicy(oldPolicyName, null, currentStep); - LifecyclePolicy newPolicy = createPolicy(newPolicyName, - new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)), 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); - List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); - - boolean canUpdatePolicy = IndexLifecycleRunner.canUpdatePolicy(oldPolicyName, newPolicy, clusterState); - - assertTrue(canUpdatePolicy); - } - - public void testCanUpdatePolicyIndexInUnsafeActionChanged() { - String indexName = randomAlphaOfLength(10); - String oldPolicyName = "old_policy"; - String newPolicyName = "new_policy"; - StepKey currentStep = new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)); - LifecyclePolicy oldPolicy = createPolicy(oldPolicyName, null, currentStep); - - // change the current action so its not equal to the old one by adding a step - Map phases = new HashMap<>(); - Map actions = new HashMap<>(); - List newSteps = new ArrayList<>(); - newSteps.add(new MockStep(currentStep, null)); - newSteps.add(new MockStep(new StepKey(currentStep.getPhase(), currentStep.getAction(), randomAlphaOfLength(5)), null)); - MockAction unsafeAction = new MockAction(newSteps, false); - actions.put(unsafeAction.getWriteableName(), unsafeAction); - Phase phase = new Phase(currentStep.getPhase(), TimeValue.timeValueMillis(0), actions); - phases.put(phase.getName(), phase); - LifecyclePolicy newPolicy = newTestLifecyclePolicy(newPolicyName, phases); - - 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); - List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); - - boolean canUpdatePolicy = IndexLifecycleRunner.canUpdatePolicy(oldPolicyName, newPolicy, clusterState); - - assertFalse(canUpdatePolicy); - } - - public void testCanUpdatePolicyIndexNotManaged() { - String indexName = randomAlphaOfLength(10); - String oldPolicyName = "old_policy"; - String newPolicyName = "new_policy"; - LifecyclePolicy oldPolicy = newTestLifecyclePolicy(oldPolicyName, Collections.emptyMap()); - LifecyclePolicy newPolicy = newTestLifecyclePolicy(newPolicyName, Collections.emptyMap()); - Settings.Builder indexSettingsBuilder = Settings.builder(); - List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); - - boolean canUpdatePolicy = IndexLifecycleRunner.canUpdatePolicy(oldPolicyName, newPolicy, clusterState); - - assertTrue(canUpdatePolicy); - } - - public void testCanUpdatePolicyDifferentPolicy() { - String indexName = randomAlphaOfLength(10); - String oldPolicyName = "old_policy"; - String newPolicyName = "new_policy"; - LifecyclePolicy oldPolicy = newTestLifecyclePolicy(oldPolicyName, Collections.emptyMap()); - LifecyclePolicy newPolicy = newTestLifecyclePolicy(newPolicyName, Collections.emptyMap()); - StepKey currentStep = new StepKey(randomAlphaOfLength(10), ShrinkAction.NAME, randomAlphaOfLength(10)); - Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, "different_policy") - .put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); - List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap())); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, policyMetadatas); - - boolean canUpdatePolicy = IndexLifecycleRunner.canUpdatePolicy(oldPolicyName, newPolicy, clusterState); - - assertTrue(canUpdatePolicy); - } - - public void testCanUpdatePolicyMultipleIndexesUpdateAllowed() { - String oldPolicyName = "old_policy"; - String newPolicyName = "new_policy"; - LifecyclePolicy newPolicy = newTestLifecyclePolicy(newPolicyName, Collections.emptyMap()); - - String index1Name = randomAlphaOfLength(10); - StepKey currentStep1 = new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)); - Settings.Builder indexSettingsBuilder1 = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, oldPolicyName).put(LifecycleSettings.LIFECYCLE_PHASE, currentStep1.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep1.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep1.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); - IndexMetaData indexMetadata1 = IndexMetaData.builder(index1Name).settings(indexSettingsBuilder1).build(); - - String index2Name = randomAlphaOfLength(10); - StepKey currentStep2 = currentStep1; - Settings.Builder indexSettingsBuilder2 = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, oldPolicyName).put(LifecycleSettings.LIFECYCLE_PHASE, currentStep2.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep2.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep2.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); - IndexMetaData indexMetadata2 = IndexMetaData.builder(index2Name).settings(indexSettingsBuilder2).build(); - - String index3Name = randomAlphaOfLength(10); - StepKey currentStep3 = new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)); - Settings.Builder indexSettingsBuilder3 = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, "different_policy").put(LifecycleSettings.LIFECYCLE_PHASE, currentStep3.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep3.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep3.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); - IndexMetaData indexMetadata3 = IndexMetaData.builder(index3Name).settings(indexSettingsBuilder3).build(); - - String index4Name = randomAlphaOfLength(10); - Settings.Builder indexSettingsBuilder4 = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT); - IndexMetaData indexMetadata4 = IndexMetaData.builder(index4Name).settings(indexSettingsBuilder4).build(); - - Map lifecyclePolicyMetadatasMap = new HashMap<>(); - lifecyclePolicyMetadatasMap.put(oldPolicyName, - new LifecyclePolicyMetadata(createPolicy(oldPolicyName, currentStep1, null), Collections.emptyMap())); - lifecyclePolicyMetadatasMap.put("different_policy", - new LifecyclePolicyMetadata(createPolicy("different_policy", null, currentStep3), Collections.emptyMap())); - IndexLifecycleMetadata indexLifecycleMetadata = new IndexLifecycleMetadata(lifecyclePolicyMetadatasMap, OperationMode.RUNNING); - - MetaData metadata = MetaData.builder().put(indexMetadata1, true).put(indexMetadata2, true).put(indexMetadata3, true) - .put(indexMetadata4, true).putCustom(IndexLifecycleMetadata.TYPE, indexLifecycleMetadata).build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("my_cluster")).metaData(metadata).build(); - - boolean canUpdatePolicy = IndexLifecycleRunner.canUpdatePolicy(oldPolicyName, newPolicy, clusterState); - - assertTrue(canUpdatePolicy); - } - - public void testCanUpdatePolicyMultipleIndexesUpdateForbidden() { - String oldPolicyName = "old_policy"; - String newPolicyName = "new_policy"; - LifecyclePolicy newPolicy = newTestLifecyclePolicy(newPolicyName, Collections.emptyMap()); - - String index1Name = randomAlphaOfLength(10); - StepKey currentStep1 = new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)); - Settings.Builder indexSettingsBuilder1 = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, oldPolicyName).put(LifecycleSettings.LIFECYCLE_PHASE, currentStep1.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep1.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep1.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); - IndexMetaData indexMetadata1 = IndexMetaData.builder(index1Name).settings(indexSettingsBuilder1).build(); - - String index2Name = randomAlphaOfLength(10); - StepKey currentStep2 = new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)); - Settings.Builder indexSettingsBuilder2 = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, oldPolicyName).put(LifecycleSettings.LIFECYCLE_PHASE, currentStep2.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep2.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep2.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); - IndexMetaData indexMetadata2 = IndexMetaData.builder(index2Name).settings(indexSettingsBuilder2).build(); - - String index3Name = randomAlphaOfLength(10); - StepKey currentStep3 = new StepKey(randomAlphaOfLength(10), MockAction.NAME, randomAlphaOfLength(10)); - Settings.Builder indexSettingsBuilder3 = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, "different_policy").put(LifecycleSettings.LIFECYCLE_PHASE, currentStep3.getPhase()) - .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep3.getAction()) - .put(LifecycleSettings.LIFECYCLE_STEP, currentStep3.getName()).put(LifecycleSettings.LIFECYCLE_SKIP, true); - IndexMetaData indexMetadata3 = IndexMetaData.builder(index3Name).settings(indexSettingsBuilder3).build(); - - String index4Name = randomAlphaOfLength(10); - Settings.Builder indexSettingsBuilder4 = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT); - IndexMetaData indexMetadata4 = IndexMetaData.builder(index4Name).settings(indexSettingsBuilder4).build(); - - Map lifecyclePolicyMetadatasMap = new HashMap<>(); - lifecyclePolicyMetadatasMap.put(oldPolicyName, - new LifecyclePolicyMetadata(createPolicy(oldPolicyName, currentStep1, currentStep2), Collections.emptyMap())); - lifecyclePolicyMetadatasMap.put("different_policy", - new LifecyclePolicyMetadata(createPolicy("different_policy", null, currentStep3), Collections.emptyMap())); - IndexLifecycleMetadata indexLifecycleMetadata = new IndexLifecycleMetadata(lifecyclePolicyMetadatasMap, OperationMode.RUNNING); - - MetaData metadata = MetaData.builder().put(indexMetadata1, true).put(indexMetadata2, true).put(indexMetadata3, true) - .put(indexMetadata4, true).putCustom(IndexLifecycleMetadata.TYPE, indexLifecycleMetadata).build(); - ClusterState clusterState = ClusterState.builder(new ClusterName("my_cluster")).metaData(metadata).build(); - - boolean canUpdatePolicy = IndexLifecycleRunner.canUpdatePolicy(oldPolicyName, newPolicy, clusterState); - - assertFalse(canUpdatePolicy); - } - public void testRemovePolicyForIndex() { String indexName = randomAlphaOfLength(10); String oldPolicyName = "old_policy"; @@ -1363,9 +1035,8 @@ public void testRemovePolicyForIndexIndexInUnsafe() { ClusterState newClusterState = IndexLifecycleRunner.removePolicyForIndexes(indices, clusterState, failedIndexes); - assertEquals(1, failedIndexes.size()); - assertEquals(index.getName(), failedIndexes.get(0)); - assertSame(clusterState, newClusterState); + assertTrue(failedIndexes.isEmpty()); + assertIndexNotManagedByILM(newClusterState, index); } public static void assertIndexNotManagedByILM(ClusterState clusterState, Index index) {