-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Validate the memory consumption in SPM created by multi level merge #17029
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
Draft
ding-young
wants to merge
3
commits into
apache:main
Choose a base branch
from
ding-young:verify-mem-multi-level
base: main
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.
Draft
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -54,8 +54,13 @@ use futures::{FutureExt as _, Stream}; | |
struct SpillReaderStream { | ||
schema: SchemaRef, | ||
state: SpillReaderStreamState, | ||
/// how much memory the largest memory batch is taking | ||
pub max_record_batch_memory: Option<usize>, | ||
} | ||
|
||
// Small margin allowed to accommodate slight memory accounting variation | ||
const MEMORY_MARGIN: usize = 4096; | ||
|
||
/// When we poll for the next batch, we will get back both the batch and the reader, | ||
/// so we can call `next` again. | ||
type NextRecordBatchResult = Result<(StreamReader<BufReader<File>>, Option<RecordBatch>)>; | ||
|
@@ -76,10 +81,15 @@ enum SpillReaderStreamState { | |
} | ||
|
||
impl SpillReaderStream { | ||
fn new(schema: SchemaRef, spill_file: RefCountedTempFile) -> Self { | ||
fn new( | ||
schema: SchemaRef, | ||
spill_file: RefCountedTempFile, | ||
max_record_batch_memory: Option<usize>, | ||
) -> Self { | ||
Self { | ||
schema, | ||
state: SpillReaderStreamState::Uninitialized(spill_file), | ||
max_record_batch_memory, | ||
} | ||
} | ||
|
||
|
@@ -125,6 +135,27 @@ impl SpillReaderStream { | |
Ok((reader, batch)) => { | ||
match batch { | ||
Some(batch) => { | ||
if let Some(max_record_batch_memory) = | ||
self.max_record_batch_memory | ||
{ | ||
let actual_size = | ||
get_record_batch_memory_size(&batch); | ||
if actual_size | ||
> max_record_batch_memory + MEMORY_MARGIN | ||
{ | ||
return Poll::Ready(Some(Err( | ||
DataFusionError::ResourcesExhausted( | ||
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 can use |
||
format!( | ||
"Record batch memory usage ({actual_size} bytes) exceeds the expected limit ({max_record_batch_memory} bytes)\n | ||
by more than the allowed tolerance ({MEMORY_MARGIN} bytes).\n | ||
This likely indicates a bug in memory accounting during spilling.\n | ||
Please report this issue", | ||
) | ||
.to_owned(), | ||
), | ||
))); | ||
} | ||
} | ||
self.state = SpillReaderStreamState::Waiting(reader); | ||
|
||
Poll::Ready(Some(Ok(batch))) | ||
|
@@ -389,7 +420,7 @@ mod tests { | |
let spilled_rows = spill_manager.metrics.spilled_rows.value(); | ||
assert_eq!(spilled_rows, num_rows); | ||
|
||
let stream = spill_manager.read_spill_as_stream(spill_file)?; | ||
let stream = spill_manager.read_spill_as_stream(spill_file, None)?; | ||
assert_eq!(stream.schema(), schema); | ||
|
||
let batches = collect(stream).await?; | ||
|
@@ -453,7 +484,7 @@ mod tests { | |
let spilled_rows = spill_manager.metrics.spilled_rows.value(); | ||
assert_eq!(spilled_rows, num_rows); | ||
|
||
let stream = spill_manager.read_spill_as_stream(spill_file)?; | ||
let stream = spill_manager.read_spill_as_stream(spill_file, None)?; | ||
assert_eq!(stream.schema(), dict_schema); | ||
let batches = collect(stream).await?; | ||
assert_eq!(batches.len(), 2); | ||
|
@@ -479,7 +510,7 @@ mod tests { | |
.unwrap(); | ||
assert!(spill_file.path().exists()); | ||
|
||
let stream = spill_manager.read_spill_as_stream(spill_file)?; | ||
let stream = spill_manager.read_spill_as_stream(spill_file, None)?; | ||
assert_eq!(stream.schema(), schema); | ||
|
||
let batches = collect(stream).await?; | ||
|
@@ -514,7 +545,7 @@ mod tests { | |
let spilled_rows = spill_manager.metrics.spilled_rows.value(); | ||
assert_eq!(spilled_rows, num_rows); | ||
|
||
let stream = spill_manager.read_spill_as_stream(spill_file)?; | ||
let stream = spill_manager.read_spill_as_stream(spill_file, None)?; | ||
assert_eq!(stream.schema(), schema); | ||
|
||
let batches = collect(stream).await?; | ||
|
@@ -894,8 +925,10 @@ mod tests { | |
.spill_record_batch_and_finish(&batches, "Test2")? | ||
.unwrap(); | ||
|
||
let mut stream_1 = spill_manager.read_spill_as_stream(spill_file_1)?; | ||
let mut stream_2 = spill_manager.read_spill_as_stream(spill_file_2)?; | ||
let mut stream_1 = | ||
spill_manager.read_spill_as_stream(spill_file_1, None)?; | ||
let mut stream_2 = | ||
spill_manager.read_spill_as_stream(spill_file_2, None)?; | ||
stream_1.next().await; | ||
stream_2.next().await; | ||
|
||
|
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.
I recommend to also explain: this is used for validation, and link to the multi-level merge's doc for the background.