-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-23366] Improve hot reading path in ReadAheadInputStream #20555
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
Closed
+133
−117
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
987f15c
locking tweak
juliuszsompolski b26ffce
fill the read ahead buffer
juliuszsompolski ca45a88
reset isWaiting after exception
juliuszsompolski eaa6b4e
remove waiting loop
juliuszsompolski 7238181
add short path for skip
juliuszsompolski d6d44fc
fix compilation
juliuszsompolski 5273176
update comment
juliuszsompolski 62cefcd
update test suite with uneven buffer sizes
juliuszsompolski 1b3e970
more testing combinations
juliuszsompolski 52f4a7c
while loop against spurious wakeups
juliuszsompolski b6852aa
add comment about spuriour wakeups
juliuszsompolski 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| import java.nio.ByteBuffer; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.locks.Condition; | ||
| import java.util.concurrent.locks.ReentrantLock; | ||
|
|
||
|
|
@@ -78,9 +79,8 @@ public class ReadAheadInputStream extends InputStream { | |
| // whether there is a read ahead task running, | ||
| private boolean isReading; | ||
|
|
||
| // If the remaining data size in the current buffer is below this threshold, | ||
| // we issue an async read from the underlying input stream. | ||
| private final int readAheadThresholdInBytes; | ||
| // whether there is a reader waiting for data. | ||
| private AtomicBoolean isWaiting = new AtomicBoolean(false); | ||
|
|
||
| private final InputStream underlyingInputStream; | ||
|
|
||
|
|
@@ -97,20 +97,13 @@ public class ReadAheadInputStream extends InputStream { | |
| * | ||
| * @param inputStream The underlying input stream. | ||
| * @param bufferSizeInBytes The buffer size. | ||
| * @param readAheadThresholdInBytes If the active buffer has less data than the read-ahead | ||
| * threshold, an async read is triggered. | ||
| */ | ||
| public ReadAheadInputStream( | ||
| InputStream inputStream, int bufferSizeInBytes, int readAheadThresholdInBytes) { | ||
| InputStream inputStream, int bufferSizeInBytes) { | ||
| Preconditions.checkArgument(bufferSizeInBytes > 0, | ||
| "bufferSizeInBytes should be greater than 0, but the value is " + bufferSizeInBytes); | ||
| Preconditions.checkArgument(readAheadThresholdInBytes > 0 && | ||
| readAheadThresholdInBytes < bufferSizeInBytes, | ||
| "readAheadThresholdInBytes should be greater than 0 and less than bufferSizeInBytes, " + | ||
| "but the value is " + readAheadThresholdInBytes); | ||
| activeBuffer = ByteBuffer.allocate(bufferSizeInBytes); | ||
| readAheadBuffer = ByteBuffer.allocate(bufferSizeInBytes); | ||
| this.readAheadThresholdInBytes = readAheadThresholdInBytes; | ||
| this.underlyingInputStream = inputStream; | ||
| activeBuffer.flip(); | ||
| readAheadBuffer.flip(); | ||
|
|
@@ -166,12 +159,17 @@ public void run() { | |
| // in that case the reader waits for this async read to complete. | ||
| // So there is no race condition in both the situations. | ||
| int read = 0; | ||
| int off = 0, len = arr.length; | ||
| Throwable exception = null; | ||
| try { | ||
| while (true) { | ||
| read = underlyingInputStream.read(arr); | ||
| if (0 != read) break; | ||
| } | ||
| // try to fill the read ahead buffer. | ||
| // if a reader is waiting, possibly return early. | ||
| do { | ||
| read = underlyingInputStream.read(arr, off, len); | ||
| if (read <= 0) break; | ||
| off += read; | ||
| len -= read; | ||
| } while (len > 0 && !isWaiting.get()); | ||
| } catch (Throwable ex) { | ||
| exception = ex; | ||
| if (ex instanceof Error) { | ||
|
|
@@ -181,13 +179,12 @@ public void run() { | |
| } | ||
| } finally { | ||
| stateChangeLock.lock(); | ||
| readAheadBuffer.limit(off); | ||
| if (read < 0 || (exception instanceof EOFException)) { | ||
| endOfStream = true; | ||
| } else if (exception != null) { | ||
| readAborted = true; | ||
| readException = exception; | ||
| } else { | ||
| readAheadBuffer.limit(read); | ||
| } | ||
| readInProgress = false; | ||
| signalAsyncReadComplete(); | ||
|
|
@@ -230,7 +227,10 @@ private void signalAsyncReadComplete() { | |
|
|
||
| private void waitForAsyncReadComplete() throws IOException { | ||
| stateChangeLock.lock(); | ||
| isWaiting.set(true); | ||
| try { | ||
| // There is only one reader, and one writer, so the writer should signal only once, | ||
| // but a while loop checking the wake up condition is still needed to avoid spurious wakeups. | ||
| while (readInProgress) { | ||
| asyncReadComplete.await(); | ||
| } | ||
|
|
@@ -239,15 +239,21 @@ private void waitForAsyncReadComplete() throws IOException { | |
| iio.initCause(e); | ||
| throw iio; | ||
| } finally { | ||
| isWaiting.set(false); | ||
| stateChangeLock.unlock(); | ||
| } | ||
| checkReadException(); | ||
| } | ||
|
|
||
| @Override | ||
| public int read() throws IOException { | ||
| byte[] oneByteArray = oneByte.get(); | ||
| return read(oneByteArray, 0, 1) == -1 ? -1 : oneByteArray[0] & 0xFF; | ||
| if (activeBuffer.hasRemaining()) { | ||
| // short path - just get one byte. | ||
| return activeBuffer.get() & 0xFF; | ||
| } else { | ||
| byte[] oneByteArray = oneByte.get(); | ||
| return read(oneByteArray, 0, 1) == -1 ? -1 : oneByteArray[0] & 0xFF; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -258,54 +264,43 @@ public int read(byte[] b, int offset, int len) throws IOException { | |
| if (len == 0) { | ||
| return 0; | ||
| } | ||
| stateChangeLock.lock(); | ||
| try { | ||
| return readInternal(b, offset, len); | ||
| } finally { | ||
| stateChangeLock.unlock(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * flip the active and read ahead buffer | ||
| */ | ||
| private void swapBuffers() { | ||
| ByteBuffer temp = activeBuffer; | ||
| activeBuffer = readAheadBuffer; | ||
| readAheadBuffer = temp; | ||
| } | ||
|
|
||
| /** | ||
| * Internal read function which should be called only from read() api. The assumption is that | ||
| * the stateChangeLock is already acquired in the caller before calling this function. | ||
| */ | ||
| private int readInternal(byte[] b, int offset, int len) throws IOException { | ||
| assert (stateChangeLock.isLocked()); | ||
| if (!activeBuffer.hasRemaining()) { | ||
| waitForAsyncReadComplete(); | ||
| if (readAheadBuffer.hasRemaining()) { | ||
| swapBuffers(); | ||
| } else { | ||
| // The first read or activeBuffer is skipped. | ||
| readAsync(); | ||
| // No remaining in active buffer - lock and switch to write ahead buffer. | ||
| stateChangeLock.lock(); | ||
| try { | ||
| waitForAsyncReadComplete(); | ||
| if (isEndOfStream()) { | ||
| return -1; | ||
| if (!readAheadBuffer.hasRemaining()) { | ||
| // The first read. | ||
| readAsync(); | ||
| waitForAsyncReadComplete(); | ||
| if (isEndOfStream()) { | ||
| return -1; | ||
| } | ||
| } | ||
| // Swap the newly read read ahead buffer in place of empty active buffer. | ||
|
Member
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. Is it good to use
Contributor
Author
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. Other existing places in comments in the file use |
||
| swapBuffers(); | ||
| // After swapping buffers, trigger another async read for read ahead buffer. | ||
| readAsync(); | ||
| } finally { | ||
| stateChangeLock.unlock(); | ||
| } | ||
| } else { | ||
| checkReadException(); | ||
| } | ||
| len = Math.min(len, activeBuffer.remaining()); | ||
| activeBuffer.get(b, offset, len); | ||
|
|
||
| if (activeBuffer.remaining() <= readAheadThresholdInBytes && !readAheadBuffer.hasRemaining()) { | ||
| readAsync(); | ||
| } | ||
| return len; | ||
| } | ||
|
|
||
| /** | ||
| * flip the active and read ahead buffer | ||
| */ | ||
| private void swapBuffers() { | ||
| ByteBuffer temp = activeBuffer; | ||
| activeBuffer = readAheadBuffer; | ||
| readAheadBuffer = temp; | ||
| } | ||
|
|
||
| @Override | ||
| public int available() throws IOException { | ||
| stateChangeLock.lock(); | ||
|
|
@@ -323,6 +318,11 @@ public long skip(long n) throws IOException { | |
| if (n <= 0L) { | ||
| return 0L; | ||
| } | ||
| if (n <= activeBuffer.remaining()) { | ||
| // Only skipping from active buffer is sufficient | ||
| activeBuffer.position((int) n + activeBuffer.position()); | ||
| return n; | ||
| } | ||
| stateChangeLock.lock(); | ||
| long skipped; | ||
| try { | ||
|
|
@@ -346,21 +346,14 @@ private long skipInternal(long n) throws IOException { | |
| if (available() >= n) { | ||
| // we can skip from the internal buffers | ||
| int toSkip = (int) n; | ||
| if (toSkip <= activeBuffer.remaining()) { | ||
| // Only skipping from active buffer is sufficient | ||
| activeBuffer.position(toSkip + activeBuffer.position()); | ||
| if (activeBuffer.remaining() <= readAheadThresholdInBytes | ||
| && !readAheadBuffer.hasRemaining()) { | ||
| readAsync(); | ||
| } | ||
| return n; | ||
| } | ||
| // We need to skip from both active buffer and read ahead buffer | ||
| toSkip -= activeBuffer.remaining(); | ||
| assert(toSkip > 0); // skipping from activeBuffer already handled. | ||
| activeBuffer.position(0); | ||
| activeBuffer.flip(); | ||
| readAheadBuffer.position(toSkip + readAheadBuffer.position()); | ||
| swapBuffers(); | ||
| // Trigger async read to emptied read ahead buffer. | ||
| readAsync(); | ||
| return n; | ||
| } else { | ||
|
|
||
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
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.
You can just use
volatilehereThere 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.
I'll leave it be - should compile to basically the same, and with using
AtomicBooleanthe intent seems more readable to me.