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

adoroszlai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 04b391f8102 HDDS-17648. Key write hangs if XceiverClientRatis is 
closed while watchForCommit is still pending (#11890)
04b391f8102 is described below

commit 04b391f81020d153723be2bc8bb63a7c16661043
Author: Andrey Yarovoy <[email protected]>
AuthorDate: Thu Nov 5 13:32:44 2026 -0500

    HDDS-17648. Key write hangs if XceiverClientRatis is closed while 
watchForCommit is still pending (#11890)
---
 .../apache/hadoop/hdds/scm/XceiverClientRatis.java |  21 +++-
 .../hadoop/hdds/scm/TestXceiverClientRatis.java    | 124 +++++++++++++++++++++
 2 files changed, 141 insertions(+), 4 deletions(-)

diff --git 
a/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/XceiverClientRatis.java
 
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/XceiverClientRatis.java
index 009b74221d9..0b2c9177e8c 100644
--- 
a/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/XceiverClientRatis.java
+++ 
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/XceiverClientRatis.java
@@ -56,6 +56,7 @@
 import org.apache.ratis.proto.RaftProtos.CommitInfoProto;
 import org.apache.ratis.proto.RaftProtos.ReplicationLevel;
 import org.apache.ratis.protocol.RaftClientReply;
+import org.apache.ratis.protocol.exceptions.AlreadyClosedException;
 import org.apache.ratis.protocol.exceptions.GroupMismatchException;
 import org.apache.ratis.protocol.exceptions.NotReplicatedException;
 import org.apache.ratis.protocol.exceptions.RaftException;
@@ -313,7 +314,9 @@ public CompletableFuture<XceiverClientReply> 
watchForCommit(long index) {
     }).exceptionally(e -> {
       LOG.warn("{} way commit failed on pipeline {}", watchType, pipeline, e);
       final boolean isGroupMismatch = HddsClientUtils.containsException(e, 
GroupMismatchException.class) != null;
-      if (!isGroupMismatch && watchType == ReplicationLevel.ALL_COMMITTED) {
+      // a closed client cannot send the MAJORITY_COMMITTED watch below
+      final boolean isClosed = HddsClientUtils.containsException(e, 
AlreadyClosedException.class) != null;
+      if (!isGroupMismatch && !isClosed && watchType == 
ReplicationLevel.ALL_COMMITTED) {
         final Throwable nre = HddsClientUtils.containsException(e, 
NotReplicatedException.class);
         if (nre instanceof NotReplicatedException) {
           // If NotReplicatedException is thrown from the Datanode leader
@@ -322,14 +325,24 @@ public CompletableFuture<XceiverClientReply> 
watchForCommit(long index) {
           final Collection<CommitInfoProto> commitInfoProtoList = 
((NotReplicatedException) nre).getCommitInfos();
           replyFuture.complete(handleFailedAllCommit(index, 
commitInfoProtoList));
         } else {
-          getClient().async().watch(index, ReplicationLevel.MAJORITY_COMMITTED)
-              .thenApply(reply -> handleFailedAllCommit(index, 
reply.getCommitInfos()))
-              .whenComplete(JavaUtils.asBiConsumer(replyFuture));
+          try {
+            getClient().async().watch(index, 
ReplicationLevel.MAJORITY_COMMITTED)
+                .thenApply(reply -> handleFailedAllCommit(index, 
reply.getCommitInfos()))
+                .whenComplete(JavaUtils.asBiConsumer(replyFuture));
+          } catch (RuntimeException fallbackFailure) {
+            // e.g. the client was closed meanwhile; an exception thrown here 
would leave replyFuture incomplete
+            e.addSuppressed(fallbackFailure);
+            replyFuture.completeExceptionally(e);
+          }
         }
       } else {
         replyFuture.completeExceptionally(e);
       }
       return null;
+    }).exceptionally(t -> {
+      // anything thrown by the handler above would otherwise leave 
replyFuture incomplete
+      replyFuture.completeExceptionally(t);
+      return null;
     });
     return replyFuture;
   }
diff --git 
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/TestXceiverClientRatis.java
 
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/TestXceiverClientRatis.java
new file mode 100644
index 00000000000..21fb7f55308
--- /dev/null
+++ 
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/TestXceiverClientRatis.java
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.scm;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.hdds.ratis.RatisHelper;
+import org.apache.hadoop.hdds.scm.pipeline.MockPipeline;
+import org.apache.hadoop.hdds.scm.pipeline.Pipeline;
+import org.apache.ratis.client.RaftClient;
+import org.apache.ratis.client.api.AsyncApi;
+import org.apache.ratis.proto.RaftProtos.ReplicationLevel;
+import org.apache.ratis.protocol.RaftClientReply;
+import org.apache.ratis.protocol.exceptions.AlreadyClosedException;
+import org.apache.ratis.protocol.exceptions.NotReplicatedException;
+import org.apache.ratis.rpc.RpcType;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+
+/**
+ * Tests for {@link XceiverClientRatis#watchForCommit}: the returned future 
must complete even when the client is
+ * closed while the watch is pending (HDDS-17648).
+ */
+class TestXceiverClientRatis {
+
+  private static final long INDEX = 10;
+
+  private final RaftClient raftClient = mock(RaftClient.class);
+  private final AsyncApi async = mock(AsyncApi.class);
+  private final CompletableFuture<RaftClientReply> allCommitted = new 
CompletableFuture<>();
+  private XceiverClientRatis client;
+
+  @BeforeEach
+  void setUp() throws Exception {
+    when(raftClient.async()).thenReturn(async);
+    when(async.watch(INDEX, 
ReplicationLevel.ALL_COMMITTED)).thenReturn(allCommitted);
+    Pipeline pipeline = MockPipeline.createRatisPipeline();
+    client = XceiverClientRatis.newXceiverClientRatis(pipeline, new 
OzoneConfiguration());
+    try (MockedStatic<RatisHelper> ratisHelper = mockStatic(RatisHelper.class, 
CALLS_REAL_METHODS)) {
+      ratisHelper.when(() -> RatisHelper.newRaftClient(any(RpcType.class), 
any(Pipeline.class), any(), any(), any()))
+          .thenReturn(raftClient);
+      client.connect();
+    }
+  }
+
+  /** The client is closed (for example evicted and released) while the 
ALL_COMMITTED watch is pending. */
+  @Test
+  void watchCompletesWhenClientIsClosedDuringWatch() {
+    CompletableFuture<XceiverClientReply> reply = client.watchForCommit(INDEX);
+    client.close();
+    allCommitted.completeExceptionally(new IOException("watch timed out"));
+
+    ExecutionException e = assertThrows(ExecutionException.class, () -> 
reply.get(10, TimeUnit.SECONDS));
+    assertThat(e.getCause()).hasMessageContaining("watch timed out");
+  }
+
+  @Test
+  void noMajorityWatchWhenClientIsClosed() {
+    CompletableFuture<XceiverClientReply> reply = client.watchForCommit(INDEX);
+    allCommitted.completeExceptionally(new AlreadyClosedException("client is 
closed"));
+
+    ExecutionException e = assertThrows(ExecutionException.class, () -> 
reply.get(10, TimeUnit.SECONDS));
+    assertThat(e.getCause()).isInstanceOf(AlreadyClosedException.class);
+    verify(async, never()).watch(anyLong(), 
eq(ReplicationLevel.MAJORITY_COMMITTED));
+  }
+
+  /** The future completes even when handling the failed ALL_COMMITTED watch 
throws. */
+  @Test
+  void watchCompletesWhenFailureHandlingThrows() {
+    CompletableFuture<XceiverClientReply> reply = client.watchForCommit(INDEX);
+    // no commit infos: handleFailedAllCommit throws NullPointerException
+    allCommitted.completeExceptionally(new NotReplicatedException(1, 
ReplicationLevel.ALL_COMMITTED, INDEX));
+
+    ExecutionException e = assertThrows(ExecutionException.class, () -> 
reply.get(10, TimeUnit.SECONDS));
+    assertThat(e.getCause()).isInstanceOf(NullPointerException.class);
+  }
+
+  /** Unchanged: when ALL_COMMITTED fails on an open client, a 
MAJORITY_COMMITTED watch is used instead. */
+  @Test
+  void fallsBackToMajorityWatch() throws Exception {
+    RaftClientReply majorityReply = mock(RaftClientReply.class);
+    when(majorityReply.getCommitInfos()).thenReturn(Collections.emptyList());
+    when(async.watch(INDEX, ReplicationLevel.MAJORITY_COMMITTED))
+        .thenReturn(CompletableFuture.completedFuture(majorityReply));
+
+    CompletableFuture<XceiverClientReply> reply = client.watchForCommit(INDEX);
+    allCommitted.completeExceptionally(new IOException("watch timed out"));
+
+    assertEquals(INDEX, reply.get(10, TimeUnit.SECONDS).getLogIndex());
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to