Skip to content

Commit

Permalink
Better wait for health
Browse files Browse the repository at this point in the history
  • Loading branch information
mihxil committed Dec 6, 2024
1 parent 2fff75c commit 5fb92d5
Showing 1 changed file with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1587,34 +1587,52 @@ public void waitForHealth() {
public static void waitForHealth(RestClient client, SimpleLogger log) {
waitForHealth(client, log, Duration.ofMinutes(1));
}

public static void waitForHealth(RestClient client, SimpleLogger log, Duration timeOut) {
waitForHealth(client, log, timeOut, "green");
}


public static void waitForHealth(RestClient client, SimpleLogger log, Duration timeOut, String waitForStatus) {
int count = 1;
Instant start = Instant.now();
while (true) {
if (Instant.now().isAfter(start.plus(timeOut))) {
throw new RuntimeException("%s not healthy after %s".formatted(client, timeOut));
}
try {
try {
Request request = new Request(GET, "/_cluster/health");
request.setOptions(request.getOptions()
.toBuilder()
.addParameter("wait_for_status", waitForStatus)
.addParameter("timeout", "10s")
.addHeader("accept", "application/json"));


Response response = client.performRequest(request);
JsonNode clusterHealth = LENIENT
.readerFor(JsonNode.class)
.readValue(response.getEntity().getContent());
} catch (ResponseException re) {
log.warn(re.getMessage());
}

Request request = new Request(GET, "/_cluster/health");
request.setOptions(request.getOptions()
.toBuilder()
.addParameter("wait_for_status", "green")
.addParameter("timeout", "50s")
.addHeader("accept", "application/json"));

Response response = client.performRequest(request);
JsonNode clusterHealth = LENIENT
.readerFor(JsonNode.class)
.readValue(response.getEntity().getContent());

String status = clusterHealth.get("status").textValue();
log.info("status {}", status);
boolean serviceIsUp = "green".equals(status) || "yellow".equals(status);
boolean serviceIsUp = waitForStatus.equals(status);
if (serviceIsUp) {
break;
} else {
log.warn("Not green? Trying again.");
log.warn("Not green? Trying again. ({})", status);
}
sleepSeconds(2);
} catch (RuntimeException ee) {
Expand Down

0 comments on commit 5fb92d5

Please sign in to comment.