This is an automated email from the ASF dual-hosted git repository.
peterxcli 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 b10a7d427bd HDDS-15886. Use block length from datanode for
under-construction block in StreamBlockInputStream (#11551)
b10a7d427bd is described below
commit b10a7d427bdf83807a7e48a1be507d3b02d5a503
Author: Chung En Lee <[email protected]>
AuthorDate: Wed Oct 21 17:09:47 2026 +0800
HDDS-15886. Use block length from datanode for under-construction block in
StreamBlockInputStream (#11551)
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Peter Lee <[email protected]>
---
.../hdds/scm/storage/MultipartInputStream.java | 8 ++-
.../hdds/scm/storage/StreamBlockInputStream.java | 42 +++++++++++++-
.../client/io/BlockInputStreamFactoryImpl.java | 3 +-
.../hdds/scm/storage/TestMultipartInputStream.java | 21 +++++++
.../scm/storage/TestStreamBlockInputStream.java | 64 ++++++++++++++++++++++
.../java/org/apache/hadoop/fs/ozone/TestHSync.java | 24 +++++++-
6 files changed, 153 insertions(+), 9 deletions(-)
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 d62c1bb42f7..57fddeb8661 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
@@ -56,7 +56,7 @@ public class MultipartInputStream extends ExtendedInputStream
{
// can be reset if a new position is seeked.
private int prevPartIndex;
- private boolean initialized = false;
+ private volatile boolean initialized = false;
public MultipartInputStream(String keyName,
List<? extends PartInputStream> inputStreams) {
@@ -185,6 +185,10 @@ protected int readPositioned(long position, ByteBuffer
buffer) throws IOExceptio
if (!buffer.hasRemaining()) {
return 0;
}
+ if (!initialized) {
+ // Update the key length, e.g. for the under-construction last block of
an hsync-ed key.
+ initialize();
+ }
long pos = position;
int bytesRead = 0;
while (buffer.hasRemaining() && pos < length) {
@@ -225,6 +229,8 @@ public synchronized void initialize() throws IOException {
for (PartInputStream partInputStream : partStreams) {
if (partInputStream instanceof BlockInputStream) {
((BlockInputStream) partInputStream).initialize();
+ } else if (partInputStream instanceof StreamBlockInputStream) {
+ ((StreamBlockInputStream) partInputStream).updateBlockLength();
}
}
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 8bde1f569cf..c725bdb242f 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
@@ -77,7 +77,9 @@ public class StreamBlockInputStream extends
BlockExtendedInputStream {
private final String name = "stream" + STREAM_ID.getAndIncrement();
private final BlockID blockID;
- private final long blockLength;
+ private long blockLength;
+ // The block is under construction, so its length from OM may be stale;
fetch it from the datanode once.
+ private boolean needUpdateBlockLength;
private final int responseDataSize;
private final long preReadSize;
private final Duration readTimeout;
@@ -105,8 +107,19 @@ public StreamBlockInputStream(
XceiverClientFactory xceiverClientFactory,
Function<BlockID, BlockLocationInfo> refreshFunction,
OzoneClientConfig config) throws IOException {
- this.blockID = blockID;
- this.blockLength = length;
+ this(new
BlockLocationInfo.Builder().setBlockID(blockID).setLength(length).build(),
+ pipeline, token, xceiverClientFactory, refreshFunction, config);
+ }
+
+ public StreamBlockInputStream(
+ BlockLocationInfo blockInfo, Pipeline pipeline,
+ Token<OzoneBlockTokenIdentifier> token,
+ XceiverClientFactory xceiverClientFactory,
+ Function<BlockID, BlockLocationInfo> refreshFunction,
+ OzoneClientConfig config) throws IOException {
+ this.blockID = blockInfo.getBlockID();
+ this.blockLength = blockInfo.getLength();
+ this.needUpdateBlockLength = blockInfo.isUnderConstruction();
pipelineRef.set(setPipeline(pipeline));
tokenRef.set(token);
this.xceiverClientFactory = xceiverClientFactory;
@@ -192,6 +205,7 @@ public int readPositioned(long blockOffset, ByteBuffer dst)
throws IOException {
if (!dst.hasRemaining()) {
return 0;
}
+ updateBlockLength();
if (blockOffset < 0 || blockOffset >= blockLength) {
return EOF;
}
@@ -270,6 +284,7 @@ private void handlePreadException(IOException cause,
StreamingReader reader, lon
}
private synchronized boolean dataAvailableToRead(int length, boolean
preRead) throws IOException {
+ updateBlockLength();
if (position >= blockLength) {
return false;
}
@@ -431,6 +446,27 @@ private XceiverClientGrpc
acquireGrpcClientForReadData(XceiverClientFactory fact
return (XceiverClientGrpc) acquired;
}
+ /**
+ * 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 {
+ while (needUpdateBlockLength) {
+ try {
+ acquireClient();
+ blockLength = ContainerProtocolCalls.getBlock(xceiverClient, blockID,
tokenRef.get(), pipelineRef.get())
+ .getBlockData().getSize();
+ needUpdateBlockLength = false;
+ retries = 0;
+ LOG.debug("{}: Updated block length to {} for block {}", name,
blockLength, blockID);
+ } catch (IOException ioe) {
+ handleExceptions(ioe);
+ } finally {
+ releaseClient();
+ }
+ }
+ }
+
private synchronized void initialize() throws IOException {
while (streamingReader == null) {
try {
diff --git
a/hadoop-hdds/client/src/main/java/org/apache/hadoop/ozone/client/io/BlockInputStreamFactoryImpl.java
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/ozone/client/io/BlockInputStreamFactoryImpl.java
index f2af2296d92..19cafb40341 100644
---
a/hadoop-hdds/client/src/main/java/org/apache/hadoop/ozone/client/io/BlockInputStreamFactoryImpl.java
+++
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/ozone/client/io/BlockInputStreamFactoryImpl.java
@@ -108,8 +108,7 @@ public BlockExtendedInputStream create(ReplicationConfig
repConfig,
blockInfo, xceiverFactory, refreshFunction,
ecBlockStreamFactory, config);
} else if (config.isStreamReadBlock() &&
allDataNodesSupportStreamBlock(pipeline)) {
- return new StreamBlockInputStream(blockInfo.getBlockID(),
blockInfo.getLength(), pipeline, token, xceiverFactory,
- refreshFunction, config);
+ return new StreamBlockInputStream(blockInfo, pipeline, token,
xceiverFactory, refreshFunction, config);
} else {
return new BlockInputStream(blockInfo,
pipeline, token, xceiverFactory, refreshFunction,
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 8eb81ed7069..3ab9e171f49 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
@@ -39,6 +39,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
import org.apache.hadoop.hdds.client.BlockID;
import org.apache.hadoop.hdds.client.ContainerBlockID;
@@ -111,6 +112,26 @@ public void testStreamBlockPositionedReadPartialAtEof()
throws Exception {
}
}
+ @Test
+ public void testPositionedReadBeyondStaleLengthOfUnderConstructionBlock()
throws Exception {
+ byte[] partData = generateRandomData(1024);
+ int staleLength = 512;
+ StreamBlockInputStream part = stubStreamBlockPart(partData);
+ AtomicLong partLength = new AtomicLong(staleLength);
+ when(part.getLength()).thenAnswer(invocation -> partLength.get());
+ doAnswer(invocation -> {
+ partLength.set(partData.length);
+ return null;
+ }).when(part).updateBlockLength();
+ try (MultipartInputStream multipartStream = new
MultipartInputStream("test-key", Collections.singletonList(part))) {
+ ByteBuffer buffer = ByteBuffer.allocate(partData.length - staleLength);
+ multipartStream.readFully(staleLength, buffer);
+ assertArrayEquals(Arrays.copyOfRange(partData, staleLength,
partData.length), buffer.array());
+ assertEquals(partData.length, multipartStream.getLength());
+ verify(part, never()).seek(anyLong());
+ }
+ }
+
@Test
public void testStreamBlockConcurrentPositionedRead() throws Exception {
byte[] part0Data = generateRandomData(PART_SIZE);
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 725297738e2..0fd8091b26d 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
@@ -474,6 +474,70 @@ public void
testPollDoesNotDropQueuedItemWhenFutureCompletesFirst() throws Excep
}
}
+ /**
+ * The length from OM may be stale for an under construction block, e.g. the
last block of an hsync-ed file,
+ * so the block length must be taken from the datanode.
+ */
+ @Test
+ public void testUnderConstructionBlockUsesLengthFromDatanode() throws
Exception {
+ byte[] data = {1, 2, 3, 4};
+ BlockLocationInfo blockInfo = new
BlockLocationInfo.Builder().setBlockID(new BlockID(1L,
30L)).setLength(2).build();
+ blockInfo.setUnderConstruction(true);
+ Pipeline pipeline = mockStandalonePipeline();
+ XceiverClientGrpc client = mockGetBlockClient(data, pipeline);
+ try (StreamBlockInputStream sbis = newStreamForBlock(blockInfo, pipeline,
client)) {
+ ByteBuffer buf = ByteBuffer.allocate(data.length + 1);
+ assertEquals(data.length, sbis.read(buf));
+ assertArrayEquals(data, Arrays.copyOf(buf.array(), data.length));
+ assertEquals(data.length, sbis.getLength());
+ assertEquals(-1, sbis.read(buf));
+ sbis.seek(data.length);
+ }
+ verify(client,
times(1)).sendCommand(any(ContainerCommandRequestProto.class), any());
+ }
+
+ @Test
+ public void testCompletedBlockDoesNotGetBlockFromDatanode() throws Exception
{
+ byte[] data = {1, 2, 3, 4};
+ BlockLocationInfo blockInfo = new
BlockLocationInfo.Builder().setBlockID(new BlockID(1L, 31L))
+ .setLength(data.length).build();
+ Pipeline pipeline = mockStandalonePipeline();
+ XceiverClientGrpc client = mockGetBlockClient(data, pipeline);
+ try (StreamBlockInputStream sbis = newStreamForBlock(blockInfo, pipeline,
client)) {
+ ByteBuffer buf = ByteBuffer.allocate(data.length);
+ assertEquals(data.length, sbis.read(buf));
+ assertArrayEquals(data, buf.array());
+ sbis.seek(0);
+ }
+ verify(client,
never()).sendCommand(any(ContainerCommandRequestProto.class), any());
+ }
+
+ private XceiverClientGrpc mockGetBlockClient(byte[] data, Pipeline pipeline)
throws Exception {
+ XceiverClientGrpc client = mockOffsetAwareStreamingReadClient(data,
mock(ClientCallStreamObserver.class));
+ when(client.getPipeline()).thenReturn(pipeline);
+ DatanodeDetails datanode = pipeline.getFirstNode();
+ when(pipeline.getClosestNode(any())).thenReturn(datanode);
+ when(client.sendCommand(any(ContainerCommandRequestProto.class),
any())).thenAnswer(inv -> {
+ ContainerCommandRequestProto request = inv.getArgument(0);
+ return ContainerCommandResponseProto.newBuilder()
+ .setCmdType(Type.GetBlock)
+ .setResult(ContainerProtos.Result.SUCCESS)
+ .setGetBlock(ContainerProtos.GetBlockResponseProto.newBuilder()
+ .setBlockData(ContainerProtos.BlockData.newBuilder()
+ .setBlockID(request.getGetBlock().getBlockID())
+ .setSize(data.length)))
+ .build();
+ });
+ return client;
+ }
+
+ private StreamBlockInputStream newStreamForBlock(BlockLocationInfo
blockInfo, Pipeline pipeline,
+ XceiverClientGrpc client) throws Exception {
+ XceiverClientFactory factory = mock(XceiverClientFactory.class);
+
when(factory.acquireClientForReadData(any(Pipeline.class))).thenReturn(client);
+ return new StreamBlockInputStream(blockInfo, pipeline, null, factory,
NO_REFRESH, newStreamReadConfig());
+ }
+
private StreamBlockInputStream newStreamForData(byte[] data) throws
Exception {
BlockID blockID = new BlockID(1L, 17L);
Pipeline pipeline = mockStandalonePipeline();
diff --git
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/TestHSync.java
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/TestHSync.java
index 01087c7cfce..de04013e8da 100644
---
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/TestHSync.java
+++
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/TestHSync.java
@@ -55,6 +55,7 @@
import java.security.GeneralSecurityException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -398,12 +399,17 @@ private static String
getChunkPathOnDataNode(FSDataOutputStream outputStream)
return chunkPath;
}
- @Test
- public void testHSyncSeek() throws Exception {
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ public void testHSyncSeek(boolean streamReadBlock) throws Exception {
// Set the fs.defaultFS
final String rootPath = String.format("%s://%s.%s/",
OZONE_URI_SCHEME, bucket.getName(), bucket.getVolumeName());
CONF.set(CommonConfigurationKeysPublic.FS_DEFAULT_NAME_KEY, rootPath);
+ final OzoneConfiguration conf = new OzoneConfiguration(CONF);
+ final OzoneClientConfig clientConfig =
conf.getObject(OzoneClientConfig.class);
+ clientConfig.setStreamReadBlock(streamReadBlock);
+ conf.setFromObject(clientConfig);
final String dir = OZONE_ROOT + bucket.getVolumeName()
+ OZONE_URI_DELIMITER + bucket.getName();
@@ -413,7 +419,7 @@ public void testHSyncSeek() throws Exception {
final byte[] buffer = new byte[1024];
ThreadLocalRandom.current().nextBytes(data);
- try (FileSystem fs = FileSystem.get(CONF)) {
+ try (FileSystem fs = FileSystem.newInstance(conf)) {
// Create key1
try (FSDataOutputStream os = fs.create(key1, true)) {
os.write(data, 0, WAL_HEADER_LEN);
@@ -431,6 +437,18 @@ public void testHSyncSeek() throws Exception {
assertEquals(data[i], buffer[i], "expected at i=" + i);
}
}
+ // sequential read without seek
+ try (FSDataInputStream in = fs.open(key1)) {
+ final byte[] readData = new byte[WAL_HEADER_LEN + data.length];
+ in.readFully(readData);
+ assertArrayEquals(data, Arrays.copyOfRange(readData, WAL_HEADER_LEN,
readData.length));
+ }
+ // positioned read beyond the length in OM
+ try (FSDataInputStream in = fs.open(key1)) {
+ final byte[] readData = new byte[data.length];
+ in.readFully(WAL_HEADER_LEN, readData);
+ assertArrayEquals(data, readData);
+ }
} finally {
fs.delete(key1, false);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]