Skip to content

Commit c0d0b99

Browse files
authored
feat: track query memory pool (#7219)
Signed-off-by: jeremyhi <[email protected]>
1 parent 7d575d1 commit c0d0b99

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/query/src/query_engine/state.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use std::collections::HashMap;
1616
use std::fmt;
17+
use std::num::NonZeroUsize;
1718
use std::sync::{Arc, RwLock};
1819

1920
use async_trait::async_trait;
@@ -34,6 +35,7 @@ use datafusion::execution::SessionStateBuilder;
3435
use datafusion::execution::context::{QueryPlanner, SessionConfig, SessionContext, SessionState};
3536
use datafusion::execution::memory_pool::{
3637
GreedyMemoryPool, MemoryConsumer, MemoryLimit, MemoryPool, MemoryReservation,
38+
TrackConsumersPool,
3739
};
3840
use datafusion::execution::runtime_env::{RuntimeEnv, RuntimeEnvBuilder};
3941
use datafusion::physical_optimizer::PhysicalOptimizerRule;
@@ -437,19 +439,25 @@ impl DfQueryPlanner {
437439
}
438440
}
439441

440-
/// A wrapper around GreedyMemoryPool that records metrics.
442+
/// A wrapper around TrackConsumersPool that records metrics.
441443
///
442444
/// This wrapper intercepts all memory pool operations and updates
443445
/// Prometheus metrics for monitoring query memory usage and rejections.
444446
#[derive(Debug)]
445447
struct MetricsMemoryPool {
446-
inner: Arc<GreedyMemoryPool>,
448+
inner: Arc<TrackConsumersPool<GreedyMemoryPool>>,
447449
}
448450

449451
impl MetricsMemoryPool {
452+
// Number of top memory consumers to report in OOM error messages
453+
const TOP_CONSUMERS_TO_REPORT: usize = 5;
454+
450455
fn new(limit: usize) -> Self {
451456
Self {
452-
inner: Arc::new(GreedyMemoryPool::new(limit)),
457+
inner: Arc::new(TrackConsumersPool::new(
458+
GreedyMemoryPool::new(limit),
459+
NonZeroUsize::new(Self::TOP_CONSUMERS_TO_REPORT).unwrap(),
460+
)),
453461
}
454462
}
455463

0 commit comments

Comments
 (0)