You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iggy.apache.org by hg...@apache.org on 2026/10/19 17:05:45 UTC

(iggy) 05/19: fix(java): preserve concurrent resolve attempts and results

This is an automated email from the ASF dual-hosted git repository.

hubcio pushed a commit to branch feat/sdk-partition-routing
in repository https://gitbox.apache.org/repos/asf/iggy.git

commit ec29f2ff50a656bc024ef3cf0b91558586281252
Author: Hubert Gruszecki <h....@gmail.com>
AuthorDate: Mon Oct 19 14:20:02 2026 +0200

    fix(java): preserve concurrent resolve attempts and results
    
    Timeout and cancellation callbacks could replay before pending-ID
    cleanup, then release the next resolver's claim a second time.
    Replace terminal pending entries and retain each attempt only once.
---
 .../iggy/client/async/tcp/AsyncTcpConnection.java  | 30 +++++++++-
 .../apache/iggy/client/async/tcp/PollRouter.java   |  8 +--
 .../client/async/tcp/vsr/VsrResponseHandler.java   | 12 ++--
 .../tcp/AsyncTcpConnectionConcurrencyTest.java     | 67 ++++++++++++++++++++++
 .../iggy/client/async/tcp/PollRouterTest.java      | 30 ++++++++++
 .../async/tcp/vsr/VsrResponseHandlerTest.java      | 48 ++++++++++++++++
 6 files changed, 184 insertions(+), 11 deletions(-)

diff --git a/foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java b/foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java
index ec3cf6592..c213f27a7 100644
--- a/foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java
+++ b/foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java
@@ -58,8 +58,10 @@ import org.apache.iggy.client.async.tcp.vsr.VsrResponseHandler;
 import org.apache.iggy.exception.IggyClientException;
 import org.apache.iggy.exception.IggyConnectionException;
 import org.apache.iggy.exception.IggyEmptyResponseException;
+import org.apache.iggy.exception.IggyErrorCode;
 import org.apache.iggy.exception.IggyInvalidArgumentException;
 import org.apache.iggy.exception.IggyNotConnectedException;
+import org.apache.iggy.exception.IggyOperationException;
 import org.apache.iggy.exception.IggyServerException;
 import org.apache.iggy.exception.IggyTimeoutException;
 import org.apache.iggy.exception.IggyTlsException;
@@ -559,7 +561,7 @@ public class AsyncTcpConnection {
         var acquisitionTimeout = eventLoop.schedule(
                 () -> {
                     Throwable failure = failoverState.ticket != null && failoverState.uncertain
-                            ? failoverState.ticket.retain()
+                            ? failoverState.retainTicket()
                             : responseTimeout(commandCode);
                     callerFuture.completeExceptionally(failure);
                     acquiring.cancel(false);
@@ -817,7 +819,7 @@ public class AsyncTcpConnection {
         releaseIfPresent(failoverPayload);
         if (failoverState.ticket != null) {
             if (failoverState.uncertain || callerFuture.isCancelled()) {
-                error = failoverState.ticket.retain();
+                error = failoverState.retainTicket();
             } else {
                 failoverState.ticket.complete();
                 failoverState.ticket = null;
@@ -1272,7 +1274,9 @@ public class AsyncTcpConnection {
         ConsensusSession.Ticket ticket = exchange.failoverState().ticket;
         if (ticket != null && nextUncertain) {
             releaseIfPresent(response);
-            exchange.responseFuture().completeExceptionally(error != null ? error : ticket.retain());
+            exchange.responseFuture()
+                    .completeExceptionally(
+                            error != null ? error : exchange.failoverState().retainTicket());
         } else {
             // Recorded while the reply is alive: a routed replay completes its ticket after the callbacks of its
             // caller, which release the reply.
@@ -1810,6 +1814,7 @@ public class AsyncTcpConnection {
         private boolean everUncertain;
         private ConsensusSession.LaneKey lane;
         private ConsensusSession.Ticket ticket;
+        private RuntimeException retainedError;
         private byte[] routingBody;
         private final Set<ConnectionInfo> visitedTargets = new HashSet<>();
 
@@ -1832,6 +1837,25 @@ public class AsyncTcpConnection {
             return ticket;
         }
 
+        RuntimeException retainTicket() {
+            return retainTicket(ticket);
+        }
+
+        RuntimeException retainTicket(ConsensusSession.Ticket retained) {
+            synchronized (this) {
+                if (retainedError != null) {
+                    return retainedError;
+                }
+                // Retaining wakes resolvers inline, so publish the guard before invoking callbacks without this lock.
+                retainedError = new IggyOperationException(IggyErrorCode.OPERATION_UNCERTAIN, retained.operationId());
+            }
+            RuntimeException error = retained.retain();
+            synchronized (this) {
+                retainedError = error;
+            }
+            return error;
+        }
+
         boolean wasUncertain() {
             return everUncertain;
         }
diff --git a/foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/PollRouter.java b/foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/PollRouter.java
index c7a6b6270..2c142e5fc 100644
--- a/foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/PollRouter.java
+++ b/foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/PollRouter.java
@@ -375,7 +375,7 @@ final class PollRouter {
                 replayed.whenComplete((response, error) -> {
                     ticket.finishWrite();
                     if (error != null) {
-                        ticket.retain();
+                        state.retainTicket(ticket);
                     }
                 });
             } else {
@@ -1212,7 +1212,7 @@ final class PollRouter {
                         && (state.wasUncertain()
                                 || unwrap(error) instanceof IggyOperationException operationError
                                         && operationError.getErrorCode() == IggyErrorCode.OPERATION_UNCERTAIN)) {
-                    state.ticket().retain();
+                    state.retainTicket();
                 } else {
                     state.ticket().complete();
                 }
@@ -1222,14 +1222,14 @@ final class PollRouter {
         private void fail(Throwable error) {
             result.completeExceptionally(
                     state.ticket() != null && state.wasUncertain() && !state.committed()
-                            ? state.ticket().retain()
+                            ? state.retainTicket()
                             : unwrap(error));
         }
 
         private synchronized void expire() {
             fail(
                     state.ticket() != null && dataSent && !retryingRefusal && lifecycleRefusal == null
-                            ? state.ticket().retain()
+                            ? state.retainTicket()
                             : lifecycleRefusal != null
                                     ? lifecycleRefusal
                                     : retryingRefusal || !dataSent ? notAccepted() : uncommitted());
diff --git a/foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/vsr/VsrResponseHandler.java b/foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/vsr/VsrResponseHandler.java
index 3afe8583a..6fcdbc219 100644
--- a/foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/vsr/VsrResponseHandler.java
+++ b/foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/vsr/VsrResponseHandler.java
@@ -96,10 +96,14 @@ public class VsrResponseHandler extends SimpleChannelInboundHandler<ByteBuf> {
     }
 
     private void registerRequest(PendingRequest pending) {
-        if (pendingRequests.putIfAbsent(pending.requestId, pending) != null) {
-            throw new IllegalStateException(
-                    "A request is already pending for request id " + Long.toUnsignedString(pending.requestId));
-        }
+        pendingRequests.compute(pending.requestId, (requestId, previous) -> {
+            if (previous != null && !previous.future.isDone()) {
+                throw new IllegalStateException(
+                        "A request is already pending for request id " + Long.toUnsignedString(requestId));
+            }
+            // Timeout and cancellation callbacks can replay before the previous entry's cleanup runs.
+            return pending;
+        });
         pending.future.whenComplete((response, error) -> pendingRequests.remove(pending.requestId, pending));
     }
 
diff --git a/foreign/java/java-sdk/src/test/java/org/apache/iggy/client/async/tcp/AsyncTcpConnectionConcurrencyTest.java b/foreign/java/java-sdk/src/test/java/org/apache/iggy/client/async/tcp/AsyncTcpConnectionConcurrencyTest.java
index 40220fa4a..8c9c7303f 100644
--- a/foreign/java/java-sdk/src/test/java/org/apache/iggy/client/async/tcp/AsyncTcpConnectionConcurrencyTest.java
+++ b/foreign/java/java-sdk/src/test/java/org/apache/iggy/client/async/tcp/AsyncTcpConnectionConcurrencyTest.java
@@ -212,6 +212,73 @@ class AsyncTcpConnectionConcurrencyTest {
         }
     }
 
+    @ParameterizedTest
+    @ValueSource(booleans = {false, true})
+    void shouldReplayWaitingResolveAfterThePreviousReplayTimesOutOrIsCancelled(boolean cancel) throws Exception {
+        try (ServerSocket serverSocket = new ServerSocket(0, 1, InetAddress.getLoopbackAddress())) {
+            CompletableFuture<Void> replayReceived = new CompletableFuture<>();
+            CompletableFuture<Void> server = CompletableFuture.runAsync(() -> {
+                try (Socket socket = serverSocket.accept()) {
+                    socket.setSoTimeout((int) TimeUnit.SECONDS.toMillis(5));
+                    InputStream input = socket.getInputStream();
+                    OutputStream output = socket.getOutputStream();
+                    writeResponse(output, readRequest(input), registerBody());
+                    Request original = readRequest(input);
+                    Request replay = readRequest(input);
+                    assertThat(replay.requestId()).isEqualTo(original.requestId());
+                    assertThat(replay.header()).containsExactly(original.header());
+                    replayReceived.complete(null);
+                    Request waitingReplay = readRequest(input);
+                    assertThat(waitingReplay.requestId()).isEqualTo(original.requestId());
+                    assertThat(waitingReplay.header()).containsExactly(original.header());
+                    writeResponse(output, waitingReplay, new byte[0]);
+                } catch (IOException error) {
+                    replayReceived.completeExceptionally(error);
+                    throw new IllegalStateException("Mock VSR server failed", error);
+                }
+            });
+            AsyncTcpConnection connection = newConnection(serverSocket, Duration.ofHours(1), 50);
+            PollRouter router = new PollRouter(() -> connection, endpoint -> {
+                throw new IllegalStateException("A direct replay opens no partition link");
+            });
+            try {
+                connection.connect().get(5, TimeUnit.SECONDS);
+                connection
+                        .send(LOGIN_CODE, loginPayload())
+                        .get(5, TimeUnit.SECONDS)
+                        .release();
+                var state = new AsyncTcpConnection.TransientFailoverState();
+                var original = connection.send(
+                        SEND_MESSAGES_CODE,
+                        sendMessagesPayload(),
+                        System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(300),
+                        state);
+                assertThatThrownBy(() -> original.get(5, TimeUnit.SECONDS))
+                        .hasCauseInstanceOf(IggyOperationException.class);
+                long operation = state.ticket().operationId();
+                var replay = router.resolve(operation, cancel ? Duration.ofSeconds(5) : Duration.ofSeconds(1));
+                replayReceived.get(5, TimeUnit.SECONDS);
+                var waiting = router.resolve(operation, Duration.ofSeconds(5));
+                assertThat(waiting).isNotDone();
+
+                if (cancel) {
+                    assertThat(replay.cancel(false)).isTrue();
+                } else {
+                    assertThatThrownBy(() -> replay.get(5, TimeUnit.SECONDS))
+                            .hasCauseInstanceOf(IggyOperationException.class);
+                }
+
+                waiting.get(5, TimeUnit.SECONDS).release();
+                router.resolve(operation, Duration.ofSeconds(1))
+                        .get(5, TimeUnit.SECONDS)
+                        .release();
+                server.get(5, TimeUnit.SECONDS);
+            } finally {
+                connection.close().get(5, TimeUnit.SECONDS);
+            }
+        }
+    }
+
     @Test
     void shouldReleaseTheFrameAliasWhenTheRequestIdCannotBeRenewed() throws Exception {
         try (ServerSocket serverSocket = new ServerSocket(0, 1, InetAddress.getLoopbackAddress())) {
diff --git a/foreign/java/java-sdk/src/test/java/org/apache/iggy/client/async/tcp/PollRouterTest.java b/foreign/java/java-sdk/src/test/java/org/apache/iggy/client/async/tcp/PollRouterTest.java
index 910091e79..c606b5033 100644
--- a/foreign/java/java-sdk/src/test/java/org/apache/iggy/client/async/tcp/PollRouterTest.java
+++ b/foreign/java/java-sdk/src/test/java/org/apache/iggy/client/async/tcp/PollRouterTest.java
@@ -266,6 +266,36 @@ class PollRouterTest {
         }
     }
 
+    @Test
+    void shouldKeepTheNextResolverClaimWhenPreviousCompletionReentersRetention() throws Exception {
+        IoEventLoopGroup group = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
+        ManualTimers timers = new ManualTimers();
+        Coordinator coordinator = new Coordinator(group, timers);
+        var owner = coordinator.sessionOwner();
+        try {
+            var ticket = retainOffset(coordinator);
+            long deadline = System.nanoTime() + NEVER.toNanos();
+            owner.resume(OPERATION_ID, deadline, timers).join();
+            var previous = new AsyncTcpConnection.TransientFailoverState();
+            previous.restore(ticket);
+            var waiting = owner.resume(OPERATION_ID, deadline, timers);
+            var completed = waiting.thenRun(previous::retainTicket);
+
+            previous.retainTicket();
+            completed.get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
+
+            var later = owner.resume(OPERATION_ID, deadline, timers);
+            assertThat(later).isNotDone();
+            ticket.recordOutcome(Unpooled.EMPTY_BUFFER, null);
+            ticket.complete();
+            assertThat(owner.provedOutcome(OPERATION_ID)).isEmpty();
+        } finally {
+            owner.reset();
+            timers.shutdownGracefully(0, 1, TimeUnit.SECONDS).get(5, TimeUnit.SECONDS);
+            group.shutdownGracefully(0, 1, TimeUnit.SECONDS).get(5, TimeUnit.SECONDS);
+        }
+    }
+
     @Test
     void shouldReleaseResolverBytesAfterQueueRejectionAndAbandon() throws Exception {
         IoEventLoopGroup group = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
diff --git a/foreign/java/java-sdk/src/test/java/org/apache/iggy/client/async/tcp/vsr/VsrResponseHandlerTest.java b/foreign/java/java-sdk/src/test/java/org/apache/iggy/client/async/tcp/vsr/VsrResponseHandlerTest.java
index 246c8034a..494626dc5 100644
--- a/foreign/java/java-sdk/src/test/java/org/apache/iggy/client/async/tcp/vsr/VsrResponseHandlerTest.java
+++ b/foreign/java/java-sdk/src/test/java/org/apache/iggy/client/async/tcp/vsr/VsrResponseHandlerTest.java
@@ -27,6 +27,8 @@ import org.apache.iggy.exception.IggyServerException;
 import org.apache.iggy.exception.IggyTimeoutException;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 
 import java.nio.charset.StandardCharsets;
 import java.util.concurrent.CompletableFuture;
@@ -299,6 +301,52 @@ class VsrResponseHandlerTest {
         response.release();
     }
 
+    @ParameterizedTest
+    @ValueSource(booleans = {false, true})
+    void shouldRegisterReplayFromTimeoutOrCancellationCallback(boolean cancel) {
+        long requestId = 7;
+        CompletableFuture<ByteBuf> attempt = new CompletableFuture<>();
+        ByteBuf request = requestFrame(VsrOperation.SEND_MESSAGES, requestId);
+        handler.registerRequest(channel, request, attempt, System.nanoTime(), SEND_MESSAGES_CODE);
+        request.release();
+        var replay = attempt.handle((response, error) -> enqueue(VsrOperation.SEND_MESSAGES, requestId))
+                .thenCompose(future -> future);
+
+        if (cancel) {
+            attempt.cancel(false);
+        } else {
+            channel.runScheduledPendingTasks();
+        }
+        assertThat(replay).isNotDone();
+        channel.writeInbound(replyFrame(
+                VsrOperation.SEND_MESSAGES, requestId, Unpooled.buffer().writeByte(42)));
+
+        ByteBuf response = replay.join();
+        try {
+            assertThat(response.readByte()).isEqualTo((byte) 42);
+        } finally {
+            response.release();
+        }
+        assertThat(channel.isActive()).isTrue();
+    }
+
+    @Test
+    void shouldRejectDuplicateRegistrationWhileTheRequestIsPending() {
+        long requestId = 7;
+        var original = enqueue(VsrOperation.SEND_MESSAGES, requestId);
+        ByteBuf duplicate = requestFrame(VsrOperation.SEND_MESSAGES, requestId);
+        try {
+            assertThatThrownBy(() -> handler.registerRequest(
+                            channel, duplicate, new CompletableFuture<>(), System.nanoTime(), SEND_MESSAGES_CODE))
+                    .isInstanceOf(IllegalStateException.class);
+        } finally {
+            duplicate.release();
+        }
+
+        channel.writeInbound(replyFrame(VsrOperation.SEND_MESSAGES, requestId, Unpooled.EMPTY_BUFFER));
+        original.join().release();
+    }
+
     @Test
     void shouldCorrelateRepliesArrivingInReverseOrder() throws Exception {
         CompletableFuture<ByteBuf> first = enqueue(VsrOperation.SEND_MESSAGES, 7);