@@ -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
170168void 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
177180void 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
0 commit comments