You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "xiaoyong-z (via GitHub)" <gi...@apache.org> on 2023/02/02 08:39:31 UTC

[GitHub] [arrow-datafusion] xiaoyong-z commented on a diff in pull request #4522: Simplify MemoryManager

xiaoyong-z commented on code in PR #4522:
URL: https://github.com/apache/arrow-datafusion/pull/4522#discussion_r1094187143


##########
datafusion/core/src/execution/memory_pool/pool.rs:
##########
@@ -0,0 +1,285 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::execution::memory_pool::{MemoryConsumer, MemoryPool, MemoryReservation};
+use datafusion_common::{DataFusionError, Result};
+use parking_lot::Mutex;
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+/// A [`MemoryPool`] that enforces no limit
+#[derive(Debug, Default)]
+pub struct UnboundedMemoryPool {
+    used: AtomicUsize,
+}
+
+impl MemoryPool for UnboundedMemoryPool {
+    fn grow(&self, _reservation: &MemoryReservation, additional: usize) {
+        self.used.fetch_add(additional, Ordering::Relaxed);
+    }
+
+    fn shrink(&self, _reservation: &MemoryReservation, shrink: usize) {
+        self.used.fetch_sub(shrink, Ordering::Relaxed);
+    }
+
+    fn try_grow(&self, reservation: &MemoryReservation, additional: usize) -> Result<()> {
+        self.grow(reservation, additional);
+        Ok(())
+    }
+
+    fn reserved(&self) -> usize {
+        self.used.load(Ordering::Relaxed)
+    }
+}
+
+/// A [`MemoryPool`] that implements a greedy first-come first-serve limit
+#[derive(Debug)]
+pub struct GreedyMemoryPool {
+    pool_size: usize,
+    used: AtomicUsize,
+}
+
+impl GreedyMemoryPool {
+    /// Allocate up to `limit` bytes
+    pub fn new(pool_size: usize) -> Self {
+        Self {
+            pool_size,
+            used: AtomicUsize::new(0),
+        }
+    }
+}
+
+impl MemoryPool for GreedyMemoryPool {
+    fn grow(&self, _reservation: &MemoryReservation, additional: usize) {
+        self.used.fetch_add(additional, Ordering::Relaxed);
+    }
+
+    fn shrink(&self, _reservation: &MemoryReservation, shrink: usize) {
+        self.used.fetch_sub(shrink, Ordering::Relaxed);
+    }
+
+    fn try_grow(&self, reservation: &MemoryReservation, additional: usize) -> Result<()> {
+        self.used
+            .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |used| {
+                let new_used = used + additional;
+                (new_used <= self.pool_size).then_some(new_used)
+            })
+            .map_err(|used| {
+                insufficient_capacity_err(reservation, additional, self.pool_size - used)
+            })?;
+        Ok(())
+    }
+
+    fn reserved(&self) -> usize {
+        self.used.load(Ordering::Relaxed)
+    }
+}
+
+/// A [`MemoryPool`] that prevents spillable reservations from using more than
+/// an even fraction of the available memory sans any unspillable reservations
+/// (i.e. `(pool_size - unspillable_memory) / num_spillable_reservations`)
+///
+///    ┌───────────────────────z──────────────────────z───────────────┐
+///    │                       z                      z               │
+///    │                       z                      z               │
+///    │       Spillable       z       Unspillable    z     Free      │
+///    │        Memory         z        Memory        z    Memory     │
+///    │                       z                      z               │
+///    │                       z                      z               │
+///    └───────────────────────z──────────────────────z───────────────┘
+///
+/// Unspillable memory is allocated in a first-come, first-serve fashion
+#[derive(Debug)]
+pub struct FairSpillPool {
+    /// The total memory limit
+    pool_size: usize,
+
+    state: Mutex<FairSpillPoolState>,
+}
+
+#[derive(Debug)]
+struct FairSpillPoolState {
+    /// The number of consumers that can spill
+    num_spill: usize,
+
+    /// The total amount of memory reserved that can be spilled
+    spillable: usize,
+
+    /// The total amount of memory reserved by consumers that cannot spill
+    unspillable: usize,
+}
+
+impl FairSpillPool {
+    /// Allocate up to `limit` bytes
+    pub fn new(pool_size: usize) -> Self {
+        Self {
+            pool_size,
+            state: Mutex::new(FairSpillPoolState {
+                num_spill: 0,
+                spillable: 0,
+                unspillable: 0,
+            }),
+        }
+    }
+}
+
+impl MemoryPool for FairSpillPool {
+    fn register(&self, consumer: &MemoryConsumer) {
+        if consumer.can_spill {
+            self.state.lock().num_spill += 1;
+        }
+    }
+
+    fn unregister(&self, consumer: &MemoryConsumer) {
+        if consumer.can_spill {
+            self.state.lock().num_spill -= 1;
+        }
+    }
+
+    fn grow(&self, reservation: &MemoryReservation, additional: usize) {
+        let mut state = self.state.lock();
+        match reservation.consumer.can_spill {
+            true => state.spillable += additional,
+            false => state.unspillable += additional,
+        }
+    }
+
+    fn shrink(&self, reservation: &MemoryReservation, shrink: usize) {
+        let mut state = self.state.lock();
+        match reservation.consumer.can_spill {
+            true => state.spillable -= shrink,
+            false => state.unspillable -= shrink,
+        }
+    }
+
+    fn try_grow(&self, reservation: &MemoryReservation, additional: usize) -> Result<()> {
+        let mut state = self.state.lock();
+
+        match reservation.consumer.can_spill {
+            true => {
+                // The total amount of memory available to spilling consumers
+                let spill_available = self.pool_size.saturating_sub(state.unspillable);
+
+                // No spiller may use more than their fraction of the memory available
+                let available = spill_available
+                    .checked_div(state.num_spill)
+                    .unwrap_or(spill_available);
+
+                if reservation.size + additional > available {
+                    return Err(insufficient_capacity_err(
+                        reservation,
+                        additional,
+                        available,
+                    ));
+                }
+                state.spillable += additional;
+            }
+            false => {
+                let available = self
+                    .pool_size
+                    .saturating_sub(state.unspillable + state.unspillable);

Review Comment:
   why sub state.unspillable twice here



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org