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 f7d63a75a4e HDDS-17916. Seek on a multi-block key sends GetBlock for 
every block (#12151)
f7d63a75a4e is described below

commit f7d63a75a4e88e1dddbce38d1c2d494be1c9e2df
Author: Peter Lee <[email protected]>
AuthorDate: Wed Nov 25 15:10:56 2026 +0800

    HDDS-17916. Seek on a multi-block key sends GetBlock for every block 
(#12151)
---
 .../hadoop/hdds/scm/storage/BlockInputStream.java  | 10 +++++
 .../hdds/scm/storage/MultipartInputStream.java     |  2 +-
 .../hdds/scm/storage/TestBlockInputStream.java     | 19 +++++++++
 .../hdds/scm/storage/TestMultipartInputStream.java | 45 ++++++++++++++++++++++
 4 files changed, 75 insertions(+), 1 deletion(-)

diff --git 
a/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockInputStream.java
 
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockInputStream.java
index 88be628673b..03d34c53e91 100644
--- 
a/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockInputStream.java
+++ 
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockInputStream.java
@@ -203,6 +203,16 @@ public synchronized void initialize() throws IOException {
     }
   }
 
+  /**
+   * Use the block length from the datanode if the block is under construction,
+   * since the length from OM may be stale (e.g. the last block of an hsync-ed 
file).
+   */
+  public synchronized void updateBlockLength() throws IOException {
+    if (blockInfo.isUnderConstruction()) {
+      initialize();
+    }
+  }
+
   private synchronized void refreshBlockInfo(IOException cause) throws 
IOException {
     refreshBlockInfo(cause, blockID, pipelineRef, tokenRef, refreshFunction);
   }
diff --git 
a/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/MultipartInputStream.java
 
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/MultipartInputStream.java
index 57fddeb8661..84da1b66833 100644
--- 
a/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/MultipartInputStream.java
+++ 
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/MultipartInputStream.java
@@ -228,7 +228,7 @@ public synchronized void initialize() throws IOException {
 
     for (PartInputStream partInputStream : partStreams) {
       if (partInputStream instanceof BlockInputStream) {
-        ((BlockInputStream) partInputStream).initialize();
+        ((BlockInputStream) partInputStream).updateBlockLength();
       } else if (partInputStream instanceof StreamBlockInputStream) {
         ((StreamBlockInputStream) partInputStream).updateBlockLength();
       }
diff --git 
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestBlockInputStream.java
 
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestBlockInputStream.java
index c4233cd979f..bff28389922 100644
--- 
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestBlockInputStream.java
+++ 
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestBlockInputStream.java
@@ -473,6 +473,25 @@ public void testReadNotRetriedOnOtherException(IOException 
ex)
     }
   }
 
+  @Test
+  public void 
testUpdateBlockLengthUsesLengthFromDatanodeForUnderConstructionBlock() throws 
Exception {
+    BlockID blockID = new BlockID(new ContainerBlockID(1, 1));
+    BlockLocationInfo blockInfo =
+        new BlockLocationInfo(new 
BlockLocationInfo.Builder().setBlockID(blockID).setLength(blockSize - 
CHUNK_SIZE));
+    blockInfo.setUnderConstruction(true);
+    try (BlockInputStream subject = new BlockInputStream(blockInfo, 
MockPipeline.createSingleNodePipeline(), null,
+        mock(XceiverClientFactory.class), refreshFunction, clientConfig) {
+      @Override
+      protected ContainerProtos.BlockData getBlockData() {
+        return 
ContainerProtos.BlockData.newBuilder().setBlockID(getBlockID().getDatanodeBlockIDProtobuf())
+            .addAllChunks(chunks).setSize(blockSize).build();
+      }
+    }) {
+      subject.updateBlockLength();
+      assertEquals(blockSize, subject.getLength());
+    }
+  }
+
   @Test
   public void 
testPositionedReadOfClosedUninitializedStreamFailsWithIOException() throws 
IOException {
     BlockID blockID = new BlockID(new ContainerBlockID(1, 1));
diff --git 
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestMultipartInputStream.java
 
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestMultipartInputStream.java
index 3ab9e171f49..505718d49c5 100644
--- 
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestMultipartInputStream.java
+++ 
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestMultipartInputStream.java
@@ -27,6 +27,7 @@
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -132,6 +133,50 @@ public void 
testPositionedReadBeyondStaleLengthOfUnderConstructionBlock() throws
     }
   }
 
+  @Test
+  public void testSeekBeyondStaleLengthOfUnderConstructionBlock() throws 
Exception {
+    int staleLength = 512;
+    int actualLength = 1024;
+    BlockInputStream part = mock(BlockInputStream.class);
+    AtomicLong partLength = new AtomicLong(staleLength);
+    when(part.getLength()).thenAnswer(invocation -> partLength.get());
+    doAnswer(invocation -> {
+      partLength.set(actualLength);
+      return null;
+    }).when(part).updateBlockLength();
+    try (MultipartInputStream multipartStream = new 
MultipartInputStream("test-key", Collections.singletonList(part))) {
+      multipartStream.seek(staleLength + 1);
+      assertEquals(actualLength, multipartStream.getLength());
+      verify(part).seek(staleLength + 1);
+    }
+  }
+
+  @Test
+  public void testSeekGetsBlockDataOnlyForTheBlockItReads() throws Exception {
+    byte[] part0Data = generateRandomData(PART_SIZE);
+    byte[] part1Data = generateRandomData(PART_SIZE);
+    OzoneConfiguration conf = new OzoneConfiguration();
+    OzoneClientConfig clientConfig = conf.getObject(OzoneClientConfig.class);
+    clientConfig.setChecksumVerify(false);
+    Pipeline pipeline = MockPipeline.createSingleNodePipeline();
+    Function<BlockID, BlockLocationInfo> refreshFunction = 
mock(Function.class);
+    Checksum checksum = new Checksum(ChecksumType.NONE, 1024);
+
+    BlockInputStream part0 = spy(createBlockStream(new BlockID(new 
ContainerBlockID(1, 1)),
+        part0Data, pipeline, refreshFunction, clientConfig, checksum));
+    BlockInputStream part1 = spy(createBlockStream(new BlockID(new 
ContainerBlockID(1, 2)),
+        part1Data, pipeline, refreshFunction, clientConfig, checksum));
+    try (MultipartInputStream multipartStream = new 
MultipartInputStream("test-key", Arrays.asList(part0, part1))) {
+      int offsetInPart1 = 10;
+      multipartStream.seek(PART_SIZE + offsetInPart1);
+      byte[] buffer = new byte[16];
+      assertEquals(buffer.length, multipartStream.read(buffer, 0, 
buffer.length));
+      assertArrayEquals(Arrays.copyOfRange(part1Data, offsetInPart1, 
offsetInPart1 + buffer.length), buffer);
+      verify(part0, never()).getBlockData();
+      verify(part1).getBlockData();
+    }
+  }
+
   @Test
   public void testStreamBlockConcurrentPositionedRead() throws Exception {
     byte[] part0Data = generateRandomData(PART_SIZE);


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

Reply via email to