diff --git a/x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunner.java b/x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunner.java index 118855ccc46..7a866d5905f 100644 --- a/x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunner.java +++ b/x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunner.java @@ -5,6 +5,8 @@ */ package org.elasticsearch.xpack.indexlifecycle; +import com.carrotsearch.hppc.cursors.ObjectCursor; + import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.admin.indices.shrink.ShrinkAction; @@ -342,6 +344,36 @@ public class IndexLifecycleRunner { } } + /** + * 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) { + for (ObjectCursor cursor : currentState.getMetaData().indices().values()) { + IndexMetaData idxMetadata = cursor.value; + Settings idxSettings = idxMetadata.getSettings(); + String currentPolicyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(idxSettings); + if (policyName.equals(currentPolicyName)) { + StepKey currentStepKey = IndexLifecycleRunner.getCurrentStepKey(idxSettings); + if (canSetPolicy(currentStepKey, policyName, newPolicy) == false) { + return false; + } + } + } + return true; + } + public static ClusterState removePolicyForIndexes(final Index[] indices, ClusterState currentState, List failedIndexes) { MetaData.Builder newMetadata = MetaData.builder(currentState.getMetaData()); boolean clusterStateChanged = false; diff --git a/x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportPutLifecycleAction.java b/x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportPutLifecycleAction.java index b9f9814e9c0..0d1096c6e23 100644 --- a/x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportPutLifecycleAction.java +++ b/x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportPutLifecycleAction.java @@ -27,6 +27,7 @@ import org.elasticsearch.xpack.core.indexlifecycle.OperationMode; 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; @@ -72,7 +73,8 @@ public class TransportPutLifecycleAction extends TransportMasterNodeAction