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


##########
hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestOzoneFSInputStream.java:
##########
@@ -187,4 +197,294 @@ public int read() {
     };
   }
 
+  @Test
+  public void testByteBufferPositionedReadNegativePositionThrows() throws 
Exception {
+    // read(long, ByteBuffer) must throw EOFException for negative positions,
+    // aligning with the byte-array PositionedReadable behaviour.
+    final byte[] source = RandomUtils.secure().randomBytes(SOURCE_SIZE);
+    final InterleavingSeekableInputStream underlying =
+        new InterleavingSeekableInputStream(source);
+    try (OzoneFSInputStream subject = new OzoneFSInputStream(underlying,
+        new FileSystem.Statistics("test"))) {
+      ByteBuffer buf = ByteBuffer.allocate(16);
+      assertThrows(EOFException.class, () -> subject.read(-1L, buf));
+    }
+  }
+
+  @Test
+  @Timeout(value = 30)
+  public void testConcurrentPositionedRead() throws Exception {
+    final byte[] source = RandomUtils.secure().randomBytes(SOURCE_SIZE);
+    final InterleavingSeekableInputStream underlying =
+        new InterleavingSeekableInputStream(source);
+    try (OzoneFSInputStream subject = new OzoneFSInputStream(underlying,
+        new FileSystem.Statistics("test"))) {
+      PositionedReadTestHelper.runConcurrentPositionedReads(source,
+          (offset, buf) -> subject.readFully(offset, buf));
+    }
+  }
+
+  @Test
+  @Timeout(value = 30)
+  public void testConcurrentPositionedReadEcFallback() throws Exception {
+    final byte[] source = RandomUtils.secure().randomBytes(SOURCE_SIZE);
+    final EcInterleavingInputStream underlying =
+        new EcInterleavingInputStream(source);
+    try (OzoneFSInputStream subject = new OzoneFSInputStream(underlying,
+        new FileSystem.Statistics("test"))) {
+      PositionedReadTestHelper.runConcurrentPositionedReads(source,
+          (offset, buf) -> subject.readFully(offset, buf));
+    }
+  }
+
+  @Test
+  @Timeout(value = 30)
+  public void testByteArrayFallbackWorksForSeekableOnlyStream() throws 
Exception {
+    // Regression: the old routing through read(long, ByteBuffer) would cast to
+    // ByteBufferReadable in readAtPositionSeekRestore and throw 
ClassCastException
+    // for a stream that only implements Seekable (not ByteBufferReadable).
+    final byte[] source = RandomUtils.secure().randomBytes(SOURCE_SIZE);
+    final SeekableOnlyInputStream underlying = new 
SeekableOnlyInputStream(source);
+    try (OzoneFSInputStream subject = new OzoneFSInputStream(underlying,
+        new FileSystem.Statistics("test"))) {
+      PositionedReadTestHelper.runConcurrentPositionedReads(source, (offset, 
buf) -> {
+        byte[] arr = new byte[buf.remaining()];
+        subject.readFully(offset, arr);
+        buf.put(arr);
+      });
+    }
+  }
+
+  @Test
+  @Timeout(value = 30)
+  public void testConcurrentByteArrayPositionedReadEcFallback() throws 
Exception {
+    final byte[] source = RandomUtils.secure().randomBytes(SOURCE_SIZE);
+    final EcInterleavingInputStream underlying =
+        new EcInterleavingInputStream(source);
+    try (OzoneFSInputStream subject = new OzoneFSInputStream(underlying,
+        new FileSystem.Statistics("test"))) {
+      PositionedReadTestHelper.runConcurrentPositionedReads(source, (offset, 
buf) -> {
+        byte[] arr = new byte[buf.remaining()];
+        subject.readFully(offset, arr);
+        buf.put(arr);
+      });
+    }
+  }
+
+  @Test
+  @Timeout(value = 30)
+  public void testConcurrentMixedApiEcFallback() throws Exception {
+    // ByteBuffer and byte-array callers share positionedReadLock; verify no 
interleaving.
+    final byte[] source = RandomUtils.secure().randomBytes(SOURCE_SIZE);
+    final EcInterleavingInputStream underlying =
+        new EcInterleavingInputStream(source);
+    try (OzoneFSInputStream subject = new OzoneFSInputStream(underlying,
+        new FileSystem.Statistics("test"))) {
+      PositionedReadTestHelper.runConcurrentPositionedReads(source, (offset, 
buf) -> {
+        if ((offset & 1) == 0) {
+          subject.readFully(offset, buf);
+        } else {
+          byte[] arr = new byte[buf.remaining()];
+          subject.readFully(offset, arr);
+          buf.put(arr);
+        }
+      });
+    }
+  }
+
+  /**
+   * Mimics KeyInputStream synchronized per-operation seek/read where 
multi-steps
+   * positioned reads must still be serialized at the FS layer.
+   */
+  private static final class InterleavingSeekableInputStream extends 
InputStream
+      implements Seekable, org.apache.hadoop.fs.ByteBufferReadable {
+
+    private final InterleavingReadState readState;
+
+    private InterleavingSeekableInputStream(byte[] data) {
+      this.readState = new InterleavingReadState(data);
+    }
+
+    @Override
+    public synchronized void seek(long p) {
+      readState.seek(p);
+    }
+
+    @Override
+    public synchronized long getPos() {
+      return readState.getPos();
+    }
+
+    @Override
+    public synchronized boolean seekToNewSource(long targetPos) {
+      return false;
+    }
+
+    @Override
+    public int read() {
+      return -1;
+    }
+
+    @Override
+    public synchronized int read(ByteBuffer buf) {
+      return readState.read(buf);
+    }
+  }
+
+  /**
+   * Mimics an erasure-coded key stream: {@link ExtendedInputStream#readFully}
+   * returns {@code false}, so {@link OzoneFSInputStream} falls back to
+   * seek-read-restore on the shared cursor. Implements both ByteBuffer and
+   * byte-array reads so both fallback paths can be exercised.
+   */
+  private static final class EcInterleavingInputStream extends 
ExtendedInputStream {
+
+    private final InterleavingReadState readState;
+
+    private EcInterleavingInputStream(byte[] data) {
+      this.readState = new InterleavingReadState(data);
+    }
+
+    @Override
+    public boolean readFully(long position, ByteBuffer buffer) {
+      return false;
+    }

Review Comment:
   It is the same as the super method.  Let's remove it.



##########
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 see.  Then, we should implement the array version. 
   - Let's make the array and ByteBuffer version look similar. 
   - We should also create private methods and reuse the code; see 
https://issues.apache.org/jira/secure/attachment/13084654/11245_review.patch



##########
hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/OzoneFSInputStream.java:
##########
@@ -211,4 +238,96 @@ 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);
+          }
+          return bytesRead;
+        }
+      } catch (EOFException e) {
+        // pos < 0 was already rejected by validatePositionedReadArgs above,
+        // so this EOFException means pos >= stream length → return -1.
+        return -1;
+      }
+    }
+    synchronized (positionedReadLock) {
+      return readAtPositionSeekRestoreByteArray(position, buffer, offset, 
length);
+    }
+  }
+
+  @Override
+  public void readFully(long position, byte[] buffer, int offset, int length) 
throws IOException {
+    validatePositionedReadArgs(position, buffer, offset, length);
+    if (length == 0) {
+      return;
+    }
+    if (inputStream instanceof ExtendedInputStream) {
+      final ByteBuffer buf = ByteBuffer.wrap(buffer, offset, length);
+      try {
+        if (((ExtendedInputStream) inputStream).readFully(position, buf)) {
+          // readFullyStateless returns true once bytesRead > 0, even if the
+          // buffer is only partially filled. Check that the buffer is full.
+          if (buf.hasRemaining()) {
+            throw new EOFException("End of file reached before reading 
fully.");
+          }
+          return;
+        }
+      } catch (EOFException e) {
+        throw e;
+      }
+    }
+    synchronized (positionedReadLock) {
+      int remaining = length;
+      int off = offset;
+      while (remaining > 0) {
+        int n = readAtPositionSeekRestoreByteArray(position + (length - 
remaining), buffer, off, remaining);

Review Comment:
   For position readFully, it should not call position read.  Otherwise, it 
will keep seeking back and forth.



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