-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
FileStream: Open next file in parallel while decoding #5161
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -22,6 +22,7 @@ | |||||||
//! compliant with the `SendableRecordBatchStream` trait. | ||||||||
|
||||||||
use std::collections::VecDeque; | ||||||||
use std::mem; | ||||||||
use std::pin::Pin; | ||||||||
use std::task::{Context, Poll}; | ||||||||
use std::time::Instant; | ||||||||
|
@@ -98,6 +99,11 @@ enum FileStreamState { | |||||||
partition_values: Vec<ScalarValue>, | ||||||||
/// The reader instance | ||||||||
reader: BoxStream<'static, Result<RecordBatch, ArrowError>>, | ||||||||
/// A [`FileOpenFuture`] for the next file to be processed, | ||||||||
/// and its corresponding partition column values, if any. | ||||||||
/// This allows the next file to be opened in parallel while the | ||||||||
/// current file is read. | ||||||||
next: Option<(FileOpenFuture, Vec<ScalarValue>)>, | ||||||||
}, | ||||||||
/// Encountered an error | ||||||||
Error, | ||||||||
|
@@ -202,30 +208,39 @@ impl<F: FileOpener> FileStream<F> { | |||||||
}) | ||||||||
} | ||||||||
|
||||||||
// Begin opening the next file in parallel while decoding the current file in FileStream. | ||||||||
// Since file opening is mostly IO (and may involve a | ||||||||
// bunch of sequential IO), it can be parallelized with decoding. | ||||||||
fn start_next_file(&mut self) -> Option<Result<(FileOpenFuture, Vec<ScalarValue>)>> { | ||||||||
let part_file = self.file_iter.pop_front()?; | ||||||||
|
||||||||
let file_meta = FileMeta { | ||||||||
object_meta: part_file.object_meta, | ||||||||
range: part_file.range, | ||||||||
extensions: part_file.extensions, | ||||||||
}; | ||||||||
|
||||||||
Some( | ||||||||
self.file_reader | ||||||||
.open(file_meta) | ||||||||
.map(|future| (future, part_file.partition_values)), | ||||||||
) | ||||||||
} | ||||||||
|
||||||||
fn poll_inner(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<RecordBatch>>> { | ||||||||
loop { | ||||||||
match &mut self.state { | ||||||||
FileStreamState::Idle => { | ||||||||
let part_file = match self.file_iter.pop_front() { | ||||||||
Some(file) => file, | ||||||||
None => return Poll::Ready(None), | ||||||||
}; | ||||||||
|
||||||||
let file_meta = FileMeta { | ||||||||
object_meta: part_file.object_meta, | ||||||||
range: part_file.range, | ||||||||
extensions: part_file.extensions, | ||||||||
}; | ||||||||
|
||||||||
self.file_stream_metrics.time_opening.start(); | ||||||||
|
||||||||
match self.file_reader.open(file_meta) { | ||||||||
Ok(future) => { | ||||||||
match self.start_next_file().transpose() { | ||||||||
Ok(Some((future, partition_values))) => { | ||||||||
self.state = FileStreamState::Open { | ||||||||
future, | ||||||||
partition_values: part_file.partition_values, | ||||||||
partition_values, | ||||||||
} | ||||||||
} | ||||||||
Ok(None) => return Poll::Ready(None), | ||||||||
Err(e) => { | ||||||||
self.state = FileStreamState::Error; | ||||||||
return Poll::Ready(Some(Err(e))); | ||||||||
|
@@ -237,13 +252,34 @@ impl<F: FileOpener> FileStream<F> { | |||||||
partition_values, | ||||||||
} => match ready!(future.poll_unpin(cx)) { | ||||||||
Ok(reader) => { | ||||||||
let partition_values = mem::take(partition_values); | ||||||||
|
||||||||
let next = self.start_next_file().transpose(); | ||||||||
|
||||||||
self.file_stream_metrics.time_opening.stop(); | ||||||||
self.file_stream_metrics.time_scanning_until_data.start(); | ||||||||
self.file_stream_metrics.time_scanning_total.start(); | ||||||||
self.state = FileStreamState::Scan { | ||||||||
partition_values: std::mem::take(partition_values), | ||||||||
reader, | ||||||||
}; | ||||||||
|
||||||||
match next { | ||||||||
Ok(Some((next_future, next_partition_values))) => { | ||||||||
self.state = FileStreamState::Scan { | ||||||||
partition_values, | ||||||||
reader, | ||||||||
next: Some((next_future, next_partition_values)), | ||||||||
}; | ||||||||
} | ||||||||
Ok(None) => { | ||||||||
self.state = FileStreamState::Scan { | ||||||||
reader, | ||||||||
partition_values, | ||||||||
next: None, | ||||||||
}; | ||||||||
} | ||||||||
Err(e) => { | ||||||||
self.state = FileStreamState::Error; | ||||||||
return Poll::Ready(Some(Err(e))); | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
Err(e) => { | ||||||||
self.state = FileStreamState::Error; | ||||||||
|
@@ -253,6 +289,7 @@ impl<F: FileOpener> FileStream<F> { | |||||||
FileStreamState::Scan { | ||||||||
reader, | ||||||||
partition_values, | ||||||||
next, | ||||||||
} => match ready!(reader.poll_next_unpin(cx)) { | ||||||||
Some(result) => { | ||||||||
self.file_stream_metrics.time_scanning_until_data.stop(); | ||||||||
|
@@ -287,7 +324,18 @@ impl<F: FileOpener> FileStream<F> { | |||||||
None => { | ||||||||
self.file_stream_metrics.time_scanning_until_data.stop(); | ||||||||
self.file_stream_metrics.time_scanning_total.stop(); | ||||||||
self.state = FileStreamState::Idle; | ||||||||
|
||||||||
match mem::take(next) { | ||||||||
Some((future, partition_values)) => { | ||||||||
self.file_stream_metrics.time_opening.start(); | ||||||||
|
||||||||
self.state = FileStreamState::Open { | ||||||||
future, | ||||||||
partition_values, | ||||||||
} | ||||||||
} | ||||||||
None => return Poll::Ready(None), | ||||||||
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.
Suggested change
|
||||||||
} | ||||||||
} | ||||||||
}, | ||||||||
FileStreamState::Error | FileStreamState::Limit => { | ||||||||
|
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.
I wonder if we could make it future-proof by potentially prefetching
n
files instead of 1? I guess in cases where file opening is slower than scanning / processing, this could make a difference (e.g. small files).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.
Perhaps a follow on PR could turn this into a stream and use
StreamExt::buffered
or somethingThere 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.
yeah, that seems like a good idea