Skip to content

Commit

Permalink
Add Get Settings API support to java high-level rest client (#29229)
Browse files Browse the repository at this point in the history
This PR adds support for the Get Settings API to the java high-level rest client.
Furthermore, logic related to the retrieval of default settings has been moved from the rest layer into the transport layer and now default settings may be retrieved consistency via both the rest API and the transport API.
  • Loading branch information
tomcallahan authored May 4, 2018
1 parent bf51a21 commit 0a93956
Show file tree
Hide file tree
Showing 16 changed files with 1,046 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
Expand Down Expand Up @@ -265,6 +267,28 @@ public void flushAsync(FlushRequest flushRequest, ActionListener<FlushResponse>
listener, emptySet(), headers);
}

/**
* Retrieve the settings of one or more indices
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-settings.html">
* Indices Get Settings API on elastic.co</a>
*/
public GetSettingsResponse getSettings(GetSettingsRequest getSettingsRequest, Header... headers) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(getSettingsRequest, RequestConverters::getSettings,
GetSettingsResponse::fromXContent, emptySet(), headers);
}

/**
* Asynchronously retrieve the settings of one or more indices
* <p>
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-settings.html">
* Indices Get Settings API on elastic.co</a>
*/
public void getSettingsAsync(GetSettingsRequest getSettingsRequest, ActionListener<GetSettingsResponse> listener, Header... headers) {
restHighLevelClient.performRequestAsyncAndParseEntity(getSettingsRequest, RequestConverters::getSettings,
GetSettingsResponse::fromXContent, listener, emptySet(), headers);
}

/**
* Force merge one or more indices using the Force Merge API
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
import org.elasticsearch.action.bulk.BulkRequest;
Expand Down Expand Up @@ -600,6 +601,22 @@ static Request rollover(RolloverRequest rolloverRequest) throws IOException {
return request;
}

static Request getSettings(GetSettingsRequest getSettingsRequest) throws IOException {
String[] indices = getSettingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getSettingsRequest.indices();
String[] names = getSettingsRequest.names() == null ? Strings.EMPTY_ARRAY : getSettingsRequest.names();

String endpoint = endpoint(indices, "_settings", names);
Request request = new Request(HttpGet.METHOD_NAME, endpoint);

Params params = new Params(request);
params.withIndicesOptions(getSettingsRequest.indicesOptions());
params.withLocal(getSettingsRequest.local());
params.withIncludeDefaults(getSettingsRequest.includeDefaults());
params.withMasterTimeout(getSettingsRequest.masterNodeTimeout());

return request;
}

static Request indicesExist(GetIndexRequest getIndexRequest) {
// this can be called with no indices as argument by transport client, not via REST though
if (getIndexRequest.indices() == null || getIndexRequest.indices().length == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
Expand Down Expand Up @@ -189,6 +191,108 @@ public void testCreateIndex() throws IOException {
}
}

public void testGetSettings() throws IOException {
String indexName = "get_settings_index";
Settings basicSettings = Settings.builder()
.put("number_of_shards", 1)
.put("number_of_replicas", 0)
.build();
createIndex(indexName, basicSettings);

GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indexName);
GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
highLevelClient().indices()::getSettingsAsync);

assertNull(getSettingsResponse.getSetting(indexName, "index.refresh_interval"));
assertEquals("1", getSettingsResponse.getSetting(indexName, "index.number_of_shards"));

updateIndexSettings(indexName, Settings.builder().put("refresh_interval", "30s"));

GetSettingsResponse updatedResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
highLevelClient().indices()::getSettingsAsync);
assertEquals("30s", updatedResponse.getSetting(indexName, "index.refresh_interval"));
}

public void testGetSettingsNonExistentIndex() throws IOException {
String nonExistentIndex = "index_that_doesnt_exist";
assertFalse(indexExists(nonExistentIndex));

GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(nonExistentIndex);
ElasticsearchException exception = expectThrows(ElasticsearchException.class,
() -> execute(getSettingsRequest, highLevelClient().indices()::getSettings, highLevelClient().indices()::getSettingsAsync));
assertEquals(RestStatus.NOT_FOUND, exception.status());
}

public void testGetSettingsFromMultipleIndices() throws IOException {
String indexName1 = "get_multiple_settings_one";
createIndex(indexName1, Settings.builder().put("number_of_shards", 2).build());

String indexName2 = "get_multiple_settings_two";
createIndex(indexName2, Settings.builder().put("number_of_shards", 3).build());

GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices("get_multiple_settings*");
GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
highLevelClient().indices()::getSettingsAsync);

assertEquals("2", getSettingsResponse.getSetting(indexName1, "index.number_of_shards"));
assertEquals("3", getSettingsResponse.getSetting(indexName2, "index.number_of_shards"));
}

public void testGetSettingsFiltered() throws IOException {
String indexName = "get_settings_index";
Settings basicSettings = Settings.builder()
.put("number_of_shards", 1)
.put("number_of_replicas", 0)
.build();
createIndex(indexName, basicSettings);

GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indexName).names("index.number_of_shards");
GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
highLevelClient().indices()::getSettingsAsync);

assertNull(getSettingsResponse.getSetting(indexName, "index.number_of_replicas"));
assertEquals("1", getSettingsResponse.getSetting(indexName, "index.number_of_shards"));
assertEquals(1, getSettingsResponse.getIndexToSettings().get("get_settings_index").size());
}

public void testGetSettingsWithDefaults() throws IOException {
String indexName = "get_settings_index";
Settings basicSettings = Settings.builder()
.put("number_of_shards", 1)
.put("number_of_replicas", 0)
.build();
createIndex(indexName, basicSettings);

GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indexName).includeDefaults(true);
GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
highLevelClient().indices()::getSettingsAsync);

assertNotNull(getSettingsResponse.getSetting(indexName, "index.refresh_interval"));
assertEquals(IndexSettings.DEFAULT_REFRESH_INTERVAL,
getSettingsResponse.getIndexToDefaultSettings().get("get_settings_index").getAsTime("index.refresh_interval", null));
assertEquals("1", getSettingsResponse.getSetting(indexName, "index.number_of_shards"));
}

public void testGetSettingsWithDefaultsFiltered() throws IOException {
String indexName = "get_settings_index";
Settings basicSettings = Settings.builder()
.put("number_of_shards", 1)
.put("number_of_replicas", 0)
.build();
createIndex(indexName, basicSettings);

GetSettingsRequest getSettingsRequest = new GetSettingsRequest()
.indices(indexName)
.names("index.refresh_interval")
.includeDefaults(true);
GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
highLevelClient().indices()::getSettingsAsync);

assertNull(getSettingsResponse.getSetting(indexName, "index.number_of_replicas"));
assertNull(getSettingsResponse.getSetting(indexName, "index.number_of_shards"));
assertEquals(0, getSettingsResponse.getIndexToSettings().get("get_settings_index").size());
assertEquals(1, getSettingsResponse.getIndexToDefaultSettings().get("get_settings_index").size());
}
public void testPutMapping() throws IOException {
{
// Add mappings to index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
import org.elasticsearch.action.bulk.BulkRequest;
Expand Down Expand Up @@ -76,6 +77,7 @@
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.lucene.uid.Versions;
import org.elasticsearch.common.settings.IndexScopedSettings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand Down Expand Up @@ -405,6 +407,52 @@ public void testDeleteIndex() {
assertNull(request.getEntity());
}

public void testGetSettings() throws IOException {
String[] indicesUnderTest = randomBoolean() ? null : randomIndicesNames(0, 5);

GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indicesUnderTest);

Map<String, String> expectedParams = new HashMap<>();
setRandomMasterTimeout(getSettingsRequest, expectedParams);
setRandomIndicesOptions(getSettingsRequest::indicesOptions, getSettingsRequest::indicesOptions, expectedParams);

setRandomLocal(getSettingsRequest, expectedParams);

if (randomBoolean()) {
//the request object will not have include_defaults present unless it is set to true
getSettingsRequest.includeDefaults(randomBoolean());
if (getSettingsRequest.includeDefaults()) {
expectedParams.put("include_defaults", Boolean.toString(true));
}
}

StringJoiner endpoint = new StringJoiner("/", "/", "");
if (indicesUnderTest != null && indicesUnderTest.length > 0) {
endpoint.add(String.join(",", indicesUnderTest));
}
endpoint.add("_settings");

if (randomBoolean()) {
String[] names = randomBoolean() ? null : new String[randomIntBetween(0, 3)];
if (names != null) {
for (int x = 0; x < names.length; x++) {
names[x] = randomAlphaOfLengthBetween(3, 10);
}
}
getSettingsRequest.names(names);
if (names != null && names.length > 0) {
endpoint.add(String.join(",", names));
}
}

Request request = RequestConverters.getSettings(getSettingsRequest);

assertThat(endpoint.toString(), equalTo(request.getEndpoint()));
assertThat(request.getParameters(), equalTo(expectedParams));
assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME));
assertThat(request.getEntity(), nullValue());
}

public void testDeleteIndexEmptyIndices() {
String[] indices = randomBoolean() ? null : Strings.EMPTY_ARRAY;
ActionRequestValidationException validationException = new DeleteIndexRequest(indices).validate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
Expand Down Expand Up @@ -775,6 +777,119 @@ public void onFailure(Exception e) {
}
}

public void testGetSettings() throws Exception {
RestHighLevelClient client = highLevelClient();

{
Settings settings = Settings.builder().put("number_of_shards", 3).build();
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index", settings));
assertTrue(createIndexResponse.isAcknowledged());
}

// tag::get-settings-request
GetSettingsRequest request = new GetSettingsRequest().indices("index");
// end::get-settings-request

// tag::get-settings-request-names
request.names("index.number_of_shards"); // <1>
// end::get-settings-request-names

// tag::get-settings-request-indicesOptions
request.indicesOptions(IndicesOptions.lenientExpandOpen()); // <1>
// end::get-settings-request-indicesOptions

// tag::get-settings-execute
GetSettingsResponse getSettingsResponse = client.indices().getSettings(request);
// end::get-settings-execute

// tag::get-settings-response
String numberOfShardsString = getSettingsResponse.getSetting("index", "index.number_of_shards"); // <1>
Settings indexSettings = getSettingsResponse.getIndexToSettings().get("index"); // <2>
Integer numberOfShards = indexSettings.getAsInt("index.number_of_shards", null); // <3>
// end::get-settings-response

assertEquals("3", numberOfShardsString);
assertEquals(Integer.valueOf(3), numberOfShards);

assertNull("refresh_interval returned but was never set!",
getSettingsResponse.getSetting("index", "index.refresh_interval"));

// tag::get-settings-execute-listener
ActionListener<GetSettingsResponse> listener =
new ActionListener<GetSettingsResponse>() {
@Override
public void onResponse(GetSettingsResponse GetSettingsResponse) {
// <1>
}

@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::get-settings-execute-listener

// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);

// tag::get-settings-execute-async
client.indices().getSettingsAsync(request, listener); // <1>
// end::get-settings-execute-async

assertTrue(latch.await(30L, TimeUnit.SECONDS));
}

public void testGetSettingsWithDefaults() throws Exception {
RestHighLevelClient client = highLevelClient();

{
Settings settings = Settings.builder().put("number_of_shards", 3).build();
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index", settings));
assertTrue(createIndexResponse.isAcknowledged());
}

GetSettingsRequest request = new GetSettingsRequest().indices("index");
request.indicesOptions(IndicesOptions.lenientExpandOpen());

// tag::get-settings-request-include-defaults
request.includeDefaults(true); // <1>
// end::get-settings-request-include-defaults

GetSettingsResponse getSettingsResponse = client.indices().getSettings(request);
String numberOfShardsString = getSettingsResponse.getSetting("index", "index.number_of_shards");
Settings indexSettings = getSettingsResponse.getIndexToSettings().get("index");
Integer numberOfShards = indexSettings.getAsInt("index.number_of_shards", null);

// tag::get-settings-defaults-response
String refreshInterval = getSettingsResponse.getSetting("index", "index.refresh_interval"); // <1>
Settings indexDefaultSettings = getSettingsResponse.getIndexToDefaultSettings().get("index"); // <2>
// end::get-settings-defaults-response

assertEquals("3", numberOfShardsString);
assertEquals(Integer.valueOf(3), numberOfShards);
assertNotNull("with defaults enabled we should get a value for refresh_interval!", refreshInterval);

assertEquals(refreshInterval, indexDefaultSettings.get("index.refresh_interval"));
ActionListener<GetSettingsResponse> listener =
new ActionListener<GetSettingsResponse>() {
@Override
public void onResponse(GetSettingsResponse GetSettingsResponse) {
}

@Override
public void onFailure(Exception e) {
}
};

// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);

client.indices().getSettingsAsync(request, listener);
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}

public void testForceMergeIndex() throws Exception {
RestHighLevelClient client = highLevelClient();

Expand Down
Loading

0 comments on commit 0a93956

Please sign in to comment.