avantgardnerio commented on code in PR #22626:
URL: https://github.com/apache/datafusion/pull/22626#discussion_r3343360991


##########
datafusion/sqllogictest/src/accounting_pool.rs:
##########
@@ -0,0 +1,174 @@
+// 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.
+
+//! [`AccountingMemoryPool`] bridges DataFusion's voluntary memory tracking
+//! to the allocator-level bank in [`crate::accounting`].
+//!
+//! It wraps any [`MemoryPool`] and re-tunes the current thread's bank
+//! account whenever the pool's limit changes (via [`MemoryPool::try_resize`],
+//! which `RuntimeEnvBuilder::with_memory_limit` triggers on `SET
+//! datafusion.runtime.memory_limit = '…'`).
+//!
+//! Each retune sets the bank to `new_limit * HEADROOM_FACTOR`. A query
+//! that allocates past that envelope panics with an `OverdraftPanic` —
+//! the gap between DF's voluntary tracker and the allocator's reality
+//! is the bug we're hunting.
+
+use crate::set_account_balance;
+use datafusion::common::Result;
+use datafusion::execution::memory_pool::{
+    MemoryConsumer, MemoryLimit, MemoryPool, MemoryReservation,
+};
+use std::fmt::{self, Display, Formatter};
+use std::sync::Arc;
+
+/// Headroom over the pool's declared limit. Anything past this is an
+/// untracked allocation — by definition, since DF's pool didn't see it.
+///
+/// 600% high, but that's what it takes to pass the SLT suite right now. Goal 
should be ~10%
+const HEADROOM_FACTOR: f64 = 6.0;

Review Comment:
   I think this is going to make tracking down memory errors a lot easier than 
heap profiling, because you get a backtrace:
   
   ```
   thread 'tokio-rt-worker' (41239) panicked at 
datafusion/sqllogictest/src/accounting.rs:250:9:
     Box<dyn Any>
     stack backtrace:
        0: std::panicking::begin_panic
        1: std::panic::panic_any
        2: datafusion_sqllogictest::accounting::track::{{closure}}
        3: std::thread::local::LocalKey<T>::try_with
        4: <datafusion_sqllogictest::accounting::AccountingAllocator<A> as 
core::alloc::global::GlobalAlloc>::alloc
        5: __rustc::__rust_alloc
        6: <alloc::raw_vec::RawVecInner>::try_allocate_in
        7: alloc::raw_vec::RawVecInner<A>::with_capacity_in
        8: <alloc::vec::Vec<T> as 
alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter
        9: <alloc::vec::Vec<T> as 
alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
       10: <alloc::vec::Vec<T> as 
core::iter::traits::collect::FromIterator<T>>::from_iter
       11: core::iter::traits::iterator::Iterator::collect
       12: arrow_arith::arity::binary
       13: arrow_arith::numeric::integer_op
       14: arrow_arith::numeric::arithmetic_op
       15: arrow_arith::numeric::add_wrapping
       16: core::ops::function::Fn::call
       17: datafusion_physical_expr_common::datum::apply
       18: <datafusion_physical_expr::expressions::binary::BinaryExpr as 
datafusion_physical_expr_common::physical_expr::PhysicalExpr>::evaluate
       19: <datafusion_physical_expr::expressions::binary::BinaryExpr as 
datafusion_physical_expr_common::physical_expr::PhysicalExpr>::evaluate
       20: 
datafusion_physical_plan::joins::nested_loop_join::NestedLoopJoinStream::process_left_range_join
       21: 
datafusion_physical_plan::joins::nested_loop_join::NestedLoopJoinStream::process_probe_batch
       22: 
datafusion_physical_plan::joins::nested_loop_join::NestedLoopJoinStream::handle_probe_right
   
     - Frame 20: NestedLoopJoinStream::process_left_range_join
     - Frame 18: BinaryExpr::evaluate (the t1.v1 + t2.v2 predicate)
     - Frame 15: arrow_arith::numeric::add_wrapping
     - Frame 12: arrow_arith::arity::binary — buffer alloc site
     - Frame 8: Vec::from_iter — the actual allocation
   ```
   `process_left_range_join` evaluates the join predicate `t1.v1 + t2.v2` on 
the cross-product (100K × 1 rows),
    and the arrow arithmetic allocates a fresh output buffer without going 
through `MemoryReservation::try_grow()`.
   
   So if we add memory tracking right above [this 
line](https://github.com/apache/datafusion/blob/00c35d0c1e7dc5c1acd5ee7c0b16f1da047faf24/datafusion/physical-plan/src/joins/nested_loop_join.rs#L2154-L2157)
 , it should fix the issue. 



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to