Skip to content

Untracked memory in FilterExec, RepartitionExec, HashJoinExec and AsyncFuncExec and more due to LimitedBatchCoalescer and arrow BatchCoalescer usage #23385

Description

@rluvaton

Describe the bug

According to the datafusion memory model, you need to reserve memory for things you hold on, LimitedBatchCoalescer which hold on the batches to coalesce and hold on batches that not yet polled does not reserve any memory

LimitedBatchCoalescer is used by:

FilterExec:

batch_coalescer: LimitedBatchCoalescer::new(
self.schema(),
self.batch_size,
self.fetch,
),

RepartitionExec:

SharedCoalescer::new(
input.schema(),
context.session_config().batch_size(),
num_input_partitions,
)

struct SharedCoalescer {
inner: Arc<Mutex<LimitedBatchCoalescer>>,
active_senders: Arc<AtomicUsize>,
}

even though RepartitionExec have some memory reservation, it does not count the LimitedBatchCoalescer memory

HashJoinExec:

// Create output buffer with coalescing and optional fetch limit.
let output_buffer =
LimitedBatchCoalescer::new(Arc::clone(&schema), batch_size, fetch);

AsyncFuncExec

let coalesced_input_stream = CoalesceInputStream {
input_stream,
batch_coalescer: LimitedBatchCoalescer::new(
Arc::clone(&self.input.schema()),
config_options_ref.execution.batch_size.get(),
None,
),
};

and more that use arrow BatchCoalescer directly, since it does not expose a way to know which batches hold which result, you have no way to reserve and free memory for them

Like SortMergeJoin:

/// Output buffer. Currently used by filtering as it requires double buffering
/// to avoid small/empty batches. Non-filtered join outputs directly from `staging_output_record_batches.batches`
pub output: BatchCoalescer,

To Reproduce

Looking at the code you can see no reservation, so just create FilterExec for example and see no reservation being made

Expected behavior

everything that hold on memory should reserve for it

Additional context

Ideally CoalesceBatchStream should output small batches when unable to reach the batch size

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions