diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaConcurrencyTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaConcurrencyTest.java new file mode 100644 index 0000000000..32a4831ce3 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaConcurrencyTest.java @@ -0,0 +1,138 @@ +package com.floci.test; + +import org.junit.jupiter.api.*; +import software.amazon.awssdk.core.SdkBytes; +import software.amazon.awssdk.services.lambda.LambdaClient; +import software.amazon.awssdk.services.lambda.model.*; +import software.amazon.awssdk.services.lambda.model.Runtime; + +import static org.assertj.core.api.Assertions.*; + +@DisplayName("Lambda - Function concurrency") +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class LambdaConcurrencyTest { + + private static final String FUNCTION_NAME = "sdk-test-concurrency-fn"; + private static final String ROLE = "arn:aws:iam::000000000000:role/lambda-role"; + + private static LambdaClient lambda; + + @BeforeAll + static void setup() { + lambda = TestFixtures.lambdaClient(); + lambda.createFunction(CreateFunctionRequest.builder() + .functionName(FUNCTION_NAME) + .runtime(Runtime.NODEJS20_X) + .role(ROLE) + .handler("index.handler") + .code(FunctionCode.builder() + .zipFile(SdkBytes.fromByteArray(LambdaUtils.minimalZip())) + .build()) + .build()); + } + + @AfterAll + static void cleanup() { + if (lambda != null) { + try { + lambda.deleteFunction(DeleteFunctionRequest.builder() + .functionName(FUNCTION_NAME).build()); + } catch (Exception ignored) {} + lambda.close(); + } + } + + @Test + @Order(1) + void getFunctionConcurrency_unset_returnsEmpty() { + GetFunctionConcurrencyResponse response = lambda.getFunctionConcurrency( + GetFunctionConcurrencyRequest.builder() + .functionName(FUNCTION_NAME) + .build()); + + assertThat(response.reservedConcurrentExecutions()).isNull(); + } + + @Test + @Order(2) + void putFunctionConcurrency_setsAndReturnsValue() { + PutFunctionConcurrencyResponse response = lambda.putFunctionConcurrency( + PutFunctionConcurrencyRequest.builder() + .functionName(FUNCTION_NAME) + .reservedConcurrentExecutions(5) + .build()); + + assertThat(response.reservedConcurrentExecutions()).isEqualTo(5); + } + + @Test + @Order(3) + void getFunctionConcurrency_afterPut_returnsValue() { + GetFunctionConcurrencyResponse response = lambda.getFunctionConcurrency( + GetFunctionConcurrencyRequest.builder() + .functionName(FUNCTION_NAME) + .build()); + + assertThat(response.reservedConcurrentExecutions()).isEqualTo(5); + } + + @Test + @Order(4) + void putFunctionConcurrency_updatesExistingValue() { + PutFunctionConcurrencyResponse response = lambda.putFunctionConcurrency( + PutFunctionConcurrencyRequest.builder() + .functionName(FUNCTION_NAME) + .reservedConcurrentExecutions(10) + .build()); + + assertThat(response.reservedConcurrentExecutions()).isEqualTo(10); + } + + @Test + @Order(5) + void putFunctionConcurrency_zeroIsAllowed() { + PutFunctionConcurrencyResponse response = lambda.putFunctionConcurrency( + PutFunctionConcurrencyRequest.builder() + .functionName(FUNCTION_NAME) + .reservedConcurrentExecutions(0) + .build()); + + assertThat(response.reservedConcurrentExecutions()).isEqualTo(0); + } + + @Test + @Order(6) + void deleteFunctionConcurrency_clearsValue() { + lambda.deleteFunctionConcurrency(DeleteFunctionConcurrencyRequest.builder() + .functionName(FUNCTION_NAME) + .build()); + + GetFunctionConcurrencyResponse response = lambda.getFunctionConcurrency( + GetFunctionConcurrencyRequest.builder() + .functionName(FUNCTION_NAME) + .build()); + + assertThat(response.reservedConcurrentExecutions()).isNull(); + } + + @Test + @Order(7) + void putFunctionConcurrency_unknownFunction_throws404() { + assertThatThrownBy(() -> lambda.putFunctionConcurrency( + PutFunctionConcurrencyRequest.builder() + .functionName("does-not-exist") + .reservedConcurrentExecutions(5) + .build())) + .isInstanceOf(ResourceNotFoundException.class); + } + + @Test + @Order(8) + void getFunctionConcurrency_unknownFunction_throws404() { + assertThatThrownBy(() -> lambda.getFunctionConcurrency( + GetFunctionConcurrencyRequest.builder() + .functionName("does-not-exist") + .build())) + .isInstanceOf(ResourceNotFoundException.class); + } +} diff --git a/docs/services/lambda.md b/docs/services/lambda.md index 990a6ad9af..8ff55d5493 100644 --- a/docs/services/lambda.md +++ b/docs/services/lambda.md @@ -39,6 +39,14 @@ Lambda runs your function code inside real Docker containers — the same way re | `ListTags` | List tags on a function | | `TagResource` | Tag a function | | `UntagResource` | Untag a function | +| `PutFunctionConcurrency` | Set reserved concurrent executions (stub — value stored but not enforced) | +| `GetFunctionConcurrency` | Get reserved concurrent executions | +| `DeleteFunctionConcurrency` | Clear reserved concurrent executions | + +!!! note "Concurrency is a stub" + `PutFunctionConcurrency` persists `ReservedConcurrentExecutions` on the function + and echoes it back, but Floci does not enforce the limit at invocation time and + does not validate against the account-level concurrent execution limit. Function URLs are also reachable directly on `/{proxy:.*}` under the Lambda URL controller, which routes the request into the normal `Invoke` path. @@ -49,7 +57,7 @@ Function URLs are also reachable directly on `/{proxy:.*}` under the Lambda URL These AWS Lambda operations have no handler in Floci. Calls will return `404` or an error: - Layers (`PublishLayerVersion`, `DeleteLayerVersion`, `GetLayerVersion`, `GetLayerVersionByArn`, `AddLayerVersionPermission`, `RemoveLayerVersionPermission`, `GetLayerVersionPolicy`) -- Concurrency controls (`PutFunctionConcurrency`, `GetFunctionConcurrency`, `DeleteFunctionConcurrency`, `PutProvisionedConcurrencyConfig`, `GetProvisionedConcurrencyConfig`, `ListProvisionedConcurrencyConfigs`, `DeleteProvisionedConcurrencyConfig`) +- Provisioned concurrency (`PutProvisionedConcurrencyConfig`, `GetProvisionedConcurrencyConfig`, `ListProvisionedConcurrencyConfigs`, `DeleteProvisionedConcurrencyConfig`) - `UpdateFunctionConfiguration` (use `UpdateFunctionCode` for code-only updates; configuration-only updates are not separately supported) - Dead-letter, async invoke config, and event invoke config operations - `InvokeWithResponseStream` diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaConcurrencyController.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaConcurrencyController.java new file mode 100644 index 0000000000..76d3964d5c --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaConcurrencyController.java @@ -0,0 +1,114 @@ +package io.github.hectorvent.floci.services.lambda; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.services.lambda.model.LambdaFunction; +import jakarta.inject.Inject; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.DELETE; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.PUT; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.Context; +import jakarta.ws.rs.core.HttpHeaders; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; + +import java.util.Map; + +/** + * Lambda reserved concurrency endpoints. These span two API version prefixes: + * + * PutFunctionConcurrency: PUT /2017-10-31/functions/{FunctionName}/concurrency + * DeleteFunctionConcurrency: DELETE /2017-10-31/functions/{FunctionName}/concurrency + * GetFunctionConcurrency: GET /2019-09-30/functions/{FunctionName}/concurrency + * + * The class-level {@code @Path("/")} lets each method declare its own absolute + * version prefix rather than inheriting a single one. + * + * The stored value is not enforced at invocation time — this is a stub so that + * tools and SDKs expecting the endpoint to exist can proceed. + */ +@Path("/") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class LambdaConcurrencyController { + + private final LambdaService lambdaService; + private final RegionResolver regionResolver; + private final ObjectMapper objectMapper; + + @Inject + public LambdaConcurrencyController(LambdaService lambdaService, + RegionResolver regionResolver, + ObjectMapper objectMapper) { + this.lambdaService = lambdaService; + this.regionResolver = regionResolver; + this.objectMapper = objectMapper; + } + + @PUT + @Path("/2017-10-31/functions/{functionName}/concurrency") + public Response putFunctionConcurrency(@Context HttpHeaders headers, + @PathParam("functionName") String functionName, + String body) { + String region = regionResolver.resolveRegion(headers); + try { + @SuppressWarnings("unchecked") + Map request = objectMapper.readValue(body, Map.class); + if (!request.containsKey("ReservedConcurrentExecutions") + || request.get("ReservedConcurrentExecutions") == null) { + throw new AwsException("InvalidParameterValueException", + "ReservedConcurrentExecutions is required", 400); + } + Object raw = request.get("ReservedConcurrentExecutions"); + // Jackson parses JSON integer literals as Integer or Long. Anything else + // (Double, BigDecimal, String, etc.) must be rejected — AWS does not + // silently truncate 1.5 to 1. + if (!(raw instanceof Integer) && !(raw instanceof Long)) { + throw new AwsException("InvalidParameterValueException", + "ReservedConcurrentExecutions must be an integer", 400); + } + long longValue = ((Number) raw).longValue(); + if (longValue < Integer.MIN_VALUE || longValue > Integer.MAX_VALUE) { + throw new AwsException("InvalidParameterValueException", + "ReservedConcurrentExecutions is out of range", 400); + } + LambdaFunction fn = lambdaService.putFunctionConcurrency( + region, functionName, (int) longValue); + ObjectNode root = objectMapper.createObjectNode(); + root.put("ReservedConcurrentExecutions", fn.getReservedConcurrentExecutions()); + return Response.ok(root).build(); + } catch (AwsException e) { + throw e; + } catch (Exception e) { + throw new AwsException("InvalidParameterValueException", e.getMessage(), 400); + } + } + + @GET + @Path("/2019-09-30/functions/{functionName}/concurrency") + public Response getFunctionConcurrency(@Context HttpHeaders headers, + @PathParam("functionName") String functionName) { + String region = regionResolver.resolveRegion(headers); + Integer reserved = lambdaService.getFunctionConcurrency(region, functionName); + ObjectNode root = objectMapper.createObjectNode(); + if (reserved != null) { + root.put("ReservedConcurrentExecutions", reserved); + } + return Response.ok(root).build(); + } + + @DELETE + @Path("/2017-10-31/functions/{functionName}/concurrency") + public Response deleteFunctionConcurrency(@Context HttpHeaders headers, + @PathParam("functionName") String functionName) { + String region = regionResolver.resolveRegion(headers); + lambdaService.deleteFunctionConcurrency(region, functionName); + return Response.noContent().build(); + } +} 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 1d96a8851b..5205a2acd8 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 @@ -583,6 +583,28 @@ public class LambdaService { } } + public LambdaFunction putFunctionConcurrency(String region, String functionName, Integer reservedConcurrentExecutions) { + if (reservedConcurrentExecutions == null || reservedConcurrentExecutions < 0) { + throw new AwsException("InvalidParameterValueException", + "ReservedConcurrentExecutions must be a non-negative integer", 400); + } + LambdaFunction fn = getFunction(region, functionName); + fn.setReservedConcurrentExecutions(reservedConcurrentExecutions); + functionStore.save(region, fn); + return fn; + } + + public Integer getFunctionConcurrency(String region, String functionName) { + LambdaFunction fn = getFunction(region, functionName); + return fn.getReservedConcurrentExecutions(); + } + + public void deleteFunctionConcurrency(String region, String functionName) { + LambdaFunction fn = getFunction(region, functionName); + fn.setReservedConcurrentExecutions(null); + functionStore.save(region, fn); + } + public LambdaFunction getFunctionByUrlId(String urlId) { return functionStore.getByUrlId(urlId) .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Function not found for URL ID: " + urlId, 404)); diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/model/LambdaFunction.java b/src/main/java/io/github/hectorvent/floci/services/lambda/model/LambdaFunction.java index ecb63b51b0..29a55da155 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/model/LambdaFunction.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/model/LambdaFunction.java @@ -35,6 +35,7 @@ public class LambdaFunction { private String revisionId; private String version = "$LATEST"; private LambdaUrlConfig urlConfig; + private Integer reservedConcurrentExecutions; @JsonIgnore private volatile ContainerState containerState = ContainerState.COLD; @@ -108,6 +109,9 @@ public class LambdaFunction { public LambdaUrlConfig getUrlConfig() { return urlConfig; } public void setUrlConfig(LambdaUrlConfig urlConfig) { this.urlConfig = urlConfig; } + public Integer getReservedConcurrentExecutions() { return reservedConcurrentExecutions; } + public void setReservedConcurrentExecutions(Integer reservedConcurrentExecutions) { this.reservedConcurrentExecutions = reservedConcurrentExecutions; } + @JsonIgnore public ContainerState getContainerState() { return containerState; } @JsonIgnore