fix(docker): share one deadline across container teardown (#6077)

Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-21 10:52:14 -04:00
1 parent ab0737798d
commit 4fa11104b4
4 files changed
+188 -22

No files matched your search

@@ -3,6 +3,16 @@ package io.github.hectorvent.floci.core.common;
import jakarta.enterprise.inject.Instance;
import org.jboss.logging.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
/**
* Runs every {@link ContainerTeardown} implementation, tolerating a failure in one so it
* doesn't block the others or whatever cleanup the caller runs next. {@code EmulatorLifecycle}
@@ -24,4 +34,50 @@ public final class ContainerTeardowns {
}
}
}
public static <T> void stopInParallel(List<T> resources, Consumer<T> stop, Logger log) {
stopInParallel(resources, stop, log, System.nanoTime() + TimeUnit.SECONDS.toNanos(15));
}
public static <T> void stopInParallel(List<T> resources, Consumer<T> stop, Logger log,
long deadlineNanos) {
if (resources.isEmpty()) {
return;
}
long remaining = Math.max(0, deadlineNanos - System.nanoTime());
if (remaining == 0) {
log.warnv("Container drain deadline expired with {0} resource(s) pending", resources.size());
return;
}
// Every owned container must receive its stop within the same deadline. A fixed worker
// queue lets slow Docker calls consume that budget before later containers can even stop.
ExecutorService executor = Executors.newThreadPerTaskExecutor(
Thread.ofVirtual().name("container-teardown-", 0).factory());
try {
List<Callable<Void>> tasks = new ArrayList<>(resources.size());
for (T resource : resources) {
tasks.add(() -> {
stop.accept(resource);
return null;
});
}
for (Future<Void> task : executor.invokeAll(tasks,
Math.max(0, deadlineNanos - System.nanoTime()), TimeUnit.NANOSECONDS)) {
if (task.isCancelled()) {
log.warn("Container drain did not finish before the shared deadline");
} else {
try {
task.get();
} catch (ExecutionException failure) {
log.warnv(failure.getCause(), "Container drain failed");
}
}
}
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
log.warn("Container drain interrupted");
} finally {
executor.shutdownNow();
}
}
}
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.lambda;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.ContainerTeardown;
import io.github.hectorvent.floci.core.common.ContainerTeardowns;
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;
@@ -21,9 +22,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@@ -384,26 +383,7 @@ public class WarmPool implements ContainerTeardown {
}
private void stopInParallel(List<ContainerHandle> handles) {
if (handles.isEmpty()) {
return;
}
ExecutorService pool = Executors.newThreadPerTaskExecutor(
Thread.ofVirtual().name("warm-pool-drainer-", 0).factory());
try {
List<Future<?>> futures = new ArrayList<>(handles.size());
for (ContainerHandle handle : handles) {
futures.add(pool.submit(() -> stopQuietly(handle)));
}
for (Future<?> f : futures) {
try {
f.get(15, TimeUnit.SECONDS);
} catch (Exception e) {
LOG.warnv("Drain task did not finish cleanly: {0}", e.getMessage());
}
}
} finally {
pool.shutdownNow();
}
ContainerTeardowns.stopInParallel(handles, this::stopQuietly, LOG);
}
private void drainAll() {