From 328b6f6c17eec593b772d3cf60a7c9ef1619a180 Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Tue, 15 Sep 2026 22:56:59 -0400 Subject: [PATCH] refactor(elasticache): dedupe RESP line-reader with MemoryDB (#3708) ElastiCacheContainerManager and MemoryDbContainerManager each carried an identical 21-line readAsciiLineCrLf helper used while probing a backend container for readiness, found via CPD (#3702). Extracted it into RespLineReader in elasticache.container, and pointed MemoryDbContainerManager at it, the same "MemoryDB depends on ElastiCache's shared Redis-wire-protocol logic" shape AbstractRedisAuthProxy already follows for the auth-proxy path (#3568). No behavior change: the logic is identical, only its location changed. Both container managers already have tests exercising the PING/PONG readiness probe against a fake socket server. --- .../ElastiCacheContainerManager.java | 19 +---------- .../elasticache/container/RespLineReader.java | 32 +++++++++++++++++++ .../container/MemoryDbContainerManager.java | 20 ++---------- 3 files changed, 35 insertions(+), 36 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/services/elasticache/container/RespLineReader.java diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/container/ElastiCacheContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/container/ElastiCacheContainerManager.java index bd67924d51..a0d8eef130 100644 --- a/src/main/java/io/github/hectorvent/floci/services/elasticache/container/ElastiCacheContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/container/ElastiCacheContainerManager.java @@ -16,7 +16,6 @@ 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; @@ -206,7 +205,7 @@ public class ElastiCacheContainerManager { OutputStream out = s.getOutputStream(); out.write(RESP_PING); out.flush(); - String line = readAsciiLineCrLf(s.getInputStream()); + String line = RespLineReader.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); @@ -233,22 +232,6 @@ public class ElastiCacheContainerManager { + " 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; diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/container/RespLineReader.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/container/RespLineReader.java new file mode 100644 index 0000000000..52ec561804 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/container/RespLineReader.java @@ -0,0 +1,32 @@ +package io.github.hectorvent.floci.services.elasticache.container; + +import java.io.IOException; +import java.io.InputStream; + +/** + * Reads a single CRLF-terminated ASCII line off a RESP connection, used while probing a + * backend container for readiness. Reused by {@code MemoryDbContainerManager} because + * MemoryDB speaks the same Redis wire protocol as ElastiCache, the same reasoning + * {@code AbstractRedisAuthProxy} follows for the auth-proxy logic. + */ +public final class RespLineReader { + + private RespLineReader() { + } + + public 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(); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/memorydb/container/MemoryDbContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/memorydb/container/MemoryDbContainerManager.java index 1f057b7beb..a5ae6eaf7b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/memorydb/container/MemoryDbContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/memorydb/container/MemoryDbContainerManager.java @@ -10,13 +10,13 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.E import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer; import io.github.hectorvent.floci.core.common.docker.ContainerSpec; import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper; +import io.github.hectorvent.floci.services.elasticache.container.RespLineReader; import jakarta.enterprise.context.ApplicationScoped; 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; @@ -168,7 +168,7 @@ public class MemoryDbContainerManager { OutputStream out = s.getOutputStream(); out.write(RESP_PING); out.flush(); - String line = readAsciiLineCrLf(s.getInputStream()); + String line = RespLineReader.readAsciiLineCrLf(s.getInputStream()); if (line.startsWith("+PONG")) { if (attempt > 1) { LOG.infov("MemoryDB backend ready for cluster {0} after {1} probe attempt(s)", clusterName, attempt); @@ -195,22 +195,6 @@ public class MemoryDbContainerManager { + " 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(MemoryDbContainerHandle handle) { if (handle == null) { return;