Skip to content

Commit 8660e8d

Browse files
authored
Warn of change of default of wait_for_active_shards (#67246)
In 7.x the close indices API defaults to `?wait_for_active_shards=0` but from 8.0 it will default to respecting the index settings instead. This commit introduces the `index-setting` value for this parameter on this API allowing users to opt-in to the future behaviour today, and starts to emit a deprecation warning for users that use the default. Relates #67158 Closes #66419
1 parent 345dd82 commit 8660e8d

58 files changed

Lines changed: 308 additions & 42 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
3939
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
4040
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest;
41+
import org.elasticsearch.action.support.ActiveShardCount;
4142
import org.elasticsearch.client.indices.AnalyzeRequest;
4243
import org.elasticsearch.client.indices.CloseIndexRequest;
4344
import org.elasticsearch.client.indices.CreateDataStreamRequest;
@@ -140,6 +141,13 @@ static Request closeIndex(CloseIndexRequest closeIndexRequest) {
140141
parameters.withTimeout(closeIndexRequest.timeout());
141142
parameters.withMasterTimeout(closeIndexRequest.masterNodeTimeout());
142143
parameters.withIndicesOptions(closeIndexRequest.indicesOptions());
144+
145+
final ActiveShardCount activeShardCount = closeIndexRequest.waitForActiveShards();
146+
if (activeShardCount == ActiveShardCount.DEFAULT) {
147+
request.addParameter("wait_for_active_shards", "index-setting");
148+
} else {
149+
parameters.withWaitForActiveShards(activeShardCount);
150+
}
143151
request.addParameters(parameters.asMap());
144152
return request;
145153
}

client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CloseIndexRequest.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.client.indices;
2121

22-
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
2322
import org.elasticsearch.action.support.ActiveShardCount;
2423
import org.elasticsearch.action.support.IndicesOptions;
2524
import org.elasticsearch.client.TimedRequest;
@@ -35,7 +34,7 @@ public class CloseIndexRequest extends TimedRequest implements Validatable {
3534

3635
private String[] indices;
3736
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen();
38-
private ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT;
37+
private ActiveShardCount waitForActiveShards = null;
3938

4039
/**
4140
* Creates a new close index request
@@ -82,16 +81,14 @@ public ActiveShardCount waitForActiveShards() {
8281
}
8382

8483
/**
85-
* Sets the number of shard copies that should be active for indices opening to return.
86-
* Defaults to {@link ActiveShardCount#DEFAULT}, which will wait for one shard copy
87-
* (the primary) to become active. Set this value to {@link ActiveShardCount#ALL} to
88-
* wait for all shards (primary and all replicas) to be active before returning.
89-
* Otherwise, use {@link ActiveShardCount#from(int)} to set this value to any
90-
* non-negative integer, up to the number of copies per shard (number of replicas + 1),
91-
* to wait for the desired amount of shard copies to become active before returning.
92-
* Indices opening will only wait up until the timeout value for the number of shard copies
93-
* to be active before returning. Check {@link OpenIndexResponse#isShardsAcknowledged()} to
94-
* determine if the requisite shard copies were all started before returning or timing out.
84+
* Sets the number of shard copies that should be active before a close-index request returns. Defaults to {@code null}, which means not
85+
* to wait. However the default behaviour is deprecated and will change in version 8. You can opt-in to the new default behaviour now by
86+
* setting this to {@link ActiveShardCount#DEFAULT}, which will wait according to the setting {@code index.write.wait_for_active_shards}
87+
* which by default will wait for one shard, the primary. Set this value to {@link ActiveShardCount#ALL} to wait for all shards (primary
88+
* and all replicas) to be active before returning. Otherwise, use {@link ActiveShardCount#from(int)} to set this value to any
89+
* non-negative integer up to the number of copies per shard (number of replicas + 1), to wait for the desired amount of shard copies
90+
* to become active before returning. To explicitly preserve today's default behaviour and suppress the deprecation warning, set this
91+
* property to {@code ActiveShardCount.from(0)}.
9592
*
9693
* @param waitForActiveShards number of active shard copies to wait on
9794
*/

client/rest-high-level/src/test/java/org/elasticsearch/client/CCRIT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ public void testIndexFollowing() throws Exception {
188188

189189
// Need to close index prior to unfollowing it:
190190
CloseIndexRequest closeIndexRequest = new CloseIndexRequest("follower");
191+
closeIndexRequest.waitForActiveShards(ActiveShardCount.from(0));
191192
org.elasticsearch.action.support.master.AcknowledgedResponse closeIndexReponse =
192193
highLevelClient().indices().close(closeIndexRequest, RequestOptions.DEFAULT);
193194
assertThat(closeIndexReponse.isAcknowledged(), is(true));

client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest;
5151
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse;
5252
import org.elasticsearch.action.index.IndexRequest;
53+
import org.elasticsearch.action.support.ActiveShardCount;
5354
import org.elasticsearch.action.support.IndicesOptions;
5455
import org.elasticsearch.action.support.WriteRequest;
5556
import org.elasticsearch.action.support.broadcast.BroadcastResponse;
@@ -904,6 +905,7 @@ public void testCloseExistingIndex() throws IOException {
904905
}
905906

906907
CloseIndexRequest closeIndexRequest = new CloseIndexRequest(indices);
908+
closeIndexRequest.waitForActiveShards(ActiveShardCount.from(0));
907909
CloseIndexResponse closeIndexResponse = execute(closeIndexRequest,
908910
highLevelClient().indices()::close, highLevelClient().indices()::closeAsync);
909911
assertTrue(closeIndexResponse.isAcknowledged());
@@ -926,6 +928,7 @@ public void testCloseNonExistentIndex() throws IOException {
926928
assertFalse(indexExists(nonExistentIndex));
927929

928930
CloseIndexRequest closeIndexRequest = new CloseIndexRequest(nonExistentIndex);
931+
closeIndexRequest.waitForActiveShards(ActiveShardCount.from(0));
929932
ElasticsearchException exception = expectThrows(ElasticsearchException.class,
930933
() -> execute(closeIndexRequest, highLevelClient().indices()::close, highLevelClient().indices()::closeAsync));
931934
assertEquals(RestStatus.NOT_FOUND, exception.status());
@@ -934,6 +937,7 @@ public void testCloseNonExistentIndex() throws IOException {
934937
public void testCloseEmptyOrNullIndex() {
935938
String[] indices = randomBoolean() ? Strings.EMPTY_ARRAY : null;
936939
CloseIndexRequest closeIndexRequest = new CloseIndexRequest(indices);
940+
closeIndexRequest.waitForActiveShards(ActiveShardCount.from(0));
937941
org.elasticsearch.client.ValidationException exception = expectThrows(org.elasticsearch.client.ValidationException.class,
938942
() -> execute(closeIndexRequest, highLevelClient().indices()::close, highLevelClient().indices()::closeAsync));
939943
assertThat(exception.validationErrors().get(0), equalTo("index is missing"));

client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
4242
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
4343
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest;
44+
import org.elasticsearch.action.support.ActiveShardCount;
4445
import org.elasticsearch.action.support.master.AcknowledgedRequest;
4546
import org.elasticsearch.client.indices.AnalyzeRequest;
4647
import org.elasticsearch.client.indices.CloseIndexRequest;
@@ -641,6 +642,22 @@ public void testCloseIndex() {
641642
RequestConvertersTests.setRandomMasterTimeout(closeIndexRequest, expectedParams);
642643
RequestConvertersTests.setRandomIndicesOptions(closeIndexRequest::indicesOptions, closeIndexRequest::indicesOptions,
643644
expectedParams);
645+
switch (between(0, 3)) {
646+
case 0:
647+
break;
648+
case 1:
649+
closeIndexRequest.waitForActiveShards(ActiveShardCount.DEFAULT);
650+
expectedParams.put("wait_for_active_shards", "index-setting");
651+
break;
652+
case 2:
653+
closeIndexRequest.waitForActiveShards(ActiveShardCount.ALL);
654+
expectedParams.put("wait_for_active_shards", "all");
655+
break;
656+
case 3:
657+
closeIndexRequest.waitForActiveShards(ActiveShardCount.from(1));
658+
expectedParams.put("wait_for_active_shards", "1");
659+
break;
660+
}
644661

645662
Request request = IndicesRequestConverters.closeIndex(closeIndexRequest);
646663
StringJoiner endpoint = new StringJoiner("/", "/", "").add(String.join(",", indices)).add("_close");

client/rest-high-level/src/test/java/org/elasticsearch/client/RankEvalIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void testRankEvalRequest() throws IOException {
108108
}
109109

110110
// now try this when test2 is closed
111-
client().performRequest(new Request("POST", "index2/_close"));
111+
closeIndex("index2");
112112
rankEvalRequest.indicesOptions(IndicesOptions.fromParameters(null, "true", null, "false", SearchRequest.DEFAULT_INDICES_OPTIONS));
113113
response = execute(rankEvalRequest, highLevelClient()::rankEval, highLevelClient()::rankEvalAsync);
114114
}

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CCRDocumentationIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ public void testUnfollow() throws Exception {
321321
assertThat(unfollowResponse.isAcknowledged(), is(true));
322322

323323
CloseIndexRequest closeIndexRequest = new CloseIndexRequest(followIndex);
324+
closeIndexRequest.waitForActiveShards(ActiveShardCount.from(0));
324325
assertThat(client.indices().close(closeIndexRequest, RequestOptions.DEFAULT).isAcknowledged(), is(true));
325326
}
326327

@@ -353,6 +354,7 @@ public void testUnfollow() throws Exception {
353354
assertThat(unfollowResponse.isAcknowledged(), is(true));
354355

355356
CloseIndexRequest closeIndexRequest = new CloseIndexRequest(followIndex);
357+
closeIndexRequest.waitForActiveShards(ActiveShardCount.from(0));
356358
assertThat(client.indices().close(closeIndexRequest, RequestOptions.DEFAULT).isAcknowledged(), is(true));
357359
}
358360

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,8 @@ public void testCloseIndex() throws Exception {
14691469
CloseIndexRequest request = new CloseIndexRequest("index"); // <1>
14701470
// end::close-index-request
14711471

1472+
request.waitForActiveShards(ActiveShardCount.from(0));
1473+
14721474
// tag::close-index-request-timeout
14731475
request.setTimeout(TimeValue.timeValueMinutes(2)); // <1>
14741476
// end::close-index-request-timeout

distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/WaitForRefreshAndCloseIT.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ private void closeWhileListenerEngaged(ActionFuture<String> future) throws Excep
9797
});
9898

9999
// Close the index. That should flush the listener.
100-
client().performRequest(new Request("POST", "/test/_close"));
100+
final Request closeRequest = new Request("POST", "/test/_close");
101+
closeRequest.addParameter("wait_for_active_shards", "0");
102+
client().performRequest(closeRequest);
101103

102104
/*
103105
* The request may fail, but we really, really, really want to make

docs/reference/ccr/apis/follow/post-unfollow.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ PUT /follower_index/_ccr/follow?wait_for_active_shards=1
2323
2424
POST /follower_index/_ccr/pause_follow
2525
26-
POST /follower_index/_close
26+
POST /follower_index/_close?wait_for_active_shards=0
2727
--------------------------------------------------
2828
// TESTSETUP
2929
// TEST[setup:remote_cluster_and_leader_index]

0 commit comments

Comments
 (0)