github-actions[bot] commented on code in PR #62273:
URL: https://github.com/apache/doris/pull/62273#discussion_r3129155937
##########
be/src/exec/common/memory.h:
##########
@@ -29,78 +30,106 @@
#include "util/uid_util.h"
namespace doris {
-static constexpr int64_t DEFAULT_SCANNER_MEM_BYTES = 64 * 1024 * 1024; // 64MB
default
+// Default memory estimate per block/task when actual usage is unknown.
+// Used as initial estimate before runtime statistics are collected.
+static constexpr int64_t DEFAULT_BLOCK_MEM_BYTES = 64 * 1024 * 1024; // 64MB
default
-// Query-level memory arbitrator that distributes memory fairly across all
scan contexts
+struct MemLimiter;
+struct AdaptiveTaskProcessor;
+
+// ==================== MemShareArbitrator ====================
+// Query-level memory arbitrator that distributes memory fairly across all
operators.
+//
+// This arbitrator implements proportional memory sharing: each operator gets
a share
+// of the total memory budget proportional to its current memory usage
relative to
+// all operators' total usage. This ensures fair distribution while allowing
operators
+// with higher memory demands to get more resources.
+//
+// Thread-safety: All public methods are thread-safe. The arbitrator can be
accessed
+// concurrently by multiple operators without external synchronization.
+//
+// Usage:
+// 1. Create one arbitrator per query with the query's memory limit
+// 2. Each operator calls register_operator() during initialization
+// 3. MemLimiter instances call update_mem_bytes() to report usage changes
+// and receive their new memory allocation
struct MemShareArbitrator {
+public:
ENABLE_FACTORY_CREATOR(MemShareArbitrator)
- TUniqueId query_id;
- int64_t query_mem_limit = 0;
- int64_t mem_limit = 0;
- std::atomic<int64_t> total_mem_bytes = 0;
-
- MemShareArbitrator(const TUniqueId& qid, int64_t query_mem_limit, double
max_scan_ratio);
-
- // Update memory allocation when scanner memory usage changes
- // Returns new scan memory limit for this context
- int64_t update_mem_bytes(int64_t old_value, int64_t new_value);
- void register_scan_node();
+ MemShareArbitrator(const TUniqueId& qid, int64_t query_mem_limit, double
max_ratio);
+ void register_operator();
std::string debug_string() const {
return fmt::format("query_id: {}, query_mem_limit: {}, mem_limit: {}",
print_id(query_id),
query_mem_limit, mem_limit);
}
-};
-// Scan-context-level memory limiter that controls scanner concurrency based
on memory
-struct MemLimiter {
private:
- TUniqueId query_id;
- mutable std::mutex lock;
- // Parallelism of the scan operator
- const int64_t parallelism = 0;
- const bool serial_operator = false;
- const int64_t operator_mem_limit;
- std::atomic<int64_t> running_tasks_count = 0;
-
- std::atomic<int64_t> estimated_block_mem_bytes = 0;
- int64_t estimated_block_mem_bytes_update_count = 0;
- int64_t arb_mem_bytes = 0;
- std::atomic<int64_t> open_tasks_count = 0;
+ friend struct MemLimiter;
+ // Update memory allocation when memory usage changes
+ // Returns new operator memory limit for this context
+ int64_t update_mem_bytes(int64_t old_value, int64_t new_value);
- // Memory limit for this scan node (shared by all instances), updated by
memory share arbitrator
- std::atomic<int64_t> mem_limit = 0;
+ const TUniqueId query_id;
+ const int64_t query_mem_limit = 0;
+ const int64_t mem_limit = 0;
+ std::atomic<int64_t> total_mem_bytes = 0;
+};
+// ==================== MemLimiter ====================
+// Operator-level memory limiter that controls task concurrency based on
memory constraints.
+//
+// Each operator (scan, local exchange, etc.) has a MemLimiter that:
+// 1. Tracks estimated memory usage per task (block)
+// 2. Calculates how many concurrent tasks can run within memory limits
+// 3. Coordinates with MemShareArbitrator for fair memory distribution
+//
+// The limiter uses a running average of block memory sizes to estimate future
usage,
+// which helps adapt to varying data characteristics during query execution.
+//
+// Lifecycle:
+// - init(): Called when first task starts, registers with arbitrator
+// - close(): Called when last task completes, releases arbitrator resources
+//
+// Thread-safety: All public methods are thread-safe.
+struct MemLimiter {
public:
ENABLE_FACTORY_CREATOR(MemLimiter)
- MemLimiter(const TUniqueId& qid, int64_t parallelism, bool
serial_operator_, int64_t mem_limit)
+ MemLimiter(const TUniqueId& qid, int64_t parallelism, bool
serial_operator_, int64_t mem_limit,
+ std::shared_ptr<MemShareArbitrator> mem_arb)
: query_id(qid),
parallelism(parallelism),
serial_operator(serial_operator_),
- operator_mem_limit(mem_limit) {}
- ~MemLimiter() { DCHECK_LE(open_tasks_count, 0); }
-
- // Calculate available scanner count based on memory limit
- int available_scanner_count(int ins_idx) const;
+ operator_mem_limit(mem_limit),
+ mem_share_arb(mem_arb) {}
+ ~MemLimiter() {
+ if (open_tasks_count > 0) {
+ // This operator shutdown with failure, memory should be reset
here.
+ estimated_block_mem_bytes = 0;
+ update_mem_limit();
+ }
+ }
+ int64_t init() {
+ // TODO(gabriel): set estimated block size
Review Comment:
`init()` now injects the 64MB bootstrap sample a second time. Both shared
call sites already do `reestimated_block_mem_bytes(DEFAULT_BLOCK_MEM_BYTES)`
before they call `init()` (`ScannerContext::init()` and
`LocalExchangeSinkLocalState::init()`). With this extra call, every
scanner/local-exchange context contributes two 64MB samples before any real
block is seen, so `estimated_block_mem_bytes` stays artificially high and
`empty_task_slots()` stays artificially low. For example, with a 256MB budget
and real 1MB blocks, the old path reached `(64 + 1) / 2 = 32.5MB` after the
first real sample, while the new path reaches `(64 + 64 + 1) / 3 = 43MB`, which
unnecessarily cuts the available concurrency. Only one layer should seed the
default sample; otherwise the shared limiter over-throttles both scan and
local-exchange paths.
##########
be/src/exec/exchange/local_exchanger.cpp:
##########
@@ -530,17 +528,43 @@ Status AdaptivePassthroughExchanger::sink(RuntimeState*
state, Block* in_block,
Review Comment:
This passthrough fast-path breaks mixed queues.
`AdaptivePassthroughExchanger` can enqueue both shuffled slices (`row_idxs !=
nullptr`) and later passthrough blocks (`row_idxs == nullptr`) into the same
source queue once `_is_pass_through` flips. If `get_block()` has already
appended rows from an earlier shuffled slice into `mutable_block`, this branch
overwrites `*block` with the passthrough block and drops the rows that were
already accumulated. A concrete path is: source queue contains shuffled slice A
for channel `i`, then a later passthrough block B for the same channel;
`get_block()` appends A, sees `mutable_block.rows() < batch_size()`, dequeues
B, hits this branch, and returns only B. Please flush the accumulated
`mutable_block` first (or defer the passthrough block to the next call) instead
of overwriting the partially assembled result.
--
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]