-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
REST high-level client: add Cluster Health API #29331
Changes from 2 commits
27d4ce2
d177736
18dfa6e
af3ba59
67eb608
4f0c01e
5a6ed00
31f587d
d4486ab
bd24a05
573990d
8030867
27cbf80
31b5bee
a529058
2f6f640
00a848f
33d9692
27c90a8
f4c8b3f
4921335
982dd98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
Elasticsearch | ||
Copyright 2009-2017 Elasticsearch | ||
Copyright 2009-2018 Elasticsearch | ||
|
||
This product includes software developed by The Apache Software | ||
Foundation (http://www.apache.org/). |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
import org.apache.http.entity.ContentType; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.action.DocWriteRequest; | ||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; | ||
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; | ||
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; | ||
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; | ||
|
@@ -58,6 +59,7 @@ | |
import org.elasticsearch.action.support.IndicesOptions; | ||
import org.elasticsearch.action.support.WriteRequest; | ||
import org.elasticsearch.action.update.UpdateRequest; | ||
import org.elasticsearch.cluster.health.ClusterHealthStatus; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.SuppressForbidden; | ||
|
@@ -583,6 +585,22 @@ static Request clusterPutSettings(ClusterUpdateSettingsRequest clusterUpdateSett | |
return new Request(HttpPut.METHOD_NAME, "/_cluster/settings", parameters.getParams(), entity); | ||
} | ||
|
||
static Request clusterHealth(ClusterHealthRequest healthRequest) { | ||
Params params = Params.builder(); | ||
params.withWaitForStatus(healthRequest.waitForStatus()); | ||
params.withWaitForNoRelocatingShards(healthRequest.waitForNoRelocatingShards()); | ||
params.withWaitForNoInitializingShards(healthRequest.waitForNoInitializingShards()); | ||
params.withWaitForActiveShards(healthRequest.waitForActiveShards()); | ||
params.withWaitForNodes(healthRequest.waitForNodes()); | ||
params.withTimeout(healthRequest.timeout()); | ||
params.withMasterTimeout(healthRequest.masterNodeTimeout()); | ||
params.withLocal(healthRequest.local()); | ||
params.putParam("level", "shards"); | ||
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. I see. Right now you request the whole thing because it is simpler that way. I think this is fine. I think if we decide we'd like to support 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. I had missed this line, so my comment on this is a bit off, sorry about that :) this is actually perfectly backwards compatible with the transport client behaviour, as it gets exactly everything. I think that we could add the level parameter though so that we can properly filter what gets returned at REST like every client works. |
||
String[] indices = healthRequest.indices() == null ? Strings.EMPTY_ARRAY : healthRequest.indices(); | ||
String endpoint = endpoint(indices, "_cluster/health"); | ||
return new Request(HttpGet.METHOD_NAME, endpoint, params.getParams(), null); | ||
} | ||
|
||
static Request rollover(RolloverRequest rolloverRequest) throws IOException { | ||
Params params = Params.builder(); | ||
params.withTimeout(rolloverRequest.timeout()); | ||
|
@@ -833,6 +851,31 @@ Params withIncludeDefaults(boolean includeDefaults) { | |
return this; | ||
} | ||
|
||
Params withWaitForStatus(ClusterHealthStatus status) { | ||
if (status != null) { | ||
return putParam("wait_for_status", status.name().toLowerCase(Locale.ROOT)); | ||
} | ||
return this; | ||
} | ||
|
||
Params withWaitForNoRelocatingShards(boolean waitNoRelocatingShards) { | ||
if (waitNoRelocatingShards) { | ||
return putParam("wait_for_no_relocating_shards", Boolean.TRUE.toString()); | ||
} | ||
return this; | ||
} | ||
|
||
Params withWaitForNoInitializingShards(boolean waitNoInitShards) { | ||
if (waitNoInitShards) { | ||
return putParam("wait_for_no_initializing_shards", Boolean.TRUE.toString()); | ||
} | ||
return this; | ||
} | ||
|
||
Params withWaitForNodes(String waitForNodes) { | ||
return putParam("wait_for_nodes", waitForNodes); | ||
} | ||
|
||
Map<String, String> getParams() { | ||
return Collections.unmodifiableMap(params); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,11 @@ | |
package org.elasticsearch.client; | ||
|
||
import org.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; | ||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; | ||
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; | ||
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse; | ||
import org.elasticsearch.cluster.health.ClusterHealthStatus; | ||
import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.unit.ByteSizeUnit; | ||
|
@@ -34,8 +37,10 @@ | |
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static java.util.Collections.emptyMap; | ||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
|
@@ -105,4 +110,21 @@ public void testClusterUpdateSettingNonExistent() { | |
assertThat(exception.getMessage(), equalTo( | ||
"Elasticsearch exception [type=illegal_argument_exception, reason=transient setting [" + setting + "], not recognized]")); | ||
} | ||
|
||
public void testClusterHealth() throws IOException { | ||
ClusterHealthRequest request = new ClusterHealthRequest(); | ||
ClusterHealthResponse response = execute(request, highLevelClient().cluster()::health, highLevelClient().cluster()::healthAsync); | ||
|
||
assertThat(response, notNullValue()); | ||
assertThat(response.isTimedOut(), is(false)); | ||
assertThat(response.status(), is(RestStatus.OK)); | ||
assertThat(response.getStatus(), is(ClusterHealthStatus.GREEN)); | ||
assertThat(response.getIndices(), is(emptyMap())); | ||
assertThat(response.getActivePrimaryShards(), is(0)); | ||
assertThat(response.getActiveShards(), is(0)); | ||
assertThat(response.getDelayedUnassignedShards(), is(0)); | ||
assertThat(response.getInitializingShards(), is(0)); | ||
assertThat(response.getUnassignedShards(), is(0)); | ||
assertThat(response.getActiveShardsPercent(), is(100d)); | ||
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. we should probably add a test that provides indices and the level parameter |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
import org.apache.http.util.EntityUtils; | ||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.action.DocWriteRequest; | ||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; | ||
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; | ||
|
@@ -127,6 +128,8 @@ | |
import static org.elasticsearch.search.RandomSearchRequestGenerator.randomSearchRequest; | ||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
public class RequestTests extends ESTestCase { | ||
|
@@ -1325,6 +1328,25 @@ public void testClusterPutSettings() throws IOException { | |
assertEquals(expectedParams, expectedRequest.getParameters()); | ||
} | ||
|
||
public void testClusterHealth() { | ||
ClusterHealthRequest healthRequest = new ClusterHealthRequest(); | ||
Map<String, String> expectedParams = new HashMap<>(); | ||
setRandomLocal(healthRequest, expectedParams); | ||
setRandomTimeout(healthRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); | ||
setRandomMasterTimeout(healthRequest, expectedParams); | ||
expectedParams.put("level", "shards"); | ||
// Default value in ClusterHealthRequest is NONE but in Request.Params::withWaitForActiveShards is DEFAULT | ||
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. I see, you may have to make changes here like we already did in the 6.x branch where we have a different default for some API. 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. Can you please clarify it? If I set in ClusterHealthRequest field
Probably it can instead of failing set it to 0. 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. I mean to adapt the test, doing something along these lines (from the 6.x branch): https://github.com/elastic/elasticsearch/blob/6.x/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestTests.java#L1649 . |
||
expectedParams.put("wait_for_active_shards", "0"); | ||
//TODO add random filling for other properties | ||
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. ++ |
||
|
||
Request request = Request.clusterHealth(healthRequest); | ||
assertThat(request, is(notNullValue())); | ||
assertThat(request.getMethod(), is(HttpGet.METHOD_NAME)); | ||
assertThat(request.getEntity(), is(nullValue())); | ||
assertThat(request.getEndpoint(), is("/_cluster/health")); | ||
assertThat(request.getParameters(), is(expectedParams)); | ||
} | ||
|
||
public void testRollover() throws IOException { | ||
RolloverRequest rolloverRequest = new RolloverRequest(randomAlphaOfLengthBetween(3, 10), | ||
randomBoolean() ? null : randomAlphaOfLengthBetween(3, 10)); | ||
|
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.
please could you revert this? This change should not be made as part of this PR, regardless of whether it's needed or not.