Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions datafusion/physical-plan/src/sorts/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,18 +782,17 @@ impl ExternalSorter {
) -> Result<()> {
let size = get_reserved_byte_for_record_batch(input);

match self.reservation.try_grow(size) {
Ok(_) => Ok(()),
Err(e) => {
if self.in_mem_batches.is_empty() {
return Err(Self::err_with_oom_context(e));
}
loop {
match self.reservation.try_grow(size) {
Ok(_) => return Ok(()),
Err(e) => {
if self.in_mem_batches.is_empty() {
return Err(Self::err_with_oom_context(e));
}

// Spill and try again.
self.sort_and_spill_in_mem_batches().await?;
self.reservation
.try_grow(size)
.map_err(Self::err_with_oom_context)
// Spill and try again.
self.sort_and_spill_in_mem_batches().await?;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion datafusion/physical-plan/src/sorts/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl RowCursorStream {

// track the memory in the newly created Rows.
let mut rows_reservation = self.reservation.new_empty();
rows_reservation.try_grow(rows.size())?;
rows_reservation.grow(rows.size());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ?
new_empty() returns a MemoryReservation with size=0 but the registration is shared (Arc::clone()), so the pool may not have enough space

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, however, if an error is thrown here when memory is rapidly occupied elsewhere, it will cause the entire system to fail. Generally speaking, physical memory is larger than the memory pool. If no error is thrown here, the components that support spill-to-disk will detect the memory pool overflow and thus spill data to disk, preventing unlimited memory growth. Therefore, this logic seems acceptable.

Ok(RowValues::new(rows, rows_reservation))
}
}
Expand Down