fix(docker): avoid global teardown admission waits (#6581)

* fix(docker): avoid global teardown admission waits
* test(docker): observe teardown stop execution
* test(ec2): match account-aware private IP allocation
* chore(docker): remove obsolete teardown admission wrappers
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-16 15:19:57 -04:00
1 parent 94fcf5cbf3
commit f5453b9585
2 files changed
+38 -107

No files matched your search

@@ -13,13 +13,9 @@ import java.io.Closeable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import static io.floci.aws.testing.ThreadStates.awaitBlockedOrDone;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -66,22 +62,24 @@ class ContainerLifecycleManagerTeardownTest {
verify(logStream).close();
}
@Test
void interruptedWaitersStillCleanUpWithoutReleasingAnotherOwnersSlot() throws Exception {
@ParameterizedTest
@ValueSource(strings = {"strict", "strict-volumes", "best-effort", "stop-only"})
void laterOwnedStopsReachDockerWhileEarlierRemovalsAreBlocked(String mode) throws Exception {
DockerClient docker = mock(DockerClient.class);
ContainerLifecycleManager manager = new ContainerLifecycleManager(docker, null, null, null);
StopContainerCmd stop = mock(StopContainerCmd.class);
CountDownLatch ninthStop = new CountDownLatch(1);
when(docker.stopContainerCmd(anyString())).thenReturn(stop);
when(stop.withTimeout(anyInt())).thenReturn(stop);
RemoveContainerCmd remove = mock(RemoveContainerCmd.class);
when(docker.removeContainerCmd(anyString())).thenAnswer(invocation -> {
if (invocation.<String>getArgument(0).startsWith("owned-")) {
return remove;
}
RemoveContainerCmd waitingRemove = mock(RemoveContainerCmd.class);
when(waitingRemove.withForce(true)).thenReturn(waitingRemove);
return waitingRemove;
StopContainerCmd ninthStopCommand = mock(StopContainerCmd.class);
when(docker.stopContainerCmd("ninth")).thenReturn(ninthStopCommand);
when(ninthStopCommand.withTimeout(anyInt())).thenReturn(ninthStopCommand);
when(ninthStopCommand.exec()).thenAnswer(invocation -> {
ninthStop.countDown();
return null;
});
RemoveContainerCmd remove = mock(RemoveContainerCmd.class);
when(docker.removeContainerCmd(anyString())).thenReturn(remove);
when(remove.withForce(true)).thenReturn(remove);
CountDownLatch removing = new CountDownLatch(8);
CountDownLatch release = new CountDownLatch(1);
@@ -94,89 +92,47 @@ class ContainerLifecycleManagerTeardownTest {
AtomicReference<Throwable> failure = new AtomicReference<>();
for (int i = 0; i < 8; i++) {
String id = "owned-" + i;
boolean strict = i % 2 == 0;
owners.add(Thread.ofVirtual().start(() -> {
try {
if (strict) {
manager.stopAndRemoveStrict(id, null);
} else {
manager.stopAndRemove(id, null, 1);
}
manager.stopAndRemoveStrict(id, null);
} catch (Throwable problem) {
failure.compareAndSet(null, problem);
}
}));
}
List<Thread> waiters = new ArrayList<>();
List<AtomicBoolean> interrupted = new ArrayList<>();
try {
assertTrue(removing.await(10, TimeUnit.SECONDS));
for (boolean strict : List.of(false, true)) {
String id = "waiting-" + strict;
AtomicBoolean preservedInterrupt = new AtomicBoolean();
interrupted.add(preservedInterrupt);
Thread waiter = Thread.ofVirtual().start(() -> {
try {
if (strict) {
manager.stopAndRemoveStrict(id, null);
} else {
manager.stopAndRemove(id, null);
}
preservedInterrupt.set(Thread.currentThread().isInterrupted());
} catch (Throwable problem) {
failure.compareAndSet(null, problem);
owners.add(Thread.ofVirtual().start(() -> {
try {
if ("strict".equals(mode)) {
manager.stopAndRemoveStrict("ninth", null);
} else if ("strict-volumes".equals(mode)) {
manager.stopAndRemoveStrict("ninth", null, 5, true);
} else if ("stop-only".equals(mode)) {
manager.stop("ninth", 5);
} else {
manager.stopAndRemove("ninth", null);
}
});
waiters.add(waiter);
awaitBlockedOrDone(waiter);
waiter.interrupt();
assertTrue(waiter.isAlive());
verify(docker, never()).stopContainerCmd(id);
verify(docker, never()).removeContainerCmd(id);
}
} catch (Throwable problem) {
failure.compareAndSet(null, problem);
}
}));
assertTrue(ninthStop.await(3, TimeUnit.SECONDS),
"Slow removals must not queue another owned container's stop past the shared drain budget");
} finally {
release.countDown();
for (Thread thread : owners) {
thread.join(10000);
assertFalse(thread.isAlive());
}
for (Thread thread : waiters) {
thread.join(10000);
assertFalse(thread.isAlive());
}
}
assertNull(failure.get());
for (AtomicBoolean preservedInterrupt : interrupted) {
assertTrue(preservedInterrupt.get());
}
for (boolean strict : List.of(false, true)) {
verify(docker).stopContainerCmd("waiting-" + strict);
verify(docker).removeContainerCmd("waiting-" + strict);
}
}
@Test
void removalFailuresReleaseAdmissionForLaterOwners() throws Exception {
DockerClient docker = mock(DockerClient.class);
ContainerLifecycleManager manager = new ContainerLifecycleManager(docker, null, null, null);
StopContainerCmd stop = mock(StopContainerCmd.class);
when(docker.stopContainerCmd(anyString())).thenReturn(stop);
when(stop.withTimeout(anyInt())).thenReturn(stop);
RemoveContainerCmd remove = mock(RemoveContainerCmd.class);
when(docker.removeContainerCmd(anyString())).thenReturn(remove);
when(remove.withForce(true)).thenReturn(remove);
when(remove.exec()).thenThrow(new IllegalStateException("daemon refused removal"));
ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
try {
executor.submit(() -> {
for (int i = 0; i < 16; i++) {
String id = "failed-" + i;
assertThrows(IllegalStateException.class,
() -> manager.stopAndRemoveStrict(id, null));
}
}).get(10, TimeUnit.SECONDS);
} finally {
executor.shutdownNow();
verify(docker).stopContainerCmd("ninth");
verify(ninthStopCommand).exec();
if ("stop-only".equals(mode)) {
verify(docker, never()).removeContainerCmd("ninth");
} else {
verify(docker).removeContainerCmd("ninth");
}
}