Skip to content

Commit 7319bc7

Browse files
authored
HLRC: split cluster request converters (#33400)
In an effort to encapsulate the different clients, the request converters are being shuffled around. This splits the ClusterClient request converters.
1 parent 24776b2 commit 7319bc7

File tree

6 files changed

+241
-171
lines changed

6 files changed

+241
-171
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public final class ClusterClient {
5656
*/
5757
public ClusterUpdateSettingsResponse putSettings(ClusterUpdateSettingsRequest clusterUpdateSettingsRequest, RequestOptions options)
5858
throws IOException {
59-
return restHighLevelClient.performRequestAndParseEntity(clusterUpdateSettingsRequest, RequestConverters::clusterPutSettings,
59+
return restHighLevelClient.performRequestAndParseEntity(clusterUpdateSettingsRequest, ClusterRequestConverters::clusterPutSettings,
6060
options, ClusterUpdateSettingsResponse::fromXContent, emptySet());
6161
}
6262

@@ -70,7 +70,7 @@ public ClusterUpdateSettingsResponse putSettings(ClusterUpdateSettingsRequest cl
7070
*/
7171
public void putSettingsAsync(ClusterUpdateSettingsRequest clusterUpdateSettingsRequest, RequestOptions options,
7272
ActionListener<ClusterUpdateSettingsResponse> listener) {
73-
restHighLevelClient.performRequestAsyncAndParseEntity(clusterUpdateSettingsRequest, RequestConverters::clusterPutSettings,
73+
restHighLevelClient.performRequestAsyncAndParseEntity(clusterUpdateSettingsRequest, ClusterRequestConverters::clusterPutSettings,
7474
options, ClusterUpdateSettingsResponse::fromXContent, listener, emptySet());
7575
}
7676

@@ -85,7 +85,7 @@ public void putSettingsAsync(ClusterUpdateSettingsRequest clusterUpdateSettingsR
8585
*/
8686
public ClusterGetSettingsResponse getSettings(ClusterGetSettingsRequest clusterGetSettingsRequest, RequestOptions options)
8787
throws IOException {
88-
return restHighLevelClient.performRequestAndParseEntity(clusterGetSettingsRequest, RequestConverters::clusterGetSettings,
88+
return restHighLevelClient.performRequestAndParseEntity(clusterGetSettingsRequest, ClusterRequestConverters::clusterGetSettings,
8989
options, ClusterGetSettingsResponse::fromXContent, emptySet());
9090
}
9191

@@ -99,7 +99,7 @@ public ClusterGetSettingsResponse getSettings(ClusterGetSettingsRequest clusterG
9999
*/
100100
public void getSettingsAsync(ClusterGetSettingsRequest clusterGetSettingsRequest, RequestOptions options,
101101
ActionListener<ClusterGetSettingsResponse> listener) {
102-
restHighLevelClient.performRequestAsyncAndParseEntity(clusterGetSettingsRequest, RequestConverters::clusterGetSettings,
102+
restHighLevelClient.performRequestAsyncAndParseEntity(clusterGetSettingsRequest, ClusterRequestConverters::clusterGetSettings,
103103
options, ClusterGetSettingsResponse::fromXContent, listener, emptySet());
104104
}
105105

@@ -115,7 +115,7 @@ public void getSettingsAsync(ClusterGetSettingsRequest clusterGetSettingsRequest
115115
* @throws IOException in case there is a problem sending the request or parsing back the response
116116
*/
117117
public ClusterHealthResponse health(ClusterHealthRequest healthRequest, RequestOptions options) throws IOException {
118-
return restHighLevelClient.performRequestAndParseEntity(healthRequest, RequestConverters::clusterHealth, options,
118+
return restHighLevelClient.performRequestAndParseEntity(healthRequest, ClusterRequestConverters::clusterHealth, options,
119119
ClusterHealthResponse::fromXContent, singleton(RestStatus.REQUEST_TIMEOUT.getStatus()));
120120
}
121121

@@ -129,7 +129,7 @@ public ClusterHealthResponse health(ClusterHealthRequest healthRequest, RequestO
129129
* @param listener the listener to be notified upon request completion
130130
*/
131131
public void healthAsync(ClusterHealthRequest healthRequest, RequestOptions options, ActionListener<ClusterHealthResponse> listener) {
132-
restHighLevelClient.performRequestAsyncAndParseEntity(healthRequest, RequestConverters::clusterHealth, options,
132+
restHighLevelClient.performRequestAsyncAndParseEntity(healthRequest, ClusterRequestConverters::clusterHealth, options,
133133
ClusterHealthResponse::fromXContent, listener, singleton(RestStatus.REQUEST_TIMEOUT.getStatus()));
134134
}
135135
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.client;
21+
22+
import org.apache.http.client.methods.HttpGet;
23+
import org.apache.http.client.methods.HttpPut;
24+
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
25+
import org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsRequest;
26+
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
27+
import org.elasticsearch.action.support.ActiveShardCount;
28+
import org.elasticsearch.common.Strings;
29+
30+
import java.io.IOException;
31+
32+
final class ClusterRequestConverters {
33+
34+
static Request clusterPutSettings(ClusterUpdateSettingsRequest clusterUpdateSettingsRequest) throws IOException {
35+
Request request = new Request(HttpPut.METHOD_NAME, "/_cluster/settings");
36+
37+
RequestConverters.Params parameters = new RequestConverters.Params(request);
38+
parameters.withTimeout(clusterUpdateSettingsRequest.timeout());
39+
parameters.withMasterTimeout(clusterUpdateSettingsRequest.masterNodeTimeout());
40+
41+
request.setEntity(RequestConverters.createEntity(clusterUpdateSettingsRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
42+
return request;
43+
}
44+
45+
static Request clusterGetSettings(ClusterGetSettingsRequest clusterGetSettingsRequest) throws IOException {
46+
Request request = new Request(HttpGet.METHOD_NAME, "/_cluster/settings");
47+
48+
RequestConverters.Params parameters = new RequestConverters.Params(request);
49+
parameters.withLocal(clusterGetSettingsRequest.local());
50+
parameters.withIncludeDefaults(clusterGetSettingsRequest.includeDefaults());
51+
parameters.withMasterTimeout(clusterGetSettingsRequest.masterNodeTimeout());
52+
53+
return request;
54+
}
55+
56+
static Request clusterHealth(ClusterHealthRequest healthRequest) {
57+
String[] indices = healthRequest.indices() == null ? Strings.EMPTY_ARRAY : healthRequest.indices();
58+
String endpoint = new RequestConverters.EndpointBuilder()
59+
.addPathPartAsIs("_cluster/health")
60+
.addCommaSeparatedPathParts(indices)
61+
.build();
62+
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
63+
64+
new RequestConverters.Params(request)
65+
.withWaitForStatus(healthRequest.waitForStatus())
66+
.withWaitForNoRelocatingShards(healthRequest.waitForNoRelocatingShards())
67+
.withWaitForNoInitializingShards(healthRequest.waitForNoInitializingShards())
68+
.withWaitForActiveShards(healthRequest.waitForActiveShards(), ActiveShardCount.NONE)
69+
.withWaitForNodes(healthRequest.waitForNodes())
70+
.withWaitForEvents(healthRequest.waitForEvents())
71+
.withTimeout(healthRequest.timeout())
72+
.withMasterTimeout(healthRequest.masterNodeTimeout())
73+
.withLocal(healthRequest.local())
74+
.withLevel(healthRequest.level());
75+
return request;
76+
}
77+
}

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

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesRequest;
3737
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryRequest;
3838
import org.elasticsearch.action.admin.cluster.repositories.verify.VerifyRepositoryRequest;
39-
import org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsRequest;
40-
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
4139
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest;
4240
import org.elasticsearch.action.admin.cluster.snapshots.delete.DeleteSnapshotRequest;
4341
import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest;
@@ -724,28 +722,6 @@ private static Request resize(ResizeRequest resizeRequest) throws IOException {
724722
return request;
725723
}
726724

727-
static Request clusterPutSettings(ClusterUpdateSettingsRequest clusterUpdateSettingsRequest) throws IOException {
728-
Request request = new Request(HttpPut.METHOD_NAME, "/_cluster/settings");
729-
730-
Params parameters = new Params(request);
731-
parameters.withTimeout(clusterUpdateSettingsRequest.timeout());
732-
parameters.withMasterTimeout(clusterUpdateSettingsRequest.masterNodeTimeout());
733-
734-
request.setEntity(createEntity(clusterUpdateSettingsRequest, REQUEST_BODY_CONTENT_TYPE));
735-
return request;
736-
}
737-
738-
static Request clusterGetSettings(ClusterGetSettingsRequest clusterGetSettingsRequest) throws IOException {
739-
Request request = new Request(HttpGet.METHOD_NAME, "/_cluster/settings");
740-
741-
Params parameters = new Params(request);
742-
parameters.withLocal(clusterGetSettingsRequest.local());
743-
parameters.withIncludeDefaults(clusterGetSettingsRequest.includeDefaults());
744-
parameters.withMasterTimeout(clusterGetSettingsRequest.masterNodeTimeout());
745-
746-
return request;
747-
}
748-
749725
static Request getPipeline(GetPipelineRequest getPipelineRequest) {
750726
String endpoint = new EndpointBuilder()
751727
.addPathPartAsIs("_ingest/pipeline")
@@ -803,28 +779,6 @@ static Request listTasks(ListTasksRequest listTaskRequest) {
803779
return request;
804780
}
805781

806-
static Request clusterHealth(ClusterHealthRequest healthRequest) {
807-
String[] indices = healthRequest.indices() == null ? Strings.EMPTY_ARRAY : healthRequest.indices();
808-
String endpoint = new EndpointBuilder()
809-
.addPathPartAsIs("_cluster/health")
810-
.addCommaSeparatedPathParts(indices)
811-
.build();
812-
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
813-
814-
new Params(request)
815-
.withWaitForStatus(healthRequest.waitForStatus())
816-
.withWaitForNoRelocatingShards(healthRequest.waitForNoRelocatingShards())
817-
.withWaitForNoInitializingShards(healthRequest.waitForNoInitializingShards())
818-
.withWaitForActiveShards(healthRequest.waitForActiveShards(), ActiveShardCount.NONE)
819-
.withWaitForNodes(healthRequest.waitForNodes())
820-
.withWaitForEvents(healthRequest.waitForEvents())
821-
.withTimeout(healthRequest.timeout())
822-
.withMasterTimeout(healthRequest.masterNodeTimeout())
823-
.withLocal(healthRequest.local())
824-
.withLevel(healthRequest.level());
825-
return request;
826-
}
827-
828782
static Request reindex(ReindexRequest reindexRequest) throws IOException {
829783
String endpoint = new EndpointBuilder().addPathPart("_reindex").build();
830784
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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.client;
21+
22+
import org.apache.http.client.methods.HttpGet;
23+
import org.apache.http.client.methods.HttpPut;
24+
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
25+
import org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsRequest;
26+
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
27+
import org.elasticsearch.action.support.ActiveShardCount;
28+
import org.elasticsearch.action.support.master.AcknowledgedRequest;
29+
import org.elasticsearch.cluster.health.ClusterHealthStatus;
30+
import org.elasticsearch.common.Priority;
31+
import org.elasticsearch.test.ESTestCase;
32+
import org.hamcrest.CoreMatchers;
33+
import org.junit.Assert;
34+
35+
import java.io.IOException;
36+
import java.util.HashMap;
37+
import java.util.Locale;
38+
import java.util.Map;
39+
40+
import static org.hamcrest.CoreMatchers.equalTo;
41+
import static org.hamcrest.Matchers.nullValue;
42+
43+
public class ClusterRequestConvertersTests extends ESTestCase {
44+
45+
public void testClusterPutSettings() throws IOException {
46+
ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest();
47+
Map<String, String> expectedParams = new HashMap<>();
48+
RequestConvertersTests.setRandomMasterTimeout(request, expectedParams);
49+
RequestConvertersTests.setRandomTimeout(request::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
50+
51+
Request expectedRequest = ClusterRequestConverters.clusterPutSettings(request);
52+
Assert.assertEquals("/_cluster/settings", expectedRequest.getEndpoint());
53+
Assert.assertEquals(HttpPut.METHOD_NAME, expectedRequest.getMethod());
54+
Assert.assertEquals(expectedParams, expectedRequest.getParameters());
55+
}
56+
57+
public void testClusterGetSettings() throws IOException {
58+
ClusterGetSettingsRequest request = new ClusterGetSettingsRequest();
59+
Map<String, String> expectedParams = new HashMap<>();
60+
RequestConvertersTests.setRandomMasterTimeout(request, expectedParams);
61+
request.includeDefaults(ESTestCase.randomBoolean());
62+
if (request.includeDefaults()) {
63+
expectedParams.put("include_defaults", String.valueOf(true));
64+
}
65+
66+
Request expectedRequest = ClusterRequestConverters.clusterGetSettings(request);
67+
Assert.assertEquals("/_cluster/settings", expectedRequest.getEndpoint());
68+
Assert.assertEquals(HttpGet.METHOD_NAME, expectedRequest.getMethod());
69+
Assert.assertEquals(expectedParams, expectedRequest.getParameters());
70+
}
71+
72+
public void testClusterHealth() {
73+
ClusterHealthRequest healthRequest = new ClusterHealthRequest();
74+
Map<String, String> expectedParams = new HashMap<>();
75+
RequestConvertersTests.setRandomLocal(healthRequest, expectedParams);
76+
String timeoutType = ESTestCase.randomFrom("timeout", "masterTimeout", "both", "none");
77+
String timeout = ESTestCase.randomTimeValue();
78+
String masterTimeout = ESTestCase.randomTimeValue();
79+
switch (timeoutType) {
80+
case "timeout":
81+
healthRequest.timeout(timeout);
82+
expectedParams.put("timeout", timeout);
83+
// If Master Timeout wasn't set it uses the same value as Timeout
84+
expectedParams.put("master_timeout", timeout);
85+
break;
86+
case "masterTimeout":
87+
expectedParams.put("timeout", "30s");
88+
healthRequest.masterNodeTimeout(masterTimeout);
89+
expectedParams.put("master_timeout", masterTimeout);
90+
break;
91+
case "both":
92+
healthRequest.timeout(timeout);
93+
expectedParams.put("timeout", timeout);
94+
healthRequest.masterNodeTimeout(timeout);
95+
expectedParams.put("master_timeout", timeout);
96+
break;
97+
case "none":
98+
expectedParams.put("timeout", "30s");
99+
expectedParams.put("master_timeout", "30s");
100+
break;
101+
default:
102+
throw new UnsupportedOperationException();
103+
}
104+
RequestConvertersTests.setRandomWaitForActiveShards(healthRequest::waitForActiveShards, ActiveShardCount.NONE, expectedParams);
105+
if (ESTestCase.randomBoolean()) {
106+
ClusterHealthRequest.Level level = ESTestCase.randomFrom(ClusterHealthRequest.Level.values());
107+
healthRequest.level(level);
108+
expectedParams.put("level", level.name().toLowerCase(Locale.ROOT));
109+
} else {
110+
expectedParams.put("level", "cluster");
111+
}
112+
if (ESTestCase.randomBoolean()) {
113+
Priority priority = ESTestCase.randomFrom(Priority.values());
114+
healthRequest.waitForEvents(priority);
115+
expectedParams.put("wait_for_events", priority.name().toLowerCase(Locale.ROOT));
116+
}
117+
if (ESTestCase.randomBoolean()) {
118+
ClusterHealthStatus status = ESTestCase.randomFrom(ClusterHealthStatus.values());
119+
healthRequest.waitForStatus(status);
120+
expectedParams.put("wait_for_status", status.name().toLowerCase(Locale.ROOT));
121+
}
122+
if (ESTestCase.randomBoolean()) {
123+
boolean waitForNoInitializingShards = ESTestCase.randomBoolean();
124+
healthRequest.waitForNoInitializingShards(waitForNoInitializingShards);
125+
if (waitForNoInitializingShards) {
126+
expectedParams.put("wait_for_no_initializing_shards", Boolean.TRUE.toString());
127+
}
128+
}
129+
if (ESTestCase.randomBoolean()) {
130+
boolean waitForNoRelocatingShards = ESTestCase.randomBoolean();
131+
healthRequest.waitForNoRelocatingShards(waitForNoRelocatingShards);
132+
if (waitForNoRelocatingShards) {
133+
expectedParams.put("wait_for_no_relocating_shards", Boolean.TRUE.toString());
134+
}
135+
}
136+
String[] indices = ESTestCase.randomBoolean() ? null : RequestConvertersTests.randomIndicesNames(0, 5);
137+
healthRequest.indices(indices);
138+
139+
Request request = ClusterRequestConverters.clusterHealth(healthRequest);
140+
Assert.assertThat(request, CoreMatchers.notNullValue());
141+
Assert.assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME));
142+
Assert.assertThat(request.getEntity(), nullValue());
143+
if (indices != null && indices.length > 0) {
144+
Assert.assertThat(request.getEndpoint(), equalTo("/_cluster/health/" + String.join(",", indices)));
145+
} else {
146+
Assert.assertThat(request.getEndpoint(), equalTo("/_cluster/health"));
147+
}
148+
Assert.assertThat(request.getParameters(), equalTo(expectedParams));
149+
}
150+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ protected static void clusterUpdateSettings(Settings persistentSettings,
134134
ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest();
135135
request.persistentSettings(persistentSettings);
136136
request.transientSettings(transientSettings);
137-
assertOK(client().performRequest(RequestConverters.clusterPutSettings(request)));
137+
assertTrue(execute(
138+
request, highLevelClient().cluster()::putSettings, highLevelClient().cluster()::putSettingsAsync).isAcknowledged());
138139
}
139140
}

0 commit comments

Comments
 (0)