Skip to content

Apache DataFusion SQL Query Engine

Fix internal error in sort when hitting memory limit (#15692)

Jannik Steinmann33f7439c7619
diff --git a/datafusion/physical-plan/src/sorts/sort.rs b/datafusion/physical-plan/src/sorts/sort.rs
index 5cc7512f88..8c0c6a7e8e 100644
--- a/datafusion/physical-plan/src/sorts/sort.rs
+++ b/datafusion/physical-plan/src/sorts/sort.rs
@@ -51,7 +51,7 @@ use arrow::compute::{concat_batches, lexsort_to_indices, take_arrays, SortColumn
use arrow::datatypes::{DataType, SchemaRef};
use arrow::row::{RowConverter, Rows, SortField};
use datafusion_common::{
- exec_datafusion_err, internal_datafusion_err, internal_err, Result,
+ exec_datafusion_err, internal_datafusion_err, internal_err, DataFusionError, Result,
};
use datafusion_execution::disk_manager::RefCountedTempFile;
use datafusion_execution::memory_pool::{MemoryConsumer, MemoryReservation};
@@ -323,13 +323,8 @@ impl ExternalSorter {
}
self.reserve_memory_for_merge()?;
-
- let size = get_reserved_byte_for_record_batch(&input);
- if self.reservation.try_grow(size).is_err() {
- self.sort_and_spill_in_mem_batches().await?;
- // After spilling all in-memory batches, the retry should succeed
- self.reservation.try_grow(size)?;
- }
+ self.reserve_memory_for_batch_and_maybe_spill(&input)
+ .await?;
self.in_mem_batches.push(input);
Ok(())
@@ -529,6 +524,12 @@ impl ExternalSorter {
/// Sorts the in-memory batches and merges them into a single sorted run, then writes
/// the result to spill files.
async fn sort_and_spill_in_mem_batches(&mut self) -> Result<()> {
+ if self.in_mem_batches.is_empty() {
+ return internal_err!(
+ "in_mem_batches must not be empty when attempting to sort and spill"
+ );
+ }
+
// Release the memory reserved for merge back to the pool so
// there is some left when `in_mem_sort_stream` requests an
// allocation. At the end of this function, memory will be
@@ -678,7 +679,8 @@ impl ExternalSorter {
let batch = concat_batches(&self.schema, &self.in_mem_batches)?;
self.in_mem_batches.clear();
self.reservation
- .try_resize(get_reserved_byte_for_record_batch(&batch))?;
+ .try_resize(get_reserved_byte_for_record_batch(&batch))
+ .map_err(Self::err_with_oom_context)?;
let reservation = self.reservation.take();
return self.sort_batch_stream(batch, metrics, reservation);
}
@@ -759,12 +761,51 @@ impl ExternalSorter {
if self.runtime.disk_manager.tmp_files_enabled() {
let size = self.sort_spill_reservation_bytes;
if self.merge_reservation.size() != size {
- self.merge_reservation.try_resize(size)?;
+ self.merge_reservation
+ .try_resize(size)
+ .map_err(Self::err_with_oom_context)?;
}
}
Ok(())
}
+
+ /// Reserves memory to be able to accommodate the given batch.
+ /// If memory is scarce, tries to spill current in-memory batches to disk first.
+ async fn reserve_memory_for_batch_and_maybe_spill(
+ &mut self,
+ input: &RecordBatch,
+ ) -> 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));
+ }
+
+ // Spill and try again.
+ self.sort_and_spill_in_mem_batches().await?;
+ self.reservation
+ .try_grow(size)
+ .map_err(Self::err_with_oom_context)
+ }
+ }
+ }
+
+ /// Wraps the error with a context message suggesting settings to tweak.
+ /// This is meant to be used with DataFusionError::ResourcesExhausted only.
+ fn err_with_oom_context(e: DataFusionError) -> DataFusionError {
+ match e {
+ DataFusionError::ResourcesExhausted(_) => e.context(
+ "Not enough memory to continue external sort. \
+ Consider increasing the memory limit, or decreasing sort_spill_reservation_bytes"
+ ),
+ // This is not an OOM error, so just return it as is.
+ _ => e,
+ }
+ }
}
/// Estimate how much memory is needed to sort a `RecordBatch`.
@@ -1327,7 +1368,7 @@ mod tests {
use arrow::datatypes::*;
use datafusion_common::cast::as_primitive_array;
use datafusion_common::test_util::batches_to_string;
- use datafusion_common::{Result, ScalarValue};
+ use datafusion_common::{DataFusionError, Result, ScalarValue};
use datafusion_execution::config::SessionConfig;
use datafusion_execution::runtime_env::RuntimeEnvBuilder;
use datafusion_execution::RecordBatchStream;
@@ -1552,6 +1593,69 @@ mod tests {
Ok(())
}
+ #[tokio::test]
+ async fn test_batch_reservation_error() -> Result<()> {
+ // Pick a memory limit and sort_spill_reservation that make the first batch reservation fail.
+ // These values assume that the ExternalSorter will reserve 800 bytes for the first batch.
+ let expected_batch_reservation = 800;
+ let merge_reservation: usize = 0; // Set to 0 for simplicity
+ let memory_limit: usize = expected_batch_reservation + merge_reservation - 1; // Just short of what we need
+
+ let session_config =
+ SessionConfig::new().with_sort_spill_reservation_bytes(merge_reservation);
+ let runtime = RuntimeEnvBuilder::new()
+ .with_memory_limit(memory_limit, 1.0)
+ .build_arc()?;
+ let task_ctx = Arc::new(
+ TaskContext::default()
+ .with_session_config(session_config)
+ .with_runtime(runtime),
+ );
+
+ let plan = test::scan_partitioned(1);
+
+ // Read the first record batch to assert that our memory limit and sort_spill_reservation
+ // settings trigger the test scenario.
+ {
+ let mut stream = plan.execute(0, Arc::clone(&task_ctx))?;
+ let first_batch = stream.next().await.unwrap()?;
+ let batch_reservation = get_reserved_byte_for_record_batch(&first_batch);
+
+ assert_eq!(batch_reservation, expected_batch_reservation);
+ assert!(memory_limit < (merge_reservation + batch_reservation));
+ }
+
+ let sort_exec = Arc::new(SortExec::new(
+ LexOrdering::new(vec![PhysicalSortExpr {
+ expr: col("i", &plan.schema())?,
+ options: SortOptions::default(),
+ }]),
+ plan,
+ ));
+
+ let result = collect(
+ Arc::clone(&sort_exec) as Arc<dyn ExecutionPlan>,
+ Arc::clone(&task_ctx),
+ )
+ .await;
+
+ let err = result.unwrap_err();
+ assert!(
+ matches!(err, DataFusionError::Context(..)),
+ "Assertion failed: expected a Context error, but got: {:?}",
+ err
+ );
+
+ // Assert that the context error is wrapping a resources exhausted error.
+ assert!(
+ matches!(err.find_root(), DataFusionError::ResourcesExhausted(_)),
+ "Assertion failed: expected a ResourcesExhausted error, but got: {:?}",
+ err
+ );
+
+ Ok(())
+ }
+
#[tokio::test]
async fn test_sort_spill_utf8_strings() -> Result<()> {
let session_config = SessionConfig::new()