fix(elbv2): offload Lambda invocation to a worker thread (#1034)

* fix(elbv2): offload Lambda invocation to a worker thread

ElbV2DataPlane.invokeLambdaTarget called lambdaService.invoke(...)
synchronously inside req.bodyHandler, which runs on a Vert.x
event-loop thread. The invocation blocks for seconds while a cold
Lambda container warms and polls the Runtime API. The Runtime API
itself is a Vert.x HttpServer, so blocking the listener's event loop
starved the runtime — every ALB → Lambda request hung until the
function timed out and returned 502.

Wrap the invocation in vertx.executeBlocking and shape the response
on the resulting Future, mirroring WebSocketHandler.invokeIntegration.

Add ElbV2LambdaTargetDataPlaneIntegrationTest, which deploys a real
Node.js Lambda, sends an HTTP request through the listener port, and
asserts the Lambda's response shape. The existing
ElbV2LambdaTargetIntegrationTest only covered the control plane and
ran with floci.services.elbv2.mock=true, which is why this bug went
undetected. The new test uses @TestProfile to disable the ELBv2
mock so the data plane is exercised end-to-end.

* fix(elbv2): address review feedback on ALB→Lambda fix

- Pass ordered=false to vertx.executeBlocking so independent ALB
  requests run in parallel on the worker pool instead of being
  serialized per context.
- Use StandardCharsets.UTF_8 when encoding the Node.js handler source
  so the generated zip is byte-deterministic across platforms.
- Make cleanup robust to partial failures by null-guarding each
  delete and accepting 404 for the Lambda function delete. Cleanup
  stays as a final ordered @Test (rather than @AfterAll) because
  the Quarkus REST endpoint is torn down before JUnit's @AfterAll
  runs; an HTTP-based teardown there would fail with connection
  refused. Bumped the order to Integer.MAX_VALUE so the cleanup
  step keeps running last regardless of future test additions.

* test(elbv2): align ALB→Lambda dataplane test with the bug repro

Switch the test Lambda from nodejs20.x to python3.11 so it mirrors
the reproduction script in the bug report verbatim. The handler now
returns {"statusCode": 200, "body": "ok"} and the assertion drops
the path-echo and content-type checks — status 200 plus body "ok"
is still a tight enough signal that the entire ALB → Lambda data
plane round-trip succeeded.

* test(elbv2): bump dataplane test runtime to python3.14

Match the latest Python runtime supported by Floci's image map
and keep alignment with the bug repro.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-05-26 21:22:50 -05:00
1 parent ec948fe3a8
commit 4aea1c7c37
2 files changed
+282 -54

No files matched your search

@@ -197,67 +197,81 @@ public class ElbV2DataPlane {
private void invokeLambdaTarget(io.vertx.core.http.HttpServerRequest req, String functionArn, String region) {
req.bodyHandler(body -> {
try {
Map<String, Object> event = buildAlbEvent(req, body);
Map<String, Object> event = buildAlbEvent(req, body);
// Lambda invocation is synchronous and may take seconds while a cold container
// boots and polls the Runtime API. The Runtime API itself runs on Vert.x event
// loops, so blocking the listener's event loop here would deadlock the runtime
// and the function would time out. Offload to a worker thread, same as WebSocket.
// ordered=false so independent ALB requests run in parallel on the worker pool.
vertx.<InvokeResult>executeBlocking(() -> {
byte[] payload = objectMapper.writeValueAsBytes(event);
InvokeResult result = lambdaService.invoke(region, functionArn, payload, InvocationType.RequestResponse);
if (result.getFunctionError() != null) {
req.response().setStatusCode(502).end("Lambda function error: " + result.getFunctionError());
return;
return lambdaService.invoke(region, functionArn, payload, InvocationType.RequestResponse);
}, false).onSuccess(result -> {
try {
writeLambdaResponse(req, result);
} catch (Exception e) {
LOG.errorf(e, "Error writing Lambda response for %s", functionArn);
req.response().setStatusCode(502).end("Lambda invocation error");
}
if (result.getPayload() == null || result.getPayload().length == 0) {
req.response().setStatusCode(200).end();
return;
}
Map<String, Object> lambdaResp = objectMapper.readValue(result.getPayload(),
new TypeReference<Map<String, Object>>() {});
int statusCode = 200;
Object sc = lambdaResp.get("statusCode");
if (sc != null) {
statusCode = ((Number) sc).intValue();
}
req.response().setStatusCode(statusCode);
Object headers = lambdaResp.get("headers");
if (headers instanceof Map<?, ?> headerMap) {
for (Map.Entry<?, ?> entry : headerMap.entrySet()) {
req.response().putHeader(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
}
}
Object multiValueHeaders = lambdaResp.get("multiValueHeaders");
if (multiValueHeaders instanceof Map<?, ?> mvh) {
for (Map.Entry<?, ?> entry : mvh.entrySet()) {
if (entry.getValue() instanceof List<?> values) {
for (Object v : values) {
req.response().putHeader(String.valueOf(entry.getKey()), String.valueOf(v));
}
}
}
}
Object responseBody = lambdaResp.get("body");
Boolean isBase64 = (Boolean) lambdaResp.get("isBase64Encoded");
if (responseBody == null) {
req.response().end();
} else if (Boolean.TRUE.equals(isBase64)) {
byte[] decoded = Base64.getDecoder().decode(String.valueOf(responseBody));
req.response().end(Buffer.buffer(decoded));
} else {
req.response().end(String.valueOf(responseBody));
}
} catch (Exception e) {
}).onFailure(e -> {
LOG.errorf(e, "Error invoking Lambda target %s", functionArn);
req.response().setStatusCode(502).end("Lambda invocation error");
}
});
});
}
private void writeLambdaResponse(io.vertx.core.http.HttpServerRequest req, InvokeResult result) throws java.io.IOException {
if (result.getFunctionError() != null) {
req.response().setStatusCode(502).end("Lambda function error: " + result.getFunctionError());
return;
}
if (result.getPayload() == null || result.getPayload().length == 0) {
req.response().setStatusCode(200).end();
return;
}
Map<String, Object> lambdaResp = objectMapper.readValue(result.getPayload(),
new TypeReference<Map<String, Object>>() {});
int statusCode = 200;
Object sc = lambdaResp.get("statusCode");
if (sc != null) {
statusCode = ((Number) sc).intValue();
}
req.response().setStatusCode(statusCode);
Object headers = lambdaResp.get("headers");
if (headers instanceof Map<?, ?> headerMap) {
for (Map.Entry<?, ?> entry : headerMap.entrySet()) {
req.response().putHeader(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
}
}
Object multiValueHeaders = lambdaResp.get("multiValueHeaders");
if (multiValueHeaders instanceof Map<?, ?> mvh) {
for (Map.Entry<?, ?> entry : mvh.entrySet()) {
if (entry.getValue() instanceof List<?> values) {
for (Object v : values) {
req.response().putHeader(String.valueOf(entry.getKey()), String.valueOf(v));
}
}
}
}
Object responseBody = lambdaResp.get("body");
Boolean isBase64 = (Boolean) lambdaResp.get("isBase64Encoded");
if (responseBody == null) {
req.response().end();
} else if (Boolean.TRUE.equals(isBase64)) {
byte[] decoded = Base64.getDecoder().decode(String.valueOf(responseBody));
req.response().end(Buffer.buffer(decoded));
} else {
req.response().end(String.valueOf(responseBody));
}
}
private Map<String, Object> buildAlbEvent(io.vertx.core.http.HttpServerRequest req, Buffer body) {
Map<String, Object> event = new HashMap<>();
event.put("requestContext", Map.of("elb", Map.of("targetGroupArn", "")));