From 31976060d9c42e64a6197c5c695400b6a3d15514 Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Thu, 21 Jun 2018 12:18:59 +0100 Subject: [PATCH] Adds ability to update a policy (#31361) * Adds ability to update a policy as long as no indexes are in the shrink action * Address review comments --- .../indexlifecycle/IndexLifecycleRunner.java | 32 ++++ .../action/TransportPutLifecycleAction.java | 6 +- .../IndexLifecycleRunnerTests.java | 156 ++++++++++++++++++ .../test/index_lifecycle/10_basic.yml | 106 ++++++++++++ 4 files changed, 298 insertions(+), 2 deletions(-) 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