Skip to content

Commit cd22a6d

Browse files
weizijundnhatn
authored andcommitted
Fix wait_for_no_initializing_shards params (#58379)
1 parent 00a2ed2 commit cd22a6d

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterHealthAction.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public String getName() {
5656

5757
@Override
5858
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
59+
final ClusterHealthRequest clusterHealthRequest = fromRequest(request);
60+
return channel -> client.admin().cluster().health(clusterHealthRequest, new RestStatusToXContentListener<>(channel));
61+
}
62+
63+
public static ClusterHealthRequest fromRequest(final RestRequest request) {
5964
final ClusterHealthRequest clusterHealthRequest = clusterHealthRequest(Strings.splitStringByCommaToArray(request.param("index")));
6065
clusterHealthRequest.indicesOptions(IndicesOptions.fromRequest(request, clusterHealthRequest.indicesOptions()));
6166
clusterHealthRequest.local(request.paramAsBoolean("local", clusterHealthRequest.local()));
@@ -68,11 +73,11 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
6873
clusterHealthRequest.waitForNoRelocatingShards(
6974
request.paramAsBoolean("wait_for_no_relocating_shards", clusterHealthRequest.waitForNoRelocatingShards()));
7075
clusterHealthRequest.waitForNoInitializingShards(
71-
request.paramAsBoolean("wait_for_no_initializing_shards", clusterHealthRequest.waitForNoRelocatingShards()));
76+
request.paramAsBoolean("wait_for_no_initializing_shards", clusterHealthRequest.waitForNoInitializingShards()));
7277
if (request.hasParam("wait_for_relocating_shards")) {
7378
// wait_for_relocating_shards has been removed in favor of wait_for_no_relocating_shards
7479
throw new IllegalArgumentException("wait_for_relocating_shards has been removed, " +
75-
"use wait_for_no_relocating_shards [true/false] instead");
80+
"use wait_for_no_relocating_shards [true/false] instead");
7681
}
7782
String waitForActiveShards = request.param("wait_for_active_shards");
7883
if (waitForActiveShards != null) {
@@ -82,7 +87,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
8287
if (request.param("wait_for_events") != null) {
8388
clusterHealthRequest.waitForEvents(Priority.valueOf(request.param("wait_for_events").toUpperCase(Locale.ROOT)));
8489
}
85-
return channel -> client.admin().cluster().health(clusterHealthRequest, new RestStatusToXContentListener<>(channel));
90+
return clusterHealthRequest;
8691
}
8792

8893
private static final Set<String> RESPONSE_PARAMS = Collections.singleton("level");
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
20+
package org.elasticsearch.rest.action.admin.cluster;
21+
22+
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
23+
import org.elasticsearch.action.support.ActiveShardCount;
24+
import org.elasticsearch.cluster.health.ClusterHealthStatus;
25+
import org.elasticsearch.common.Priority;
26+
import org.elasticsearch.common.unit.TimeValue;
27+
import org.elasticsearch.rest.RestRequest;
28+
import org.elasticsearch.test.ESTestCase;
29+
import org.elasticsearch.test.rest.FakeRestRequest;
30+
31+
import java.util.HashMap;
32+
import java.util.Map;
33+
34+
import static org.hamcrest.Matchers.equalTo;
35+
36+
public class RestClusterHealthActionTests extends ESTestCase {
37+
38+
public void testFromRequest() {
39+
Map<String, String> params = new HashMap<>();
40+
String index = "index";
41+
boolean local = randomBoolean();
42+
String masterTimeout = randomTimeValue();
43+
String timeout = randomTimeValue();
44+
ClusterHealthStatus waitForStatus = randomFrom(ClusterHealthStatus.values());
45+
boolean waitForNoRelocatingShards = randomBoolean();
46+
boolean waitForNoInitializingShards = randomBoolean();
47+
int waitForActiveShards = randomIntBetween(1, 3);
48+
String waitForNodes = "node";
49+
Priority waitForEvents = randomFrom(Priority.values());
50+
51+
params.put("index", index);
52+
params.put("local", String.valueOf(local));
53+
params.put("master_timeout", masterTimeout);
54+
params.put("timeout", timeout);
55+
params.put("wait_for_status", waitForStatus.name());
56+
if (waitForNoRelocatingShards || randomBoolean()) {
57+
params.put("wait_for_no_relocating_shards", String.valueOf(waitForNoRelocatingShards));
58+
}
59+
if (waitForNoInitializingShards || randomBoolean()) {
60+
params.put("wait_for_no_initializing_shards", String.valueOf(waitForNoInitializingShards));
61+
}
62+
params.put("wait_for_active_shards", String.valueOf(waitForActiveShards));
63+
params.put("wait_for_nodes", waitForNodes);
64+
params.put("wait_for_events", waitForEvents.name());
65+
66+
FakeRestRequest restRequest = buildRestRequest(params);
67+
ClusterHealthRequest clusterHealthRequest = RestClusterHealthAction.fromRequest(restRequest);
68+
assertThat(clusterHealthRequest.indices().length, equalTo(1));
69+
assertThat(clusterHealthRequest.indices()[0], equalTo(index));
70+
assertThat(clusterHealthRequest.local(), equalTo(local));
71+
assertThat(clusterHealthRequest.masterNodeTimeout(), equalTo(TimeValue.parseTimeValue(masterTimeout, "test")));
72+
assertThat(clusterHealthRequest.timeout(), equalTo(TimeValue.parseTimeValue(timeout, "test")));
73+
assertThat(clusterHealthRequest.waitForStatus(), equalTo(waitForStatus));
74+
assertThat(clusterHealthRequest.waitForNoRelocatingShards(), equalTo(waitForNoRelocatingShards));
75+
assertThat(clusterHealthRequest.waitForNoInitializingShards(), equalTo(waitForNoInitializingShards));
76+
assertThat(clusterHealthRequest.waitForActiveShards(), equalTo(ActiveShardCount.parseString(String.valueOf(waitForActiveShards))));
77+
assertThat(clusterHealthRequest.waitForNodes(), equalTo(waitForNodes));
78+
assertThat(clusterHealthRequest.waitForEvents(), equalTo(waitForEvents));
79+
80+
}
81+
82+
private FakeRestRequest buildRestRequest(Map<String, String> params) {
83+
return new FakeRestRequest.Builder(xContentRegistry())
84+
.withMethod(RestRequest.Method.GET)
85+
.withPath("/_cluster/health")
86+
.withParams(params)
87+
.build();
88+
}
89+
}

0 commit comments

Comments
 (0)