mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 09:22:49 -05:00
fix(lambda): honor MaximumRetryAttempts and DestinationConfig.OnFailure for DynamoDB Streams ESM (#3263) (#3428)
* fix(lambda): honor MaximumRetryAttempts and DestinationConfig.OnFailure for DynamoDB Streams ESM (#3263) * test(lambda): pin the OnFailure payload's arrival timestamps to epoch seconds Seeds a known approximateCreationDateTime and asserts the exact approximateArrivalOfFirstRecord/LastRecord timestamps, using a value 46+ years away from its millis interpretation so a seconds/millis mix-up cannot accidentally pass. The prior tests only asserted the fields were present, which stayed green even with the epoch-millis bug fixed in the previous commit's Instant.ofEpochSecond conversion. * fix(lambda): track retries and trigger OnFailure when batch does not clear under ReportBatchItemFailures * fix(lambda): carry retry count across partial batch failures in DynamoDB Streams ESM
This commit is contained in:
1 parent
2290247802
commit
1af3b56683
6 files changed
+633
-30
No files matched your search
+405
-2
@@ -19,11 +19,13 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -34,6 +36,7 @@ import static org.mockito.Mockito.atLeast;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.timeout;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -58,6 +61,8 @@ class DynamoDbStreamsEventSourcePollerTest {
|
||||
private EsmStore esmStore;
|
||||
private EmulatorConfig config;
|
||||
private PipesFilterMatcher filterMatcher;
|
||||
private io.github.hectorvent.floci.services.sqs.SqsService sqsService;
|
||||
private io.github.hectorvent.floci.services.sns.SnsService snsService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
@@ -67,18 +72,21 @@ class DynamoDbStreamsEventSourcePollerTest {
|
||||
when(config.services()).thenReturn(services);
|
||||
when(services.lambda()).thenReturn(lambdaConfig);
|
||||
when(lambdaConfig.pollIntervalMs()).thenReturn(1000L);
|
||||
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
|
||||
|
||||
streamService = mock(DynamoDbStreamService.class);
|
||||
executorService = mock(LambdaExecutorService.class);
|
||||
functionStore = mock(LambdaFunctionStore.class);
|
||||
esmStore = new EsmStore(new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, ACCOUNT_ID));
|
||||
filterMatcher = new PipesFilterMatcher(OBJECT_MAPPER);
|
||||
sqsService = mock(io.github.hectorvent.floci.services.sqs.SqsService.class);
|
||||
snsService = mock(io.github.hectorvent.floci.services.sns.SnsService.class);
|
||||
|
||||
// A mocked Vertx makes setPeriodic a no-op, so startPolling registers no live timer and
|
||||
// the tests drive pollAndInvoke deterministically.
|
||||
poller = new DynamoDbStreamsEventSourcePoller(
|
||||
mock(Vertx.class), streamService, executorService, functionStore,
|
||||
esmStore, OBJECT_MAPPER, config, filterMatcher);
|
||||
esmStore, OBJECT_MAPPER, config, filterMatcher, sqsService, snsService);
|
||||
}
|
||||
|
||||
private EventSourceMapping persistedStreamsEsmWithStaleCheckpoint() {
|
||||
@@ -152,7 +160,7 @@ class DynamoDbStreamsEventSourcePollerTest {
|
||||
private DynamoDbStreamsEventSourcePoller pollerWith(EsmStore store) {
|
||||
return new DynamoDbStreamsEventSourcePoller(
|
||||
mock(Vertx.class), streamService, executorService, functionStore,
|
||||
store, OBJECT_MAPPER, config, filterMatcher);
|
||||
store, OBJECT_MAPPER, config, filterMatcher, sqsService, snsService);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -555,6 +563,401 @@ class DynamoDbStreamsEventSourcePollerTest {
|
||||
assertEquals(STALE_CHECKPOINT, esm.getShardSequenceNumbers().get(DynamoDbStreamService.SHARD_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void maxRetryAttemptsExhaustedDeliversToSqsOnFailureDestinationAndAdvancesCheckpoint() throws Exception {
|
||||
stubTrimHorizon(List.of(ddbRecord("s1", "INSERT", "{\"status\":{\"S\":\"active\"}}")));
|
||||
InvokeResult err = new InvokeResult();
|
||||
err.setFunctionError("Unhandled");
|
||||
err.setStatusCode(200);
|
||||
err.setRequestId("req-123");
|
||||
err.setExecutedVersion("$LATEST");
|
||||
when(executorService.invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(err);
|
||||
|
||||
EsmStore store = mock(EsmStore.class);
|
||||
EventSourceMapping esm = filterEsm();
|
||||
esm.setMaximumRetryAttempts(1); // 1 retry attempt allowed, 2nd error exhausts retries
|
||||
|
||||
String sqsArn = "arn:aws:sqs:us-east-1:000000000000:my-dlq";
|
||||
EventSourceMapping.DestinationConfig destConfig = new EventSourceMapping.DestinationConfig();
|
||||
EventSourceMapping.OnFailure onFailure = new EventSourceMapping.OnFailure();
|
||||
onFailure.setDestination(sqsArn);
|
||||
destConfig.setOnFailure(onFailure);
|
||||
esm.setDestinationConfig(destConfig);
|
||||
|
||||
DynamoDbStreamsEventSourcePoller p = pollerWith(store);
|
||||
|
||||
// First poll: attempt 1 (initial failure, retries left)
|
||||
p.pollAndInvoke(esm);
|
||||
// Verify invoke was called for attempt 1
|
||||
verify(executorService, timeout(2000)).invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse));
|
||||
// Wait for poll 1 to finish by ensuring activePolls is released
|
||||
long deadline = System.currentTimeMillis() + 3000;
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
if (p.activePolls.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
Thread.sleep(25);
|
||||
}
|
||||
// After 1st failure: checkpoint not advanced, no DLQ delivery
|
||||
verify(store, never()).saveForAccount(anyString(), any());
|
||||
verify(sqsService, never()).sendMessage(anyString(), anyString(), anyInt(), anyString());
|
||||
|
||||
// Second poll: attempt 2 (retry 1, which exceeds maxRetryAttempts=1 -> exhausted!)
|
||||
p.pollAndInvoke(esm);
|
||||
|
||||
// Checkpoint advanced to "s1"
|
||||
verify(store, timeout(2000)).saveForAccount(eq(ACCOUNT_ID), any());
|
||||
assertEquals("s1", esm.getShardSequenceNumbers().get(DynamoDbStreamService.SHARD_ID));
|
||||
|
||||
// SQS delivery invoked
|
||||
ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
|
||||
String expectedQueueUrl = "http://localhost:4566/000000000000/my-dlq";
|
||||
verify(sqsService, timeout(2000)).sendMessage(eq(expectedQueueUrl), bodyCaptor.capture(), eq(0), eq("us-east-1"));
|
||||
|
||||
JsonNode dlqPayload = OBJECT_MAPPER.readTree(bodyCaptor.getValue());
|
||||
assertEquals("1.0", dlqPayload.path("version").asText());
|
||||
assertEquals("RetryAttemptsExhausted", dlqPayload.path("requestContext").path("condition").asText());
|
||||
assertEquals(2, dlqPayload.path("requestContext").path("approximateInvokeCount").asInt());
|
||||
assertEquals("req-123", dlqPayload.path("requestContext").path("requestId").asText());
|
||||
assertEquals("Unhandled", dlqPayload.path("responseContext").path("functionError").asText());
|
||||
assertEquals(DynamoDbStreamService.SHARD_ID, dlqPayload.path("DDBStreamBatchInfo").path("shardId").asText());
|
||||
assertEquals("s1", dlqPayload.path("DDBStreamBatchInfo").path("startSequenceNumber").asText());
|
||||
assertEquals("s1", dlqPayload.path("DDBStreamBatchInfo").path("endSequenceNumber").asText());
|
||||
assertEquals(1, dlqPayload.path("DDBStreamBatchInfo").path("batchSize").asInt());
|
||||
assertEquals(STREAM_ARN, dlqPayload.path("DDBStreamBatchInfo").path("streamArn").asText());
|
||||
assertTrue(dlqPayload.path("DDBStreamBatchInfo").has("approximateArrivalOfFirstRecord"));
|
||||
assertTrue(dlqPayload.path("DDBStreamBatchInfo").has("approximateArrivalOfLastRecord"));
|
||||
assertFalse(dlqPayload.has("hasBeenTruncated"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void onFailurePayloadUsesEpochSecondsNotMillisForArrivalTimestamps() throws Exception {
|
||||
// Chosen so the seconds/millis interpretations land 46+ years apart: a millis bug cannot
|
||||
// accidentally parse back to this epoch-seconds value.
|
||||
long knownEpochSeconds = 1_700_000_000L;
|
||||
DynamoDbStreamRecord rec = ddbRecord("s1", "INSERT", "{\"status\":{\"S\":\"active\"}}");
|
||||
rec.setApproximateCreationDateTime(knownEpochSeconds);
|
||||
stubTrimHorizon(List.of(rec));
|
||||
|
||||
InvokeResult err = new InvokeResult();
|
||||
err.setFunctionError("Unhandled");
|
||||
when(executorService.invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(err);
|
||||
|
||||
EsmStore store = mock(EsmStore.class);
|
||||
EventSourceMapping esm = filterEsm();
|
||||
esm.setMaximumRetryAttempts(0); // exhausts on the first failure
|
||||
|
||||
String sqsArn = "arn:aws:sqs:us-east-1:000000000000:my-dlq";
|
||||
EventSourceMapping.DestinationConfig destConfig = new EventSourceMapping.DestinationConfig();
|
||||
EventSourceMapping.OnFailure onFailure = new EventSourceMapping.OnFailure();
|
||||
onFailure.setDestination(sqsArn);
|
||||
destConfig.setOnFailure(onFailure);
|
||||
esm.setDestinationConfig(destConfig);
|
||||
|
||||
DynamoDbStreamsEventSourcePoller p = pollerWith(store);
|
||||
p.pollAndInvoke(esm);
|
||||
|
||||
ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
|
||||
verify(sqsService, timeout(2000)).sendMessage(anyString(), bodyCaptor.capture(), anyInt(), anyString());
|
||||
|
||||
JsonNode dlqPayload = OBJECT_MAPPER.readTree(bodyCaptor.getValue());
|
||||
Instant first = Instant.parse(
|
||||
dlqPayload.path("DDBStreamBatchInfo").path("approximateArrivalOfFirstRecord").asText());
|
||||
Instant last = Instant.parse(
|
||||
dlqPayload.path("DDBStreamBatchInfo").path("approximateArrivalOfLastRecord").asText());
|
||||
assertEquals(knownEpochSeconds, first.getEpochSecond());
|
||||
assertEquals(knownEpochSeconds, last.getEpochSecond());
|
||||
}
|
||||
|
||||
@Test
|
||||
void maxRetryAttemptsExhaustedDeliversToSnsOnFailureDestination() {
|
||||
stubTrimHorizon(List.of(ddbRecord("s1", "INSERT", "{\"status\":{\"S\":\"active\"}}")));
|
||||
InvokeResult err = new InvokeResult();
|
||||
err.setFunctionError("Handled");
|
||||
when(executorService.invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(err);
|
||||
|
||||
EsmStore store = mock(EsmStore.class);
|
||||
EventSourceMapping esm = filterEsm();
|
||||
esm.setMaximumRetryAttempts(0); // 0 retries allowed, 1st error exhausts immediately
|
||||
|
||||
String snsArn = "arn:aws:sns:us-east-1:000000000000:my-dlq-topic";
|
||||
EventSourceMapping.DestinationConfig destConfig = new EventSourceMapping.DestinationConfig();
|
||||
EventSourceMapping.OnFailure onFailure = new EventSourceMapping.OnFailure();
|
||||
onFailure.setDestination(snsArn);
|
||||
destConfig.setOnFailure(onFailure);
|
||||
esm.setDestinationConfig(destConfig);
|
||||
|
||||
DynamoDbStreamsEventSourcePoller p = pollerWith(store);
|
||||
|
||||
p.pollAndInvoke(esm);
|
||||
|
||||
verify(store, timeout(2000)).saveForAccount(eq(ACCOUNT_ID), any());
|
||||
assertEquals("s1", esm.getShardSequenceNumbers().get(DynamoDbStreamService.SHARD_ID));
|
||||
|
||||
verify(snsService, timeout(2000)).publish(eq(snsArn), any(), anyString(), eq("ESM OnFailure"), eq("us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void maxRetryAttemptsExhaustedTracksRetriesAcrossExpandingBatchWindow() throws Exception {
|
||||
when(streamService.getShardIterator(eq(STREAM_ARN), eq(DynamoDbStreamService.SHARD_ID),
|
||||
eq("TRIM_HORIZON"), any())).thenReturn("it-1", "it-2");
|
||||
when(streamService.getRecords(eq("it-1"), anyInt())).thenReturn(
|
||||
new DynamoDbStreamService.GetRecordsResult(
|
||||
List.of(ddbRecord("s1", "INSERT", "{\"status\":{\"S\":\"active\"}}")), "it-1"));
|
||||
when(streamService.getRecords(eq("it-2"), anyInt())).thenReturn(
|
||||
new DynamoDbStreamService.GetRecordsResult(
|
||||
List.of(ddbRecord("s1", "INSERT", "{\"status\":{\"S\":\"active\"}}"),
|
||||
ddbRecord("s2", "INSERT", "{\"status\":{\"S\":\"active\"}}")), "it-2"));
|
||||
|
||||
InvokeResult err = new InvokeResult();
|
||||
err.setFunctionError("Unhandled");
|
||||
when(executorService.invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(err);
|
||||
LambdaFunction fn = new LambdaFunction();
|
||||
fn.setFunctionName("fn");
|
||||
when(functionStore.getForAccount(ACCOUNT_ID, "us-east-1", "fn")).thenReturn(Optional.of(fn));
|
||||
|
||||
EsmStore store = mock(EsmStore.class);
|
||||
EventSourceMapping esm = filterEsm();
|
||||
esm.setMaximumRetryAttempts(1); // 1 retry allowed, 2nd error exhausts
|
||||
|
||||
String sqsArn = "arn:aws:sqs:us-east-1:000000000000:my-dlq";
|
||||
EventSourceMapping.DestinationConfig destConfig = new EventSourceMapping.DestinationConfig();
|
||||
EventSourceMapping.OnFailure onFailure = new EventSourceMapping.OnFailure();
|
||||
onFailure.setDestination(sqsArn);
|
||||
destConfig.setOnFailure(onFailure);
|
||||
esm.setDestinationConfig(destConfig);
|
||||
|
||||
DynamoDbStreamsEventSourcePoller p = pollerWith(store);
|
||||
|
||||
p.pollAndInvoke(esm);
|
||||
verify(executorService, timeout(2000)).invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse));
|
||||
|
||||
long deadline = System.currentTimeMillis() + 3000;
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
if (p.activePolls.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
Thread.sleep(25);
|
||||
}
|
||||
|
||||
p.pollAndInvoke(esm);
|
||||
|
||||
verify(store, timeout(2000)).saveForAccount(eq(ACCOUNT_ID), any());
|
||||
assertEquals("s2", esm.getShardSequenceNumbers().get(DynamoDbStreamService.SHARD_ID));
|
||||
|
||||
ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
|
||||
String expectedQueueUrl = "http://localhost:4566/000000000000/my-dlq";
|
||||
verify(sqsService, timeout(2000)).sendMessage(eq(expectedQueueUrl), bodyCaptor.capture(), eq(0), eq("us-east-1"));
|
||||
|
||||
JsonNode dlqPayload = OBJECT_MAPPER.readTree(bodyCaptor.getValue());
|
||||
assertEquals("1.0", dlqPayload.path("version").asText());
|
||||
assertEquals(2, dlqPayload.path("requestContext").path("approximateInvokeCount").asInt());
|
||||
assertEquals("s1", dlqPayload.path("DDBStreamBatchInfo").path("startSequenceNumber").asText());
|
||||
assertEquals("s2", dlqPayload.path("DDBStreamBatchInfo").path("endSequenceNumber").asText());
|
||||
assertEquals(2, dlqPayload.path("DDBStreamBatchInfo").path("batchSize").asInt());
|
||||
assertFalse(dlqPayload.has("hasBeenTruncated"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportBatchItemFailuresExhaustsRetriesAndDeliversToOnFailureDestination() throws Exception {
|
||||
stubTrimHorizon(List.of(ddbRecord("s1", "INSERT", "{\"status\":{\"S\":\"active\"}}")));
|
||||
InvokeResult result = new InvokeResult();
|
||||
result.setStatusCode(200);
|
||||
result.setRequestId("req-batch-fail");
|
||||
result.setPayload("{\"batchItemFailures\":[{\"itemIdentifier\":\"s1\"}]}".getBytes());
|
||||
when(executorService.invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(result);
|
||||
|
||||
EsmStore store = mock(EsmStore.class);
|
||||
EventSourceMapping esm = filterEsm();
|
||||
esm.setFunctionResponseTypes(List.of("ReportBatchItemFailures"));
|
||||
esm.setMaximumRetryAttempts(1); // 1 retry attempt allowed, 2nd error exhausts retries
|
||||
|
||||
String sqsArn = "arn:aws:sqs:us-east-1:000000000000:my-dlq";
|
||||
EventSourceMapping.DestinationConfig destConfig = new EventSourceMapping.DestinationConfig();
|
||||
EventSourceMapping.OnFailure onFailure = new EventSourceMapping.OnFailure();
|
||||
onFailure.setDestination(sqsArn);
|
||||
destConfig.setOnFailure(onFailure);
|
||||
esm.setDestinationConfig(destConfig);
|
||||
|
||||
DynamoDbStreamsEventSourcePoller p = pollerWith(store);
|
||||
|
||||
// First poll: attempt 1 (initial partial failure, retries left)
|
||||
p.pollAndInvoke(esm);
|
||||
verify(executorService, timeout(2000)).invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse));
|
||||
|
||||
long deadline = System.currentTimeMillis() + 3000;
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
if (p.activePolls.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
Thread.sleep(25);
|
||||
}
|
||||
|
||||
// After 1st failure: checkpoint not advanced, no DLQ delivery
|
||||
verify(store, never()).saveForAccount(anyString(), any());
|
||||
verify(sqsService, never()).sendMessage(anyString(), anyString(), anyInt(), anyString());
|
||||
|
||||
// Second poll: attempt 2 (retry 1, which exceeds maxRetryAttempts=1 -> exhausted!)
|
||||
p.pollAndInvoke(esm);
|
||||
|
||||
// Checkpoint advanced to "s1"
|
||||
verify(store, timeout(2000)).saveForAccount(eq(ACCOUNT_ID), any());
|
||||
assertEquals("s1", esm.getShardSequenceNumbers().get(DynamoDbStreamService.SHARD_ID));
|
||||
|
||||
// SQS delivery invoked
|
||||
ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
|
||||
String expectedQueueUrl = "http://localhost:4566/000000000000/my-dlq";
|
||||
verify(sqsService, timeout(2000)).sendMessage(eq(expectedQueueUrl), bodyCaptor.capture(), eq(0), eq("us-east-1"));
|
||||
|
||||
JsonNode dlqPayload = OBJECT_MAPPER.readTree(bodyCaptor.getValue());
|
||||
assertEquals("1.0", dlqPayload.path("version").asText());
|
||||
assertEquals("RetryAttemptsExhausted", dlqPayload.path("requestContext").path("condition").asText());
|
||||
assertEquals(2, dlqPayload.path("requestContext").path("approximateInvokeCount").asInt());
|
||||
assertEquals("req-batch-fail", dlqPayload.path("requestContext").path("requestId").asText());
|
||||
assertEquals(200, dlqPayload.path("responseContext").path("statusCode").asInt());
|
||||
assertFalse(dlqPayload.path("responseContext").has("functionError"));
|
||||
assertEquals(DynamoDbStreamService.SHARD_ID, dlqPayload.path("DDBStreamBatchInfo").path("shardId").asText());
|
||||
assertEquals("s1", dlqPayload.path("DDBStreamBatchInfo").path("startSequenceNumber").asText());
|
||||
assertEquals("s1", dlqPayload.path("DDBStreamBatchInfo").path("endSequenceNumber").asText());
|
||||
assertEquals(1, dlqPayload.path("DDBStreamBatchInfo").path("batchSize").asInt());
|
||||
assertEquals(STREAM_ARN, dlqPayload.path("DDBStreamBatchInfo").path("streamArn").asText());
|
||||
assertFalse(dlqPayload.has("hasBeenTruncated"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportBatchItemFailuresPartialSuccessWithZeroRetriesExhaustsImmediately() throws Exception {
|
||||
when(streamService.getShardIterator(eq(STREAM_ARN), eq(DynamoDbStreamService.SHARD_ID),
|
||||
eq("TRIM_HORIZON"), any())).thenReturn("it-1");
|
||||
when(streamService.getRecords(eq("it-1"), anyInt())).thenReturn(
|
||||
new DynamoDbStreamService.GetRecordsResult(
|
||||
List.of(ddbRecord("s1", "INSERT", "{\"status\":{\"S\":\"active\"}}"),
|
||||
ddbRecord("s2", "INSERT", "{\"status\":{\"S\":\"active\"}}")), "it-1"));
|
||||
|
||||
InvokeResult partialFailure = new InvokeResult();
|
||||
partialFailure.setStatusCode(200);
|
||||
partialFailure.setPayload("{\"batchItemFailures\":[{\"itemIdentifier\":\"s2\"}]}".getBytes());
|
||||
when(executorService.invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(partialFailure);
|
||||
|
||||
LambdaFunction fn = new LambdaFunction();
|
||||
fn.setFunctionName("fn");
|
||||
when(functionStore.getForAccount(ACCOUNT_ID, "us-east-1", "fn")).thenReturn(Optional.of(fn));
|
||||
|
||||
EsmStore store = mock(EsmStore.class);
|
||||
EventSourceMapping esm = filterEsm();
|
||||
esm.setFunctionResponseTypes(List.of("ReportBatchItemFailures"));
|
||||
esm.setMaximumRetryAttempts(0); // 0 retries allowed: initial failure exhausts immediately
|
||||
|
||||
String sqsArn = "arn:aws:sqs:us-east-1:000000000000:my-dlq";
|
||||
EventSourceMapping.DestinationConfig destConfig = new EventSourceMapping.DestinationConfig();
|
||||
EventSourceMapping.OnFailure onFailure = new EventSourceMapping.OnFailure();
|
||||
onFailure.setDestination(sqsArn);
|
||||
destConfig.setOnFailure(onFailure);
|
||||
esm.setDestinationConfig(destConfig);
|
||||
|
||||
DynamoDbStreamsEventSourcePoller p = pollerWith(store);
|
||||
|
||||
// First poll: s1 succeeds, s2 fails -> maxRetries=0 exhausts immediately
|
||||
p.pollAndInvoke(esm);
|
||||
|
||||
// Checkpoint advanced to "s2"
|
||||
verify(store, timeout(2000)).saveForAccount(eq(ACCOUNT_ID), any());
|
||||
assertEquals("s2", esm.getShardSequenceNumbers().get(DynamoDbStreamService.SHARD_ID));
|
||||
|
||||
ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
|
||||
String expectedQueueUrl = "http://localhost:4566/000000000000/my-dlq";
|
||||
verify(sqsService, timeout(2000)).sendMessage(eq(expectedQueueUrl), bodyCaptor.capture(), eq(0), eq("us-east-1"));
|
||||
|
||||
JsonNode dlqPayload = OBJECT_MAPPER.readTree(bodyCaptor.getValue());
|
||||
assertEquals("1.0", dlqPayload.path("version").asText());
|
||||
assertEquals("RetryAttemptsExhausted", dlqPayload.path("requestContext").path("condition").asText());
|
||||
assertEquals(1, dlqPayload.path("requestContext").path("approximateInvokeCount").asInt());
|
||||
assertEquals("s2", dlqPayload.path("DDBStreamBatchInfo").path("startSequenceNumber").asText());
|
||||
assertEquals("s2", dlqPayload.path("DDBStreamBatchInfo").path("endSequenceNumber").asText());
|
||||
assertEquals(1, dlqPayload.path("DDBStreamBatchInfo").path("batchSize").asInt());
|
||||
|
||||
// Prove there is NO second Lambda invocation for that failed item
|
||||
verify(executorService, times(1)).invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportBatchItemFailuresPartialSuccessAllowsOneRetryBeforeExhaustion() throws Exception {
|
||||
when(streamService.getShardIterator(eq(STREAM_ARN), eq(DynamoDbStreamService.SHARD_ID),
|
||||
eq("TRIM_HORIZON"), any())).thenReturn("it-1");
|
||||
when(streamService.getShardIterator(eq(STREAM_ARN), eq(DynamoDbStreamService.SHARD_ID),
|
||||
eq("AFTER_SEQUENCE_NUMBER"), eq("s1"))).thenReturn("it-2");
|
||||
when(streamService.getRecords(eq("it-1"), anyInt())).thenReturn(
|
||||
new DynamoDbStreamService.GetRecordsResult(
|
||||
List.of(ddbRecord("s1", "INSERT", "{\"status\":{\"S\":\"active\"}}"),
|
||||
ddbRecord("s2", "INSERT", "{\"status\":{\"S\":\"active\"}}")), "it-1"));
|
||||
when(streamService.getRecords(eq("it-2"), anyInt())).thenReturn(
|
||||
new DynamoDbStreamService.GetRecordsResult(
|
||||
List.of(ddbRecord("s2", "INSERT", "{\"status\":{\"S\":\"active\"}}")), "it-2"));
|
||||
|
||||
InvokeResult partialFailure = new InvokeResult();
|
||||
partialFailure.setStatusCode(200);
|
||||
partialFailure.setPayload("{\"batchItemFailures\":[{\"itemIdentifier\":\"s2\"}]}".getBytes());
|
||||
when(executorService.invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(partialFailure);
|
||||
|
||||
LambdaFunction fn = new LambdaFunction();
|
||||
fn.setFunctionName("fn");
|
||||
when(functionStore.getForAccount(ACCOUNT_ID, "us-east-1", "fn")).thenReturn(Optional.of(fn));
|
||||
|
||||
EsmStore store = mock(EsmStore.class);
|
||||
EventSourceMapping esm = filterEsm();
|
||||
esm.setFunctionResponseTypes(List.of("ReportBatchItemFailures"));
|
||||
esm.setMaximumRetryAttempts(1); // 1 retry allowed; 2nd failure exhausts
|
||||
|
||||
String sqsArn = "arn:aws:sqs:us-east-1:000000000000:my-dlq";
|
||||
EventSourceMapping.DestinationConfig destConfig = new EventSourceMapping.DestinationConfig();
|
||||
EventSourceMapping.OnFailure onFailure = new EventSourceMapping.OnFailure();
|
||||
onFailure.setDestination(sqsArn);
|
||||
destConfig.setOnFailure(onFailure);
|
||||
esm.setDestinationConfig(destConfig);
|
||||
|
||||
DynamoDbStreamsEventSourcePoller p = pollerWith(store);
|
||||
|
||||
// First poll: s1 succeeds, s2 fails -> checkpoint advances to s1, retry count carried over, no DLQ delivery
|
||||
p.pollAndInvoke(esm);
|
||||
verify(store, timeout(2000)).saveForAccount(eq(ACCOUNT_ID), any());
|
||||
assertEquals("s1", esm.getShardSequenceNumbers().get(DynamoDbStreamService.SHARD_ID));
|
||||
verify(sqsService, never()).sendMessage(anyString(), anyString(), anyInt(), anyString());
|
||||
|
||||
long deadline = System.currentTimeMillis() + 3000;
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
if (p.activePolls.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
Thread.sleep(25);
|
||||
}
|
||||
|
||||
// Second poll: fetches [s2], s2 fails again -> maxRetries=1 exhausts (attempt 2, retry 1)
|
||||
p.pollAndInvoke(esm);
|
||||
|
||||
verify(store, timeout(2000).times(2)).saveForAccount(eq(ACCOUNT_ID), any());
|
||||
assertEquals("s2", esm.getShardSequenceNumbers().get(DynamoDbStreamService.SHARD_ID));
|
||||
|
||||
ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
|
||||
String expectedQueueUrl = "http://localhost:4566/000000000000/my-dlq";
|
||||
verify(sqsService, timeout(2000)).sendMessage(eq(expectedQueueUrl), bodyCaptor.capture(), eq(0), eq("us-east-1"));
|
||||
|
||||
JsonNode dlqPayload = OBJECT_MAPPER.readTree(bodyCaptor.getValue());
|
||||
assertEquals("1.0", dlqPayload.path("version").asText());
|
||||
assertEquals("RetryAttemptsExhausted", dlqPayload.path("requestContext").path("condition").asText());
|
||||
assertEquals(2, dlqPayload.path("requestContext").path("approximateInvokeCount").asInt());
|
||||
assertEquals("s2", dlqPayload.path("DDBStreamBatchInfo").path("startSequenceNumber").asText());
|
||||
assertEquals("s2", dlqPayload.path("DDBStreamBatchInfo").path("endSequenceNumber").asText());
|
||||
assertEquals(1, dlqPayload.path("DDBStreamBatchInfo").path("batchSize").asInt());
|
||||
|
||||
verify(executorService, times(2)).invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse));
|
||||
}
|
||||
|
||||
private JsonNode readRecords(byte[] payload) {
|
||||
try {
|
||||
return OBJECT_MAPPER.readTree(payload).path("Records");
|
||||
|
||||
@@ -223,6 +223,7 @@ class EsmIntegrationTest {
|
||||
.body("""
|
||||
{
|
||||
"BisectBatchOnFunctionError": true,
|
||||
"MaximumRetryAttempts": 3,
|
||||
"DestinationConfig": {
|
||||
"OnFailure": {
|
||||
"Destination": "%s"
|
||||
@@ -235,6 +236,7 @@ class EsmIntegrationTest {
|
||||
.then()
|
||||
.statusCode(202)
|
||||
.body("BisectBatchOnFunctionError", equalTo(true))
|
||||
.body("MaximumRetryAttempts", equalTo(3))
|
||||
.body("DestinationConfig.OnFailure.Destination", equalTo(destinationArn));
|
||||
|
||||
given()
|
||||
@@ -243,6 +245,7 @@ class EsmIntegrationTest {
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("BisectBatchOnFunctionError", equalTo(true))
|
||||
.body("MaximumRetryAttempts", equalTo(3))
|
||||
.body("DestinationConfig.OnFailure.Destination", equalTo(destinationArn));
|
||||
|
||||
given()
|
||||
@@ -266,6 +269,7 @@ class EsmIntegrationTest {
|
||||
.then()
|
||||
.statusCode(202)
|
||||
.body("$", not(hasKey("BisectBatchOnFunctionError")))
|
||||
.body("$", not(hasKey("MaximumRetryAttempts")))
|
||||
.body("$", not(hasKey("DestinationConfig")))
|
||||
.extract()
|
||||
.path("UUID");
|
||||
|
||||
Reference in new issue
Block a user