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 df2ba8714d9..7b91a42ff64 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 @@ -278,13 +278,30 @@ public class IndexLifecycleRunner { return newClusterStateBuilder; } + /** + * Conditionally updates cluster state with new step info. The new cluster state is only + * built if the step info has changed, otherwise the same old clusterState is + * returned + * + * @param index the index to modify + * @param clusterState the cluster state to modify + * @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 + * @throws IOException if parsing step info fails + */ static ClusterState addStepInfoToClusterState(Index index, ClusterState clusterState, ToXContentObject stepInfo) throws IOException { IndexMetaData idxMeta = clusterState.getMetaData().index(index); - XContentBuilder infoXContentBuilder = JsonXContent.contentBuilder(); - stepInfo.toXContent(infoXContentBuilder, ToXContent.EMPTY_PARAMS); - String stepInfoString = BytesReference.bytes(infoXContentBuilder).utf8ToString(); + 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()))) { + return clusterState; + } Settings.Builder indexSettings = Settings.builder().put(idxMeta.getSettings()) - .put(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.getKey(), stepInfoString); + .put(LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING.getKey(), stepInfoString); ClusterState.Builder newClusterStateBuilder = newClusterStateWithIndexSettings(index, clusterState, indexSettings); return newClusterStateBuilder.build(); } 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 efa1f2e8d70..be37809f2f0 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 @@ -820,6 +820,8 @@ public class IndexLifecycleRunnerTests extends ESTestCase { Index index = clusterState.metaData().index(indexName).getIndex(); ClusterState newClusterState = IndexLifecycleRunner.addStepInfoToClusterState(index, clusterState, stepInfo); assertClusterStateStepInfo(clusterState, index, currentStep, newClusterState, stepInfo); + ClusterState runAgainClusterState = IndexLifecycleRunner.addStepInfoToClusterState(index, newClusterState, stepInfo); + assertSame(newClusterState, runAgainClusterState); } @SuppressWarnings("unchecked")