From 33e12b3cf7c96fbe39946c6bfb35c368620698b4 Mon Sep 17 00:00:00 2001 From: "Ovidiu (Ovi)" <163712869+ovi-fabric@users.noreply.github.com> Date: Mon, 2 Nov 2026 15:36:25 +0200 Subject: [PATCH] fix(lambda): persist ParallelizationFactor on stream event source mappings (#7299) * fix(lambda): persist ParallelizationFactor on stream event source mappings CreateEventSourceMapping and UpdateEventSourceMapping dropped ParallelizationFactor, and the responses never returned it. A Terraform aws_lambda_event_source_mapping with parallelization_factor set therefore showed a change on every plan. The field is now validated (an integer from 1 to 10, Kinesis and DynamoDB Streams sources only), stored, and returned on every stream mapping, 1 when none was set, as AWS does. SQS mappings still omit it. The pollers still process one batch per shard at a time; the docs say so. * fix(lambda): keep ParallelizationFactor on a null update and reject values beyond long An UpdateEventSourceMapping with "ParallelizationFactor": null cleared the stored factor, so a mapping set to 10 read back as 1. A JSON null is an absent member, as FunctionResponseTypes already treats it, and now leaves the factor unchanged. The range check ran after narrowing to long, so 18446744073709551617 wrapped to 1 and was accepted. The value is now range-checked before it is narrowed. * fix(lambda): share one range check across the event source mapping integers MaximumBatchingWindowInSeconds, MaximumRetryAttempts, MaximumRecordAgeInSeconds, ScalingConfig.MaximumConcurrency and ParallelizationFactor each repeated the same numeric, whole-number and range check. One boundedInteger helper now serves all five, with the error messages unchanged. It checks the range before narrowing, so a value beyond long such as 18446744073709551617 no longer wraps to 1 and passes in the other four. --- docs/services/lambda.md | 26 +++ .../services/lambda/LambdaController.java | 6 + .../floci/services/lambda/LambdaService.java | 129 ++++++------- .../lambda/model/EventSourceMapping.java | 10 + .../services/lambda/EsmIntegrationTest.java | 181 ++++++++++++++++++ 5 files changed, 288 insertions(+), 64 deletions(-) diff --git a/docs/services/lambda.md b/docs/services/lambda.md index 1572c2254a..f3b5cd6a6f 100644 --- a/docs/services/lambda.md +++ b/docs/services/lambda.md @@ -1277,6 +1277,32 @@ use `ParallelizationFactor` instead, which is a separate field. at a time regardless). Real parallel dispatch capped by `MaximumConcurrency` is tracked as a follow-up. +### ParallelizationFactor (Kinesis and DynamoDB Streams only) + +`CreateEventSourceMapping` and `UpdateEventSourceMapping` accept a +`ParallelizationFactor` integer between 1 and 10 on Kinesis and DynamoDB +Streams event sources. `GetEventSourceMapping`, `ListEventSourceMappings` +and the create, update and delete responses return it on every stream +mapping, `1` when none was set, as AWS does. SQS mappings omit it. + +```bash +aws lambda create-event-source-mapping \ + --function-name my-function \ + --event-source-arn $STREAM_ARN \ + --starting-position LATEST \ + --parallelization-factor 4 \ + --endpoint-url $AWS_ENDPOINT_URL +``` + +Values outside 1 to 10 are rejected with `InvalidParameterValueException`, +and so is `ParallelizationFactor` on any other event source. + +!!! note "Enforcement status" + The configured `ParallelizationFactor` is persisted and returned on the + wire, but the Kinesis and DynamoDB Streams pollers still process one + batch per shard at a time. Concurrent batches per shard are tracked as + a follow-up. + ### FilterCriteria `CreateEventSourceMapping` and `UpdateEventSourceMapping` accept a diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java index a5100e5dc2..948c903b22 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java @@ -414,6 +414,12 @@ public class LambdaController { if (esm.getMaximumRecordAgeInSeconds() != null) { node.put("MaximumRecordAgeInSeconds", esm.getMaximumRecordAgeInSeconds()); } + // AWS returns ParallelizationFactor on every stream mapping, 1 when none was set. + if (esm.getParallelizationFactor() != null) { + node.put("ParallelizationFactor", esm.getParallelizationFactor()); + } else if (LambdaService.isStreamSource(esm.getEventSourceArn())) { + node.put("ParallelizationFactor", 1); + } if (esm.getDestinationConfig() != null && esm.getDestinationConfig().getOnFailure() != null) { ObjectNode destinationConfig = node.putObject("DestinationConfig"); diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java index 2ecd883ced..a96f5bca69 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java @@ -51,6 +51,7 @@ import jakarta.inject.Inject; import org.jboss.logging.Logger; import java.io.IOException; +import java.math.BigDecimal; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -1693,6 +1694,7 @@ public class LambdaService implements ResourceProvider { Integer maximumRetryAttempts = parseMaximumRetryAttempts(request); Integer maximumRecordAgeInSeconds = parseMaximumRecordAgeInSeconds(request); + Integer parallelizationFactor = parseParallelizationFactor(request, eventSourceArn); EventSourceMapping.DestinationConfig destinationConfig = parseDestinationConfig(request); @@ -1721,6 +1723,7 @@ public class LambdaService implements ResourceProvider { esm.setBisectBatchOnFunctionError(bisectBatchOnFunctionError); esm.setMaximumRetryAttempts(maximumRetryAttempts); esm.setMaximumRecordAgeInSeconds(maximumRecordAgeInSeconds); + esm.setParallelizationFactor(parallelizationFactor); esm.setDestinationConfig(destinationConfig); esm.setFilterCriteria(filterCriteria); esm.setStartingPosition(startingPosition.position()); @@ -2098,25 +2101,12 @@ public class LambdaService implements ResourceProvider { } return null; } - if (!(mc instanceof Number)) { - throw new AwsException("InvalidParameterValueException", - "ScalingConfig.MaximumConcurrency must be a numeric value", 400); - } - double d = ((Number) mc).doubleValue(); - if (Double.isNaN(d) || Double.isInfinite(d) || d != Math.floor(d)) { - throw new AwsException("InvalidParameterValueException", - "ScalingConfig.MaximumConcurrency must be an integer", 400); - } - long longValue = ((Number) mc).longValue(); - if (longValue < 2 || longValue > 1000) { - throw new AwsException("InvalidParameterValueException", - "ScalingConfig.MaximumConcurrency must be between 2 and 1000 (got " + longValue + ")", 400); - } + int maximumConcurrency = boundedInteger(mc, "ScalingConfig.MaximumConcurrency", 2, 1000); if (!isSqs) { throw new AwsException("InvalidParameterValueException", "ScalingConfig is only supported for Amazon SQS event source mappings", 400); } - return new ScalingConfig((int) longValue); + return new ScalingConfig(maximumConcurrency); } /** @@ -2125,69 +2115,75 @@ public class LambdaService implements ResourceProvider { * is absent so a create leaves it unset and an update leaves the stored value untouched. */ private Integer parseMaximumBatchingWindow(Map request) { - Object raw = request.get("MaximumBatchingWindowInSeconds"); - if (raw == null) { - return null; - } - if (!(raw instanceof Number)) { - throw new AwsException("InvalidParameterValueException", - "MaximumBatchingWindowInSeconds must be a numeric value", 400); - } - double d = ((Number) raw).doubleValue(); - if (Double.isNaN(d) || Double.isInfinite(d) || d != Math.floor(d)) { - throw new AwsException("InvalidParameterValueException", - "MaximumBatchingWindowInSeconds must be an integer", 400); - } - long value = ((Number) raw).longValue(); - if (value < 0 || value > 300) { - throw new AwsException("InvalidParameterValueException", - "MaximumBatchingWindowInSeconds must be between 0 and 300 (got " + value + ")", 400); - } - return (int) value; + return boundedInteger(request.get("MaximumBatchingWindowInSeconds"), "MaximumBatchingWindowInSeconds", 0, 300); } private Integer parseMaximumRetryAttempts(Map request) { - Object raw = request.get("MaximumRetryAttempts"); - if (raw == null) { - return null; - } - if (!(raw instanceof Number)) { - throw new AwsException("InvalidParameterValueException", - "MaximumRetryAttempts must be a numeric value", 400); - } - double d = ((Number) raw).doubleValue(); - if (Double.isNaN(d) || Double.isInfinite(d) || d != Math.floor(d)) { - throw new AwsException("InvalidParameterValueException", - "MaximumRetryAttempts must be an integer", 400); - } - long value = ((Number) raw).longValue(); - if (value < -1 || value > 10000) { - throw new AwsException("InvalidParameterValueException", - "MaximumRetryAttempts must be between -1 and 10000 (got " + value + ")", 400); - } - return (int) value; + return boundedInteger(request.get("MaximumRetryAttempts"), "MaximumRetryAttempts", -1, 10000); } + /** -1 (infinite) or 60 to 604800: the one member whose range has a gap. */ private Integer parseMaximumRecordAgeInSeconds(Map request) { Object raw = request.get("MaximumRecordAgeInSeconds"); + String range = "-1 or between 60 and 604800"; + Integer value = boundedInteger(raw, "MaximumRecordAgeInSeconds", -1, 604800, range); + if (value != null && value != -1 && value < 60) { + throw outOfRange("MaximumRecordAgeInSeconds", range, value); + } + return value; + } + + /** + * Reads an integer member of an event source mapping request: {@code null} when absent, otherwise a + * whole number from {@code minimum} to {@code maximum}, or {@code InvalidParameterValueException}. The + * range is checked on the value as sent, before it is narrowed, so a value beyond {@code long} cannot + * wrap into range. + */ + private static Integer boundedInteger(Object raw, String member, int minimum, int maximum) { + return boundedInteger(raw, member, minimum, maximum, "between " + minimum + " and " + maximum); + } + + private static Integer boundedInteger(Object raw, String member, int minimum, int maximum, String range) { if (raw == null) { return null; } - if (!(raw instanceof Number)) { - throw new AwsException("InvalidParameterValueException", - "MaximumRecordAgeInSeconds must be a numeric value", 400); + if (!(raw instanceof Number number)) { + throw new AwsException("InvalidParameterValueException", member + " must be a numeric value", 400); } - double d = ((Number) raw).doubleValue(); + double d = number.doubleValue(); if (Double.isNaN(d) || Double.isInfinite(d) || d != Math.floor(d)) { - throw new AwsException("InvalidParameterValueException", - "MaximumRecordAgeInSeconds must be an integer", 400); + throw new AwsException("InvalidParameterValueException", member + " must be an integer", 400); } - long value = ((Number) raw).longValue(); - if (value != -1 && (value < 60 || value > 604800)) { - throw new AwsException("InvalidParameterValueException", - "MaximumRecordAgeInSeconds must be -1 or between 60 and 604800 (got " + value + ")", 400); + if (d < minimum || d > maximum) { + throw outOfRange(member, range, number); } - return (int) value; + return number.intValue(); + } + + private static AwsException outOfRange(String member, String range, Number value) { + // Printed as a whole number whatever its JSON spelling: 301.0 reads as 301. + String got = new BigDecimal(value.toString()).toBigInteger().toString(); + return new AwsException("InvalidParameterValueException", + member + " must be " + range + " (got " + got + ")", 400); + } + + /** + * Parses {@code ParallelizationFactor}: an integer in [1, 10], accepted only on Kinesis and + * DynamoDB Streams sources. Returns {@code null} when the field is absent. + */ + private Integer parseParallelizationFactor(Map request, String eventSourceArn) { + Integer value = boundedInteger(request.get("ParallelizationFactor"), "ParallelizationFactor", 1, 10); + if (value != null && !isStreamSource(eventSourceArn)) { + throw new AwsException("InvalidParameterValueException", + "ParallelizationFactor is only supported for Amazon Kinesis and Amazon DynamoDB Streams event sources", + 400); + } + return value; + } + + static boolean isStreamSource(String eventSourceArn) { + return eventSourceArn != null + && (eventSourceArn.contains(":kinesis:") || eventSourceArn.contains(":dynamodb:")); } private void startPollingHelper(EventSourceMapping esm) { @@ -2306,6 +2302,11 @@ public class LambdaService implements ResourceProvider { Integer maximumRecordAge = parseMaximumRecordAgeInSeconds(request); changes.add(m -> m.setMaximumRecordAgeInSeconds(maximumRecordAge)); } + if (request.get("ParallelizationFactor") != null) { + // A JSON null is an absent member and leaves the stored factor unchanged. + Integer parallelizationFactor = parseParallelizationFactor(request, esm.getEventSourceArn()); + changes.add(m -> m.setParallelizationFactor(parallelizationFactor)); + } if (request.containsKey("DestinationConfig")) { EventSourceMapping.DestinationConfig destinationConfig = parseDestinationConfig(request); diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/model/EventSourceMapping.java b/src/main/java/io/github/hectorvent/floci/services/lambda/model/EventSourceMapping.java index 382c5b7022..f06f154e63 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/model/EventSourceMapping.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/model/EventSourceMapping.java @@ -32,6 +32,7 @@ public class EventSourceMapping { private Boolean bisectBatchOnFunctionError; private Integer maximumRetryAttempts; private Integer maximumRecordAgeInSeconds; + private Integer parallelizationFactor; private DestinationConfig destinationConfig; private FilterCriteria filterCriteria; private Map selfManagedEventSource; @@ -136,6 +137,15 @@ public class EventSourceMapping { this.maximumRecordAgeInSeconds = maximumRecordAgeInSeconds; } + /** Batches processed concurrently per shard (Kinesis and DynamoDB Streams); {@code null} means the AWS default of 1. */ + public Integer getParallelizationFactor() { + return parallelizationFactor; + } + + public void setParallelizationFactor(Integer parallelizationFactor) { + this.parallelizationFactor = parallelizationFactor; + } + public DestinationConfig getDestinationConfig() { return destinationConfig; } diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/EsmIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/EsmIntegrationTest.java index 0f0d17c910..4a02a4e45f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/EsmIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/EsmIntegrationTest.java @@ -264,6 +264,187 @@ class EsmIntegrationTest { .statusCode(202); } + @Test + void parallelizationFactorPersistsAcrossCreateGetListAndUpdate() { + String streamArn = "arn:aws:dynamodb:us-east-1:000000000000:table/esm-parallel/stream/2026-01-01T00:00:00.000"; + + String uuid = given() + .contentType("application/json") + .body(""" + { + "FunctionName": "%s", + "EventSourceArn": "%s", + "StartingPosition": "TRIM_HORIZON", + "ParallelizationFactor": 10, + "Enabled": false + } + """.formatted(FUNCTION_NAME, streamArn)) + .when() + .post(LAMBDA_BASE + "/event-source-mappings/") + .then() + .statusCode(202) + .body("ParallelizationFactor", equalTo(10)) + .extract() + .path("UUID"); + + try { + given() + .when() + .get(LAMBDA_BASE + "/event-source-mappings/" + uuid) + .then() + .statusCode(200) + .body("ParallelizationFactor", equalTo(10)); + + given() + .when() + .get(LAMBDA_BASE + "/event-source-mappings?EventSourceArn=" + streamArn) + .then() + .statusCode(200) + .body("EventSourceMappings.find { it.UUID == '%s' }.ParallelizationFactor".formatted(uuid), + equalTo(10)); + + given() + .contentType("application/json") + .body(""" + { "ParallelizationFactor": 4 } + """) + .when() + .put(LAMBDA_BASE + "/event-source-mappings/" + uuid) + .then() + .statusCode(202) + .body("ParallelizationFactor", equalTo(4)); + + given() + .when() + .get(LAMBDA_BASE + "/event-source-mappings/" + uuid) + .then() + .statusCode(200) + .body("ParallelizationFactor", equalTo(4)); + + given() + .contentType("application/json") + .body(""" + { "ParallelizationFactor": null, "BatchSize": 50 } + """) + .when() + .put(LAMBDA_BASE + "/event-source-mappings/" + uuid) + .then() + .statusCode(202) + .body("BatchSize", equalTo(50)) + .body("ParallelizationFactor", equalTo(4)); + } finally { + given().delete(LAMBDA_BASE + "/event-source-mappings/" + uuid).then().statusCode(202); + } + } + + @Test + void parallelizationFactorDefaultsToOneOnStreamMappingsOnly() { + String kinesisUuid = given() + .contentType("application/json") + .body(""" + { + "FunctionName": "%s", + "EventSourceArn": "%s", + "StartingPosition": "LATEST", + "Enabled": false + } + """.formatted(FUNCTION_NAME, STREAM_ARN)) + .when() + .post(LAMBDA_BASE + "/event-source-mappings/") + .then() + .statusCode(202) + .body("ParallelizationFactor", equalTo(1)) + .extract() + .path("UUID"); + given().delete(LAMBDA_BASE + "/event-source-mappings/" + kinesisUuid).then().statusCode(202); + + String sqsUuid = given() + .contentType("application/json") + .body(""" + { + "FunctionName": "%s", + "EventSourceArn": "%s", + "Enabled": false + } + """.formatted(FUNCTION_NAME, QUEUE_ARN)) + .when() + .post(LAMBDA_BASE + "/event-source-mappings/") + .then() + .statusCode(202) + .body("$", not(hasKey("ParallelizationFactor"))) + .extract() + .path("UUID"); + given().delete(LAMBDA_BASE + "/event-source-mappings/" + sqsUuid).then().statusCode(202); + } + + @Test + void createEventSourceMappingRejectsParallelizationFactorOutOfRange() { + for (String factor : List.of("0", "11", "2.5", "18446744073709551617")) { + given() + .contentType("application/json") + .body(""" + { + "FunctionName": "%s", + "EventSourceArn": "%s", + "StartingPosition": "LATEST", + "ParallelizationFactor": %s, + "Enabled": false + } + """.formatted(FUNCTION_NAME, STREAM_ARN, factor)) + .when() + .post(LAMBDA_BASE + "/event-source-mappings") + .then() + .statusCode(400) + .body("__type", equalTo("InvalidParameterValueException")); + } + } + + /** 2^64 + 1 is a whole number that wraps to 1 when narrowed to long, so each range check must see it first. */ + @Test + void createEventSourceMappingRejectsIntegerOptionsBeyondLong() { + String beyondLong = "18446744073709551617"; + List cases = List.of( + new String[] {QUEUE_ARN, "\"MaximumBatchingWindowInSeconds\": " + beyondLong}, + new String[] {QUEUE_ARN, "\"ScalingConfig\": {\"MaximumConcurrency\": " + beyondLong + "}"}, + new String[] {STREAM_ARN, "\"StartingPosition\": \"LATEST\", \"MaximumRetryAttempts\": " + beyondLong}, + new String[] {STREAM_ARN, "\"StartingPosition\": \"LATEST\", \"MaximumRecordAgeInSeconds\": " + beyondLong}); + for (String[] c : cases) { + given() + .contentType("application/json") + .body(""" + { + "FunctionName": "%s", + "EventSourceArn": "%s", + "Enabled": false, + %s + } + """.formatted(FUNCTION_NAME, c[0], c[1])) + .when() + .post(LAMBDA_BASE + "/event-source-mappings") + .then() + .statusCode(400) + .body("message", containsString("(got " + beyondLong + ")")); + } + } + + @Test + void createEventSourceMappingRejectsParallelizationFactorOnSqsSource() { + given() + .contentType("application/json") + .body(""" + { + "FunctionName": "%s", + "EventSourceArn": "%s", + "ParallelizationFactor": 2 + } + """.formatted(FUNCTION_NAME, QUEUE_ARN)) + .when() + .post(LAMBDA_BASE + "/event-source-mappings") + .then() + .statusCode(400) + .body("message", containsString("only supported for Amazon Kinesis and Amazon DynamoDB Streams")); + } + @Test void createLatestDynamoDbMappingStartsAfterTheNewestRecord() { String tableName = "esm-latest-" + UUID.randomUUID();