mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(lambda): honor MaximumBatchingWindowInSeconds on SQS event source mappings (#3203)
* fix(lambda): honor MaximumBatchingWindowInSeconds on SQS event source mappings CreateEventSourceMapping, GetEventSourceMapping and UpdateEventSourceMapping now parse, validate (0 to 300), persist and return MaximumBatchingWindowInSeconds instead of silently dropping it. When the window is greater than 0 the SQS poller holds an underfilled batch open, accumulating messages across polls, and invokes the function once the batch reaches BatchSize or the window elapses since the first buffered message. A window of 0 or unset keeps the previous invoke-on-first-receive behavior. Closes #3155 * fix(lambda): cover the batching window in SQS message visibility and window-off transitions Address review feedback on the batching-window poller: - Visibility timeout is now fn.timeout + window + 30 instead of the max of the two, so a message buffered for the full window and then processed for the full function timeout stays invisible for the whole span. Otherwise a long invoke could outlive the visibility and another consumer would receive a duplicate with a stale receipt handle. - When the window is changed from positive to zero (or unset) on a live mapping, the poller now drains and delivers any already-buffered batch on the next tick instead of ignoring it, so those messages are not stranded until their visibility expires and cannot be replayed alongside fresh copies if a positive window is later restored.
This commit is contained in:
1 parent
53a84c8890
commit
ec2d678e6d
8 files changed
+350
-5
No files matched your search
@@ -140,6 +140,62 @@ class EsmIntegrationTest {
|
||||
given().delete(LAMBDA_BASE + "/event-source-mappings/" + uuid).then().statusCode(202);
|
||||
}
|
||||
|
||||
@Test
|
||||
void maximumBatchingWindowRoundTripsThroughCreateGetAndUpdate() {
|
||||
String uuid = given()
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{
|
||||
"FunctionName": "%s",
|
||||
"EventSourceArn": "%s",
|
||||
"BatchSize": 2,
|
||||
"MaximumBatchingWindowInSeconds": 5
|
||||
}
|
||||
""".formatted(FUNCTION_NAME, QUEUE_ARN))
|
||||
.when()
|
||||
.post(LAMBDA_BASE + "/event-source-mappings")
|
||||
.then()
|
||||
.statusCode(202)
|
||||
.body("MaximumBatchingWindowInSeconds", equalTo(5))
|
||||
.extract()
|
||||
.path("UUID");
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get(LAMBDA_BASE + "/event-source-mappings/" + uuid)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("MaximumBatchingWindowInSeconds", equalTo(5));
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body("{\"MaximumBatchingWindowInSeconds\": 0}")
|
||||
.when()
|
||||
.put(LAMBDA_BASE + "/event-source-mappings/" + uuid)
|
||||
.then()
|
||||
.statusCode(202)
|
||||
.body("MaximumBatchingWindowInSeconds", equalTo(0));
|
||||
|
||||
given().delete(LAMBDA_BASE + "/event-source-mappings/" + uuid).then().statusCode(202);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createEventSourceMappingRejectsMaximumBatchingWindowAbove300() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{
|
||||
"FunctionName": "%s",
|
||||
"EventSourceArn": "%s",
|
||||
"MaximumBatchingWindowInSeconds": 301
|
||||
}
|
||||
""".formatted(FUNCTION_NAME, QUEUE_ARN))
|
||||
.when()
|
||||
.post(LAMBDA_BASE + "/event-source-mappings")
|
||||
.then()
|
||||
.statusCode(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateEventSourceMappingReturnsFailureConfig() {
|
||||
String streamArn = "arn:aws:dynamodb:us-east-1:000000000000:table/esm-table/stream/2026-01-01T00:00:00.000";
|
||||
|
||||
+32
@@ -96,6 +96,38 @@ class EventSourceMappingSerializationTest {
|
||||
assertNotNull(response.get("SourceAccessConfigurations"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void maximumBatchingWindowRoundTripsThroughJacksonAndIsOmittedWhenUnset() throws Exception {
|
||||
EventSourceMapping esm = new EventSourceMapping();
|
||||
esm.setUuid("esm-window-001");
|
||||
esm.setMaximumBatchingWindowInSeconds(5);
|
||||
|
||||
EventSourceMapping deserialized = objectMapper.readValue(
|
||||
objectMapper.writeValueAsString(esm), EventSourceMapping.class);
|
||||
assertEquals(5, deserialized.getMaximumBatchingWindowInSeconds());
|
||||
|
||||
assertNull(new EventSourceMapping().getMaximumBatchingWindowInSeconds());
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildEsmResponseEmitsMaximumBatchingWindowOnlyWhenSet() {
|
||||
EventSourceMapping esm = new EventSourceMapping();
|
||||
esm.setUuid("esm-window-002");
|
||||
esm.setFunctionArn("arn:aws:lambda:us-east-1:123456789012:function:sqsConsumer");
|
||||
esm.setEventSourceArn("arn:aws:sqs:us-east-1:123456789012:my-queue");
|
||||
esm.setBatchSize(2);
|
||||
esm.setState("Enabled");
|
||||
esm.setLastModified(1700000000000L);
|
||||
|
||||
assertFalse(controller.buildEsmResponse(esm).containsKey("MaximumBatchingWindowInSeconds"));
|
||||
|
||||
esm.setMaximumBatchingWindowInSeconds(5);
|
||||
assertEquals(5, controller.buildEsmResponse(esm).get("MaximumBatchingWindowInSeconds"));
|
||||
|
||||
esm.setMaximumBatchingWindowInSeconds(0);
|
||||
assertEquals(0, controller.buildEsmResponse(esm).get("MaximumBatchingWindowInSeconds"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBuildEsmResponseWithEventSourceArnPresent() {
|
||||
EventSourceMapping esm = new EventSourceMapping();
|
||||
|
||||
+129
@@ -21,6 +21,7 @@ import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -473,6 +474,134 @@ class SqsEventSourcePollerTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static void sleep(long millis) {
|
||||
try {
|
||||
Thread.sleep(millis);
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
private void pollUntilInvoked(EventSourceMapping esm, LambdaFunction fn) {
|
||||
long deadline = System.currentTimeMillis() + 5000;
|
||||
while (true) {
|
||||
poller.pollAndInvoke(esm);
|
||||
try {
|
||||
verify(executorService, timeout(2000))
|
||||
.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse));
|
||||
return;
|
||||
} catch (AssertionError retry) {
|
||||
if (System.currentTimeMillis() > deadline) {
|
||||
throw retry;
|
||||
}
|
||||
sleep(25);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void batchingWindowHoldsUnderfilledBatchUntilTheWindowElapses() {
|
||||
LambdaFunction fn = stubThrowFn();
|
||||
EventSourceMapping esm = esm();
|
||||
esm.setBatchSize(2);
|
||||
esm.setMaximumBatchingWindowInSeconds(5);
|
||||
|
||||
AtomicLong now = new AtomicLong(0);
|
||||
poller.setClockForTest(now::get);
|
||||
|
||||
when(sqsService.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
|
||||
.thenReturn(List.of(message("m1")))
|
||||
.thenReturn(List.of());
|
||||
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(new InvokeResult());
|
||||
|
||||
// Polls inside the window buffer the record without invoking.
|
||||
for (int i = 0; i < 4; i++) {
|
||||
poller.pollAndInvoke(esm);
|
||||
sleep(25);
|
||||
now.addAndGet(1000); // reaches 4s, short of the 5s window
|
||||
}
|
||||
verify(sqsService, atLeast(1))
|
||||
.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1"));
|
||||
verify(executorService, never()).invoke(any(), any(), any());
|
||||
verify(sqsService, never()).deleteMessage(any(), any(), any());
|
||||
|
||||
// Once the window elapses the buffered record is delivered.
|
||||
now.set(5000);
|
||||
pollUntilInvoked(esm, fn);
|
||||
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void batchingWindowFlushesEarlyOnceTheBatchIsFull() {
|
||||
LambdaFunction fn = stubThrowFn();
|
||||
EventSourceMapping esm = esm();
|
||||
esm.setBatchSize(2);
|
||||
esm.setMaximumBatchingWindowInSeconds(30);
|
||||
|
||||
poller.setClockForTest(() -> 0L); // the window never expires on its own
|
||||
|
||||
when(sqsService.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
|
||||
.thenReturn(List.of(message("m1")))
|
||||
.thenReturn(List.of(message("m2")))
|
||||
.thenReturn(List.of());
|
||||
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(new InvokeResult());
|
||||
|
||||
pollUntilInvoked(esm, fn);
|
||||
|
||||
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
|
||||
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m2", "us-east-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void batchingWindowVisibilityTimeoutCoversTheWindowPlusFunctionTimeout() {
|
||||
EventSourceMapping esm = esm();
|
||||
esm.setMaximumBatchingWindowInSeconds(60);
|
||||
LambdaFunction fn = new LambdaFunction();
|
||||
fn.setFunctionName("throwfn");
|
||||
fn.setTimeout(120);
|
||||
when(functionStore.getForAccount("000000000000", "us-east-1", "throwfn"))
|
||||
.thenReturn(Optional.of(fn));
|
||||
poller.setClockForTest(() -> 0L);
|
||||
when(sqsService.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
|
||||
.thenReturn(List.of());
|
||||
|
||||
poller.pollAndInvoke(esm);
|
||||
|
||||
ArgumentCaptor<Integer> visibility = ArgumentCaptor.forClass(Integer.class);
|
||||
verify(sqsService, timeout(2000)).receiveMessage(
|
||||
eq(esm.getQueueUrl()), anyInt(), visibility.capture(), anyInt(), eq("us-east-1"));
|
||||
// held up to 60s buffered, then up to 120s executing, plus AWS's 30s margin
|
||||
assertEquals(120 + 60 + 30, visibility.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void turningTheBatchingWindowOffDeliversAnAlreadyBufferedBatch() {
|
||||
LambdaFunction fn = stubThrowFn();
|
||||
EventSourceMapping esm = esm();
|
||||
esm.setBatchSize(10);
|
||||
esm.setMaximumBatchingWindowInSeconds(300);
|
||||
poller.setClockForTest(() -> 0L); // the window never elapses on its own
|
||||
|
||||
when(sqsService.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
|
||||
.thenReturn(List.of(message("m1")))
|
||||
.thenReturn(List.of());
|
||||
when(executorService.invoke(eq(fn), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(new InvokeResult());
|
||||
|
||||
// Poll once with the window open: the record is buffered, not delivered.
|
||||
poller.pollAndInvoke(esm);
|
||||
verify(sqsService, timeout(2000))
|
||||
.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1"));
|
||||
verify(executorService, never()).invoke(any(), any(), any());
|
||||
|
||||
// Window turned off: the next poll must flush the stranded batch instead of ignoring it.
|
||||
esm.setMaximumBatchingWindowInSeconds(0);
|
||||
pollUntilInvoked(esm, fn);
|
||||
verify(sqsService, timeout(2000)).deleteMessage(esm.getQueueUrl(), "rh-m1", "us-east-1");
|
||||
}
|
||||
|
||||
private static final String BODY_PATTERN = "{\"body\":{\"type\":[\"order\"]}}";
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user