Skip to content

Track peak_mem_used in ExternalSorter #16192

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
16 changes: 13 additions & 3 deletions datafusion/physical-plan/src/sorts/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ use crate::execution_plan::{Boundedness, CardinalityEffect, EmissionType};
use crate::expressions::PhysicalSortExpr;
use crate::limit::LimitStream;
use crate::metrics::{
BaselineMetrics, ExecutionPlanMetricsSet, MetricsSet, SpillMetrics,
self, BaselineMetrics, ExecutionPlanMetricsSet, MetricBuilder, MetricsSet,
SpillMetrics,
};
use crate::projection::{make_with_child, update_expr, ProjectionExec};
use crate::sorts::streaming_merge::StreamingMergeBuilder;
Expand Down Expand Up @@ -63,13 +64,17 @@ struct ExternalSorterMetrics {
baseline: BaselineMetrics,

spill_metrics: SpillMetrics,
/// Peak memory used for buffered data.
/// Calculated as sum of peak memory values across partitions
peak_mem_used: metrics::Gauge,
}

impl ExternalSorterMetrics {
fn new(metrics: &ExecutionPlanMetricsSet, partition: usize) -> Self {
Self {
baseline: BaselineMetrics::new(metrics, partition),
spill_metrics: SpillMetrics::new(metrics, partition),
peak_mem_used: MetricBuilder::new(metrics).gauge("peak_mem_used", partition),
}
}
}
Expand Down Expand Up @@ -538,6 +543,7 @@ impl ExternalSorter {
self.consume_and_spill_append(&mut globally_sorted_batches)
.await?; // reservation is freed in spill()
} else {
self.metrics.peak_mem_used.set_max(self.used());
globally_sorted_batches.push(batch);
}
}
Expand Down Expand Up @@ -658,6 +664,8 @@ impl ExternalSorter {
self.reservation
.try_resize(get_reserved_byte_for_record_batch(&batch))
.map_err(Self::err_with_oom_context)?;
// TODO(ding-young) can reservation grow here?
Copy link
Contributor

Choose a reason for hiding this comment

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

I think so. After concatenating small batches into a single large batch, it will allocate new buffers and copy payloads into it, so the size might change.

self.metrics.peak_mem_used.set_max(self.used());
let reservation = self.reservation.take();
return self.sort_batch_stream(batch, metrics, reservation);
}
Expand Down Expand Up @@ -743,7 +751,7 @@ impl ExternalSorter {
) -> Result<()> {
let size = get_reserved_byte_for_record_batch(input);

match self.reservation.try_grow(size) {
let result = match self.reservation.try_grow(size) {
Ok(_) => Ok(()),
Err(e) => {
if self.in_mem_batches.is_empty() {
Expand All @@ -756,7 +764,9 @@ impl ExternalSorter {
.try_grow(size)
.map_err(Self::err_with_oom_context)
}
}
};
self.metrics.peak_mem_used.set_max(self.used());
result
}

/// Wraps the error with a context message suggesting settings to tweak.
Expand Down