mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(docker): hold Lambda extension and exit-watch streams on the streaming pool (#8498)
A Lambda extension's exec and the container's exit watcher each hold a Docker connection for the container's whole lifetime, and both still ran on the control-plane client. Enough warm containers, especially with extensions, could fill that pool and stall every create, start, stop and remove call, the failure the streaming split was made to prevent. Both now use the @StreamingDocker client. The exit watcher's inspect is a short call and stays on the control-plane client, with its retries. The streaming transport now replays a container wait that fails before the daemon answers, as the control-plane one does, so a stale socket does not spend one of the watcher's re-arms. Exec starts and log and attach streams are still never replayed. Each live Lambda container now holds its log follow, its exit watcher and one exec per extension on the streaming pool, so its default rises from 512 to 2048 to cover the 500 containers the runtime API port range allows with two extensions each. The control-plane default stays at 1024. Closes #2224
This commit is contained in:
1 parent
ec9c19ff62
commit
d51af1ecff
19 files changed
+252
-103
No files matched your search
@@ -4152,30 +4152,33 @@ public interface EmulatorConfig {
|
||||
String dockerHost();
|
||||
|
||||
/**
|
||||
* Size of the connection pool behind the shared Docker client. Every Docker call leases a
|
||||
* connection from it, and some hold one for as long as a container runs: a Lambda
|
||||
* container holds one (its exit watcher) plus one per extension. Log-follow streams
|
||||
* live in the separate streaming pool. Once the long-lived connections fill this pool,
|
||||
* create, start, stop and remove wait for a free connection, including the calls that
|
||||
* would release one, so the emulator stalls.
|
||||
* Size of the connection pool behind the shared Docker client. Every short Docker call
|
||||
* leases a connection from it, and a few streams hold one for as long as they run:
|
||||
* CodeBuild phase execs, EKS audit followers and ECS Exec sessions. The streams a Lambda
|
||||
* container holds for its whole lifetime live in the separate streaming pool. Once
|
||||
* long-lived connections fill this pool, create, start, stop and remove wait for a free
|
||||
* connection, including the calls that would release one, so the emulator stalls.
|
||||
*
|
||||
* <p>1024 rather than the former hard-coded 100, which capped live Lambda containers at
|
||||
* about 50: a tenth of the 500 that the runtime API port range
|
||||
* ({@link LambdaServiceConfig#runtimeApiBasePort}) exists to allow. Connections are
|
||||
* opened on demand, so a small stack never approaches the limit.
|
||||
* <p>1024 rather than the former hard-coded 100. Connections are opened on demand, so a
|
||||
* small stack never approaches the limit.
|
||||
*/
|
||||
@WithDefault("1024")
|
||||
int maxConnections();
|
||||
|
||||
/**
|
||||
* Connection pool size for the {@code @StreamingDocker} DockerClient, sized for the
|
||||
* long-lived container log-follow streams that occupy a slot for a container's entire
|
||||
* lifetime. Exec-output and container-wait streams stay on the main pool. Total live
|
||||
* containers is unbounded across distinct functions (each {@code WarmPool} caps only
|
||||
* per-function), so this pool is sized generously rather than tied to any single
|
||||
* function's warm-pool cap.
|
||||
* long-lived streams that occupy a slot for a container's entire lifetime: container log
|
||||
* follow, and for each Lambda container its exit watcher and one exec per extension.
|
||||
* Total live containers is unbounded across distinct functions (each {@code WarmPool}
|
||||
* caps only per-function), so this pool is sized generously rather than tied to any
|
||||
* single function's warm-pool cap.
|
||||
*
|
||||
* <p>2048 covers the 500 containers the runtime API port range
|
||||
* ({@link LambdaServiceConfig#runtimeApiBasePort}) allows, each with its log follow, its
|
||||
* exit watcher and two extensions. Connections are opened on demand, so a small stack
|
||||
* never approaches the limit.
|
||||
*/
|
||||
@WithDefault("512")
|
||||
@WithDefault("2048")
|
||||
int streamingMaxConnections();
|
||||
|
||||
/**
|
||||
|
||||
@@ -1500,8 +1500,9 @@ public class ContainerLifecycleManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the underlying DockerClient for operations not covered by this manager.
|
||||
* Prefer using manager methods when available.
|
||||
* Returns the underlying control-plane DockerClient for operations not covered by this
|
||||
* manager. Prefer using manager methods when available. A stream that stays open for a
|
||||
* container's lifetime belongs on the {@link StreamingDocker} client instead.
|
||||
*/
|
||||
public DockerClient getDockerClient() {
|
||||
return dockerClient;
|
||||
|
||||
@@ -424,10 +424,9 @@ public class DockerClientProducer {
|
||||
/**
|
||||
* Control-plane DockerClient: create/start/stop/remove/copyArchive and every other
|
||||
* short-lived call in Floci. Kept on its own connection pool, separate from the
|
||||
* {@link StreamingDocker} bean, so long-lived log-follow streams can never occupy every
|
||||
* lease in this pool and starve these short calls until httpclient5's connection-request
|
||||
* timeout fires. Exec-output and container-wait streams (CodeBuild phases, Lambda
|
||||
* extensions and exit watchers, EKS audit followers) still use this pool.
|
||||
* {@link StreamingDocker} bean, so long-lived streams can never occupy every lease in this
|
||||
* pool and starve these short calls until httpclient5's connection-request timeout fires.
|
||||
* CodeBuild phase execs, EKS audit followers and ECS Exec sessions still use this pool.
|
||||
*/
|
||||
@Produces
|
||||
@ApplicationScoped
|
||||
@@ -437,7 +436,8 @@ public class DockerClientProducer {
|
||||
}
|
||||
|
||||
/**
|
||||
* Streaming DockerClient: container log-follow ({@link ContainerLogStreamer}). Each
|
||||
* Streaming DockerClient: container log-follow ({@link ContainerLogStreamer}), Lambda
|
||||
* extension execs and Lambda exit watchers, and Lambda Web attach streams. Each
|
||||
* stream occupies a connection pool slot for its entire lifetime, so sharing the
|
||||
* control-plane pool meant a fan-out of many concurrent streams exhausted it and blocked
|
||||
* control-plane calls. Sized separately because {@code WarmPool}'s cap is per-function,
|
||||
@@ -519,14 +519,14 @@ public class DockerClientProducer {
|
||||
|
||||
/**
|
||||
* Wraps the control-plane transport in {@link RetryingDockerHttpClient} so every short-lived
|
||||
* docker call survives a transient socket drop; the streaming transport stays unwrapped,
|
||||
* its requests (log-follow, exec output) hold hijacked or long-lived streams that must never
|
||||
* be replayed, and its exec-start calls are the one request retrying could turn into a
|
||||
* re-run command.
|
||||
* docker call survives a transient socket drop. The streaming transport replays only container
|
||||
* waits, as the control-plane one does: its other requests (log follow, attach, exec output)
|
||||
* hold hijacked or long-lived streams that must never be replayed, and its exec-start calls are
|
||||
* the one request retrying could turn into a re-run command.
|
||||
*/
|
||||
static DockerHttpClient wrapForRole(DockerHttpClient transport, String role) {
|
||||
if (!"control-plane".equals(role)) {
|
||||
return transport;
|
||||
return RetryingDockerHttpClient.forContainerWaits(transport);
|
||||
}
|
||||
LOG.info("Wrapping control-plane docker transport in transient-I/O retry");
|
||||
return new RetryingDockerHttpClient(transport);
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.github.dockerjava.transport.DockerHttpClient;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Decorates a {@link DockerHttpClient} with the {@link DockerRetry} policy so every docker call
|
||||
@@ -65,20 +66,43 @@ public final class RetryingDockerHttpClient implements DockerHttpClient {
|
||||
private final DockerHttpClient delegate;
|
||||
private final int maxAttempts;
|
||||
private final long backoffMillis;
|
||||
private final Predicate<Request> replayable;
|
||||
|
||||
public RetryingDockerHttpClient(DockerHttpClient delegate) {
|
||||
this(delegate, MAX_ATTEMPTS, BACKOFF_MS);
|
||||
}
|
||||
|
||||
RetryingDockerHttpClient(DockerHttpClient delegate, int maxAttempts, long backoffMillis) {
|
||||
this(delegate, maxAttempts, backoffMillis, RetryingDockerHttpClient::isReplayable);
|
||||
}
|
||||
|
||||
private RetryingDockerHttpClient(DockerHttpClient delegate, int maxAttempts, long backoffMillis,
|
||||
Predicate<Request> replayable) {
|
||||
this.delegate = delegate;
|
||||
this.maxAttempts = maxAttempts;
|
||||
this.backoffMillis = backoffMillis;
|
||||
this.replayable = replayable;
|
||||
}
|
||||
|
||||
/**
|
||||
* A decorator that replays only container waits, for the streaming transport. A wait that fails
|
||||
* before the daemon answers is replayed as it is on the control-plane transport, while the
|
||||
* streaming transport's other requests, exec starts and log and attach streams, are never
|
||||
* replayed.
|
||||
*/
|
||||
static RetryingDockerHttpClient forContainerWaits(DockerHttpClient delegate) {
|
||||
return forContainerWaits(delegate, MAX_ATTEMPTS, BACKOFF_MS);
|
||||
}
|
||||
|
||||
static RetryingDockerHttpClient forContainerWaits(DockerHttpClient delegate, int maxAttempts,
|
||||
long backoffMillis) {
|
||||
return new RetryingDockerHttpClient(delegate, maxAttempts, backoffMillis,
|
||||
request -> isReplayable(request) && isContainerWait(request));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response execute(Request request) {
|
||||
if (!isReplayable(request)) {
|
||||
if (!replayable.test(request)) {
|
||||
return delegate.execute(request);
|
||||
}
|
||||
return DockerRetry.call(maxAttempts, backoffMillis, () -> delegate.execute(request));
|
||||
@@ -110,10 +134,9 @@ public final class RetryingDockerHttpClient implements DockerHttpClient {
|
||||
/** The {@code POST} routes whose replay after a lost response leaves the same outcome. */
|
||||
private static boolean isReplayablePost(Request request) {
|
||||
String path = request.path();
|
||||
int queryStart = path.indexOf('?');
|
||||
String route = queryStart < 0 ? path : path.substring(0, queryStart);
|
||||
String route = route(path);
|
||||
if (route.endsWith("/containers/create")) {
|
||||
String query = queryStart < 0 ? "" : path.substring(queryStart + 1);
|
||||
String query = route.length() < path.length() ? path.substring(route.length() + 1) : "";
|
||||
for (String param : query.split("&")) {
|
||||
if (param.startsWith("name=")) {
|
||||
return true;
|
||||
@@ -128,6 +151,16 @@ public final class RetryingDockerHttpClient implements DockerHttpClient {
|
||||
|| route.endsWith("/images/create");
|
||||
}
|
||||
|
||||
private static boolean isContainerWait(Request request) {
|
||||
return "POST".equals(request.method()) && route(request.path()).endsWith("/wait");
|
||||
}
|
||||
|
||||
/** A request path without its query string. */
|
||||
private static String route(String path) {
|
||||
int queryStart = path.indexOf('?');
|
||||
return queryStart < 0 ? path : path.substring(0, queryStart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a volume create names its volume. A replayed named create returns the existing
|
||||
* volume; a replayed unnamed one makes a second volume and orphans the first.
|
||||
|
||||
@@ -8,9 +8,9 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* CDI qualifier for the DockerClient dedicated to long-lived streaming operations,
|
||||
* container log-follow ({@link ContainerLogStreamer}). Exec-output and container-wait
|
||||
* streams still use the control-plane client. These calls occupy a connection pool
|
||||
* CDI qualifier for the DockerClient dedicated to long-lived streaming operations:
|
||||
* container log-follow ({@link ContainerLogStreamer}), Lambda extension execs and exit
|
||||
* watchers, and Lambda Web attach streams. These calls occupy a connection pool
|
||||
* slot for a container's entire lifetime, so they must not share a pool
|
||||
* with short-lived control-plane calls (create/start/stop/remove/copyArchive): a fan-out
|
||||
* of many long-lived streams would otherwise starve the control-plane calls out of a
|
||||
|
||||
@@ -16,6 +16,7 @@ import io.floci.aws.core.common.docker.DockerHostResolver;
|
||||
import io.floci.aws.core.common.docker.DockerPlatforms;
|
||||
import io.floci.aws.core.common.docker.LaunchedContainerAwsEnv;
|
||||
import io.floci.aws.core.common.docker.RetryingTarCopier;
|
||||
import io.floci.aws.core.common.docker.StreamingDocker;
|
||||
import io.floci.aws.services.ec2.Ec2Service;
|
||||
import io.floci.aws.services.ecr.registry.EcrRegistryManager;
|
||||
import io.floci.aws.services.iam.model.SessionCreds;
|
||||
@@ -119,6 +120,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
|
||||
private final ContainerBuilder containerBuilder;
|
||||
private final ContainerLifecycleManager lifecycleManager;
|
||||
private final DockerClient streamingDocker;
|
||||
private final ContainerLogStreamer logStreamer;
|
||||
private final ImageResolver imageResolver;
|
||||
private final RuntimeApiServerFactory runtimeApiServerFactory;
|
||||
@@ -136,6 +138,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
@Inject
|
||||
public ContainerLauncher(ContainerBuilder containerBuilder,
|
||||
ContainerLifecycleManager lifecycleManager,
|
||||
@StreamingDocker DockerClient streamingDocker,
|
||||
ContainerLogStreamer logStreamer,
|
||||
ImageResolver imageResolver,
|
||||
RuntimeApiServerFactory runtimeApiServerFactory,
|
||||
@@ -148,6 +151,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
Ec2Service ec2Service) {
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.streamingDocker = streamingDocker;
|
||||
this.logStreamer = logStreamer;
|
||||
this.imageResolver = imageResolver;
|
||||
this.runtimeApiServerFactory = runtimeApiServerFactory;
|
||||
@@ -1469,7 +1473,8 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
*/
|
||||
private void watchForUnexpectedExit(DockerClient dockerClient, String containerId,
|
||||
RuntimeApiServer runtimeApiServer) {
|
||||
LambdaExitWatcher.watch(dockerClient, containerId, runtimeApiServer::handleRuntimeProcessExited);
|
||||
LambdaExitWatcher.watch(dockerClient, streamingDocker, containerId,
|
||||
runtimeApiServer::handleRuntimeProcessExited);
|
||||
}
|
||||
|
||||
private void launchExtensions(DockerClient dockerClient, String containerId, String functionName,
|
||||
@@ -1496,7 +1501,10 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
// the container log and an observability extension's output would vanish entirely.
|
||||
// Draining is also required in its own right — an unread exec pipe fills up and
|
||||
// stalls the extension process.
|
||||
dockerClient.execStartCmd(execId).exec(logStreamer.execLogCallbackForAccount(
|
||||
//
|
||||
// The stream holds its connection while the extension runs, so it uses the
|
||||
// streaming pool, where it cannot starve short control-plane calls.
|
||||
streamingDocker.execStartCmd(execId).exec(logStreamer.execLogCallbackForAccount(
|
||||
logDestination.accountId(), logDestination.logGroup(), logDestination.logStream(),
|
||||
logDestination.region(),
|
||||
"lambda:" + functionName + ":" + name));
|
||||
|
||||
@@ -29,6 +29,9 @@ import java.util.function.LongSupplier;
|
||||
* time, so the watch outlasts a short outage instead of spending its attempts within milliseconds; after
|
||||
* {@link #MAX_QUICK_FAILURES} in a row it stops. A stream that lived longer than {@link #QUICK_FAILURE}
|
||||
* resets the count and re-arms at once.
|
||||
*
|
||||
* <p>The wait stream holds its connection for the container's whole lifetime, so it runs on the
|
||||
* streaming client; the inspect is a short call and stays on the control-plane client.
|
||||
*/
|
||||
final class LambdaExitWatcher {
|
||||
|
||||
@@ -42,15 +45,17 @@ final class LambdaExitWatcher {
|
||||
static final Duration FIRST_RETRY_DELAY = Duration.ofSeconds(1);
|
||||
|
||||
private final DockerClient dockerClient;
|
||||
private final DockerClient streamingDocker;
|
||||
private final String containerId;
|
||||
private final IntConsumer onExit;
|
||||
private final BiConsumer<Duration, Runnable> scheduler;
|
||||
private final LongSupplier nanoClock;
|
||||
private int quickFailures;
|
||||
|
||||
LambdaExitWatcher(DockerClient dockerClient, String containerId, IntConsumer onExit,
|
||||
BiConsumer<Duration, Runnable> scheduler, LongSupplier nanoClock) {
|
||||
LambdaExitWatcher(DockerClient dockerClient, DockerClient streamingDocker, String containerId,
|
||||
IntConsumer onExit, BiConsumer<Duration, Runnable> scheduler, LongSupplier nanoClock) {
|
||||
this.dockerClient = dockerClient;
|
||||
this.streamingDocker = streamingDocker;
|
||||
this.containerId = containerId;
|
||||
this.onExit = onExit;
|
||||
this.scheduler = scheduler;
|
||||
@@ -58,9 +63,10 @@ final class LambdaExitWatcher {
|
||||
}
|
||||
|
||||
/** Arms a watch that calls {@code onExit} with the exit status, at most once per exit. */
|
||||
static void watch(DockerClient dockerClient, String containerId, IntConsumer onExit) {
|
||||
new LambdaExitWatcher(dockerClient, containerId, onExit, LambdaExitWatcher::schedule, System::nanoTime)
|
||||
.arm();
|
||||
static void watch(DockerClient dockerClient, DockerClient streamingDocker, String containerId,
|
||||
IntConsumer onExit) {
|
||||
new LambdaExitWatcher(dockerClient, streamingDocker, containerId, onExit, LambdaExitWatcher::schedule,
|
||||
System::nanoTime).arm();
|
||||
}
|
||||
|
||||
private static void schedule(Duration delay, Runnable task) {
|
||||
@@ -70,7 +76,7 @@ final class LambdaExitWatcher {
|
||||
void arm() {
|
||||
long armedAt = nanoClock.getAsLong();
|
||||
try {
|
||||
dockerClient.waitContainerCmd(containerId).exec(new WaitContainerResultCallback() {
|
||||
streamingDocker.waitContainerCmd(containerId).exec(new WaitContainerResultCallback() {
|
||||
@Override
|
||||
public void onNext(WaitResponse response) {
|
||||
super.onNext(response);
|
||||
|
||||
Reference in new issue
Block a user