Light, fluffy, and always free - The AWS Local Emulator alternative
feat(lambda): add provisioned concurrency configuration APIs (#6317)
Andre Kurait6bc598fbfddf
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaProvisionedConcurrencyTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaProvisionedConcurrencyTest.javanew file mode 100644index 0000000000..9fc8ffe92b--- /dev/null+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaProvisionedConcurrencyTest.java@@ -0,0 +1,163 @@+package com.floci.test;++import org.junit.jupiter.api.AfterEach;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.DisplayName;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.ValueSource;+import software.amazon.awssdk.core.SdkBytes;+import software.amazon.awssdk.services.lambda.LambdaClient;+import software.amazon.awssdk.services.lambda.model.FunctionCode;+import software.amazon.awssdk.services.lambda.model.GetAliasResponse;+import software.amazon.awssdk.services.lambda.model.GetProvisionedConcurrencyConfigResponse;+import software.amazon.awssdk.services.lambda.model.InvalidParameterValueException;+import software.amazon.awssdk.services.lambda.model.ProvisionedConcurrencyConfigNotFoundException;+import software.amazon.awssdk.services.lambda.model.PutProvisionedConcurrencyConfigResponse;+import software.amazon.awssdk.services.lambda.model.ResourceConflictException;+import software.amazon.awssdk.services.lambda.model.ResourceNotFoundException;+import software.amazon.awssdk.services.lambda.model.Runtime;++import java.util.UUID;++import static org.assertj.core.api.Assertions.assertThat;+import static org.assertj.core.api.Assertions.assertThatThrownBy;++@DisplayName("Lambda provisioned concurrency control plane")+class LambdaProvisionedConcurrencyTest {+ private LambdaClient lambda;+ private String name;+ private String arn;+ private String version;++ @BeforeEach+ void createFunctionAndAlias() {+ lambda = TestFixtures.lambdaClient();+ name = "sdk-pc-" + UUID.randomUUID();+ arn = lambda.createFunction(request -> request.functionName(name).runtime(Runtime.NODEJS20_X)+ .role(TestFixtures.globalArn("iam", "000000000000", "role/lambda-role")).handler("index.handler")+ .code(FunctionCode.builder().zipFile(SdkBytes.fromByteArray(LambdaUtils.minimalZip())).build()))+ .functionArn();+ version = lambda.publishVersion(request -> request.functionName(name)).version();+ lambda.createAlias(request -> request.functionName(name).name("live").functionVersion(version));+ }++ @AfterEach+ void deleteFunction() {+ if (lambda != null) {+ try {+ lambda.deleteFunction(request -> request.functionName(name));+ } catch (ResourceNotFoundException ignored) {+ // The deletion test already removed this test-owned function.+ } finally {+ lambda.close();+ }+ }+ }++ @Test+ @DisplayName("Put/Get/Delete deserialize through the Java SDK")+ void configurationRoundTrip() {+ PutProvisionedConcurrencyConfigResponse put = lambda.putProvisionedConcurrencyConfig(request -> request+ .functionName(arn).qualifier("live").provisionedConcurrentExecutions(1));+ assertThat(put.sdkHttpResponse().statusCode()).isEqualTo(202);+ assertThat(put.requestedProvisionedConcurrentExecutions()).isEqualTo(1);+ assertThat(put.lastModified()).isNotBlank();+ GetProvisionedConcurrencyConfigResponse get = lambda.getProvisionedConcurrencyConfig(request -> request+ .functionName(name).qualifier("live"));+ assertThat(get.requestedProvisionedConcurrentExecutions()).isEqualTo(1);+ assertThat(get.lastModified()).isEqualTo(put.lastModified());+ assertThatThrownBy(() -> lambda.putProvisionedConcurrencyConfig(request -> request+ .functionName(name).qualifier(version).provisionedConcurrentExecutions(1)))+ .isInstanceOf(ResourceConflictException.class);+ assertThat(lambda.deleteProvisionedConcurrencyConfig(request -> request.functionName(name).qualifier("live"))+ .sdkHttpResponse().statusCode()).isEqualTo(204);+ assertThatThrownBy(() -> lambda.getProvisionedConcurrencyConfig(request -> request+ .functionName(name).qualifier("live"))).isInstanceOf(ProvisionedConcurrencyConfigNotFoundException.class);+ lambda.deleteProvisionedConcurrencyConfig(request -> request.functionName(name).qualifier("live"));+ }++ @Test+ @DisplayName("Deleting and recreating an alias does not restore its configuration")+ void aliasDeletionRemovesConfiguration() {+ lambda.putProvisionedConcurrencyConfig(request -> request.functionName(name).qualifier("live")+ .provisionedConcurrentExecutions(1));+ lambda.deleteAlias(request -> request.functionName(name).name("live"));+ lambda.createAlias(request -> request.functionName(name).name("live").functionVersion(version));+ assertThatThrownBy(() -> lambda.getProvisionedConcurrencyConfig(request -> request+ .functionName(name).qualifier("live"))).isInstanceOf(ProvisionedConcurrencyConfigNotFoundException.class);+ lambda.putProvisionedConcurrencyConfig(request -> request.functionName(name).qualifier(version)+ .provisionedConcurrentExecutions(1));+ }++ @Test+ @DisplayName("Version deletion releases its requested capacity")+ void versionDeletionReleasesCapacity() {+ lambda.deleteAlias(request -> request.functionName(name).name("live"));+ lambda.putProvisionedConcurrencyConfig(request -> request.functionName(name).qualifier(version)+ .provisionedConcurrentExecutions(1));+ lambda.deleteFunction(request -> request.functionName(name).qualifier(version));+ assertThatThrownBy(() -> lambda.getProvisionedConcurrencyConfig(request -> request+ .functionName(name).qualifier(version))).isInstanceOf(ResourceNotFoundException.class);+ lambda.putFunctionConcurrency(request -> request.functionName(name).reservedConcurrentExecutions(0));+ }++ @Test+ @DisplayName("Reserved-concurrency rejection preserves both configurations")+ void rejectedReservationPreservesState() {+ lambda.putFunctionConcurrency(request -> request.functionName(name).reservedConcurrentExecutions(2));+ lambda.putProvisionedConcurrencyConfig(request -> request.functionName(name).qualifier("live")+ .provisionedConcurrentExecutions(2));+ assertThatThrownBy(() -> lambda.putFunctionConcurrency(request -> request.functionName(name)+ .reservedConcurrentExecutions(1))).isInstanceOf(InvalidParameterValueException.class);+ assertThat(lambda.getFunctionConcurrency(request -> request.functionName(name)).reservedConcurrentExecutions())+ .isEqualTo(2);+ assertThat(lambda.getProvisionedConcurrencyConfig(request -> request.functionName(name).qualifier("live"))+ .requestedProvisionedConcurrentExecutions()).isEqualTo(2);+ }++ @Test+ @DisplayName("Function deletion cleans configuration before name reuse")+ void functionDeletionRemovesConfiguration() {+ lambda.putProvisionedConcurrencyConfig(request -> request.functionName(name).qualifier("live")+ .provisionedConcurrentExecutions(1));+ lambda.deleteFunction(request -> request.functionName(name));+ lambda.createFunction(request -> request.functionName(name).runtime(Runtime.NODEJS20_X)+ .role(TestFixtures.globalArn("iam", "000000000000", "role/lambda-role")).handler("index.handler")+ .code(FunctionCode.builder().zipFile(SdkBytes.fromByteArray(LambdaUtils.minimalZip())).build()));+ String replacement = lambda.publishVersion(request -> request.functionName(name)).version();+ lambda.createAlias(request -> request.functionName(name).name("live").functionVersion(replacement));+ assertThatThrownBy(() -> lambda.getProvisionedConcurrencyConfig(request -> request+ .functionName(name).qualifier("live"))).isInstanceOf(ProvisionedConcurrencyConfigNotFoundException.class);+ }+ @ParameterizedTest+ @ValueSource(booleans = {false, true})+ @DisplayName("Alias retarget validates destination capacity before mutating alias fields")+ void aliasRetargetRejectsConfiguredDestination(boolean otherAlias) {+ lambda.updateFunctionConfiguration(request -> request.functionName(name).description("second version"));+ String second = lambda.publishVersion(request -> request.functionName(name)).version();+ lambda.createAlias(request -> request.functionName(name).name("peer").functionVersion(second));+ String destination = otherAlias ? "peer" : second;+ lambda.putProvisionedConcurrencyConfig(request -> request.functionName(name).qualifier("live")+ .provisionedConcurrentExecutions(1));+ lambda.putProvisionedConcurrencyConfig(request -> request.functionName(name).qualifier(destination)+ .provisionedConcurrentExecutions(1));+ GetAliasResponse before = lambda.getAlias(request -> request.functionName(name).name("live"));+ assertThatThrownBy(() -> lambda.updateAlias(request -> request.functionName(name).name("live")+ .functionVersion(second).description("must not persist")))+ .isInstanceOf(ResourceConflictException.class)+ .hasMessageContaining("Alias can't be used for Provisioned Concurrency configuration on an already Provisioned version");+ GetAliasResponse after = lambda.getAlias(request -> request.functionName(name).name("live"));+ assertThat(after.functionVersion()).isEqualTo(before.functionVersion());+ assertThat(after.description()).isEqualTo(before.description());+ assertThat(after.revisionId()).isEqualTo(before.revisionId());+ lambda.updateAlias(request -> request.functionName(name).name("live").description("description only"));+ lambda.createAlias(request -> request.functionName(name).name("unconfigured").functionVersion(version));+ lambda.updateAlias(request -> request.functionName(name).name("unconfigured").functionVersion(second));+ lambda.deleteProvisionedConcurrencyConfig(request -> request.functionName(name).qualifier(destination));+ lambda.updateAlias(request -> request.functionName(name).name("live").functionVersion(second));+ assertThat(lambda.getProvisionedConcurrencyConfig(request -> request.functionName(name).qualifier("live"))+ .requestedProvisionedConcurrentExecutions()).isEqualTo(1);+ }++}diff --git a/docs/services/lambda.md b/docs/services/lambda.mdindex b652f000e4..6715093f42 100644--- a/docs/services/lambda.md+++ b/docs/services/lambda.md@@ -5,6 +5,36 @@Floci Lambda runs your function code locally inside real Docker containers - close enough as AWS Lambda does (using Firecracker micro VM).+## Provisioned concurrency configuration++`PutProvisionedConcurrencyConfig`, `GetProvisionedConcurrencyConfig`, and+`DeleteProvisionedConcurrencyConfig` use the AWS REST JSON route+`/2019-09-30/functions/{FunctionName}/provisioned-concurrency?Qualifier=...`.+They accept published versions and aliases pointing to published versions.+Function names, partial ARNs, and full ARNs resolve in the named account and request region.+IAM checks use the qualified function resource and the operation's own action.++This stage implements the control plane only. Put returns HTTP 202 and stores the desired+count. Get reports `IN_PROGRESS` with zero allocated and available environments. No runtime+is initialized, no handler is invoked, and ordinary invocation routing is unchanged. Runtime+initialization and routing are separate follow-up work; `READY` is not simulated.++Requested counts share the existing `warm-pool-max-per-function` and `warm-pool-max-total`+local limits, and cannot exceed the function's reserved concurrency. These local limits are+not AWS account-quota emulation. A version and an alias targeting it cannot both have a+configuration. Weighted aliases, hot-reload functions, and durable functions are not eligible+for Put. Put requires the non-ephemeral Docker executor. Get and Delete still address an+existing configuration if an alias later becomes+weighted. An alias with a configuration cannot move to `$LATEST` or to a version already+configured directly or through another alias. Description-only changes and unconfigured aliases+remain allowed.++Deleting a version, alias, or function removes its corresponding configurations. Delete is+idempotent for an existing target without a configuration; an absent target returns+`ResourceNotFoundException`. Desired configuration uses the Lambda storage mode through+`StorageFactory` and is restored after restart. Reset removes it. Storage failures propagate+before the in-memory configuration is replaced or removed, so callers can retry.+## Supported Operations| Operation | Description |@@ -49,6 +79,9 @@ Floci Lambda runs your function code locally inside real Docker containers - clo| `PutFunctionConcurrency` | Set reserved concurrent executions || `GetFunctionConcurrency` | Get reserved concurrent executions || `DeleteFunctionConcurrency` | Clear reserved concurrent executions |+| `PutProvisionedConcurrencyConfig` | Store requested capacity for a version or alias |+| `GetProvisionedConcurrencyConfig` | Read requested capacity and configuration status |+| `DeleteProvisionedConcurrencyConfig` | Remove requested capacity || `GetAccountSettings` | Account limits plus usage derived from the caller's stored functions || `PutFunctionEventInvokeConfig` | Set the asynchronous invocation settings of a function, version or alias (retries, event age, destinations) || `UpdateFunctionEventInvokeConfig` | Change some of those settings, leaving the rest as they are |@@ -90,16 +123,18 @@ part in authorizing calls to the function:- `GetFunction`, `GetFunctionConfiguration` and `ListDurableExecutionsByFunction`, given a full ARNwith a version or alias, read the function in the account in the ARN, and need its functionpolicy to allow the caller in the same way.+- Provisioned-concurrency configuration operations also act in the account named by the function+ reference. Cross-account access requires that function's policy to allow the caller.- Other operations act on the caller's own function of the name the ARN gives, whatever accountthe ARN names, and are authorized against that function. Floci does not serve them on anotheraccount's function, which AWS allows through a function policy.- Within one account, a statement naming the caller grants on its own. One naming only theaccount, as `AddPermission --principal <account-id>` writes it, delegates to the account, so thecaller's identity policy has to allow the call too.-- An invoke is authorized against the version or alias it names, in the function name or in- `Qualifier`. A grant added with `Qualifier` covers that version or alias only, and a grant on the- function does not cover a qualified invoke. Other operations are authorized against the- unqualified function.+- An invoke or provisioned-concurrency configuration operation is authorized against the version or+ alias it names, in the function name or in `Qualifier`. A grant added with `Qualifier` covers that+ version or alias only, and a grant on the function does not cover a qualified operation.+ Other operations are authorized against the unqualified function.- A statement conditioned on `InvokedViaFunctionUrl` does not admit a direct `Invoke`.- `aws:PrincipalOrgID` is not part of the request context, so a grant added with `PrincipalOrgID`admits no caller.@@ -370,8 +405,8 @@ disabled fails with Lambda's error as the resource status reason.applies independently to each region. `PutFunctionConcurrency` validatesthat the requested value leaves at least`floci.services.lambda.unreserved-concurrency-min` (default 100) available- for unreserved functions in that region. `PutProvisionedConcurrencyConfig`- and related provisioned-concurrency operations remain unimplemented.+ for unreserved functions in that region. Provisioned-concurrency requests+ must fit within any reserved concurrency configured for the function.Reducing or clearing a function's reserved value does not killinvocations that are already in flight. This matches AWS, which@@ -473,7 +508,7 @@ For a longer drain, explicitly stop with the larger timeout and then start FlociThese AWS Lambda operations have no handler in Floci. Calls will return `404` or an error:- Layer permissions (`AddLayerVersionPermission`, `RemoveLayerVersionPermission`, `GetLayerVersionPolicy`)-- Provisioned concurrency (`PutProvisionedConcurrencyConfig`, `GetProvisionedConcurrencyConfig`, `ListProvisionedConcurrencyConfigs`, `DeleteProvisionedConcurrencyConfig`)+- `ListProvisionedConcurrencyConfigs`- `InvokeWithResponseStream`- Code signing enforcement. A configuration is created, read, updated, deleted and listed, andnothing verifies a signature against it, so it never gates a deployment. Attaching one to adiff --git a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.javaindex 6f4c531f7c..825cff0bda 100644--- a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java+++ b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java@@ -754,10 +754,11 @@ public class ResourceArnBuilder {// path: /2015-03-31/functions/name or similarString name = extractSegmentAfter(path, "functions");if (name == null) return "*";- boolean invoke = path.endsWith("/functions/" + name + "/invocations")- || path.endsWith("/functions/" + name + "/response-streaming-invocations");+ boolean qualifiedTarget = path.endsWith("/functions/" + name + "/invocations")+ || path.endsWith("/functions/" + name + "/response-streaming-invocations")+ || path.endsWith("/functions/" + name + "/provisioned-concurrency");try {- if (!invoke) {+ if (!qualifiedTarget) {LambdaArnUtils.ResolvedFunctionRef ref = LambdaArnUtils.resolve(name);boolean namedAccount = readsInTheAccountItNames(path, name, ref, ctx);return AwsArnUtils.Arn.of("lambda",diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaFunctionStore.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaFunctionStore.javaindex aeeb79b0f0..f41227dae1 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaFunctionStore.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaFunctionStore.java@@ -25,6 +25,8 @@ public class LambdaFunctionStore implements Resettable {private final StorageBackend<String, LambdaFunction> backend;private final ConcurrentHashMap<String, LambdaFunction> urlIdIndex = new ConcurrentHashMap<>();+ // Retain locks across deletion and reset so a queued operation cannot race a newly created lock.+ private final ConcurrentHashMap<String, Object> concurrencyOpLocks = new ConcurrentHashMap<>();@Injectpublic LambdaFunctionStore(StorageFactory storageFactory) {@@ -39,6 +41,10 @@ public class LambdaFunctionStore implements Resettable {loadIndex();}+ Object lockForConcurrencyOp(String functionArn) {+ return concurrencyOpLocks.computeIfAbsent(functionArn, key -> new Object());+ }+private void loadIndex() {List<LambdaFunction> all = backend instanceof AccountAwareStorageBackend<LambdaFunction> aware? aware.scanAllAccounts()diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaProvisionedConcurrencyController.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaProvisionedConcurrencyController.javanew file mode 100644index 0000000000..9bd46141e1--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaProvisionedConcurrencyController.java@@ -0,0 +1,95 @@+package io.github.hectorvent.floci.services.lambda;++import com.fasterxml.jackson.databind.JsonNode;+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 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.QueryParam;+import jakarta.ws.rs.core.Context;+import jakarta.ws.rs.core.HttpHeaders;+import jakarta.ws.rs.core.MediaType;+import jakarta.ws.rs.core.Response;++@Path("/2019-09-30/functions/{functionName}/provisioned-concurrency")+@Produces(MediaType.APPLICATION_JSON)+@Consumes(MediaType.APPLICATION_JSON)+public class LambdaProvisionedConcurrencyController {+ private final ProvisionedConcurrencyPool pool;+ private final RegionResolver regions;+ private final ObjectMapper mapper;++ @Inject+ public LambdaProvisionedConcurrencyController(ProvisionedConcurrencyPool pool,+ RegionResolver regions, ObjectMapper mapper) {+ this.pool = pool;+ this.regions = regions;+ this.mapper = mapper;+ }++ @PUT+ public Response put(@Context HttpHeaders headers, @PathParam("functionName") String name,+ @QueryParam("Qualifier") String qualifier, String body) {+ int count;+ try {+ JsonNode request = mapper.readTree(body);+ JsonNode raw = request == null ? null : request.get("ProvisionedConcurrentExecutions");+ if (raw == null || raw.isNull()) {+ throw ProvisionedConcurrencyPool.invalidCount(null);+ }+ if (raw.isTextual() || raw.isBoolean()) {+ String type = raw.isTextual() ? "STRING_VALUE" : raw.booleanValue() ? "TRUE_VALUE" : "FALSE_VALUE";+ throw new AwsException("SerializationException", type + " can not be converted to an Integer", 400);+ }+ if (!raw.isNumber() || !raw.canConvertToInt()) {+ throw new IllegalArgumentException();+ }+ count = raw.intValue();+ if (count < 1) {+ throw ProvisionedConcurrencyPool.invalidCount(count);+ }+ } catch (AwsException failure) {+ throw failure;+ } catch (Exception failure) {+ throw new AwsException("InvalidParameterValueException",+ "ProvisionedConcurrentExecutions must be a positive integer", 400);+ }+ return Response.status(202).entity(wire(pool.put(regions.resolveRegion(headers), name,+ qualifier, count, regions.getAccountId()))).build();+ }++ @GET+ public Response get(@Context HttpHeaders headers, @PathParam("functionName") String name,+ @QueryParam("Qualifier") String qualifier) {+ return Response.ok(wire(pool.get(regions.resolveRegion(headers), name, qualifier,+ regions.getAccountId()))).build();+ }++ @DELETE+ public Response delete(@Context HttpHeaders headers, @PathParam("functionName") String name,+ @QueryParam("Qualifier") String qualifier) {+ pool.delete(regions.resolveRegion(headers), name, qualifier, regions.getAccountId());+ return Response.noContent().build();+ }++ private ObjectNode wire(ProvisionedConcurrencyPool.Status value) {+ ObjectNode result = mapper.createObjectNode();+ result.put("RequestedProvisionedConcurrentExecutions", value.requested());+ result.put("AllocatedProvisionedConcurrentExecutions", value.allocated());+ result.put("AvailableProvisionedConcurrentExecutions", value.available());+ result.put("Status", value.status());+ result.put("LastModified", value.lastModified());+ if (value.reason() != null) {+ result.put("StatusReason", value.reason());+ }+ return result;+ }+}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.javaindex e2b1577c6f..ee27bfc767 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@@ -190,20 +190,6 @@ public class LambdaService implements ResourceProvider {private final ObjectMapper objectMapper;private Map<String, Integer> versionCounters = new ConcurrentHashMap<>();private Map<String, FunctionEventInvokeConfig> eventInvokeConfigs = new ConcurrentHashMap<>();- /**- * Per-function locks covering PutFunctionConcurrency,- * DeleteFunctionConcurrency, and deleteFunction itself. Serializing the- * limiter update + persistence pair against itself for a given function- * prevents the limiter and store from diverging on interleaved concurrent- * requests.- *- * <p>Entries are intentionally never removed — see {@code deleteFunction}- * for the race this avoids. The map therefore grows by one {@code Object}- * per distinct function ARN the emulator has ever seen (create/delete- * cycles with fresh names included). Acceptable footprint for a local- * emulator workload.- */- private final ConcurrentHashMap<String, Object> concurrencyOpLocks = new ConcurrentHashMap<>();private final ConcurrentHashMap<String, Object> policyMutationLocks = new ConcurrentHashMap<>();private final ConcurrentHashMap<String, Object> versionCounterLocks = new ConcurrentHashMap<>();@@ -1088,6 +1074,7 @@ public class LambdaService implements ResourceProvider {return;}synchronized (lockForConcurrencyOp(fn.getFunctionArn())) {+ warmPool.deleteProvisionedQualifier(fn, qualifier);warmPool.drainEnvironment(version.get());functionStore.deleteVersion(region, name, qualifier);reclaimVersionCodeDirectory(region, fn, qualifier, version.get());@@ -1116,6 +1103,7 @@ public class LambdaService implements ResourceProvider {// object, letting a follow-up request allocate a fresh lock and run// in parallel — the very serialization this map exists to prevent.synchronized (lockForConcurrencyOp(arn)) {+ warmPool.deleteProvisionedFunction(fn);if (concurrencyLimiter != null) {concurrencyLimiter.reset(arn);}@@ -2253,7 +2241,7 @@ public class LambdaService implements ResourceProvider {* Locked get-increment-put: StorageBackedMap has no atomic merge, so the* sequence must not interleave or concurrent PublishVersion calls could issue* duplicate version numbers. The lock is per counter key (same pattern as- * {@code concurrencyOpLocks}) so publishes of unrelated functions do not+ * the per-function concurrency lock) so publishes of unrelated functions do not* serialize on the instance monitor for the storage round-trip.*//**@@ -2993,7 +2981,10 @@ public class LambdaService implements ResourceProvider {"Version must be a published version number, got: " + version, 400);}LambdaFunction fn = getFunction(region, functionName); // throws 404 if not found- functionStore.deleteVersion(region, fn.getFunctionName(), version);+ synchronized (lockForConcurrencyOp(fn.getFunctionArn())) {+ warmPool.deleteProvisionedQualifier(fn, version);+ functionStore.deleteVersion(region, fn.getFunctionName(), version);+ }LOG.infov("Deleted version {0} of function {1}", version, fn.getFunctionName());}@@ -3046,14 +3037,18 @@ public class LambdaService implements ResourceProvider {java.util.Map<String, Double> routingConfig) {validateAliasName(aliasName);validateAliasMembers(functionVersion, description);- LambdaAlias alias = getAlias(region, functionName, aliasName);- if (functionVersion != null) alias.setFunctionVersion(functionVersion);- if (description != null) alias.setDescription(description);- if (routingConfig != null) alias.setRoutingConfig(routingConfig.isEmpty() ? null : routingConfig);- alias.setLastModifiedDate(System.currentTimeMillis() / 1000L);- alias.setRevisionId(UUID.randomUUID().toString());- if (aliasStore != null) aliasStore.save(region, alias);- return alias;+ LambdaFunction fn = getFunction(region, functionName);+ synchronized (lockForConcurrencyOp(fn.getFunctionArn())) {+ LambdaAlias alias = getAlias(region, functionName, aliasName);+ warmPool.validateProvisionedAliasUpdate(fn, aliasName, functionVersion);+ if (functionVersion != null) alias.setFunctionVersion(functionVersion);+ if (description != null) alias.setDescription(description);+ if (routingConfig != null) alias.setRoutingConfig(routingConfig.isEmpty() ? null : routingConfig);+ alias.setLastModifiedDate(System.currentTimeMillis() / 1000L);+ alias.setRevisionId(UUID.randomUUID().toString());+ if (aliasStore != null) aliasStore.save(region, alias);+ return alias;+ }}/** FunctionUrlQualifier in botocore: 1-128 characters, not all digits. */@@ -3079,8 +3074,12 @@ public class LambdaService implements ResourceProvider {public void deleteAlias(String region, String functionName, String aliasName) {validatePattern(aliasName, "name", ALIAS_NAME_PATTERN);String canonical = canonicalFunctionName(region, functionName);- getAlias(region, canonical, aliasName); // verify it exists- if (aliasStore != null) aliasStore.delete(region, canonical, aliasName);+ LambdaFunction fn = getFunction(region, canonical);+ synchronized (lockForConcurrencyOp(fn.getFunctionArn())) {+ getAlias(region, canonical, aliasName); // verify it exists+ warmPool.deleteProvisionedQualifier(fn, aliasName);+ if (aliasStore != null) aliasStore.delete(region, canonical, aliasName);+ }LOG.infov("Deleted alias {0} for function {1}", aliasName, canonical);}@@ -3283,6 +3282,7 @@ public class LambdaService implements ResourceProvider {// concurrent Puts cannot leave the limiter and persisted state out of// sync, regardless of which call acquires the reservedLock first.synchronized (lockForConcurrencyOp(arn)) {+ warmPool.validateProvisionedReservation(fn, reservedConcurrentExecutions);Integer previousReserved = null;boolean limiterUpdated = false;if (concurrencyLimiter != null) {@@ -3359,7 +3359,7 @@ public class LambdaService implements ResourceProvider {/** Package-private (not private) so tests can hold this lock to prove a critical section waits for it. */Object lockForConcurrencyOp(String functionArn) {- return concurrencyOpLocks.computeIfAbsent(functionArn, k -> new Object());+ return functionStore.lockForConcurrencyOp(functionArn);}public LambdaFunction getFunctionByUrlId(String urlId) {diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/ProvisionedConcurrencyPool.java b/src/main/java/io/github/hectorvent/floci/services/lambda/ProvisionedConcurrencyPool.javanew file mode 100644index 0000000000..17fce92b29--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/ProvisionedConcurrencyPool.java@@ -0,0 +1,352 @@+package io.github.hectorvent.floci.services.lambda;++import com.fasterxml.jackson.core.type.TypeReference;+import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.core.common.AwsArnUtils;+import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.Resettable;+import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;+import io.github.hectorvent.floci.core.storage.StorageBackend;+import io.github.hectorvent.floci.core.storage.StorageFactory;+import io.github.hectorvent.floci.services.lambda.model.LambdaAlias;+import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;+import io.quarkus.runtime.annotations.RegisterForReflection;+import jakarta.annotation.PostConstruct;+import jakarta.enterprise.context.ApplicationScoped;+import jakarta.inject.Inject;++import java.time.Clock;+import java.time.Instant;+import java.time.ZoneOffset;+import java.time.format.DateTimeFormatter;+import java.util.ArrayList;+import java.util.HashMap;+import java.util.List;+import java.util.Map;+import java.util.Objects;++/** Stores desired capacity. Runtime allocation is introduced by the next stage. */+@ApplicationScoped+public class ProvisionedConcurrencyPool implements Resettable {+ private static final DateTimeFormatter LAST_MODIFIED =+ DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ").withZone(ZoneOffset.UTC);+ private final Object lock = new Object();+ private final LambdaFunctionStore functions;+ private final LambdaAliasStore aliases;+ private final StorageBackend<String, Configuration> storage;+ private final Clock clock;+ private final int maxPerFunction;+ private final int maxTotal;+ private final boolean supported;+ private final Map<String, Configuration> configurations = new HashMap<>();++ @RegisterForReflection+ public record Configuration(String account, String region, String name, String functionArn,+ String qualifier, String version, String revision,+ int requested, String lastModified) {+ String key() {+ return functionArn + ":" + qualifier;+ }+ }++ public record Status(int requested, int allocated, int available, String status,+ String reason, String lastModified) {}++ @Inject+ public ProvisionedConcurrencyPool(LambdaFunctionStore functions, LambdaAliasStore aliases,+ StorageFactory storageFactory, EmulatorConfig config, Clock clock) {+ this(functions, aliases, storageFactory.create("lambda",+ "lambda-provisioned-concurrency.json", new TypeReference<>() {}),+ clock, WarmPool.resolveMaxPerFunction(config), WarmPool.resolveMaxTotal(config),+ !config.services().lambda().ephemeral()+ && "docker".equalsIgnoreCase(config.services().lambda().executor().trim()));+ }++ ProvisionedConcurrencyPool(LambdaFunctionStore functions, LambdaAliasStore aliases,+ StorageBackend<String, Configuration> storage,+ Clock clock, int maxPerFunction, int maxTotal) {+ this(functions, aliases, storage, clock, maxPerFunction, maxTotal, true);+ }++ ProvisionedConcurrencyPool(LambdaFunctionStore functions, LambdaAliasStore aliases,+ StorageBackend<String, Configuration> storage,+ Clock clock, int maxPerFunction, int maxTotal, boolean supported) {+ this.supported = supported;+ this.functions = functions;+ this.aliases = aliases;+ this.storage = storage;+ this.clock = clock;+ this.maxPerFunction = maxPerFunction;+ this.maxTotal = maxTotal;+ }++ @PostConstruct+ void start() {+ synchronized (lock) {+ List<Configuration> stored = storage instanceof AccountAwareStorageBackend<Configuration> aware+ ? aware.scanAllAccounts() : storage.scan(ignored -> true);+ for (Configuration configuration : stored) {+ configurations.put(configuration.key(), configuration);+ }+ }+ }++ public Status put(String region, String name, String qualifier, int count, String account) {+ if (count < 1) {+ throw invalidCount(count);+ }+ if ("$LATEST".equals(qualifier)) {+ throw invalid("Provisioned Concurrency Configs cannot be applied to unpublished function versions.");+ }+ Configuration target = resolve(region, name, qualifier, account, count);+ synchronized (functions.lockForConcurrencyOp(target.functionArn())) {+ return put(resolve(region, name, qualifier, account, count));+ }+ }++ private Status put(Configuration target) {+ int count = target.requested();+ if (!supported) {+ throw invalid("Provisioned concurrency requires the non-ephemeral Docker Lambda executor");+ }+ LambdaFunction fn = functions.getForAccount(target.account(), target.region(), target.name(), target.version())+ .orElseThrow(() -> missing("Function no longer exists"));+ if (fn.isHotReload() || fn.isDurable()) {+ throw invalid("Provisioned concurrency is not supported for hot-reload or durable functions");+ }+ synchronized (lock) {+ Configuration previous = configurations.get(target.key());+ long otherFunction = 0;+ long otherTotal = 0;+ boolean versionConflict = false;+ for (Configuration entry : configurations.values()) {+ if (entry == previous) {+ continue;+ }+ otherTotal += entry.requested();+ if (target.functionArn().equals(entry.functionArn())) {+ versionConflict |= target.version().equals(pointedVersion(entry));+ otherFunction += entry.requested();+ }+ }+ if (otherFunction + count > maxPerFunction+ || (maxTotal > 0 && otherTotal + count > maxTotal)) {+ throw invalid("Requested provisioned concurrency exceeds the configured local warm-pool capacity");+ }+ LambdaFunction latest = functions.getForAccount(target.account(), target.region(), target.name()).orElseThrow(+ () -> missing("Function no longer exists"));+ Integer reserved = latest.getReservedConcurrentExecutions();+ if (reserved != null && otherFunction + count > reserved) {+ throw invalid("Provisioned concurrency cannot exceed the function's reserved concurrency");+ }+ if (versionConflict) {+ throw new AwsException("ResourceConflictException",+ target.qualifier().equals(target.version())+ ? "Version is pointed by a Provisioned Concurrency alias"+ : "Alias can't be used for Provisioned Concurrency configuration "+ + "on an already Provisioned version", 409);+ }+ if (previous != null && sameTarget(previous, target) && previous.requested() == count) {+ return status(previous);+ }+ write(target);+ configurations.put(target.key(), target);+ return status(target);+ }+ }++ public Status get(String region, String name, String qualifier, String account) {+ Target target = resolveTarget(region, name, qualifier, account);+ synchronized (lock) {+ Configuration configuration = configurations.get(target.key());+ if (configuration == null) {+ throw new AwsException("ProvisionedConcurrencyConfigNotFoundException",+ "No Provisioned Concurrency Config found for this function", 404);+ }+ return status(configuration);+ }+ }++ public void delete(String region, String name, String qualifier, String account) {+ Target target = resolveTarget(region, name, qualifier, account);+ synchronized (functions.lockForConcurrencyOp(target.functionArn())) {+ resolveTarget(region, name, qualifier, account);+ deleteMatching(target.functionArn(), qualifier);+ }+ }++ void deleteFunction(LambdaFunction fn) {+ deleteMatching(baseArn(fn), null);+ }++ void deleteQualifier(LambdaFunction fn, String qualifier) {+ deleteMatching(baseArn(fn), qualifier);+ }++ void validateReservation(LambdaFunction fn, Integer reserved) {+ if (reserved == null) {+ return;+ }+ synchronized (lock) {+ long requested = configurations.values().stream()+ .filter(entry -> entry.functionArn().equals(baseArn(fn)))+ .mapToLong(entry -> entry.requested()).sum();+ if (requested > reserved) {+ throw invalid("Reserved concurrency cannot be lower than provisioned concurrency");+ }+ }+ }++ private void deleteMatching(String functionArn, String qualifier) {+ synchronized (lock) {+ for (Configuration value : new ArrayList<>(configurations.values())) {+ if (value.functionArn().equals(functionArn)+ && (qualifier == null || qualifier.equals(value.qualifier()))) {+ erase(value);+ configurations.remove(value.key());+ }+ }+ }+ }++ private Status status(Configuration configuration) {+ return new Status(configuration.requested(), 0, 0, "IN_PROGRESS", null, configuration.lastModified());+ }++ private String pointedVersion(Configuration configuration) {+ if (configuration.qualifier().matches("[0-9]+")) {+ return configuration.version();+ }+ return aliases.getForAccount(configuration.account(), configuration.region(),+ configuration.name(), configuration.qualifier())+ .map(LambdaAlias::getFunctionVersion).orElse(null);+ }++ void validateAliasUpdate(LambdaFunction fn, String qualifier, String version) {+ synchronized (lock) {+ String functionArn = baseArn(fn);+ if (!configurations.containsKey(functionArn + ":" + qualifier)) {+ return;+ }+ if ("$LATEST".equals(version)) {+ throw invalid("Provisioned Concurrency Configs cannot be applied to unpublished function versions.");+ }+ if (version != null && version.matches("[0-9]+") && configurations.values().stream()+ .anyMatch(entry -> functionArn.equals(entry.functionArn())+ && !qualifier.equals(entry.qualifier())+ && version.equals(pointedVersion(entry)))) {+ throw new AwsException("ResourceConflictException",+ "Alias can't be used for Provisioned Concurrency configuration on an already Provisioned version", 409);+ }+ }+ }++ private record Target(String account, String region, String name, String functionArn,+ String qualifier, LambdaAlias alias) {+ String key() {+ return functionArn + ":" + qualifier;+ }++ String missingMessage() {+ return (alias == null ? "Function not found: " : "Cannot find alias arn: ") + key();+ }+ }++ private Configuration resolve(String region, String name, String qualifier, String account, int count) {+ Target target = resolveTarget(region, name, qualifier, account);+ LambdaAlias alias = target.alias();+ if (alias != null && alias.getRoutingConfig() != null && !alias.getRoutingConfig().isEmpty()) {+ throw invalid("Weighted aliases are not supported for provisioned concurrency");+ }+ String version = alias == null ? qualifier : alias.getFunctionVersion();+ if (version == null || !version.matches("[0-9]+")) {+ throw invalid("Provisioned concurrency requires an immutable published version");+ }+ LambdaFunction fn = functions.getForAccount(target.account(), region, target.name(), version)+ .orElseThrow(() -> missing(target.missingMessage()));+ return new Configuration(target.account(), region, target.name(), target.functionArn(), qualifier,+ version, fn.getRevisionId(), count, LAST_MODIFIED.format(Instant.ofEpochMilli(clock.millis())));+ }++ private Target resolveTarget(String region, String name, String qualifier, String account) {+ if (qualifier == null || qualifier.isBlank() || qualifier.length() > 128) {+ throw invalid("Qualifier is required and must contain 1 to 128 characters");+ }+ if ("$LATEST".equals(qualifier)) {+ throw invalid("The function resource provided must be an alias or a published version.");+ }+ LambdaArnUtils.ResolvedFunctionRef ref = LambdaArnUtils.resolveWithQualifier(name, qualifier);+ if (ref.region() != null && !region.equals(ref.region())) {+ throw invalid("Function ARN region must match the request region");+ }+ account = ref.account() != null ? ref.account() : account;+ boolean publishedVersion = qualifier.matches("[0-9]+");+ String qualifiedArn = AwsArnUtils.Arn.of("lambda", region, account,+ "function:" + ref.name() + ":" + qualifier).toString();+ String notFoundMessage = (publishedVersion ? "Function not found: " : "Cannot find alias arn: ")+ + qualifiedArn;+ LambdaFunction latest = functions.getForAccount(account, region, ref.name())+ .orElseThrow(() -> missing(notFoundMessage));+ LambdaAlias alias = null;+ if (publishedVersion) {+ functions.getForAccount(account, region, ref.name(), qualifier)+ .orElseThrow(() -> missing(notFoundMessage));+ } else {+ alias = aliases.getForAccount(account, region, ref.name(), qualifier)+ .orElseThrow(() -> missing(notFoundMessage));+ }+ return new Target(account, region, ref.name(), baseArn(latest), qualifier, alias);+ }++ private static boolean sameTarget(Configuration first, Configuration second) {+ return first.key().equals(second.key()) && first.version().equals(second.version())+ && Objects.equals(first.revision(), second.revision());+ }++ private static String baseArn(LambdaFunction fn) {+ AwsArnUtils.Arn arn = AwsArnUtils.parse(fn.getFunctionArn());+ return new AwsArnUtils.Arn(arn.partition(), "lambda", arn.region(), arn.accountId(),+ "function:" + fn.getFunctionName()).toString();+ }++ private void write(Configuration value) {+ if (storage instanceof AccountAwareStorageBackend<Configuration> aware) {+ aware.putForAccount(value.account(), value.key(), value);+ } else {+ storage.put(value.key(), value);+ }+ }++ private void erase(Configuration value) {+ if (storage instanceof AccountAwareStorageBackend<Configuration> aware) {+ aware.deleteForAccount(value.account(), value.key());+ } else {+ storage.delete(value.key());+ }+ }++ @Override+ public void clear() {+ synchronized (lock) {+ storage.clear();+ configurations.clear();+ }+ }++ private static AwsException invalid(String message) {+ return new AwsException("InvalidParameterValueException", message, 400);+ }++ static AwsException invalidCount(Integer count) {+ String value = count == null ? "null" : "'" + count + "'";+ String constraint = count == null ? "Member must not be null"+ : "Member must have value greater than or equal to 1";+ return new AwsException("ValidationException", "1 validation error detected: Value "+ + value + " at 'provisionedConcurrentExecutions' failed to satisfy constraint: "+ + constraint, 400);+ }++ private static AwsException missing(String message) {+ return new AwsException("ResourceNotFoundException", message, 404);+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/WarmPool.java b/src/main/java/io/github/hectorvent/floci/services/lambda/WarmPool.javaindex 2f106c06c6..9ae52033dd 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/WarmPool.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/WarmPool.java@@ -52,6 +52,7 @@ public class WarmPool implements ContainerTeardown, Resettable {private static final int DEFAULT_MAX_POOL_SIZE = Math.max(4, Runtime.getRuntime().availableProcessors());private final LambdaRuntimeLauncher lambdaRuntimeLauncher;+ private final ProvisionedConcurrencyPool provisioned;private final EmulatorConfig config;private final Clock clock;private final int maxPoolSizePerFunction;@@ -92,7 +93,9 @@ public class WarmPool implements ContainerTeardown, Resettable {}@Inject- public WarmPool(LambdaRuntimeLauncher lambdaRuntimeLauncher, EmulatorConfig config, Clock clock) {+ public WarmPool(LambdaRuntimeLauncher lambdaRuntimeLauncher, EmulatorConfig config, Clock clock,+ ProvisionedConcurrencyPool provisioned) {+ this.provisioned = provisioned;this.lambdaRuntimeLauncher = lambdaRuntimeLauncher;this.config = config;this.clock = clock;@@ -101,9 +104,14 @@ public class WarmPool implements ContainerTeardown, Resettable {this.drainTimeoutNanos = TimeUnit.SECONDS.toNanos(resolveDrainTimeout(config));}+ public WarmPool(LambdaRuntimeLauncher launcher, EmulatorConfig config, Clock clock) {+ this(launcher, config, clock, null);+ }+/** Package-private constructor for testing (empty pool, no containers to drain). */WarmPool() {this.lambdaRuntimeLauncher = null;+ this.provisioned = null;this.config = null;this.clock = Clock.systemUTC();this.maxPoolSizePerFunction = DEFAULT_MAX_POOL_SIZE;@@ -447,6 +455,30 @@ public class WarmPool implements ContainerTeardown, Resettable {stopQuietly(handle);}+ void deleteProvisionedFunction(LambdaFunction fn) {+ if (provisioned != null) {+ provisioned.deleteFunction(fn);+ }+ }++ void deleteProvisionedQualifier(LambdaFunction fn, String qualifier) {+ if (provisioned != null) {+ provisioned.deleteQualifier(fn, qualifier);+ }+ }++ void validateProvisionedAliasUpdate(LambdaFunction fn, String qualifier, String version) {+ if (provisioned != null) {+ provisioned.validateAliasUpdate(fn, qualifier, version);+ }+ }++ void validateProvisionedReservation(LambdaFunction fn, Integer reserved) {+ if (provisioned != null) {+ provisioned.validateReservation(fn, reserved);+ }+ }+/*** Stops and removes warm containers for one immutable execution environment, such as* {@code $LATEST}. Published versions keep their independently keyed warm containers.diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.javaindex ef49642a37..a22750232c 100644--- a/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java@@ -36,6 +36,27 @@ class IamActionRegistryTest {private final IamActionRegistry registry = new IamActionRegistry();+ @ParameterizedTest+ @CsvSource({"PUT,PutProvisionedConcurrencyConfig", "GET,GetProvisionedConcurrencyConfig",+ "DELETE,DeleteProvisionedConcurrencyConfig"})+ void provisionedConcurrencyRoutesResolveTheirActions(String method, String operation) {+ ContainerRequestContext ctx = mockCtx(method, "/2019-09-30/functions/example/provisioned-concurrency",+ new MultivaluedHashMap<>(), MediaType.APPLICATION_JSON_TYPE, "{}");+ assertEquals("lambda:" + operation, registry.resolveRoute("lambda", ctx));+ }++ @ParameterizedTest+ @CsvSource({"ALL,ListProvisionedConcurrencyConfigs", "all,GetProvisionedConcurrencyConfig",+ "OTHER,GetProvisionedConcurrencyConfig"})+ void provisionedConcurrencyListMarkerSelectsItsOwnAction(String marker, String operation) {+ MultivaluedMap<String, String> query = new MultivaluedHashMap<>();+ query.add("List", marker);+ query.add("Qualifier", "live");+ ContainerRequestContext ctx = mockCtx("GET", "/2019-09-30/functions/example/provisioned-concurrency",+ query, null, "");+ assertEquals("lambda:" + operation, registry.resolveRoute("lambda", ctx));+ }+@ParameterizedTest@CsvSource({"groups,CreateGroup", "get-group,GetGroup", "delete-group,DeleteGroup","group-resources,GroupResources", "list-group-resources,ListGroupResources"})diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.javaindex e7f4cd02ca..79e15958d2 100644--- a/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java@@ -1180,6 +1180,18 @@ class ResourceArnBuilderTest {builder.build("lambda", ctx, "us-east-1", "000000000000"));}+ @Test+ void provisionedConcurrencyKeepsTheOwnerAndQualifierItConfigures() {+ when(uriInfo.getPath()).thenReturn(+ "/2019-09-30/functions/arn:aws:lambda:us-east-1:111122223333:function:example/provisioned-concurrency");+ queryParams.putSingle("Qualifier", "live");+ assertEquals("arn:aws:lambda:us-east-1:111122223333:function:example:live",+ builder.build("lambda", ctx, "us-east-1", "000000000000"));+ when(uriInfo.getPath()).thenReturn("/2019-09-30/functions/example/provisioned-concurrency");+ assertEquals("arn:aws:lambda:us-east-1:000000000000:function:example:live",+ builder.build("lambda", ctx, "us-east-1", "000000000000"));+ }+@Testvoid lambdaWebRoutesResolveTheFunctionRevisionOrEndpointTheyActOn() {String fn = "arn:aws:lambda:us-east-1:000000000000:web-function/app";diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaCrossAccountInvokeIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaCrossAccountInvokeIntegrationTest.javaindex 1d6fd12f99..bf33a77326 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaCrossAccountInvokeIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaCrossAccountInvokeIntegrationTest.java@@ -238,6 +238,66 @@ class LambdaCrossAccountInvokeIntegrationTest {.then().statusCode(200).body("Configuration.FunctionArn", startsWith(foreignArn));}+ @Test+ void unpublishedCapacityValidationPrecedesMissingFunctionLookupAfterIamAuthorization() {+ String name = "absent-capacity-" + suffix();+ String user = createUser(OWNER, "capacity-admin-" + suffix(), """+ {"Version":"2012-10-17","Statement":[+ {"Effect":"Allow","Action":"lambda:*","Resource":"*"}]}""");+ String path = "/2019-09-30/functions/{name}/provisioned-concurrency";+ lambda(user).queryParam("Qualifier", "$LATEST").body("{\"ProvisionedConcurrentExecutions\":1}")+ .put(path, name).then().statusCode(400)+ .body("__type", equalTo("InvalidParameterValueException"))+ .body("message", equalTo(+ "Provisioned Concurrency Configs cannot be applied to unpublished function versions."));+ String message = "The function resource provided must be an alias or a published version.";+ lambda(user).queryParam("Qualifier", "$LATEST").get(path, name).then().statusCode(400)+ .body("message", equalTo(message));+ lambda(user).queryParam("Qualifier", "$LATEST").delete(path, name).then().statusCode(400)+ .body("message", equalTo(message));+ lambda(user).queryParam("Qualifier", "1").get(path, name).then().statusCode(404)+ .body("__type", equalTo("ResourceNotFoundException"));++ String denied = createUser(OWNER, "capacity-denied-" + suffix(), INVOKE_ANYTHING);+ lambda(denied).queryParam("Qualifier", "$LATEST").get(path, name).then().statusCode(403)+ .body("__type", equalTo("AccessDeniedException"));+ }++ @Test+ void provisionedConcurrencyRequiresTheOwnersQualifiedGrant() {+ String name = "capacity-" + suffix();+ String arn = createFunction(OWNER, name);+ publishVersionAndAlias(OWNER, name, "live");+ String user = createUser(CALLER, "capacity-reader-" + suffix(), """+ {"Version":"2012-10-17","Statement":[+ {"Effect":"Allow","Action":"lambda:*","Resource":"*"}]}""");+ String path = "/2019-09-30/functions/{name}/provisioned-concurrency";+ lambda(user).queryParam("Qualifier", "1").get(path, arn).then().statusCode(403);+ addPermission(OWNER, arn, "1", Map.of("Principal", CALLER,+ "Action", "lambda:GetProvisionedConcurrencyConfig"));+ lambda(user).queryParam("Qualifier", "1").get(path, arn).then().statusCode(404)+ .body("__type", equalTo("ProvisionedConcurrencyConfigNotFoundException"));+ lambda(user).queryParam("Qualifier", "live").get(path, arn).then().statusCode(403);+ lambda(user).queryParam("Qualifier", "1").delete(path, arn).then().statusCode(403);+ lambda(user).queryParam("Qualifier", "1").body("{\"ProvisionedConcurrentExecutions\":1}")+ .put(path, arn).then().statusCode(403);+ lambda(OWNER).queryParam("Qualifier", "1").get(path, name).then().statusCode(404);+ }++ @Test+ void provisionedConcurrencyGrantDoesNotReplaceTheCallersIdentityPolicy() {+ String name = "capacity-deny-" + suffix();+ String arn = createFunction(OWNER, name);+ publishVersionAndAlias(OWNER, name, "live");+ addPermission(OWNER, arn, "1", Map.of("Principal", CALLER,+ "Action", "lambda:GetProvisionedConcurrencyConfig"));+ String user = createUser(CALLER, "capacity-denied-" + suffix(), """+ {"Version":"2012-10-17","Statement":[+ {"Effect":"Allow","Action":"lambda:InvokeFunction","Resource":"*"}]}""");+ lambda(user).queryParam("Qualifier", "1")+ .get("/2019-09-30/functions/{name}/provisioned-concurrency", arn).then().statusCode(403);+ }+@Testvoid rotateSecretIsRefusedForAnotherAccountsFunctionThatGrantsNothing() {String name = "rotator-" + suffix();diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaFunctionTestFixtures.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaFunctionTestFixtures.javanew file mode 100644index 0000000000..f28c2aefa4--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaFunctionTestFixtures.java@@ -0,0 +1,31 @@+package io.github.hectorvent.floci.services.lambda;++import io.github.hectorvent.floci.core.common.AwsArnUtils;+import io.github.hectorvent.floci.services.lambda.model.LambdaAlias;+import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;++final class LambdaFunctionTestFixtures {++ private LambdaFunctionTestFixtures() {+ }++ static LambdaFunction function(String account, String region, String name, String version) {+ LambdaFunction function = new LambdaFunction();+ function.setFunctionName(name);+ function.setAccountId(account);+ String arn = AwsArnUtils.Arn.of("lambda", region, account, "function:" + name).toString();+ function.setFunctionArn(arn + ("$LATEST".equals(version) ? "" : ":" + version));+ function.setVersion(version);+ function.setRevisionId(account + region + version);+ return function;+ }++ static LambdaAlias alias(String account, String region, String function, String name, String version) {+ LambdaAlias alias = new LambdaAlias();+ alias.setName(name);+ alias.setFunctionName(function);+ alias.setFunctionVersion(version);+ alias.setAliasArn(AwsArnUtils.Arn.of("lambda", region, account, "function:" + function + ":" + name).toString());+ return alias;+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaProvisionedConcurrencyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaProvisionedConcurrencyIntegrationTest.javanew file mode 100644index 0000000000..0519d65235--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaProvisionedConcurrencyIntegrationTest.java@@ -0,0 +1,108 @@+package io.github.hectorvent.floci.services.lambda;++import io.github.hectorvent.floci.core.common.AwsException;+import io.quarkus.test.InjectMock;+import io.quarkus.test.junit.QuarkusTest;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.ValueSource;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.equalTo;+import static org.mockito.ArgumentMatchers.*;+import static org.mockito.Mockito.*;++@QuarkusTest+class LambdaProvisionedConcurrencyIntegrationTest {+ private static final String PATH = "/2019-09-30/functions/example/provisioned-concurrency";+ @InjectMock+ ProvisionedConcurrencyPool pool;++ @Test+ void putUsesTheDatedAwsRouteAndReturnsAcceptedAllocation() {+ when(pool.put(anyString(), eq("example"), eq("live"), eq(1), anyString()))+ .thenReturn(new ProvisionedConcurrencyPool.Status(1, 0, 0, "IN_PROGRESS", null,+ "2026-01-01T00:00:00Z"));+ given().contentType("application/json").queryParam("Qualifier", "live")+ .body("{\"ProvisionedConcurrentExecutions\":1}").put(PATH).then().statusCode(202)+ .body("Status", equalTo("IN_PROGRESS"))+ .body("RequestedProvisionedConcurrentExecutions", equalTo(1))+ .body("AllocatedProvisionedConcurrentExecutions", equalTo(0))+ .body("AvailableProvisionedConcurrentExecutions", equalTo(0))+ .body("LastModified", equalTo("2026-01-01T00:00:00Z"));+ }++ @Test+ void getReportsAllocatedCapacityAndDeleteIsEmpty204() {+ when(pool.get(anyString(), eq("example"), eq("live"), anyString()))+ .thenReturn(new ProvisionedConcurrencyPool.Status(2, 2, 1, "READY", null,+ "2026-01-01T00:00:00Z"));+ given().queryParam("Qualifier", "live").get(PATH).then().statusCode(200)+ .body("Status", equalTo("READY"))+ .body("RequestedProvisionedConcurrentExecutions", equalTo(2))+ .body("AllocatedProvisionedConcurrentExecutions", equalTo(2))+ .body("AvailableProvisionedConcurrentExecutions", equalTo(1));+ given().queryParam("Qualifier", "live").delete(PATH).then().statusCode(204).body(equalTo(""));+ verify(pool).delete(anyString(), eq("example"), eq("live"), anyString());+ }++ @Test+ void missingConfigurationUsesTheModeledAwsException() {+ when(pool.get(anyString(), eq("example"), eq("live"), anyString()))+ .thenThrow(new AwsException("ProvisionedConcurrencyConfigNotFoundException", "absent", 404));+ given().queryParam("Qualifier", "live").get(PATH).then().statusCode(404)+ .body("__type", equalTo("ProvisionedConcurrencyConfigNotFoundException"));+ }++ @ParameterizedTest+ @ValueSource(strings = {"{}", "null", "[]", "{\"ProvisionedConcurrentExecutions\":true}",+ "{\"ProvisionedConcurrentExecutions\":\"1\"}",+ "{\"ProvisionedConcurrentExecutions\":0}", "{\"ProvisionedConcurrentExecutions\":-1}",+ "{\"ProvisionedConcurrentExecutions\":2147483648}"})+ void invalidCountsNeverReachTheAllocator(String body) {+ given().contentType("application/json").queryParam("Qualifier", "live").body(body)+ .put(PATH).then().statusCode(400);+ verify(pool, never()).put(any(), any(), any(), anyInt(), any());+ }++ @Test+ void decimalCountUsesAwsIntegerCoercion() {+ when(pool.put(anyString(), eq("example"), eq("live"), eq(1), anyString()))+ .thenReturn(new ProvisionedConcurrencyPool.Status(1, 0, 0, "IN_PROGRESS", null,+ "2026-10-20T20:11:30+0000"));+ given().contentType("application/json").queryParam("Qualifier", "live")+ .body("{\"ProvisionedConcurrentExecutions\":1.5}").put(PATH).then().statusCode(202)+ .body("RequestedProvisionedConcurrentExecutions", equalTo(1))+ .body("LastModified", equalTo("2026-10-20T20:11:30+0000"));+ verify(pool).put(anyString(), eq("example"), eq("live"), eq(1), anyString());+ }++ @ParameterizedTest+ @ValueSource(strings = {"{}", "{\"ProvisionedConcurrentExecutions\":null}"})+ void omittedAndNullCountsUseAwsValidation(String body) {+ given().contentType("application/json").queryParam("Qualifier", "live").body(body)+ .put(PATH).then().statusCode(400)+ .body("__type", equalTo("ValidationException"))+ .body("message", equalTo("1 validation error detected: Value null at 'provisionedConcurrentExecutions' "+ + "failed to satisfy constraint: Member must not be null"));+ verify(pool, never()).put(any(), any(), any(), anyInt(), any());+ }++ @ParameterizedTest+ @ValueSource(ints = {0, -1})+ void nonpositiveCountsUseAwsValidation(int count) {+ given().contentType("application/json").queryParam("Qualifier", "live")+ .body("{\"ProvisionedConcurrentExecutions\":" + count + "}").put(PATH).then().statusCode(400)+ .body("__type", equalTo("ValidationException"));+ verify(pool, never()).put(any(), any(), any(), anyInt(), any());+ }++ @ParameterizedTest+ @ValueSource(strings = {"true", "\"1\""})+ void incompatibleCountTypesUseAwsSerializationError(String value) {+ given().contentType("application/json").queryParam("Qualifier", "live")+ .body("{\"ProvisionedConcurrentExecutions\":" + value + "}").put(PATH).then().statusCode(400)+ .body("__type", equalTo("SerializationException"));+ verify(pool, never()).put(any(), any(), any(), anyInt(), any());+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaProvisionedConcurrencyNotFoundIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaProvisionedConcurrencyNotFoundIntegrationTest.javanew file mode 100644index 0000000000..8fe385ef2d--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaProvisionedConcurrencyNotFoundIntegrationTest.java@@ -0,0 +1,83 @@+package io.github.hectorvent.floci.services.lambda;++import io.github.hectorvent.floci.core.common.RegionResolver;+import io.github.hectorvent.floci.services.lambda.model.LambdaAlias;+import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;+import io.quarkus.test.junit.QuarkusTest;+import jakarta.inject.Inject;+import org.junit.jupiter.api.AfterEach;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.CsvSource;++import java.util.List;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.equalTo;++@QuarkusTest+class LambdaProvisionedConcurrencyNotFoundIntegrationTest {+ private static final String NAME = "pc-missing-target-control";+ @Inject+ LambdaFunctionStore functions;+ @Inject+ LambdaAliasStore aliases;+ @Inject+ RegionResolver resolver;++ @BeforeEach+ void createTarget() {+ for (String version : List.of("$LATEST", "1")) {+ LambdaFunction function = LambdaFunctionTestFixtures.function(+ resolver.getAccountId(), resolver.getRegion(), NAME, version);+ function.setRevisionId("missing-target-control-" + version);+ functions.save(resolver.getRegion(), function);+ }+ LambdaAlias alias = LambdaFunctionTestFixtures.alias(+ resolver.getAccountId(), resolver.getRegion(), NAME, "live", "1");+ aliases.save(resolver.getRegion(), alias);+ }++ @AfterEach+ void deleteTarget() {+ aliases.delete(resolver.getRegion(), NAME, "live");+ functions.delete(resolver.getRegion(), NAME);+ }++ @ParameterizedTest+ @CsvSource({+ "pc-missing-target-control, missing, Cannot find alias arn: ",+ "pc-missing-target-control, 99, Function not found: ",+ "pc-absent-function-control, missing, Cannot find alias arn: ",+ "pc-absent-function-control, 99, Function not found: "+ })+ void everyOperationReturnsTheQualifiedTarget(String name, String qualifier, String prefix) {+ String message = prefix + " " + functionArn(name) + ":" + qualifier;+ for (String reference : List.of(name, functionArn(name))) {+ String path = "/2019-09-30/functions/{function}/provisioned-concurrency";+ for (String method : List.of("GET", "PUT", "DELETE")) {+ given().contentType("application/json").queryParam("Qualifier", qualifier)+ .pathParam("function", reference).body("{\"ProvisionedConcurrentExecutions\":1}")+ .request(method, path).then().statusCode(404)+ .body("__type", equalTo("ResourceNotFoundException"))+ .body("message", equalTo(message));+ }+ }+ }++ @Test+ void existingAliasAndVersionRetainTheirConfigurationBehavior() {+ for (String qualifier : List.of("live", "1")) {+ String path = "/2019-09-30/functions/" + NAME + "/provisioned-concurrency";+ given().queryParam("Qualifier", qualifier).get(path).then().statusCode(404)+ .body("__type", equalTo("ProvisionedConcurrencyConfigNotFoundException"))+ .body("message", equalTo("No Provisioned Concurrency Config found for this function"));+ given().queryParam("Qualifier", qualifier).delete(path).then().statusCode(204).body(equalTo(""));+ }+ }++ private String functionArn(String name) {+ return resolver.buildArn("lambda", resolver.getRegion(), "function:" + name);+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.javaindex afeb489e1c..2baadd841e 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;import io.github.hectorvent.floci.core.storage.InMemoryStorage;import io.github.hectorvent.floci.core.storage.StorageFactory;import io.github.hectorvent.floci.services.lambda.model.EventSourceMapping;+import io.github.hectorvent.floci.services.lambda.model.LambdaAlias;import io.github.hectorvent.floci.services.lambda.model.LambdaFileSystemConfig;import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;import io.github.hectorvent.floci.services.lambda.model.LambdaUrlConfig;@@ -19,6 +20,7 @@ import io.github.hectorvent.floci.services.s3.model.S3ObjectUpdatedEvent;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.CsvSource;import org.junit.jupiter.params.provider.ValueSource;import java.io.ByteArrayInputStream;@@ -1641,6 +1643,49 @@ class LambdaServiceTest {assertEquals("ValidationException", error.getErrorCode());}+ @ParameterizedTest+ @CsvSource({"false,false", "false,true", "true,false", "true,true"})+ void deletingQualifierWaitsForProvisionedConcurrencyLock(boolean alias, boolean qualifiedName) throws Exception {+ String name = "qualifier-deletion-lock";+ LambdaFunction latest = service.createFunction(REGION, baseRequest(name));+ LambdaFunction version = service.publishVersion(REGION, name, null);+ if (alias) {+ service.createAlias(REGION, name, "live", version.getVersion(), null, null);+ }+ String reference = qualifiedName ? latest.getFunctionArn() + ":" + version.getVersion() : name;+ AtomicReference<Thread> requester = new AtomicReference<>();+ try (ExecutorService tasks = Executors.newSingleThreadExecutor()) {+ Future<?> deletion;+ synchronized (service.lockForConcurrencyOp(latest.getFunctionArn())) {+ deletion = tasks.submit(() -> {+ requester.set(Thread.currentThread());+ if (alias) {+ service.deleteAlias(REGION, reference, "live");+ } else {+ service.deleteVersion(REGION, reference, version.getVersion());+ }+ });+ awaitBlockedOrDone(requester, deletion);+ assertFalse(deletion.isDone());+ if (alias) {+ assertEquals(version.getVersion(), service.getAlias(REGION, name, "live").getFunctionVersion());+ } else {+ assertEquals(version.getVersion(), service.getFunction(REGION, name, version.getVersion()).getVersion());+ }+ }+ deletion.get(5, TimeUnit.SECONDS);+ }+ AwsException missing = assertThrows(AwsException.class, () -> {+ if (alias) {+ service.getAlias(REGION, name, "live");+ } else {+ service.getFunction(REGION, name, version.getVersion());+ }+ });+ assertEquals("ResourceNotFoundException", missing.getErrorCode());+ assertEquals("$LATEST", service.getFunction(REGION, name).getVersion());+ }+@Testvoid createFunctionUrlConfigRejectsUnknownAuthType() {AwsException error = assertThrows(AwsException.class,@@ -2849,6 +2894,28 @@ class LambdaServiceTest {+ "Member must satisfy regular expression pattern: (\\$LATEST(\\.PUBLISHED)?|[0-9]+)", error.getMessage());}+ @ParameterizedTest+ @ValueSource(strings = {"$LATEST", "2"})+ void updateAliasValidatesProvisionedCapacityBeforeMutatingItsStoredFields(String targetVersion) {+ WarmPool warmPool = mock(WarmPool.class);+ LambdaService guarded = s3BackedService(null, warmPool, new ZipExtractor());+ LambdaFunction fn = guarded.createFunction(REGION, baseRequest("alias-capacity-fn"));+ LambdaAlias alias = guarded.createAlias(REGION, fn.getFunctionName(), "live", "1", "original", null);+ AwsException failure = new AwsException("InvalidParameterValueException",+ "Provisioned Concurrency Configs cannot be applied to unpublished function versions.", 400);+ doAnswer(invocation -> {+ assertTrue(Thread.holdsLock(guarded.lockForConcurrencyOp(fn.getFunctionArn())));+ throw failure;+ }).when(warmPool).validateProvisionedAliasUpdate(fn, "live", targetVersion);++ assertSame(failure, assertThrows(AwsException.class, () -> guarded.updateAlias(REGION,+ fn.getFunctionName(), "live", targetVersion, "changed", Map.of("2", 0.1))));+ assertSame(alias, guarded.getAlias(REGION, fn.getFunctionName(), "live"));+ assertEquals("1", alias.getFunctionVersion());+ assertEquals("original", alias.getDescription());+ assertTrue(alias.getRoutingConfig() == null || alias.getRoutingConfig().isEmpty());+ }+@Testvoid updateAlias_descriptionLongerThan256_isRejectedAndLeavesTheAliasUnchanged() {// Catches: UpdateAlias accepting (or half-applying) a Description over the model's 256-character maximumdiff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/ProvisionedConcurrencyPoolTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/ProvisionedConcurrencyPoolTest.javanew file mode 100644index 0000000000..3e3740ce5b--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/ProvisionedConcurrencyPoolTest.java@@ -0,0 +1,381 @@+package io.github.hectorvent.floci.services.lambda;++import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.ServiceConfigAccess;+import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;+import io.github.hectorvent.floci.core.storage.StorageFactory;+import io.github.hectorvent.floci.services.lambda.model.LambdaAlias;+import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.io.TempDir;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.CsvSource;+import org.junit.jupiter.params.provider.ValueSource;++import java.nio.file.Path;+import java.time.Clock;+import java.time.Instant;+import java.time.ZoneOffset;+import java.util.List;+import java.util.Map;+import java.util.concurrent.ExecutorService;+import java.util.concurrent.Executors;+import java.util.concurrent.Future;+import java.util.concurrent.TimeUnit;+import java.util.concurrent.atomic.AtomicReference;++import static io.github.hectorvent.floci.testing.ThreadStates.awaitBlockedOrDone;+import static org.junit.jupiter.api.Assertions.*;+import static org.mockito.ArgumentMatchers.any;+import static org.mockito.Mockito.*;++class ProvisionedConcurrencyPoolTest {+ @TempDir+ Path storagePath;++ private static final String ACCOUNT = "111111111111";+ private static final String REGION = "us-east-1";+ private final AccountAwareStorageBackend<LambdaFunction> functionStorage =+ AccountAwareStorageBackend.inMemory(ACCOUNT);+ private final AccountAwareStorageBackend<LambdaAlias> aliasStorage =+ AccountAwareStorageBackend.inMemory(ACCOUNT);+ private final AccountAwareStorageBackend<ProvisionedConcurrencyPool.Configuration> storage =+ AccountAwareStorageBackend.inMemory(ACCOUNT);+ private final LambdaFunctionStore functions = new LambdaFunctionStore(functionStorage);+ private final LambdaAliasStore aliases = new LambdaAliasStore(aliasStorage);+ private ProvisionedConcurrencyPool pool;+ private LambdaFunction version;++ @BeforeEach+ void setUp() {+ version = function(ACCOUNT, REGION, "1");+ function(ACCOUNT, REGION, "$LATEST");+ alias(ACCOUNT, REGION, "live", "1", Map.of());+ pool = newPool();+ }++ private ProvisionedConcurrencyPool newPool() {+ return new ProvisionedConcurrencyPool(functions, aliases, storage, Clock.systemUTC(), 4, 8);+ }++ private LambdaFunction function(String account, String region, String number) {+ LambdaFunction fn = LambdaFunctionTestFixtures.function(account, region, "example", number);+ fn.setTimeout(3);+ functions.saveForAccount(account, region, fn);+ return fn;+ }++ private void alias(String account, String region, String name, String number, Map<String, Double> weights) {+ LambdaAlias alias = LambdaFunctionTestFixtures.alias(account, region, "example", name, number);+ alias.setRoutingConfig(weights);+ aliasStorage.putForAccount(account, "alias::" + region + "::example::" + name, alias);+ }++ private ProvisionedConcurrencyPool.Status put(int count) {+ return pool.put(REGION, "example", "live", count, ACCOUNT);+ }++ @ParameterizedTest+ @ValueSource(strings = {"example", "absent"})+ void unpublishedVersionErrorsDependOnTheOperation(String name) {+ AwsException put = assertThrows(AwsException.class,+ () -> pool.put(REGION, name, "$LATEST", 1, ACCOUNT));+ assertEquals("InvalidParameterValueException", put.getErrorCode());+ assertEquals("Provisioned Concurrency Configs cannot be applied to unpublished function versions.",+ put.getMessage());+ for (Runnable operation : List.<Runnable>of(+ () -> pool.get(REGION, name, "$LATEST", ACCOUNT),+ () -> pool.delete(REGION, name, "$LATEST", ACCOUNT))) {+ AwsException error = assertThrows(AwsException.class, operation::run);+ assertEquals("InvalidParameterValueException", error.getErrorCode());+ assertEquals("The function resource provided must be an alias or a published version.",+ error.getMessage());+ }+ }++ @ParameterizedTest+ @ValueSource(ints = {0, -1})+ void invalidCountUsesAwsValidationBeforeAllocating(int count) {+ AwsException error = assertThrows(AwsException.class, () -> put(count));+ assertEquals("ValidationException", error.getErrorCode());+ assertEquals("1 validation error detected: Value '" + count+ + "' at 'provisionedConcurrentExecutions' failed to satisfy constraint: "+ + "Member must have value greater than or equal to 1", error.getMessage());+ assertTrue(storage.scanAllAccounts().isEmpty());+ }++ @Test+ void missingConfigurationUsesAwsMessageAndDeleteIsIdempotent() {+ AwsException error = assertThrows(AwsException.class, this::get);+ assertEquals("ProvisionedConcurrencyConfigNotFoundException", error.getErrorCode());+ assertEquals("No Provisioned Concurrency Config found for this function", error.getMessage());+ assertDoesNotThrow(() -> pool.delete(REGION, "example", "live", ACCOUNT));+ assertTrue(storage.scanAllAccounts().isEmpty());+ }++ @ParameterizedTest+ @CsvSource({+ "example, missing, Cannot find alias arn: arn:aws:lambda:us-east-1:111111111111:function:example:missing",+ "example, 99, Function not found: arn:aws:lambda:us-east-1:111111111111:function:example:99",+ "absent, missing, Cannot find alias arn: arn:aws:lambda:us-east-1:111111111111:function:absent:missing",+ "absent, 99, Function not found: arn:aws:lambda:us-east-1:111111111111:function:absent:99"+ })+ void missingTargetsUseTheQualifiedAwsMessageForEveryOperation(String name, String qualifier, String message) {+ for (String reference : List.of(name, ACCOUNT + ":function:" + name,+ "arn:aws:lambda:" + REGION + ":" + ACCOUNT + ":function:" + name,+ "arn:aws:lambda:" + REGION + ":" + ACCOUNT + ":function:" + name + ":" + qualifier)) {+ for (Runnable operation : List.<Runnable>of(+ () -> pool.get(REGION, reference, qualifier, ACCOUNT),+ () -> pool.put(REGION, reference, qualifier, 1, ACCOUNT),+ () -> pool.delete(REGION, reference, qualifier, ACCOUNT))) {+ AwsException error = assertThrows(AwsException.class, operation::run);+ assertEquals("ResourceNotFoundException", error.getErrorCode());+ assertEquals(404, error.getHttpStatus());+ assertEquals(message, error.getMessage());+ }+ }+ assertTrue(storage.scanAllAccounts().isEmpty());+ }++ @Test+ void missingTargetMessageUsesTheNamedAccountAndRegionalPartition() {+ AwsException error = assertThrows(AwsException.class,+ () -> pool.get("cn-north-1", "222222222222:function:absent", "live", ACCOUNT));+ assertEquals("Cannot find alias arn: arn:aws-cn:lambda:cn-north-1:222222222222:function:absent:live",+ error.getMessage());+ assertTrue(storage.scanAllAccounts().isEmpty());+ }++ @ParameterizedTest+ @ValueSource(strings = {"1", "peer"})+ void provisionedAliasRefusesAnotherQualifierOfTheSameVersion(String qualifier) {+ alias(ACCOUNT, REGION, "peer", "1", Map.of());+ put(1);+ AwsException error = assertThrows(AwsException.class,+ () -> pool.put(REGION, "example", qualifier, 1, ACCOUNT));+ assertEquals("ResourceConflictException", error.getErrorCode());+ assertEquals(qualifier.equals("1") ? "Version is pointed by a Provisioned Concurrency alias"+ : "Alias can't be used for Provisioned Concurrency configuration on an already Provisioned version",+ error.getMessage());+ assertEquals(1, storage.scanAllAccounts().size());+ }++ @Test+ void provisionedVersionRefusesAliasButAllowsADifferentVersion() {+ pool.put(REGION, "example", "1", 1, ACCOUNT);+ AwsException error = assertThrows(AwsException.class, () -> put(1));+ assertEquals("ResourceConflictException", error.getErrorCode());+ assertEquals("Alias can't be used for Provisioned Concurrency configuration on an already Provisioned version",+ error.getMessage());+ function(ACCOUNT, REGION, "2");+ pool.put(REGION, "example", "2", 1, ACCOUNT);+ assertEquals(2, storage.scanAllAccounts().size());+ }++ @Test+ void lastModifiedUsesLambdasUtcOffsetForm() {+ pool = new ProvisionedConcurrencyPool(functions, aliases, storage,+ Clock.fixed(Instant.parse("2026-10-20T20:11:30.987Z"), ZoneOffset.UTC), 4, 8);+ assertEquals("2026-10-20T20:11:30+0000", put(1).lastModified());+ }++ private ProvisionedConcurrencyPool.Status get() {+ return pool.get(REGION, "example", "live", ACCOUNT);+ }++ @ParameterizedTest+ @ValueSource(strings = {"persistent", "hybrid", "wal"})+ void storedConfigurationSurvivesFactoryRestartAndDeletedConfigurationStaysDeleted(String mode) {+ EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);+ when(config.defaultAccountId()).thenReturn(ACCOUNT);+ when(config.services().lambda().executor()).thenReturn("docker");+ when(config.storage().persistentPath()).thenReturn(storagePath.toString());+ when(config.storage().wal().compactionIntervalMs()).thenReturn(60_000L);+ ServiceConfigAccess access = mock(ServiceConfigAccess.class);+ when(access.storageMode("lambda")).thenReturn(mode);+ when(access.storageFlushInterval("lambda")).thenReturn(60_000L);+ for (int restart = 0; restart < 3; restart++) {+ StorageFactory factory = new StorageFactory(config, access);+ try {+ pool = new ProvisionedConcurrencyPool(functions, aliases, factory, config, Clock.systemUTC());+ pool.start();+ if (restart == 0) {+ put(2);+ } else if (restart == 1) {+ assertEquals(2, get().requested());+ assertEquals("IN_PROGRESS", get().status());+ assertEquals(0, get().allocated());+ pool.delete(REGION, "example", "live", ACCOUNT);+ } else {+ assertThrows(AwsException.class, this::get);+ }+ } finally {+ factory.shutdownAll();+ }+ }+ }++ @Test+ void unsupportedExecutorRejectsPutWithoutPersistingButExistingConfigurationCanBeRemoved() {+ put(1);+ pool = new ProvisionedConcurrencyPool(functions, aliases, storage, Clock.systemUTC(), 4, 8, false);+ pool.start();+ assertEquals(1, get().requested());+ AwsException failure = assertThrows(AwsException.class, () -> put(2));+ assertEquals("Provisioned concurrency requires the non-ephemeral Docker Lambda executor", failure.getMessage());+ assertEquals(1, get().requested());+ pool.delete(REGION, "example", "live", ACCOUNT);+ assertTrue(storage.scanAllAccounts().isEmpty());+ }++ @Test+ void replacementAndRestoreKeepDesiredCapacityWithoutClaimingReadiness() {+ ProvisionedConcurrencyPool.Status initial = put(1);+ assertEquals(initial, put(1));+ assertEquals(2, put(2).requested());+ pool = newPool();+ pool.start();+ ProvisionedConcurrencyPool.Status restored = get();+ assertEquals(2, restored.requested());+ assertEquals(0, restored.allocated());+ assertEquals(0, restored.available());+ assertEquals("IN_PROGRESS", restored.status());+ pool.delete(REGION, "example", "live", ACCOUNT);+ assertTrue(storage.scanAllAccounts().isEmpty());+ assertThrows(AwsException.class, this::get);+ }++ @Test+ void reservationsAndQualifiersShareTheFunctionCeiling() {+ LambdaFunction latest = functions.getForAccount(ACCOUNT, REGION, "example").orElseThrow();+ latest.setReservedConcurrentExecutions(2);+ assertThrows(AwsException.class, () -> put(3));+ put(1);+ function(ACCOUNT, REGION, "2");+ pool.put(REGION, "example", "2", 1, ACCOUNT);+ assertThrows(AwsException.class, () -> pool.validateReservation(latest, 1));+ pool.delete(REGION, "example", "2", ACCOUNT);+ assertDoesNotThrow(() -> pool.validateReservation(latest, 1));+ assertEquals(1, get().requested());+ }++ @Test+ void quotaErrorPrecedesVersionConflictAndRejectedReplacementPreservesTheRequest() {+ put(4);+ AwsException failure = assertThrows(AwsException.class,+ () -> pool.put(REGION, "example", "1", 1, ACCOUNT));+ assertEquals("InvalidParameterValueException", failure.getErrorCode());+ assertThrows(AwsException.class, () -> put(5));+ assertEquals(4, get().requested());+ }++ @Test+ void deletionAndResetRespectAccountAndRegionOwnership() {+ put(1);+ for (String account : List.of(ACCOUNT, "222222222222")) {+ function(account, "eu-west-1", "$LATEST");+ function(account, "eu-west-1", "1");+ alias(account, "eu-west-1", "live", "1", Map.of());+ pool.put("eu-west-1", "example", "live", 1, account);+ }+ pool.deleteFunction(version);+ assertThrows(AwsException.class, this::get);+ assertEquals(2, storage.scanAllAccounts().size());+ for (String account : List.of(ACCOUNT, "222222222222")) {+ assertEquals(1, pool.get("eu-west-1", "example", "live", account).requested());+ }+ pool.clear();+ assertTrue(storage.scanAllAccounts().isEmpty());+ pool.start();+ assertThrows(AwsException.class, () -> pool.get("eu-west-1", "example", "live", ACCOUNT));+ }++ @Test+ void weightedAliasLookupAndDeletionDoNotReapplyPutEligibility() {+ put(1);+ alias(ACCOUNT, REGION, "live", "1", Map.of("2", 0.1));+ assertEquals(1, get().requested());+ assertThrows(AwsException.class, () -> put(2));+ pool.delete(REGION, "example", "live", ACCOUNT);+ assertTrue(storage.scanAllAccounts().isEmpty());+ }++ @ParameterizedTest+ @ValueSource(booleans = {false, true})+ void configuredAliasCannotRetargetToCapacityOwnedByAnotherQualifier(boolean otherAlias) {+ function(ACCOUNT, REGION, "2");+ alias(ACCOUNT, REGION, "peer", "2", Map.of());+ put(1);+ String destination = otherAlias ? "peer" : "2";+ pool.put(REGION, "example", destination, 1, ACCOUNT);+ AwsException conflict = assertThrows(AwsException.class,+ () -> pool.validateAliasUpdate(version, "live", "2"));+ assertEquals("ResourceConflictException", conflict.getErrorCode());+ assertEquals(409, conflict.getHttpStatus());+ assertEquals("Alias can't be used for Provisioned Concurrency configuration on an already Provisioned version",+ conflict.getMessage());+ assertEquals(1, get().requested());+ assertEquals(2, storage.scanAllAccounts().size());+ assertDoesNotThrow(() -> pool.validateAliasUpdate(version, "live", null));+ assertDoesNotThrow(() -> pool.validateAliasUpdate(version, "live", "1"));+ assertDoesNotThrow(() -> pool.validateAliasUpdate(version, "unconfigured", "2"));+ pool.delete(REGION, "example", destination, ACCOUNT);+ assertDoesNotThrow(() -> pool.validateAliasUpdate(version, "live", "2"));+ }++ @Test+ void aliasCannotMoveToLatestWhileItHasRequestedCapacity() {+ put(1);+ assertThrows(AwsException.class, () -> pool.validateAliasUpdate(version, "live", "$LATEST"));+ pool.deleteQualifier(version, "live");+ assertDoesNotThrow(() -> pool.validateAliasUpdate(version, "live", "$LATEST"));+ }++ @Test+ void failedStorageMutationPreservesTheLastVisibleConfigurationForRetry() {+ AccountAwareStorageBackend<ProvisionedConcurrencyPool.Configuration> backend = spy(storage);+ pool = new ProvisionedConcurrencyPool(functions, aliases, backend, Clock.systemUTC(), 4, 8);+ put(1);+ IllegalStateException outage = new IllegalStateException("storage unavailable");+ doThrow(outage).when(backend)+ .putForAccount(any(), any(), any());+ assertSame(outage, assertThrows(IllegalStateException.class, () -> put(2)));+ assertEquals(1, get().requested());+ doThrow(outage).when(backend)+ .deleteForAccount(any(), any());+ assertSame(outage, assertThrows(IllegalStateException.class,+ () -> pool.delete(REGION, "example", "live", ACCOUNT)));+ assertEquals(1, get().requested());+ doCallRealMethod().when(backend)+ .deleteForAccount(any(), any());+ pool.delete(REGION, "example", "live", ACCOUNT);+ assertThrows(AwsException.class, this::get);+ }++ @ParameterizedTest+ @ValueSource(booleans = {false, true})+ void putRechecksDeletionAndReservationAfterAcquiringTheSharedLock(boolean deleted) throws Exception {+ AtomicReference<Thread> requester = new AtomicReference<>();+ try (ExecutorService tasks = Executors.newSingleThreadExecutor()) {+ Future<?> put;+ synchronized (functions.lockForConcurrencyOp(functions.getForAccount(ACCOUNT, REGION, "example").orElseThrow().getFunctionArn())) {+ put = tasks.submit(() -> {+ requester.set(Thread.currentThread());+ assertThrows(AwsException.class, () -> put(1));+ });+ awaitBlockedOrDone(requester, put);+ assertFalse(put.isDone());+ if (deleted) {+ functions.delete(REGION, "example");+ } else {+ functions.getForAccount(ACCOUNT, REGION, "example").orElseThrow()+ .setReservedConcurrentExecutions(0);+ }+ }+ put.get(5, TimeUnit.SECONDS);+ }+ assertTrue(storage.scanAllAccounts().isEmpty());+ }+}