@@ -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
142141void 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
149153void 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
0 commit comments