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

@@ -0,0 +1,75 @@
package io.github.hectorvent.floci.core.common;
import org.jboss.logging.Logger;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
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.AtomicInteger;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ContainerTeardownsTest {
private static final Logger LOG = Logger.getLogger(ContainerTeardownsTest.class);
@Test
void admitsEveryOwnedStopBeforeWaitingForOtherContainers() throws Exception {
List<Integer> resources = IntStream.range(0, 83).boxed().toList();
CountDownLatch started = new CountDownLatch(resources.size());
CountDownLatch release = new CountDownLatch(1);
Set<Integer> stopped = ConcurrentHashMap.newKeySet();
ExecutorService caller = Executors.newSingleThreadExecutor();
Future<?> drain = caller.submit(() -> ContainerTeardowns.stopInParallel(resources, resource -> {
started.countDown();
try {
release.await();
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
throw new IllegalStateException(interrupted);
}
stopped.add(resource);
}, LOG));
try {
assertTrue(started.await(3, TimeUnit.SECONDS),
"Queued containers must receive their stop before slower containers consume the shared deadline");
} finally {
release.countDown();
try {
drain.get(5, TimeUnit.SECONDS);
} finally {
caller.shutdownNow();
}
}
assertEquals(Set.copyOf(resources), stopped);
}
@Test
void failedStopDoesNotPreventTheOtherOwnedStops() {
List<Integer> resources = IntStream.range(0, 83).boxed().toList();
Set<Integer> attempted = ConcurrentHashMap.newKeySet();
ContainerTeardowns.stopInParallel(resources, resource -> {
attempted.add(resource);
if (resource == 0) {
throw new IllegalStateException("Docker removal failed");
}
}, LOG);
assertEquals(Set.copyOf(resources), attempted);
}
@Test
void anExpiredDeadlineDoesNotStartAnotherDrainBudget() {
AtomicInteger attempts = new AtomicInteger();
ContainerTeardowns.stopInParallel(List.of(1, 2),
ignored -> attempts.incrementAndGet(), LOG, System.nanoTime() - 1);
assertEquals(0, attempts.get());
}
}
@@ -25,6 +25,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
@@ -178,6 +179,60 @@ class WarmPoolTest {
pool.shutdown();
}
@Test
void interruptedDrainCancelsEveryStopAndPreservesTheCallerInterrupt() throws Exception {
WarmPool pool = buildPool();
pool.init();
LambdaFunction fn = function("interrupted-drain");
AtomicInteger ids = new AtomicInteger();
when(containerLauncher.launch(any())).thenAnswer(invocation ->
new ContainerHandle("cid-interrupt-" + ids.incrementAndGet(),
fn.getFunctionName(), null, ContainerState.WARM));
List<ContainerHandle> handles = new ArrayList<>();
for (int i = 0; i < 3; i++) {
handles.add(pool.acquire(fn));
}
handles.forEach(pool::release);
CountDownLatch allStopping = new CountDownLatch(handles.size());
CountDownLatch finishStops = new CountDownLatch(1);
CountDownLatch interruptedStops = new CountDownLatch(handles.size());
doAnswer(invocation -> {
allStopping.countDown();
try {
finishStops.await();
} catch (InterruptedException interrupted) {
interruptedStops.countDown();
throw interrupted;
}
return null;
}).when(containerLauncher).stop(any());
AtomicReference<Thread> drainThread = new AtomicReference<>();
ExecutorService caller = Executors.newSingleThreadExecutor();
Future<Boolean> drain = caller.submit(() -> {
drainThread.set(Thread.currentThread());
pool.stopManagedContainers();
return Thread.currentThread().isInterrupted();
});
try {
assertTrue(allStopping.await(3, TimeUnit.SECONDS));
drainThread.get().interrupt();
assertTrue(drain.get(2, TimeUnit.SECONDS));
assertTrue(interruptedStops.await(3, TimeUnit.SECONDS));
} finally {
finishStops.countDown();
try {
drain.get(5, TimeUnit.SECONDS);
} finally {
caller.shutdownNow();
pool.shutdown();
}
}
for (ContainerHandle handle : handles) {
verify(containerLauncher, times(1)).stop(handle);
}
}
@Test
void destroyHandleStopsContainerAndDoesNotReturnToPool() {
WarmPool pool = buildPool();