-
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
Changes from 9 commits
987f15c
b26ffce
ca45a88
eaa6b4e
7238181
d6d44fc
5273176
62cefcd
1b3e970
52f4a7c
b6852aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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,24 +227,32 @@ private void signalAsyncReadComplete() { | |
|
|
||
| private void waitForAsyncReadComplete() throws IOException { | ||
| stateChangeLock.lock(); | ||
| isWaiting.set(true); | ||
| try { | ||
| while (readInProgress) { | ||
| if (readInProgress) { | ||
|
||
| asyncReadComplete.await(); | ||
| } | ||
| assert(!readInProgress); | ||
| } catch (InterruptedException e) { | ||
| InterruptedIOException iio = new InterruptedIOException(e.getMessage()); | ||
| 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 +263,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. | ||
|
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 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 +317,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 +345,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 { | ||
|
|
||
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.