Skip to content

Commit 3e6a4eb

Browse files
amotobclozel
authored andcommitted
Use cluster endpoint in Jest HealthIndicator
This commit changes the requested endpoint for the Jest HealthIndicator. The `"/_all/_stats"` was previously used, but the response size can be quite large and costly. This is now using the `"/_cluster/health"` endpoint.
1 parent 1341789 commit 3e6a4eb

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchJestHealthIndicator.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
package org.springframework.boot.actuate.elasticsearch;
1818

1919
import com.google.gson.JsonElement;
20-
import com.google.gson.JsonObject;
2120
import com.google.gson.JsonParser;
2221
import io.searchbox.client.JestClient;
2322
import io.searchbox.client.JestResult;
24-
import io.searchbox.indices.Stats;
2523

2624
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
2725
import org.springframework.boot.actuate.health.Health;
@@ -31,6 +29,7 @@
3129
* {@link HealthIndicator} for Elasticsearch using a {@link JestClient}.
3230
*
3331
* @author Stephane Nicoll
32+
* @author Julian Devia Serna
3433
* @since 2.0.0
3534
*/
3635
public class ElasticsearchJestHealthIndicator extends AbstractHealthIndicator {
@@ -46,11 +45,12 @@ public ElasticsearchJestHealthIndicator(JestClient jestClient) {
4645

4746
@Override
4847
protected void doHealthCheck(Health.Builder builder) throws Exception {
49-
JestResult aliases = this.jestClient.execute(new Stats.Builder().build());
50-
JsonElement root = this.jsonParser.parse(aliases.getJsonString());
51-
JsonObject shards = root.getAsJsonObject().get("_shards").getAsJsonObject();
52-
int failedShards = shards.get("failed").getAsInt();
53-
if (failedShards != 0) {
48+
JestResult healthResult = this.jestClient
49+
.execute(new io.searchbox.cluster.Health.Builder().build());
50+
JsonElement root = this.jsonParser.parse(healthResult.getJsonString());
51+
JsonElement status = root.getAsJsonObject().get("status");
52+
if (!healthResult.isSucceeded() || healthResult.getResponseCode() != 200 || status
53+
.getAsString().equals(io.searchbox.cluster.Health.Status.RED.getKey())) {
5454
builder.outOfService();
5555
}
5656
else {

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchJestHealthIndicatorTests.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* Tests for {@link ElasticsearchJestHealthIndicator}.
4040
*
4141
* @author Stephane Nicoll
42+
* @author Julian Devia Serna
4243
*/
4344
public class ElasticsearchJestHealthIndicatorTests {
4445

@@ -51,7 +52,7 @@ public class ElasticsearchJestHealthIndicatorTests {
5152
@Test
5253
public void elasticsearchIsUp() throws IOException {
5354
given(this.jestClient.execute(any(Action.class)))
54-
.willReturn(createJestResult(4, 0));
55+
.willReturn(createJestResult("green", 200, true));
5556
Health health = this.healthIndicator.health();
5657
assertThat(health.getStatus()).isEqualTo(Status.UP);
5758
}
@@ -67,19 +68,46 @@ public void elasticsearchIsDown() throws IOException {
6768

6869
@SuppressWarnings("unchecked")
6970
@Test
70-
public void elasticsearchIsOutOfService() throws IOException {
71+
public void elasticsearchIsOutOfServiceByStatus() throws IOException {
7172
given(this.jestClient.execute(any(Action.class)))
72-
.willReturn(createJestResult(4, 1));
73+
.willReturn(createJestResult("red", 200, true));
7374
Health health = this.healthIndicator.health();
7475
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
7576
}
7677

77-
private static JestResult createJestResult(int shards, int failedShards) {
78-
String json = String.format("{_shards: {\n" + "total: %s,\n" + "successful: %s,\n"
79-
+ "failed: %s\n" + "}}", shards, shards - failedShards, failedShards);
78+
@SuppressWarnings("unchecked")
79+
@Test
80+
public void elasticsearchIsOutOfServiceByResponseCode() throws IOException {
81+
given(this.jestClient.execute(any(Action.class)))
82+
.willReturn(createJestResult("", 500, true));
83+
Health health = this.healthIndicator.health();
84+
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
85+
}
86+
87+
@SuppressWarnings("unchecked")
88+
@Test
89+
public void elasticsearchIsOutOfServiceBySucceeded() throws IOException {
90+
given(this.jestClient.execute(any(Action.class)))
91+
.willReturn(createJestResult("red", 500, false));
92+
Health health = this.healthIndicator.health();
93+
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
94+
}
95+
96+
private static JestResult createJestResult(String status, int responseCode,
97+
boolean succeeded) {
98+
String json = String.format("{\"cluster_name\":\"docker-cluster\","
99+
+ "\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1,"
100+
+ "\"number_of_data_nodes\":1,\"active_primary_shards\":0,"
101+
+ "\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0,"
102+
+ "\"unassigned_shards\":0,\"delayed_unassigned_shards\":0,"
103+
+ "\"number_of_pending_tasks\":0,\"number_of_in_flight_fetch\":0,"
104+
+ "\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}",
105+
status);
80106
SearchResult searchResult = new SearchResult(new Gson());
81107
searchResult.setJsonString(json);
82108
searchResult.setJsonObject(new JsonParser().parse(json).getAsJsonObject());
109+
searchResult.setResponseCode(responseCode);
110+
searchResult.setSucceeded(succeeded);
83111
return searchResult;
84112
}
85113

0 commit comments

Comments
 (0)