From e959bdda363bca38af3d85ab3cb6faa4ae577bee Mon Sep 17 00:00:00 2001 From: Andrei Dan Date: Fri, 18 Oct 2019 17:06:19 +0100 Subject: [PATCH] ILM open/close steps are noop if idx is open/close The open and close follower steps didn't check if the index is open, closed respectively, before executing the open/close request. This changes the steps to check the index state and only perform the open/close operation if the index is not already open/closed. --- .../core/ilm/CloseFollowerIndexStep.java | 20 ++++++++------ .../xpack/core/ilm/OpenFollowerIndexStep.java | 20 ++++++++------ .../core/ilm/CloseFollowerIndexStepTests.java | 24 +++++++++++++++++ .../core/ilm/OpenFollowerIndexStepTests.java | 26 +++++++++++++++++++ 4 files changed, 74 insertions(+), 16 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java index cdc5c91e7eb13..dbd7ba54d2212 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java @@ -32,13 +32,17 @@ void performDuringNoSnapshot(IndexMetaData indexMetaData, ClusterState currentCl return; } - CloseIndexRequest closeIndexRequest = new CloseIndexRequest(followerIndex); - getClient().admin().indices().close(closeIndexRequest, ActionListener.wrap( - r -> { - assert r.isAcknowledged() : "close index response is not acknowledged"; - listener.onResponse(true); - }, - listener::onFailure) - ); + if (indexMetaData.getState() == IndexMetaData.State.OPEN) { + CloseIndexRequest closeIndexRequest = new CloseIndexRequest(followerIndex); + getClient().admin().indices().close(closeIndexRequest, ActionListener.wrap( + r -> { + assert r.isAcknowledged() : "close index response is not acknowledged"; + listener.onResponse(true); + }, + listener::onFailure) + ); + } else { + listener.onResponse(true); + } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStep.java index 00dc416312feb..9883f83d02057 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStep.java @@ -23,13 +23,17 @@ final class OpenFollowerIndexStep extends AsyncActionStep { @Override public void performAction(IndexMetaData indexMetaData, ClusterState currentClusterState, ClusterStateObserver observer, Listener listener) { - OpenIndexRequest request = new OpenIndexRequest(indexMetaData.getIndex().getName()); - getClient().admin().indices().open(request, ActionListener.wrap( - r -> { - assert r.isAcknowledged() : "open index response is not acknowledged"; - listener.onResponse(true); - }, - listener::onFailure - )); + if (indexMetaData.getState() == IndexMetaData.State.CLOSE) { + OpenIndexRequest request = new OpenIndexRequest(indexMetaData.getIndex().getName()); + getClient().admin().indices().open(request, ActionListener.wrap( + r -> { + assert r.isAcknowledged() : "open index response is not acknowledged"; + listener.onResponse(true); + }, + listener::onFailure + )); + } else { + listener.onResponse(true); + } } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java index 7858f9831ac3c..d096ff0009c6b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java @@ -110,6 +110,30 @@ public void onFailure(Exception e) { Mockito.verifyNoMoreInteractions(indicesClient); } + public void testCloseFollowerIndexIsNoopForAlreadyClosedIndex() { + IndexMetaData indexMetadata = IndexMetaData.builder("follower-index") + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true")) + .putCustom(CCR_METADATA_KEY, Collections.emptyMap()) + .state(IndexMetaData.State.CLOSE) + .numberOfShards(1) + .numberOfReplicas(0) + .build(); + Client client = Mockito.mock(Client.class); + CloseFollowerIndexStep step = new CloseFollowerIndexStep(randomStepKey(), randomStepKey(), client); + step.performAction(indexMetadata, null, null, new AsyncActionStep.Listener() { + @Override + public void onResponse(boolean complete) { + assertThat(complete, is(true)); + } + + @Override + public void onFailure(Exception e) { + } + }); + + Mockito.verifyZeroInteractions(client); + } + @Override protected CloseFollowerIndexStep createRandomInstance() { Step.StepKey stepKey = randomStepKey(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStepTests.java index 3c349d05ba87d..577d66d906aee 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStepTests.java @@ -51,10 +51,35 @@ protected OpenFollowerIndexStep copyInstance(OpenFollowerIndexStep instance) { return new OpenFollowerIndexStep(instance.getKey(), instance.getNextStepKey(), instance.getClient()); } + public void testOpenFollowerIndexIsNoopForAlreadyOpenIndex() { + IndexMetaData indexMetadata = IndexMetaData.builder("follower-index") + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true")) + .putCustom(CCR_METADATA_KEY, Collections.emptyMap()) + .state(IndexMetaData.State.OPEN) + .numberOfShards(1) + .numberOfReplicas(0) + .build(); + Client client = Mockito.mock(Client.class); + OpenFollowerIndexStep step = new OpenFollowerIndexStep(randomStepKey(), randomStepKey(), client); + step.performAction(indexMetadata, null, null, new AsyncActionStep.Listener() { + @Override + public void onResponse(boolean complete) { + assertThat(complete, is(true)); + } + + @Override + public void onFailure(Exception e) { + } + }); + + Mockito.verifyZeroInteractions(client); + } + public void testOpenFollowingIndex() { IndexMetaData indexMetadata = IndexMetaData.builder("follower-index") .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true")) .putCustom(CCR_METADATA_KEY, Collections.emptyMap()) + .state(IndexMetaData.State.CLOSE) .numberOfShards(1) .numberOfReplicas(0) .build(); @@ -95,6 +120,7 @@ public void onFailure(Exception e) { public void testOpenFollowingIndexFailed() { IndexMetaData indexMetadata = IndexMetaData.builder("follower-index") .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true")) + .state(IndexMetaData.State.CLOSE) .putCustom(CCR_METADATA_KEY, Collections.emptyMap()) .numberOfShards(1) .numberOfReplicas(0)