fix(pipes): stop Karapace before Docker client disposal (#7293)

This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-02 10:03:24 -04:00
1 parent 9b3943691a
commit 3963e03797
2 files changed
+338 -21

No files matched your search

@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.pipes;
import com.sun.net.httpserver.HttpServer;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.ContainerTeardown;
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
@@ -16,23 +17,36 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import static io.github.hectorvent.floci.testing.ThreadStates.awaitBlockedOrDone;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -79,6 +93,7 @@ class KarapaceManagerTest {
@AfterEach
void tearDown() {
manager.shutdown();
if (fakeRestServer != null) {
fakeRestServer.stop(0);
}
@@ -86,10 +101,14 @@ class KarapaceManagerTest {
/** Stands in for Karapace's own {@code GET /_health}, which {@code isReady} polls. */
private int startFakeRestServer() throws Exception {
return startFakeRestServer(new AtomicInteger(200));
}
private int startFakeRestServer(AtomicInteger healthStatus) throws Exception {
fakeRestServer = HttpServer.create(new InetSocketAddress("localhost", 0), 0);
fakeRestServer.createContext("/_health", exchange -> {
byte[] body = "{}".getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(200, body.length);
exchange.sendResponseHeaders(healthStatus.get(), body.length);
exchange.getResponseBody().write(body);
exchange.close();
});
@@ -208,7 +227,232 @@ class KarapaceManagerTest {
assertThrows(IllegalStateException.class,
() -> manager.ensureStarted("broker-1:9092"));
verify(lifecycleManager).stopAndRemove("container-1", null);
verify(lifecycleManager).stopAndRemoveStrict("container-1", null);
verify(portAllocator).release(54321);
}
@Test
void managedTeardownStopsEveryTargetAndPredestroyDoesNotStopThemAgain() throws Exception {
int hostPort = startFakeRestServer();
when(containerDetector.isRunningInContainer()).thenReturn(true);
when(lifecycleManager.createAndStart(any())).thenReturn(
new ContainerInfo("container-1", Map.of(REST_PORT, new EndpointInfo("localhost", hostPort))),
new ContainerInfo("container-2", Map.of(REST_PORT, new EndpointInfo("localhost", hostPort))));
manager.ensureStarted("broker-1:9092");
manager.ensureStarted("broker-2:9092");
ContainerTeardown teardown = assertInstanceOf(ContainerTeardown.class, manager);
teardown.stopManagedContainers();
manager.shutdown();
verify(lifecycleManager, times(1)).stopAndRemoveStrict("container-1", null);
verify(lifecycleManager, times(1)).stopAndRemoveStrict("container-2", null);
}
@Test
void managedTeardownClearsCachedTargetBeforeTheNextStart() throws Exception {
int hostPort = startFakeRestServer();
when(containerDetector.isRunningInContainer()).thenReturn(true);
when(lifecycleManager.createAndStart(any())).thenReturn(
new ContainerInfo("container-1", Map.of(REST_PORT, new EndpointInfo("localhost", hostPort))),
new ContainerInfo("container-2", Map.of(REST_PORT, new EndpointInfo("localhost", hostPort))));
manager.ensureStarted("broker-1:9092");
ContainerTeardown teardown = assertInstanceOf(ContainerTeardown.class, manager);
teardown.stopManagedContainers();
manager.ensureStarted("broker-1:9092");
teardown.stopManagedContainers();
verify(lifecycleManager, times(2)).createAndStart(any());
verify(lifecycleManager, times(1)).stopAndRemoveStrict("container-1", null);
verify(lifecycleManager, times(1)).stopAndRemoveStrict("container-2", null);
}
@Test
void failedManagedStopDoesNotPreventStoppingAnotherTarget() throws Exception {
int hostPort = startFakeRestServer();
when(containerDetector.isRunningInContainer()).thenReturn(true);
when(lifecycleManager.createAndStart(any())).thenReturn(
new ContainerInfo("container-1", Map.of(REST_PORT, new EndpointInfo("localhost", hostPort))),
new ContainerInfo("container-2", Map.of(REST_PORT, new EndpointInfo("localhost", hostPort))));
manager.ensureStarted("broker-1:9092");
manager.ensureStarted("broker-2:9092");
doThrow(new IllegalStateException("Docker stop failed"))
.when(lifecycleManager).stopAndRemoveStrict("container-1", null);
ContainerTeardown teardown = assertInstanceOf(ContainerTeardown.class, manager);
teardown.stopManagedContainers();
verify(lifecycleManager).stopAndRemoveStrict("container-1", null);
verify(lifecycleManager).stopAndRemoveStrict("container-2", null);
}
@Test
void hostModeResetReusesTheOnlyAvailablePort() throws Exception {
int endpointPort = startFakeRestServer();
int port;
try (ServerSocket socket = new ServerSocket(0)) {
port = socket.getLocalPort();
}
when(config.services().pipes().kafkaRestBridge().hostPortBase()).thenReturn(port);
when(config.services().pipes().kafkaRestBridge().hostPortMax()).thenReturn(port);
when(containerDetector.isRunningInContainer()).thenReturn(false);
PortAllocator allocator = new PortAllocator();
manager = new KarapaceManager(new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer),
lifecycleManager, containerDetector, config, allocator, dockerHostResolver, Duration.ofMillis(500));
when(lifecycleManager.createAndStart(any())).thenReturn(
new ContainerInfo("container-1", Map.of(REST_PORT, new EndpointInfo("localhost", endpointPort))),
new ContainerInfo("container-2", Map.of(REST_PORT, new EndpointInfo("localhost", endpointPort))));
manager.ensureStarted("broker:9092");
manager.stopManagedContainers();
assertDoesNotThrow(() -> manager.ensureStarted("broker:9092"));
manager.stopManagedContainers();
ArgumentCaptor<ContainerSpec> specs = ArgumentCaptor.forClass(ContainerSpec.class);
verify(lifecycleManager, times(2)).createAndStart(specs.capture());
for (ContainerSpec spec : specs.getAllValues()) {
assertEquals(port, spec.portBindings().get(REST_PORT));
}
assertEquals(port, allocator.allocate(port, port));
allocator.release(port);
}
@Test
void resetDrainsAnAdmittedStartAndRejectsNewStartsUntilCleanupEnds() throws Exception {
int hostPort = startFakeRestServer();
when(containerDetector.isRunningInContainer()).thenReturn(false);
when(portAllocator.allocate(9500, 9599)).thenReturn(hostPort);
CountDownLatch creating = new CountDownLatch(1);
CountDownLatch finishCreating = new CountDownLatch(1);
CountDownLatch cleanupEntered = new CountDownLatch(1);
CountDownLatch finishCleanup = new CountDownLatch(1);
lenient().doAnswer(call -> {
cleanupEntered.countDown();
assertTrue(finishCleanup.await(5, TimeUnit.SECONDS));
return null;
}).when(lifecycleManager).stopAndRemoveStrict("container-1", null);
when(lifecycleManager.createAndStart(any())).thenAnswer(call -> {
ContainerSpec spec = call.getArgument(0);
if (spec.env().contains("KARAPACE_BOOTSTRAP_URI=other:9092")) {
return new ContainerInfo("other-container",
Map.of(REST_PORT, new EndpointInfo("localhost", hostPort)));
}
creating.countDown();
assertTrue(finishCreating.await(5, TimeUnit.SECONDS));
return new ContainerInfo("container-1", Map.of(REST_PORT, new EndpointInfo("localhost", hostPort)));
});
FutureTask<URI> start = new FutureTask<>(() -> manager.ensureStarted("broker:9092"));
FutureTask<Void> reset = new FutureTask<>(() -> {
manager.stopManagedContainers();
return null;
});
Thread starting = new Thread(start, "karapace-test-start");
Thread resetting = new Thread(reset, "karapace-test-reset");
starting.start();
try {
assertTrue(creating.await(5, TimeUnit.SECONDS));
resetting.start();
awaitBlockedOrDone(resetting);
assertFalse(reset.isDone(), "reset must wait for the admitted create before taking ownership");
assertThrows(IllegalStateException.class, () -> manager.ensureStarted("other:9092"));
verify(lifecycleManager, never()).stopAndRemoveStrict("container-1", null);
finishCreating.countDown();
start.get(5, TimeUnit.SECONDS);
assertTrue(cleanupEntered.await(5, TimeUnit.SECONDS));
assertFalse(reset.isDone(), "reset must finish strict removal before reopening admission");
assertThrows(IllegalStateException.class, () -> manager.ensureStarted("other:9092"));
verify(portAllocator, never()).release(hostPort);
finishCleanup.countDown();
reset.get(5, TimeUnit.SECONDS);
verify(lifecycleManager).stopAndRemoveStrict("container-1", null);
verify(portAllocator).release(hostPort);
manager.ensureStarted("broker:9092");
verify(lifecycleManager, times(2)).createAndStart(any());
} finally {
finishCreating.countDown();
finishCleanup.countDown();
starting.join(5000);
resetting.join(5000);
assertFalse(starting.isAlive());
assertFalse(resetting.isAlive());
}
}
@Test
void failedStopRetainsContainerAndPortForRetry() throws Exception {
int hostPort = startFakeRestServer();
when(containerDetector.isRunningInContainer()).thenReturn(false);
when(portAllocator.allocate(9500, 9599)).thenReturn(hostPort);
when(lifecycleManager.createAndStart(any())).thenReturn(
new ContainerInfo("container-1", Map.of(REST_PORT, new EndpointInfo("localhost", hostPort))),
new ContainerInfo("container-2", Map.of(REST_PORT, new EndpointInfo("localhost", hostPort))));
manager.ensureStarted("broker:9092");
lenient().doThrow(new IllegalStateException("Docker stop failed")).doNothing()
.when(lifecycleManager).stopAndRemoveStrict("container-1", null);
manager.stopManagedContainers();
verify(portAllocator, never()).release(hostPort);
assertDoesNotThrow(() -> manager.ensureStarted("broker:9092"));
verify(lifecycleManager, times(2)).stopAndRemoveStrict("container-1", null);
verify(portAllocator).release(hostPort);
manager.stopManagedContainers();
verify(lifecycleManager).stopAndRemoveStrict("container-2", null);
verify(portAllocator, times(2)).release(hostPort);
}
@Test
void failedReadinessCleanupRetriesBeforeAllocatingAReplacement() throws Exception {
AtomicInteger healthStatus = new AtomicInteger(503);
int hostPort = startFakeRestServer(healthStatus);
when(containerDetector.isRunningInContainer()).thenReturn(false);
when(portAllocator.allocate(9500, 9599)).thenReturn(hostPort);
when(lifecycleManager.createAndStart(any())).thenReturn(
new ContainerInfo("container-1", Map.of(REST_PORT, new EndpointInfo("localhost", hostPort))),
new ContainerInfo("container-2", Map.of(REST_PORT, new EndpointInfo("localhost", hostPort))));
IllegalStateException cleanupFailure = new IllegalStateException("Docker removal unavailable");
lenient().doThrow(cleanupFailure).doThrow(cleanupFailure).doNothing()
.when(lifecycleManager).stopAndRemoveStrict("container-1", null);
IllegalStateException readinessFailure = assertThrows(IllegalStateException.class,
() -> manager.ensureStarted("broker:9092"));
assertTrue(readinessFailure.getMessage().contains("did not become ready"));
assertEquals(1, readinessFailure.getSuppressed().length);
assertSame(cleanupFailure, readinessFailure.getSuppressed()[0]);
verify(portAllocator, never()).release(hostPort);
assertSame(cleanupFailure, assertThrows(IllegalStateException.class,
() -> manager.ensureStarted("broker:9092")));
verify(portAllocator, never()).release(hostPort);
verify(portAllocator).allocate(9500, 9599);
verify(lifecycleManager).createAndStart(any());
healthStatus.set(200);
assertEquals(URI.create("http://localhost:" + hostPort + "/"), manager.ensureStarted("broker:9092"));
InOrder cleanupOrder = inOrder(lifecycleManager, portAllocator);
cleanupOrder.verify(lifecycleManager, times(3)).stopAndRemoveStrict("container-1", null);
cleanupOrder.verify(portAllocator).release(hostPort);
cleanupOrder.verify(portAllocator).allocate(9500, 9599);
cleanupOrder.verify(lifecycleManager).createAndStart(any());
verify(lifecycleManager, times(3)).stopAndRemoveStrict("container-1", null);
verify(portAllocator).release(hostPort);
verify(portAllocator, times(2)).allocate(9500, 9599);
verify(lifecycleManager, times(2)).createAndStart(any());
manager.stopManagedContainers();
verify(lifecycleManager).stopAndRemoveStrict("container-2", null);
verify(portAllocator, times(2)).release(hostPort);
}
@Test
void createFailureReleasesItsAllocatedPort() {
when(containerDetector.isRunningInContainer()).thenReturn(false);
when(portAllocator.allocate(9500, 9599)).thenReturn(9500);
IllegalStateException failure = new IllegalStateException("create failed");
when(lifecycleManager.createAndStart(any())).thenThrow(failure);
assertSame(failure, assertThrows(IllegalStateException.class, () -> manager.ensureStarted("broker:9092")));
verify(portAllocator).release(9500);
}
}