mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(docker): count open Docker streams per service and add a saturation harness (#8953)
* feat(docker): count open Docker streams per service and add a saturation harness * fix(docker): match container routes at the start of the path, and tidy the harness * test(docker): drop the saturation harness's quiet-stream sleep
This commit is contained in:
1 parent
43d6ea4961
commit
b9ebbe0a42
12 files changed
+629
-25
No files matched your search
+16
@@ -101,6 +101,22 @@ class ContainerLifecycleManagerLabelsTest {
|
||||
capturedLabels(createCmd));
|
||||
}
|
||||
|
||||
// Catches: a container created without being recorded, so every stream that follows it is
|
||||
// reported as unattributed in the Docker pool line.
|
||||
@Test
|
||||
void createRecordsTheServiceTheContainerIsLabelledWith() {
|
||||
stubCreateContainer();
|
||||
|
||||
try {
|
||||
manager().create(specWithLabels(
|
||||
ContainerStorageHelper.resourceIdentityLabels("rds", "db-1", "000000000000", "us-east-1")));
|
||||
|
||||
assertEquals("rds", DockerContainerServices.serviceOf("container-id"));
|
||||
} finally {
|
||||
DockerContainerServices.forget("container-id");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWritesTheLegacyAliasOfEachNewKeyWithTheSameValue() {
|
||||
CreateContainerCmd createCmd = stubCreateContainer();
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package io.floci.aws.core.common.docker;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class DockerContainerServicesTest {
|
||||
|
||||
@AfterEach
|
||||
void forgetTheTestContainers() {
|
||||
DockerContainerServices.forget("services-test-id");
|
||||
DockerContainerServices.forget("services-test-unlabelled");
|
||||
DockerContainerServices.forget("services-test-old");
|
||||
DockerContainerServices.forget("services-test-name");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aContainerWithoutTheServiceLabelIsUnattributed() {
|
||||
DockerContainerServices.record("services-test-unlabelled", null, Map.of("floci", "true"));
|
||||
|
||||
assertEquals(DockerContainerServices.UNATTRIBUTED,
|
||||
DockerContainerServices.serviceOf("services-test-unlabelled"));
|
||||
assertEquals(DockerContainerServices.UNATTRIBUTED, DockerContainerServices.serviceOf(null));
|
||||
}
|
||||
|
||||
// Catches: forgetting a container removed outside the transport also wiping a newer container's
|
||||
// entry under the same name, so that one's name-keyed streams read as unattributed.
|
||||
@Test
|
||||
void forgettingAnOldContainerLeavesANewerOneOfTheSameNameAttributed() {
|
||||
DockerContainerServices.record("services-test-old", "services-test-name",
|
||||
Map.of(ContainerStorageHelper.SERVICE_LABEL, "lambda"));
|
||||
DockerContainerServices.record("services-test-id", "services-test-name",
|
||||
Map.of(ContainerStorageHelper.SERVICE_LABEL, "ecs"));
|
||||
|
||||
DockerContainerServices.forget("services-test-old");
|
||||
|
||||
assertEquals("ecs", DockerContainerServices.serviceOf("services-test-name"));
|
||||
assertEquals("ecs", DockerContainerServices.serviceOf("services-test-id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aContainerIsKnownByItsIdAndItsNameUntilEitherIsForgotten() {
|
||||
DockerContainerServices.record("services-test-id", "services-test-name",
|
||||
Map.of(ContainerStorageHelper.SERVICE_LABEL, "opensearch"));
|
||||
assertEquals("opensearch", DockerContainerServices.serviceOf("services-test-id"));
|
||||
assertEquals("opensearch", DockerContainerServices.serviceOf("services-test-name"));
|
||||
|
||||
DockerContainerServices.forget("services-test-name");
|
||||
|
||||
assertEquals(DockerContainerServices.UNATTRIBUTED, DockerContainerServices.serviceOf("services-test-id"));
|
||||
assertEquals(DockerContainerServices.UNATTRIBUTED, DockerContainerServices.serviceOf("services-test-name"));
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
class DockerPoolGaugeLoggerTest {
|
||||
|
||||
private static final FlociDockerHttpClient.PoolGauges IDLE =
|
||||
new FlociDockerHttpClient.PoolGauges(0, 3, 0, 1024, Map.of());
|
||||
new FlociDockerHttpClient.PoolGauges(0, 3, 0, 1024, Map.of(), Map.of());
|
||||
|
||||
// Catches: an idle emulator writing a pool line every interval, or paying for a thread scan on a
|
||||
// reading that writes nothing.
|
||||
@@ -26,11 +26,13 @@ class DockerPoolGaugeLoggerTest {
|
||||
void aBusyPoolReportsEveryPoolInNameOrderWithItsStreamsAndTheStreamThreads() {
|
||||
Map<String, FlociDockerHttpClient.PoolGauges> readings = new TreeMap<>();
|
||||
readings.put("streaming", new FlociDockerHttpClient.PoolGauges(40, 0, 0, 2048,
|
||||
new TreeMap<>(Map.of("wait", 20, "logs", 20))));
|
||||
new TreeMap<>(Map.of("wait", 20, "logs", 20)),
|
||||
new TreeMap<>(Map.of("lambda", 36, "unattributed", 4))));
|
||||
readings.put("control-plane", IDLE);
|
||||
|
||||
assertEquals("control-plane leased=0 available=3 pending=0 max=1024; "
|
||||
+ "streaming leased=40 available=0 pending=0 max=2048 streams[logs=20 wait=20]; "
|
||||
+ "streaming leased=40 available=0 pending=0 max=2048 streams[logs=20 wait=20] "
|
||||
+ "services[lambda=36 unattributed=4]; "
|
||||
+ "stream threads=40",
|
||||
DockerPoolGaugeLogger.line(readings, () -> 40));
|
||||
}
|
||||
@@ -38,7 +40,8 @@ class DockerPoolGaugeLoggerTest {
|
||||
// Catches: callers waiting on a full pool reading as idle because no connection is leased yet.
|
||||
@Test
|
||||
void aCallerWaitingForAConnectionIsNotIdle() {
|
||||
FlociDockerHttpClient.PoolGauges waiting = new FlociDockerHttpClient.PoolGauges(0, 0, 2, 1, Map.of());
|
||||
FlociDockerHttpClient.PoolGauges waiting =
|
||||
new FlociDockerHttpClient.PoolGauges(0, 0, 2, 1, Map.of(), Map.of());
|
||||
|
||||
assertEquals("control-plane leased=0 available=0 pending=2 max=1; stream threads=0",
|
||||
DockerPoolGaugeLogger.line(Map.of("control-plane", waiting), () -> 0));
|
||||
|
||||
+303
@@ -0,0 +1,303 @@
|
||||
package io.floci.aws.core.common.docker;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.async.ResultCallback;
|
||||
import com.github.dockerjava.api.command.PullImageResultCallback;
|
||||
import com.github.dockerjava.api.exception.NotFoundException;
|
||||
import com.github.dockerjava.api.model.Container;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import com.github.dockerjava.api.model.WaitResponse;
|
||||
import com.github.dockerjava.core.DefaultDockerClientConfig;
|
||||
import com.github.dockerjava.core.DockerClientImpl;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.testing.DockerAvailability;
|
||||
import io.floci.aws.testing.TestImages;
|
||||
import io.smallrye.config.SmallRyeConfigBuilder;
|
||||
import org.apache.hc.core5.http.ConnectionRequestTimeoutException;
|
||||
import org.jboss.logging.Logger;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* Saturation harness for the Docker transport (BL-706). It starts idle containers the way Lambda
|
||||
* does, each followed by a log stream and a container wait on the streaming pool, in steps, and at
|
||||
* each step logs what one more container costs: its start latency, the latency of a control-plane
|
||||
* ping, the streaming pool's leases and the docker-java stream threads. These are the numbers the
|
||||
* admission work (BL-212, BL-216, BL-217) is sized from.
|
||||
*
|
||||
* <p>It holds stream leases, which no response timeout releases, so it cannot drain itself (that
|
||||
* these streams carry no response timeout is covered by {@link FlociDockerHttpClientTest}). The
|
||||
* streaming pool is sized for exactly the containers the ramp starts, so the stream of one more waits for a
|
||||
* connection and fails with the lease timeout, the failure a saturated Floci reports.
|
||||
*
|
||||
* <p>Each container carries the {@code io.floci.service} label and is recorded the way
|
||||
* {@link ContainerLifecycleManager} records the containers it creates, so the streams it holds are
|
||||
* checked to be counted under that service, and its remove to forget it.
|
||||
*
|
||||
* <p>{@code -Ddocker.saturation.containers=N} raises the ramp (default 40) to measure a host; the
|
||||
* three-minute bound fits the default on a hosted runner, so a much larger ramp needs it raised too.
|
||||
*/
|
||||
@EnabledIfSystemProperty(named = "nightly.checks.enabled", matches = "true")
|
||||
class DockerPoolSaturationNightlyIntegrationTest {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(DockerPoolSaturationNightlyIntegrationTest.class);
|
||||
|
||||
private static final String LABEL = "floci.test";
|
||||
private static final String LABEL_VALUE = "docker-pool-saturation";
|
||||
/** The service the ramp's containers stand in for, as Lambda's log follow and exit wait. */
|
||||
private static final String SERVICE = "lambda";
|
||||
private static final int CONTAINERS = Integer.getInteger("docker.saturation.containers", 40);
|
||||
private static final int STEP = Math.max(1, CONTAINERS / 4);
|
||||
/** The streaming pool's response timeout, which its long-lived streams do not carry. */
|
||||
private static final Duration RESPONSE_TIMEOUT = Duration.ofSeconds(3);
|
||||
/** The control plane's, long enough for a create, start or remove on a loaded runner. */
|
||||
private static final Duration CONTROL_TIMEOUT = Duration.ofSeconds(60);
|
||||
private static final Duration LEASE_TIMEOUT = Duration.ofSeconds(2);
|
||||
|
||||
private FlociDockerHttpClient streamingTransport;
|
||||
private DockerClient control;
|
||||
private DockerClient streaming;
|
||||
private boolean dockerAvailable;
|
||||
private final List<String> containers = Collections.synchronizedList(new ArrayList<>());
|
||||
private final List<Closeable> streams = Collections.synchronizedList(new ArrayList<>());
|
||||
|
||||
@BeforeEach
|
||||
void connect() {
|
||||
EmulatorConfig config = new SmallRyeConfigBuilder().withMapping(EmulatorConfig.class)
|
||||
.build().getConfigMapping(EmulatorConfig.class);
|
||||
String host = DockerClientProducer.resolveDockerConnection(config.docker(), System::getenv).host();
|
||||
DefaultDockerClientConfig clientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder()
|
||||
.withDockerHost(host).build();
|
||||
control = DockerClientImpl.getInstance(clientConfig, DockerClientProducer.wrapForRole(
|
||||
DockerClientProducer.newHttpClient(clientConfig, 16, CONTROL_TIMEOUT, LEASE_TIMEOUT), "control-plane",
|
||||
CONTROL_TIMEOUT));
|
||||
// Two streams per container, the log follow and the wait, and not one connection more.
|
||||
streamingTransport = DockerClientProducer.newHttpClient(clientConfig, 2 * CONTAINERS, RESPONSE_TIMEOUT,
|
||||
LEASE_TIMEOUT);
|
||||
streaming = DockerClientImpl.getInstance(clientConfig,
|
||||
DockerClientProducer.wrapForRole(streamingTransport, "streaming", RESPONSE_TIMEOUT));
|
||||
dockerAvailable = DockerAvailability.answers(control);
|
||||
Assumptions.assumeTrue(dockerAvailable, "Docker daemon is unavailable");
|
||||
removeLeftovers();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() throws IOException {
|
||||
try {
|
||||
synchronized (streams) {
|
||||
for (Closeable stream : streams) {
|
||||
closeQuietly(stream);
|
||||
}
|
||||
}
|
||||
if (control != null && dockerAvailable) {
|
||||
synchronized (containers) {
|
||||
for (String id : containers) {
|
||||
removeQuietly(id);
|
||||
}
|
||||
}
|
||||
removeLeftovers();
|
||||
}
|
||||
} finally {
|
||||
// A cleanup that throws still leaves no connection pool open for the rest of the run.
|
||||
try {
|
||||
if (control != null) {
|
||||
control.close();
|
||||
}
|
||||
} finally {
|
||||
if (streaming != null) {
|
||||
streaming.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(value = 3, unit = TimeUnit.MINUTES)
|
||||
void aFullStreamingPoolMakesTheNextStreamWaitThenFailOnTheLeaseTimeout() throws Exception {
|
||||
try (PullImageResultCallback pulled = new PullImageResultCallback()) {
|
||||
assertTrue(control.pullImageCmd(TestImages.BUSYBOX).exec(pulled).awaitCompletion(120, TimeUnit.SECONDS),
|
||||
"pulling " + TestImages.BUSYBOX);
|
||||
}
|
||||
|
||||
List<String> report = new ArrayList<>();
|
||||
report.add("containers start-ms(median) ping-ms(median) streaming-leased pending stream-threads");
|
||||
for (int started = 0; started < CONTAINERS; ) {
|
||||
int target = Math.min(CONTAINERS, started + STEP);
|
||||
List<Long> startMillis = new ArrayList<>();
|
||||
for (; started < target; started++) {
|
||||
String id = createIdleContainer();
|
||||
long begin = System.nanoTime();
|
||||
control.startContainerCmd(id).exec();
|
||||
startMillis.add(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - begin));
|
||||
followLikeLambda(id);
|
||||
}
|
||||
int expected = 2 * target;
|
||||
FlociDockerHttpClient.PoolGauges reading = awaitGauges(gauges -> gauges.leased() == expected,
|
||||
"the streaming pool to hold " + expected + " streams");
|
||||
report.add(String.format("%10d %16d %15d %16d %7d %14d", target, median(startMillis),
|
||||
median(pingMillis()), reading.leased(), reading.pending(),
|
||||
DockerPoolGaugeLogger.countStreamThreads()));
|
||||
}
|
||||
LOG.infov("Docker pool saturation, {0} containers in steps of {1}:\n{2}", CONTAINERS, STEP,
|
||||
String.join("\n", report));
|
||||
|
||||
FlociDockerHttpClient.PoolGauges quiet = streamingTransport.gauges();
|
||||
assertEquals(2 * CONTAINERS, quiet.leased(), "every container should hold both of its streams: " + quiet);
|
||||
assertEquals(Map.of("logs", CONTAINERS, "wait", CONTAINERS), quiet.openStreams());
|
||||
// docker-java reads each stream on a thread of its own, so the threads grow with the streams too.
|
||||
int streamThreads = DockerPoolGaugeLogger.countStreamThreads();
|
||||
assertTrue(streamThreads >= 2 * CONTAINERS, "one docker-java stream thread per open stream, counted "
|
||||
+ streamThreads);
|
||||
assertEquals(Map.of(SERVICE, 2 * CONTAINERS), quiet.openStreamsByService(),
|
||||
"every stream should be counted under the service of the container it follows");
|
||||
|
||||
// The pool is full: one more container's log stream waits for a connection, then fails.
|
||||
String extra = createIdleContainer();
|
||||
control.startContainerCmd(extra).exec();
|
||||
CompletableFuture<Throwable> failure = new CompletableFuture<>();
|
||||
long begin = System.nanoTime();
|
||||
streams.add(followLogs(extra, new ResultCallback.Adapter<Frame>() {
|
||||
@Override
|
||||
public void onError(Throwable throwable) {
|
||||
failure.complete(throwable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
failure.complete(null);
|
||||
}
|
||||
}));
|
||||
FlociDockerHttpClient.PoolGauges waiting = awaitGauges(gauges -> gauges.pending() >= 1,
|
||||
"the extra stream to wait for a connection");
|
||||
assertEquals(2 * CONTAINERS, waiting.leased(), waiting.toString());
|
||||
|
||||
Throwable error = failure.get(LEASE_TIMEOUT.multipliedBy(10).toMillis(), TimeUnit.MILLISECONDS);
|
||||
long waitedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - begin);
|
||||
assertTrue(error != null, "the extra stream should fail, not complete");
|
||||
assertInstanceOf(ConnectionRequestTimeoutException.class, rootCause(error), "got " + error);
|
||||
assertTrue(waitedMillis >= LEASE_TIMEOUT.toMillis() - 200,
|
||||
"should fail after the " + LEASE_TIMEOUT.toMillis() + " ms lease timeout, took " + waitedMillis + " ms");
|
||||
LOG.infov("With {0} streams open the next one waited {1} ms and failed with {2}", 2 * CONTAINERS,
|
||||
waitedMillis, rootCause(error).getClass().getSimpleName());
|
||||
|
||||
// A remove that succeeds forgets the container, whichever transport sent it.
|
||||
assertEquals(SERVICE, DockerContainerServices.serviceOf(extra));
|
||||
control.removeContainerCmd(extra).withForce(true).exec();
|
||||
assertEquals(DockerContainerServices.UNATTRIBUTED, DockerContainerServices.serviceOf(extra));
|
||||
}
|
||||
|
||||
private String createIdleContainer() {
|
||||
Map<String, String> labels = Map.of(LABEL, LABEL_VALUE, ContainerStorageHelper.SERVICE_LABEL, SERVICE);
|
||||
String id = control.createContainerCmd(TestImages.BUSYBOX)
|
||||
.withCmd("sleep", "600")
|
||||
.withLabels(labels)
|
||||
.exec().getId();
|
||||
DockerContainerServices.record(id, null, labels);
|
||||
containers.add(id);
|
||||
return id;
|
||||
}
|
||||
|
||||
/** The two streams a Lambda container holds on the streaming pool: its log follow and its exit wait. */
|
||||
private void followLikeLambda(String id) {
|
||||
streams.add(followLogs(id, new ResultCallback.Adapter<Frame>() {
|
||||
@Override
|
||||
public void onError(Throwable ignored) {
|
||||
// Expected once the test closes the stream.
|
||||
}
|
||||
}));
|
||||
streams.add(streaming.waitContainerCmd(id).exec(new ResultCallback.Adapter<WaitResponse>() {
|
||||
@Override
|
||||
public void onError(Throwable ignored) {
|
||||
// Expected once the test closes the stream.
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
/** Follows a container's stdout and stderr on the streaming pool, as Lambda's log follow does. */
|
||||
private <T extends ResultCallback<Frame>> T followLogs(String id, T callback) {
|
||||
return streaming.logContainerCmd(id).withStdOut(true).withStdErr(true).withFollowStream(true).exec(callback);
|
||||
}
|
||||
|
||||
private List<Long> pingMillis() {
|
||||
List<Long> millis = new ArrayList<>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
long begin = System.nanoTime();
|
||||
control.pingCmd().exec();
|
||||
millis.add(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - begin));
|
||||
}
|
||||
return millis;
|
||||
}
|
||||
|
||||
private FlociDockerHttpClient.PoolGauges awaitGauges(Predicate<FlociDockerHttpClient.PoolGauges> condition,
|
||||
String what) throws InterruptedException {
|
||||
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(30);
|
||||
FlociDockerHttpClient.PoolGauges reading = streamingTransport.gauges();
|
||||
while (!condition.test(reading)) {
|
||||
if (System.nanoTime() > deadline) {
|
||||
fail("timed out waiting for " + what + ": " + reading);
|
||||
}
|
||||
Thread.sleep(50);
|
||||
reading = streamingTransport.gauges();
|
||||
}
|
||||
return reading;
|
||||
}
|
||||
|
||||
private static long median(List<Long> values) {
|
||||
List<Long> sorted = new ArrayList<>(values);
|
||||
Collections.sort(sorted);
|
||||
return sorted.get(sorted.size() / 2);
|
||||
}
|
||||
|
||||
private static Throwable rootCause(Throwable error) {
|
||||
Throwable cause = error;
|
||||
while (cause.getCause() != null && cause.getCause() != cause) {
|
||||
cause = cause.getCause();
|
||||
}
|
||||
return cause;
|
||||
}
|
||||
|
||||
private void removeLeftovers() {
|
||||
for (Container container : control.listContainersCmd().withShowAll(true)
|
||||
.withLabelFilter(Map.of(LABEL, LABEL_VALUE)).exec()) {
|
||||
removeQuietly(container.getId());
|
||||
}
|
||||
}
|
||||
|
||||
private void removeQuietly(String id) {
|
||||
try {
|
||||
control.removeContainerCmd(id).withForce(true).exec();
|
||||
} catch (NotFoundException expected) {
|
||||
LOG.debugv("Container {0} was already removed", id);
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv(e, "Container {0} could not be removed; it may be left running", id);
|
||||
}
|
||||
}
|
||||
|
||||
private static void closeQuietly(Closeable stream) {
|
||||
try {
|
||||
stream.close();
|
||||
} catch (IOException e) {
|
||||
LOG.debugv(e, "Closing a stream at teardown");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -395,6 +395,90 @@ class FlociDockerHttpClientTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void streamContainerNamesTheContainerAStreamFollows() {
|
||||
assertEquals("abc", FlociDockerHttpClient.streamContainer(request("POST /v1.43/containers/abc/wait")));
|
||||
assertEquals("abc", FlociDockerHttpClient.streamContainer(request("GET /containers/abc/logs?follow=1")));
|
||||
assertEquals("fn-1", FlociDockerHttpClient.streamContainer(request("POST /containers/fn-1/attach?stream=1")));
|
||||
assertEquals("abc", FlociDockerHttpClient.streamContainer(request("GET /containers/abc/stats")));
|
||||
// A container may be named like a collection route.
|
||||
assertEquals("create", FlociDockerHttpClient.streamContainer(request("GET /containers/create/logs?follow=1")));
|
||||
assertEquals("json", FlociDockerHttpClient.streamContainer(request("POST /v1.43/containers/json/wait")));
|
||||
for (String noContainer : List.of("POST /exec/abc/start", "POST /images/create?fromImage=alpine",
|
||||
"POST /build", "GET /events", "GET /containers/json", "GET /containers//", "DELETE /containers//",
|
||||
"GET /images/quay.io/containers/podman/json")) {
|
||||
assertNull(FlociDockerHttpClient.streamContainer(request(noContainer)), noContainer);
|
||||
}
|
||||
}
|
||||
|
||||
// Catches: streams reported under the wrong service, or a removed container whose id is still
|
||||
// attributed to its old service (and kept in memory) after the remove succeeded.
|
||||
@Test
|
||||
@Timeout(30)
|
||||
void gaugesCountOpenStreamsByTheServiceOfTheContainerTheyFollow() throws Exception {
|
||||
List<Socket> accepted = new CopyOnWriteArrayList<>();
|
||||
List<DockerHttpClient.Response> open = new CopyOnWriteArrayList<>();
|
||||
DockerContainerServices.record("fn-1", "floci-lambda-fn",
|
||||
ContainerStorageHelper.resourceIdentityLabels("lambda", "fn", "000000000000", "us-east-1"));
|
||||
DockerContainerServices.record("task-1", null,
|
||||
ContainerStorageHelper.resourceIdentityLabels("ecs", "task", "000000000000", "us-east-1"));
|
||||
try (ServerSocket server = new ServerSocket(0)) {
|
||||
Thread acceptor = Thread.ofVirtual().start(() -> {
|
||||
try {
|
||||
while (true) {
|
||||
Socket socket = server.accept();
|
||||
accepted.add(socket);
|
||||
socket.getOutputStream().write(("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n"
|
||||
+ "Transfer-Encoding: chunked\r\n\r\n").getBytes(StandardCharsets.US_ASCII));
|
||||
socket.getOutputStream().flush();
|
||||
}
|
||||
} catch (IOException expected) {
|
||||
// The server socket closed at the end of the test.
|
||||
}
|
||||
});
|
||||
try (FlociDockerHttpClient client = new FlociDockerHttpClient.Builder()
|
||||
.dockerHost(URI.create("tcp://127.0.0.1:" + server.getLocalPort()))
|
||||
.maxConnections(8)
|
||||
.responseTimeout(Duration.ofSeconds(30))
|
||||
.connectionRequestTimeout(Duration.ofSeconds(5))
|
||||
.build()) {
|
||||
for (String stream : List.of("POST /containers/fn-1/wait",
|
||||
"GET /containers/floci-lambda-fn/logs?follow=1",
|
||||
"POST /containers/task-1/wait", "POST /exec/e-1/start")) {
|
||||
open.add(client.execute(request(stream)));
|
||||
}
|
||||
|
||||
FlociDockerHttpClient.PoolGauges held = client.gauges();
|
||||
assertEquals(Map.of("lambda", 2, "ecs", 1, DockerContainerServices.UNATTRIBUTED, 1),
|
||||
held.openStreamsByService(), held.toString());
|
||||
assertEquals(Map.of("wait", 2, "logs", 1, "exec", 1), held.openStreams(), held.toString());
|
||||
|
||||
// An image whose name has containers/ in it is not a container remove.
|
||||
open.add(client.execute(request("DELETE /images/quay.io/containers/fn-1")));
|
||||
assertEquals("lambda", DockerContainerServices.serviceOf("fn-1"));
|
||||
|
||||
open.add(client.execute(request("DELETE /containers/fn-1")));
|
||||
assertEquals(DockerContainerServices.UNATTRIBUTED, DockerContainerServices.serviceOf("fn-1"));
|
||||
assertEquals(DockerContainerServices.UNATTRIBUTED, DockerContainerServices.serviceOf("floci-lambda-fn"),
|
||||
"removing a container by its id forgets its name too");
|
||||
assertEquals("ecs", DockerContainerServices.serviceOf("task-1"));
|
||||
|
||||
for (DockerHttpClient.Response response : open) {
|
||||
response.close();
|
||||
}
|
||||
assertEquals(Map.of(), client.gauges().openStreamsByService());
|
||||
} finally {
|
||||
for (Socket socket : accepted) {
|
||||
socket.close();
|
||||
}
|
||||
acceptor.interrupt();
|
||||
}
|
||||
} finally {
|
||||
DockerContainerServices.forget("fn-1");
|
||||
DockerContainerServices.forget("task-1");
|
||||
}
|
||||
}
|
||||
|
||||
private static DockerHttpClient.Request request(String methodAndPath) {
|
||||
String[] parts = methodAndPath.split(" ", 2);
|
||||
return DockerHttpClient.Request.builder().method(parts[0]).path(parts[1]).build();
|
||||
|
||||
Reference in new issue
Block a user