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

amaliujia 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 60b15ecfbd2 HDDS-17431. Release stream-close PutBlock buffers on 
client when PutBlock commits without Raft. (#12077)
60b15ecfbd2 is described below

commit 60b15ecfbd2b172c3fb031f4a85900fbd47dd38e
Author: fmorg-git <[email protected]>
AuthorDate: Wed Nov 18 19:27:12 2026 -0800

    HDDS-17431. Release stream-close PutBlock buffers on client when PutBlock 
commits without Raft. (#12077)
---
 .../hdds/scm/storage/BlockDataStreamOutput.java    | 23 +++++--
 .../hdds/scm/storage/MockDatanodePipeline.java     | 23 ++++++-
 .../scm/storage/TestBlockDataStreamOutput.java     | 70 +++++++++++++++++++---
 .../ozone/client/io/TestKeyDataStreamOutput.java   | 30 +++++++++-
 4 files changed, 131 insertions(+), 15 deletions(-)

diff --git 
a/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockDataStreamOutput.java
 
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockDataStreamOutput.java
index 5c4f5be58ed..ca6aaac7380 100644
--- 
a/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockDataStreamOutput.java
+++ 
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockDataStreamOutput.java
@@ -126,7 +126,8 @@ public class BlockDataStreamOutput implements 
ByteBufferStreamOutput {
   private Queue<CompletableFuture<ContainerCommandResponseProto>>
       putBlockFutures = new LinkedList<>();
 
-  // Futures of the PutBlock(s) sent as data stream commands, i.e. committed 
without the Raft log.
+  // PutBlock(s) on the data stream without a Raft log entry: mid-stream 
commandAsync PutBlocks
+  // (flush boundaries, hsync/flush) and the PutBlock on stream close.
   private final Queue<CompletableFuture<DataStreamReply>>
       putBlockCommandFutures = new LinkedList<>();
 
@@ -448,6 +449,8 @@ public void executePutBlock(boolean sync, boolean close) 
throws IOException {
     if (close && putBlockWithoutRaft) {
       // Wait for boundary PutBlock(s) before appending the stream-close 
PutBlock.
       waitPutBlockCommandFuturesComplete();
+      // handleFlush skips its watch if the stream close fails; release what 
the boundary PutBlock(s) committed.
+      watchForCommit(false);
     }
     final BlockData blockData = containerBlockData.build();
     if (close) {
@@ -467,10 +470,14 @@ public void executePutBlock(boolean sync, boolean close) 
throws IOException {
               setIoException(ioe);
               throw new CompletionException(ioe);
             }
+            if (putBlockWithoutRaft) {
+              addBuffersCommittedWithoutRaft(byteBufferList);
+            }
           });
       if (putBlockWithoutRaft) {
         // PutBlock is supposed to be committed after the data stream close so 
there
         // is no need to continue.
+        putBlockCommandFutures.add(dataStreamCloseReply);
         return;
       }
     } else if (putBlockWithoutRaft) {
@@ -556,14 +563,18 @@ private void executePutBlockCommand(BlockData blockData,
             setIoException(ioe);
             throw new CompletionException(ioe);
           }
-          // The command has no log index; use 0 as for the standalone protocol
-          // so that the buffers are released by the next watchForCommit.
-          if (byteBufferList != null) {
-            commitWatcher.updateCommitInfoMap(0, byteBufferList);
-          }
+          addBuffersCommittedWithoutRaft(byteBufferList);
         }, responseExecutor));
   }
 
+  private void addBuffersCommittedWithoutRaft(List<StreamBuffer> 
byteBufferList) {
+    // The command has no log index; use 0 as for the standalone protocol
+    // so that the buffers are released by the next watchForCommit.
+    if (byteBufferList != null) {
+      commitWatcher.updateCommitInfoMap(0, byteBufferList);
+    }
+  }
+
   private void validatePutBlockCommandReply(DataStreamReply reply, Throwable e)
       throws IOException {
     if (e != null || reply == null || !reply.isSuccess()) {
diff --git 
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/MockDatanodePipeline.java
 
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/MockDatanodePipeline.java
index 4cd480f3798..9c5ee0d7a21 100644
--- 
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/MockDatanodePipeline.java
+++ 
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/MockDatanodePipeline.java
@@ -33,6 +33,8 @@
 import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.function.Supplier;
@@ -99,6 +101,8 @@ public class MockDatanodePipeline {
   private volatile int watchFailAfter = Integer.MAX_VALUE;
 
   private volatile boolean streamCloseFailure;
+  // When true, the stream close reply is completed asynchronously after a 
short delay.
+  private volatile boolean streamCloseDelayed;
   private final AtomicInteger streamCloseCount = new AtomicInteger(0);
 
   public MockDatanodePipeline() throws IOException {
@@ -198,7 +202,18 @@ public MockDatanodePipeline(BlockID blockID) throws 
IOException {
           if (streamCloseFailure) {
             when(reply.isSuccess()).thenReturn(false);
           }
-          return CompletableFuture.completedFuture(reply);
+          if (!streamCloseDelayed) {
+            return CompletableFuture.completedFuture(reply);
+          }
+          return CompletableFuture.supplyAsync(() -> {
+            try {
+              TimeUnit.MILLISECONDS.sleep(50);
+            } catch (InterruptedException e) {
+              Thread.currentThread().interrupt();
+              throw new CompletionException(e);
+            }
+            return reply;
+          });
         }
       }
       int count = chunkCount.incrementAndGet();
@@ -321,6 +336,12 @@ public MockDatanodePipeline failStreamClose() {
     return this;
   }
 
+  /** Complete the stream close reply asynchronously, as a real Ratis pipeline 
does. */
+  public MockDatanodePipeline withDelayedStreamClose() {
+    this.streamCloseDelayed = true;
+    return this;
+  }
+
   /** Reply to stream commands without a payload, as a datanode which does not 
handle them does. */
   public MockDatanodePipeline withUnsupportedCommand() {
     this.commandUnsupported = true;
diff --git 
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestBlockDataStreamOutput.java
 
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestBlockDataStreamOutput.java
index 131fcca5360..78ccbfc3517 100644
--- 
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestBlockDataStreamOutput.java
+++ 
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestBlockDataStreamOutput.java
@@ -26,7 +26,9 @@
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
+import java.util.stream.Stream;
 import org.apache.commons.lang3.RandomUtils;
 import org.apache.hadoop.hdds.HDDSVersion;
 import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos;
@@ -35,6 +37,8 @@
 import org.apache.hadoop.hdds.scm.OzoneClientConfig;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
 import org.junit.jupiter.params.provider.ValueSource;
 
 /**
@@ -68,7 +72,11 @@ private BlockDataStreamOutput 
createStream(MockDatanodePipeline pipeline) throws
 
   private BlockDataStreamOutput createStream(
       MockDatanodePipeline pipeline, OzoneClientConfig config) throws 
IOException {
-    List<StreamBuffer> bufferList = new ArrayList<>();
+    return createStream(pipeline, config, new ArrayList<>());
+  }
+
+  private BlockDataStreamOutput createStream(MockDatanodePipeline pipeline, 
OzoneClientConfig config,
+      List<StreamBuffer> bufferList) throws IOException {
     return new BlockDataStreamOutput(
         pipeline.getBlockID(),
         pipeline.getClientFactory(),
@@ -246,6 +254,45 @@ void 
midStreamPutBlockFailsOnDatanodeWithoutCommandSupport() throws Exception {
     assertThrows(IOException.class, stream::close);
   }
 
+  @Test
+  void testCloseReleasesBuffersCommittedByStreamClose() throws Exception {
+    final MockDatanodePipeline pipeline = new 
MockDatanodePipeline().withDelayedStreamClose();
+    final OzoneClientConfig config = createConfig();
+    config.setDatastreamPutBlockWithoutRaftEnabled(true);
+    final List<StreamBuffer> bufferList = new ArrayList<>();
+    final BlockDataStreamOutput stream = createStream(pipeline, config, 
bufferList);
+    final byte[] data = randomBytes(500);
+    stream.write(ByteBuffer.wrap(data), 0, data.length);
+    stream.close();
+
+    assertEquals(500, stream.getTotalAckDataLength());
+    assertThat(bufferList).isEmpty();
+  }
+
+  @Test
+  void testFailedStreamCloseKeepsDataCommittedByBoundaryPutBlock() throws 
Exception {
+    final MockDatanodePipeline pipeline = new 
MockDatanodePipeline().failStreamClose().withDelayedStreamClose();
+    final OzoneClientConfig config = createConfig();
+    config.setDatastreamPutBlockWithoutRaftEnabled(true);
+    final List<StreamBuffer> bufferList = new ArrayList<>();
+    final BlockDataStreamOutput stream = createStream(pipeline, config, 
bufferList);
+    // The boundary PutBlock commits the first 400B; the stream close, which 
fails, would commit the last 50B.
+    final byte[] data = randomBytes(450);
+    stream.write(ByteBuffer.wrap(data), 0, data.length);
+
+    assertThrows(IOException.class, stream::close);
+    assertEquals(400, stream.getTotalAckDataLength());
+
+    // Only the last 50B are left in the buffer list, to be written again by 
the retry.
+    final ByteBuffer unacked = ByteBuffer.allocate(50);
+    for (StreamBuffer buffer : bufferList) {
+      final ByteBuffer dup = buffer.duplicate();
+      dup.flip();
+      unacked.put(dup);
+    }
+    assertArrayEquals(Arrays.copyOfRange(data, 400, 450), unacked.array());
+  }
+
   @Test
   void writeAcrossStreamWindowTriggersBackPressure() throws Exception {
     MockDatanodePipeline pipeline = new MockDatanodePipeline();
@@ -325,16 +372,19 @@ void closeAfterWriteFailureThrows() throws Exception {
   }
 
   @ParameterizedTest
-  @ValueSource(booleans = {false, true})
-  void failedStreamCloseIsNotRetried(boolean putBlockWithoutRaft) throws 
Exception {
+  @MethodSource("failedStreamCloseParams")
+  void testFailedStreamCloseIsNotRetried(boolean putBlockWithoutRaft, boolean 
delayedClose) throws Exception {
     MockDatanodePipeline pipeline = new 
MockDatanodePipeline().failStreamClose();
-    OzoneClientConfig config = createConfig();
+    if (delayedClose) {
+      pipeline.withDelayedStreamClose();
+    }
+    final OzoneClientConfig config = createConfig();
     config.setDatastreamPutBlockWithoutRaftEnabled(putBlockWithoutRaft);
-    BlockDataStreamOutput stream = createStream(pipeline, config);
-    byte[] data = randomBytes(50);
+    final BlockDataStreamOutput stream = createStream(pipeline, config);
+    final byte[] data = randomBytes(50);
     stream.write(ByteBuffer.wrap(data), 0, data.length);
 
-    IOException e = assertThrows(IOException.class, stream::close);
+    final IOException e = assertThrows(IOException.class, stream::close);
     assertThat(e).hasStackTraceContaining("Failed to close the data stream");
     assertEquals(1, pipeline.getStreamCloseCount(), "A failed stream close 
must not be retried");
   }
@@ -399,4 +449,10 @@ void putBlockContainsAllChunkMetadata() throws Exception {
   private static byte[] randomBytes(int length) {
     return RandomUtils.secure().randomBytes(length);
   }
+
+  static Stream<Arguments> failedStreamCloseParams() {
+    return Stream.of(false, true).flatMap(
+        putBlockWithoutRaft -> Stream.of(false, true).map(
+            delayedClose -> Arguments.of(putBlockWithoutRaft, delayedClose)));
+  }
 }
diff --git 
a/hadoop-ozone/client/src/test/java/org/apache/hadoop/ozone/client/io/TestKeyDataStreamOutput.java
 
b/hadoop-ozone/client/src/test/java/org/apache/hadoop/ozone/client/io/TestKeyDataStreamOutput.java
index 0900c70584f..ebfb652fd27 100644
--- 
a/hadoop-ozone/client/src/test/java/org/apache/hadoop/ozone/client/io/TestKeyDataStreamOutput.java
+++ 
b/hadoop-ozone/client/src/test/java/org/apache/hadoop/ozone/client/io/TestKeyDataStreamOutput.java
@@ -17,11 +17,13 @@
 
 package org.apache.hadoop.ozone.client.io;
 
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 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.anyBoolean;
 import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.argThat;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
@@ -30,6 +32,7 @@
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.concurrent.atomic.AtomicInteger;
 import org.apache.commons.lang3.RandomUtils;
@@ -117,8 +120,11 @@ private XceiverClientFactory 
createSharedClientFactory(MockDatanodePipeline... p
    */
   private KeyDataStreamOutput createKeyStream(OzoneManagerProtocol omClient, 
MockDatanodePipeline... pipelines)
       throws Exception {
+    return createKeyStream(createConfig(), omClient, pipelines);
+  }
 
-    OzoneClientConfig config = createConfig();
+  private KeyDataStreamOutput createKeyStream(OzoneClientConfig config, 
OzoneManagerProtocol omClient,
+      MockDatanodePipeline... pipelines) throws Exception {
     ReplicationConfig replicationConfig = 
RatisReplicationConfig.getInstance(ReplicationFactor.THREE);
 
     OmKeyInfo keyInfo = new OmKeyInfo.Builder()
@@ -269,6 +275,28 @@ void containerCloseTriggersRetryOnNewBlock() throws 
Exception {
     verify(omClient).commitKey(any(OmKeyArgs.class), anyLong());
   }
 
+  @Test
+  void testFailedStreamCloseOfSecondBlockRewritesOnlyItsData() throws 
Exception {
+    final MockDatanodePipeline pipeline1 = new MockDatanodePipeline(new 
BlockID(1, 1));
+    final MockDatanodePipeline pipeline2 = new MockDatanodePipeline(new 
BlockID(2, 2)).failStreamClose();
+    final MockDatanodePipeline pipeline3 = new MockDatanodePipeline(new 
BlockID(3, 3));
+    // createOmClient only lists pipelines returned by allocateBlock in call 
order, i.e. pipeline2 then pipeline3.
+    final OzoneManagerProtocol omClient = createOmClient(pipeline2, pipeline3);
+    final OzoneClientConfig config = createConfig();
+    config.setDatastreamPutBlockWithoutRaftEnabled(true);
+    // PutBlock commands at 300B and 600B, so the stream close of the first 
block commits its last 200B.
+    config.setDataStreamBufferFlushSize(300);
+    final byte[] data = RandomUtils.secure().randomBytes(850);
+
+    try (KeyDataStreamOutput stream = createKeyStream(config, omClient, 
pipeline1, pipeline2, pipeline3)) {
+      stream.write(ByteBuffer.wrap(data), 0, data.length);
+    }
+
+    // Only the data of the second block is written again; none of the first 
block, whose close succeeded.
+    assertArrayEquals(Arrays.copyOfRange(data, 800, 850), 
pipeline3.getAllReceivedData());
+    verify(omClient).commitKey(argThat(args -> args.getDataSize() == 
data.length), anyLong());
+  }
+
   @Test
   void multipleHsyncsCallOmAtLeastOnce() throws Exception {
     MockDatanodePipeline pipeline = new MockDatanodePipeline();


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

Reply via email to