mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(docker): make the Docker client pool size configurable, default 1024 (#4276)
The shared DockerClient's connection pool was hard-coded to 100. Each live Lambda container holds two of those connections for its whole lifetime (the followed log stream and the exit watcher), so about 50 warm containers exhaust the pool. Create, start, stop and remove then wait for a free connection, including the idle-container stop that would release one, and the emulator stalls. Expose the size as floci.docker.max-connections (FLOCI_DOCKER_MAX_CONNECTIONS), reject values below 1, and default it to 1024 so that the 500-port Lambda runtime API range, not the Docker pool, is the ceiling on concurrent Lambda containers. Refs #2219, #2224 Co-authored-by: Goooooooooody <205637002+Goooooooooody@users.noreply.github.com>
This commit is contained in:
1 parent
8c2ceb5aa1
commit
76f8c09341
9 files changed
+163
-3
No files matched your search
@@ -110,4 +110,26 @@ class ApplicationDefaultsTest {
|
||||
assertEquals("true", fallback.value(),
|
||||
"a stock Floci should refuse a PostgreSQL IAM token generated for another endpoint, as RDS does");
|
||||
}
|
||||
|
||||
@Test
|
||||
void productionConfigSizesTheDockerPoolForTheLambdaRuntimeApiPortRange() throws IOException, NoSuchMethodException {
|
||||
JsonNode floci = new YAMLMapper().readTree(Path.of("src/main/resources/application.yml").toFile())
|
||||
.path("floci");
|
||||
int maxConnections = floci.path("docker").path("max-connections").asInt();
|
||||
JsonNode lambda = floci.path("services").path("lambda");
|
||||
int runtimeApiPorts = lambda.path("runtime-api-max-port").asInt()
|
||||
- lambda.path("runtime-api-base-port").asInt() + 1;
|
||||
|
||||
assertTrue(maxConnections >= 2 * runtimeApiPorts,
|
||||
"each live Lambda container holds two Docker connections (log follow and exit watcher), so "
|
||||
+ "max-connections " + maxConnections + " should cover the " + runtimeApiPorts
|
||||
+ " containers the runtime API port range allows");
|
||||
|
||||
WithDefault fallback = EmulatorConfig.DockerConfig.class
|
||||
.getMethod("maxConnections")
|
||||
.getAnnotation(WithDefault.class);
|
||||
assertNotNull(fallback, "maxConnections should declare a fallback default");
|
||||
assertEquals(String.valueOf(maxConnections), fallback.value(),
|
||||
"the @WithDefault must match application.yml, which wins over it");
|
||||
}
|
||||
}
|
||||
+85
@@ -1,11 +1,19 @@
|
||||
package io.github.hectorvent.floci.core.common.docker;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.async.ResultCallback;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.URI;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -13,9 +21,16 @@ import java.nio.file.Paths;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Bug condition exploration test for Docker host scheme normalization.
|
||||
@@ -610,4 +625,74 @@ class DockerClientProducerTest {
|
||||
"unix:///var/run/docker.sock", null, false, tempDir, null),
|
||||
"An incomplete TLS material directory should fail loudly rather than connect insecurely");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {0, -1})
|
||||
void validateMaxConnections_belowOne_throwsNamingTheSetting(int maxConnections) {
|
||||
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
|
||||
() -> DockerClientProducer.validateMaxConnections(maxConnections));
|
||||
|
||||
assertThat(e.getMessage(), containsString("floci.docker.max-connections"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateMaxConnections_positive_returnsItUnchanged() {
|
||||
assertEquals(1, DockerClientProducer.validateMaxConnections(1));
|
||||
assertEquals(1024, DockerClientProducer.validateMaxConnections(1024));
|
||||
}
|
||||
|
||||
/**
|
||||
* The pool size comes from {@code floci.docker.max-connections}. A followed log stream holds
|
||||
* its connection until the container exits, and this fake daemon never answers, so with a
|
||||
* pool of two only two of three streams may reach it; the third waits for a free connection.
|
||||
*/
|
||||
@Test
|
||||
void dockerClient_opensAtMostMaxConnectionsToTheDaemon() throws Exception {
|
||||
try (ServerSocket daemon = new ServerSocket(0, 50, InetAddress.getLoopbackAddress())) {
|
||||
BlockingQueue<Socket> accepted = new LinkedBlockingQueue<>();
|
||||
Thread acceptor = new Thread(() -> {
|
||||
try {
|
||||
while (!daemon.isClosed()) {
|
||||
accepted.add(daemon.accept());
|
||||
}
|
||||
} catch (IOException expected) {
|
||||
// Closing the server socket at the end of the test ends the accept loop.
|
||||
}
|
||||
});
|
||||
acceptor.setDaemon(true);
|
||||
acceptor.start();
|
||||
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.DockerConfig docker = mock(EmulatorConfig.DockerConfig.class);
|
||||
when(config.docker()).thenReturn(docker);
|
||||
when(docker.dockerHost()).thenReturn("tcp://127.0.0.1:" + daemon.getLocalPort());
|
||||
when(docker.dockerConfigPath()).thenReturn(Optional.empty());
|
||||
when(docker.maxConnections()).thenReturn(2);
|
||||
|
||||
DockerClient client = new DockerClientProducer(config).dockerClient();
|
||||
try {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
client.logContainerCmd("container-" + i)
|
||||
.withStdOut(true)
|
||||
.withFollowStream(true)
|
||||
.exec(new ResultCallback.Adapter<Frame>() {
|
||||
@Override
|
||||
public void onError(Throwable ignored) {
|
||||
// Expected once the client is closed under the pending streams.
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
assertNotNull(accepted.poll(10, TimeUnit.SECONDS), "first stream should connect");
|
||||
assertNotNull(accepted.poll(10, TimeUnit.SECONDS), "second stream should connect");
|
||||
assertNull(accepted.poll(1, TimeUnit.SECONDS),
|
||||
"third stream should wait for a pooled connection instead of opening one");
|
||||
} finally {
|
||||
client.close();
|
||||
for (Socket socket : accepted) {
|
||||
socket.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user