-
Notifications
You must be signed in to change notification settings - Fork 24.6k
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 5 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 |
---|---|---|
|
@@ -20,8 +20,13 @@ | |
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.health.ClusterIndexHealth; | ||
import org.elasticsearch.cluster.health.ClusterShardHealth; | ||
import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.unit.ByteSizeUnit; | ||
|
@@ -34,6 +39,7 @@ | |
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.notNullValue; | ||
|
@@ -105,4 +111,137 @@ public void testClusterUpdateSettingNonExistent() { | |
assertThat(exception.getMessage(), equalTo( | ||
"Elasticsearch exception [type=illegal_argument_exception, reason=transient setting [" + setting + "], not recognized]")); | ||
} | ||
|
||
public void testClusterHealthGreen() throws IOException { | ||
ClusterHealthRequest request = new ClusterHealthRequest(); | ||
request.timeout("5s"); | ||
ClusterHealthResponse response = execute(request, highLevelClient().cluster()::health, highLevelClient().cluster()::healthAsync); | ||
|
||
assertThat(response, notNullValue()); | ||
assertThat(response.isTimedOut(), equalTo(false)); | ||
assertThat(response.status(), equalTo(RestStatus.OK)); | ||
assertThat(response.getStatus(), equalTo(ClusterHealthStatus.GREEN)); | ||
emptyClusterAssertion(response); | ||
} | ||
|
||
public void testClusterHealthYellowClusterLevel() throws IOException { | ||
createIndex("index", Settings.EMPTY); | ||
createIndex("index2", Settings.EMPTY); | ||
ClusterHealthRequest request = new ClusterHealthRequest(); | ||
request.timeout("5s"); | ||
request.level("cluster"); | ||
ClusterHealthResponse response = execute(request, highLevelClient().cluster()::health, highLevelClient().cluster()::healthAsync); | ||
|
||
yellowTenShardsClusterAssertion(response); | ||
assertThat(response.getIndices().size(), equalTo(0)); | ||
} | ||
|
||
public void testClusterHealthYellowIndicesLevel() throws IOException { | ||
createIndex("index", Settings.EMPTY); | ||
createIndex("index2", Settings.EMPTY); | ||
ClusterHealthRequest request = new ClusterHealthRequest(); | ||
request.timeout("5s"); | ||
request.level("indices"); | ||
ClusterHealthResponse response = execute(request, highLevelClient().cluster()::health, highLevelClient().cluster()::healthAsync); | ||
|
||
yellowTenShardsClusterAssertion(response); | ||
assertThat(response.getIndices().size(), equalTo(2)); | ||
for (Map.Entry<String, ClusterIndexHealth> entry : response.getIndices().entrySet()) { | ||
indexAssertion(entry.getKey(), entry.getValue(), true); | ||
} | ||
} | ||
|
||
private void yellowTenShardsClusterAssertion(ClusterHealthResponse response) { | ||
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. call it |
||
assertThat(response, notNullValue()); | ||
assertThat(response.isTimedOut(), equalTo(false)); | ||
assertThat(response.status(), equalTo(RestStatus.OK)); | ||
assertThat(response.getStatus(), equalTo(ClusterHealthStatus.YELLOW)); | ||
assertThat(response.getActivePrimaryShards(), equalTo(10)); | ||
assertThat(response.getNumberOfDataNodes(), equalTo(1)); | ||
assertThat(response.getNumberOfNodes(), equalTo(1)); | ||
assertThat(response.getActiveShards(), equalTo(10)); | ||
assertThat(response.getDelayedUnassignedShards(), equalTo(0)); | ||
assertThat(response.getInitializingShards(), equalTo(0)); | ||
assertThat(response.getUnassignedShards(), equalTo(10)); | ||
assertThat(response.getActiveShardsPercent(), equalTo(50d)); | ||
} | ||
|
||
|
||
public void testClusterHealthYellowSpecificIndex() throws IOException { | ||
createIndex("index", Settings.EMPTY); | ||
createIndex("index2", Settings.EMPTY); | ||
ClusterHealthRequest request = new ClusterHealthRequest("index"); | ||
request.timeout("5s"); | ||
ClusterHealthResponse response = execute(request, highLevelClient().cluster()::health, highLevelClient().cluster()::healthAsync); | ||
|
||
assertThat(response, notNullValue()); | ||
assertThat(response.isTimedOut(), equalTo(false)); | ||
assertThat(response.status(), equalTo(RestStatus.OK)); | ||
assertThat(response.getStatus(), equalTo(ClusterHealthStatus.YELLOW)); | ||
assertThat(response.getActivePrimaryShards(), equalTo(5)); | ||
assertThat(response.getNumberOfDataNodes(), equalTo(1)); | ||
assertThat(response.getNumberOfNodes(), equalTo(1)); | ||
assertThat(response.getActiveShards(), equalTo(5)); | ||
assertThat(response.getDelayedUnassignedShards(), equalTo(0)); | ||
assertThat(response.getInitializingShards(), equalTo(0)); | ||
assertThat(response.getUnassignedShards(), equalTo(5)); | ||
assertThat(response.getActiveShardsPercent(), equalTo(50d)); | ||
assertThat(response.getIndices().size(), equalTo(1)); | ||
Map.Entry<String, ClusterIndexHealth> index = response.getIndices().entrySet().iterator().next(); | ||
indexAssertion(index.getKey(), index.getValue(), false); | ||
} | ||
|
||
private void indexAssertion(String indexName, ClusterIndexHealth indexHealth, boolean emptyShards) { | ||
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 call it |
||
assertThat(indexHealth, notNullValue()); | ||
assertThat(indexHealth.getIndex(),equalTo(indexName)); | ||
assertThat(indexHealth.getActivePrimaryShards(),equalTo(5)); | ||
assertThat(indexHealth.getActiveShards(),equalTo(5)); | ||
assertThat(indexHealth.getNumberOfReplicas(),equalTo(1)); | ||
assertThat(indexHealth.getInitializingShards(),equalTo(0)); | ||
assertThat(indexHealth.getUnassignedShards(),equalTo(5)); | ||
assertThat(indexHealth.getRelocatingShards(),equalTo(0)); | ||
assertThat(indexHealth.getStatus(),equalTo(ClusterHealthStatus.YELLOW)); | ||
if (emptyShards) { | ||
assertThat(indexHealth.getShards().size(), equalTo(0)); | ||
} else { | ||
assertThat(indexHealth.getShards().size(), equalTo(5)); | ||
for (Map.Entry<Integer, ClusterShardHealth> entry : indexHealth.getShards().entrySet()) { | ||
shardAssertion(entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
} | ||
|
||
private void shardAssertion(int shardId, ClusterShardHealth shardHealth) { | ||
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 call it 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. also, all these assert methods could be static? |
||
assertThat(shardHealth, notNullValue()); | ||
assertThat(shardHealth.getShardId(), equalTo(shardId)); | ||
assertThat(shardHealth.getStatus(), equalTo(ClusterHealthStatus.YELLOW)); | ||
assertThat(shardHealth.getActiveShards(), equalTo(1)); | ||
assertThat(shardHealth.getInitializingShards(), equalTo(0)); | ||
assertThat(shardHealth.getUnassignedShards(), equalTo(1)); | ||
assertThat(shardHealth.getRelocatingShards(), equalTo(0)); | ||
} | ||
|
||
public void testClusterHealthNotFoundIndex() throws IOException { | ||
ClusterHealthRequest request = new ClusterHealthRequest("notexisted-index"); | ||
request.timeout("5s"); | ||
ClusterHealthResponse response = execute(request, highLevelClient().cluster()::health, highLevelClient().cluster()::healthAsync); | ||
|
||
assertThat(response, notNullValue()); | ||
assertThat(response.isTimedOut(), equalTo(true)); | ||
assertThat(response.status(), equalTo(RestStatus.REQUEST_TIMEOUT)); | ||
assertThat(response.getStatus(), equalTo(ClusterHealthStatus.RED)); | ||
emptyClusterAssertion(response); | ||
} | ||
|
||
public static void emptyClusterAssertion(ClusterHealthResponse response) { | ||
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 call it |
||
assertThat(response.getIndices(), equalTo(emptyMap())); | ||
assertThat(response.getActivePrimaryShards(), equalTo(0)); | ||
assertThat(response.getNumberOfDataNodes(), equalTo(1)); | ||
assertThat(response.getNumberOfNodes(), equalTo(1)); | ||
assertThat(response.getActiveShards(), equalTo(0)); | ||
assertThat(response.getDelayedUnassignedShards(), equalTo(0)); | ||
assertThat(response.getInitializingShards(), equalTo(0)); | ||
assertThat(response.getUnassignedShards(), equalTo(0)); | ||
assertThat(response.getActiveShardsPercent(), equalTo(100d)); | ||
} | ||
} |
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.
I would prefer that we don't want randomize anything in this integration test. These are quite hard to debug if they fail, I would prefer to have different test methods here for the main code paths.