taklwu commented on code in PR #11314:
URL: https://github.com/apache/ozone/pull/11314#discussion_r4126618258


##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/StreamBlockInputStream.java:
##########
@@ -178,6 +181,143 @@ synchronized int readFully(ByteBuffer targetBuf, boolean 
preRead) throws IOExcep
     return read > 0 ? read : EOF;
   }
 
+  /**
+   * Positioned read served by a dedicated one-shot streaming read which never 
touches the
+   * cursor, the pre-read buffer or the sequential reader.
+   *
+   * @param blockOffset the offset within the block to read from.
+   * @param dst the buffer to read into.
+   * @return the number of bytes copied into {@code dst}, or -1 if no byte 
could be read.
+   */
+  int readPositioned(long blockOffset, ByteBuffer dst) throws IOException {
+    if (!dst.hasRemaining()) {
+      return 0;
+    }
+    if (blockOffset < 0 || blockOffset >= blockLength) {
+      return EOF;
+    }
+    final XceiverClientFactory factory;
+    synchronized (this) {
+      checkOpen();
+      factory = xceiverClientFactory;
+    }
+    final int length = Math.toIntExact(Math.min(dst.remaining(), blockLength - 
blockOffset));
+    final int startPosition = dst.position();
+    int callRetries = 0;
+    while (true) {
+      final AtomicReference<OneShotReader> readerRef = new AtomicReference<>();
+      try {
+        return preadOnce(factory, readerRef, blockOffset, length, dst);
+      } catch (IOException e) {
+        dst.position(startPosition);
+        handlePreadException(e, readerRef.get(), blockOffset, callRetries++);
+      }
+    }
+  }
+
+  private int preadOnce(XceiverClientFactory factory, 
AtomicReference<OneShotReader> readerRef,
+      long blockOffset, int length, ByteBuffer dst) throws IOException {
+    final Pipeline pipeline = pipelineRef.get();
+    final XceiverClientSpi acquired = 
factory.acquireClientForReadData(pipeline);
+    if (acquired == null) {
+      throw new IOException("Failed to acquire client for " + pipeline);
+    }
+    if (!(acquired instanceof XceiverClientGrpc)) {
+      throw new IOException("Unexpected client class: " + 
acquired.getClass().getName() + ", " + pipeline);
+    }
+    final XceiverClientGrpc client = (XceiverClientGrpc) acquired;
+    try {
+      final OneShotReader reader = new OneShotReader(client);
+      readerRef.set(reader);
+      boolean streamInitialized = false;
+      try {
+        client.initStreamRead(blockID, reader, failedStreamingDatanodes);
+        streamInitialized = true;
+        final StreamingReadResponse response = reader.getResponse();
+        if (response == null) {
+          throw new IOException("Uninitialized StreamingReadResponse: " + 
blockID);
+        }
+        client.streamRead(ContainerProtocolCalls.buildReadBlockCommandProto(
+            blockID, blockOffset, length, responseDataSize, tokenRef.get(), 
pipeline), response);
+
+        int copied = 0;
+        while (copied < length) {
+          final ReadBlockResponseProto proto = reader.poll();
+          if (proto == null) {
+            break;
+          }
+          final ByteBuffer buffer = getByteBuffer(proto, blockOffset + copied);
+          if (buffer == null || !buffer.hasRemaining()) {
+            continue;
+          }
+          final ByteBuffer tmpBuf = buffer.duplicate();
+          tmpBuf.limit(tmpBuf.position() + Math.min(buffer.remaining(), length 
- copied));
+          copied += tmpBuf.remaining();
+          dst.put(tmpBuf);

Review Comment:
   ack



##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/StreamBlockInputStream.java:
##########
@@ -178,6 +181,143 @@ synchronized int readFully(ByteBuffer targetBuf, boolean 
preRead) throws IOExcep
     return read > 0 ? read : EOF;
   }
 
+  /**
+   * Positioned read served by a dedicated one-shot streaming read which never 
touches the
+   * cursor, the pre-read buffer or the sequential reader.
+   *
+   * @param blockOffset the offset within the block to read from.
+   * @param dst the buffer to read into.
+   * @return the number of bytes copied into {@code dst}, or -1 if no byte 
could be read.
+   */
+  int readPositioned(long blockOffset, ByteBuffer dst) throws IOException {
+    if (!dst.hasRemaining()) {
+      return 0;
+    }
+    if (blockOffset < 0 || blockOffset >= blockLength) {
+      return EOF;
+    }
+    final XceiverClientFactory factory;
+    synchronized (this) {
+      checkOpen();
+      factory = xceiverClientFactory;
+    }
+    final int length = Math.toIntExact(Math.min(dst.remaining(), blockLength - 
blockOffset));
+    final int startPosition = dst.position();
+    int callRetries = 0;
+    while (true) {
+      final AtomicReference<OneShotReader> readerRef = new AtomicReference<>();
+      try {
+        return preadOnce(factory, readerRef, blockOffset, length, dst);
+      } catch (IOException e) {
+        dst.position(startPosition);
+        handlePreadException(e, readerRef.get(), blockOffset, callRetries++);
+      }
+    }
+  }
+
+  private int preadOnce(XceiverClientFactory factory, 
AtomicReference<OneShotReader> readerRef,
+      long blockOffset, int length, ByteBuffer dst) throws IOException {
+    final Pipeline pipeline = pipelineRef.get();
+    final XceiverClientSpi acquired = 
factory.acquireClientForReadData(pipeline);
+    if (acquired == null) {
+      throw new IOException("Failed to acquire client for " + pipeline);
+    }
+    if (!(acquired instanceof XceiverClientGrpc)) {
+      throw new IOException("Unexpected client class: " + 
acquired.getClass().getName() + ", " + pipeline);
+    }
+    final XceiverClientGrpc client = (XceiverClientGrpc) acquired;

Review Comment:
   we want the pread have its own XceiverClientGrpc and does not share with the 
sequential read that use synchronized acquireClient. it will avoid the 
   
   this logic is already part of ChunkInputStream#readPositioned at line#468
   
   let me refactor the StreamBlockInputStream#acquireClient and reuse them 
separately



##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/StreamBlockInputStream.java:
##########
@@ -96,7 +97,9 @@ public class StreamBlockInputStream extends 
BlockExtendedInputStream {
   private final Function<BlockID, BlockLocationInfo> refreshFunction;
   private final RetryPolicy retryPolicy;
   private int retries = 0;
-  private final Set<DatanodeID> failedStreamingDatanodes = new HashSet<>();
+  // Shared by sequential and positioned reads; must be thread-safe for 
concurrent access.
+  private final Set<DatanodeID> failedStreamingDatanodes = 
ConcurrentHashMap.newKeySet();
+  private final Object refreshLock = new Object();

Review Comment:
   failedStreamingDatanodes should need the concurrent access and be used by 
both sequential and  positional reads. 
   
   but for the refreshLock , I missed that pipelineRef and  tokenRef are using 
atomic reference, so the lock should be removed, thanks!



##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/MultipartInputStream.java:
##########
@@ -276,6 +268,11 @@ private boolean readFullyStateless(long position, 
ByteBuffer buffer) throws IOEx
     return true;
   }
 
+  @FunctionalInterface
+  private interface PartPositionedReader {
+    int readPositioned(int index, long partOffset, ByteBuffer buffer) throws 
IOException;
+  }

Review Comment:
   this will touch more files but it's a good idea



##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/StreamBlockInputStream.java:
##########
@@ -480,10 +626,12 @@ public String toString() {
   }
 
   /**
-   * Implementation of a StreamObserver used to received and buffer streaming 
GRPC reads.
+   * Common StreamObserver logic shared by the sequential reader and the 
one-shot readers used by
+   * positioned reads.
    */
-  public class StreamingReader implements StreamingReaderSpi {
-    private final String name = StreamBlockInputStream.this.name + "-reader" + 
READER_ID.getAndIncrement();
+  abstract class AbstractStreamingReader implements StreamingReaderSpi {

Review Comment:
   ack



##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/StreamBlockInputStream.java:
##########
@@ -178,6 +181,143 @@ synchronized int readFully(ByteBuffer targetBuf, boolean 
preRead) throws IOExcep
     return read > 0 ? read : EOF;
   }
 
+  /**
+   * Positioned read served by a dedicated one-shot streaming read which never 
touches the
+   * cursor, the pre-read buffer or the sequential reader.
+   *
+   * @param blockOffset the offset within the block to read from.
+   * @param dst the buffer to read into.
+   * @return the number of bytes copied into {@code dst}, or -1 if no byte 
could be read.
+   */
+  int readPositioned(long blockOffset, ByteBuffer dst) throws IOException {
+    if (!dst.hasRemaining()) {
+      return 0;
+    }

Review Comment:
   ack and moved to PartInputStream



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to