yjshen commented on a change in pull request #1526: URL: https://github.com/apache/arrow-datafusion/pull/1526#discussion_r782802400
########## File path: datafusion/src/execution/memory_manager.rs ########## @@ -0,0 +1,490 @@ +// 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. + +//! Manages all available memory during query execution + +use crate::error::Result; +use async_trait::async_trait; +use hashbrown::HashMap; +use log::info; +use std::fmt; +use std::fmt::{Debug, Display, Formatter}; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::{Arc, Condvar, Mutex, Weak}; + +static CONSUMER_ID: AtomicUsize = AtomicUsize::new(0); + +fn next_id() -> usize { + CONSUMER_ID.fetch_add(1, Ordering::SeqCst) +} + +/// Type of the memory consumer +pub enum ConsumerType { + /// consumers that can grow its memory usage by requesting more from the memory manager or + /// shrinks its memory usage when we can no more assign available memory to it. + /// Examples are spillable sorter, spillable hashmap, etc. + Requesting, + /// consumers that are not spillable, counting in for only tracking purpose. + Tracking, +} + +#[derive(Clone, Debug, Hash, Eq, PartialEq)] +/// Id that uniquely identifies a Memory Consumer +pub struct MemoryConsumerId { + /// partition the consumer belongs to + pub partition_id: usize, + /// unique id + pub id: usize, +} + +impl MemoryConsumerId { + /// Auto incremented new Id + pub fn new(partition_id: usize) -> Self { + let id = next_id(); + Self { partition_id, id } + } +} + +impl Display for MemoryConsumerId { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}:{}", self.partition_id, self.id) + } +} + +#[async_trait] +/// A memory consumer that either takes up memory (of type `ConsumerType::Tracking`) +/// or grows/shrinks memory usage based on available memory (of type `ConsumerType::Requesting`). +pub trait MemoryConsumer: Send + Sync { + /// Display name of the consumer + fn name(&self) -> String; + + /// Unique id of the consumer + fn id(&self) -> &MemoryConsumerId; + + /// Ptr to MemoryManager + fn memory_manager(&self) -> Arc<MemoryManager>; + + /// Partition that the consumer belongs to + fn partition_id(&self) -> usize { + self.id().partition_id + } + + /// Type of the consumer + fn type_(&self) -> &ConsumerType; + + /// Grow memory by `required` to buffer more data in memory, + /// this may trigger spill before grow when the memory threshold is + /// reached for this consumer. + async fn try_grow(&self, required: usize) -> Result<()> { + let current = self.mem_used(); + info!( + "trying to acquire {} whiling holding {} from consumer {}", + human_readable_size(required), + human_readable_size(current), + self.id(), + ); + + let can_grow_directly = self + .memory_manager() + .can_grow_directly(required, current) + .await; + if !can_grow_directly { + info!( + "Failed to grow memory of {} directly from consumer {}, spilling first ...", + human_readable_size(required), + self.id() + ); + let freed = self.spill().await?; + self.memory_manager().record_free(freed); + } + self.memory_manager().record_acquire(required); Review comment: Fixed, mind taking another look? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
