feat(lambdaweb): scale environments per endpoint and apply maxEnvironments and rateLimit (#5785)

* feat(lambdaweb): scale environments per endpoint and apply maxEnvironments and rateLimit

Each endpoint keeps its own pool of environments, as on AWS. An
environment takes up to the revision's maxConcurrencyPerEnvironment
requests at once, the least busy one first so requests rotate, and a
request that finds every environment full starts another or waits for
one that is starting. At maxEnvironments the request gets the 429 page
with Platform.EndpointMaxEnvThrottle. A rateLimit admits a burst of up
to rateLimit requests refilled at rateLimit per second and refuses the
rest with Platform.EndpointRequestThrottle. Both pages were recorded
against AWS.

Part of #5699

* fix(lambdaweb): key environment pools by endpoint creation and let a new revision take an idle slot

A recreated endpoint gets a new domain, so it no longer reuses the
environments of the deleted one. At maxEnvironments an idle environment
of another revision is stopped to make room. Empty pools are dropped.
The burst in the Docker test now arrives at once, so a slow runner still
goes past the rate.

* fix(lambdaweb): keep the rate limiter clock monotonic and drop refilled buckets

A bucket untouched for a second is full, so it is dropped. A clock read
that lands behind the bucket no longer takes tokens back.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-10-16 07:10:39 -04:00
1 parent 3cb1643d30
commit 832f1008bb
9 files changed
+499 -69

No files matched your search

@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.core.common.RequestHost;
import io.github.hectorvent.floci.core.common.ServiceConfigAccess;
import io.github.hectorvent.floci.services.lambdaweb.LambdaWebEnvironmentManager.Environment;
import io.github.hectorvent.floci.services.lambdaweb.LambdaWebEnvironmentManager.StartFailure;
import io.github.hectorvent.floci.services.lambdaweb.LambdaWebEnvironmentManager.Throttled;
import io.github.hectorvent.floci.services.lambdaweb.LambdaWebService.WebFunctionTarget;
import io.github.hectorvent.floci.services.lambdaweb.model.WebFunctionRevision;
import io.vertx.core.Future;
@@ -69,6 +70,7 @@ public class LambdaWebDataPlane {
private final ServiceConfigAccess serviceConfigAccess;
private final Vertx vertx;
private final HttpClient client;
private final LambdaWebRateLimiter rateLimiter = new LambdaWebRateLimiter();
@Inject
public LambdaWebDataPlane(LambdaWebService service, LambdaWebEnvironmentManager environments,
@@ -109,11 +111,15 @@ public class LambdaWebDataPlane {
return;
}
WebFunctionTarget target = located.get();
if (target.endpoint().throttleConfig != null
&& Integer.valueOf(0).equals(target.endpoint().throttleConfig.rateLimit)) {
Integer rateLimit = target.endpoint().throttleConfig == null ? null : target.endpoint().throttleConfig.rateLimit;
if (Integer.valueOf(0).equals(rateLimit)) {
page(request, LambdaWebErrorPages.ENDPOINT_DISABLED, requestId, false);
return;
}
if (rateLimit != null && !rateLimiter.tryAcquire(endpointKey(target), rateLimit)) {
page(request, LambdaWebErrorPages.ENDPOINT_REQUEST_THROTTLE, requestId, false);
return;
}
WebFunctionRevision revision = LambdaWebService.pickRevision(target);
if (revision == null) {
page(request, LambdaWebErrorPages.ORIGIN_REQUEST_ERROR, requestId, false);
@@ -126,6 +132,11 @@ public class LambdaWebDataPlane {
acquire(request, host, target, revision, requestId, null);
}
/** The domain is minted for each creation, so a recreated endpoint shares no state with the deleted one. */
private static String endpointKey(WebFunctionTarget target) {
return String.join("|", target.accountId(), target.region(), target.endpoint().domainName);
}
/** Starts or reuses the revision's environment off the event loop, then forwards. */
private void acquire(HttpServerRequest request, String host, WebFunctionTarget target,
WebFunctionRevision revision, String requestId, Environment dead) {
@@ -133,7 +144,10 @@ public class LambdaWebDataPlane {
if (dead != null) {
environments.discard(dead);
}
return environments.acquire(target.accountId(), target.region(), target.function(), revision);
Integer maxEnvironments = target.endpoint().scalingConfig == null
? null : target.endpoint().scalingConfig.maxEnvironments;
return environments.acquire(target.accountId(), target.region(), target.function(),
endpointKey(target), maxEnvironments, revision);
}, false)
.onSuccess(environment -> forward(request, host, target, revision, environment, requestId, dead == null))
.onFailure(error -> {
@@ -142,6 +156,10 @@ public class LambdaWebDataPlane {
page(request, failure.page(), requestId, false);
return;
}
if (error instanceof Throttled) {
page(request, LambdaWebErrorPages.ENDPOINT_MAX_ENV_THROTTLE, requestId, false);
return;
}
LOG.errorv(error, "Could not start an environment for {0}", host);
internalError(request, requestId);
});
@@ -49,7 +49,9 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@@ -60,9 +62,9 @@ import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;
/**
* The execution environments of Web Function revisions: one Node.js server container per revision,
* started by the first request, reused while requests keep coming and removed after the Lambda idle
* timeout. The runtime loads {@code index.js}, or the file {@code AWS_LAMBDA_NODEJS_ENTRYPOINT} names,
* The execution environments of Web Function endpoints: Node.js server containers of a revision,
* started by requests that find no room, reused while requests keep coming and removed after the
* Lambda idle timeout. The runtime loads {@code index.js}, or the file {@code AWS_LAMBDA_NODEJS_ENTRYPOINT} names,
* from {@code /var/task} with the node flags AWS uses.
*/
@ApplicationScoped
@@ -86,31 +88,34 @@ public class LambdaWebEnvironmentManager implements ContainerTeardown {
private static final Duration EXIT_WATCH_REARM = Duration.ofSeconds(1);
/**
* A started container. {@code acquire} reserves it for one request and {@code release} gives
* the reservation back; the idle reaper only stops an environment with no reservation.
* One container of an endpoint's pool. It joins the pool before it starts, so requests that
* arrive while it initializes wait for it instead of starting more. {@code acquire} reserves it
* for one request and {@code release} gives the reservation back; the idle reaper only stops an
* environment with no reservation.
*/
public static final class Environment {
private final String key;
private final String poolKey;
private final String revisionId;
private final String accountId;
private final String containerId;
private final String host;
private final int port;
private final String logStream;
private final Optional<SessionCreds> roleCredentials;
private final CompletableFuture<Void> ready = new CompletableFuture<>();
private volatile String containerId;
private volatile String host;
private volatile int port;
private volatile String logStream;
private volatile SessionCreds roleCredentials;
private final AtomicInteger inFlight = new AtomicInteger();
private final AtomicLong lastUsed = new AtomicLong(System.nanoTime());
private final AtomicBoolean alive = new AtomicBoolean(true);
private final AtomicReference<Instant> exitedAt = new AtomicReference<>();
Environment(String key, String accountId, String containerId, String host, int port, String logStream,
Optional<SessionCreds> roleCredentials) {
this.key = key;
Environment(String poolKey, String revisionId, String accountId) {
this.poolKey = poolKey;
this.revisionId = revisionId;
this.accountId = accountId;
this.containerId = containerId;
this.host = host;
this.port = port;
this.logStream = logStream;
this.roleCredentials = roleCredentials;
}
private boolean isReady() {
return ready.isDone() && !ready.isCompletedExceptionally();
}
public String containerId() {
@@ -158,6 +163,13 @@ public class LambdaWebEnvironmentManager implements ContainerTeardown {
}
}
/** Every environment of the endpoint is full and {@code maxEnvironments} allows no more. */
public static final class Throttled extends RuntimeException {
Throttled() {
super(LambdaWebErrorPages.ENDPOINT_MAX_ENV_THROTTLE.errorType());
}
}
private final EmulatorConfig config;
private final ContainerBuilder containerBuilder;
private final ContainerLifecycleManager lifecycleManager;
@@ -167,8 +179,8 @@ public class LambdaWebEnvironmentManager implements ContainerTeardown {
private final ZipExtractor zipExtractor;
private final LaunchedContainerAwsEnv awsEnv;
private final LambdaExecutionRoleCredentials executionRoleCredentials;
private final ConcurrentHashMap<String, Environment> environments = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Object> locks = new ConcurrentHashMap<>();
/** The environments of each endpoint in each region. A list is only touched inside a map operation on its key. */
private final ConcurrentHashMap<String, List<Environment>> pools = new ConcurrentHashMap<>();
private final ScheduledExecutorService reaper = Executors.newSingleThreadScheduledExecutor(task -> {
Thread thread = new Thread(task, "lambda-web-environments");
thread.setDaemon(true);
@@ -195,31 +207,123 @@ public class LambdaWebEnvironmentManager implements ContainerTeardown {
}
/**
* The running environment of {@code revision}, started if there is none, reserved for one
* request. The caller releases it when the request is over. Blocks on Docker and S3.
* An environment of the endpoint for {@code revision} with room for one more request, reserved
* for it. The least busy one is taken, the one idle longest on a tie, so requests rotate. When
* all are full a new one starts. At {@code maxEnvironments} an idle environment of another
* revision makes room, and without one the request is {@link Throttled}. {@code endpointKey}
* names one creation of the endpoint in one region, so a recreated endpoint starts afresh. The
* caller releases the environment when the request is over. Blocks on Docker and S3.
*/
public Environment acquire(String accountId, String region, WebFunction fn, WebFunctionRevision revision) {
String key = String.join("|", accountId, region, fn.functionName, revision.revisionId);
synchronized (lockFor(key)) {
Environment existing = environments.get(key);
if (existing != null && existing.isAlive()) {
existing.reserve();
return existing;
}
Environment started = start(key, accountId, region, fn, revision);
started.reserve();
environments.put(key, started);
return started;
public Environment acquire(String accountId, String region, WebFunction fn, String endpointKey,
Integer maxEnvironments, WebFunctionRevision revision) {
int perEnvironment = Math.max(1, revision.serviceConfig.maxConcurrencyPerEnvironment);
AtomicReference<Pick> picked = new AtomicReference<>();
pools.compute(endpointKey, (key, current) -> {
List<Environment> pool = current == null ? new ArrayList<>() : current;
picked.set(pick(pool, endpointKey, accountId, revision.revisionId, perEnvironment, maxEnvironments));
return pool;
});
Pick pick = picked.get();
if (pick.retired() != null) {
reaper.execute(() -> stopRetired(pick.retired()));
}
Environment chosen = pick.environment();
if (!pick.starter()) {
awaitReady(chosen);
return chosen;
}
try {
start(chosen, region, fn, revision);
} catch (RuntimeException e) {
chosen.alive.set(false);
removeFromPool(chosen);
chosen.ready.completeExceptionally(e);
throw e;
}
if (!chosen.isAlive()) {
IllegalStateException stopped = new IllegalStateException("The environment was stopped while it started");
chosen.ready.completeExceptionally(stopped);
stopContainer(chosen);
throw stopped;
}
chosen.ready.complete(null);
return chosen;
}
private record Pick(Environment environment, boolean starter, Environment retired) {
}
private static Pick pick(List<Environment> pool, String endpointKey, String accountId, String revisionId,
int perEnvironment, Integer maxEnvironments) {
Environment open = pool.stream()
.filter(each -> each.isAlive() && each.revisionId.equals(revisionId)
&& each.inFlight.get() < perEnvironment)
.min(Comparator.comparingInt((Environment each) -> each.inFlight.get())
.thenComparingLong(each -> each.lastUsed.get()))
.orElse(null);
if (open != null) {
open.reserve();
return new Pick(open, false, null);
}
Environment retired = null;
if (maxEnvironments != null && pool.size() >= maxEnvironments) {
retired = pool.stream()
.filter(each -> !each.revisionId.equals(revisionId) && isIdle(each, 0))
.min(Comparator.comparingLong(each -> each.lastUsed.get()))
.orElseThrow(Throttled::new);
pool.remove(retired);
}
Environment started = new Environment(endpointKey, revisionId, accountId);
pool.add(started);
started.reserve();
return new Pick(started, true, retired);
}
private static boolean isIdle(Environment environment, long idleNanos) {
return environment.isAlive() && environment.isReady() && environment.inFlight.get() == 0
&& System.nanoTime() - environment.lastUsed.get() >= idleNanos;
}
private void stopRetired(Environment environment) {
try {
LOG.infov("Removing the Lambda Web environment {0} of revision {1} to make room",
environment.containerId, environment.revisionId);
discard(environment);
} catch (RuntimeException e) {
LOG.warnv(e, "Could not remove the Lambda Web environment {0}", environment.containerId);
}
}
private static void awaitReady(Environment environment) {
try {
environment.ready.get();
} catch (ExecutionException e) {
if (e.getCause() instanceof RuntimeException runtime) {
throw runtime;
}
throw new IllegalStateException(e.getCause());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
}
}
private void removeFromPool(Environment environment) {
pools.computeIfPresent(environment.poolKey, (key, pool) -> {
pool.remove(environment);
return pool.isEmpty() ? null : pool;
});
}
/** Stops an environment, with the one second AWS gives the process after SIGTERM. */
public void discard(Environment environment) {
removeFromPool(environment);
if (!environment.alive.compareAndSet(true, false)) {
environments.remove(environment.key, environment);
return;
}
environments.remove(environment.key, environment);
if (environment.containerId == null) {
return;
}
try {
exitCodeIfStopped(environment.containerId).ifPresent(exitCode ->
LOG.infov("The Lambda Web environment {0} had exited with status {1}: {2}",
@@ -227,6 +331,10 @@ public class LambdaWebEnvironmentManager implements ContainerTeardown {
} catch (RuntimeException e) {
LOG.debugv(e, "Could not inspect the Lambda Web environment {0}", environment.containerId);
}
stopContainer(environment);
}
private void stopContainer(Environment environment) {
try {
lifecycleManager.stopAndRemove(environment.containerId, null, 1);
} finally {
@@ -252,7 +360,18 @@ public class LambdaWebEnvironmentManager implements ContainerTeardown {
@Override
public void stopManagedContainers() {
new ArrayList<>(environments.values()).forEach(this::discard);
allEnvironments().forEach(this::discard);
}
private List<Environment> allEnvironments() {
List<Environment> all = new ArrayList<>();
for (String key : pools.keySet()) {
pools.computeIfPresent(key, (ignored, pool) -> {
all.addAll(pool);
return pool;
});
}
return all;
}
@PreDestroy
@@ -260,30 +379,30 @@ public class LambdaWebEnvironmentManager implements ContainerTeardown {
reaper.shutdownNow();
}
private Object lockFor(String key) {
return locks.computeIfAbsent(key, ignored -> new Object());
}
private void reapIdle() {
long idleNanos = TimeUnit.SECONDS.toNanos(config.services().lambda().containerIdleTimeoutSeconds());
for (Environment environment : new ArrayList<>(environments.values())) {
try {
synchronized (lockFor(environment.key)) {
if (environment.inFlight.get() != 0
|| System.nanoTime() - environment.lastUsed.get() <= idleNanos) {
continue;
}
LOG.infov("Removing the idle Lambda Web environment {0}", environment.containerId);
discard(environment);
for (Environment environment : allEnvironments()) {
AtomicBoolean idle = new AtomicBoolean();
pools.computeIfPresent(environment.poolKey, (key, pool) -> {
if (isIdle(environment, idleNanos) && pool.remove(environment)) {
idle.set(true);
}
return pool.isEmpty() ? null : pool;
});
if (!idle.get()) {
continue;
}
try {
LOG.infov("Removing the idle Lambda Web environment {0}", environment.containerId);
discard(environment);
} catch (RuntimeException e) {
LOG.warnv(e, "Could not remove the idle Lambda Web environment {0}", environment.containerId);
}
}
}
private Environment start(String key, String accountId, String region, WebFunction fn,
WebFunctionRevision revision) {
private void start(Environment environment, String region, WebFunction fn, WebFunctionRevision revision) {
String accountId = environment.accountId;
String image = ecrRegistryManager.rewriteImageUri(imageResolver.resolve(revision.buildConfig.runtime));
String shortId = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
String name = ContainerStorageHelper.prefixedDockerName(config, "floci-lambda-web",
@@ -320,14 +439,15 @@ public class LambdaWebEnvironmentManager implements ContainerTeardown {
ContainerInfo info = lifecycleManager.startCreated(containerId, spec);
EndpointInfo endpoint = info.getEndpoint(APP_PORT);
awaitListening(containerId);
String logStream = LOG_STREAM_DATE.format(Instant.now()) + "/" + fn.functionName + "[" + revision.revisionId
+ "]" + UUID.randomUUID().toString().replace("-", "");
Environment environment = new Environment(key, accountId, containerId, endpoint.host(), endpoint.port(),
logStream, roleCredentials);
environment.containerId = containerId;
environment.host = endpoint.host();
environment.port = endpoint.port();
environment.logStream = LOG_STREAM_DATE.format(Instant.now()) + "/" + fn.functionName + "["
+ revision.revisionId + "]" + UUID.randomUUID().toString().replace("-", "");
environment.roleCredentials = roleCredentials.orElse(null);
watchExit(environment);
LOG.infov("Started the Lambda Web environment {0} of {1} revision {2} at {3}:{4}",
containerId, fn.functionName, revision.revisionId, endpoint.host(), endpoint.port());
return environment;
} catch (RuntimeException e) {
lifecycleManager.stopAndRemove(containerId, null, 1);
roleCredentials.ifPresent(creds -> executionRoleCredentials.unregister(accountId, creds.accessKeyId()));
@@ -351,8 +471,10 @@ public class LambdaWebEnvironmentManager implements ContainerTeardown {
}
private void forgetCredentials(Environment environment) {
environment.roleCredentials.ifPresent(creds ->
executionRoleCredentials.unregister(environment.accountId, creds.accessKeyId()));
SessionCreds credentials = environment.roleCredentials;
if (credentials != null) {
executionRoleCredentials.unregister(environment.accountId, credentials.accessKeyId());
}
}
/** The code goes in by {@code docker cp}, so it works when Floci itself runs in a container. */
@@ -559,7 +681,7 @@ public class LambdaWebEnvironmentManager implements ContainerTeardown {
return;
}
environment.exitedAt.compareAndSet(null, finishedAt(environment.containerId).orElse(Instant.now()));
environments.remove(environment.key, environment);
removeFromPool(environment);
reaper.execute(() -> {
try {
LOG.infov("The Lambda Web environment {0} exited with status {1}: {2}",
@@ -27,6 +27,11 @@ final class LambdaWebErrorPages {
APPLICATION_ERROR);
static final Page INIT_CODE_ERROR = new Page(502, "Bad Gateway", "Application.InitCodeError", APPLICATION_ERROR);
static final Page RUNTIME_EXIT = new Page(502, "Bad Gateway", "Runtime.Exit", APPLICATION_ERROR);
private static final String RATE_EXCEEDED = "Request rate exceeded. Please try again later.";
static final Page ENDPOINT_MAX_ENV_THROTTLE = new Page(429, "Too Many Requests",
"Platform.EndpointMaxEnvThrottle", RATE_EXCEEDED);
static final Page ENDPOINT_REQUEST_THROTTLE = new Page(429, "Too Many Requests",
"Platform.EndpointRequestThrottle", RATE_EXCEEDED);
/** The edge in front of the platform refuses a method itself, with its own page and header casing. */
static final String MALFORMED_REQUEST_TYPE = "Platform.MalformedRequest";
@@ -0,0 +1,68 @@
package io.github.hectorvent.floci.services.lambdaweb;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.LongSupplier;
/**
* The {@code rateLimit} of each endpoint: a bucket of {@code rateLimit} requests that refills at
* {@code rateLimit} per second, so a brief burst above the rate passes and a sustained one does not.
*/
final class LambdaWebRateLimiter {
private static final long SECOND = TimeUnit.SECONDS.toNanos(1);
private record Bucket(int rate, double tokens, long at) {
}
private final ConcurrentHashMap<String, Bucket> buckets = new ConcurrentHashMap<>();
private final LongSupplier nanoClock;
private final AtomicLong sweptAt;
LambdaWebRateLimiter() {
this(System::nanoTime);
}
LambdaWebRateLimiter(LongSupplier nanoClock) {
this.nanoClock = nanoClock;
this.sweptAt = new AtomicLong(nanoClock.getAsLong());
}
/** Whether one more request to {@code endpointKey} fits under {@code rate} requests per second. */
boolean tryAcquire(String endpointKey, int rate) {
sweep();
AtomicBoolean admitted = new AtomicBoolean();
buckets.compute(endpointKey, (key, bucket) -> {
double tokens;
long now = nanoClock.getAsLong();
if (bucket == null || bucket.rate() != rate) {
tokens = rate;
} else {
now = Math.max(now, bucket.at());
double refill = rate * (double) (now - bucket.at()) / SECOND;
tokens = Math.min(rate, bucket.tokens() + refill);
}
if (tokens >= 1) {
admitted.set(true);
tokens -= 1;
}
return new Bucket(rate, tokens, now);
});
return admitted.get();
}
int size() {
return buckets.size();
}
/** A bucket untouched for a second is full again, the same as no bucket. */
private void sweep() {
long now = nanoClock.getAsLong();
long last = sweptAt.get();
if (now - last >= SECOND && sweptAt.compareAndSet(last, now)) {
buckets.values().removeIf(bucket -> now - bucket.at() >= SECOND);
}
}
}