mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
94fcf5cbf3
commit
f5453b9585
2 files changed
+38
-107
No files matched your search
@@ -55,7 +55,6 @@ import java.util.OptionalLong;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -92,8 +91,6 @@ public class ContainerLifecycleManager {
|
||||
/** Host interface a port marked loopback-only publishes on. */
|
||||
private static final String LOOPBACK_HOST_IP = "127.0.0.1";
|
||||
|
||||
private final Semaphore teardownSlots = new Semaphore(8, true);
|
||||
|
||||
private final DockerClient dockerClient;
|
||||
private final ImageCacheService imageCacheService;
|
||||
private final ContainerDetector containerDetector;
|
||||
@@ -618,11 +615,7 @@ public class ContainerLifecycleManager {
|
||||
* SIGTERM before Docker sends SIGKILL. Must be a non-negative integer.
|
||||
*/
|
||||
public void stopAndRemove(String containerId, Closeable logStream, int stopTimeoutSeconds) {
|
||||
withTeardownSlot(() -> stopAndRemoveAdmitted(containerId, logStream, stopTimeoutSeconds));
|
||||
}
|
||||
|
||||
private void stopAndRemoveAdmitted(String containerId, Closeable logStream, int stopTimeoutSeconds) {
|
||||
boolean stoppedOrMissing = stopAdmitted(containerId, stopTimeoutSeconds);
|
||||
boolean stoppedOrMissing = stop(containerId, stopTimeoutSeconds);
|
||||
|
||||
// Force removal is the recovery path when Docker could not stop the container cleanly. It also
|
||||
// terminates the follow-log transport, allowing its terminal callback to drain the final tail.
|
||||
@@ -649,12 +642,9 @@ public class ContainerLifecycleManager {
|
||||
*
|
||||
* @param stopTimeoutSeconds seconds to wait for the container to stop cleanly after
|
||||
* SIGTERM before Docker sends SIGKILL. Must be a non-negative integer.
|
||||
* @return whether the container is stopped or already missing
|
||||
*/
|
||||
public void stop(String containerId, int stopTimeoutSeconds) {
|
||||
withTeardownSlot(() -> stopAdmitted(containerId, stopTimeoutSeconds));
|
||||
}
|
||||
|
||||
private boolean stopAdmitted(String containerId, int stopTimeoutSeconds) {
|
||||
public boolean stop(String containerId, int stopTimeoutSeconds) {
|
||||
LOG.infov("Stopping container {0}", containerId);
|
||||
try {
|
||||
dockerClient.stopContainerCmd(containerId).withTimeout(stopTimeoutSeconds).exec();
|
||||
@@ -705,12 +695,6 @@ public class ContainerLifecycleManager {
|
||||
/** Strict teardown that can also reclaim the container's anonymous volumes. */
|
||||
public void stopAndRemoveStrict(String containerId, Closeable logStream, int stopTimeoutSeconds,
|
||||
boolean removeAnonymousVolumes) {
|
||||
withTeardownSlot(() -> stopAndRemoveStrictAdmitted(
|
||||
containerId, logStream, stopTimeoutSeconds, removeAnonymousVolumes));
|
||||
}
|
||||
|
||||
private void stopAndRemoveStrictAdmitted(String containerId, Closeable logStream, int stopTimeoutSeconds,
|
||||
boolean removeAnonymousVolumes) {
|
||||
LOG.infov("Stopping container {0}", containerId);
|
||||
|
||||
Exception stopFailure = null;
|
||||
@@ -774,15 +758,6 @@ public class ContainerLifecycleManager {
|
||||
return presenceOf(containerId) == ContainerPresence.ABSENT;
|
||||
}
|
||||
|
||||
private void withTeardownSlot(Runnable stopAndRemove) {
|
||||
teardownSlots.acquireUninterruptibly();
|
||||
try {
|
||||
stopAndRemove.run();
|
||||
} finally {
|
||||
teardownSlots.release();
|
||||
}
|
||||
}
|
||||
|
||||
private void closeLogStreamIfPresent(Closeable logStream) {
|
||||
if (logStream != null) {
|
||||
closeLogStreamAfterContainerStop(logStream);
|
||||
|
||||
+35
-79
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user