apache / apache/datafusion

[DISCUSSION] Memory accounting model discussion

Open
#16,841 24 comments 9 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
9.3k
Forks
2.4k
Avg merge
3d 7h
Merged PRs (30d)
344

Description

The current model used in DataFusion for measuring memory consumption assumes that the different entities that can consume memory (accumulators, joins, etc...) are the ones owning the data. This does not exactly match with how memory is managed in arrow-rs, where underlying data buffers might be referenced more than 1 time in different parts of DataFusion.

For example: `ArrayAggAccumulator` accumulates data by storing `ArrayRef`s, so even if it's accumulating and retaining data, the actual memory was allocated before `ArrayAggAccumulator` came into play, and the accumulator is only adding a reference to it:

https://github.com/apache/datafusion/blob/ac407a19e030bfee092a0992093b886bde86d97e/datafusion/functions-aggregate/src/array_agg.rs#L214

For that case, one could argue `ArrayAggAccumulator` is not really consuming any memory, as it's not performing new allocations, and the underlying data was there potentially even before the `ArrayAggAccumulator` was instantiated.

There has been several attempts in the past towards addressing memory accounting issues in DataFusion code
- https://github.com/apache/datafusion/pull/15924
- https://github.com/apache/datafusion/pull/16346
- https://github.com/apache/datafusion/pull/16519
- https://github.com/apache/datafusion/pull/16816

Some imply copying/compacting just the necessary slice of data from the underlying buffer (`ScalarValue::compact`) so that it's actually owned by the consumer, but in certain cases that could take a hit to performance.

---

The main point about this issue is to start a conversation around what could be the ideal approach for memory counting:
- Is copying/compacting accumulated data and calling `get_array_memory_size()` or storing array references and calling `get_slice_memory_size()` acceptable for measuring memory consumption?
- Should the memory counting model in DataFusion be expanded so that it does not take into account just memory consumed, but also memory retained because of references to shared buffers

Contributor guide

Open the contributing guide

Research direction

Start with datafusion/functions-aggregate/src/array_agg.rs at the referenced ArrayAggAccumulator code, then review the linked pull requests and the uses of ScalarValue::compact, get_array_memory_size(), and get_slice_memory_size(). The discussion needs an agreed memory-accounting model covering copied data and shared-buffer references; done means a documented direction with its performance and accounting tradeoffs.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
data-engineering
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.