Skip to content

Commit 9a19581

Browse files
authored
[improvement](scanner_schedule) reduce memory consumption of scanner #24199 (#25547)
1 parent 2b34a7f commit 9a19581

7 files changed

Lines changed: 66 additions & 55 deletions

File tree

‎be/src/exec/exec_node.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "runtime/runtime_state.h"
4242
#include "util/debug_util.h"
4343
#include "util/runtime_profile.h"
44+
#include "util/stack_util.h"
4445
#include "util/uid_util.h"
4546
#include "vec/columns/column_nullable.h"
4647
#include "vec/core/block.h"
@@ -205,7 +206,7 @@ Status ExecNode::close(RuntimeState* state) {
205206
<< " already closed";
206207
return Status::OK();
207208
}
208-
LOG(INFO) << "fragment_instance_id=" << print_id(state->fragment_instance_id()) << " closed";
209+
LOG(INFO) << "fragment_instance_id=" << print_id(state->fragment_instance_id()) << " closed. ";
209210
_is_closed = true;
210211

211212
Status result;

‎be/src/pipeline/exec/scan_operator.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ bool ScanOperator::can_read() {
4444
return true;
4545
} else {
4646
if (_node->_scanner_ctx->get_num_running_scanners() == 0 &&
47-
_node->_scanner_ctx->has_enough_space_in_blocks_queue()) {
47+
_node->_scanner_ctx->should_be_scheduled()) {
4848
_node->_scanner_ctx->reschedule_scanner_ctx();
4949
}
5050
return _node->ready_to_read(); // there are some blocks to process

‎be/src/runtime/plan_fragment_executor.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "util/container_util.hpp"
5454
#include "util/defer_op.h"
5555
#include "util/pretty_printer.h"
56+
#include "util/stack_util.h"
5657
#include "util/telemetry/telemetry.h"
5758
#include "util/threadpool.h"
5859
#include "util/time.h"

‎be/src/vec/exec/scan/pip_scanner_context.h‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,6 @@ class PipScannerContext : public vectorized::ScannerContext {
166166
_free_blocks_memory_usage->add(free_blocks_memory_usage);
167167
}
168168

169-
bool has_enough_space_in_blocks_queue() const override {
170-
return _current_used_bytes < _max_bytes_in_queue / 2 * _num_parallel_instances;
171-
}
172-
173169
void _dispose_coloate_blocks_not_in_queue() override {
174170
if (_need_colocate_distribute) {
175171
for (int i = 0; i < _num_parallel_instances; ++i) {
@@ -221,8 +217,7 @@ class PipScannerContext : public vectorized::ScannerContext {
221217
std::lock_guard<std::mutex> queue_l(*_queue_mutexs[loc]);
222218
_blocks_queues[loc].emplace_back(std::move(_colocate_blocks[loc]));
223219
}
224-
bool get_block_not_empty = true;
225-
_colocate_blocks[loc] = get_free_block(&get_block_not_empty, get_block_not_empty);
220+
_colocate_blocks[loc] = get_free_block();
226221
_colocate_mutable_blocks[loc]->set_muatable_columns(
227222
_colocate_blocks[loc]->mutate_columns());
228223
}

‎be/src/vec/exec/scan/scanner_context.cpp‎

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ ScannerContext::ScannerContext(doris::RuntimeState* state_, doris::vectorized::V
5252
_process_status(Status::OK()),
5353
_batch_size(state_->batch_size()),
5454
limit(limit_),
55-
_max_bytes_in_queue(max_bytes_in_blocks_queue_),
55+
_max_bytes_in_queue(max_bytes_in_blocks_queue_ * num_parallel_instances),
5656
_scanner_scheduler(state_->exec_env()->scanner_scheduler()),
5757
_scanners(scanners_),
5858
_num_parallel_instances(num_parallel_instances) {
@@ -63,26 +63,21 @@ ScannerContext::ScannerContext(doris::RuntimeState* state_, doris::vectorized::V
6363
if (limit < 0) {
6464
limit = -1;
6565
}
66-
}
6766

68-
// After init function call, should not access _parent
69-
Status ScannerContext::init() {
70-
// 1. Calculate max concurrency
71-
// TODO: now the max thread num <= config::doris_scanner_thread_pool_thread_num / 4
72-
// should find a more reasonable value.
7367
_max_thread_num = config::doris_scanner_thread_pool_thread_num / 4;
74-
if (_parent->_shared_scan_opt) {
75-
DCHECK(_num_parallel_instances > 0);
76-
_max_thread_num *= _num_parallel_instances;
77-
}
68+
_max_thread_num *= num_parallel_instances;
7869
_max_thread_num = _max_thread_num == 0 ? 1 : _max_thread_num;
7970
DCHECK(_max_thread_num > 0);
8071
_max_thread_num = std::min(_max_thread_num, (int32_t)_scanners.size());
72+
// 1. Calculate max concurrency
8173
// For select * from table limit 10; should just use one thread.
8274
if (_parent->should_run_serial()) {
8375
_max_thread_num = 1;
8476
}
77+
}
8578

79+
// After init function call, should not access _parent
80+
Status ScannerContext::init() {
8681
_scanner_profile = _parent->_scanner_profile;
8782
_scanner_sched_counter = _parent->_scanner_sched_counter;
8883
_scanner_ctx_sched_counter = _parent->_scanner_ctx_sched_counter;
@@ -104,6 +99,9 @@ Status ScannerContext::init() {
10499
limit == -1 ? _batch_size : std::min(static_cast<int64_t>(_batch_size), limit);
105100
_block_per_scanner = (doris_scanner_row_num + (real_block_size - 1)) / real_block_size;
106101
_free_blocks_capacity = _max_thread_num * _block_per_scanner;
102+
auto block = get_free_block();
103+
_estimated_block_bytes = std::max(block->allocated_bytes(), (size_t)16);
104+
return_free_block(std::move(block));
107105

108106
#ifndef BE_TEST
109107
// 3. get thread token
@@ -123,27 +121,33 @@ Status ScannerContext::init() {
123121
return Status::OK();
124122
}
125123

126-
vectorized::BlockUPtr ScannerContext::get_free_block(bool* has_free_block,
127-
bool get_block_not_empty) {
124+
vectorized::BlockUPtr ScannerContext::get_free_block() {
128125
vectorized::BlockUPtr block;
129126
if (_free_blocks.try_dequeue(block)) {
130-
if (!get_block_not_empty || block->mem_reuse()) {
131-
_free_blocks_capacity--;
132-
_free_blocks_memory_usage->add(-block->allocated_bytes());
133-
return block;
134-
}
127+
DCHECK(block->mem_reuse());
128+
_free_blocks_memory_usage->add(-block->allocated_bytes());
129+
_serving_blocks_num++;
130+
return block;
135131
}
136132

133+
block = vectorized::Block::create_unique(_output_tuple_desc->slots(), _batch_size,
134+
true /*ignore invalid slots*/);
137135
COUNTER_UPDATE(_newly_create_free_blocks_num, 1);
138-
return vectorized::Block::create_unique(_output_tuple_desc->slots(), _batch_size,
139-
true /*ignore invalid slots*/);
136+
137+
_serving_blocks_num++;
138+
return block;
140139
}
141140

142141
void ScannerContext::return_free_block(std::unique_ptr<vectorized::Block> block) {
143-
block->clear_column_data();
144-
_free_blocks_memory_usage->add(block->allocated_bytes());
145-
_free_blocks.enqueue(std::move(block));
146-
++_free_blocks_capacity;
142+
_serving_blocks_num--;
143+
if (block->mem_reuse()) {
144+
// Only put blocks with schema to free blocks, because colocate blocks
145+
// need schema.
146+
_estimated_block_bytes = std::max(block->allocated_bytes(), (size_t)16);
147+
block->clear_column_data();
148+
_free_blocks_memory_usage->add(block->allocated_bytes());
149+
_free_blocks.enqueue(std::move(block));
150+
}
147151
}
148152

149153
void ScannerContext::append_blocks_to_queue(std::vector<vectorized::BlockUPtr>& blocks) {
@@ -176,14 +180,15 @@ Status ScannerContext::get_block_from_queue(RuntimeState* state, vectorized::Blo
176180
// (if the scheduler continues to schedule, it will cause a lot of busy running).
177181
// At this point, consumers are required to trigger new scheduling to ensure that
178182
// data can be continuously fetched.
179-
if (has_enough_space_in_blocks_queue() && _num_running_scanners == 0) {
183+
if (should_be_scheduled() && _num_running_scanners == 0) {
180184
auto state = _scanner_scheduler->submit(this);
181185
if (state.ok()) {
182186
_num_scheduling_ctx++;
183187
} else {
184188
set_status_on_error(state, false);
185189
}
186190
}
191+
187192
// Wait for block from queue
188193
if (wait) {
189194
SCOPED_TIMER(_scanner_wait_batch_timer);
@@ -207,6 +212,7 @@ Status ScannerContext::get_block_from_queue(RuntimeState* state, vectorized::Blo
207212

208213
auto block_bytes = (*block)->allocated_bytes();
209214
_cur_bytes_in_queue -= block_bytes;
215+
210216
_queued_blocks_memory_usage->add(-block_bytes);
211217
return Status::OK();
212218
} else {
@@ -353,7 +359,13 @@ void ScannerContext::push_back_scanner_and_reschedule(VScannerSPtr scanner) {
353359
_scanners.push_front(scanner);
354360
}
355361
std::lock_guard l(_transfer_lock);
356-
if (has_enough_space_in_blocks_queue()) {
362+
363+
// In pipeline engine, doris will close scanners when `no_schedule`.
364+
// We have to decrease _num_running_scanners before schedule, otherwise
365+
// schedule does not woring due to _num_running_scanners.
366+
_num_running_scanners--;
367+
368+
if (should_be_scheduled()) {
357369
auto state = _scanner_scheduler->submit(this);
358370
if (state.ok()) {
359371
_num_scheduling_ctx++;
@@ -373,8 +385,6 @@ void ScannerContext::push_back_scanner_and_reschedule(VScannerSPtr scanner) {
373385
_is_finished = true;
374386
_blocks_queue_added_cv.notify_one();
375387
}
376-
// In pipeline engine, doris will close scanners when `no_schedule`.
377-
_num_running_scanners--;
378388
_ctx_finish_cv.notify_one();
379389
}
380390

@@ -384,7 +394,7 @@ void ScannerContext::get_next_batch_of_scanners(std::list<VScannerSPtr>* current
384394
{
385395
// If there are enough space in blocks queue,
386396
// the scanner number depends on the _free_blocks numbers
387-
thread_slot_num = cal_thread_slot_num_by_free_block_num();
397+
thread_slot_num = get_available_thread_slot_num();
388398
}
389399

390400
// 2. get #thread_slot_num scanners from ctx->scanners

‎be/src/vec/exec/scan/scanner_context.h‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ class ScannerContext {
6262
ScannerContext(RuntimeState* state_, VScanNode* parent,
6363
const TupleDescriptor* output_tuple_desc,
6464
const std::list<VScannerSPtr>& scanners_, int64_t limit_,
65-
int64_t max_bytes_in_blocks_queue_, const int num_parallel_instances = 0);
65+
int64_t max_bytes_in_blocks_queue_, const int num_parallel_instances = 1);
6666

6767
virtual ~ScannerContext() = default;
6868
virtual Status init();
6969

70-
vectorized::BlockUPtr get_free_block(bool* has_free_block, bool get_not_empty_block = false);
70+
vectorized::BlockUPtr get_free_block();
7171
void return_free_block(std::unique_ptr<vectorized::Block> block);
7272

7373
// Append blocks from scanners to the blocks queue.
@@ -136,20 +136,25 @@ class ScannerContext {
136136
virtual bool empty_in_queue(int id);
137137

138138
// todo(wb) rethinking how to calculate ```_max_bytes_in_queue``` when executing shared scan
139-
virtual inline bool has_enough_space_in_blocks_queue() const {
140-
return _cur_bytes_in_queue < _max_bytes_in_queue / 2;
139+
inline bool should_be_scheduled() const {
140+
return (_cur_bytes_in_queue < _max_bytes_in_queue / 2) &&
141+
(_serving_blocks_num < allowed_blocks_num());
141142
}
142143

143-
int cal_thread_slot_num_by_free_block_num() {
144+
int get_available_thread_slot_num() {
144145
int thread_slot_num = 0;
145-
thread_slot_num = (_free_blocks_capacity + _block_per_scanner - 1) / _block_per_scanner;
146+
thread_slot_num = (allowed_blocks_num() + _block_per_scanner - 1) / _block_per_scanner;
146147
thread_slot_num = std::min(thread_slot_num, _max_thread_num - _num_running_scanners);
147-
if (thread_slot_num <= 0) {
148-
thread_slot_num = 1;
149-
}
150148
return thread_slot_num;
151149
}
152150

151+
int32_t allowed_blocks_num() const {
152+
int32_t blocks_num = std::min(_free_blocks_capacity,
153+
int32_t((_max_bytes_in_queue + _estimated_block_bytes - 1) /
154+
_estimated_block_bytes));
155+
return blocks_num;
156+
}
157+
153158
void reschedule_scanner_ctx();
154159

155160
// the unique id of this context
@@ -203,10 +208,12 @@ class ScannerContext {
203208

204209
// Lazy-allocated blocks for all scanners to share, for memory reuse.
205210
moodycamel::ConcurrentQueue<vectorized::BlockUPtr> _free_blocks;
211+
std::atomic<int32_t> _serving_blocks_num = 0;
206212
// The current number of free blocks available to the scanners.
207213
// Used to limit the memory usage of the scanner.
208214
// NOTE: this is NOT the size of `_free_blocks`.
209-
std::atomic_int32_t _free_blocks_capacity = 0;
215+
int32_t _free_blocks_capacity = 0;
216+
int64_t _estimated_block_bytes = 0;
210217

211218
int _batch_size;
212219
// The limit from SQL's limit clause
@@ -231,6 +238,7 @@ class ScannerContext {
231238
int64_t _cur_bytes_in_queue = 0;
232239
// The max limit bytes of blocks in blocks queue
233240
const int64_t _max_bytes_in_queue;
241+
std::atomic<int64_t> _bytes_allocated = 0;
234242

235243
doris::vectorized::ScannerScheduler* _scanner_scheduler;
236244
// List "scanners" saves all "unfinished" scanners.

‎be/src/vec/exec/scan/scanner_scheduler.cpp‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,6 @@ void ScannerScheduler::_scanner_scan(ScannerScheduler* scheduler, ScannerContext
321321
int64_t raw_rows_threshold = raw_rows_read + config::doris_scanner_row_num;
322322
int64_t raw_bytes_read = 0;
323323
int64_t raw_bytes_threshold = config::doris_scanner_row_bytes;
324-
bool has_free_block = true;
325324
int num_rows_in_block = 0;
326325

327326
// Only set to true when ctx->done() return true.
@@ -331,17 +330,16 @@ void ScannerScheduler::_scanner_scan(ScannerScheduler* scheduler, ScannerContext
331330
bool should_stop = false;
332331
// Has to wait at least one full block, or it will cause a lot of schedule task in priority
333332
// queue, it will affect query latency and query concurrency for example ssb 3.3.
334-
while (!eos && raw_bytes_read < raw_bytes_threshold &&
335-
((raw_rows_read < raw_rows_threshold && has_free_block) ||
336-
num_rows_in_block < state->batch_size())) {
333+
while (!eos && raw_bytes_read < raw_bytes_threshold && raw_rows_read < raw_rows_threshold &&
334+
num_rows_in_block < state->batch_size()) {
337335
if (UNLIKELY(ctx->done())) {
338336
// No need to set status on error here.
339337
// Because done() maybe caused by "should_stop"
340338
should_stop = true;
341339
break;
342340
}
343341

344-
BlockUPtr block = ctx->get_free_block(&has_free_block);
342+
BlockUPtr block = ctx->get_free_block();
345343
status = scanner->get_block(state, block.get(), &eos);
346344
VLOG_ROW << "VScanNode input rows: " << block->rows() << ", eos: " << eos;
347345
// The VFileScanner for external table may try to open not exist files,
@@ -357,12 +355,11 @@ void ScannerScheduler::_scanner_scan(ScannerScheduler* scheduler, ScannerContext
357355
if (status.is<ErrorCode::NOT_FOUND>()) {
358356
// The only case in this "if" branch is external table file delete and fe cache has not been updated yet.
359357
// Set status to OK.
360-
LOG(INFO) << "scan range not found: " << scanner->get_current_scan_range_name();
361358
status = Status::OK();
362359
eos = true;
363360
}
364361

365-
raw_bytes_read += block->bytes();
362+
raw_bytes_read += block->allocated_bytes();
366363
num_rows_in_block += block->rows();
367364
if (UNLIKELY(block->rows() == 0)) {
368365
ctx->return_free_block(std::move(block));
@@ -397,7 +394,6 @@ void ScannerScheduler::_scanner_scan(ScannerScheduler* scheduler, ScannerContext
397394
if (eos || should_stop) {
398395
scanner->mark_to_need_to_close();
399396
}
400-
401397
ctx->push_back_scanner_and_reschedule(scanner);
402398
}
403399

0 commit comments

Comments
 (0)