Light, fluffy, and always free - The AWS Local Emulator alternative
fix(lambda): initialize ESM pollers at startup and fix worker-pool exhaustion in RuntimeApiServer (#543)
Hector Venturac8057785eb2d
diff --git a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.javaindex 1548629cd5..0907d268da 100644--- a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java+++ b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java@@ -7,6 +7,9 @@ import io.github.hectorvent.floci.lifecycle.inithook.InitializationHook;import io.github.hectorvent.floci.lifecycle.inithook.InitializationHooksRunner;import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerManager;import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheProxyManager;+import io.github.hectorvent.floci.services.lambda.DynamoDbStreamsEventSourcePoller;+import io.github.hectorvent.floci.services.lambda.KinesisEventSourcePoller;+import io.github.hectorvent.floci.services.lambda.SqsEventSourcePoller;import io.github.hectorvent.floci.services.rds.container.RdsContainerManager;import io.github.hectorvent.floci.services.rds.proxy.RdsProxyManager;import io.quarkus.runtime.Quarkus;@@ -36,6 +39,9 @@ public class EmulatorLifecycle {private final RdsContainerManager rdsContainerManager;private final RdsProxyManager rdsProxyManager;private final InitializationHooksRunner initializationHooksRunner;+ private final SqsEventSourcePoller sqsPoller;+ private final KinesisEventSourcePoller kinesisPoller;+ private final DynamoDbStreamsEventSourcePoller dynamodbStreamsPoller;@Injectpublic EmulatorLifecycle(StorageFactory storageFactory, ServiceRegistry serviceRegistry,@@ -44,7 +50,10 @@ public class EmulatorLifecycle {ElastiCacheProxyManager elastiCacheProxyManager,RdsContainerManager rdsContainerManager,RdsProxyManager rdsProxyManager,- InitializationHooksRunner initializationHooksRunner) {+ InitializationHooksRunner initializationHooksRunner,+ SqsEventSourcePoller sqsPoller,+ KinesisEventSourcePoller kinesisPoller,+ DynamoDbStreamsEventSourcePoller dynamodbStreamsPoller) {this.storageFactory = storageFactory;this.serviceRegistry = serviceRegistry;this.config = config;@@ -53,6 +62,9 @@ public class EmulatorLifecycle {this.rdsContainerManager = rdsContainerManager;this.rdsProxyManager = rdsProxyManager;this.initializationHooksRunner = initializationHooksRunner;+ this.sqsPoller = sqsPoller;+ this.kinesisPoller = kinesisPoller;+ this.dynamodbStreamsPoller = dynamodbStreamsPoller;}void onStart(@Observes StartupEvent ignored) {@@ -63,6 +75,10 @@ public class EmulatorLifecycle {serviceRegistry.logEnabledServices();storageFactory.loadAll();+ sqsPoller.startPersistedPollers();+ kinesisPoller.startPersistedPollers();+ dynamodbStreamsPoller.startPersistedPollers();+if (!initializationHooksRunner.hasHooks(InitializationHook.START)) {LOG.info("=== AWS Local Emulator Ready ===");}diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/DynamoDbStreamsEventSourcePoller.java b/src/main/java/io/github/hectorvent/floci/services/lambda/DynamoDbStreamsEventSourcePoller.javaindex 73cfba498d..29b37913a8 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/DynamoDbStreamsEventSourcePoller.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/DynamoDbStreamsEventSourcePoller.java@@ -12,7 +12,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.node.ArrayNode;import com.fasterxml.jackson.databind.node.ObjectNode;import io.vertx.core.Vertx;-import jakarta.annotation.PostConstruct;import jakarta.annotation.PreDestroy;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;@@ -59,8 +58,7 @@ public class DynamoDbStreamsEventSourcePoller {this.pollIntervalMs = config.services().lambda().pollIntervalMs();}- @PostConstruct- void init() {+ public void startPersistedPollers() {for (EventSourceMapping esm : esmStore.list()) {if (esm.isEnabled() && esm.getEventSourceArn().contains(":dynamodb:")) {startPolling(esm);diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePoller.java b/src/main/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePoller.javaindex 3d6c29573b..2a357acedb 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePoller.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/KinesisEventSourcePoller.java@@ -13,7 +13,6 @@ import io.github.hectorvent.floci.services.lambda.model.InvocationType;import io.github.hectorvent.floci.services.lambda.model.InvokeResult;import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;import io.vertx.core.Vertx;-import jakarta.annotation.PostConstruct;import jakarta.annotation.PreDestroy;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;@@ -61,8 +60,7 @@ public class KinesisEventSourcePoller {this.objectMapper = objectMapper;}- @PostConstruct- void init() {+ public void startPersistedPollers() {List<EventSourceMapping> esms = esmStore.list();for (EventSourceMapping esm : esms) {if (esm.isEnabled() && esm.getEventSourceArn().contains(":kinesis:")) {diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java b/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.javaindex 7b58f9329f..f7545f4f79 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java@@ -12,7 +12,6 @@ import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;import io.github.hectorvent.floci.services.sqs.SqsService;import io.github.hectorvent.floci.services.sqs.model.Message;import io.vertx.core.Vertx;-import jakarta.annotation.PostConstruct;import jakarta.annotation.PreDestroy;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;@@ -69,8 +68,7 @@ public class SqsEventSourcePoller {this.objectMapper = objectMapper;}- @PostConstruct- void init() {+ public void startPersistedPollers() {List<EventSourceMapping> esms = esmStore.list();for (EventSourceMapping esm : esms) {if (esm.isEnabled() && esm.getEventSourceArn().contains(":sqs:")) {diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java b/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.javaindex b997585ac5..b99d6a9e53 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java@@ -63,16 +63,20 @@ public class RuntimeApiServer {Router router = Router.router(vertx);router.route().handler(BodyHandler.create());- // GET /runtime/invocation/next — long-poll for next invocation+ // GET /runtime/invocation/next — poll for next invocation, one request per worker thread.+ // Returns 204 when nothing arrives within 30 s so the runtime re-polls.+ // This matches the real AWS Runtime API contract and caps each handler to one+ // Vert.x worker thread for at most 30 seconds, preventing worker-pool exhaustion+ // when multiple warm containers are polling concurrently.router.get(NEXT_PATH).blockingHandler(ctx -> {try {- PendingInvocation invocation = null;- while (invocation == null && !stopped) {- invocation = pendingQueue.poll(30, TimeUnit.SECONDS);- if (invocation == SHUTDOWN_SENTINEL) {- invocation = null;- break;- }+ if (stopped) {+ ctx.response().setStatusCode(204).end();+ return;+ }+ PendingInvocation invocation = pendingQueue.poll(30, TimeUnit.SECONDS);+ if (invocation == SHUTDOWN_SENTINEL) {+ invocation = null;}// If stop() ran after poll() returned a real invocation (narrow race before// inFlight.put), complete it here so the caller doesn't hang.@@ -97,7 +101,7 @@ public class RuntimeApiServer {: "{}");} catch (InterruptedException e) {Thread.currentThread().interrupt();- ctx.response().setStatusCode(500).end();+ ctx.response().setStatusCode(204).end();}});diff --git a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.javaindex b6b4653a24..4323afb966 100644--- a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java+++ b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycleTest.java@@ -7,6 +7,9 @@ import io.github.hectorvent.floci.lifecycle.inithook.InitializationHook;import io.github.hectorvent.floci.lifecycle.inithook.InitializationHooksRunner;import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerManager;import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheProxyManager;+import io.github.hectorvent.floci.services.lambda.DynamoDbStreamsEventSourcePoller;+import io.github.hectorvent.floci.services.lambda.KinesisEventSourcePoller;+import io.github.hectorvent.floci.services.lambda.SqsEventSourcePoller;import io.github.hectorvent.floci.services.rds.container.RdsContainerManager;import io.github.hectorvent.floci.services.rds.proxy.RdsProxyManager;import io.quarkus.runtime.ShutdownDelayInitiatedEvent;@@ -39,6 +42,9 @@ class EmulatorLifecycleTest {@Mock private RdsContainerManager rdsContainerManager;@Mock private RdsProxyManager rdsProxyManager;@Mock private InitializationHooksRunner initializationHooksRunner;+ @Mock private SqsEventSourcePoller sqsPoller;+ @Mock private KinesisEventSourcePoller kinesisPoller;+ @Mock private DynamoDbStreamsEventSourcePoller dynamodbStreamsPoller;private EmulatorLifecycle emulatorLifecycle;@@ -47,7 +53,8 @@ class EmulatorLifecycleTest {emulatorLifecycle = new EmulatorLifecycle(storageFactory, serviceRegistry, config,elastiCacheContainerManager, elastiCacheProxyManager,- rdsContainerManager, rdsProxyManager, initializationHooksRunner);+ rdsContainerManager, rdsProxyManager, initializationHooksRunner,+ sqsPoller, kinesisPoller, dynamodbStreamsPoller);}private void stubStorageConfig() {diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.javaindex cfebaec8f6..8adb924689 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java@@ -78,30 +78,30 @@ class RuntimeApiServerTest {@Test@Timeout(45)- void nextEndpoint_doesNotReturn204AfterPollCycle() throws Exception {+ void nextEndpoint_returns204OnTimeout_thenReturns200OnRepoll() throws Exception {+ // AWS Runtime API contract: GET /next returns 204 when no invocation arrives within+ // the poll window. The runtime re-polls and picks up the next queued invocation.HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://localhost:" + port + "/2018-06-01/runtime/invocation/next")).GET().build();- CompletableFuture<HttpResponse<String>> asyncResponse =- httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());-- Thread.sleep(33_000);-- assertFalse(asyncResponse.isDone(),- "GET /next must NOT return (old bug: 204 after 30s single poll)");+ // First poll: nothing queued — should return 204 after ~30 s.+ HttpResponse<String> emptyResponse = httpClient.send(request, HttpResponse.BodyHandlers.ofString());+ assertEquals(204, emptyResponse.statusCode(),+ "GET /next should return 204 when the queue is empty after the poll timeout");+ // Enqueue an invocation and re-poll — should be returned immediately.PendingInvocation invocation = new PendingInvocation(- "req-delayed", "{\"after_poll_cycle\":true}".getBytes(),+ "req-after-timeout", "{\"repoll\":true}".getBytes(),System.currentTimeMillis() + 60_000,"arn:aws:lambda:us-east-1:000000000000:function:test",new CompletableFuture<>());server.enqueue(invocation);- HttpResponse<String> response = asyncResponse.get(10, TimeUnit.SECONDS);+ HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());assertEquals(200, response.statusCode());- assertEquals("req-delayed",+ assertEquals("req-after-timeout",response.headers().firstValue("Lambda-Runtime-Aws-Request-Id").orElse(""));}