fix(lambda): redrive failed SQS ESM messages to the DLQ (#1419)

* fix(lambda): redrive failed SQS ESM messages to the DLQ

When an SQS event source mapping invocation failed, the poller left the
message in-flight for the full execution-cover visibility window
(fn.timeout + 30s) instead of returning it to the queue. The message was
therefore never re-received, so ApproximateReceiveCount never crossed
maxReceiveCount and the queue's RedrivePolicy never moved it to the DLQ.

On a failed invocation (and for reported partial-batch failures) reset the
affected messages' visibility to 0 so the next poll re-receives them,
letting the existing redrive machinery move them to the DLQ.

Closes #1416

* fix(lambda): return failed ESM messages using the queue visibility timeout

Address PR review: resetting a failed message's visibility to 0 made it
immediately eligible for redelivery, which spins a tight retry loop for a
persistently failing function (far more aggressive than AWS). Reset to the
queue's configured VisibilityTimeout instead (falling back to the AWS
default of 30s when unset), matching AWS's redelivery cadence while still
letting ApproximateReceiveCount climb so the RedrivePolicy moves the
message to the DLQ.

Also document pollAndInvoke's package-private visibility as test-only.
This commit is contained in:
Stuart Lang authored and GitHub committed 2026-06-19 00:47:37 -05:00
1 parent b2aea42072
commit dba23bf7e2
2 files changed
+183 -6

No files matched your search

@@ -4,6 +4,9 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.lambda.model.EventSourceMapping;
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.github.hectorvent.floci.services.sqs.SqsService;
import io.github.hectorvent.floci.services.sqs.model.Message;
import io.vertx.core.Vertx;
@@ -12,15 +15,26 @@ import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class SqsEventSourcePollerTest {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private SqsEventSourcePoller poller;
private SqsService sqsService;
private LambdaExecutorService executorService;
private LambdaFunctionStore functionStore;
@BeforeEach
void setUp() {
@@ -32,11 +46,15 @@ class SqsEventSourcePollerTest {
when(lambdaConfig.pollIntervalMs()).thenReturn(1000L);
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
sqsService = mock(SqsService.class);
executorService = mock(LambdaExecutorService.class);
functionStore = mock(LambdaFunctionStore.class);
poller = new SqsEventSourcePoller(
mock(Vertx.class),
mock(SqsService.class),
mock(LambdaExecutorService.class),
mock(LambdaFunctionStore.class),
sqsService,
executorService,
functionStore,
mock(EsmStore.class),
config,
OBJECT_MAPPER
@@ -87,4 +105,105 @@ class SqsEventSourcePollerTest {
assertEquals("000000000000", attrs.get("SenderId").asText());
}
private EventSourceMapping esm() {
EventSourceMapping esm = new EventSourceMapping();
esm.setUuid("esm-uuid");
esm.setAccountId("000000000000");
esm.setRegion("us-east-1");
esm.setFunctionName("throwfn");
esm.setEventSourceArn("arn:aws:sqs:us-east-1:000000000000:esm-src");
esm.setQueueUrl("http://localhost:4566/000000000000/esm-src");
esm.setBatchSize(1);
return esm;
}
private Message message(String id) {
Message msg = new Message();
msg.setMessageId(id);
msg.setReceiptHandle("rh-" + id);
msg.setBody("body-" + id);
msg.setSentTimestamp(Instant.now());
return msg;
}
@Test
void failedInvocationReturnsMessagesToQueueUsingQueueVisibilityTimeout() {
EventSourceMapping esm = esm();
LambdaFunction fn = new LambdaFunction();
fn.setFunctionName("throwfn");
fn.setTimeout(10);
when(functionStore.getForAccount("000000000000", "us-east-1", "throwfn"))
.thenReturn(Optional.of(fn));
Message msg = message("m1");
when(sqsService.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
.thenReturn(List.of(msg));
when(sqsService.getQueueAttributes(eq(esm.getQueueUrl()), any(), eq("us-east-1")))
.thenReturn(Map.of("VisibilityTimeout", "2"));
InvokeResult failure = new InvokeResult();
failure.setFunctionError("Handled");
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(failure);
poller.pollAndInvoke(esm);
// The failed message must be made visible again after the queue's own visibility
// timeout (2s here) so the next poll re-receives it and the queue's RedrivePolicy
// can move it to the DLQ — rather than staying in-flight for fn.timeout + 30s.
verify(sqsService, timeout(2000)).changeMessageVisibility(
esm.getQueueUrl(), "rh-m1", 2, "us-east-1");
verify(sqsService, never()).deleteMessage(any(), any(), any());
}
@Test
void failedInvocationFallsBackToDefaultVisibilityWhenQueueHasNone() {
EventSourceMapping esm = esm();
LambdaFunction fn = new LambdaFunction();
fn.setFunctionName("throwfn");
fn.setTimeout(10);
when(functionStore.getForAccount("000000000000", "us-east-1", "throwfn"))
.thenReturn(Optional.of(fn));
Message msg = message("m1");
when(sqsService.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
.thenReturn(List.of(msg));
// Queue reports no VisibilityTimeout attribute.
when(sqsService.getQueueAttributes(eq(esm.getQueueUrl()), any(), eq("us-east-1")))
.thenReturn(Map.of());
InvokeResult failure = new InvokeResult();
failure.setFunctionError("Handled");
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(failure);
poller.pollAndInvoke(esm);
// Falls back to the AWS default of 30s rather than 0 (which would spin a tight retry loop).
verify(sqsService, timeout(2000)).changeMessageVisibility(
esm.getQueueUrl(), "rh-m1", 30, "us-east-1");
}
@Test
void successfulInvocationDeletesMessagesAndDoesNotResetVisibility() {
EventSourceMapping esm = esm();
LambdaFunction fn = new LambdaFunction();
fn.setFunctionName("throwfn");
fn.setTimeout(10);
when(functionStore.getForAccount("000000000000", "us-east-1", "throwfn"))
.thenReturn(Optional.of(fn));
Message msg = message("m1");
when(sqsService.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
.thenReturn(List.of(msg));
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(new InvokeResult());
poller.pollAndInvoke(esm);
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
verify(sqsService, never()).changeMessageVisibility(any(), any(), anyInt(), any());
}
}