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


##########
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:
   could you please explain why we need this? refresh block info shouldnt care 
about concurrency.
   
   `pipelineRef` and `tokenRef` are atomics, too.



##########
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:
   Let's make `PartInputStream` have a `readPositioned` function, make sure all 
its downstream derived classes have the implementation of `readPositioned`, and 
pass the part stream to `readFullyStatelessAtParts`.
   
   so we can remove this.
   
   
   ```suggestion
   ```



##########
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:
   just like `BlockInputStream.readPositioned`, after this become the 
implementation of `PartInputStream.readPositioned`, this should return `EOF`.



##########
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:
   `buffer = buffer = getByteBuffer(...)` is already a new buffer in every 
iteration.
   ```suggestion
             buffer.limit(buffer.position() + Math.min(buffer.remaining(), 
length - copied));
             copied += buffer.remaining();
             dst.put(buffer);
   ```



##########
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:
   reuse `acquireClient()`? Seems the same.



##########
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:
   let's just add client field which is initialized in 
`StreamingReader(client)` constructor, and make it call 
`client.completeStreamRead()` when closing.
   
   so we dont need to add two classes



-- 
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