Closes gh-14914
This commit is contained in:
Brian Clozel 2018-10-29 18:09:42 +01:00
parent 3e6a4eb6e3
commit 2232f7d82f
2 changed files with 51 additions and 36 deletions

View File

@ -30,6 +30,7 @@ import org.springframework.boot.actuate.health.HealthIndicator;
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Julian Devia Serna * @author Julian Devia Serna
* @author Brian Clozel
* @since 2.0.0 * @since 2.0.0
*/ */
public class ElasticsearchJestHealthIndicator extends AbstractHealthIndicator { public class ElasticsearchJestHealthIndicator extends AbstractHealthIndicator {
@ -47,15 +48,20 @@ public class ElasticsearchJestHealthIndicator extends AbstractHealthIndicator {
protected void doHealthCheck(Health.Builder builder) throws Exception { protected void doHealthCheck(Health.Builder builder) throws Exception {
JestResult healthResult = this.jestClient JestResult healthResult = this.jestClient
.execute(new io.searchbox.cluster.Health.Builder().build()); .execute(new io.searchbox.cluster.Health.Builder().build());
if (healthResult.getResponseCode() != 200 || !healthResult.isSucceeded()) {
builder.down();
}
else {
JsonElement root = this.jsonParser.parse(healthResult.getJsonString()); JsonElement root = this.jsonParser.parse(healthResult.getJsonString());
JsonElement status = root.getAsJsonObject().get("status"); JsonElement status = root.getAsJsonObject().get("status");
if (!healthResult.isSucceeded() || healthResult.getResponseCode() != 200 || status if (status.getAsString()
.getAsString().equals(io.searchbox.cluster.Health.Status.RED.getKey())) { .equals(io.searchbox.cluster.Health.Status.RED.getKey())) {
builder.outOfService(); builder.outOfService();
} }
else { else {
builder.up(); builder.up();
} }
} }
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,6 +40,7 @@ import static org.mockito.Mockito.mock;
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Julian Devia Serna * @author Julian Devia Serna
* @author Brian Clozel
*/ */
public class ElasticsearchJestHealthIndicatorTests { public class ElasticsearchJestHealthIndicatorTests {
@ -52,7 +53,7 @@ public class ElasticsearchJestHealthIndicatorTests {
@Test @Test
public void elasticsearchIsUp() throws IOException { public void elasticsearchIsUp() throws IOException {
given(this.jestClient.execute(any(Action.class))) given(this.jestClient.execute(any(Action.class)))
.willReturn(createJestResult("green", 200, true)); .willReturn(createJestResult(200, true, "green"));
Health health = this.healthIndicator.health(); Health health = this.healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP); assertThat(health.getStatus()).isEqualTo(Status.UP);
} }
@ -66,36 +67,40 @@ public class ElasticsearchJestHealthIndicatorTests {
assertThat(health.getStatus()).isEqualTo(Status.DOWN); assertThat(health.getStatus()).isEqualTo(Status.DOWN);
} }
@SuppressWarnings("unchecked")
@Test
public void elasticsearchIsDownWhenQueryDidNotSucceed() throws IOException {
given(this.jestClient.execute(any(Action.class)))
.willReturn(createJestResult(200, false, ""));
Health health = this.healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
}
@SuppressWarnings("unchecked")
@Test
public void elasticsearchIsDownByResponseCode() throws IOException {
given(this.jestClient.execute(any(Action.class)))
.willReturn(createJestResult(500, false, ""));
Health health = this.healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void elasticsearchIsOutOfServiceByStatus() throws IOException { public void elasticsearchIsOutOfServiceByStatus() throws IOException {
given(this.jestClient.execute(any(Action.class))) given(this.jestClient.execute(any(Action.class)))
.willReturn(createJestResult("red", 200, true)); .willReturn(createJestResult(200, true, "red"));
Health health = this.healthIndicator.health(); Health health = this.healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE); assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
} }
@SuppressWarnings("unchecked") private static JestResult createJestResult(int responseCode, boolean succeeded,
@Test String status) {
public void elasticsearchIsOutOfServiceByResponseCode() throws IOException {
given(this.jestClient.execute(any(Action.class)))
.willReturn(createJestResult("", 500, true));
Health health = this.healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
}
@SuppressWarnings("unchecked") SearchResult searchResult = new SearchResult(new Gson());
@Test String json;
public void elasticsearchIsOutOfServiceBySucceeded() throws IOException { if (responseCode == 200) {
given(this.jestClient.execute(any(Action.class))) json = String.format("{\"cluster_name\":\"elasticsearch\","
.willReturn(createJestResult("red", 500, false));
Health health = this.healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
}
private static JestResult createJestResult(String status, int responseCode,
boolean succeeded) {
String json = String.format("{\"cluster_name\":\"docker-cluster\","
+ "\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1," + "\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1,"
+ "\"number_of_data_nodes\":1,\"active_primary_shards\":0," + "\"number_of_data_nodes\":1,\"active_primary_shards\":0,"
+ "\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0," + "\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0,"
@ -103,7 +108,11 @@ public class ElasticsearchJestHealthIndicatorTests {
+ "\"number_of_pending_tasks\":0,\"number_of_in_flight_fetch\":0," + "\"number_of_pending_tasks\":0,\"number_of_in_flight_fetch\":0,"
+ "\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}", + "\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}",
status); status);
SearchResult searchResult = new SearchResult(new Gson()); }
else {
json = "{\n" + " \"error\": \"Server Error\",\n" + " \"status\": "
+ responseCode + "\n" + "}";
}
searchResult.setJsonString(json); searchResult.setJsonString(json);
searchResult.setJsonObject(new JsonParser().parse(json).getAsJsonObject()); searchResult.setJsonObject(new JsonParser().parse(json).getAsJsonObject());
searchResult.setResponseCode(responseCode); searchResult.setResponseCode(responseCode);