|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use datafusion::execution::memory_pool::{ |
| 19 | + MemoryConsumer, MemoryLimit, MemoryPool, MemoryReservation, |
| 20 | +}; |
| 21 | +use log::info; |
| 22 | +use std::sync::Arc; |
| 23 | + |
| 24 | +#[derive(Debug)] |
| 25 | +pub(crate) struct LoggingPool { |
| 26 | + task_attempt_id: u64, |
| 27 | + pool: Arc<dyn MemoryPool>, |
| 28 | +} |
| 29 | + |
| 30 | +impl LoggingPool { |
| 31 | + pub fn new(task_attempt_id: u64, pool: Arc<dyn MemoryPool>) -> Self { |
| 32 | + Self { |
| 33 | + task_attempt_id, |
| 34 | + pool, |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl MemoryPool for LoggingPool { |
| 40 | + fn register(&self, consumer: &MemoryConsumer) { |
| 41 | + self.pool.register(consumer) |
| 42 | + } |
| 43 | + |
| 44 | + fn unregister(&self, consumer: &MemoryConsumer) { |
| 45 | + self.pool.unregister(consumer) |
| 46 | + } |
| 47 | + |
| 48 | + fn grow(&self, reservation: &MemoryReservation, additional: usize) { |
| 49 | + info!( |
| 50 | + "[Task {}] MemoryPool[{}].grow({})", |
| 51 | + self.task_attempt_id, |
| 52 | + reservation.consumer().name(), |
| 53 | + additional |
| 54 | + ); |
| 55 | + self.pool.grow(reservation, additional); |
| 56 | + } |
| 57 | + |
| 58 | + fn shrink(&self, reservation: &MemoryReservation, shrink: usize) { |
| 59 | + info!( |
| 60 | + "[Task {}] MemoryPool[{}].shrink({})", |
| 61 | + self.task_attempt_id, |
| 62 | + reservation.consumer().name(), |
| 63 | + shrink |
| 64 | + ); |
| 65 | + self.pool.shrink(reservation, shrink); |
| 66 | + } |
| 67 | + |
| 68 | + fn try_grow( |
| 69 | + &self, |
| 70 | + reservation: &MemoryReservation, |
| 71 | + additional: usize, |
| 72 | + ) -> datafusion::common::Result<()> { |
| 73 | + match self.pool.try_grow(reservation, additional) { |
| 74 | + Ok(_) => { |
| 75 | + info!( |
| 76 | + "[Task {}] MemoryPool[{}].try_grow({}) returning Ok", |
| 77 | + self.task_attempt_id, |
| 78 | + reservation.consumer().name(), |
| 79 | + additional |
| 80 | + ); |
| 81 | + Ok(()) |
| 82 | + } |
| 83 | + Err(e) => { |
| 84 | + info!( |
| 85 | + "[Task {}] MemoryPool[{}].try_grow({}) returning Err: {e:?}", |
| 86 | + self.task_attempt_id, |
| 87 | + reservation.consumer().name(), |
| 88 | + additional |
| 89 | + ); |
| 90 | + Err(e) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + fn reserved(&self) -> usize { |
| 96 | + self.pool.reserved() |
| 97 | + } |
| 98 | + |
| 99 | + fn memory_limit(&self) -> MemoryLimit { |
| 100 | + self.pool.memory_limit() |
| 101 | + } |
| 102 | +} |
0 commit comments