fix(elbv2): expose target health reason parity (#1743)

This commit is contained in:
Jason van Zyl authored and GitHub committed 2026-07-14 22:57:45 -04:00
1 parent e49bc432bd
commit aa77d4ce46
6 files changed
+342 -13

No files matched your search

@@ -12,6 +12,7 @@ import org.jboss.logging.Logger;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
@@ -88,6 +89,9 @@ public class ElbV2HealthChecker implements Resettable {
String key = stateKey(t.getId(), port);
tgStates.putIfAbsent(key, new TargetState());
}
if (Boolean.TRUE.equals(tg.getHealthCheckEnabled())) {
vertx.setTimer(1, ignored -> probeAll(tgArn, tg));
}
}
public void removeTargets(String tgArn, List<TargetDescription> targets, TargetGroup tg) {
@@ -102,15 +106,19 @@ public class ElbV2HealthChecker implements Resettable {
}
public String getState(String tgArn, String targetId, int port) {
return getHealth(tgArn, targetId, port).state();
}
public TargetHealthStatus getHealth(String tgArn, String targetId, int port) {
Map<String, TargetState> tgStates = states.get(tgArn);
if (tgStates == null) {
return "initial";
return TargetHealthStatus.registrationInProgress();
}
TargetState s = tgStates.get(stateKey(targetId, port));
if (s == null) {
return "initial";
return TargetHealthStatus.registrationInProgress();
}
return s.status;
return new TargetHealthStatus(s.status, s.reason, s.description);
}
public boolean isHealthy(String tgArn, TargetDescription target, int port) {
@@ -153,12 +161,19 @@ public class ElbV2HealthChecker implements Resettable {
state.consecutiveSuccesses++;
if (state.consecutiveSuccesses >= healthyThreshold) {
state.status = "healthy";
state.reason = null;
state.description = null;
} else if ("initial".equals(state.status)) {
state.reason = "Elb.InitialHealthChecking";
state.description = "Initial health checks in progress";
}
} else {
state.consecutiveSuccesses = 0;
state.consecutiveFailures++;
if (state.consecutiveFailures >= unhealthyThreshold) {
state.status = "unhealthy";
state.reason = "Target.ResponseCodeMismatch";
state.description = "Health checks failed with these codes: [" + statusCode + "]";
}
}
}).onFailure(err -> {
@@ -166,6 +181,13 @@ public class ElbV2HealthChecker implements Resettable {
state.consecutiveFailures++;
if (state.consecutiveFailures >= unhealthyThreshold) {
state.status = "unhealthy";
if (isTimeout(err)) {
state.reason = "Target.Timeout";
state.description = "Request timed out";
} else {
state.reason = "Target.FailedHealthChecks";
state.description = "Health checks failed";
}
}
LOG.debugv("Health check failed for {0}:{1} - {2}", host, port, err.getMessage());
});
@@ -204,12 +226,34 @@ public class ElbV2HealthChecker implements Resettable {
.anyMatch(codeStr::equals);
}
private static boolean isTimeout(Throwable err) {
Throwable current = err;
while (current != null) {
if (current instanceof SocketTimeoutException) {
return true;
}
current = current.getCause();
}
return false;
}
private static String stateKey(String targetId, int port) {
return targetId + ":" + port;
}
public record TargetHealthStatus(String state, String reason, String description) {
private static TargetHealthStatus registrationInProgress() {
return new TargetHealthStatus(
"initial",
"Elb.RegistrationInProgress",
"Target registration is in progress");
}
}
private static class TargetState {
volatile String status = "initial";
volatile String reason = "Elb.RegistrationInProgress";
volatile String description = "Target registration is in progress";
volatile int consecutiveSuccesses = 0;
volatile int consecutiveFailures = 0;
}
@@ -753,8 +753,8 @@ public class ElbV2Service {
public List<TargetHealth> describeTargetHealth(String region, String tgArn,
List<TargetDescription> filterTargets) {
TargetGroup tg = requireTargetGroup(region, tgArn);
List<TargetDescription> candidates = filterTargets != null && !filterTargets.isEmpty()
? filterTargets : tg.getTargets();
boolean hasFilterTargets = filterTargets != null && !filterTargets.isEmpty();
List<TargetDescription> candidates = hasFilterTargets ? filterTargets : tg.getTargets();
boolean isLambdaTg = "lambda".equals(tg.getTargetType());
return candidates.stream().map(t -> {
@@ -767,19 +767,26 @@ public class ElbV2Service {
}
int port = ElbV2HealthChecker.effectivePort(t, tg);
th.setHealthCheckPort(String.valueOf(port));
String state = healthChecker.getState(tgArn, t.getId(), port);
th.setState(state);
if ("initial".equals(state)) {
th.setReason("Elb.RegistrationInProgress");
th.setDescription("Target registration is in progress");
} else if ("unhealthy".equals(state)) {
th.setReason("Target.FailedHealthChecks");
th.setDescription("Health checks failed");
if (hasFilterTargets && !isRegisteredTarget(tg, t, port)) {
th.setState("unused");
th.setReason("Target.NotRegistered");
th.setDescription("Target is not registered to the target group");
return th;
}
ElbV2HealthChecker.TargetHealthStatus health = healthChecker.getHealth(tgArn, t.getId(), port);
th.setState(health.state());
th.setReason(health.reason());
th.setDescription(health.description());
return th;
}).collect(Collectors.toList());
}
private static boolean isRegisteredTarget(TargetGroup targetGroup, TargetDescription candidate, int candidatePort) {
return targetGroup.getTargets().stream()
.anyMatch(registered -> Objects.equals(registered.getId(), candidate.getId())
&& ElbV2HealthChecker.effectivePort(registered, targetGroup) == candidatePort);
}
// ── Tags ──────────────────────────────────────────────────────────────────
public void addTags(List<String> resourceArns, Map<String, String> newTags) {