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


##########
hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/OzoneFSInputStream.java:
##########
@@ -211,4 +225,101 @@ public void readFully(long position, ByteBuffer buf) 
throws IOException {
       }
     }
   }
+
+  /**
+   * Byte-array positioned read. Tries the native stateless {@link 
ExtendedInputStream#readFully}
+   * path first (via a zero-copy {@link ByteBuffer#wrap}). Falls back to a 
synchronized
+   * seek-read-restore using byte-array {@link #read(byte[], int, int)} so 
that the fallback
+   * works for any {@link Seekable} stream, not just those that also implement
+   * {@link org.apache.hadoop.fs.ByteBufferReadable}.
+   * <p>
+   * {@link FSInputStream} synchronizes its inherited implementation on {@code 
this}, a different
+   * monitor from {@code positionedReadLock}; without this override the two 
APIs can interleave.
+   */
+  @Override
+  public int read(long position, byte[] buffer, int offset, int length) throws 
IOException {
+    // Validate before touching ByteBuffer so that null throws IAE (not NPE) 
and
+    // negative position propagates as EOFException rather than being 
swallowed.
+    validatePositionedReadArgs(position, buffer, offset, length);
+    if (length == 0) {
+      return 0;
+    }
+    if (inputStream instanceof ExtendedInputStream) {
+      final ByteBuffer buf = ByteBuffer.wrap(buffer, offset, length);
+      try {
+        if (((ExtendedInputStream) inputStream).readFully(position, buf)) {
+          final int bytesRead = length - buf.remaining();
+          // readFullyStateless can return true with bytesRead==0 for 
pos==length.
+          // Convert that to -1 to comply with PositionedReadable contract.
+          if (bytesRead == 0) {
+            return -1;
+          }
+          if (statistics != null) {
+            statistics.incrementBytesRead(bytesRead);
+          }

Review Comment:
   let me add it as well.



##########
hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/OzoneFSInputStream.java:
##########
@@ -211,4 +225,101 @@ public void readFully(long position, ByteBuffer buf) 
throws IOException {
       }
     }
   }
+
+  /**
+   * Byte-array positioned read. Tries the native stateless {@link 
ExtendedInputStream#readFully}
+   * path first (via a zero-copy {@link ByteBuffer#wrap}). Falls back to a 
synchronized
+   * seek-read-restore using byte-array {@link #read(byte[], int, int)} so 
that the fallback
+   * works for any {@link Seekable} stream, not just those that also implement
+   * {@link org.apache.hadoop.fs.ByteBufferReadable}.
+   * <p>
+   * {@link FSInputStream} synchronizes its inherited implementation on {@code 
this}, a different
+   * monitor from {@code positionedReadLock}; without this override the two 
APIs can interleave.
+   */
+  @Override
+  public int read(long position, byte[] buffer, int offset, int length) throws 
IOException {

Review Comment:
   I mentioned that in the comment that if the the inner input stream is not 
child class of `ExtendedInputStream`, we will cannot use `ByteBuffer.wrap` 
because not all Seekable streams also implement `ByteBufferReadable`, and the 
casting may fail line#201 with `((ByteBufferReadable) inputStream).read(buf);` .
   
   but if you think we only support `ByteBufferReadable` , then we can reuse 
those existing method. 
   



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