Skip to content

Commit 3f9993d

Browse files
authored
Wait for shards to be active after closing indices (#38854)
This commit changes the Close Index API to add a `wait_for_active_shards` parameter that allows to wait for shards of closed indices to be active before returning a response. Relates #33888
1 parent 5e7a428 commit 3f9993d

File tree

24 files changed

+459
-44
lines changed

24 files changed

+459
-44
lines changed

rest-api-spec/src/main/resources/rest-api-spec/api/indices.close.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
"options" : ["open","closed","none","all"],
3535
"default" : "open",
3636
"description" : "Whether to expand wildcard expression to concrete indices that are open, closed or both."
37+
},
38+
"wait_for_active_shards": {
39+
"type" : "string",
40+
"description" : "Sets the number of active shards to wait for before the operation returns."
3741
}
3842
}
3943
},

rest-api-spec/src/main/resources/rest-api-spec/test/indices.open/10_basic.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- do:
1515
indices.close:
1616
index: test_index
17+
- is_true: acknowledged
1718

1819
- do:
1920
catch: bad_request
@@ -24,6 +25,7 @@
2425
- do:
2526
indices.open:
2627
index: test_index
28+
- is_true: acknowledged
2729

2830
- do:
2931
cluster.health:
@@ -50,11 +52,33 @@
5052
- do:
5153
indices.close:
5254
index: test_index
55+
- is_true: acknowledged
5356

5457
- do:
5558
indices.open:
5659
index: test_index
5760
wait_for_active_shards: all
61+
- is_true: acknowledged
62+
- match: { acknowledged: true }
63+
- match: { shards_acknowledged: true }
64+
65+
---
66+
"Close index with wait_for_active_shards set to all":
67+
- skip:
68+
version: " - 7.99.99"
69+
reason: "closed indices are replicated starting version 8.0"
70+
71+
- do:
72+
indices.create:
73+
index: test_index
74+
body:
75+
settings:
76+
number_of_replicas: 0
5877

78+
- do:
79+
indices.close:
80+
index: test_index
81+
wait_for_active_shards: all
82+
- is_true: acknowledged
5983
- match: { acknowledged: true }
6084
- match: { shards_acknowledged: true }

rest-api-spec/src/main/resources/rest-api-spec/test/indices.open/20_multiple_indices.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ setup:
2626
- do:
2727
indices.close:
2828
index: _all
29+
- is_true: acknowledged
2930

3031
- do:
3132
catch: bad_request
@@ -36,6 +37,7 @@ setup:
3637
- do:
3738
indices.open:
3839
index: _all
40+
- is_true: acknowledged
3941

4042
- do:
4143
cluster.health:
@@ -51,6 +53,7 @@ setup:
5153
- do:
5254
indices.close:
5355
index: test_*
56+
- is_true: acknowledged
5457

5558
- do:
5659
catch: bad_request
@@ -61,6 +64,7 @@ setup:
6164
- do:
6265
indices.open:
6366
index: test_*
67+
- is_true: acknowledged
6468

6569
- do:
6670
cluster.health:
@@ -76,6 +80,7 @@ setup:
7680
- do:
7781
indices.close:
7882
index: '*'
83+
- is_true: acknowledged
7984

8085
- do:
8186
catch: bad_request
@@ -86,6 +91,7 @@ setup:
8691
- do:
8792
indices.open:
8893
index: '*'
94+
- is_true: acknowledged
8995

9096
- do:
9197
cluster.health:

server/src/main/java/org/elasticsearch/action/admin/indices/close/CloseIndexAction.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
package org.elasticsearch.action.admin.indices.close;
2121

2222
import org.elasticsearch.action.Action;
23-
import org.elasticsearch.action.support.master.AcknowledgedResponse;
2423

25-
public class CloseIndexAction extends Action<AcknowledgedResponse> {
24+
public class CloseIndexAction extends Action<CloseIndexResponse> {
2625

2726
public static final CloseIndexAction INSTANCE = new CloseIndexAction();
2827
public static final String NAME = "indices:admin/close";
@@ -32,7 +31,7 @@ private CloseIndexAction() {
3231
}
3332

3433
@Override
35-
public AcknowledgedResponse newResponse() {
36-
return new AcknowledgedResponse();
34+
public CloseIndexResponse newResponse() {
35+
return new CloseIndexResponse();
3736
}
3837
}

server/src/main/java/org/elasticsearch/action/admin/indices/close/CloseIndexClusterStateUpdateRequest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
*/
1919
package org.elasticsearch.action.admin.indices.close;
2020

21+
import org.elasticsearch.action.support.ActiveShardCount;
2122
import org.elasticsearch.cluster.ack.IndicesClusterStateUpdateRequest;
2223

2324
/**
2425
* Cluster state update request that allows to close one or more indices
2526
*/
2627
public class CloseIndexClusterStateUpdateRequest extends IndicesClusterStateUpdateRequest<CloseIndexClusterStateUpdateRequest> {
2728

28-
private final long taskId;
29+
private long taskId;
30+
private ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT;
2931

3032
public CloseIndexClusterStateUpdateRequest(final long taskId) {
3133
this.taskId = taskId;
@@ -34,4 +36,18 @@ public CloseIndexClusterStateUpdateRequest(final long taskId) {
3436
public long taskId() {
3537
return taskId;
3638
}
39+
40+
public CloseIndexClusterStateUpdateRequest taskId(final long taskId) {
41+
this.taskId = taskId;
42+
return this;
43+
}
44+
45+
public ActiveShardCount waitForActiveShards() {
46+
return waitForActiveShards;
47+
}
48+
49+
public CloseIndexClusterStateUpdateRequest waitForActiveShards(final ActiveShardCount waitForActiveShards) {
50+
this.waitForActiveShards = waitForActiveShards;
51+
return this;
52+
}
3753
}

server/src/main/java/org/elasticsearch/action/admin/indices/close/CloseIndexRequest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
package org.elasticsearch.action.admin.indices.close;
2121

22+
import org.elasticsearch.Version;
2223
import org.elasticsearch.action.ActionRequestValidationException;
2324
import org.elasticsearch.action.IndicesRequest;
25+
import org.elasticsearch.action.support.ActiveShardCount;
2426
import org.elasticsearch.action.support.IndicesOptions;
2527
import org.elasticsearch.action.support.master.AcknowledgedRequest;
2628
import org.elasticsearch.common.io.stream.StreamInput;
@@ -38,6 +40,7 @@ public class CloseIndexRequest extends AcknowledgedRequest<CloseIndexRequest> im
3840

3941
private String[] indices;
4042
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen();
43+
private ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT; //NORELEASE Changes this to NONE to keep previous behavior
4144

4245
public CloseIndexRequest() {
4346
}
@@ -101,17 +104,34 @@ public CloseIndexRequest indicesOptions(IndicesOptions indicesOptions) {
101104
return this;
102105
}
103106

107+
public ActiveShardCount waitForActiveShards() {
108+
return waitForActiveShards;
109+
}
110+
111+
public CloseIndexRequest waitForActiveShards(final ActiveShardCount waitForActiveShards) {
112+
this.waitForActiveShards = waitForActiveShards;
113+
return this;
114+
}
115+
104116
@Override
105117
public void readFrom(StreamInput in) throws IOException {
106118
super.readFrom(in);
107119
indices = in.readStringArray();
108120
indicesOptions = IndicesOptions.readIndicesOptions(in);
121+
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
122+
waitForActiveShards = ActiveShardCount.readFrom(in);
123+
} else {
124+
waitForActiveShards = ActiveShardCount.NONE;
125+
}
109126
}
110127

111128
@Override
112129
public void writeTo(StreamOutput out) throws IOException {
113130
super.writeTo(out);
114131
out.writeStringArray(indices);
115132
indicesOptions.writeIndicesOptions(out);
133+
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
134+
waitForActiveShards.writeTo(out);
135+
}
116136
}
117137
}

server/src/main/java/org/elasticsearch/action/admin/indices/close/CloseIndexRequestBuilder.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919

2020
package org.elasticsearch.action.admin.indices.close;
2121

22+
import org.elasticsearch.action.support.ActiveShardCount;
2223
import org.elasticsearch.action.support.IndicesOptions;
2324
import org.elasticsearch.action.support.master.AcknowledgedRequestBuilder;
24-
import org.elasticsearch.action.support.master.AcknowledgedResponse;
2525
import org.elasticsearch.client.ElasticsearchClient;
2626

2727
/**
2828
* Builder for close index request
2929
*/
3030
public class CloseIndexRequestBuilder
31-
extends AcknowledgedRequestBuilder<CloseIndexRequest, AcknowledgedResponse, CloseIndexRequestBuilder> {
31+
extends AcknowledgedRequestBuilder<CloseIndexRequest, CloseIndexResponse, CloseIndexRequestBuilder> {
3232

3333
public CloseIndexRequestBuilder(ElasticsearchClient client, CloseIndexAction action) {
3434
super(client, action, new CloseIndexRequest());
@@ -60,4 +60,31 @@ public CloseIndexRequestBuilder setIndicesOptions(IndicesOptions indicesOptions)
6060
request.indicesOptions(indicesOptions);
6161
return this;
6262
}
63+
64+
/**
65+
* Sets the number of shard copies that should be active for indices closing to return.
66+
* Defaults to {@link ActiveShardCount#DEFAULT}, which will wait for one shard copy
67+
* (the primary) to become active. Set this value to {@link ActiveShardCount#ALL} to
68+
* wait for all shards (primary and all replicas) to be active before returning.
69+
* Otherwise, use {@link ActiveShardCount#from(int)} to set this value to any
70+
* non-negative integer, up to the number of copies per shard (number of replicas + 1),
71+
* to wait for the desired amount of shard copies to become active before returning.
72+
* Indices closing will only wait up until the timeout value for the number of shard copies
73+
* to be active before returning.
74+
*
75+
* @param waitForActiveShards number of active shard copies to wait on
76+
*/
77+
public CloseIndexRequestBuilder setWaitForActiveShards(final ActiveShardCount waitForActiveShards) {
78+
request.waitForActiveShards(waitForActiveShards);
79+
return this;
80+
}
81+
82+
/**
83+
* A shortcut for {@link #setWaitForActiveShards(ActiveShardCount)} where the numerical
84+
* shard count is passed in, instead of having to first call {@link ActiveShardCount#from(int)}
85+
* to get the ActiveShardCount.
86+
*/
87+
public CloseIndexRequestBuilder setWaitForActiveShards(final int waitForActiveShards) {
88+
return setWaitForActiveShards(ActiveShardCount.from(waitForActiveShards));
89+
}
6390
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.action.admin.indices.close;
20+
21+
import org.elasticsearch.Version;
22+
import org.elasticsearch.action.support.master.ShardsAcknowledgedResponse;
23+
import org.elasticsearch.common.io.stream.StreamInput;
24+
import org.elasticsearch.common.io.stream.StreamOutput;
25+
26+
import java.io.IOException;
27+
28+
public class CloseIndexResponse extends ShardsAcknowledgedResponse {
29+
30+
CloseIndexResponse() {
31+
}
32+
33+
public CloseIndexResponse(final boolean acknowledged, final boolean shardsAcknowledged) {
34+
super(acknowledged, shardsAcknowledged);
35+
}
36+
37+
@Override
38+
public void readFrom(StreamInput in) throws IOException {
39+
super.readFrom(in);
40+
if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
41+
readShardsAcknowledged(in);
42+
}
43+
}
44+
45+
@Override
46+
public void writeTo(StreamOutput out) throws IOException {
47+
super.writeTo(out);
48+
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
49+
writeShardsAcknowledged(out);
50+
}
51+
}
52+
}

server/src/main/java/org/elasticsearch/action/admin/indices/close/TransportCloseIndexAction.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.elasticsearch.action.ActionListener;
2424
import org.elasticsearch.action.support.ActionFilters;
2525
import org.elasticsearch.action.support.DestructiveOperations;
26-
import org.elasticsearch.action.support.master.AcknowledgedResponse;
2726
import org.elasticsearch.action.support.master.TransportMasterNodeAction;
2827
import org.elasticsearch.cluster.ClusterState;
2928
import org.elasticsearch.cluster.block.ClusterBlockException;
@@ -44,7 +43,7 @@
4443
/**
4544
* Close index action
4645
*/
47-
public class TransportCloseIndexAction extends TransportMasterNodeAction<CloseIndexRequest, AcknowledgedResponse> {
46+
public class TransportCloseIndexAction extends TransportMasterNodeAction<CloseIndexRequest, CloseIndexResponse> {
4847

4948
private final MetaDataIndexStateService indexStateService;
5049
private final DestructiveOperations destructiveOperations;
@@ -76,12 +75,12 @@ protected String executor() {
7675
}
7776

7877
@Override
79-
protected AcknowledgedResponse newResponse() {
80-
return new AcknowledgedResponse();
78+
protected CloseIndexResponse newResponse() {
79+
return new CloseIndexResponse();
8180
}
8281

8382
@Override
84-
protected void doExecute(Task task, CloseIndexRequest request, ActionListener<AcknowledgedResponse> listener) {
83+
protected void doExecute(Task task, CloseIndexRequest request, ActionListener<CloseIndexResponse> listener) {
8584
destructiveOperations.failDestructive(request.indices());
8685
if (closeIndexEnabled == false) {
8786
throw new IllegalStateException("closing indices is disabled - set [" + CLUSTER_INDICES_CLOSE_ENABLE_SETTING.getKey() +
@@ -97,29 +96,33 @@ protected ClusterBlockException checkBlock(CloseIndexRequest request, ClusterSta
9796
}
9897

9998
@Override
100-
protected void masterOperation(final CloseIndexRequest request, final ClusterState state,
101-
final ActionListener<AcknowledgedResponse> listener) {
99+
protected void masterOperation(final CloseIndexRequest request,
100+
final ClusterState state,
101+
final ActionListener<CloseIndexResponse> listener) {
102102
throw new UnsupportedOperationException("The task parameter is required");
103103
}
104104

105105
@Override
106-
protected void masterOperation(final Task task, final CloseIndexRequest request, final ClusterState state,
107-
final ActionListener<AcknowledgedResponse> listener) throws Exception {
106+
protected void masterOperation(final Task task,
107+
final CloseIndexRequest request,
108+
final ClusterState state,
109+
final ActionListener<CloseIndexResponse> listener) throws Exception {
108110
final Index[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, request);
109111
if (concreteIndices == null || concreteIndices.length == 0) {
110-
listener.onResponse(new AcknowledgedResponse(true));
112+
listener.onResponse(new CloseIndexResponse(true, false));
111113
return;
112114
}
113115

114116
final CloseIndexClusterStateUpdateRequest closeRequest = new CloseIndexClusterStateUpdateRequest(task.getId())
115117
.ackTimeout(request.timeout())
116118
.masterNodeTimeout(request.masterNodeTimeout())
119+
.waitForActiveShards(request.waitForActiveShards())
117120
.indices(concreteIndices);
118121

119-
indexStateService.closeIndices(closeRequest, new ActionListener<AcknowledgedResponse>() {
122+
indexStateService.closeIndices(closeRequest, new ActionListener<CloseIndexResponse>() {
120123

121124
@Override
122-
public void onResponse(final AcknowledgedResponse response) {
125+
public void onResponse(final CloseIndexResponse response) {
123126
listener.onResponse(response);
124127
}
125128

0 commit comments

Comments
 (0)