Skip to content

Apache DataFusion SQL Query Engine

feat: Track peak value in tracked consumer (#17327)

Zhen Wang1756692218d4
diff --git a/datafusion-cli/tests/cli_integration.rs b/datafusion-cli/tests/cli_integration.rs
index a77f702bc9..ba3f29be9a 100644
--- a/datafusion-cli/tests/cli_integration.rs
+++ b/datafusion-cli/tests/cli_integration.rs
@@ -219,8 +219,8 @@ fn test_cli_top_memory_consumers<'a>(
settings.set_snapshot_suffix(snapshot_name);
settings.add_filter(
- r"[^\s]+\#\d+\(can spill: (true|false)\) consumed .*?B",
- "Consumer(can spill: bool) consumed XB",
+ r"[^\s]+\#\d+\(can spill: (true|false)\) consumed .*?B, peak .*?B",
+ "Consumer(can spill: bool) consumed XB, peak XB",
);
settings.add_filter(
r"Error: Failed to allocate additional .*? for .*? with .*? already allocated for this reservation - .*? remain available for the total pool",
diff --git a/datafusion-cli/tests/snapshots/[email protected] b/datafusion-cli/tests/snapshots/[email protected]
index ed925a6f64..a49d7c9755 100644
--- a/datafusion-cli/tests/snapshots/[email protected]
+++ b/datafusion-cli/tests/snapshots/[email protected]
@@ -17,8 +17,8 @@ exit_code: 1
Error: Not enough memory to continue external sort. Consider increasing the memory limit, or decreasing sort_spill_reservation_bytes
caused by
Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as:
- Consumer(can spill: bool) consumed XB,
- Consumer(can spill: bool) consumed XB.
+ Consumer(can spill: bool) consumed XB, peak XB,
+ Consumer(can spill: bool) consumed XB, peak XB.
Error: Failed to allocate
----- stderr -----
diff --git a/datafusion-cli/tests/snapshots/cli_top_memory_consumers@top3_default.snap b/datafusion-cli/tests/snapshots/cli_top_memory_consumers@top3_default.snap
index f35e3b1171..7416f5cf6b 100644
--- a/datafusion-cli/tests/snapshots/cli_top_memory_consumers@top3_default.snap
+++ b/datafusion-cli/tests/snapshots/cli_top_memory_consumers@top3_default.snap
@@ -15,9 +15,9 @@ exit_code: 1
Error: Not enough memory to continue external sort. Consider increasing the memory limit, or decreasing sort_spill_reservation_bytes
caused by
Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as:
- Consumer(can spill: bool) consumed XB,
- Consumer(can spill: bool) consumed XB,
- Consumer(can spill: bool) consumed XB.
+ Consumer(can spill: bool) consumed XB, peak XB,
+ Consumer(can spill: bool) consumed XB, peak XB,
+ Consumer(can spill: bool) consumed XB, peak XB.
Error: Failed to allocate
----- stderr -----
diff --git a/datafusion-examples/examples/memory_pool_tracking.rs b/datafusion-examples/examples/memory_pool_tracking.rs
index 287a1d25c8..d5823b1173 100644
--- a/datafusion-examples/examples/memory_pool_tracking.rs
+++ b/datafusion-examples/examples/memory_pool_tracking.rs
@@ -110,11 +110,11 @@ async fn automatic_usage_example() -> datafusion::error::Result<()> {
Error: Not enough memory to continue external sort. Consider increasing the memory limit, or decreasing sort_spill_reservation_bytes
caused by
Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as:
- ExternalSorterMerge[3]#112(can spill: false) consumed 10.0 MB,
- ExternalSorterMerge[10]#147(can spill: false) consumed 10.0 MB,
- ExternalSorter[1]#93(can spill: true) consumed 69.0 KB,
- ExternalSorter[13]#155(can spill: true) consumed 67.6 KB,
- ExternalSorter[8]#140(can spill: true) consumed 67.2 KB.
+ ExternalSorterMerge[3]#112(can spill: false) consumed 10.0 MB, peak 10.0 MB,
+ ExternalSorterMerge[10]#147(can spill: false) consumed 10.0 MB, peak 10.0 MB,
+ ExternalSorter[1]#93(can spill: true) consumed 69.0 KB, peak 69.0 KB,
+ ExternalSorter[13]#155(can spill: true) consumed 67.6 KB, peak 67.6 KB,
+ ExternalSorter[8]#140(can spill: true) consumed 67.2 KB, peak 67.2 KB.
Error: Failed to allocate additional 10.0 MB for ExternalSorterMerge[0] with 0.0 B already allocated for this reservation - 7.1 MB remain available for the total pool
*/
}
diff --git a/datafusion/execution/src/memory_pool/pool.rs b/datafusion/execution/src/memory_pool/pool.rs
index 8ad61ba9f8..da456b7071 100644
--- a/datafusion/execution/src/memory_pool/pool.rs
+++ b/datafusion/execution/src/memory_pool/pool.rs
@@ -269,6 +269,7 @@ struct TrackedConsumer {
name: String,
can_spill: bool,
reserved: AtomicUsize,
+ peak: AtomicUsize,
}
impl TrackedConsumer {
@@ -277,10 +278,16 @@ impl TrackedConsumer {
self.reserved.load(Ordering::Relaxed)
}
+ /// Return the peak value
+ fn peak(&self) -> usize {
+ self.peak.load(Ordering::Relaxed)
+ }
+
/// Grows the tracked consumer's reserved size,
/// should be called after the pool has successfully performed the grow().
fn grow(&self, additional: usize) {
self.reserved.fetch_add(additional, Ordering::Relaxed);
+ self.peak.fetch_max(self.reserved(), Ordering::Relaxed);
}
/// Reduce the tracked consumer's reserved size,
@@ -379,6 +386,7 @@ impl<I: MemoryPool> TrackConsumersPool<I> {
*consumer_id,
tracked_consumer.name.to_owned(),
tracked_consumer.can_spill,
+ tracked_consumer.peak(),
),
tracked_consumer.reserved(),
)
@@ -388,10 +396,11 @@ impl<I: MemoryPool> TrackConsumersPool<I> {
consumers[0..std::cmp::min(top, consumers.len())]
.iter()
- .map(|((id, name, can_spill), size)| {
+ .map(|((id, name, can_spill, peak), size)| {
format!(
- " {name}#{id}(can spill: {can_spill}) consumed {}",
- human_readable_size(*size)
+ " {name}#{id}(can spill: {can_spill}) consumed {}, peak {}",
+ human_readable_size(*size),
+ human_readable_size(*peak),
)
})
.collect::<Vec<_>>()
@@ -411,6 +420,7 @@ impl<I: MemoryPool> MemoryPool for TrackConsumersPool<I> {
name: consumer.name().to_string(),
can_spill: consumer.can_spill(),
reserved: Default::default(),
+ peak: Default::default(),
},
);
@@ -581,7 +591,8 @@ mod tests {
// set r1=50, using grow and shrink
let mut r1 = MemoryConsumer::new("r1").register(&pool);
- r1.grow(70);
+ r1.grow(50);
+ r1.grow(20);
r1.shrink(20);
// set r2=15 using try_grow
@@ -609,9 +620,9 @@ mod tests {
let error = res.unwrap_err().strip_backtrace();
assert_snapshot!(error, @r"
Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as:
- r1#[ID](can spill: false) consumed 50.0 B,
- r3#[ID](can spill: false) consumed 20.0 B,
- r2#[ID](can spill: false) consumed 15.0 B.
+ r1#[ID](can spill: false) consumed 50.0 B, peak 70.0 B,
+ r3#[ID](can spill: false) consumed 20.0 B, peak 25.0 B,
+ r2#[ID](can spill: false) consumed 15.0 B, peak 15.0 B.
Error: Failed to allocate additional 150.0 B for r5 with 0.0 B already allocated for this reservation - 5.0 B remain available for the total pool
");
}
@@ -634,7 +645,7 @@ mod tests {
let error = res.unwrap_err().strip_backtrace();
assert_snapshot!(error, @r"
Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as:
- foo#[ID](can spill: false) consumed 0.0 B.
+ foo#[ID](can spill: false) consumed 0.0 B, peak 0.0 B.
Error: Failed to allocate additional 150.0 B for foo with 0.0 B already allocated for this reservation - 100.0 B remain available for the total pool
");
@@ -651,8 +662,8 @@ mod tests {
let error = res.unwrap_err().strip_backtrace();
assert_snapshot!(error, @r"
Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as:
- foo#[ID](can spill: false) consumed 10.0 B,
- foo#[ID](can spill: false) consumed 0.0 B.
+ foo#[ID](can spill: false) consumed 10.0 B, peak 10.0 B,
+ foo#[ID](can spill: false) consumed 0.0 B, peak 0.0 B.
Error: Failed to allocate additional 150.0 B for foo with 0.0 B already allocated for this reservation - 90.0 B remain available for the total pool
");
@@ -664,8 +675,8 @@ mod tests {
let error = res.unwrap_err().strip_backtrace();
assert_snapshot!(error, @r"
Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as:
- foo#[ID](can spill: false) consumed 20.0 B,
- foo#[ID](can spill: false) consumed 10.0 B.
+ foo#[ID](can spill: false) consumed 20.0 B, peak 20.0 B,
+ foo#[ID](can spill: false) consumed 10.0 B, peak 10.0 B.
Error: Failed to allocate additional 150.0 B for foo with 20.0 B already allocated for this reservation - 70.0 B remain available for the total pool
");
@@ -679,9 +690,9 @@ mod tests {
let error = res.unwrap_err().strip_backtrace();
assert_snapshot!(error, @r"
Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as:
- foo#[ID](can spill: false) consumed 20.0 B,
- foo#[ID](can spill: false) consumed 10.0 B,
- foo#[ID](can spill: true) consumed 0.0 B.
+ foo#[ID](can spill: false) consumed 20.0 B, peak 20.0 B,
+ foo#[ID](can spill: false) consumed 10.0 B, peak 10.0 B,
+ foo#[ID](can spill: true) consumed 0.0 B, peak 0.0 B.
Error: Failed to allocate additional 150.0 B for foo with 0.0 B already allocated for this reservation - 70.0 B remain available for the total pool
");
}
@@ -703,8 +714,8 @@ mod tests {
let error = res.unwrap_err().strip_backtrace();
allow_duplicates!(assert_snapshot!(error, @r"
Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as:
- r1#[ID](can spill: false) consumed 20.0 B,
- r0#[ID](can spill: false) consumed 10.0 B.
+ r1#[ID](can spill: false) consumed 20.0 B, peak 20.0 B,
+ r0#[ID](can spill: false) consumed 10.0 B, peak 10.0 B.
Error: Failed to allocate additional 150.0 B for r0 with 10.0 B already allocated for this reservation - 70.0 B remain available for the total pool
"));
@@ -716,7 +727,7 @@ mod tests {
let error = res.unwrap_err().strip_backtrace();
allow_duplicates!(assert_snapshot!(error, @r"
Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as:
- r0#[ID](can spill: false) consumed 10.0 B.
+ r0#[ID](can spill: false) consumed 10.0 B, peak 10.0 B.
Error: Failed to allocate additional 150.0 B for r0 with 10.0 B already allocated for this reservation - 90.0 B remain available for the total pool
"));
@@ -727,7 +738,7 @@ mod tests {
let error = res.unwrap_err().strip_backtrace();
allow_duplicates!(assert_snapshot!(error, @r"
Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as:
- r0#[ID](can spill: false) consumed 10.0 B.
+ r0#[ID](can spill: false) consumed 10.0 B, peak 10.0 B.
Error: Failed to allocate additional 150.0 B for r0 with 10.0 B already allocated for this reservation - 90.0 B remain available for the total pool
"));
@@ -738,7 +749,7 @@ mod tests {
let error = res.unwrap_err().strip_backtrace();
allow_duplicates!(assert_snapshot!(error, @r"
Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as:
- r0#[ID](can spill: false) consumed 10.0 B.
+ r0#[ID](can spill: false) consumed 10.0 B, peak 10.0 B.
Error: Failed to allocate additional 150.0 B for r0 with 10.0 B already allocated for this reservation - 90.0 B remain available for the total pool
"));
}
@@ -785,8 +796,8 @@ mod tests {
// Test: can get runtime metrics, even without an error thrown
let res = downcasted.report_top(2);
assert_snapshot!(res, @r"
- r3#[ID](can spill: false) consumed 45.0 B,
- r1#[ID](can spill: false) consumed 20.0 B.
+ r3#[ID](can spill: false) consumed 45.0 B, peak 45.0 B,
+ r1#[ID](can spill: false) consumed 20.0 B, peak 20.0 B.
");
}
}