-
Notifications
You must be signed in to change notification settings - Fork 642
HDDS-16400. Fix thread-safety of positioned reads in OzoneFSInputStream #11245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
taklwu
wants to merge
5
commits into
apache:master
Choose a base branch
from
taklwu:HDDS-16400
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b35981c
HDDS-16400. Fix thread-safety of positioned reads in OzoneFSInputStream
taklwu bbe7753
address comment and align with AbstractOzoneContractTest
taklwu bf24a8a
fix another unit test for position<=-1
taklwu ca831e4
Merge remote-tracking branch 'origin/master' into HDDS-16400
adoroszlai f64eb71
Merge remote-tracking branch 'origin/master' into HDDS-16400
adoroszlai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
| */ | ||
| @InterfaceAudience.Private | ||
| @InterfaceStability.Evolving | ||
|
|
@@ -46,6 +48,7 @@ public class OzoneFSInputStream extends FSInputStream | |
|
|
||
| private final InputStream inputStream; | ||
| private final Statistics statistics; | ||
| private final Object positionedReadLock = new Object(); | ||
|
|
||
| public OzoneFSInputStream(InputStream inputStream, Statistics statistics) { | ||
| this.inputStream = inputStream; | ||
|
|
@@ -169,6 +172,9 @@ public int read(long position, ByteBuffer buf) throws IOException { | |
| if (!buf.hasRemaining()) { | ||
| return 0; | ||
| } | ||
| if (position < 0) { | ||
| throw new EOFException("position is negative: " + position); | ||
| } | ||
| if (inputStream instanceof ExtendedInputStream) { | ||
| final int remainingBeforeRead = buf.remaining(); | ||
| try { | ||
|
|
@@ -180,6 +186,14 @@ public int read(long position, ByteBuffer buf) throws IOException { | |
| } | ||
| } | ||
|
|
||
| // Fallback: stateful seek-read-restore on the shared cursor. | ||
| synchronized (positionedReadLock) { | ||
| return readAtPositionSeekRestore(position, buf); | ||
| } | ||
| } | ||
|
|
||
| private int readAtPositionSeekRestore(long position, ByteBuffer buf) | ||
| throws IOException { | ||
| long oldPos = this.getPos(); | ||
| int bytesRead; | ||
| try { | ||
|
|
@@ -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); | ||
| } | ||
|
Comment on lines
+257
to
+259
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We seems not updating the statistics in the ByteBuffer methods. |
||
| 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); | ||
| } | ||
|
Comment on lines
+308
to
+311
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not needed since it is the same as the super method in FSInputStream. |
||
|
|
||
| private int readAtPositionSeekRestoreByteArray(long position, byte[] buffer, int offset, int length) | ||
| throws IOException { | ||
| final long oldPos = getPos(); | ||
| try { | ||
| ((Seekable) inputStream).seek(position); | ||
| return read(buffer, offset, length); | ||
| } catch (EOFException e) { | ||
| return -1; | ||
| } finally { | ||
| ((Seekable) inputStream).seek(oldPos); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For byte array read and readFully, let's just call the ByteBuffer methods?