mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(tests): Enhance ElastiCache test reliability and optimize thread … (#758)
* fix(tests): Enhance ElastiCache test reliability and optimize thread handling Introduced configurable socket timeout and retry logic in integration tests to handle stalling under load. Updated relay threads in the proxy to use platform daemon threads, preventing scheduling issues and improving response delivery in high-load scenarios. These changes improve test robustness and backend-client communication stability. * fix(tests): Add readiness probe for ElastiCache backend connections Introduce a retry-based readiness probe to ensure that ElastiCache handles backend availability before connecting. This prevents issues with premature connections, such as blocking on PONG responses, and improves stability of the container lifecycle process.
This commit is contained in:
1 parent
ded329beac
commit
0d8c6f8bd7
3 files changed
+116
-5
No files matched your search
+72
@@ -14,6 +14,12 @@ import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -30,6 +36,15 @@ public class ElastiCacheContainerManager {
|
||||
private static final Logger LOG = Logger.getLogger(ElastiCacheContainerManager.class);
|
||||
private static final int BACKEND_PORT = 6379;
|
||||
|
||||
/**
|
||||
* Docker can publish a host port before Valkey inside the container is listening. Without a probe,
|
||||
* the auth proxy may connect, forward PING, and block on PONG until the client times out.
|
||||
*/
|
||||
private static final int BACKEND_READY_DEADLINE_MS = 60_000;
|
||||
private static final int BACKEND_READY_RETRY_MS = 100;
|
||||
private static final int BACKEND_PROBE_CONNECT_MS = 2_000;
|
||||
private static final byte[] RESP_PING = "*1\r\n$4\r\nPING\r\n".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
private final ContainerBuilder containerBuilder;
|
||||
private final ContainerLifecycleManager lifecycleManager;
|
||||
private final ContainerLogStreamer logStreamer;
|
||||
@@ -100,9 +115,66 @@ public class ElastiCacheContainerManager {
|
||||
info.containerId(), logGroup, logStream, region, "elasticache:" + groupId);
|
||||
handle.setLogStream(logHandle);
|
||||
|
||||
waitForBackendReady(groupId, endpoint.host(), endpoint.port());
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
private static void waitForBackendReady(String groupId, String host, int port) {
|
||||
long deadline = System.currentTimeMillis() + BACKEND_READY_DEADLINE_MS;
|
||||
int attempt = 0;
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
attempt++;
|
||||
try (Socket s = new Socket()) {
|
||||
s.connect(new InetSocketAddress(host, port), BACKEND_PROBE_CONNECT_MS);
|
||||
s.setTcpNoDelay(true);
|
||||
s.setSoTimeout(BACKEND_PROBE_CONNECT_MS);
|
||||
OutputStream out = s.getOutputStream();
|
||||
out.write(RESP_PING);
|
||||
out.flush();
|
||||
String line = readAsciiLineCrLf(s.getInputStream());
|
||||
if (line.startsWith("+PONG")) {
|
||||
if (attempt > 1) {
|
||||
LOG.infov("ElastiCache backend ready for group {0} after {1} probe attempt(s)", groupId, attempt);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debugv("ElastiCache backend probe for group {0}: unexpected line {1}", groupId, line);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debugv("ElastiCache backend probe for group {0} attempt {1}: {2}", groupId, attempt, e.getMessage());
|
||||
}
|
||||
}
|
||||
try {
|
||||
Thread.sleep(BACKEND_READY_RETRY_MS);
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException("Interrupted while waiting for ElastiCache backend " + groupId, ie);
|
||||
}
|
||||
}
|
||||
throw new RuntimeException(
|
||||
"ElastiCache backend for group " + groupId + " did not become ready on " + host + ":" + port
|
||||
+ " within " + BACKEND_READY_DEADLINE_MS + "ms");
|
||||
}
|
||||
|
||||
private static String readAsciiLineCrLf(InputStream in) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int b;
|
||||
while ((b = in.read()) != -1) {
|
||||
if (b == '\r') {
|
||||
int next = in.read();
|
||||
if (next != '\n') {
|
||||
throw new IOException("Expected \\n after \\r in RESP line");
|
||||
}
|
||||
break;
|
||||
}
|
||||
sb.append((char) b);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void stop(ElastiCacheContainerHandle handle) {
|
||||
if (handle == null) {
|
||||
return;
|
||||
|
||||
+7
-2
@@ -159,10 +159,15 @@ public class ElastiCacheAuthProxy {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Relay I/O runs on platform daemon threads (not virtual threads). A parent virtual thread
|
||||
* from {@code handleConnection} blocks in {@code join} here; scheduling nested virtual-thread
|
||||
* relays under load can stall delivery of backend responses (e.g. PING/PONG) to the client.
|
||||
*/
|
||||
private void bridge(Socket client, Socket backend) {
|
||||
Thread t1 = Thread.ofVirtual().name("ec-relay-c2b-" + groupId)
|
||||
Thread t1 = Thread.ofPlatform().daemon(true).name("ec-relay-c2b-" + groupId)
|
||||
.start(() -> relay(client, backend));
|
||||
Thread t2 = Thread.ofVirtual().name("ec-relay-b2c-" + groupId)
|
||||
Thread t2 = Thread.ofPlatform().daemon(true).name("ec-relay-b2c-" + groupId)
|
||||
.start(() -> relay(backend, client));
|
||||
try {
|
||||
t1.join();
|
||||
|
||||
Reference in new issue
Block a user