Skip to content

Commit 5d98d2c

Browse files
Rachelintwaynexia
andauthored
Add MemoryPool::memory_limit to expose setting memory usage limit (#15828)
* add `memory_limit` to `MemoryPool`, and impl it for the pools in datafusion. * Update datafusion/execution/src/memory_pool/mod.rs Co-authored-by: Ruihang Xia <waynestxia@gmail.com> --------- Co-authored-by: Ruihang Xia <waynestxia@gmail.com>
1 parent 07a310f commit 5d98d2c

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

datafusion/execution/src/memory_pool/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,25 @@ pub trait MemoryPool: Send + Sync + std::fmt::Debug {
141141

142142
/// Return the total amount of memory reserved
143143
fn reserved(&self) -> usize;
144+
145+
/// Return the memory limit of the pool
146+
///
147+
/// The default implementation of `MemoryPool::memory_limit`
148+
/// will return `MemoryLimit::Unknown`.
149+
/// If you are using your custom memory pool, but have the requirement to
150+
/// know the memory usage limit of the pool, please implement this method
151+
/// to return it(`Memory::Finite(limit)`).
152+
fn memory_limit(&self) -> MemoryLimit {
153+
MemoryLimit::Unknown
154+
}
155+
}
156+
157+
/// Memory limit of `MemoryPool`
158+
pub enum MemoryLimit {
159+
Infinite,
160+
/// Bounded memory limit in bytes.
161+
Finite(usize),
162+
Unknown,
144163
}
145164

146165
/// A memory consumer is a named allocation traced by a particular

datafusion/execution/src/memory_pool/pool.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use crate::memory_pool::{MemoryConsumer, MemoryPool, MemoryReservation};
18+
use crate::memory_pool::{MemoryConsumer, MemoryLimit, MemoryPool, MemoryReservation};
1919
use datafusion_common::HashMap;
2020
use datafusion_common::{resources_datafusion_err, DataFusionError, Result};
2121
use log::debug;
@@ -48,6 +48,10 @@ impl MemoryPool for UnboundedMemoryPool {
4848
fn reserved(&self) -> usize {
4949
self.used.load(Ordering::Relaxed)
5050
}
51+
52+
fn memory_limit(&self) -> MemoryLimit {
53+
MemoryLimit::Infinite
54+
}
5155
}
5256

5357
/// A [`MemoryPool`] that implements a greedy first-come first-serve limit.
@@ -100,6 +104,10 @@ impl MemoryPool for GreedyMemoryPool {
100104
fn reserved(&self) -> usize {
101105
self.used.load(Ordering::Relaxed)
102106
}
107+
108+
fn memory_limit(&self) -> MemoryLimit {
109+
MemoryLimit::Finite(self.pool_size)
110+
}
103111
}
104112

105113
/// A [`MemoryPool`] that prevents spillable reservations from using more than
@@ -233,6 +241,10 @@ impl MemoryPool for FairSpillPool {
233241
let state = self.state.lock();
234242
state.spillable + state.unspillable
235243
}
244+
245+
fn memory_limit(&self) -> MemoryLimit {
246+
MemoryLimit::Finite(self.pool_size)
247+
}
236248
}
237249

238250
/// Constructs a resources error based upon the individual [`MemoryReservation`].
@@ -408,6 +420,10 @@ impl<I: MemoryPool> MemoryPool for TrackConsumersPool<I> {
408420
fn reserved(&self) -> usize {
409421
self.inner.reserved()
410422
}
423+
424+
fn memory_limit(&self) -> MemoryLimit {
425+
self.inner.memory_limit()
426+
}
411427
}
412428

413429
fn provide_top_memory_consumers_to_error_msg(

0 commit comments

Comments
 (0)