be/src/exec/scan/scanner_scheduler.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "exec/scan/scanner_scheduler.h" |
19 | | |
20 | | #include <algorithm> |
21 | | #include <cstdint> |
22 | | #include <functional> |
23 | | #include <list> |
24 | | #include <memory> |
25 | | #include <ostream> |
26 | | #include <string> |
27 | | #include <utility> |
28 | | |
29 | | #include "common/compiler_util.h" // IWYU pragma: keep |
30 | | #include "common/config.h" |
31 | | #include "common/exception.h" |
32 | | #include "common/logging.h" |
33 | | #include "common/status.h" |
34 | | #include "core/block/block.h" |
35 | | #include "exec/pipeline/pipeline_task.h" |
36 | | #include "exec/scan/file_scanner.h" |
37 | | #include "exec/scan/olap_scanner.h" // IWYU pragma: keep |
38 | | #include "exec/scan/scan_node.h" |
39 | | #include "exec/scan/scanner.h" |
40 | | #include "exec/scan/scanner_context.h" |
41 | | #include "runtime/exec_env.h" |
42 | | #include "runtime/runtime_state.h" |
43 | | #include "runtime/thread_context.h" |
44 | | #include "runtime/workload_group/workload_group_manager.h" |
45 | | #include "storage/tablet/tablet.h" |
46 | | #include "util/async_io.h" // IWYU pragma: keep |
47 | | #include "util/cpu_info.h" |
48 | | #include "util/defer_op.h" |
49 | | #include "util/thread.h" |
50 | | #include "util/threadpool.h" |
51 | | |
52 | | namespace doris { |
53 | | |
54 | | Status ScannerScheduler::submit(std::shared_ptr<ScannerContext> ctx, |
55 | 889k | std::shared_ptr<ScanTask> scan_task) { |
56 | 889k | if (ctx->done()) { |
57 | 0 | return Status::OK(); |
58 | 0 | } |
59 | 889k | auto task_lock = ctx->task_exec_ctx(); |
60 | 889k | if (task_lock == nullptr) { |
61 | 18 | LOG(INFO) << "could not lock task execution context, query " << ctx->debug_string() |
62 | 18 | << " maybe finished"; |
63 | 18 | return Status::OK(); |
64 | 18 | } |
65 | 889k | std::shared_ptr<ScannerDelegate> scanner_delegate = scan_task->scanner.lock(); |
66 | 889k | if (scanner_delegate == nullptr) { |
67 | 0 | return Status::OK(); |
68 | 0 | } |
69 | | |
70 | 889k | scan_task->set_state(ScanTask::State::IN_FLIGHT); |
71 | 889k | scanner_delegate->_scanner->pause(); |
72 | 889k | TabletStorageType type = scanner_delegate->_scanner->get_storage_type(); |
73 | 889k | auto sumbit_task = [&]() { |
74 | 889k | auto work_func = [scanner_ref = scan_task, ctx]() { |
75 | 889k | auto status = [&] { |
76 | 888k | RETURN_IF_CATCH_EXCEPTION(_scanner_scan(ctx, scanner_ref)); |
77 | 889k | return Status::OK(); |
78 | | }(); |
79 | 889k |
|
80 | 0 | if (!status.ok()) { |
81 | 0 | scanner_ref->set_status(status); |
82 | 0 | ctx->push_back_scan_task(scanner_ref); |
83 | 0 | return true; |
84 | 889k | } |
85 | 889k | return scanner_ref->is_eos(); |
86 | 889k | }; |
87 | 889k | SimplifiedScanTask simple_scan_task = {work_func, ctx, scan_task}; |
88 | 889k | return this->submit_scan_task(simple_scan_task); |
89 | | }; |
90 | 889k | |
91 | 889k | Status submit_status = sumbit_task(); |
92 | | if (!submit_status.ok()) { |
93 | 0 | // User will see TooManyTasks error. It looks like a more reasonable error. |
94 | 0 | Status scan_task_status = Status::TooManyTasks( |
95 | 0 | "Failed to submit scanner to scanner pool reason:" + |
96 | 0 | std::string(submit_status.msg()) + "|type:" + std::to_string(type)); |
97 | 0 | scan_task->set_status(scan_task_status); |
98 | 0 | return scan_task_status; |
99 | | } |
100 | 889k | |
101 | 889k | return Status::OK(); |
102 | | } |
103 | | |
104 | 0 | void handle_reserve_memory_failure(RuntimeState* state, std::shared_ptr<ScannerContext> ctx, |
105 | 0 | const Status& st, size_t reserve_size) { |
106 | 0 | ctx->clear_free_blocks(); |
107 | | auto* local_state = ctx->local_state(); |
108 | 0 |
|
109 | 0 | auto debug_msg = fmt::format( |
110 | 0 | "Query: {} , scanner try to reserve: {}, operator name {}, " |
111 | 0 | "operator " |
112 | 0 | "id: {}, " |
113 | 0 | "task id: " |
114 | 0 | "{}, failed: {}", |
115 | 0 | print_id(state->query_id()), PrettyPrinter::print_bytes(reserve_size), |
116 | 0 | local_state->get_name(), local_state->parent()->node_id(), state->task_id(), |
117 | | st.to_string()); |
118 | 0 | // PROCESS_MEMORY_EXCEEDED error msg alread contains process_mem_log_str |
119 | 0 | if (!st.is<ErrorCode::PROCESS_MEMORY_EXCEEDED>()) { |
120 | 0 | debug_msg += fmt::format(", debug info: {}", GlobalMemoryArbitrator::process_mem_log_str()); |
121 | 0 | } |
122 | | VLOG_DEBUG << debug_msg; |
123 | 0 |
|
124 | 0 | state->get_query_ctx()->set_low_memory_mode(); |
125 | | } |
126 | | |
127 | 889k | void ScannerScheduler::_scanner_scan(std::shared_ptr<ScannerContext> ctx, |
128 | 889k | std::shared_ptr<ScanTask> scan_task) { |
129 | 889k | auto task_lock = ctx->task_exec_ctx(); |
130 | 0 | if (task_lock == nullptr) { |
131 | 0 | return; |
132 | 889k | } |
133 | | SCOPED_ATTACH_TASK(ctx->state()); |
134 | 889k | |
135 | 889k | ctx->update_peak_running_scanner(1); |
136 | | Defer defer([&] { ctx->update_peak_running_scanner(-1); }); |
137 | 889k | |
138 | 889k | std::shared_ptr<ScannerDelegate> scanner_delegate = scan_task->scanner.lock(); |
139 | 1 | if (scanner_delegate == nullptr) { |
140 | 1 | return; |
141 | | } |
142 | 889k | |
143 | | ScannerSPtr& scanner = scanner_delegate->_scanner; |
144 | 889k | // for cpu hard limit, thread name should not be reset |
145 | 0 | if (ctx->_should_reset_thread_name) { |
146 | 0 | Thread::set_self_name("_scanner_scan"); |
147 | | } |
148 | 889k | |
149 | | #ifndef __APPLE__ |
150 | | // The configuration item is used to lower the priority of the scanner thread, |
151 | 889k | // typically employed to ensure CPU scheduling for write operations. |
152 | 0 | if (config::scan_thread_nice_value != 0 && scanner->get_name() != FileScanner::NAME) { |
153 | 0 | Thread::set_thread_nice_value(); |
154 | 889k | } |
155 | | #endif |
156 | | |
157 | | // we set and get counter according below order, to make sure the counter is updated before get_block, and the time of get_block is recorded in the counter. |
158 | | // 1. update_wait_worker_timer to make sure the time of waiting for worker thread is recorded in the timer |
159 | | // 2. start_scan_cpu_timer to make sure the cpu timer include the time of open and get_block, which is the real cpu time of scanner |
160 | | // 3. update_scan_cpu_timer when defer, to make sure the cpu timer include the time of open and get_block, which is the real cpu time of scanner |
161 | | // 4. start_wait_worker_timer when defer, to make sure the time of waiting for worker thread is recorded in the timer |
162 | 889k | |
163 | 889k | MonotonicStopWatch max_run_time_watch; |
164 | 889k | max_run_time_watch.start(); |
165 | 889k | scanner->resume(); |
166 | | |
167 | 889k | bool need_update_profile = true; |
168 | 889k | auto update_scanner_profile = [&]() { |
169 | 882k | if (need_update_profile) { |
170 | 882k | scanner->pause(); |
171 | 882k | scanner->update_realtime_counters(); |
172 | 882k | need_update_profile = false; |
173 | 882k | } |
174 | 881k | }; |
175 | | |
176 | 889k | Status status = Status::OK(); |
177 | 889k | bool eos = false; |
178 | 889k | |
179 | 888k | ASSIGN_STATUS_IF_CATCH_EXCEPTION( |
180 | | RuntimeState* state = ctx->state(); DCHECK(nullptr != state); |
181 | 5.13k | // scanner->open may alloc plenty amount of memory(read blocks of data), |
182 | 5.13k | // so better to also check low memory and clear free blocks here. |
183 | 888k | if (ctx->low_memory_mode()) { ctx->clear_free_blocks(); } |
184 | | |
185 | 889k | if (!scanner->has_prepared()) { |
186 | 889k | status = scanner->prepare(); |
187 | | if (!status.ok()) { |
188 | | eos = true; |
189 | 889k | } |
190 | | } |
191 | 889k | |
192 | 889k | if (!eos && !scanner->is_open()) { |
193 | 889k | status = scanner->open(state); |
194 | 889k | if (!status.ok()) { |
195 | 889k | eos = true; |
196 | 889k | } |
197 | | scanner->set_opened(); |
198 | 889k | } |
199 | 889k | |
200 | 889k | Status rf_status = scanner->try_append_late_arrival_runtime_filter(); |
201 | 889k | if (!rf_status.ok()) { |
202 | 889k | LOG(WARNING) << "Failed to append late arrival runtime filter: " |
203 | 889k | << rf_status.to_string(); |
204 | 889k | } |
205 | | |
206 | 889k | size_t raw_bytes_threshold = config::doris_scanner_row_bytes; |
207 | 889k | if (ctx->low_memory_mode()) { |
208 | 889k | ctx->clear_free_blocks(); |
209 | 889k | if (raw_bytes_threshold > ctx->low_memory_mode_scan_bytes_per_scanner()) { |
210 | 889k | raw_bytes_threshold = ctx->low_memory_mode_scan_bytes_per_scanner(); |
211 | | } |
212 | 889k | } |
213 | 889k | |
214 | 889k | bool first_read = true; |
215 | 889k | int64_t limit = scanner->limit(); if (UNLIKELY(ctx->done())) { |
216 | 889k | eos = true; |
217 | 889k | } else if (ctx->remaining_limit() == 0) { eos = true; } else if (!eos) { |
218 | 889k | do { |
219 | | DEFER_RELEASE_RESERVED(); |
220 | 889k | BlockUPtr free_block; |
221 | 889k | if (first_read) { |
222 | | free_block = ctx->get_free_block(first_read); |
223 | 889k | } else { |
224 | | if (state->get_query_ctx() |
225 | | ->resource_ctx() |
226 | 889k | ->task_controller() |
227 | 889k | ->is_enable_reserve_memory()) { |
228 | 889k | size_t block_avg_bytes = scanner->get_block_avg_bytes(); |
229 | 889k | auto st = thread_context()->thread_mem_tracker_mgr->try_reserve( |
230 | 889k | block_avg_bytes); |
231 | 889k | if (!st.ok()) { |
232 | 889k | handle_reserve_memory_failure(state, ctx, st, block_avg_bytes); |
233 | 889k | break; |
234 | 889k | } |
235 | | } |
236 | 889k | free_block = ctx->get_free_block(first_read); |
237 | 889k | } |
238 | 889k | if (free_block == nullptr) { |
239 | 889k | break; |
240 | 889k | } |
241 | 889k | // We got a new created block or a reused block. |
242 | 889k | status = scanner->get_block_after_projects(state, free_block.get(), &eos); |
243 | 889k | first_read = false; |
244 | 889k | if (!status.ok()) { |
245 | 889k | LOG(WARNING) << "Scan thread read Scanner failed: " << status.to_string(); |
246 | 889k | break; |
247 | 889k | } |
248 | 889k | // Check column type only after block is read successfully. |
249 | 889k | // Or it may cause a crash when the block is not normal. |
250 | 889k | _make_sure_virtual_col_is_materialized(scanner, free_block.get()); |
251 | 889k | |
252 | 889k | // Shared limit quota: acquire rows from the context's shared pool. |
253 | 889k | // Discard or truncate the block if quota is exhausted. |
254 | 889k | if (free_block->rows() > 0) { |
255 | 889k | int64_t block_rows = free_block->rows(); |
256 | 889k | int64_t granted = ctx->acquire_limit_quota(block_rows); |
257 | 889k | if (granted == 0) { |
258 | 889k | // No quota remaining, discard this block and mark eos. |
259 | 889k | ctx->return_free_block(std::move(free_block)); |
260 | 889k | eos = true; |
261 | 889k | break; |
262 | 889k | } else if (granted < block_rows) { |
263 | 889k | // Partial quota: truncate block to granted rows and mark eos. |
264 | 889k | free_block->set_num_rows(granted); |
265 | 889k | eos = true; |
266 | | } |
267 | 889k | } |
268 | 889k | // Projection will truncate useless columns, makes block size change. |
269 | 889k | auto free_block_bytes = free_block->allocated_bytes(); |
270 | 889k | ctx->reestimated_block_mem_bytes(cast_set<int64_t>(free_block_bytes)); |
271 | 889k | DCHECK(scan_task->cached_block == nullptr); |
272 | 889k | ctx->inc_block_usage(free_block->allocated_bytes()); |
273 | | scan_task->cached_block = std::move(free_block); |
274 | | |
275 | 889k | // Per-scanner small-limit optimization: if limit is small (< batch_size), |
276 | | // return immediately instead of accumulating to raw_bytes_threshold. |
277 | | if (limit > 0 && limit < ctx->batch_size()) { |
278 | | break; |
279 | 889k | } |
280 | 889k | |
281 | 889k | if (scan_task->cached_block->rows() > 0) { |
282 | 889k | auto block_avg_bytes = (scan_task->cached_block->bytes() + |
283 | | scan_task->cached_block->rows() - 1) / |
284 | 889k | scan_task->cached_block->rows() * ctx->batch_size(); |
285 | 889k | scanner->update_block_avg_bytes(block_avg_bytes); |
286 | 889k | } |
287 | 889k | if (ctx->low_memory_mode()) { |
288 | | ctx->clear_free_blocks(); |
289 | 889k | } |
290 | 889k | } while (false); |
291 | 889k | } |
292 | 889k | |
293 | | if (UNLIKELY(!status.ok())) { |
294 | 889k | scan_task->set_status(status); |
295 | 889k | eos = true; |
296 | 889k | }, |
297 | 889k | status); |
298 | 889k | |
299 | 889k | if (UNLIKELY(!status.ok())) { |
300 | 889k | scan_task->set_status(status); |
301 | 889k | eos = true; |
302 | 889k | } |
303 | 889k | |
304 | 889k | if (eos) { |
305 | 889k | // If eos, scanner will call _collect_profile_before_close to update profile, |
306 | 889k | // so we need update_scanner_profile here |
307 | 889k | update_scanner_profile(); |
308 | 889k | scanner->mark_to_need_to_close(); |
309 | | scan_task->set_state(ScanTask::State::EOS); |
310 | | } else { |
311 | | scan_task->set_state(ScanTask::State::COMPLETED); |
312 | 889k | } |
313 | 889k | |
314 | 889k | VLOG_DEBUG << fmt::format( |
315 | 889k | "Scanner context {} has finished task, current scheduled task is " |
316 | 889k | "{}, eos: {}, status: {}", |
317 | 889k | ctx->ctx_id, ctx->num_scheduled_scanners(), eos, status.to_string()); |
318 | 889k | |
319 | 889k | ctx->push_back_scan_task(scan_task); |
320 | 889k | } |
321 | 889k | int ScannerScheduler::default_local_scan_thread_num() { |
322 | | return config::doris_scanner_thread_pool_thread_num > 0 |
323 | | ? config::doris_scanner_thread_pool_thread_num |
324 | | : std::max(48, CpuInfo::num_cores() * 2); |
325 | 889k | } |
326 | 889k | int ScannerScheduler::default_remote_scan_thread_num() { |
327 | 889k | int num = config::doris_max_remote_scanner_thread_pool_thread_num > 0 |
328 | | ? config::doris_max_remote_scanner_thread_pool_thread_num |
329 | 889k | : std::max(512, CpuInfo::num_cores() * 10); |
330 | 889k | return std::max(num, default_local_scan_thread_num()); |
331 | 889k | } |
332 | 889k | |
333 | 889k | int ScannerScheduler::get_remote_scan_thread_queue_size() { |
334 | 889k | return config::doris_remote_scanner_thread_pool_queue_size; |
335 | 889k | } |
336 | 889k | |
337 | 889k | int ScannerScheduler::default_min_active_scan_threads() { |
338 | 889k | return config::min_active_scan_threads > 0 |
339 | 889k | ? config::min_active_scan_threads |
340 | 889k | : config::min_active_scan_threads = CpuInfo::num_cores() * 2; |
341 | 889k | } |
342 | 889k | |
343 | | int ScannerScheduler::default_min_active_file_scan_threads() { |
344 | 889k | return config::min_active_file_scan_threads > 0 |
345 | 889k | ? config::min_active_file_scan_threads |
346 | 889k | : config::min_active_file_scan_threads = CpuInfo::num_cores() * 8; |
347 | 889k | } |
348 | 889k | |
349 | | void ScannerScheduler::_make_sure_virtual_col_is_materialized( |
350 | 887k | const std::shared_ptr<Scanner>& scanner, Block* free_block) { |
351 | 1.33k | #ifndef NDEBUG |
352 | 1.33k | // Currently, virtual column can only be used on olap table. |
353 | 1.33k | std::shared_ptr<OlapScanner> olap_scanner = std::dynamic_pointer_cast<OlapScanner>(scanner); |
354 | | if (olap_scanner == nullptr) { |
355 | 887k | return; |
356 | | } |
357 | | |
358 | 882k | if (free_block->rows() == 0) { |
359 | 882k | return; |
360 | 882k | } |
361 | 887k | |
362 | | size_t idx = 0; |
363 | 18.4E | for (const auto& entry : *free_block) { |
364 | 18.4E | // Virtual column must be materialized on the end of SegmentIterator's next batch method. |
365 | 18.4E | const ColumnNothing* column_nothing = |
366 | 18.4E | check_and_get_column<ColumnNothing>(entry.column.get()); |
367 | 18.4E | if (column_nothing == nullptr) { |
368 | | idx++; |
369 | 887k | continue; |
370 | 887k | } |
371 | 16.8k | |
372 | 16.8k | std::vector<std::string> vcid_to_idx; |
373 | 16.8k |
|
374 | 16.8k | for (const auto& pair : olap_scanner->_vir_cid_to_idx_in_block) { |
375 | 16.8k | vcid_to_idx.push_back(fmt::format("{}-{}", pair.first, pair.second)); |
376 | 11.1k | } |
377 | 11.1k | |
378 | 11.1k | std::string error_msg = fmt::format( |
379 | 11.1k | "Column in idx {} is nothing, block columns {}, normal_columns " |
380 | 11.1k | "{}, " |
381 | 11.1k | "vir_cid_to_idx_in_block_msg {}", |
382 | | idx, free_block->columns(), olap_scanner->_return_columns.size(), |
383 | 37 | fmt::format("_vir_cid_to_idx_in_block:[{}]", fmt::join(vcid_to_idx, ","))); |
384 | 37 | throw doris::Exception(ErrorCode::INTERNAL_ERROR, error_msg); |
385 | 37 | } |
386 | | #endif |
387 | 5.78k | } |
388 | 5.78k | |
389 | 5.78k | Result<SharedListenableFuture<Void>> ScannerSplitRunner::process_for(std::chrono::nanoseconds) { |
390 | 5.78k | _started = true; |
391 | 5.78k | bool is_completed = _scan_func(); |
392 | | if (is_completed) { |
393 | 5.78k | _completion_future.set_value(Void {}); |
394 | 5.78k | } |
395 | 5.78k | return SharedListenableFuture<Void>::create_ready(Void {}); |
396 | 5.78k | } |
397 | 5.78k | |
398 | | bool ScannerSplitRunner::is_finished() { |
399 | | return _completion_future.is_done(); |
400 | 928k | } |
401 | 928k | |
402 | | Status ScannerSplitRunner::finished_status() { |
403 | 928k | return _completion_future.get_status(); |
404 | 928k | } |
405 | 13.3k | |
406 | 13.3k | bool ScannerSplitRunner::is_started() const { |
407 | | return _started.load(); |
408 | 914k | } |
409 | 693k | |
410 | 693k | bool ScannerSplitRunner::is_auto_reschedule() const { |
411 | | return false; |
412 | 220k | } |
413 | 690k | |
414 | | } // namespace doris |