mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(docker): read Docker streams on virtual threads (#9174)
docker-java read every stream (a log follow, a container wait, an exec's output, the events stream) on a platform thread of its own, so the threads grew with the containers. Floci now builds its DockerClients with FlociDockerCmdExecFactory, adapted from docker-java 3.7.1's DefaultDockerCmdExecFactory, DefaultInvocationBuilder and FramedInputStreamConsumer (Apache License 2.0), whose one change runs each stream on a virtual thread. Closing a stream still interrupts its thread and closes the response, and the client is still built over the launch bulkhead, the circuit breaker and the retry layer. The pool gauge's stream-thread count, which listed threads by name and cannot see virtual ones, now counts the running stream callbacks.
This commit is contained in:
1 parent
a9d0aab496
commit
c4238de1c1
8 files changed
+708
-38
No files matched your search
@@ -1,12 +1,24 @@
|
||||
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.model.Frame;
|
||||
import com.github.dockerjava.core.DefaultDockerClientConfig;
|
||||
import com.github.dockerjava.transport.DockerHttpClient;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class DockerPoolGaugeLoggerTest {
|
||||
|
||||
@@ -47,26 +59,74 @@ class DockerPoolGaugeLoggerTest {
|
||||
DockerPoolGaugeLogger.line(Map.of("control-plane", waiting), () -> 0));
|
||||
}
|
||||
|
||||
// Catches: a docker-java stream read on a platform thread of its own again, or the gauge losing count
|
||||
// of the virtual ones, which no thread-group listing sees.
|
||||
@Test
|
||||
void streamThreadsAreCountedByTheDockerJavaPrefix() throws InterruptedException {
|
||||
int before = DockerPoolGaugeLogger.countStreamThreads();
|
||||
Object release = new Object();
|
||||
Thread thread = new Thread(() -> {
|
||||
synchronized (release) {
|
||||
try {
|
||||
release.wait();
|
||||
} catch (InterruptedException expected) {
|
||||
// The test interrupts it once counted.
|
||||
}
|
||||
void aStreamCallbackRunsOnAVirtualThreadAndIsCountedWhileItRuns() throws Exception {
|
||||
CountDownLatch inCallback = new CountDownLatch(1);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
AtomicBoolean virtual = new AtomicBoolean();
|
||||
byte[] frame = {1, 0, 0, 0, 0, 0, 0, 5, 'h', 'e', 'l', 'l', 'o'};
|
||||
DockerHttpClient transport = new DockerHttpClient() {
|
||||
@Override
|
||||
public Response execute(Request request) {
|
||||
return new Response() {
|
||||
@Override
|
||||
public int getStatusCode() {
|
||||
return 200;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<String>> getHeaders() {
|
||||
return Map.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getBody() {
|
||||
return new ByteArrayInputStream(frame);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
// Nothing to release.
|
||||
}
|
||||
};
|
||||
}
|
||||
}, DockerPoolGaugeLogger.STREAM_THREAD_PREFIX + "test");
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
try {
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
// Nothing to release.
|
||||
}
|
||||
};
|
||||
DefaultDockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
|
||||
.withDockerHost("tcp://127.0.0.1:2375").build();
|
||||
int before = DockerPoolGaugeLogger.countStreamThreads();
|
||||
|
||||
try (DockerClient client = DockerClientProducer.newDockerClient(config, transport);
|
||||
ResultCallback.Adapter<Frame> callback = client.logContainerCmd("c1").withStdOut(true)
|
||||
.withFollowStream(true).exec(new ResultCallback.Adapter<>() {
|
||||
@Override
|
||||
public void onNext(Frame received) {
|
||||
virtual.set(Thread.currentThread().isVirtual());
|
||||
inCallback.countDown();
|
||||
try {
|
||||
release.await(5, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
})) {
|
||||
assertTrue(inCallback.await(5, TimeUnit.SECONDS), "the frame should reach the callback");
|
||||
assertTrue(virtual.get(), "the stream callback should run on a virtual thread");
|
||||
assertEquals(before + 1, DockerPoolGaugeLogger.countStreamThreads());
|
||||
} finally {
|
||||
thread.interrupt();
|
||||
thread.join(5_000);
|
||||
|
||||
release.countDown();
|
||||
assertTrue(callback.awaitCompletion(5, TimeUnit.SECONDS));
|
||||
}
|
||||
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
|
||||
while (DockerPoolGaugeLogger.countStreamThreads() != before && System.nanoTime() < deadline) {
|
||||
Thread.onSpinWait();
|
||||
}
|
||||
assertEquals(before, DockerPoolGaugeLogger.countStreamThreads(), "an ended stream is no longer counted");
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -94,7 +94,7 @@ class DockerPoolSaturationNightlyIntegrationTest {
|
||||
// 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,
|
||||
streaming = DockerClientProducer.newDockerClient(clientConfig,
|
||||
DockerClientProducer.wrapForRole(streamingTransport, "streaming", RESPONSE_TIMEOUT));
|
||||
dockerAvailable = DockerAvailability.answers(control);
|
||||
Assumptions.assumeTrue(dockerAvailable, "Docker daemon is unavailable");
|
||||
@@ -164,7 +164,7 @@ class DockerPoolSaturationNightlyIntegrationTest {
|
||||
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.
|
||||
// Each stream is read on a virtual thread of its own, counted while its callback runs.
|
||||
int streamThreads = DockerPoolGaugeLogger.countStreamThreads();
|
||||
assertTrue(streamThreads >= 2 * CONTAINERS, "one docker-java stream thread per open stream, counted "
|
||||
+ streamThreads);
|
||||
|
||||
Reference in new issue
Block a user