-
Notifications
You must be signed in to change notification settings - Fork 24.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
6 changed files
with
250 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterRequestConverters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.client; | ||
|
||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpPut; | ||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; | ||
import org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsRequest; | ||
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; | ||
import org.elasticsearch.action.support.ActiveShardCount; | ||
import org.elasticsearch.common.Strings; | ||
|
||
import java.io.IOException; | ||
|
||
final class ClusterRequestConverters { | ||
|
||
static Request clusterPutSettings(ClusterUpdateSettingsRequest clusterUpdateSettingsRequest) throws IOException { | ||
Request request = new Request(HttpPut.METHOD_NAME, "/_cluster/settings"); | ||
|
||
RequestConverters.Params parameters = new RequestConverters.Params(request); | ||
parameters.withTimeout(clusterUpdateSettingsRequest.timeout()); | ||
parameters.withMasterTimeout(clusterUpdateSettingsRequest.masterNodeTimeout()); | ||
|
||
request.setEntity(RequestConverters.createEntity(clusterUpdateSettingsRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE)); | ||
return request; | ||
} | ||
|
||
static Request clusterGetSettings(ClusterGetSettingsRequest clusterGetSettingsRequest) throws IOException { | ||
Request request = new Request(HttpGet.METHOD_NAME, "/_cluster/settings"); | ||
|
||
RequestConverters.Params parameters = new RequestConverters.Params(request); | ||
parameters.withLocal(clusterGetSettingsRequest.local()); | ||
parameters.withIncludeDefaults(clusterGetSettingsRequest.includeDefaults()); | ||
parameters.withMasterTimeout(clusterGetSettingsRequest.masterNodeTimeout()); | ||
|
||
return request; | ||
} | ||
|
||
static Request clusterHealth(ClusterHealthRequest healthRequest) { | ||
String[] indices = healthRequest.indices() == null ? Strings.EMPTY_ARRAY : healthRequest.indices(); | ||
String endpoint = new RequestConverters.EndpointBuilder() | ||
.addPathPartAsIs("_cluster/health") | ||
.addCommaSeparatedPathParts(indices) | ||
.build(); | ||
Request request = new Request(HttpGet.METHOD_NAME, endpoint); | ||
|
||
new RequestConverters.Params(request) | ||
.withWaitForStatus(healthRequest.waitForStatus()) | ||
.withWaitForNoRelocatingShards(healthRequest.waitForNoRelocatingShards()) | ||
.withWaitForNoInitializingShards(healthRequest.waitForNoInitializingShards()) | ||
.withWaitForActiveShards(healthRequest.waitForActiveShards(), ActiveShardCount.NONE) | ||
.withWaitForNodes(healthRequest.waitForNodes()) | ||
.withWaitForEvents(healthRequest.waitForEvents()) | ||
.withTimeout(healthRequest.timeout()) | ||
.withMasterTimeout(healthRequest.masterNodeTimeout()) | ||
.withLocal(healthRequest.local()) | ||
.withLevel(healthRequest.level()); | ||
return request; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
...rest-high-level/src/test/java/org/elasticsearch/client/ClusterRequestConvertersTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.client; | ||
|
||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpPut; | ||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; | ||
import org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsRequest; | ||
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; | ||
import org.elasticsearch.action.support.ActiveShardCount; | ||
import org.elasticsearch.action.support.master.AcknowledgedRequest; | ||
import org.elasticsearch.cluster.health.ClusterHealthStatus; | ||
import org.elasticsearch.common.Priority; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.hamcrest.CoreMatchers; | ||
import org.junit.Assert; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
import static org.elasticsearch.client.RequestConvertersTests.randomIndicesNames; | ||
import static org.elasticsearch.client.RequestConvertersTests.setRandomLocal; | ||
import static org.elasticsearch.client.RequestConvertersTests.setRandomWaitForActiveShards; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
public class ClusterRequestConvertersTests extends ESTestCase { | ||
|
||
public void testClusterPutSettings() throws IOException { | ||
ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest(); | ||
Map<String, String> expectedParams = new HashMap<>(); | ||
RequestConvertersTests.setRandomMasterTimeout(request, expectedParams); | ||
RequestConvertersTests.setRandomTimeout(request::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); | ||
|
||
Request expectedRequest = ClusterRequestConverters.clusterPutSettings(request); | ||
Assert.assertEquals("/_cluster/settings", expectedRequest.getEndpoint()); | ||
Assert.assertEquals(HttpPut.METHOD_NAME, expectedRequest.getMethod()); | ||
Assert.assertEquals(expectedParams, expectedRequest.getParameters()); | ||
} | ||
|
||
public void testClusterGetSettings() throws IOException { | ||
ClusterGetSettingsRequest request = new ClusterGetSettingsRequest(); | ||
Map<String, String> expectedParams = new HashMap<>(); | ||
RequestConvertersTests.setRandomMasterTimeout(request, expectedParams); | ||
request.includeDefaults(ESTestCase.randomBoolean()); | ||
if (request.includeDefaults()) { | ||
expectedParams.put("include_defaults", String.valueOf(true)); | ||
} | ||
|
||
Request expectedRequest = ClusterRequestConverters.clusterGetSettings(request); | ||
Assert.assertEquals("/_cluster/settings", expectedRequest.getEndpoint()); | ||
Assert.assertEquals(HttpGet.METHOD_NAME, expectedRequest.getMethod()); | ||
Assert.assertEquals(expectedParams, expectedRequest.getParameters()); | ||
} | ||
|
||
public void testClusterHealth() { | ||
ClusterHealthRequest healthRequest = new ClusterHealthRequest(); | ||
Map<String, String> expectedParams = new HashMap<>(); | ||
setRandomLocal(healthRequest, expectedParams); | ||
String timeoutType = randomFrom("timeout", "masterTimeout", "both", "none"); | ||
String timeout = randomTimeValue(); | ||
String masterTimeout = randomTimeValue(); | ||
switch (timeoutType) { | ||
case "timeout": | ||
healthRequest.timeout(timeout); | ||
expectedParams.put("timeout", timeout); | ||
// If Master Timeout wasn't set it uses the same value as Timeout | ||
expectedParams.put("master_timeout", timeout); | ||
break; | ||
case "masterTimeout": | ||
expectedParams.put("timeout", "30s"); | ||
healthRequest.masterNodeTimeout(masterTimeout); | ||
expectedParams.put("master_timeout", masterTimeout); | ||
break; | ||
case "both": | ||
healthRequest.timeout(timeout); | ||
expectedParams.put("timeout", timeout); | ||
healthRequest.masterNodeTimeout(timeout); | ||
expectedParams.put("master_timeout", timeout); | ||
break; | ||
case "none": | ||
expectedParams.put("timeout", "30s"); | ||
expectedParams.put("master_timeout", "30s"); | ||
break; | ||
default: | ||
throw new UnsupportedOperationException(); | ||
} | ||
setRandomWaitForActiveShards(healthRequest::waitForActiveShards, ActiveShardCount.NONE, expectedParams); | ||
if (randomBoolean()) { | ||
ClusterHealthRequest.Level level = randomFrom(ClusterHealthRequest.Level.values()); | ||
healthRequest.level(level); | ||
expectedParams.put("level", level.name().toLowerCase(Locale.ROOT)); | ||
} else { | ||
expectedParams.put("level", "shards"); | ||
} | ||
if (randomBoolean()) { | ||
Priority priority = randomFrom(Priority.values()); | ||
healthRequest.waitForEvents(priority); | ||
expectedParams.put("wait_for_events", priority.name().toLowerCase(Locale.ROOT)); | ||
} | ||
if (randomBoolean()) { | ||
ClusterHealthStatus status = randomFrom(ClusterHealthStatus.values()); | ||
healthRequest.waitForStatus(status); | ||
expectedParams.put("wait_for_status", status.name().toLowerCase(Locale.ROOT)); | ||
} | ||
if (randomBoolean()) { | ||
boolean waitForNoInitializingShards = randomBoolean(); | ||
healthRequest.waitForNoInitializingShards(waitForNoInitializingShards); | ||
if (waitForNoInitializingShards) { | ||
expectedParams.put("wait_for_no_initializing_shards", Boolean.TRUE.toString()); | ||
} | ||
} | ||
if (randomBoolean()) { | ||
boolean waitForNoRelocatingShards = randomBoolean(); | ||
healthRequest.waitForNoRelocatingShards(waitForNoRelocatingShards); | ||
if (waitForNoRelocatingShards) { | ||
expectedParams.put("wait_for_no_relocating_shards", Boolean.TRUE.toString()); | ||
} | ||
} | ||
String[] indices = randomBoolean() ? null : randomIndicesNames(0, 5); | ||
healthRequest.indices(indices); | ||
|
||
Request request = ClusterRequestConverters.clusterHealth(healthRequest); | ||
assertThat(request, CoreMatchers.notNullValue()); | ||
assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME)); | ||
assertThat(request.getEntity(), nullValue()); | ||
if (indices != null && indices.length > 0) { | ||
assertThat(request.getEndpoint(), equalTo("/_cluster/health/" + String.join(",", indices))); | ||
} else { | ||
assertThat(request.getEndpoint(), equalTo("/_cluster/health")); | ||
} | ||
assertThat(request.getParameters(), equalTo(expectedParams)); | ||
} | ||
} |
Oops, something went wrong.