Skip to content
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

Return ResourceExhausted errors when memory limit is exceed in GroupedHashAggregateStreamV2 (Row Hash) #4202

Merged
merged 4 commits into from
Nov 18, 2022
Merged
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
8 changes: 3 additions & 5 deletions datafusion/core/src/execution/memory_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,8 @@ pub trait MemoryConsumer: Send + Sync {
self.id(),
);

let can_grow_directly = self
.memory_manager()
.can_grow_directly(required, current)
.await;
let can_grow_directly =
self.memory_manager().can_grow_directly(required, current);
if !can_grow_directly {
debug!(
"Failed to grow memory of {} directly from consumer {}, spilling first ...",
Expand Down Expand Up @@ -334,7 +332,7 @@ impl MemoryManager {
}

/// Grow memory attempt from a consumer, return if we could grant that much to it
async fn can_grow_directly(&self, required: usize, current: usize) -> bool {
fn can_grow_directly(&self, required: usize, current: usize) -> bool {
let num_rqt = self.requesters.lock().len();
let mut rqt_current_used = self.requesters_total.lock();
let mut rqt_max = self.max_mem_for_requesters();
Expand Down
66 changes: 63 additions & 3 deletions datafusion/core/src/physical_plan/aggregates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ impl ExecutionPlan for AggregateExec {
context: Arc<TaskContext>,
) -> Result<SendableRecordBatchStream> {
let batch_size = context.session_config().batch_size();
let input = self.input.execute(partition, context)?;
let input = self.input.execute(partition, Arc::clone(&context))?;

let baseline_metrics = BaselineMetrics::new(&self.metrics, partition);

Expand All @@ -369,6 +369,8 @@ impl ExecutionPlan for AggregateExec {
input,
baseline_metrics,
batch_size,
context,
partition,
)?))
} else {
Ok(Box::pin(GroupedHashAggregateStream::new(
Expand Down Expand Up @@ -689,7 +691,8 @@ fn evaluate_group_by(

#[cfg(test)]
mod tests {
use crate::execution::context::TaskContext;
use crate::execution::context::{SessionConfig, TaskContext};
use crate::execution::runtime_env::{RuntimeConfig, RuntimeEnv};
use crate::from_slice::FromSlice;
use crate::physical_plan::aggregates::{
AggregateExec, AggregateMode, PhysicalGroupBy,
Expand All @@ -700,7 +703,7 @@ mod tests {
use crate::{assert_batches_sorted_eq, physical_plan::common};
use arrow::array::{Float64Array, UInt32Array};
use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
use arrow::error::Result as ArrowResult;
use arrow::error::{ArrowError, Result as ArrowResult};
use arrow::record_batch::RecordBatch;
use datafusion_common::{DataFusionError, Result, ScalarValue};
use datafusion_physical_expr::expressions::{lit, Count};
Expand Down Expand Up @@ -1081,6 +1084,63 @@ mod tests {
check_grouping_sets(input).await
}

#[tokio::test]
async fn test_oom() -> Result<()> {
let input: Arc<dyn ExecutionPlan> =
Arc::new(TestYieldingExec { yield_first: true });
let input_schema = input.schema();

let session_ctx = SessionContext::with_config_rt(
SessionConfig::default(),
Arc::new(
RuntimeEnv::new(RuntimeConfig::default().with_memory_limit(1, 1.0))
.unwrap(),
),
);
let task_ctx = session_ctx.task_ctx();

let groups = PhysicalGroupBy {
expr: vec![(col("a", &input_schema)?, "a".to_string())],
null_expr: vec![],
groups: vec![vec![false]],
};

let aggregates: Vec<Arc<dyn AggregateExpr>> = vec![Arc::new(Avg::new(
col("b", &input_schema)?,
"AVG(b)".to_string(),
DataType::Float64,
))];

let partial_aggregate = Arc::new(AggregateExec::try_new(
AggregateMode::Partial,
groups,
aggregates,
input,
input_schema.clone(),
)?);

let err = common::collect(partial_aggregate.execute(0, task_ctx.clone())?)
.await
.unwrap_err();

// error root cause traversal is a bit complicated, see #4172.
if let DataFusionError::ArrowError(ArrowError::ExternalError(err)) = err {
if let Some(err) = err.downcast_ref::<DataFusionError>() {
assert!(
matches!(err, DataFusionError::ResourcesExhausted(_)),
"Wrong inner error type: {}",
err,
);
} else {
panic!("Wrong arrow error type: {err}")
}
} else {
panic!("Wrong outer error type: {err}")
}

Ok(())
}

#[tokio::test]
async fn test_drop_cancel_without_groups() -> Result<()> {
let session_ctx = SessionContext::new();
Expand Down
Loading