fix(elbv2): route only to healthy targets while any target is healthy (#8452)

An ALB routed to targets still in the initial state beside healthy ones,
so a newly registered target that was not ready took requests before its
first health check passed. While a target group has a healthy target,
the proxy now routes only to its healthy targets; when none is healthy it
still fails open to every registered target, matching the ALB guide's
"Before a target can receive requests from the load balancer, it must
pass the initial health checks". The integration tests share their
target group and health state helpers.

Closes #8451
This commit is contained in:
Maxence LEFEBVRE authored and GitHub committed 2026-11-15 10:58:44 -04:00
1 parent 4fc41cc683
commit f930634fa8
3 files changed
+94 -28

No files matched your search

+5 -2
View File
@@ -218,8 +218,11 @@ on `HealthCheckPort`, or the target's traffic port when configured as `traffic-p
The response-code matcher and healthy/unhealthy thresholds affect target health.
`HealthCheckProtocol=TCP` does not perform a TCP-only probe.
Lambda targets are not actively probed. In ELBv2 mock mode, sockets
and active monitoring are disabled. The proxy can route to initial targets and fails
open to registered targets when none are healthy, so a successful request alone does
and active monitoring are disabled. While a target group has a `healthy` target, the
proxy routes only to its `healthy` targets, so a newly registered target in `initial`
state gets no requests until its first health check passes. When no target is
`healthy`, the proxy fails open to every registered target, including `initial` ones
and those in a group with health checks disabled, so a successful request alone does
not prove that a health check passed.
### Security groups and PrivateLink
@@ -154,8 +154,7 @@ public class ElbV2HealthChecker implements Resettable {
}
public boolean isHealthy(String tgArn, TargetDescription target, int port) {
String state = getState(tgArn, target.getId(), port);
return "healthy".equals(state) || "initial".equals(state);
return "healthy".equals(getState(tgArn, target.getId(), port));
}
void probeAll(String tgArn, TargetGroup tg) {
@@ -3,17 +3,25 @@ package io.floci.aws.services.elbv2;
import com.sun.net.httpserver.HttpServer;
import io.floci.aws.services.ec2.Ec2Service;
import io.floci.aws.testing.RealElbV2DataPlaneProfile;
import io.floci.aws.testutil.FreePorts;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import org.junit.jupiter.api.Test;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import static io.floci.aws.testing.AlbFixtures.createListener;
import static io.floci.aws.testing.AlbFixtures.createLoadBalancer;
import static io.floci.aws.testing.AlbFixtures.deleteLoadBalancer;
import static io.floci.aws.testing.AlbFixtures.deleteTargetGroup;
import static io.floci.aws.testing.AlbFixtures.registerTarget;
import static io.restassured.RestAssured.given;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;
@QuarkusTest
@@ -35,19 +43,8 @@ class ElbV2InitialHealthIntegrationTest {
backend.start();
String groupArn = null;
try {
groupArn = given()
.header("Authorization", AUTH)
.formParam("Action", "CreateTargetGroup")
.formParam("Name", "initial-health-wire")
.formParam("Protocol", "HTTP")
.formParam("Port", backend.getAddress().getPort())
.formParam("VpcId", Ec2Service.defaultVpcId("us-east-1"))
.formParam("TargetType", "ip")
.formParam("HealthCheckIntervalSeconds", 300)
.formParam("HealthyThresholdCount", 10)
.when().post("/")
.then().statusCode(200)
.extract().path("CreateTargetGroupResponse.CreateTargetGroupResult.TargetGroups.member.TargetGroupArn");
groupArn = createTargetGroup("initial-health-wire", backend.getAddress().getPort(),
"HealthyThresholdCount", 10);
given()
.header("Authorization", AUTH)
.formParam("Action", "RegisterTargets")
@@ -58,16 +55,10 @@ class ElbV2InitialHealthIntegrationTest {
.then().statusCode(200);
String registeredGroupArn = groupArn;
await().atMost(Duration.ofSeconds(5)).untilAsserted(() -> given()
.header("Authorization", AUTH)
.formParam("Action", "DescribeTargetHealth")
.formParam("TargetGroupArn", registeredGroupArn)
.when().post("/")
.then().statusCode(200)
.body("DescribeTargetHealthResponse.DescribeTargetHealthResult.TargetHealthDescriptions.member.TargetHealth.State",
equalTo("healthy"))
.body("DescribeTargetHealthResponse.DescribeTargetHealthResult.TargetHealthDescriptions.member.TargetHealth.Reason.size()",
equalTo(0)));
await().atMost(Duration.ofSeconds(5)).untilAsserted(() -> {
assertEquals(List.of("healthy"), targetHealth(registeredGroupArn, "State"));
assertEquals(List.of(), targetHealth(registeredGroupArn, "Reason"));
});
assertEquals(1, probes.get(), "Initial admission must not wait for the recovery threshold");
} finally {
try {
@@ -84,4 +75,77 @@ class ElbV2InitialHealthIntegrationTest {
}
}
}
@Test
void initialTargetGetsNoRequestsWhileAnotherTargetIsHealthy() throws Exception {
HttpServer serving = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
serving.createContext("/", exchange -> {
exchange.sendResponseHeaders(200, -1);
exchange.close();
});
serving.start();
int notListening = FreePorts.loopbackIpv4Port();
int listenerPort = FreePorts.anyInterfacePort();
String loadBalancerArn = null;
String groupArn = null;
try {
loadBalancerArn = createLoadBalancer("initial-health-routing");
groupArn = createTargetGroup("initial-health-routing", serving.getAddress().getPort(),
"UnhealthyThresholdCount", 10);
registerTarget(groupArn, "127.0.0.1", serving.getAddress().getPort());
createListener(loadBalancerArn, groupArn, listenerPort);
String registeredGroupArn = groupArn;
await().atMost(Duration.ofSeconds(5)).untilAsserted(() ->
assertEquals(List.of("healthy"), targetHealth(registeredGroupArn, "State")));
// A replacement that is not listening yet stays initial: one failed probe is below the threshold.
registerTarget(groupArn, "127.0.0.1", notListening);
assertThat(targetHealth(groupArn, "State"), containsInAnyOrder("healthy", "initial"));
for (int i = 0; i < 6; i++) {
given().baseUri("http://127.0.0.1").port(listenerPort)
.when().get("/")
.then().statusCode(200);
}
} finally {
try {
if (loadBalancerArn != null) {
deleteLoadBalancer(loadBalancerArn);
}
if (groupArn != null) {
deleteTargetGroup(groupArn);
}
} finally {
serving.stop(0);
}
}
}
/** An HTTP ip target group probed every 300 s, with {@code threshold} set to {@code value}. */
private static String createTargetGroup(String name, int port, String threshold, int value) {
return given()
.header("Authorization", AUTH)
.formParam("Action", "CreateTargetGroup")
.formParam("Name", name)
.formParam("Protocol", "HTTP")
.formParam("Port", port)
.formParam("VpcId", Ec2Service.defaultVpcId("us-east-1"))
.formParam("TargetType", "ip")
.formParam("HealthCheckIntervalSeconds", 300)
.formParam(threshold, value)
.when().post("/")
.then().statusCode(200)
.extract().path("CreateTargetGroupResponse.CreateTargetGroupResult.TargetGroups.member.TargetGroupArn");
}
/** One {@code TargetHealth} field, such as {@code State} or {@code Reason}, for each registered target. */
private static List<String> targetHealth(String groupArn, String field) {
return given()
.header("Authorization", AUTH)
.formParam("Action", "DescribeTargetHealth")
.formParam("TargetGroupArn", groupArn)
.when().post("/")
.then().statusCode(200)
.extract().xmlPath().getList("DescribeTargetHealthResponse.DescribeTargetHealthResult"
+ ".TargetHealthDescriptions.member.TargetHealth." + field);
}
}