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 1e72968c9f9 HDDS-16191. Extract PendingFlush state object from 
BlockDataStreamOutput (#11384)
1e72968c9f9 is described below

commit 1e72968c9f90b440674baae6f65c21e79d54e142
Author: len548 <[email protected]>
AuthorDate: Fri Nov 6 14:42:33 2026 +0900

    HDDS-16191. Extract PendingFlush state object from BlockDataStreamOutput 
(#11384)
---
 .../hdds/scm/storage/BlockDataStreamOutput.java    | 33 +++++-------
 .../hadoop/hdds/scm/storage/PendingFlush.java      | 62 ++++++++++++++++++++++
 .../scm/storage/TestBlockDataStreamOutput.java     | 10 ++++
 3 files changed, 84 insertions(+), 21 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 c25c66ace8d..cd2c5dba032 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
@@ -137,14 +137,12 @@ public class BlockDataStreamOutput implements 
ByteBufferStreamOutput {
   private final String tokenString;
   private final DataStreamOutput out;
   private CompletableFuture<DataStreamReply> dataStreamCloseReply;
-  private List<CompletableFuture<DataStreamReply>> futures = new ArrayList<>();
   private final long syncSize;
   private long syncPosition = 0;
   private StreamBuffer currentBuffer;
   private XceiverClientMetrics metrics;
-  // buffers for which putBlock is yet to be executed
-  private List<StreamBuffer> buffersForPutBlock;
   private boolean isDatastreamPipelineMode;
+  private PendingFlush pendingFlush = new PendingFlush();
   // Whether PutBlock is committed through the data stream instead of the Raft 
log; fixed for the stream lifetime.
   private final boolean putBlockWithoutRaft;
 
@@ -308,10 +306,7 @@ private void writeChunkIfNeeded() throws IOException {
 
   private void writeChunk(StreamBuffer sb) throws IOException {
     bufferList.add(sb);
-    if (buffersForPutBlock == null) {
-      buffersForPutBlock = new ArrayList<>();
-    }
-    buffersForPutBlock.add(sb);
+    pendingFlush.addBuffer(sb);
     ByteBuffer dup = sb.duplicate();
     dup.position(0);
     dup.limit(sb.position());
@@ -336,7 +331,7 @@ private void doFlushIfNeeded() throws IOException {
     long streamWindow = config.getStreamWindowSize() / config
         .getDataStreamMinPacketSize();
     if (!bufferList.isEmpty() && bufferList.size() % boundary == 0 &&
-        buffersForPutBlock != null && !buffersForPutBlock.isEmpty()) {
+        pendingFlush.hasBuffersForPutBlock()) {
       updateFlushLength();
       executePutBlock(false, false);
     }
@@ -381,10 +376,7 @@ public void writeOnRetry(long len) throws IOException {
     while (len > 0) {
       final StreamBuffer buf = bufferList.get(count);
       final long writeLen = Math.min(buf.position(), len);
-      if (buffersForPutBlock == null) {
-        buffersForPutBlock = new ArrayList<>();
-      }
-      buffersForPutBlock.add(buf);
+      pendingFlush.addBuffer(buf);
       final ByteBuffer duplicated = buf.duplicate();
       duplicated.position(0);
       duplicated.limit(buf.position());
@@ -436,16 +428,16 @@ public void executePutBlock(boolean sync, boolean close) 
throws IOException {
     checkOpen();
     long flushPos = totalDataFlushedLength;
     final List<StreamBuffer> byteBufferList;
-    if (buffersForPutBlock != null) {
+    if (pendingFlush.hasBuffersForPutBlock()) {
       Objects.requireNonNull(bufferList, "bufferList == null");
-      byteBufferList = buffersForPutBlock;
-      buffersForPutBlock = null;
+      byteBufferList = pendingFlush.consumeBuffersForPutBlock();
       Objects.requireNonNull(byteBufferList, "byteBufferList == null");
     } else {
       // no data was written since the most recent putBlock
       byteBufferList = null;
     }
-    waitFuturesComplete();
+    waitFuturesComplete(pendingFlush);
+    pendingFlush = new PendingFlush();
     if (close && putBlockWithoutRaft) {
       // Wait for boundary PutBlock(s) before appending the stream-close 
PutBlock.
       waitPutBlockCommandFuturesComplete();
@@ -609,7 +601,7 @@ public static ByteBuffer getProtoLength(ByteBuffer 
putBlock, int max) {
   public void flush() throws IOException {
     if (xceiverClientFactory != null && xceiverClient != null
         && !config.isStreamBufferFlushDelay()) {
-      waitFuturesComplete();
+      waitFuturesComplete(pendingFlush);
     }
   }
 
@@ -636,10 +628,9 @@ private void flushOrSync(boolean sync) throws IOException {
     }
   }
 
-  public void waitFuturesComplete() throws IOException {
+  public void waitFuturesComplete(PendingFlush flush) throws IOException {
     try {
-      CompletableFuture.allOf(futures.toArray(EMPTY_FUTURE_ARRAY)).get();
-      futures.clear();
+      flush.waitForChunkFutures();
     } catch (Exception e) {
       LOG.warn("Failed to write all chunks through stream: " + e);
       throw new IOException(e);
@@ -834,7 +825,7 @@ private void writeChunkToContainer(ByteBuffer buf)
               }
             }, responseExecutor);
 
-    futures.add(future);
+    pendingFlush.addChunkFuture(future);
     containerBlockData.addChunks(chunkInfo);
   }
 
diff --git 
a/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/PendingFlush.java
 
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/PendingFlush.java
new file mode 100644
index 00000000000..69ce3126ece
--- /dev/null
+++ 
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/PendingFlush.java
@@ -0,0 +1,62 @@
+/*
+ * 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.storage;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import org.apache.ratis.protocol.DataStreamReply;
+
+/**
+ * Represents chunks written since the most recent PutBlock.
+ */
+class PendingFlush {
+  private static final CompletableFuture[] EMPTY_FUTURE_ARRAY = {};
+
+  // buffers for which putBlock is yet to be executed
+  private List<StreamBuffer> buffersForPutBlock;
+  private final List<CompletableFuture<DataStreamReply>> chunkFutures = new 
ArrayList<>();
+
+  /** Add a buffer for which PutBlock is yet to be executed. */
+  void addBuffer(StreamBuffer buffer) {
+    if (buffersForPutBlock == null) {
+      buffersForPutBlock = new ArrayList<>();
+    }
+    buffersForPutBlock.add(buffer);
+  }
+
+  /** Add a future for a chunk write sent to the DataStream. */
+  void addChunkFuture(CompletableFuture<DataStreamReply> future) {
+    chunkFutures.add(future);
+  }
+
+  boolean hasBuffersForPutBlock() {
+    return buffersForPutBlock != null && !buffersForPutBlock.isEmpty();
+  }
+
+  List<StreamBuffer> consumeBuffersForPutBlock() {
+    List<StreamBuffer> buffers = buffersForPutBlock;
+    buffersForPutBlock = null;
+    return buffers;
+  }
+
+  void waitForChunkFutures() throws Exception {
+    CompletableFuture.allOf(chunkFutures.toArray(EMPTY_FUTURE_ARRAY)).get();
+    chunkFutures.clear();
+  }
+}
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 3001a17616f..131fcca5360 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
@@ -149,6 +149,16 @@ void writeFlushBoundaryTriggersPutBlock() throws Exception 
{
     assertEquals(2, pipeline.getReceivedPutBlocks().size());
   }
 
+  @Test
+  void flushAfterFailedPutBlockBoundaryStillThrows() throws Exception {
+    MockDatanodePipeline pipeline = new MockDatanodePipeline();
+    byte[] data = randomBytes(400);
+    pipeline.failChunkAfter(3, () -> new IOException("injected"));
+    BlockDataStreamOutput stream = createStream(pipeline);
+    assertThrows(IOException.class, () -> stream.write(ByteBuffer.wrap(data), 
0, data.length));
+    assertThrows(IOException.class, stream::flush);
+  }
+
   @Test
   void midStreamPutBlockUsesStreamCommandWhenEnabled() throws Exception {
     MockDatanodePipeline pipeline = new MockDatanodePipeline();


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

Reply via email to