Skip to content

Commit 7923145

Browse files
authored
ILM open/close steps are noop if idx is open/close (#48614)
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.
1 parent 4e81ae7 commit 7923145

File tree

4 files changed

+74
-16
lines changed

4 files changed

+74
-16
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ void performDuringNoSnapshot(IndexMetaData indexMetaData, ClusterState currentCl
3232
return;
3333
}
3434

35-
CloseIndexRequest closeIndexRequest = new CloseIndexRequest(followerIndex);
36-
getClient().admin().indices().close(closeIndexRequest, ActionListener.wrap(
37-
r -> {
38-
assert r.isAcknowledged() : "close index response is not acknowledged";
39-
listener.onResponse(true);
40-
},
41-
listener::onFailure)
42-
);
35+
if (indexMetaData.getState() == IndexMetaData.State.OPEN) {
36+
CloseIndexRequest closeIndexRequest = new CloseIndexRequest(followerIndex);
37+
getClient().admin().indices().close(closeIndexRequest, ActionListener.wrap(
38+
r -> {
39+
assert r.isAcknowledged() : "close index response is not acknowledged";
40+
listener.onResponse(true);
41+
},
42+
listener::onFailure)
43+
);
44+
} else {
45+
listener.onResponse(true);
46+
}
4347
}
4448
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStep.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ final class OpenFollowerIndexStep extends AsyncActionStep {
2323
@Override
2424
public void performAction(IndexMetaData indexMetaData, ClusterState currentClusterState,
2525
ClusterStateObserver observer, Listener listener) {
26-
OpenIndexRequest request = new OpenIndexRequest(indexMetaData.getIndex().getName());
27-
getClient().admin().indices().open(request, ActionListener.wrap(
28-
r -> {
29-
assert r.isAcknowledged() : "open index response is not acknowledged";
30-
listener.onResponse(true);
31-
},
32-
listener::onFailure
33-
));
26+
if (indexMetaData.getState() == IndexMetaData.State.CLOSE) {
27+
OpenIndexRequest request = new OpenIndexRequest(indexMetaData.getIndex().getName());
28+
getClient().admin().indices().open(request, ActionListener.wrap(
29+
r -> {
30+
assert r.isAcknowledged() : "open index response is not acknowledged";
31+
listener.onResponse(true);
32+
},
33+
listener::onFailure
34+
));
35+
} else {
36+
listener.onResponse(true);
37+
}
3438
}
3539
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,30 @@ public void onFailure(Exception e) {
110110
Mockito.verifyNoMoreInteractions(indicesClient);
111111
}
112112

113+
public void testCloseFollowerIndexIsNoopForAlreadyClosedIndex() {
114+
IndexMetaData indexMetadata = IndexMetaData.builder("follower-index")
115+
.settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true"))
116+
.putCustom(CCR_METADATA_KEY, Collections.emptyMap())
117+
.state(IndexMetaData.State.CLOSE)
118+
.numberOfShards(1)
119+
.numberOfReplicas(0)
120+
.build();
121+
Client client = Mockito.mock(Client.class);
122+
CloseFollowerIndexStep step = new CloseFollowerIndexStep(randomStepKey(), randomStepKey(), client);
123+
step.performAction(indexMetadata, null, null, new AsyncActionStep.Listener() {
124+
@Override
125+
public void onResponse(boolean complete) {
126+
assertThat(complete, is(true));
127+
}
128+
129+
@Override
130+
public void onFailure(Exception e) {
131+
}
132+
});
133+
134+
Mockito.verifyZeroInteractions(client);
135+
}
136+
113137
@Override
114138
protected CloseFollowerIndexStep createRandomInstance() {
115139
Step.StepKey stepKey = randomStepKey();

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStepTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,35 @@ protected OpenFollowerIndexStep copyInstance(OpenFollowerIndexStep instance) {
5151
return new OpenFollowerIndexStep(instance.getKey(), instance.getNextStepKey(), instance.getClient());
5252
}
5353

54+
public void testOpenFollowerIndexIsNoopForAlreadyOpenIndex() {
55+
IndexMetaData indexMetadata = IndexMetaData.builder("follower-index")
56+
.settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true"))
57+
.putCustom(CCR_METADATA_KEY, Collections.emptyMap())
58+
.state(IndexMetaData.State.OPEN)
59+
.numberOfShards(1)
60+
.numberOfReplicas(0)
61+
.build();
62+
Client client = Mockito.mock(Client.class);
63+
OpenFollowerIndexStep step = new OpenFollowerIndexStep(randomStepKey(), randomStepKey(), client);
64+
step.performAction(indexMetadata, null, null, new AsyncActionStep.Listener() {
65+
@Override
66+
public void onResponse(boolean complete) {
67+
assertThat(complete, is(true));
68+
}
69+
70+
@Override
71+
public void onFailure(Exception e) {
72+
}
73+
});
74+
75+
Mockito.verifyZeroInteractions(client);
76+
}
77+
5478
public void testOpenFollowingIndex() {
5579
IndexMetaData indexMetadata = IndexMetaData.builder("follower-index")
5680
.settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true"))
5781
.putCustom(CCR_METADATA_KEY, Collections.emptyMap())
82+
.state(IndexMetaData.State.CLOSE)
5883
.numberOfShards(1)
5984
.numberOfReplicas(0)
6085
.build();
@@ -95,6 +120,7 @@ public void onFailure(Exception e) {
95120
public void testOpenFollowingIndexFailed() {
96121
IndexMetaData indexMetadata = IndexMetaData.builder("follower-index")
97122
.settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true"))
123+
.state(IndexMetaData.State.CLOSE)
98124
.putCustom(CCR_METADATA_KEY, Collections.emptyMap())
99125
.numberOfShards(1)
100126
.numberOfReplicas(0)

0 commit comments

Comments
 (0)