-
Notifications
You must be signed in to change notification settings - Fork 24.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Cluster Put Settings API to the high level REST client #28633
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9378473
Add Cluster Update Settings API to the high level REST client
olcbean a930033
address reviewers' comments
olcbean 432b659
address reviewer comments
olcbean dda7951
Turn off the formatter for all sections which were
olcbean 131a05f
modify tests
olcbean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.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,66 @@ | ||
/* | ||
* 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.Header; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; | ||
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse; | ||
|
||
import java.io.IOException; | ||
|
||
import static java.util.Collections.emptySet; | ||
|
||
/** | ||
* A wrapper for the {@link RestHighLevelClient} that provides methods for accessing the Cluster API. | ||
* <p> | ||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster.html">Cluster API on elastic.co</a> | ||
*/ | ||
public final class ClusterClient { | ||
private final RestHighLevelClient restHighLevelClient; | ||
|
||
ClusterClient(RestHighLevelClient restHighLevelClient) { | ||
this.restHighLevelClient = restHighLevelClient; | ||
} | ||
|
||
/** | ||
* Updates cluster wide specific settings using the Cluster Update Settings API | ||
* <p> | ||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-update-settings.html"> Cluster Update Settings | ||
* API on elastic.co</a> | ||
*/ | ||
public ClusterUpdateSettingsResponse putSettings(ClusterUpdateSettingsRequest clusterUpdateSettingsRequest, Header... headers) | ||
throws IOException { | ||
return restHighLevelClient.performRequestAndParseEntity(clusterUpdateSettingsRequest, Request::clusterPutSettings, | ||
ClusterUpdateSettingsResponse::fromXContent, emptySet(), headers); | ||
} | ||
|
||
/** | ||
* Asynchronously updates cluster wide specific settings using the Cluster Update Settings API | ||
* <p> | ||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-update-settings.html"> Cluster Update Settings | ||
* API on elastic.co</a> | ||
*/ | ||
public void putSettingsAsync(ClusterUpdateSettingsRequest clusterUpdateSettingsRequest, | ||
ActionListener<ClusterUpdateSettingsResponse> listener, Header... headers) { | ||
restHighLevelClient.performRequestAsyncAndParseEntity(clusterUpdateSettingsRequest, Request::clusterPutSettings, | ||
ClusterUpdateSettingsResponse::fromXContent, listener, emptySet(), headers); | ||
} | ||
} |
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
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
108 changes: 108 additions & 0 deletions
108
client/rest-high-level/src/test/java/org/elasticsearch/client/ClusterClientIT.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,108 @@ | ||
/* | ||
* 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.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; | ||
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse; | ||
import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.unit.ByteSizeUnit; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
import org.elasticsearch.common.xcontent.support.XContentMapValues; | ||
import org.elasticsearch.indices.recovery.RecoverySettings; | ||
import org.elasticsearch.rest.RestStatus; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
public class ClusterClientIT extends ESRestHighLevelClientTestCase { | ||
|
||
public void testClusterPutSettings() throws IOException { | ||
final String transientSettingKey = RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey(); | ||
final int transientSettingValue = 10; | ||
|
||
final String persistentSettingKey = EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(); | ||
final String persistentSettingValue = EnableAllocationDecider.Allocation.NONE.name(); | ||
|
||
Settings transientSettings = Settings.builder().put(transientSettingKey, transientSettingValue, ByteSizeUnit.BYTES).build(); | ||
Map<String, Object> map = new HashMap<>(); | ||
map.put(persistentSettingKey, persistentSettingValue); | ||
|
||
ClusterUpdateSettingsRequest setRequest = new ClusterUpdateSettingsRequest(); | ||
setRequest.transientSettings(transientSettings); | ||
setRequest.persistentSettings(map); | ||
|
||
ClusterUpdateSettingsResponse setResponse = execute(setRequest, highLevelClient().cluster()::putSettings, | ||
highLevelClient().cluster()::putSettingsAsync); | ||
|
||
assertAcked(setResponse); | ||
assertThat(setResponse.getTransientSettings().get(transientSettingKey), notNullValue()); | ||
assertThat(setResponse.getTransientSettings().get(persistentSettingKey), nullValue()); | ||
assertThat(setResponse.getTransientSettings().get(transientSettingKey), | ||
equalTo(transientSettingValue + ByteSizeUnit.BYTES.getSuffix())); | ||
assertThat(setResponse.getPersistentSettings().get(transientSettingKey), nullValue()); | ||
assertThat(setResponse.getPersistentSettings().get(persistentSettingKey), notNullValue()); | ||
assertThat(setResponse.getPersistentSettings().get(persistentSettingKey), equalTo(persistentSettingValue)); | ||
|
||
Map<String, Object> setMap = getAsMap("/_cluster/settings"); | ||
String transientSetValue = (String) XContentMapValues.extractValue("transient." + transientSettingKey, setMap); | ||
assertThat(transientSetValue, equalTo(transientSettingValue + ByteSizeUnit.BYTES.getSuffix())); | ||
String persistentSetValue = (String) XContentMapValues.extractValue("persistent." + persistentSettingKey, setMap); | ||
assertThat(persistentSetValue, equalTo(persistentSettingValue)); | ||
|
||
ClusterUpdateSettingsRequest resetRequest = new ClusterUpdateSettingsRequest(); | ||
resetRequest.transientSettings(Settings.builder().putNull(transientSettingKey)); | ||
resetRequest.persistentSettings("{\"" + persistentSettingKey + "\": null }", XContentType.JSON); | ||
|
||
ClusterUpdateSettingsResponse resetResponse = execute(resetRequest, highLevelClient().cluster()::putSettings, | ||
highLevelClient().cluster()::putSettingsAsync); | ||
|
||
assertThat(resetResponse.getTransientSettings().get(transientSettingKey), equalTo(null)); | ||
assertThat(resetResponse.getPersistentSettings().get(persistentSettingKey), equalTo(null)); | ||
assertThat(resetResponse.getTransientSettings(), equalTo(Settings.EMPTY)); | ||
assertThat(resetResponse.getPersistentSettings(), equalTo(Settings.EMPTY)); | ||
|
||
Map<String, Object> resetMap = getAsMap("/_cluster/settings"); | ||
String transientResetValue = (String) XContentMapValues.extractValue("transient." + transientSettingKey, resetMap); | ||
assertThat(transientResetValue, equalTo(null)); | ||
String persistentResetValue = (String) XContentMapValues.extractValue("persistent." + persistentSettingKey, resetMap); | ||
assertThat(persistentResetValue, equalTo(null)); | ||
} | ||
|
||
public void testClusterUpdateSettingNonExistent() { | ||
String setting = "no_idea_what_you_are_talking_about"; | ||
int value = 10; | ||
ClusterUpdateSettingsRequest clusterUpdateSettingsRequest = new ClusterUpdateSettingsRequest(); | ||
clusterUpdateSettingsRequest.transientSettings(Settings.builder().put(setting, value).build()); | ||
|
||
ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(clusterUpdateSettingsRequest, | ||
highLevelClient().cluster()::putSettings, highLevelClient().cluster()::putSettingsAsync)); | ||
assertThat(exception.status(), equalTo(RestStatus.BAD_REQUEST)); | ||
assertThat(exception.getMessage(), equalTo( | ||
"Elasticsearch exception [type=illegal_argument_exception, reason=transient setting [" + setting + "], not recognized]")); | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
import org.apache.http.entity.StringEntity; | ||
import org.apache.http.util.EntityUtils; | ||
import org.elasticsearch.action.DocWriteRequest; | ||
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; | ||
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; | ||
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions; | ||
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; | ||
|
@@ -272,7 +273,7 @@ public void testIndicesExist() { | |
Map<String, String> expectedParams = new HashMap<>(); | ||
setRandomIndicesOptions(getIndexRequest::indicesOptions, getIndexRequest::indicesOptions, expectedParams); | ||
setRandomLocal(getIndexRequest, expectedParams); | ||
setRandomFlatSettings(getIndexRequest, expectedParams); | ||
setRandomFlatSettings(getIndexRequest::flatSettings, expectedParams); | ||
setRandomHumanReadable(getIndexRequest, expectedParams); | ||
setRandomIncludeDefaults(getIndexRequest, expectedParams); | ||
|
||
|
@@ -1115,14 +1116,10 @@ private static void resizeTest(ResizeType resizeType, CheckedFunction<ResizeRequ | |
if (randomBoolean()) { | ||
randomAliases(createIndexRequest); | ||
} | ||
if (randomBoolean()) { | ||
setRandomWaitForActiveShards(createIndexRequest::waitForActiveShards, expectedParams); | ||
} | ||
setRandomWaitForActiveShards(createIndexRequest::waitForActiveShards, expectedParams); | ||
resizeRequest.setTargetIndex(createIndexRequest); | ||
} else { | ||
if (randomBoolean()) { | ||
setRandomWaitForActiveShards(resizeRequest::setWaitForActiveShards, expectedParams); | ||
} | ||
setRandomWaitForActiveShards(resizeRequest::setWaitForActiveShards, expectedParams); | ||
} | ||
|
||
Request request = function.apply(resizeRequest); | ||
|
@@ -1133,6 +1130,19 @@ private static void resizeTest(ResizeType resizeType, CheckedFunction<ResizeRequ | |
assertEquals(expectedParams, request.getParameters()); | ||
assertToXContentBody(resizeRequest, request.getEntity()); | ||
} | ||
|
||
public void testClusterPutSettings() throws IOException { | ||
ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest(); | ||
Map<String, String> expectedParams = new HashMap<>(); | ||
setRandomFlatSettings(request::flatSettings, expectedParams); | ||
setRandomMasterTimeout(request, expectedParams); | ||
setRandomTimeout(request::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); | ||
|
||
Request expectedRequest = Request.clusterPutSettings(request); | ||
assertEquals("/_cluster/settings", expectedRequest.getEndpoint()); | ||
assertEquals(HttpPut.METHOD_NAME, expectedRequest.getMethod()); | ||
assertEquals(expectedParams, expectedRequest.getParameters()); | ||
} | ||
|
||
private static void assertToXContentBody(ToXContent expectedBody, HttpEntity actualEntity) throws IOException { | ||
BytesReference expectedBytes = XContentHelper.toXContent(expectedBody, REQUEST_BODY_CONTENT_TYPE, false); | ||
|
@@ -1289,16 +1299,6 @@ private static void setRandomHumanReadable(GetIndexRequest request, Map<String, | |
} | ||
} | ||
|
||
private static void setRandomFlatSettings(GetIndexRequest request, Map<String, String> expectedParams) { | ||
if (randomBoolean()) { | ||
boolean flatSettings = randomBoolean(); | ||
request.flatSettings(flatSettings); | ||
if (flatSettings) { | ||
expectedParams.put("flat_settings", String.valueOf(flatSettings)); | ||
} | ||
} | ||
} | ||
|
||
private static void setRandomLocal(MasterNodeReadRequest<?> request, Map<String, String> expectedParams) { | ||
if (randomBoolean()) { | ||
boolean local = randomBoolean(); | ||
|
@@ -1319,6 +1319,16 @@ private static void setRandomTimeout(Consumer<String> setter, TimeValue defaultT | |
} | ||
} | ||
|
||
private static void setRandomFlatSettings(Consumer<Boolean> setter, Map<String, String> expectedParams) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we can replace the existing |
||
if (randomBoolean()) { | ||
boolean flatSettings = randomBoolean(); | ||
setter.accept(flatSettings); | ||
if (flatSettings) { | ||
expectedParams.put("flat_settings", String.valueOf(flatSettings)); | ||
} | ||
} | ||
} | ||
|
||
private static void setRandomMasterTimeout(MasterNodeRequest<?> request, Map<String, String> expectedParams) { | ||
if (randomBoolean()) { | ||
String masterTimeout = randomTimeValue(); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why these two changes? I meant to to randomly choose one of the two branches, but then still set the param only randomly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (randomBoolean())
is already wrapped in thesetRandom*
. If we keep it here, thenwaitForRandomShards
is set to a random value only 25% of the time.I assumed that it was an oversight...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh right :) thanks