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 3896af2276b HDDS-17002. Streaming block read after unbuffer() requests 
nothing at the current position (#11892)
3896af2276b is described below

commit 3896af2276b315bd1f279b7a48bf7eaa686622b1
Author: Sergey Soldatov <[email protected]>
AuthorDate: Mon Nov 2 01:48:49 2026 -0800

    HDDS-17002. Streaming block read after unbuffer() requests nothing at the 
current position (#11892)
    
    Co-authored-by: Claude Fable 5 <[email protected]>
---
 .../hdds/scm/storage/StreamBlockInputStream.java   |  2 +
 .../scm/storage/TestStreamBlockInputStream.java    | 59 ++++++++++++++++++++++
 .../rpc/read/TestStreamReadDatanodeFailover.java   | 32 ++++++++++++
 3 files changed, 93 insertions(+)

diff --git 
a/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/StreamBlockInputStream.java
 
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/StreamBlockInputStream.java
index a0aac296fca..2b7269cd314 100644
--- 
a/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/StreamBlockInputStream.java
+++ 
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/StreamBlockInputStream.java
@@ -374,6 +374,8 @@ public synchronized void unbuffer() {
 
   private synchronized void closeReader(String reason) {
     readBuffer = null;
+    // Requested-but-unreceived data dies with the stream; a fresh stream must 
re-request from position.
+    requestedLength = position;
     if (streamingReader == null) {
       return;
     }
diff --git 
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestStreamBlockInputStream.java
 
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestStreamBlockInputStream.java
index 7bdf10b495f..7cabc661a28 100644
--- 
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestStreamBlockInputStream.java
+++ 
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestStreamBlockInputStream.java
@@ -85,6 +85,8 @@
 import org.apache.ratis.thirdparty.io.grpc.StatusRuntimeException;
 import org.apache.ratis.thirdparty.io.grpc.stub.ClientCallStreamObserver;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 import org.mockito.ArgumentCaptor;
 
 /**
@@ -713,6 +715,63 @@ public void testDeadlineExceededFailsOverToFreshStream() 
throws Exception {
     assertEquals(2, initCount.get(), "a fresh stream should have been 
initialized for the retry");
   }
 
+  /**
+   * unbuffer() must reset the request high-water mark: a fresh stream 
requesting from the stale
+   * prefetch offset never receives data covering the position and stalls 
until the read timeout.
+   * A pre-read larger than the block clamps the stale mark to the block end, 
so nothing is left to request;
+   * a smaller one leaves enough stale outstanding bytes to skip the request.
+   */
+  @ParameterizedTest
+  @ValueSource(longs = {1024, 4})
+  public void testReadAfterUnbufferResumesFromPosition(long preReadSize) 
throws Exception {
+    OzoneClientConfig clientConfig = newStreamReadConfig();
+    clientConfig.setStreamReadPreReadSize(preReadSize);
+    // Keep the test fast if a stale request offset makes the read wait for a 
response that never comes.
+    clientConfig.setStreamReadTimeout(Duration.ofMillis(500));
+    clientConfig.setMaxReadRetryCount(0);
+    byte[] data = {1, 2, 3, 4, 5, 6, 7, 8};
+    BlockID blockID = new BlockID(1L, 23L);
+    Pipeline pipeline = mockStandalonePipeline();
+    ClientCallStreamObserver<ContainerCommandRequestProto> requestObserver = 
mock(ClientCallStreamObserver.class);
+    StreamingReadResponse streamingReadResponse = 
mock(StreamingReadResponse.class);
+    
when(streamingReadResponse.getRequestObserver()).thenReturn(requestObserver);
+    
when(streamingReadResponse.getDatanodeDetails()).thenReturn(MockDatanodeDetails.randomDatanodeDetails());
+
+    AtomicReference<StreamingReaderSpi> readerRef = new AtomicReference<>();
+    XceiverClientGrpc xceiverClient = mock(XceiverClientGrpc.class);
+    doAnswer(inv -> {
+      StreamingReaderSpi reader = inv.getArgument(1);
+      reader.setStreamingReadResponse(streamingReadResponse);
+      readerRef.set(reader);
+      return null;
+    }).when(xceiverClient).initStreamRead(any(BlockID.class), any(), any());
+    // Serve exactly the requested range, like a real datanode.
+    doAnswer(inv -> {
+      ContainerCommandRequestProto request = inv.getArgument(0);
+      final int offset = (int) request.getReadBlock().getOffset();
+      final int length = (int) request.getReadBlock().getLength();
+      byte[] slice = Arrays.copyOfRange(data, offset, Math.min(offset + 
length, data.length));
+      readerRef.get().onNext(buildResponseProto(slice, offset));
+      return null;
+    }).when(xceiverClient).streamRead(any(), any());
+
+    XceiverClientFactory xceiverClientFactory = 
mock(XceiverClientFactory.class);
+    
when(xceiverClientFactory.acquireClientForReadData(any(Pipeline.class))).thenReturn(xceiverClient);
+
+    try (StreamBlockInputStream sbis = new StreamBlockInputStream(
+        newBlockInfo(blockID, data.length), pipeline, null, 
xceiverClientFactory, NO_REFRESH, clientConfig)) {
+      ByteBuffer first = ByteBuffer.allocate(2);
+      assertEquals(2, sbis.read(first), "first read should return the 
requested bytes");
+      assertArrayEquals(new byte[] {1, 2}, first.array());
+
+      sbis.unbuffer();
+
+      ByteBuffer second = ByteBuffer.allocate(2);
+      assertEquals(2, sbis.read(second), "read after unbuffer must resume from 
the current position");
+      assertArrayEquals(new byte[] {3, 4}, second.array());
+    }
+  }
+
   private OzoneClientConfig newStreamReadConfig() {
     OzoneClientConfig clientConfig = new OzoneClientConfig();
     clientConfig.setChecksumVerify(false);
diff --git 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/client/rpc/read/TestStreamReadDatanodeFailover.java
 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/client/rpc/read/TestStreamReadDatanodeFailover.java
index ebfeb28e8b9..aa44acffa53 100644
--- 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/client/rpc/read/TestStreamReadDatanodeFailover.java
+++ 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/client/rpc/read/TestStreamReadDatanodeFailover.java
@@ -63,6 +63,8 @@
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 
 /**
  * Verifies that streaming block reads fail over to a healthy replica when the
@@ -207,6 +209,36 @@ void testStreamReadFailoverAfterActiveDatanodeStopped() 
throws Exception {
     }
   }
 
+  /**
+   * Reads after {@code unbuffer()} must be served by a fresh stream from the 
current position, without
+   * timing out and excluding the healthy datanode, so they keep working after 
more unbuffer calls than replicas.
+   * The pre-read window is either smaller or larger than the key, which 
covers both ways a stale request mark
+   * would make the new stream request nothing.
+   */
+  @ParameterizedTest
+  @ValueSource(longs = {16 * 1024, 32 * 1024 * 1024})
+  void testStreamReadAfterUnbuffer(long preReadSize) throws Exception {
+    OzoneConfiguration streamConf = streamReadConfig(cluster.getConf());
+    OzoneClientConfig clientConfig = 
streamConf.getObject(OzoneClientConfig.class);
+    clientConfig.setStreamReadPreReadSize(preReadSize);
+    streamConf.setFromObject(clientConfig);
+    try (OzoneClient client = OzoneClientFactory.getRpcClient(streamConf)) {
+      BucketForTesting bucket = BucketForTesting.newBuilder(client).build();
+      String keyName = newKeyName();
+      byte[] content = bucket.writeRandomBytes(keyName, 32 * 1024);
+      int replicas = getPipelineDatanodes(cluster, bucket.delegate(), 
keyName).size();
+
+      try (KeyInputStream in = bucket.getKeyInputStream(keyName)) {
+        int offset = 0;
+        for (int i = 0; i <= replicas; i++) {
+          in.unbuffer();
+          assertEquals(Byte.toUnsignedInt(content[offset++]), in.read());
+        }
+        readRemaining(in, content, offset);
+      }
+    }
+  }
+
   private void stopDatanode(DatanodeDetails dn) throws IOException {
     cluster.shutdownHddsDatanode(dn);
     stoppedDatanodes.add(dn);


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

Reply via email to