fix(lambda): split a failed Kinesis batch with BisectBatchOnFunctionError (#6523)

* fix(lambda): split a failed Kinesis batch with BisectBatchOnFunctionError

* refactor(lambda): share the stream batch split rule between the pollers
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-10-25 14:13:47 -04:00
1 parent a4196c19e3
commit d1b9baeb4a
6 files changed
+142 -13

No files matched your search

@@ -705,6 +705,64 @@ class KinesisEventSourcePollerTest {
assertEquals(2, record.at("/requestContext/approximateInvokeCount").asInt());
}
/**
* "BisectBatchOnFunctionError, which splits a failed batch into two smaller batches, isolating bad records
* [...] Splitting batches doesn't consume the retry quota." With no retries left, only the bad record is
* discarded; the records around it are delivered.
*/
@Test
void bisectingSplitsAFailedBatchUntilOnlyTheBadRecordIsDiscarded() throws Exception {
stubShard("s1", "s2", "s3", "s4");
List<byte[]> events = failInvocationsContaining("s3");
EventSourceMapping esm = esmWithOnFailure(0, QUEUE_ARN);
esm.setBisectBatchOnFunctionError(true);
for (int poll = 0; poll < 5; poll++) {
pollOnce(esm);
}
assertEquals(List.of(List.of("s1", "s2", "s3", "s4"), List.of("s1", "s2"), List.of("s3", "s4"),
List.of("s3"), List.of("s4")), invokedSequences(events));
assertEquals("s4", checkpoint(esm));
ArgumentCaptor<String> body = ArgumentCaptor.forClass(String.class);
verify(sqsService, times(1)).sendMessage(eq(QUEUE_URL), body.capture(), eq(0), eq(REGION));
JsonNode record = MAPPER.readTree(body.getValue());
assertEquals("RetryAttemptsExhausted", record.at("/requestContext/condition").asText());
assertEquals(1, record.at("/requestContext/approximateInvokeCount").asInt());
assertBatchInfo(record, "s3", "s3", "2026-10-20T12:07:28.012Z", "2026-10-20T12:07:28.012Z", 1);
}
@Test
void aFailedBatchIsNotSplitWithoutBisectBatchOnFunctionError() throws Exception {
stubShard("s1", "s2", "s3", "s4");
List<byte[]> events = failInvocationsContaining("s3");
EventSourceMapping esm = esmWithOnFailure(0, QUEUE_ARN);
pollOnce(esm);
assertEquals(List.of(List.of("s1", "s2", "s3", "s4")), invokedSequences(events));
assertEquals("s4", checkpoint(esm), "the whole batch is discarded");
}
@Test
void aBatchOlderThanMaximumRecordAgeIsDiscardedRatherThanSplit() throws Exception {
stubShard("s1", "s2");
List<byte[]> events = failInvocationsContaining("s2");
EventSourceMapping esm = esmWithOnFailure(null, QUEUE_ARN);
esm.setBisectBatchOnFunctionError(true);
esm.setMaximumRecordAgeInSeconds(60);
clock.set(FIRST_ARRIVAL.plusSeconds(61).toEpochMilli());
pollOnce(esm);
assertEquals(1, events.size());
assertEquals("s2", checkpoint(esm));
ArgumentCaptor<String> body = ArgumentCaptor.forClass(String.class);
verify(sqsService, times(1)).sendMessage(eq(QUEUE_URL), body.capture(), eq(0), eq(REGION));
assertEquals("MaximumRecordAgeExceeded",
MAPPER.readTree(body.getValue()).at("/requestContext/condition").asText());
}
@Test
void failedBatchIsNotRetriedBeforeItsDoublingBackoffElapses() throws Exception {
stubShard("s1");
@@ -103,6 +103,36 @@ class StreamBatchFailuresTest {
assertTrue(StreamBatchFailures.maximumRecordAgeExceeded(esm(null, 60), arrival, aMinute));
}
@Test
void onlyAFunctionErrorOnMoreThanOneRecordSplitsAndOnlyWhenBisecting() {
EventSourceMapping bisecting = esm(null, null);
bisecting.setBisectBatchOnFunctionError(true);
InvokeResult functionError = new InvokeResult(200, "Unhandled", new byte[0], null, "req-1");
assertTrue(StreamBatchFailures.splitsOnFunctionError(bisecting, functionError, 2));
assertFalse(StreamBatchFailures.splitsOnFunctionError(bisecting, functionError, 1), "a single record");
assertFalse(StreamBatchFailures.splitsOnFunctionError(bisecting, new InvokeResult(), 2), "batchItemFailures");
assertFalse(StreamBatchFailures.splitsOnFunctionError(esm(null, null), functionError, 2), "unset");
}
@Test
void aSplitBatchIsRefetchedAtItsFirstHalfRoundedUp() {
assertEquals(2, StreamBatchFailures.splitBatchSize(4));
assertEquals(3, StreamBatchFailures.splitBatchSize(5));
assertEquals(1, StreamBatchFailures.splitBatchSize(2));
}
@Test
void theSplitSizeLimitsTheFetchOnlyWhileBisecting() {
Map<String, Integer> splitSizes = Map.of("esm-1:shard:42", 3);
EventSourceMapping bisecting = esm(null, null);
bisecting.setBisectBatchOnFunctionError(true);
assertEquals(3, StreamBatchFailures.bisectLimit(bisecting, splitSizes, "esm-1:shard:42"));
assertEquals(Integer.MAX_VALUE, StreamBatchFailures.bisectLimit(bisecting, splitSizes, "esm-1:shard:43"));
assertEquals(Integer.MAX_VALUE, StreamBatchFailures.bisectLimit(esm(null, null), splitSizes, "esm-1:shard:42"));
}
@Test
void sqsRecordFillsDefaultsWritesTheTimestampInMillisecondsAndDoesNotBuildTheEvent() throws Exception {
EventSourceMapping esm = esm(0, null);