Skip to content

Commit 71dcb58

Browse files
authored
[improvement](scanner_schedule) reduce memory consumption of scanner (apache#24199)
* [improvement](scanner_schedule) reduce memory consumption of scanner 1. limit scanner by memory consumptin rather than blocks. 2. scheduler run correcty instread of at lest 1.
1 parent 8afdfd5 commit 71dcb58

7 files changed

Lines changed: 71 additions & 58 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ bool ScanOperator::can_read() {
5454
return true;
5555
} else {
5656
if (_node->_scanner_ctx->get_num_running_scanners() == 0 &&
57-
_node->_scanner_ctx->has_enough_space_in_blocks_queue()) {
57+
_node->_scanner_ctx->should_be_scheduled()) {
5858
_node->_scanner_ctx->reschedule_scanner_ctx();
5959
}
6060
return _node->ready_to_read(); // there are some blocks to process

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ struct DataReadyDependency : public Dependency {
9898
void* shared_state() override { return nullptr; }
9999

100100
[[nodiscard]] Dependency* read_blocked_by() override {
101-
if (_scanner_ctx->get_num_running_scanners() == 0 &&
102-
_scanner_ctx->has_enough_space_in_blocks_queue()) {
101+
if (_scanner_ctx->get_num_running_scanners() == 0 && _scanner_ctx->should_be_scheduled()) {
103102
_scanner_ctx->reschedule_scanner_ctx();
104103
}
105104
return _ready_for_read ? nullptr : this;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ Status NewOlapScanner::open(RuntimeState* state) {
272272
return Status::InternalError(ss.str());
273273
}
274274

275+
// Do not hold rs_splits any more to release memory.
276+
_tablet_reader_params.rs_splits.clear();
277+
275278
return Status::OK();
276279
}
277280

@@ -530,7 +533,8 @@ Status NewOlapScanner::close(RuntimeState* state) {
530533
// so that it will core
531534
_tablet_reader_params.rs_splits.clear();
532535
_tablet_reader.reset();
533-
536+
auto tablet_id = _scan_range.tablet_id;
537+
LOG(INFO) << "close_tablet_id" << tablet_id;
534538
RETURN_IF_ERROR(VScanner::close(state));
535539
return Status::OK();
536540
}

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

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

188-
bool has_enough_space_in_blocks_queue() const override {
189-
return _current_used_bytes < _max_bytes_in_queue / 2 * _num_parallel_instances;
190-
}
191-
192188
void _dispose_coloate_blocks_not_in_queue() override {
193189
if (_need_colocate_distribute) {
194190
for (int i = 0; i < _num_parallel_instances; ++i) {
@@ -257,8 +253,7 @@ class PipScannerContext : public vectorized::ScannerContext {
257253
if (_data_dependency) {
258254
_data_dependency->set_ready_for_read();
259255
}
260-
bool get_block_not_empty = true;
261-
_colocate_blocks[loc] = get_free_block(&get_block_not_empty, get_block_not_empty);
256+
_colocate_blocks[loc] = get_free_block();
262257
_colocate_mutable_blocks[loc]->set_muatable_columns(
263258
_colocate_blocks[loc]->mutate_columns());
264259
}

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

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ ScannerContext::ScannerContext(doris::RuntimeState* state_, doris::vectorized::V
5555
_process_status(Status::OK()),
5656
_batch_size(state_->batch_size()),
5757
limit(limit_),
58-
_max_bytes_in_queue(max_bytes_in_blocks_queue_),
58+
_max_bytes_in_queue(max_bytes_in_blocks_queue_ * num_parallel_instances),
5959
_scanner_scheduler(state_->exec_env()->scanner_scheduler()),
6060
_scanners(scanners_),
6161
_num_parallel_instances(num_parallel_instances) {
@@ -66,27 +66,21 @@ ScannerContext::ScannerContext(doris::RuntimeState* state_, doris::vectorized::V
6666
if (limit < 0) {
6767
limit = -1;
6868
}
69-
}
70-
71-
// After init function call, should not access _parent
72-
Status ScannerContext::init() {
73-
// 1. Calculate max concurrency
74-
// TODO: now the max thread num <= config::doris_scanner_thread_pool_thread_num / 4
75-
// should find a more reasonable value.
7669
_max_thread_num = config::doris_scanner_thread_pool_thread_num / 4;
77-
if (_parent && _parent->_shared_scan_opt) {
78-
DCHECK(_num_parallel_instances > 0);
79-
_max_thread_num *= _num_parallel_instances;
80-
}
70+
_max_thread_num *= num_parallel_instances;
8171
_max_thread_num = _max_thread_num == 0 ? 1 : _max_thread_num;
8272
DCHECK(_max_thread_num > 0);
8373
_max_thread_num = std::min(_max_thread_num, (int32_t)_scanners.size());
74+
// 1. Calculate max concurrency
8475
// For select * from table limit 10; should just use one thread.
8576
if ((_parent && _parent->should_run_serial()) ||
8677
(_local_state && _local_state->should_run_serial())) {
8778
_max_thread_num = 1;
8879
}
80+
}
8981

82+
// After init function call, should not access _parent
83+
Status ScannerContext::init() {
9084
if (_parent) {
9185
_scanner_profile = _parent->_scanner_profile;
9286
_scanner_sched_counter = _parent->_scanner_sched_counter;
@@ -120,6 +114,9 @@ Status ScannerContext::init() {
120114
limit == -1 ? _batch_size : std::min(static_cast<int64_t>(_batch_size), limit);
121115
_block_per_scanner = (doris_scanner_row_num + (real_block_size - 1)) / real_block_size;
122116
_free_blocks_capacity = _max_thread_num * _block_per_scanner;
117+
auto block = get_free_block();
118+
_estimated_block_bytes = std::max(block->allocated_bytes(), (size_t)16);
119+
return_free_block(std::move(block));
123120

124121
#ifndef BE_TEST
125122
// 3. get thread token
@@ -151,27 +148,33 @@ std::string ScannerContext::parent_name() {
151148
return _parent ? _parent->get_name() : _local_state->get_name();
152149
}
153150

154-
vectorized::BlockUPtr ScannerContext::get_free_block(bool* has_free_block,
155-
bool get_block_not_empty) {
151+
vectorized::BlockUPtr ScannerContext::get_free_block() {
156152
vectorized::BlockUPtr block;
157153
if (_free_blocks.try_dequeue(block)) {
158-
if (!get_block_not_empty || block->mem_reuse()) {
159-
_free_blocks_capacity--;
160-
_free_blocks_memory_usage->add(-block->allocated_bytes());
161-
return block;
162-
}
154+
DCHECK(block->mem_reuse());
155+
_free_blocks_memory_usage->add(-block->allocated_bytes());
156+
_serving_blocks_num++;
157+
return block;
163158
}
164159

160+
block = vectorized::Block::create_unique(_output_tuple_desc->slots(), _batch_size,
161+
true /*ignore invalid slots*/);
165162
COUNTER_UPDATE(_newly_create_free_blocks_num, 1);
166-
return vectorized::Block::create_unique(_output_tuple_desc->slots(), _batch_size,
167-
true /*ignore invalid slots*/);
163+
164+
_serving_blocks_num++;
165+
return block;
168166
}
169167

170168
void ScannerContext::return_free_block(std::unique_ptr<vectorized::Block> block) {
171-
block->clear_column_data();
172-
_free_blocks_memory_usage->add(block->allocated_bytes());
173-
_free_blocks.enqueue(std::move(block));
174-
++_free_blocks_capacity;
169+
_serving_blocks_num--;
170+
if (block->mem_reuse()) {
171+
// Only put blocks with schema to free blocks, because colocate blocks
172+
// need schema.
173+
_estimated_block_bytes = std::max(block->allocated_bytes(), (size_t)16);
174+
block->clear_column_data();
175+
_free_blocks_memory_usage->add(block->allocated_bytes());
176+
_free_blocks.enqueue(std::move(block));
177+
}
175178
}
176179

177180
void ScannerContext::append_blocks_to_queue(std::vector<vectorized::BlockUPtr>& blocks) {
@@ -200,14 +203,15 @@ Status ScannerContext::get_block_from_queue(RuntimeState* state, vectorized::Blo
200203
// (if the scheduler continues to schedule, it will cause a lot of busy running).
201204
// At this point, consumers are required to trigger new scheduling to ensure that
202205
// data can be continuously fetched.
203-
if (has_enough_space_in_blocks_queue() && _num_running_scanners == 0) {
206+
if (should_be_scheduled() && _num_running_scanners == 0) {
204207
auto state = _scanner_scheduler->submit(this);
205208
if (state.ok()) {
206209
_num_scheduling_ctx++;
207210
} else {
208211
set_status_on_error(state, false);
209212
}
210213
}
214+
211215
// Wait for block from queue
212216
if (wait) {
213217
// scanner batch wait time
@@ -231,6 +235,7 @@ Status ScannerContext::get_block_from_queue(RuntimeState* state, vectorized::Blo
231235
_blocks_queue.pop_front();
232236
auto block_bytes = (*block)->allocated_bytes();
233237
_cur_bytes_in_queue -= block_bytes;
238+
234239
_queued_blocks_memory_usage->add(-block_bytes);
235240
return Status::OK();
236241
} else {
@@ -381,7 +386,13 @@ void ScannerContext::push_back_scanner_and_reschedule(VScannerSPtr scanner) {
381386
_scanners.push_front(scanner);
382387
}
383388
std::lock_guard l(_transfer_lock);
384-
if (has_enough_space_in_blocks_queue()) {
389+
390+
// In pipeline engine, doris will close scanners when `no_schedule`.
391+
// We have to decrease _num_running_scanners before schedule, otherwise
392+
// schedule does not woring due to _num_running_scanners.
393+
_num_running_scanners--;
394+
395+
if (should_be_scheduled()) {
385396
auto state = _scanner_scheduler->submit(this);
386397
if (state.ok()) {
387398
_num_scheduling_ctx++;
@@ -404,8 +415,6 @@ void ScannerContext::push_back_scanner_and_reschedule(VScannerSPtr scanner) {
404415
}
405416
_blocks_queue_added_cv.notify_one();
406417
}
407-
// In pipeline engine, doris will close scanners when `no_schedule`.
408-
_num_running_scanners--;
409418
_ctx_finish_cv.notify_one();
410419
}
411420

@@ -415,7 +424,7 @@ void ScannerContext::get_next_batch_of_scanners(std::list<VScannerSPtr>* current
415424
{
416425
// If there are enough space in blocks queue,
417426
// the scanner number depends on the _free_blocks numbers
418-
thread_slot_num = cal_thread_slot_num_by_free_block_num();
427+
thread_slot_num = get_available_thread_slot_num();
419428
}
420429

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

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ class ScannerContext {
7272
ScannerContext(RuntimeState* state_, VScanNode* parent,
7373
const TupleDescriptor* output_tuple_desc,
7474
const std::list<VScannerSPtr>& scanners_, int64_t limit_,
75-
int64_t max_bytes_in_blocks_queue_, const int num_parallel_instances = 0,
75+
int64_t max_bytes_in_blocks_queue_, const int num_parallel_instances = 1,
7676
pipeline::ScanLocalStateBase* local_state = nullptr);
7777

7878
virtual ~ScannerContext() = default;
7979
virtual Status init();
8080

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

8484
// Append blocks from scanners to the blocks queue.
@@ -146,19 +146,25 @@ class ScannerContext {
146146
virtual bool empty_in_queue(int id);
147147

148148
// todo(wb) rethinking how to calculate ```_max_bytes_in_queue``` when executing shared scan
149-
virtual inline bool has_enough_space_in_blocks_queue() const {
150-
return _cur_bytes_in_queue < _max_bytes_in_queue / 2;
149+
inline bool should_be_scheduled() const {
150+
return (_cur_bytes_in_queue < _max_bytes_in_queue / 2) &&
151+
(_serving_blocks_num < allowed_blocks_num());
151152
}
152153

153-
int cal_thread_slot_num_by_free_block_num() {
154+
int get_available_thread_slot_num() {
154155
int thread_slot_num = 0;
155-
thread_slot_num = (_free_blocks_capacity + _block_per_scanner - 1) / _block_per_scanner;
156+
thread_slot_num = (allowed_blocks_num() + _block_per_scanner - 1) / _block_per_scanner;
156157
thread_slot_num = std::min(thread_slot_num, _max_thread_num - _num_running_scanners);
157-
if (thread_slot_num <= 0) {
158-
thread_slot_num = 1;
159-
}
160158
return thread_slot_num;
161159
}
160+
161+
int32_t allowed_blocks_num() const {
162+
int32_t blocks_num = std::min(_free_blocks_capacity,
163+
int32_t((_max_bytes_in_queue + _estimated_block_bytes - 1) /
164+
_estimated_block_bytes));
165+
return blocks_num;
166+
}
167+
162168
taskgroup::TaskGroup* get_task_group() const;
163169

164170
void reschedule_scanner_ctx();
@@ -216,10 +222,12 @@ class ScannerContext {
216222

217223
// Lazy-allocated blocks for all scanners to share, for memory reuse.
218224
moodycamel::ConcurrentQueue<vectorized::BlockUPtr> _free_blocks;
225+
std::atomic<int32_t> _serving_blocks_num = 0;
219226
// The current number of free blocks available to the scanners.
220227
// Used to limit the memory usage of the scanner.
221228
// NOTE: this is NOT the size of `_free_blocks`.
222-
std::atomic_int32_t _free_blocks_capacity = 0;
229+
int32_t _free_blocks_capacity = 0;
230+
int64_t _estimated_block_bytes = 0;
223231

224232
int _batch_size;
225233
// The limit from SQL's limit clause
@@ -244,6 +252,7 @@ class ScannerContext {
244252
int64_t _cur_bytes_in_queue = 0;
245253
// The max limit bytes of blocks in blocks queue
246254
const int64_t _max_bytes_in_queue;
255+
std::atomic<int64_t> _bytes_allocated = 0;
247256

248257
doris::vectorized::ScannerScheduler* _scanner_scheduler;
249258
// List "scanners" saves all "unfinished" scanners.

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,6 @@ void ScannerScheduler::_scanner_scan(ScannerScheduler* scheduler, ScannerContext
353353
int64_t raw_rows_threshold = raw_rows_read + config::doris_scanner_row_num;
354354
int64_t raw_bytes_read = 0;
355355
int64_t raw_bytes_threshold = config::doris_scanner_row_bytes;
356-
bool has_free_block = true;
357356
int num_rows_in_block = 0;
358357

359358
// Only set to true when ctx->done() return true.
@@ -363,9 +362,8 @@ void ScannerScheduler::_scanner_scan(ScannerScheduler* scheduler, ScannerContext
363362
bool should_stop = false;
364363
// Has to wait at least one full block, or it will cause a lot of schedule task in priority
365364
// queue, it will affect query latency and query concurrency for example ssb 3.3.
366-
while (!eos && raw_bytes_read < raw_bytes_threshold &&
367-
((raw_rows_read < raw_rows_threshold && has_free_block) ||
368-
num_rows_in_block < state->batch_size())) {
365+
while (!eos && raw_bytes_read < raw_bytes_threshold && raw_rows_read < raw_rows_threshold &&
366+
num_rows_in_block < state->batch_size()) {
369367
// TODO llj task group should should_yield?
370368
if (UNLIKELY(ctx->done())) {
371369
// No need to set status on error here.
@@ -374,7 +372,8 @@ void ScannerScheduler::_scanner_scan(ScannerScheduler* scheduler, ScannerContext
374372
break;
375373
}
376374

377-
BlockUPtr block = ctx->get_free_block(&has_free_block);
375+
BlockUPtr block = ctx->get_free_block();
376+
378377
status = scanner->get_block(state, block.get(), &eos);
379378
VLOG_ROW << "VScanNode input rows: " << block->rows() << ", eos: " << eos;
380379
// The VFileScanner for external table may try to open not exist files,
@@ -390,12 +389,11 @@ void ScannerScheduler::_scanner_scan(ScannerScheduler* scheduler, ScannerContext
390389
if (status.is<ErrorCode::NOT_FOUND>()) {
391390
// The only case in this "if" branch is external table file delete and fe cache has not been updated yet.
392391
// Set status to OK.
393-
LOG(INFO) << "scan range not found: " << scanner->get_current_scan_range_name();
394392
status = Status::OK();
395393
eos = true;
396394
}
397395

398-
raw_bytes_read += block->bytes();
396+
raw_bytes_read += block->allocated_bytes();
399397
num_rows_in_block += block->rows();
400398
if (UNLIKELY(block->rows() == 0)) {
401399
ctx->return_free_block(std::move(block));
@@ -430,7 +428,6 @@ void ScannerScheduler::_scanner_scan(ScannerScheduler* scheduler, ScannerContext
430428
if (eos || should_stop) {
431429
scanner->mark_to_need_to_close();
432430
}
433-
434431
ctx->push_back_scanner_and_reschedule(scanner);
435432
}
436433

0 commit comments

Comments
 (0)