szetszwo commented on code in PR #11245:
URL: https://github.com/apache/ozone/pull/11245#discussion_r4053839280
##########
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:
For byte array read and readFully, let's just call the ByteBuffer methods?
```java
@Override
public int read(long position, byte[] buffer, int offset, int length)
throws IOException {
validatePositionedReadArgs(position, buffer, offset, length);
return read(position, ByteBuffer.wrap(buffer, offset, length));
}
@Override
public void readFully(long position, byte[] buffer, int offset, int
length) throws IOException {
validatePositionedReadArgs(position, buffer, offset, length);
readFully(position, ByteBuffer.wrap(buffer, offset, length));
}
```
##########
hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/OzoneFSInputStream.java:
##########
@@ -37,7 +37,9 @@
* The input stream for Ozone file system.
*
* TODO: Make inputStream generic for both rest and rpc clients
- * This class is not thread safe.
+ * Sequential reads are not thread safe. Positioned reads delegate to the
+ * underlying {@link ExtendedInputStream} when it supports them; otherwise they
+ * fall back to a synchronized seek-read-restore sequence.
*/
Review Comment:
Let's remove the TODO and some suggestion below:
```java
/**
* The input stream for Ozone file system.
* <p>
* Sequential reads are NOT thread safe.
* <p>
* Positioned reads are thread safe.
* When the underlying stream is an {@link ExtendedInputStream} and
* when it supports {@link ExtendedInputStream#readFully(long, ByteBuffer)},
* they delegate to it.
* Otherwise, they fall back to the default synchronized seek-read-restore
implementation.
* <p>
* Applications must use either sequential reads or position reads at any
given time,
* but not concurrent sequential/position reads
*/
```
##########
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);
+ }
+ 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);
+ if (n < 0) {
+ throw new EOFException("End of file reached before reading fully.");
+ }
+ off += n;
+ remaining -= n;
+ }
+ }
+ }
+
+ @Override
+ public void readFully(long position, byte[] buffer) throws IOException {
+ readFully(position, buffer, 0, buffer.length);
+ }
Review Comment:
This is not needed since it is the same as the super method in FSInputStream.
##########
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:
We seems not updating the statistics in the ByteBuffer methods.
--
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]