diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/AbstractRedisAuthProxy.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/AbstractRedisAuthProxy.java index f0309ad91a..52e204a4fb 100644 --- a/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/AbstractRedisAuthProxy.java +++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/AbstractRedisAuthProxy.java @@ -8,6 +8,7 @@ import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.nio.charset.StandardCharsets; +import java.util.concurrent.CountDownLatch; /** * Shared TCP auth-proxy skeleton for the Redis (RESP) wire protocol. Intercepts the @@ -19,6 +20,7 @@ import java.nio.charset.StandardCharsets; *
Uses Java virtual threads to accept connections and run the AUTH handshake. */ public abstract class AbstractRedisAuthProxy { + private static final long RELAY_JOIN_TIMEOUT_MILLIS = 1_000; private static final byte[] OK_RESPONSE = "+OK\r\n".getBytes(StandardCharsets.UTF_8); private static final byte[] NOAUTH_RESPONSE = @@ -164,13 +166,27 @@ public abstract class AbstractRedisAuthProxy { * relays under load can stall delivery of backend responses (e.g. PING/PONG) to the client. */ private void bridge(Socket client, Socket backend) { + CountDownLatch firstRelayDone = new CountDownLatch(1); Thread t1 = Thread.ofPlatform().daemon(true).name(threadPrefix + "-relay-c2b-" + resourceId) - .start(() -> relay(client, backend)); + .start(() -> { + try { + relay(client, backend); + } finally { + firstRelayDone.countDown(); + } + }); Thread t2 = Thread.ofPlatform().daemon(true).name(threadPrefix + "-relay-b2c-" + resourceId) - .start(() -> relay(backend, client)); + .start(() -> { + try { + relay(backend, client); + } finally { + firstRelayDone.countDown(); + } + }); try { - t1.join(); - t2.join(); + firstRelayDone.await(); + t1.join(RELAY_JOIN_TIMEOUT_MILLIS); + t2.join(RELAY_JOIN_TIMEOUT_MILLIS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { @@ -191,6 +207,16 @@ public abstract class AbstractRedisAuthProxy { } } catch (IOException ignored) { // Normal when either side closes the connection + } finally { + shutdownOutput(to); + } + } + + private static void shutdownOutput(Socket socket) { + try { + socket.shutdownOutput(); + } catch (IOException ignored) { + // The bridge closes both sockets after both relay directions finish. } } diff --git a/src/main/java/io/github/hectorvent/floci/services/neptune/proxy/NeptuneGremlinProxy.java b/src/main/java/io/github/hectorvent/floci/services/neptune/proxy/NeptuneGremlinProxy.java index 6db9659db6..2acbe85a89 100644 --- a/src/main/java/io/github/hectorvent/floci/services/neptune/proxy/NeptuneGremlinProxy.java +++ b/src/main/java/io/github/hectorvent/floci/services/neptune/proxy/NeptuneGremlinProxy.java @@ -7,6 +7,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; +import java.util.concurrent.CountDownLatch; /** * Transparent TCP proxy for a single Neptune DB cluster's Gremlin endpoint. @@ -18,6 +19,7 @@ import java.net.Socket; *
Uses Java virtual threads for non-blocking I/O. */ public class NeptuneGremlinProxy { + private static final long RELAY_JOIN_TIMEOUT_MILLIS = 1_000; private static final Logger LOG = Logger.getLogger(NeptuneGremlinProxy.class); @@ -84,13 +86,27 @@ public class NeptuneGremlinProxy { * for I/O-bound work can stall WebSocket frame delivery under high concurrency. */ private void bridge(Socket client, Socket backend) { + CountDownLatch firstRelayDone = new CountDownLatch(1); Thread t1 = Thread.ofPlatform().daemon(true).name("neptune-relay-c2b-" + clusterId) - .start(() -> pipe(client, backend)); + .start(() -> { + try { + pipe(client, backend); + } finally { + firstRelayDone.countDown(); + } + }); Thread t2 = Thread.ofPlatform().daemon(true).name("neptune-relay-b2c-" + clusterId) - .start(() -> pipe(backend, client)); + .start(() -> { + try { + pipe(backend, client); + } finally { + firstRelayDone.countDown(); + } + }); try { - t1.join(); - t2.join(); + firstRelayDone.await(); + t1.join(RELAY_JOIN_TIMEOUT_MILLIS); + t2.join(RELAY_JOIN_TIMEOUT_MILLIS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { @@ -111,10 +127,24 @@ public class NeptuneGremlinProxy { } } catch (IOException ignored) { // Normal when either side closes the connection + } finally { + shutdownOutput(to); + } + } + + private static void shutdownOutput(Socket s) { + try { + s.shutdownOutput(); + } catch (IOException ignored) { + // The bridge closes both sockets after both relay directions finish. } } private static void closeQuietly(Socket s) { - try { s.close(); } catch (IOException ignored) {} + try { + s.close(); + } catch (IOException ignored) { + // The peer may already have closed the socket. + } } } diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/AbstractRedisAuthProxyRelayTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/AbstractRedisAuthProxyRelayTest.java new file mode 100644 index 0000000000..45ce14540c --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/AbstractRedisAuthProxyRelayTest.java @@ -0,0 +1,160 @@ +package io.github.hectorvent.floci.services.elasticache.proxy; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class AbstractRedisAuthProxyRelayTest { + + @Test + void closingBackendReleasesClientConnection() throws Exception { + try (ServerSocket backendServer = new ServerSocket(0); + ServerSocket freePort = new ServerSocket(0)) { + int proxyPort = freePort.getLocalPort(); + TestProxy proxy = new TestProxy("127.0.0.1", backendServer.getLocalPort()); + freePort.close(); + proxy.start(proxyPort); + + try (Socket client = new Socket("127.0.0.1", proxyPort)) { + client.getOutputStream().write("*1\r\n$4\r\nPING\r\n".getBytes(StandardCharsets.US_ASCII)); + client.getOutputStream().flush(); + + try (Socket backend = backendServer.accept()) { + backend.close(); + } + + client.setSoTimeout(2_000); + assertEquals(-1, client.getInputStream().read()); + } finally { + proxy.stop(); + } + } + } + + @Test + void clientHalfCloseStillAllowsBackendResponse() throws Exception { + try (ServerSocket backendServer = new ServerSocket(0); + ServerSocket freePort = new ServerSocket(0)) { + int proxyPort = freePort.getLocalPort(); + TestProxy proxy = new TestProxy("127.0.0.1", backendServer.getLocalPort()); + freePort.close(); + proxy.start(proxyPort); + + try (Socket client = new Socket("127.0.0.1", proxyPort)) { + client.setSoTimeout(2_000); + client.getOutputStream().write("*1\r\n$4\r\nPING\r\n".getBytes(StandardCharsets.US_ASCII)); + client.getOutputStream().flush(); + client.shutdownOutput(); + + try (Socket backend = backendServer.accept()) { + backend.setSoTimeout(2_000); + while (backend.getInputStream().read() != -1) { + // Drain the request before asserting that the half-close reached the backend. + } + backend.getOutputStream().write("response".getBytes(StandardCharsets.US_ASCII)); + backend.getOutputStream().flush(); + + byte[] response = client.getInputStream().readNBytes(8); + assertEquals("response", new String(response, StandardCharsets.US_ASCII)); + } + } finally { + proxy.stop(); + } + } + } + + @Test + void clientHalfCloseClosesRelayWhenBackendStaysOpenAfterResponse() throws Exception { + try (ServerSocket backendServer = new ServerSocket(0); + ServerSocket freePort = new ServerSocket(0)) { + int proxyPort = freePort.getLocalPort(); + TestProxy proxy = new TestProxy("127.0.0.1", backendServer.getLocalPort()); + freePort.close(); + proxy.start(proxyPort); + + try (Socket client = new Socket("127.0.0.1", proxyPort)) { + client.setSoTimeout(3_000); + client.getOutputStream().write("*1\r\n$4\r\nPING\r\n".getBytes(StandardCharsets.US_ASCII)); + client.getOutputStream().flush(); + client.shutdownOutput(); + try (Socket backend = backendServer.accept()) { + while (backend.getInputStream().read() != -1) { + // Drain until the client half-close reaches the backend. + } + backend.getOutputStream().write("response".getBytes(StandardCharsets.US_ASCII)); + backend.getOutputStream().flush(); + + assertEquals("response", new String(client.getInputStream().readNBytes(8), + StandardCharsets.US_ASCII)); + assertEquals(-1, client.getInputStream().read()); + } + } finally { + proxy.stop(); + } + } + } + + @Test + void keepsHealthyIdleConnectionOpenBeyondRelayShutdownDeadline() throws Exception { + try (ServerSocket backendServer = new ServerSocket(0); + ServerSocket freePort = new ServerSocket(0)) { + int proxyPort = freePort.getLocalPort(); + TestProxy proxy = new TestProxy("127.0.0.1", backendServer.getLocalPort()); + freePort.close(); + proxy.start(proxyPort); + + try (Socket client = new Socket("127.0.0.1", proxyPort)) { + client.setSoTimeout(5_000); + byte[] request = "*1\r\n$4\r\nPING\r\n".getBytes(StandardCharsets.US_ASCII); + client.getOutputStream().write(request); + client.getOutputStream().flush(); + + try (Socket backend = backendServer.accept()) { + backend.setSoTimeout(5_000); + assertEquals(new String(request, StandardCharsets.US_ASCII), + new String(backend.getInputStream().readNBytes(request.length), + StandardCharsets.US_ASCII)); + Thread.sleep(2_200); + + backend.getOutputStream().write("PONG".getBytes(StandardCharsets.US_ASCII)); + backend.getOutputStream().flush(); + assertEquals("PONG", new String(client.getInputStream().readNBytes(4), + StandardCharsets.US_ASCII)); + } + } finally { + proxy.stop(); + } + } + } + + private static final class TestProxy extends AbstractRedisAuthProxy { + private TestProxy(String backendHost, int backendPort) { + super(org.jboss.logging.Logger.getLogger(TestProxy.class), "test", "test", "relay", + backendHost, backendPort); + } + + @Override + protected boolean authRequired() { + return false; + } + + @Override + protected boolean authenticate(String username, String password) { + return true; + } + + @Override + protected void closeQuietly(Socket socket) { + try { + socket.close(); + } catch (IOException ignored) { + // The peer may already be closed. + } + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/neptune/proxy/NeptuneGremlinProxyRelayTest.java b/src/test/java/io/github/hectorvent/floci/services/neptune/proxy/NeptuneGremlinProxyRelayTest.java new file mode 100644 index 0000000000..5391fff7de --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/neptune/proxy/NeptuneGremlinProxyRelayTest.java @@ -0,0 +1,129 @@ +package io.github.hectorvent.floci.services.neptune.proxy; + +import org.junit.jupiter.api.Test; + +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class NeptuneGremlinProxyRelayTest { + + @Test + void closingBackendReleasesClientConnection() throws Exception { + try (ServerSocket backendServer = new ServerSocket(0); + ServerSocket freePort = new ServerSocket(0)) { + int proxyPort = freePort.getLocalPort(); + NeptuneGremlinProxy proxy = new NeptuneGremlinProxy( + "cluster", "127.0.0.1", backendServer.getLocalPort()); + freePort.close(); + proxy.start(proxyPort); + + try (Socket client = new Socket("127.0.0.1", proxyPort)) { + try (Socket backend = backendServer.accept()) { + backend.close(); + } + + client.setSoTimeout(2_000); + assertEquals(-1, client.getInputStream().read()); + } finally { + proxy.stop(); + } + } + } + + @Test + void clientHalfCloseStillAllowsBackendResponse() throws Exception { + try (ServerSocket backendServer = new ServerSocket(0); + ServerSocket freePort = new ServerSocket(0)) { + int proxyPort = freePort.getLocalPort(); + NeptuneGremlinProxy proxy = new NeptuneGremlinProxy( + "cluster", "127.0.0.1", backendServer.getLocalPort()); + freePort.close(); + proxy.start(proxyPort); + + try (Socket client = new Socket("127.0.0.1", proxyPort); + Socket backend = backendServer.accept()) { + client.setSoTimeout(2_000); + backend.setSoTimeout(2_000); + client.getOutputStream().write("request".getBytes(StandardCharsets.US_ASCII)); + client.getOutputStream().flush(); + client.shutdownOutput(); + + while (backend.getInputStream().read() != -1) { + // Drain the request before asserting that the half-close reached the backend. + } + backend.getOutputStream().write("response".getBytes(StandardCharsets.US_ASCII)); + backend.getOutputStream().flush(); + + byte[] response = client.getInputStream().readNBytes(8); + assertEquals("response", new String(response, StandardCharsets.US_ASCII)); + } finally { + proxy.stop(); + } + } + } + + @Test + void clientHalfCloseClosesRelayWhenBackendStaysOpenAfterResponse() throws Exception { + try (ServerSocket backendServer = new ServerSocket(0); + ServerSocket freePort = new ServerSocket(0)) { + int proxyPort = freePort.getLocalPort(); + NeptuneGremlinProxy proxy = new NeptuneGremlinProxy( + "cluster", "127.0.0.1", backendServer.getLocalPort()); + freePort.close(); + proxy.start(proxyPort); + + try (Socket client = new Socket("127.0.0.1", proxyPort); + Socket backend = backendServer.accept()) { + client.setSoTimeout(3_000); + client.shutdownOutput(); + while (backend.getInputStream().read() != -1) { + // Drain until the client half-close reaches the backend. + } + backend.getOutputStream().write("response".getBytes(StandardCharsets.US_ASCII)); + backend.getOutputStream().flush(); + + assertEquals("response", new String(client.getInputStream().readNBytes(8), + StandardCharsets.US_ASCII)); + assertEquals(-1, client.getInputStream().read()); + } finally { + proxy.stop(); + } + } + } + + @Test + void keepsHealthyIdleConnectionOpenBeyondRelayShutdownDeadline() throws Exception { + try (ServerSocket backendServer = new ServerSocket(0); + ServerSocket freePort = new ServerSocket(0)) { + int proxyPort = freePort.getLocalPort(); + NeptuneGremlinProxy proxy = new NeptuneGremlinProxy( + "cluster", "127.0.0.1", backendServer.getLocalPort()); + freePort.close(); + proxy.start(proxyPort); + + try (Socket client = new Socket("127.0.0.1", proxyPort)) { + client.setSoTimeout(5_000); + byte[] request = "PING".getBytes(StandardCharsets.US_ASCII); + client.getOutputStream().write(request); + client.getOutputStream().flush(); + + try (Socket backend = backendServer.accept()) { + backend.setSoTimeout(5_000); + assertEquals("PING", new String(backend.getInputStream().readNBytes(request.length), + StandardCharsets.US_ASCII)); + Thread.sleep(2_200); + + backend.getOutputStream().write("PONG".getBytes(StandardCharsets.US_ASCII)); + backend.getOutputStream().flush(); + assertEquals("PONG", new String(client.getInputStream().readNBytes(4), + StandardCharsets.US_ASCII)); + } + } finally { + proxy.stop(); + } + } + } +}