-
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
fix: incorrect memory usage track for sort #2135
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 |
---|---|---|
|
@@ -195,6 +195,12 @@ pub trait MemoryConsumer: Send + Sync { | |
Ok(()) | ||
} | ||
|
||
/// Return `freed` memory to the memory manager, | ||
/// may wake up other requesters waiting for their minimum memory quota. | ||
fn shrink(&self, freed: usize) { | ||
self.memory_manager().record_free(freed); | ||
} | ||
|
||
/// Spill in-memory buffers to disk, free memory, return the previous used | ||
async fn spill(&self) -> Result<usize>; | ||
|
||
|
@@ -303,7 +309,8 @@ impl MemoryManager { | |
)); | ||
} | ||
|
||
fn get_requester_total(&self) -> usize { | ||
/// Return the total memory usage for all requesters | ||
pub fn get_requester_total(&self) -> usize { | ||
*self.requesters_total.lock() | ||
} | ||
|
||
|
@@ -342,8 +349,8 @@ impl MemoryManager { | |
// if we cannot acquire at lease 1/2n memory, just wait for others | ||
// to spill instead spill self frequently with limited total mem | ||
debug!( | ||
"Cannot acquire minimum amount of memory {} on memory manager {}, waiting for others to spill ...", | ||
human_readable_size(min_per_rqt), self); | ||
"Cannot acquire a minimum amount of {} memory from the manager of total {}, waiting for others to spill ...", | ||
human_readable_size(min_per_rqt), human_readable_size(self.pool_size)); | ||
let now = Instant::now(); | ||
self.cv.wait(&mut rqt_current_used); | ||
let elapsed = now.elapsed(); | ||
|
@@ -361,12 +368,30 @@ impl MemoryManager { | |
granted | ||
} | ||
|
||
fn record_free_then_acquire(&self, freed: usize, acquired: usize) -> usize { | ||
fn record_free_then_acquire(&self, freed: usize, acquired: usize) { | ||
let mut requesters_total = self.requesters_total.lock(); | ||
debug!( | ||
"free_then_acquire: total {}, freed {}, acquired {}", | ||
human_readable_size(*requesters_total), | ||
human_readable_size(freed), | ||
human_readable_size(acquired) | ||
); | ||
assert!(*requesters_total >= freed); | ||
*requesters_total -= freed; | ||
*requesters_total += acquired; | ||
self.cv.notify_all() | ||
self.cv.notify_all(); | ||
} | ||
|
||
fn record_free(&self, freed: usize) { | ||
let mut requesters_total = self.requesters_total.lock(); | ||
debug!( | ||
"free: total {}, freed {}", | ||
human_readable_size(*requesters_total), | ||
human_readable_size(freed) | ||
); | ||
assert!(*requesters_total >= freed); | ||
*requesters_total -= freed; | ||
self.cv.notify_all(); | ||
} | ||
|
||
/// Drop a memory consumer and reclaim the memory | ||
|
@@ -378,6 +403,8 @@ impl MemoryManager { | |
let mut total = self.requesters_total.lock(); | ||
assert!(*total >= mem_used); | ||
*total -= mem_used; | ||
self.cv.notify_all(); | ||
return; | ||
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. The third fix: stop releasing memory twice for requesters. |
||
} | ||
} | ||
self.shrink_tracker_usage(mem_used); | ||
|
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 |
---|---|---|
|
@@ -138,7 +138,7 @@ impl ExternalSorter { | |
&self.expr, | ||
tracking_metrics, | ||
)?; | ||
let prev_used = self.metrics.mem_used().set(0); | ||
let prev_used = self.free_all_memory(); | ||
streams.push(SortedStream::new(in_mem_stream, prev_used)); | ||
} | ||
|
||
|
@@ -169,13 +169,19 @@ impl ExternalSorter { | |
tracking_metrics, | ||
); | ||
// Report to the memory manager we are no longer using memory | ||
self.metrics.mem_used().set(0); | ||
self.free_all_memory(); | ||
result | ||
} else { | ||
Ok(Box::pin(EmptyRecordBatchStream::new(self.schema.clone()))) | ||
} | ||
} | ||
|
||
fn free_all_memory(&self) -> usize { | ||
let used = self.metrics.mem_used().set(0); | ||
self.shrink(used); | ||
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. The first fix, return all memory to the manager. |
||
used | ||
} | ||
|
||
fn used(&self) -> usize { | ||
self.metrics.mem_used().value() | ||
} | ||
|
@@ -678,6 +684,15 @@ mod tests { | |
assert_eq!(c7.value(0), 15); | ||
assert_eq!(c7.value(c7.len() - 1), 254,); | ||
|
||
assert_eq!( | ||
session_ctx | ||
.runtime_env() | ||
.memory_manager | ||
.get_requester_total(), | ||
0, | ||
"The sort should have returned all memory used back to the memory manager" | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
|
@@ -755,6 +770,15 @@ mod tests { | |
assert_eq!(c7.value(0), 15); | ||
assert_eq!(c7.value(c7.len() - 1), 254,); | ||
|
||
assert_eq!( | ||
session_ctx | ||
.runtime_env() | ||
.memory_manager | ||
.get_requester_total(), | ||
0, | ||
"The sort should have returned all memory used back to the memory manager" | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
|
@@ -941,6 +965,15 @@ mod tests { | |
drop(fut); | ||
assert_strong_count_converges_to_zero(refs).await; | ||
|
||
assert_eq!( | ||
session_ctx | ||
.runtime_env() | ||
.memory_manager | ||
.get_requester_total(), | ||
0, | ||
"The sort should have returned all memory used back to the memory manager" | ||
); | ||
|
||
Ok(()) | ||
} | ||
} |
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
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.
The second fix: stop calling std::fmt::Display on memory manager since it's acquiring a lock we are already holding.