fix(lambda): apply FunctionResponseTypes on UpdateEventSourceMapping (#4584)

* fix(lambda): apply FunctionResponseTypes on UpdateEventSourceMapping

UpdateEventSourceMapping ignored FunctionResponseTypes, so turning
ReportBatchItemFailures on or off for an existing mapping succeeded but
changed nothing: GetEventSourceMapping kept the old list and the pollers
kept retrying whole batches. Apply the member when present, sharing the
create path's parsing; an empty list clears it.

* fix(lambda): validate FunctionResponseTypes on event source mappings

AWS accepts at most one FunctionResponseTypes entry, and only
ReportBatchItemFailures. Reject anything else with
InvalidParameterValueException on both CreateEventSourceMapping and
UpdateEventSourceMapping instead of storing it.

* fix(lambda): validate every UpdateEventSourceMapping member before applying any

A request that failed on a later member (FunctionName in another region,
empty Topics) still changed the members parsed before it on the stored
mapping, which the pollers share. A JSON null FunctionResponseTypes is now
an absent member, and a non-array value is a SerializationException rather
than silently clearing ReportBatchItemFailures.

* fix(lambda): honour ReportBatchItemFailures on Kinesis event source mappings

The Kinesis poller checkpointed the whole batch after a successful invoke,
so records the function reported in batchItemFailures were never retried.
It now checkpoints just before the lowest reported record and retries the
whole batch on a malformed response, matching the DynamoDB Streams poller.
This commit is contained in:
Abhigya authored and GitHub committed 2026-09-29 08:00:57 -04:00
1 parent 1d646c6fea
commit e8b768382a
6 files changed
+386 -22

No files matched your search

@@ -1661,4 +1661,80 @@ class EsmIntegrationTest {
given().delete(LAMBDA_BASE + "/event-source-mappings/" + uuid).then().statusCode(202);
}
@Test
@Order(88)
void updateEventSourceMappingSetsAndClearsFunctionResponseTypes() {
String uuid = given()
.contentType("application/json")
.body("""
{ "FunctionName": "%s", "EventSourceArn": "%s", "BatchSize": 2 }
""".formatted(FUNCTION_NAME, QUEUE_ARN))
.when().post(LAMBDA_BASE + "/event-source-mappings")
.then().statusCode(202)
.body("FunctionResponseTypes", empty())
.extract().path("UUID");
given().contentType("application/json")
.body("{ \"FunctionResponseTypes\": [\"ReportBatchItemFailures\"] }")
.when().put(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then().statusCode(202)
.body("FunctionResponseTypes", contains("ReportBatchItemFailures"));
given()
.when().get(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then().statusCode(200)
.body("FunctionResponseTypes", contains("ReportBatchItemFailures"));
// An update that omits the member leaves it unchanged
given().contentType("application/json").body("{ \"BatchSize\": 3 }")
.when().put(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then().statusCode(202)
.body("FunctionResponseTypes", contains("ReportBatchItemFailures"));
given().contentType("application/json").body("{ \"FunctionResponseTypes\": [\"Bogus\"] }")
.when().put(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then().statusCode(400)
.body("__type", equalTo("InvalidParameterValueException"));
given()
.when().get(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then().statusCode(200)
.body("FunctionResponseTypes", contains("ReportBatchItemFailures"));
// A null member is absent, and a scalar is a serialization error that changes nothing
given().contentType("application/json").body("{ \"FunctionResponseTypes\": null }")
.when().put(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then().statusCode(202)
.body("FunctionResponseTypes", contains("ReportBatchItemFailures"));
given().contentType("application/json").body("{ \"FunctionResponseTypes\": \"ReportBatchItemFailures\" }")
.when().put(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then().statusCode(400)
.body("__type", equalTo("SerializationException"));
// A request that fails on a later member applies none of its members
given().contentType("application/json")
.body("{ \"FunctionResponseTypes\": [], \"BatchSize\": 5, \"Topics\": [] }")
.when().put(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then().statusCode(400);
given()
.when().get(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then().statusCode(200)
.body("FunctionResponseTypes", contains("ReportBatchItemFailures"))
.body("BatchSize", equalTo(3));
given().contentType("application/json").body("{ \"FunctionResponseTypes\": [] }")
.when().put(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then().statusCode(202)
.body("FunctionResponseTypes", empty());
given()
.when().get(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then().statusCode(200)
.body("FunctionResponseTypes", empty());
given().delete(LAMBDA_BASE + "/event-source-mappings/" + uuid).then().statusCode(202);
}
}
@@ -349,4 +349,90 @@ class KinesisEventSourcePollerTest {
verify(esmStore, never()).saveForAccount(eq(ACCOUNT), any());
assertNull(esm.getShardSequenceNumbers().get(SHARD));
}
private static InvokeResult batchItemFailures(String json) {
InvokeResult result = new InvokeResult();
result.setPayload(json.getBytes(StandardCharsets.UTF_8));
return result;
}
private EventSourceMapping reportingEsm() {
EventSourceMapping esm = esm();
esm.setFunctionResponseTypes(List.of("ReportBatchItemFailures"));
return esm;
}
@Test
void reportedFailureCheckpointsBeforeTheLowestFailedRecord() {
stubFunction();
stubStreamWith(List.of(record("s1", "p1", "{}"), record("s2", "p2", "{}"), record("s3", "p3", "{}")));
when(executorService.invoke(any(), any(), eq(InvocationType.RequestResponse))).thenReturn(
batchItemFailures("{\"batchItemFailures\":[{\"itemIdentifier\":\"s3\"},{\"itemIdentifier\":\"s2\"}]}"));
EventSourceMapping esm = reportingEsm();
poller.pollAndInvoke(esm);
verify(esmStore, timeout(2000)).saveForAccount(eq(ACCOUNT), any());
assertEquals("s1", esm.getShardSequenceNumbers().get(SHARD));
}
@Test
void reportedFailureOnTheFirstRecordRetriesTheWholeBatch() {
stubFunction();
stubStreamWith(List.of(record("s1", "p1", "{}"), record("s2", "p2", "{}")));
when(executorService.invoke(any(), any(), eq(InvocationType.RequestResponse))).thenReturn(
batchItemFailures("{\"batchItemFailures\":[{\"itemIdentifier\":\"s1\"}]}"));
EventSourceMapping esm = reportingEsm();
poller.pollAndInvoke(esm);
verify(executorService, timeout(2000)).invoke(any(), any(), eq(InvocationType.RequestResponse));
awaitPollCompletedViaSecondFetch(esm);
verify(esmStore, never()).saveForAccount(eq(ACCOUNT), any());
assertNull(esm.getShardSequenceNumbers().get(SHARD));
}
@Test
void unknownItemIdentifierRetriesTheWholeBatch() {
stubFunction();
stubStreamWith(List.of(record("s1", "p1", "{}"), record("s2", "p2", "{}")));
when(executorService.invoke(any(), any(), eq(InvocationType.RequestResponse))).thenReturn(
batchItemFailures("{\"batchItemFailures\":[{\"itemIdentifier\":\"nope\"}]}"));
EventSourceMapping esm = reportingEsm();
poller.pollAndInvoke(esm);
verify(executorService, timeout(2000)).invoke(any(), any(), eq(InvocationType.RequestResponse));
awaitPollCompletedViaSecondFetch(esm);
verify(esmStore, never()).saveForAccount(eq(ACCOUNT), any());
assertNull(esm.getShardSequenceNumbers().get(SHARD));
}
@Test
void emptyBatchItemFailuresCheckpointsTheWholeBatch() {
stubFunction();
stubStreamWith(List.of(record("s1", "p1", "{}"), record("s2", "p2", "{}")));
when(executorService.invoke(any(), any(), eq(InvocationType.RequestResponse)))
.thenReturn(batchItemFailures("{\"batchItemFailures\":[]}"));
EventSourceMapping esm = reportingEsm();
poller.pollAndInvoke(esm);
verify(esmStore, timeout(2000)).saveForAccount(eq(ACCOUNT), any());
assertEquals("s2", esm.getShardSequenceNumbers().get(SHARD));
}
@Test
void batchItemFailuresAreIgnoredWithoutReportBatchItemFailures() {
stubFunction();
stubStreamWith(List.of(record("s1", "p1", "{}"), record("s2", "p2", "{}")));
when(executorService.invoke(any(), any(), eq(InvocationType.RequestResponse))).thenReturn(
batchItemFailures("{\"batchItemFailures\":[{\"itemIdentifier\":\"s1\"}]}"));
EventSourceMapping esm = esm();
poller.pollAndInvoke(esm);
verify(esmStore, timeout(2000)).saveForAccount(eq(ACCOUNT), any());
assertEquals("s2", esm.getShardSequenceNumbers().get(SHARD));
}
}
@@ -0,0 +1,75 @@
package io.github.hectorvent.floci.services.lambda;
import io.github.hectorvent.floci.core.common.AwsException;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class LambdaEsmFunctionResponseTypesValidationTest {
private static Map<String, Object> request(Object functionResponseTypes) {
Map<String, Object> req = new HashMap<>();
req.put("FunctionResponseTypes", functionResponseTypes);
return req;
}
private static AwsException assertRejected(Object functionResponseTypes) {
AwsException ex = assertThrows(AwsException.class,
() -> LambdaService.parseFunctionResponseTypes(request(functionResponseTypes)));
assertEquals("InvalidParameterValueException", ex.getErrorCode());
assertEquals(400, ex.getHttpStatus());
return ex;
}
@Test
void reportBatchItemFailuresIsAccepted() {
assertEquals(List.of("ReportBatchItemFailures"),
LambdaService.parseFunctionResponseTypes(request(List.of("ReportBatchItemFailures"))));
}
@Test
void emptyListIsAccepted() {
assertEquals(List.of(), LambdaService.parseFunctionResponseTypes(request(List.of())));
}
@Test
void absentMemberParsesAsEmpty() {
assertEquals(List.of(), LambdaService.parseFunctionResponseTypes(new HashMap<>()));
}
@Test
void nullMemberParsesAsEmpty() {
assertEquals(List.of(), LambdaService.parseFunctionResponseTypes(request(null)));
}
@Test
void scalarMemberIsASerializationError() {
AwsException ex = assertThrows(AwsException.class,
() -> LambdaService.parseFunctionResponseTypes(request("ReportBatchItemFailures")));
assertEquals("SerializationException", ex.getErrorCode());
assertEquals(400, ex.getHttpStatus());
}
@Test
void unknownValueIsRejected() {
AwsException ex = assertRejected(List.of("ReportItemFailures"));
assertTrue(ex.getMessage().contains("enum value set: [ReportBatchItemFailures]"), ex.getMessage());
}
@Test
void nonStringValueIsRejected() {
assertRejected(List.of(1));
}
@Test
void moreThanOneItemIsRejected() {
AwsException ex = assertRejected(List.of("ReportBatchItemFailures", "ReportBatchItemFailures"));
assertTrue(ex.getMessage().contains("length less than or equal to 1"), ex.getMessage());
}
}