Skip to content

Commit 7d98189

Browse files
committed
Changed jestHealthCheck to perform cluster health request
1 parent f145e81 commit 7d98189

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +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;
25-
2623
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
2724
import org.springframework.boot.actuate.health.Health;
2825
import org.springframework.boot.actuate.health.HealthIndicator;
@@ -46,14 +43,13 @@ public ElasticsearchJestHealthIndicator(JestClient jestClient) {
4643

4744
@Override
4845
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) {
46+
JestResult healthResult = this.jestClient.execute(new io.searchbox.cluster.Health.Builder().build());
47+
JsonElement root = this.jsonParser.parse(healthResult.getJsonString());
48+
JsonElement status = root.getAsJsonObject().get("status");
49+
if (!healthResult.isSucceeded() || healthResult.getResponseCode() != 200
50+
|| status.getAsString().equals(io.searchbox.cluster.Health.Status.RED.getKey())) {
5451
builder.outOfService();
55-
}
56-
else {
52+
} else {
5753
builder.up();
5854
}
5955
}

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

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.actuate.elasticsearch;
1818

19-
import java.io.IOException;
20-
2119
import com.google.gson.Gson;
2220
import com.google.gson.JsonParser;
2321
import io.searchbox.action.Action;
@@ -26,10 +24,11 @@
2624
import io.searchbox.client.config.exception.CouldNotConnectException;
2725
import io.searchbox.core.SearchResult;
2826
import org.junit.Test;
29-
3027
import org.springframework.boot.actuate.health.Health;
3128
import org.springframework.boot.actuate.health.Status;
3229

30+
import java.io.IOException;
31+
3332
import static org.assertj.core.api.Assertions.assertThat;
3433
import static org.mockito.ArgumentMatchers.any;
3534
import static org.mockito.BDDMockito.given;
@@ -51,7 +50,7 @@ public class ElasticsearchJestHealthIndicatorTests {
5150
@Test
5251
public void elasticsearchIsUp() throws IOException {
5352
given(this.jestClient.execute(any(Action.class)))
54-
.willReturn(createJestResult(4, 0));
53+
.willReturn(createJestResult("green", 200, true));
5554
Health health = this.healthIndicator.health();
5655
assertThat(health.getStatus()).isEqualTo(Status.UP);
5756
}
@@ -67,19 +66,44 @@ public void elasticsearchIsDown() throws IOException {
6766

6867
@SuppressWarnings("unchecked")
6968
@Test
70-
public void elasticsearchIsOutOfService() throws IOException {
69+
public void elasticsearchIsOutOfServiceByStatus() throws IOException {
70+
given(this.jestClient.execute(any(Action.class)))
71+
.willReturn(createJestResult("red", 200, true));
72+
Health health = this.healthIndicator.health();
73+
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
74+
}
75+
76+
@SuppressWarnings("unchecked")
77+
@Test
78+
public void elasticsearchIsOutOfServiceByResponseCode() throws IOException {
79+
given(this.jestClient.execute(any(Action.class)))
80+
.willReturn(createJestResult("", 500, true));
81+
Health health = this.healthIndicator.health();
82+
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
83+
}
84+
85+
@SuppressWarnings("unchecked")
86+
@Test
87+
public void elasticsearchIsOutOfServiceBySucceeded() throws IOException {
7188
given(this.jestClient.execute(any(Action.class)))
72-
.willReturn(createJestResult(4, 1));
89+
.willReturn(createJestResult("red", 500, false));
7390
Health health = this.healthIndicator.health();
7491
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
7592
}
7693

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);
94+
private static JestResult createJestResult(String status, int responseCode, boolean succeeded) {
95+
String json = String.format("{\"cluster_name\":\"docker-cluster\"," +
96+
"\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1," +
97+
"\"number_of_data_nodes\":1,\"active_primary_shards\":0," +
98+
"\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0," +
99+
"\"unassigned_shards\":0,\"delayed_unassigned_shards\":0," +
100+
"\"number_of_pending_tasks\":0,\"number_of_in_flight_fetch\":0," +
101+
"\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}", status);
80102
SearchResult searchResult = new SearchResult(new Gson());
81103
searchResult.setJsonString(json);
82104
searchResult.setJsonObject(new JsonParser().parse(json).getAsJsonObject());
105+
searchResult.setResponseCode(responseCode);
106+
searchResult.setSucceeded(succeeded);
83107
return searchResult;
84108
}
85109

0 commit comments

Comments
 (0)