feat(lambda): initialize provisioned concurrency environments (#6320)

Initialize and route qualifier-owned provisioned environments without invoking handlers during initialization. Keep configuration outcomes committed while failed removal remains owned for retry, and bound cleanup after late invocation release. Preserve on-demand spillover and the separate alias-retargeting stage.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-23 06:35:02 -04:00
1 parent f60cf120c6
commit 238d16e541
15 files changed
+2061 -248

No files matched your search

@@ -3004,7 +3004,7 @@ public interface EmulatorConfig {
int containerIdleTimeoutSeconds();
/**
* One shutdown drain budget shared by on-demand Lambda containers.
* One shutdown drain budget shared by provisioned and on-demand Lambda containers.
* Values outside 1 through 300 seconds fail initialization.
*
* Env var: FLOCI_SERVICES_LAMBDA_CONTAINER_DRAIN_TIMEOUT_SECONDS
@@ -233,7 +233,7 @@ public class LambdaExecutorService implements Resettable {
}
try {
return executeSync(fn, payload, requestId, clientContext);
return executeSync(fn, payload, requestId, clientContext, invokedQualifier);
} finally {
permit.close();
}
@@ -289,7 +289,7 @@ public class LambdaExecutorService implements Resettable {
}
InvokeResult result;
try (permit) {
result = executeSync(event.fn(), event.payload(), event.requestId(), null);
result = executeSync(event.fn(), event.payload(), event.requestId(), null, event.invokedQualifier());
} catch (RuntimeException e) {
LOG.warnv("Error in async Lambda execution for {0}: {1}", event.fn().getFunctionName(), e.getMessage());
// Stops retrying, as upstream did: routes the previous attempt's result, or UnknownError when
@@ -347,10 +347,13 @@ public class LambdaExecutorService implements Resettable {
}
private InvokeResult executeSync(LambdaFunction fn, byte[] payload, String requestId,
String clientContext) {
String clientContext, String invokedQualifier) {
ContainerHandle handle;
try {
handle = warmPool.acquire(fn);
handle = warmPool.acquireProvisioned(fn, invokedQualifier);
if (handle == null) {
handle = warmPool.acquire(fn);
}
} catch (Exception e) {
LOG.warnv("Failed to acquire container for function {0}: {1}", fn.getFunctionName(), e.getMessage());
return new InvokeResult(200, "Unhandled",
@@ -4,41 +4,72 @@ 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.ContainerTeardowns;
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.launcher.ContainerHandle;
import io.github.hectorvent.floci.services.lambda.launcher.LambdaRuntimeLauncher;
import io.github.hectorvent.floci.services.lambda.model.ContainerState;
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 org.jboss.logging.Logger;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/** Stores desired capacity. Runtime allocation is introduced by the next stage. */
/**
* Owns initialized environments separately from the on-demand idle pool.
* Persisted requests never imply readiness: every new process must finish real initialization.
*/
@ApplicationScoped
public class ProvisionedConcurrencyPool implements Resettable {
private static final Logger LOG = Logger.getLogger(ProvisionedConcurrencyPool.class);
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 LambdaRuntimeLauncher launcher;
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<>();
private final boolean ephemeral;
private final long drainTimeoutNanos;
private final Map<String, Allocation> allocations = new HashMap<>();
private final Map<ContainerHandle, Allocation> leases = new HashMap<>();
private final Map<ContainerHandle, Configuration> owned = new HashMap<>();
private final Set<Allocation> launchingAllocations = new HashSet<>();
private final Set<ContainerHandle> pendingTeardown = new HashSet<>();
private final Map<ContainerHandle, CountDownLatch> stopping = new HashMap<>();
private final ExecutorService workers = Executors.newVirtualThreadPerTaskExecutor();
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(
Thread.ofPlatform().daemon().name("lambda-provisioned-concurrency").factory());
private boolean stopped;
private int activeLaunches;
private long shutdownDeadlineNanos;
@RegisterForReflection
public record Configuration(String account, String region, String name, String functionArn,
@@ -52,32 +83,47 @@ public class ProvisionedConcurrencyPool implements Resettable {
public record Status(int requested, int allocated, int available, String status,
String reason, String lastModified) {}
private static final class Allocation {
final Configuration configuration;
final Set<ContainerHandle> handles = new HashSet<>();
final ArrayDeque<ContainerHandle> idle = new ArrayDeque<>();
int launching;
String status = "IN_PROGRESS";
String reason;
Allocation(Configuration configuration) {
this.configuration = configuration;
}
}
private static final class TargetUnavailableException extends RuntimeException {
TargetUnavailableException() {
super("The configured function version or alias is no longer available");
}
}
@Inject
public ProvisionedConcurrencyPool(LambdaFunctionStore functions, LambdaAliasStore aliases,
StorageFactory storageFactory, EmulatorConfig config, Clock clock) {
this(functions, aliases, storageFactory.create("lambda",
public ProvisionedConcurrencyPool(LambdaRuntimeLauncher launcher, LambdaFunctionStore functions,
LambdaAliasStore aliases, StorageFactory storageFactory,
EmulatorConfig config, Clock clock) {
this(launcher, 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()));
config.services().lambda().ephemeral(), WarmPool.resolveDrainTimeout(config));
}
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;
ProvisionedConcurrencyPool(LambdaRuntimeLauncher launcher, LambdaFunctionStore functions,
LambdaAliasStore aliases, StorageBackend<String, Configuration> storage,
Clock clock, int maxPerFunction, int maxTotal, boolean ephemeral, int drainSeconds) {
this.launcher = launcher;
this.functions = functions;
this.aliases = aliases;
this.storage = storage;
this.clock = clock;
this.maxPerFunction = maxPerFunction;
this.maxTotal = maxTotal;
this.ephemeral = ephemeral;
this.drainTimeoutNanos = TimeUnit.SECONDS.toNanos(drainSeconds);
}
@PostConstruct
@@ -86,9 +132,16 @@ public class ProvisionedConcurrencyPool implements Resettable {
List<Configuration> stored = storage instanceof AccountAwareStorageBackend<Configuration> aware
? aware.scanAllAccounts() : storage.scan(ignored -> true);
for (Configuration configuration : stored) {
configurations.put(configuration.key(), configuration);
allocations.put(configuration.key(), new Allocation(configuration));
}
}
scheduler.scheduleWithFixedDelay(() -> {
try {
reconcileSafely();
} catch (RuntimeException failure) {
LOG.errorv(failure, "Provisioned concurrency reconciliation failed");
}
}, 0, 1, TimeUnit.SECONDS);
}
public Status put(String region, String name, String qualifier, int count, String account) {
@@ -106,27 +159,35 @@ public class ProvisionedConcurrencyPool implements Resettable {
private Status put(Configuration target) {
int count = target.requested();
if (!supported) {
if (ephemeral || !launcher.supportsProvisionedConcurrency()) {
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"));
LambdaFunction fn = currentFunction(target);
if (fn.isHotReload() || fn.isDurable()) {
throw invalid("Provisioned concurrency is not supported for hot-reload or durable functions");
}
List<ContainerHandle> retired = List.of();
Status result;
synchronized (lock) {
Configuration previous = configurations.get(target.key());
if (stopped) {
throw new AwsException("ResourceConflictException", "Lambda executor is stopping", 409);
}
if (hasPendingTeardown(target)) {
throw new AwsException("ResourceConflictException",
"The previous provisioned allocation is still being removed", 409);
}
Allocation previous = allocations.get(target.key());
long otherFunction = 0;
long otherTotal = 0;
boolean versionConflict = false;
for (Configuration entry : configurations.values()) {
for (Allocation entry : allocations.values()) {
if (entry == previous) {
continue;
}
otherTotal += entry.requested();
if (target.functionArn().equals(entry.functionArn())) {
versionConflict |= target.version().equals(pointedVersion(entry));
otherFunction += entry.requested();
otherTotal += entry.configuration.requested();
if (target.functionArn().equals(entry.configuration.functionArn())) {
versionConflict |= target.version().equals(pointedVersion(entry.configuration));
otherFunction += entry.configuration.requested();
}
}
if (otherFunction + count > maxPerFunction
@@ -146,24 +207,33 @@ public class ProvisionedConcurrencyPool implements Resettable {
: "Alias can't be used for Provisioned Concurrency configuration "
+ "on an already Provisioned version", 409);
}
if (previous != null && sameTarget(previous, target) && previous.requested() == count) {
if (previous != null && sameTarget(previous.configuration, target)
&& previous.configuration.requested() == count && !"FAILED".equals(previous.status)) {
return status(previous);
}
write(target);
configurations.put(target.key(), target);
return status(target);
Allocation next = new Allocation(target);
allocations.put(target.key(), next);
if (previous != null) {
retired = retire(previous);
}
result = status(next);
}
stop(retired);
reconcile();
return result;
}
public Status get(String region, String name, String qualifier, String account) {
Target target = resolveTarget(region, name, qualifier, account);
reconcile();
synchronized (lock) {
Configuration configuration = configurations.get(target.key());
if (configuration == null) {
Allocation allocation = allocations.get(target.key());
if (allocation == null) {
throw new AwsException("ProvisionedConcurrencyConfigNotFoundException",
"No Provisioned Concurrency Config found for this function", 404);
}
return status(configuration);
return status(allocation);
}
}
@@ -175,6 +245,71 @@ public class ProvisionedConcurrencyPool implements Resettable {
}
}
/** Returns null when the matching allocation has no idle capacity: on-demand execution may spill over. */
ContainerHandle acquire(LambdaFunction fn, String qualifier) {
if (qualifier == null) {
return null;
}
String baseArn = baseArn(fn);
List<ContainerHandle> discarded = new ArrayList<>();
ContainerHandle selected = null;
synchronized (lock) {
Allocation allocation = allocations.get(baseArn + ":" + qualifier);
if (allocation == null || !allocation.configuration.version().equals(fn.getVersion())) {
return null;
}
try {
currentFunction(allocation.configuration);
} catch (TargetUnavailableException changed) {
return null;
}
while (!allocation.idle.isEmpty()) {
ContainerHandle candidate = allocation.idle.removeFirst();
if (healthy(candidate) && current(allocation)) {
selected = candidate;
leases.put(candidate, allocation);
candidate.setState(ContainerState.BUSY);
break;
}
allocation.handles.remove(candidate);
pendingTeardown.add(candidate);
discarded.add(candidate);
}
}
stop(discarded);
return selected;
}
boolean release(ContainerHandle handle, boolean failed) {
boolean retire;
synchronized (lock) {
Allocation allocation = leases.remove(handle);
if (allocation == null) {
return false;
}
retire = failed || !current(allocation) || !healthy(handle);
if (retire) {
allocation.handles.remove(handle);
pendingTeardown.add(handle);
} else {
handle.setState(ContainerState.WARM);
allocation.idle.addLast(handle);
}
}
if (retire) {
boolean shutdownDrain;
long deadline;
synchronized (lock) {
shutdownDrain = stopped;
long now = System.nanoTime();
deadline = stopped && shutdownDeadlineNanos > now
? shutdownDeadlineNanos : now + drainTimeoutNanos;
}
stop(List.of(handle), shutdownDrain, deadline);
}
return true;
}
void deleteFunction(LambdaFunction fn) {
deleteMatching(baseArn(fn), null);
}
@@ -188,9 +323,9 @@ public class ProvisionedConcurrencyPool implements Resettable {
return;
}
synchronized (lock) {
long requested = configurations.values().stream()
.filter(entry -> entry.functionArn().equals(baseArn(fn)))
.mapToLong(entry -> entry.requested()).sum();
long requested = allocations.values().stream()
.filter(entry -> entry.configuration.functionArn().equals(baseArn(fn)))
.mapToLong(entry -> entry.configuration.requested()).sum();
if (requested > reserved) {
throw invalid("Reserved concurrency cannot be lower than provisioned concurrency");
}
@@ -198,19 +333,96 @@ public class ProvisionedConcurrencyPool implements Resettable {
}
private void deleteMatching(String functionArn, String qualifier) {
List<ContainerHandle> retired = new ArrayList<>();
synchronized (lock) {
for (Configuration value : new ArrayList<>(configurations.values())) {
for (Allocation entry : new ArrayList<>(allocations.values())) {
Configuration value = entry.configuration;
if (value.functionArn().equals(functionArn)
&& (qualifier == null || qualifier.equals(value.qualifier()))) {
erase(value);
configurations.remove(value.key());
allocations.remove(value.key());
retired.addAll(retire(entry));
}
}
for (ContainerHandle handle : pendingTeardown) {
Configuration value = owned.get(handle);
if (value.functionArn().equals(functionArn)
&& (qualifier == null || qualifier.equals(value.qualifier()))) {
retired.add(handle);
}
}
}
stop(retired);
}
private Status status(Configuration configuration) {
return new Status(configuration.requested(), 0, 0, "IN_PROGRESS", null, configuration.lastModified());
private boolean hasPendingTeardown(Configuration configuration) {
return pendingTeardown.stream().anyMatch(handle ->
configuration.key().equals(owned.get(handle).key()));
}
void reconcileSafely() {
List<ContainerHandle> retry;
synchronized (lock) {
if (stopped) {
return;
}
retry = new ArrayList<>(pendingTeardown);
}
stop(retry);
reconcile();
}
void reconcile() {
List<ContainerHandle> retired = new ArrayList<>();
synchronized (lock) {
if (stopped) {
return;
}
for (Allocation allocation : new ArrayList<>(allocations.values())) {
if ("FAILED".equals(allocation.status)) {
continue;
}
LambdaFunction fn;
try {
fn = currentFunction(allocation.configuration);
} catch (TargetUnavailableException failure) {
allocation.status = "FAILED";
allocation.reason = "The configured function version or alias is no longer available";
retired.addAll(retire(allocation));
continue;
}
for (ContainerHandle handle : new ArrayList<>(allocation.idle)) {
if (!healthy(handle)) {
allocation.idle.remove(handle);
allocation.handles.remove(handle);
pendingTeardown.add(handle);
retired.add(handle);
}
}
if (hasPendingTeardown(allocation.configuration)) {
continue;
}
int missing = allocation.configuration.requested()
- allocation.handles.size() - allocation.launching;
missing = Math.min(missing, availableCapacity(allocation.configuration));
if (missing > 0) {
allocation.status = "IN_PROGRESS";
for (int i = 0; i < missing; i++) {
allocation.launching++;
launchingAllocations.add(allocation);
activeLaunches++;
try {
workers.execute(() -> initialize(allocation, fn));
} catch (RejectedExecutionException failure) {
finishedLaunching(allocation);
activeLaunches--;
throw failure;
}
}
}
}
}
stop(retired);
}
private String pointedVersion(Configuration configuration) {
@@ -225,22 +437,212 @@ public class ProvisionedConcurrencyPool implements Resettable {
void validateAliasUpdate(LambdaFunction fn, String qualifier, String version) {
synchronized (lock) {
String functionArn = baseArn(fn);
if (!configurations.containsKey(functionArn + ":" + qualifier)) {
if (!allocations.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)))) {
if (version != null && version.matches("[0-9]+") && allocations.values().stream()
.anyMatch(entry -> functionArn.equals(entry.configuration.functionArn())
&& !qualifier.equals(entry.configuration.qualifier())
&& version.equals(pointedVersion(entry.configuration)))) {
throw new AwsException("ResourceConflictException",
"Alias can't be used for Provisioned Concurrency configuration on an already Provisioned version", 409);
}
}
}
private int availableCapacity(Configuration configuration) {
long functionOwned = owned.values().stream()
.filter(value -> value.functionArn().equals(configuration.functionArn())).count();
long functionLaunching = launchingAllocations.stream()
.filter(value -> value.configuration.functionArn().equals(configuration.functionArn()))
.mapToLong(value -> value.launching).sum();
long available = maxPerFunction - functionOwned - functionLaunching;
if (maxTotal > 0) {
long launching = launchingAllocations.stream().mapToLong(value -> value.launching).sum();
available = Math.min(available, maxTotal - owned.size() - launching);
}
return (int) Math.max(0, available);
}
private void finishedLaunching(Allocation allocation) {
allocation.launching--;
if (allocation.launching == 0) {
launchingAllocations.remove(allocation);
}
}
private void initialize(Allocation allocation, LambdaFunction fn) {
ContainerHandle handle = null;
boolean retained = false;
List<ContainerHandle> retired = List.of();
try {
handle = launcher.launchProvisioned(fn);
synchronized (lock) {
owned.put(handle, allocation.configuration);
finishedLaunching(allocation);
if (!current(allocation)) {
return;
}
allocation.handles.add(handle);
}
long timeoutMs = Math.max(130, fn.getTimeout()) * 1000L;
handle.getRuntimeApiServer().awaitInitialization(timeoutMs);
synchronized (lock) {
currentFunction(allocation.configuration);
if (current(allocation) && healthy(handle)) {
allocation.idle.addLast(handle);
retained = true;
if (allocation.handles.size() == allocation.configuration.requested()
&& allocation.handles.stream().allMatch(this::healthy)) {
allocation.status = "READY";
}
}
}
} catch (Exception failure) {
synchronized (lock) {
if (handle == null) {
finishedLaunching(allocation);
}
if (current(allocation)) {
allocation.status = "FAILED";
allocation.reason = "Execution environment initialization failed: "
+ failure.getClass().getSimpleName()
+ (failure.getMessage() == null ? "" : ": " + failure.getMessage());
retired = retire(allocation);
}
}
LOG.warnv(failure, "Provisioned Lambda initialization failed");
if (failure instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
} finally {
Set<ContainerHandle> toStop = new HashSet<>(retired);
if (!retained && handle != null) {
synchronized (lock) {
allocation.handles.remove(handle);
allocation.idle.remove(handle);
}
toStop.add(handle);
}
boolean shutdownDrain;
long deadline;
synchronized (lock) {
shutdownDrain = stopped;
long now = System.nanoTime();
deadline = stopped && shutdownDeadlineNanos > now
? shutdownDeadlineNanos : now + drainTimeoutNanos;
}
try {
stop(new ArrayList<>(toStop), shutdownDrain, deadline);
} finally {
synchronized (lock) {
activeLaunches--;
lock.notifyAll();
}
}
}
}
private Status status(Allocation allocation) {
int healthy = (int) allocation.handles.stream().filter(this::healthy).count();
int available = (int) allocation.idle.stream().filter(this::healthy).count();
String state = allocation.status;
if ("READY".equals(state) && healthy != allocation.configuration.requested()) {
state = "IN_PROGRESS";
}
return new Status(allocation.configuration.requested(), healthy, available, state,
allocation.reason, allocation.configuration.lastModified());
}
private boolean healthy(ContainerHandle handle) {
return handle.getState() != ContainerState.STOPPED
&& handle.getRuntimeApiServer().isInitialized();
}
private boolean current(Allocation allocation) {
return !stopped && allocations.get(allocation.configuration.key()) == allocation
&& !"FAILED".equals(allocation.status);
}
private List<ContainerHandle> retire(Allocation allocation) {
List<ContainerHandle> result = allocation.handles.stream()
.filter(handle -> !leases.containsKey(handle)).toList();
allocation.handles.clear();
allocation.idle.clear();
pendingTeardown.addAll(result);
return result;
}
private void stop(List<ContainerHandle> handles) {
stop(handles, false);
}
private void stop(List<ContainerHandle> handles, boolean shutdownDrain) {
stop(handles, shutdownDrain, System.nanoTime() + drainTimeoutNanos);
}
private void stop(List<ContainerHandle> handles, boolean shutdownDrain, long deadlineNanos) {
handles = handles.stream().distinct().toList();
synchronized (lock) {
for (ContainerHandle handle : handles) {
if (owned.containsKey(handle)) {
pendingTeardown.add(handle);
handle.setState(ContainerState.STOPPED);
}
}
if (stopped && !shutdownDrain) {
return;
}
}
ContainerTeardowns.stopInParallel(handles, handle -> stop(handle, shutdownDrain, deadlineNanos),
LOG, deadlineNanos);
}
private void stop(ContainerHandle handle, boolean shutdownDrain, long deadlineNanos) {
CountDownLatch completion;
boolean alreadyStopping;
synchronized (lock) {
if (!owned.containsKey(handle) || (stopped && !shutdownDrain)) {
return;
}
completion = stopping.get(handle);
alreadyStopping = completion != null;
if (!alreadyStopping) {
completion = new CountDownLatch(1);
stopping.put(handle, completion);
}
}
if (alreadyStopping) {
// An admitted retry owns this stop. Shutdown joins it within the same bounded
// drain instead of returning while it runs, or issuing another Docker stop.
if (shutdownDrain) {
try {
completion.await(Math.max(0, deadlineNanos - System.nanoTime()), TimeUnit.NANOSECONDS);
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
}
}
return;
}
try {
launcher.stop(handle);
synchronized (lock) {
pendingTeardown.remove(handle);
owned.remove(handle);
}
} catch (RuntimeException failure) {
LOG.warnv(failure, "Could not stop provisioned Lambda environment {0}", handle.getContainerId());
} finally {
synchronized (lock) {
stopping.remove(handle);
}
completion.countDown();
}
}
private record Target(String account, String region, String name, String functionArn,
String qualifier, LambdaAlias alias) {
String key() {
@@ -298,6 +700,25 @@ public class ProvisionedConcurrencyPool implements Resettable {
return new Target(account, region, ref.name(), baseArn(latest), qualifier, alias);
}
private LambdaFunction currentFunction(Configuration configuration) {
functions.getForAccount(configuration.account(), configuration.region(), configuration.name())
.orElseThrow(TargetUnavailableException::new);
if (!configuration.qualifier().matches("[0-9]+")) {
LambdaAlias alias = aliases.getForAccount(configuration.account(), configuration.region(),
configuration.name(), configuration.qualifier()).orElseThrow(TargetUnavailableException::new);
if (!configuration.version().equals(alias.getFunctionVersion())
|| (alias.getRoutingConfig() != null && !alias.getRoutingConfig().isEmpty())) {
throw new TargetUnavailableException();
}
}
LambdaFunction fn = functions.getForAccount(configuration.account(), configuration.region(),
configuration.name(), configuration.version()).orElseThrow(TargetUnavailableException::new);
if (!Objects.equals(configuration.revision(), fn.getRevisionId())) {
throw new TargetUnavailableException();
}
return fn;
}
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());
@@ -327,9 +748,66 @@ public class ProvisionedConcurrencyPool implements Resettable {
@Override
public void clear() {
stopManagedContainers();
synchronized (lock) {
storage.clear();
configurations.clear();
stopped = false;
}
}
public void stopManagedContainers() {
stopManagedContainers(System.nanoTime() + drainTimeoutNanos);
}
void stopManagedContainers(long deadlineNanos) {
List<ContainerHandle> retired;
synchronized (lock) {
if (stopped) {
return;
}
stopped = true;
shutdownDeadlineNanos = deadlineNanos;
allocations.values().forEach(this::retire);
allocations.clear();
retired = new ArrayList<>(owned.keySet());
}
stop(retired, true, deadlineNanos);
synchronized (lock) {
try {
long remaining;
while (activeLaunches > 0 && (remaining = deadlineNanos - System.nanoTime()) > 0) {
TimeUnit.NANOSECONDS.timedWait(lock, remaining);
}
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
}
if (activeLaunches > 0) {
LOG.warnv("Shutdown deadline reached with {0} provisioned Lambda launch(es) still pending",
activeLaunches);
}
}
synchronized (lock) {
if (!owned.isEmpty()) {
LOG.warnv("Shutdown drain ended with {0} provisioned Lambda environment(s) still owned",
owned.size());
}
}
}
void shutdown() {
stopManagedContainers();
closeExecutors();
}
void closeExecutors() {
scheduler.shutdownNow();
workers.shutdownNow();
}
@Override
public void afterReset() {
synchronized (lock) {
stopped = false;
}
}
@@ -95,8 +95,8 @@ public class WarmPool implements ContainerTeardown, Resettable {
@Inject
public WarmPool(LambdaRuntimeLauncher lambdaRuntimeLauncher, EmulatorConfig config, Clock clock,
ProvisionedConcurrencyPool provisioned) {
this.provisioned = provisioned;
this.lambdaRuntimeLauncher = lambdaRuntimeLauncher;
this.provisioned = provisioned;
this.config = config;
this.clock = clock;
this.maxPoolSizePerFunction = resolveMaxPerFunction(config);
@@ -195,13 +195,24 @@ public class WarmPool implements ContainerTeardown, Resettable {
pendingTeardown.addAll(knownHandles);
pendingTeardown.removeIf(handle -> !owned.contains(handle));
}
drainAll(shutdownState, knownHandles);
List<Runnable> drains = new ArrayList<>();
if (provisioned != null) {
drains.add(() -> provisioned.stopManagedContainers(shutdownState.deadlineNanos));
}
drains.add(() -> drainAll(shutdownState, knownHandles));
ContainerTeardowns.stopInParallel(drains, Runnable::run, LOG, shutdownState.deadlineNanos);
}
@PreDestroy
void shutdown() {
evictionScheduler.shutdownNow();
stopManagedContainers();
try {
stopManagedContainers();
} finally {
if (provisioned != null) {
provisioned.closeExecutors();
}
}
}
@Override
@@ -330,6 +341,9 @@ public class WarmPool implements ContainerTeardown, Resettable {
* Otherwise it is returned to the warm pool.
*/
public void release(ContainerHandle handle) {
if (provisioned != null && provisioned.release(handle, false)) {
return;
}
Lease lease = activeLeases.remove(handle);
if (!owned.contains(handle)) {
return;
@@ -449,12 +463,19 @@ public class WarmPool implements ContainerTeardown, Resettable {
* stop is needed — no pool bookkeeping required.
*/
public void destroyHandle(ContainerHandle handle) {
if (provisioned != null && provisioned.release(handle, true)) {
return;
}
activeLeases.remove(handle);
LOG.debugv("Destroying timed-out container {0} for function {1}",
handle.getContainerId(), handle.getFunctionName());
stopQuietly(handle);
}
ContainerHandle acquireProvisioned(LambdaFunction fn, String qualifier) {
return provisioned == null ? null : provisioned.acquire(fn, qualifier);
}
void deleteProvisionedFunction(LambdaFunction fn) {
if (provisioned != null) {
provisioned.deleteFunction(fn);
@@ -153,6 +153,20 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
}
public ContainerHandle launch(LambdaFunction fn) {
return launch(fn, false);
}
@Override
public boolean supportsProvisionedConcurrency() {
return true;
}
@Override
public ContainerHandle launchProvisioned(LambdaFunction fn) {
return launch(fn, true);
}
private ContainerHandle launch(LambdaFunction fn, boolean provisioned) {
LOG.infov("Launching container for function: {0}", fn.getFunctionName());
// For Zip functions, verify code exists before allocating any resources.
@@ -266,6 +280,10 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
env.removeIf(value -> value.startsWith("AWS_LAMBDA_FUNCTION_VERSION="));
env.add("AWS_LAMBDA_FUNCTION_VERSION=" + fn.getVersion());
if (provisioned) {
env.removeIf(value -> value.startsWith("AWS_LAMBDA_INITIALIZATION_TYPE="));
env.add("AWS_LAMBDA_INITIALIZATION_TYPE=provisioned-concurrency");
}
ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(image)
.withName(containerName)
@@ -391,6 +409,11 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
for (String layerArn : fn.getLayers()) {
LambdaLayerVersion layer = layerService.resolveLayerByArnForAccount(
layerArn, lambdaAccountId, lambdaRegion);
if (provisioned && (layer == null || layer.getCodeLocalPath() == null
|| !Files.isDirectory(Path.of(layer.getCodeLocalPath())))) {
throw new IllegalStateException("Configured layer is unavailable for provisioned initialization: "
+ layerArn);
}
if (layer != null && layer.getCodeLocalPath() != null) {
Path layerPath = Path.of(layer.getCodeLocalPath());
if (Files.exists(layerPath)) {
@@ -432,7 +455,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
// (e.g. aws-lambda-web-adapter) never start without this. Best-effort: an extension
// launch failure shouldn't fail the whole container launch, since a function with no
// extensions is the common case and this must be a no-op for it.
launchExtensions(dockerClient, containerId, fn.getFunctionName(), runtimeApiServer, logDestination);
launchExtensions(dockerClient, containerId, fn.getFunctionName(), runtimeApiServer, logDestination, provisioned);
// Init-readiness barrier: the execs above are detached, so without waiting here the caller
// could enqueue the first invocation before an extension is ready to receive it — the
@@ -441,7 +464,9 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
// /extension/event/next rather than its register call. A no-extensions function (the
// common case) does not wait at all; a timeout is non-fatal, since a container that serves
// invocations without a slow extension is strictly better than failing the launch.
awaitExtensionReadiness(runtimeApiServer, fn.getFunctionName());
if (!provisioned) {
awaitExtensionReadiness(runtimeApiServer, fn.getFunctionName());
}
ContainerHandle handle = new ContainerHandle(
containerId, fn.getFunctionName(), runtimeApiServer, ContainerState.WARM, fn.isHotReload(),
@@ -1372,8 +1397,8 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
}
private void launchExtensions(DockerClient dockerClient, String containerId, String functionName,
RuntimeApiServer runtimeApiServer, LogDestination logDestination) {
List<String> extensionNames = listExtensionBinaries(dockerClient, containerId, functionName);
RuntimeApiServer runtimeApiServer, LogDestination logDestination, boolean strict) {
List<String> extensionNames = listExtensionBinaries(dockerClient, containerId, functionName, strict);
// Arm the readiness barrier before starting any extension process, so the latch already
// exists when the first one starts polling /extension/event/next — otherwise a
// fast-starting extension could become ready before there is anything to count it down.
@@ -1402,6 +1427,9 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
LOG.infov("Launched extension {0} for function {1} (container {2})",
name, functionName, containerId);
} catch (Exception e) {
if (strict) {
throw new IllegalStateException("Could not launch a provisioned Lambda extension", e);
}
LOG.warnv(e, "Failed to launch extension {0} for function {1}; continuing without it",
name, functionName);
}
@@ -1442,7 +1470,8 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
* those utilities present, which would silently look like "no extensions" to a shell-based probe.
* The archive API only talks to the Docker daemon, so it works regardless of the image contents.
*/
private List<String> listExtensionBinaries(DockerClient dockerClient, String containerId, String functionName) {
private List<String> listExtensionBinaries(DockerClient dockerClient, String containerId,
String functionName, boolean strict) {
try (InputStream tarStream = dockerClient.copyArchiveFromContainerCmd(containerId, EXTENSIONS_DIR).exec();
TarArchiveInputStream tar = new TarArchiveInputStream(tarStream)) {
@@ -1469,6 +1498,9 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
} catch (NotFoundException e) {
return List.of();
} catch (Exception e) {
if (strict) {
throw new IllegalStateException("Could not discover provisioned Lambda extensions", e);
}
LOG.debugv("Could not list {0} for function {1} ({2}); assuming no extensions",
EXTENSIONS_DIR, functionName, e.getMessage());
return List.of();
@@ -12,6 +12,14 @@ public interface LambdaRuntimeLauncher {
ContainerHandle launch(LambdaFunction fn);
default boolean supportsProvisionedConcurrency() {
return false;
}
default ContainerHandle launchProvisioned(LambdaFunction fn) {
throw new UnsupportedOperationException("Provisioned concurrency is not supported by this Lambda executor");
}
void stop(ContainerHandle handle);
boolean isAlive(ContainerHandle handle);
@@ -59,6 +59,10 @@ public class RegisteredExtension {
return waitingContext != null;
}
public boolean hasReceivedFirstNext() {
return firstNextReceived;
}
/**
* Records that this extension has issued its first {@code /extension/event/next}, which is
* what AWS treats as the extension being init-ready (registering alone only obtains an
@@ -24,6 +24,7 @@ import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
@@ -111,6 +112,8 @@ public class RuntimeApiServer {
// Guards stopped, pendingQueue, waitingContexts, extensions, closeFuture, and every
// RegisteredExtension's pendingEvents/waitingContext. See class doc for the discipline.
private final Object lock = new Object();
private final CompletableFuture<Void> initialization = new CompletableFuture<>();
private boolean runtimeNextReceived;
// Invocations queued before a /next poller arrived. Guarded by lock.
private final ArrayDeque<PendingInvocation> pendingQueue = new ArrayDeque<>();
@@ -290,6 +293,32 @@ public class RuntimeApiServer {
return faulted;
}
/** Waits for runtime and extension initialization, without enqueuing an invocation. */
public void awaitInitialization(long timeoutMs) throws Exception {
initialization.get(timeoutMs, TimeUnit.MILLISECONDS);
synchronized (lock) {
if (stopped || faulted) {
throw new IllegalStateException("Lambda execution environment is no longer ready");
}
}
}
public boolean isInitialized() {
synchronized (lock) {
return initialization.isDone() && !initialization.isCompletedExceptionally() && !stopped && !faulted;
}
}
private void completeInitializationIfReady() {
synchronized (lock) {
CountDownLatch latch = extensionsReady;
if (runtimeNextReceived && !stopped && !faulted && latch != null && latch.getCount() == 0
&& extensions.values().stream().allMatch(RegisteredExtension::hasReceivedFirstNext)) {
initialization.complete(null);
}
}
}
/**
* Declares how many extension binaries were launched for this container, arming the
* init-readiness barrier that {@link #awaitExtensionsReady(long)} waits on.
@@ -299,7 +328,10 @@ public class RuntimeApiServer {
* no {@code /opt/extensions} directory) leaves the barrier permanently open.
*/
public void expectExtensions(int count) {
extensionsReady = new CountDownLatch(Math.max(0, count));
synchronized (lock) {
extensionsReady = new CountDownLatch(Math.max(0, count));
completeInitializationIfReady();
}
}
/**
@@ -363,6 +395,8 @@ public class RuntimeApiServer {
// AWS Lambda tears an environment down.
waitingContexts.add(ctx);
} else {
runtimeNextReceived = true;
completeInitializationIfReady();
toDispatch = pendingQueue.poll();
if (toDispatch == null) {
waitingContexts.add(ctx);
@@ -525,6 +559,7 @@ public class RuntimeApiServer {
if (readyLatch != null) {
readyLatch.countDown();
}
completeInitializationIfReady();
if (unknownExtension) {
ctx.response().setStatusCode(403)
.putHeader("Content-Type", "application/json")
@@ -606,6 +641,7 @@ public class RuntimeApiServer {
return;
}
stopped = true;
initialization.completeExceptionally(new IllegalStateException("Lambda execution environment stopped"));
extensionWritesFlushed = writesFuture;
ExtensionEvent shutdownEvent = ExtensionEvent.shutdown(System.currentTimeMillis() + 2000, "SPINDOWN");
@@ -911,6 +947,7 @@ public class RuntimeApiServer {
List<PendingInvocation> stranded;
synchronized (lock) {
faulted = true;
initialization.completeExceptionally(new IllegalStateException("Lambda runtime initialization failed"));
// Retained for a possible later enqueue(): the invocation that triggered this cold
// start may not exist yet (see fatalRuntimeErrorPayload's doc), in which case
// `stranded` below is empty and this is the only place the real error survives.
@@ -1037,6 +1074,7 @@ public class RuntimeApiServer {
return;
}
faulted = true;
initialization.completeExceptionally(new IllegalStateException("Lambda runtime exited: " + exitCode));
// Retained for a possible later enqueue(): the invocation that would have hit this
// container may not exist yet (see fatalRuntimeErrorPayload's doc), so this generic,
// requestId-free payload is built here rather than only per-stranded-invocation below.
@@ -1105,6 +1143,7 @@ public class RuntimeApiServer {
// Set under the lock alongside the unregistration so both land as one atomic
// change.
faulted = true;
initialization.completeExceptionally(new IllegalStateException("Lambda extension initialization failed"));
// Anything already queued or parked would otherwise wait forever, since the guards
// in enqueue()/NEXT_PATH now refuse to move work through a condemned environment.