/root/doris/be/src/pipeline/pipeline_task.cpp
Line | Count | Source (jump to first uncovered line) |
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 "pipeline_task.h" |
19 | | |
20 | | #include <fmt/core.h> |
21 | | #include <fmt/format.h> |
22 | | #include <gen_cpp/Metrics_types.h> |
23 | | #include <glog/logging.h> |
24 | | |
25 | | #include <ostream> |
26 | | #include <vector> |
27 | | |
28 | | #include "common/logging.h" |
29 | | #include "common/status.h" |
30 | | #include "pipeline/dependency.h" |
31 | | #include "pipeline/exec/operator.h" |
32 | | #include "pipeline/exec/scan_operator.h" |
33 | | #include "pipeline/pipeline.h" |
34 | | #include "pipeline/pipeline_fragment_context.h" |
35 | | #include "pipeline/task_queue.h" |
36 | | #include "pipeline/task_scheduler.h" |
37 | | #include "runtime/descriptors.h" |
38 | | #include "runtime/exec_env.h" |
39 | | #include "runtime/query_context.h" |
40 | | #include "runtime/thread_context.h" |
41 | | #include "runtime/workload_group/workload_group_manager.h" |
42 | | #include "util/container_util.hpp" |
43 | | #include "util/defer_op.h" |
44 | | #include "util/mem_info.h" |
45 | | #include "util/runtime_profile.h" |
46 | | #include "util/uid_util.h" |
47 | | #include "vec/core/block.h" |
48 | | #include "vec/spill/spill_stream.h" |
49 | | |
50 | | namespace doris { |
51 | | class RuntimeState; |
52 | | } // namespace doris |
53 | | |
54 | | namespace doris::pipeline { |
55 | | |
56 | | PipelineTask::PipelineTask(PipelinePtr& pipeline, uint32_t task_id, RuntimeState* state, |
57 | | std::shared_ptr<PipelineFragmentContext> fragment_context, |
58 | | RuntimeProfile* parent_profile, |
59 | | std::map<int, std::pair<std::shared_ptr<BasicSharedState>, |
60 | | std::vector<std::shared_ptr<Dependency>>>> |
61 | | shared_state_map, |
62 | | int task_idx) |
63 | | : |
64 | | #ifdef BE_TEST |
65 | | _query_id(fragment_context ? fragment_context->get_query_id() : TUniqueId()), |
66 | | #else |
67 | | _query_id(fragment_context->get_query_id()), |
68 | | #endif |
69 | | _index(task_id), |
70 | | _pipeline(pipeline), |
71 | | _opened(false), |
72 | | _state(state), |
73 | | _fragment_context(fragment_context), |
74 | | _parent_profile(parent_profile), |
75 | | _operators(pipeline->operators()), |
76 | | _source(_operators.front().get()), |
77 | | _root(_operators.back().get()), |
78 | | _sink(pipeline->sink_shared_pointer()), |
79 | | _shared_state_map(std::move(shared_state_map)), |
80 | | _task_idx(task_idx), |
81 | | _execution_dep(state->get_query_ctx()->get_execution_dependency()), |
82 | | _memory_sufficient_dependency(state->get_query_ctx()->get_memory_sufficient_dependency()), |
83 | 80 | _pipeline_name(_pipeline->name()) { |
84 | 80 | _pipeline_task_watcher.start(); |
85 | | |
86 | 80 | if (!_shared_state_map.contains(_sink->dests_id().front())) { |
87 | 79 | auto shared_state = _sink->create_shared_state(); |
88 | 79 | if (shared_state) { |
89 | 26 | _sink_shared_state = shared_state; |
90 | 26 | } |
91 | 79 | } |
92 | 80 | } |
93 | | |
94 | | Status PipelineTask::prepare(const std::vector<TScanRangeParams>& scan_range, const int sender_id, |
95 | 14 | const TDataSink& tsink) { |
96 | 14 | DCHECK(_sink); |
97 | 14 | _init_profile(); |
98 | 14 | SCOPED_TIMER(_task_profile->total_time_counter()); |
99 | 14 | SCOPED_CPU_TIMER(_task_cpu_timer); |
100 | 14 | SCOPED_TIMER(_prepare_timer); |
101 | 14 | DBUG_EXECUTE_IF("fault_inject::PipelineXTask::prepare", { |
102 | 14 | Status status = Status::Error<INTERNAL_ERROR>("fault_inject pipeline_task prepare failed"); |
103 | 14 | return status; |
104 | 14 | }); |
105 | 14 | { |
106 | | // set sink local state |
107 | 14 | LocalSinkStateInfo info {_task_idx, _task_profile.get(), |
108 | 14 | sender_id, get_sink_shared_state().get(), |
109 | 14 | _shared_state_map, tsink}; |
110 | 14 | RETURN_IF_ERROR(_sink->setup_local_state(_state, info)); |
111 | 14 | } |
112 | | |
113 | 14 | _scan_ranges = scan_range; |
114 | 14 | auto* parent_profile = _state->get_sink_local_state()->profile(); |
115 | | |
116 | 30 | for (int op_idx = _operators.size() - 1; op_idx >= 0; op_idx--) { |
117 | 16 | auto& op = _operators[op_idx]; |
118 | 16 | LocalStateInfo info {parent_profile, _scan_ranges, get_op_shared_state(op->operator_id()), |
119 | 16 | _shared_state_map, _task_idx}; |
120 | 16 | RETURN_IF_ERROR(op->setup_local_state(_state, info)); |
121 | 16 | parent_profile = _state->get_local_state(op->operator_id())->profile(); |
122 | 16 | } |
123 | 14 | { |
124 | 14 | std::vector<Dependency*> filter_dependencies; |
125 | 14 | const auto& deps = _state->get_local_state(_source->operator_id())->filter_dependencies(); |
126 | 14 | std::copy(deps.begin(), deps.end(), |
127 | 14 | std::inserter(filter_dependencies, filter_dependencies.end())); |
128 | | |
129 | 14 | std::unique_lock<std::mutex> lc(_dependency_lock); |
130 | 14 | filter_dependencies.swap(_filter_dependencies); |
131 | 14 | } |
132 | 14 | if (auto fragment = _fragment_context.lock()) { |
133 | 13 | if (fragment->get_query_ctx()->is_cancelled()) { |
134 | 0 | terminate(); |
135 | 0 | return fragment->get_query_ctx()->exec_status(); |
136 | 0 | } |
137 | 13 | } else { |
138 | 1 | return Status::InternalError("Fragment already finished! Query: {}", print_id(_query_id)); |
139 | 1 | } |
140 | 13 | _block = doris::vectorized::Block::create_unique(); |
141 | 13 | return _state_transition(State::RUNNABLE); |
142 | 14 | } |
143 | | |
144 | 12 | Status PipelineTask::_extract_dependencies() { |
145 | 12 | std::vector<std::vector<Dependency*>> read_dependencies; |
146 | 12 | std::vector<Dependency*> write_dependencies; |
147 | 12 | std::vector<Dependency*> finish_dependencies; |
148 | 12 | std::vector<Dependency*> spill_dependencies; |
149 | 12 | read_dependencies.resize(_operators.size()); |
150 | 12 | size_t i = 0; |
151 | 14 | for (auto& op : _operators) { |
152 | 14 | auto result = _state->get_local_state_result(op->operator_id()); |
153 | 14 | if (!result) { |
154 | 1 | return result.error(); |
155 | 1 | } |
156 | 13 | auto* local_state = result.value(); |
157 | 13 | read_dependencies[i] = local_state->dependencies(); |
158 | 13 | auto* fin_dep = local_state->finishdependency(); |
159 | 13 | if (fin_dep) { |
160 | 6 | finish_dependencies.push_back(fin_dep); |
161 | 6 | } |
162 | 13 | if (auto* spill_dependency = local_state->spill_dependency()) { |
163 | 6 | spill_dependencies.push_back(spill_dependency); |
164 | 6 | } |
165 | 13 | i++; |
166 | 13 | } |
167 | 11 | DBUG_EXECUTE_IF("fault_inject::PipelineXTask::_extract_dependencies", { |
168 | 11 | Status status = Status::Error<INTERNAL_ERROR>( |
169 | 11 | "fault_inject pipeline_task _extract_dependencies failed"); |
170 | 11 | return status; |
171 | 11 | }); |
172 | 11 | { |
173 | 11 | auto* local_state = _state->get_sink_local_state(); |
174 | 11 | write_dependencies = local_state->dependencies(); |
175 | 11 | auto* fin_dep = local_state->finishdependency(); |
176 | 11 | if (fin_dep) { |
177 | 11 | finish_dependencies.push_back(fin_dep); |
178 | 11 | } |
179 | 11 | if (auto* spill_dependency = local_state->spill_dependency()) { |
180 | 6 | spill_dependencies.push_back(spill_dependency); |
181 | 6 | } |
182 | 11 | } |
183 | 11 | { |
184 | 11 | std::unique_lock<std::mutex> lc(_dependency_lock); |
185 | 11 | read_dependencies.swap(_read_dependencies); |
186 | 11 | write_dependencies.swap(_write_dependencies); |
187 | 11 | finish_dependencies.swap(_finish_dependencies); |
188 | 11 | spill_dependencies.swap(_spill_dependencies); |
189 | 11 | } |
190 | 11 | return Status::OK(); |
191 | 11 | } |
192 | | |
193 | 14 | void PipelineTask::_init_profile() { |
194 | 14 | _task_profile = |
195 | 14 | std::make_unique<RuntimeProfile>(fmt::format("PipelineTask (index={})", _index)); |
196 | 14 | _parent_profile->add_child(_task_profile.get(), true, nullptr); |
197 | 14 | _task_cpu_timer = ADD_TIMER(_task_profile, "TaskCpuTime"); |
198 | | |
199 | 14 | static const char* exec_time = "ExecuteTime"; |
200 | 14 | _exec_timer = ADD_TIMER(_task_profile, exec_time); |
201 | 14 | _prepare_timer = ADD_CHILD_TIMER(_task_profile, "PrepareTime", exec_time); |
202 | 14 | _open_timer = ADD_CHILD_TIMER(_task_profile, "OpenTime", exec_time); |
203 | 14 | _get_block_timer = ADD_CHILD_TIMER(_task_profile, "GetBlockTime", exec_time); |
204 | 14 | _get_block_counter = ADD_COUNTER(_task_profile, "GetBlockCounter", TUnit::UNIT); |
205 | 14 | _sink_timer = ADD_CHILD_TIMER(_task_profile, "SinkTime", exec_time); |
206 | 14 | _close_timer = ADD_CHILD_TIMER(_task_profile, "CloseTime", exec_time); |
207 | | |
208 | 14 | _wait_worker_timer = ADD_TIMER_WITH_LEVEL(_task_profile, "WaitWorkerTime", 1); |
209 | | |
210 | 14 | _schedule_counts = ADD_COUNTER(_task_profile, "NumScheduleTimes", TUnit::UNIT); |
211 | 14 | _yield_counts = ADD_COUNTER(_task_profile, "NumYieldTimes", TUnit::UNIT); |
212 | 14 | _core_change_times = ADD_COUNTER(_task_profile, "CoreChangeTimes", TUnit::UNIT); |
213 | 14 | _memory_reserve_times = ADD_COUNTER(_task_profile, "MemoryReserveTimes", TUnit::UNIT); |
214 | 14 | _memory_reserve_failed_times = |
215 | 14 | ADD_COUNTER(_task_profile, "MemoryReserveFailedTimes", TUnit::UNIT); |
216 | 14 | } |
217 | | |
218 | 6 | void PipelineTask::_fresh_profile_counter() { |
219 | 6 | COUNTER_SET(_schedule_counts, (int64_t)_schedule_time); |
220 | 6 | COUNTER_SET(_wait_worker_timer, (int64_t)_wait_worker_watcher.elapsed_time()); |
221 | 6 | } |
222 | | |
223 | 11 | Status PipelineTask::_open() { |
224 | 11 | SCOPED_TIMER(_task_profile->total_time_counter()); |
225 | 11 | SCOPED_CPU_TIMER(_task_cpu_timer); |
226 | 11 | SCOPED_TIMER(_open_timer); |
227 | 11 | _dry_run = _sink->should_dry_run(_state); |
228 | 13 | for (auto& o : _operators) { |
229 | 13 | RETURN_IF_ERROR(_state->get_local_state(o->operator_id())->open(_state)); |
230 | 13 | } |
231 | 11 | RETURN_IF_ERROR(_state->get_sink_local_state()->open(_state)); |
232 | 11 | RETURN_IF_ERROR(_extract_dependencies()); |
233 | 11 | DBUG_EXECUTE_IF("fault_inject::PipelineXTask::open", { |
234 | 11 | Status status = Status::Error<INTERNAL_ERROR>("fault_inject pipeline_task open failed"); |
235 | 11 | return status; |
236 | 11 | }); |
237 | 11 | _opened = true; |
238 | 11 | return Status::OK(); |
239 | 11 | } |
240 | | |
241 | 37 | bool PipelineTask::_wait_to_start() { |
242 | | // Before task starting, we should make sure |
243 | | // 1. Execution dependency is ready (which is controlled by FE 2-phase commit) |
244 | | // 2. Runtime filter dependencies are ready |
245 | 37 | return _execution_dep->is_blocked_by(shared_from_this()) || |
246 | 37 | std::any_of( |
247 | 31 | _filter_dependencies.begin(), _filter_dependencies.end(), |
248 | 31 | [&](Dependency* dep) -> bool { return dep->is_blocked_by(shared_from_this()); }); |
249 | 37 | } |
250 | | |
251 | 13 | bool PipelineTask::_is_pending_finish() { |
252 | | // Spilling may be in progress if eos is true. |
253 | 13 | return std::any_of(_spill_dependencies.begin(), _spill_dependencies.end(), |
254 | 13 | [&](Dependency* dep) -> bool { |
255 | 6 | return dep->is_blocked_by(shared_from_this()); |
256 | 6 | }) || |
257 | 13 | std::any_of( |
258 | 13 | _finish_dependencies.begin(), _finish_dependencies.end(), |
259 | 15 | [&](Dependency* dep) -> bool { return dep->is_blocked_by(shared_from_this()); }); |
260 | 13 | } |
261 | | |
262 | 993k | bool PipelineTask::_is_blocked() { |
263 | | // `_dry_run = true` means we do not need data from source operator. |
264 | 993k | if (!_dry_run) { |
265 | 1.98M | for (int i = _read_dependencies.size() - 1; i >= 0; i--) { |
266 | | // `_read_dependencies` is organized according to operators. For each operator, running condition is met iff all dependencies are ready. |
267 | 993k | for (auto* dep : _read_dependencies[i]) { |
268 | 993k | if (dep->is_blocked_by(shared_from_this())) { |
269 | 13 | return true; |
270 | 13 | } |
271 | 993k | } |
272 | | // If all dependencies are ready for this operator, we can execute this task if no datum is needed from upstream operators. |
273 | 993k | if (!_operators[i]->need_more_input_data(_state)) { |
274 | 2 | break; |
275 | 2 | } |
276 | 993k | } |
277 | 993k | } |
278 | 992k | return std::any_of(_spill_dependencies.begin(), _spill_dependencies.end(), |
279 | 1.98M | [&](Dependency* dep) -> bool { |
280 | 1.98M | return dep->is_blocked_by(shared_from_this()); |
281 | 1.98M | }) || |
282 | 992k | _memory_sufficient_dependency->is_blocked_by(shared_from_this()) || |
283 | 992k | std::any_of( |
284 | 992k | _write_dependencies.begin(), _write_dependencies.end(), |
285 | 992k | [&](Dependency* dep) -> bool { return dep->is_blocked_by(shared_from_this()); }); |
286 | 993k | } |
287 | | |
288 | 5 | void PipelineTask::terminate() { |
289 | | // We use a lock to assure all dependencies are not deconstructed here. |
290 | 5 | std::unique_lock<std::mutex> lc(_dependency_lock); |
291 | 5 | auto fragment = _fragment_context.lock(); |
292 | 5 | if (!is_finalized() && fragment) { |
293 | 5 | DCHECK(_wake_up_early || fragment->is_canceled()); |
294 | 5 | std::for_each(_spill_dependencies.begin(), _spill_dependencies.end(), |
295 | 10 | [&](Dependency* dep) { dep->set_always_ready(); }); |
296 | 5 | std::for_each(_filter_dependencies.begin(), _filter_dependencies.end(), |
297 | 5 | [&](Dependency* dep) { dep->set_always_ready(); }); |
298 | 5 | std::for_each(_write_dependencies.begin(), _write_dependencies.end(), |
299 | 5 | [&](Dependency* dep) { dep->set_always_ready(); }); |
300 | 5 | std::for_each(_finish_dependencies.begin(), _finish_dependencies.end(), |
301 | 10 | [&](Dependency* dep) { dep->set_always_ready(); }); |
302 | 5 | std::for_each(_read_dependencies.begin(), _read_dependencies.end(), |
303 | 5 | [&](std::vector<Dependency*>& deps) { |
304 | 5 | std::for_each(deps.begin(), deps.end(), |
305 | 5 | [&](Dependency* dep) { dep->set_always_ready(); }); |
306 | 5 | }); |
307 | 5 | _execution_dep->set_ready(); |
308 | 5 | _memory_sufficient_dependency->set_ready(); |
309 | 5 | } |
310 | 5 | } |
311 | | |
312 | | /** |
313 | | * `_eos` indicates whether the execution phase is done. `done` indicates whether we could close |
314 | | * this task. |
315 | | * |
316 | | * For example, |
317 | | * 1. if `_eos` is false which means we should continue to get next block so we cannot close (e.g. |
318 | | * `done` is false) |
319 | | * 2. if `_eos` is true which means all blocks from source are exhausted but `_is_pending_finish()` |
320 | | * is true which means we should wait for a pending dependency ready (maybe a running rpc), so we |
321 | | * cannot close (e.g. `done` is false) |
322 | | * 3. if `_eos` is true which means all blocks from source are exhausted and `_is_pending_finish()` |
323 | | * is false which means we can close immediately (e.g. `done` is true) |
324 | | * @param done |
325 | | * @return |
326 | | */ |
327 | 29 | Status PipelineTask::execute(bool* done) { |
328 | 29 | if (_exec_state != State::RUNNABLE || _blocked_dep != nullptr) [[unlikely]] { |
329 | 1 | return Status::InternalError("Pipeline task is not runnable! Task info: {}", |
330 | 1 | debug_string()); |
331 | 1 | } |
332 | 28 | auto fragment_context = _fragment_context.lock(); |
333 | 28 | DCHECK(fragment_context); |
334 | 28 | int64_t time_spent = 0; |
335 | 28 | ThreadCpuStopWatch cpu_time_stop_watch; |
336 | 28 | cpu_time_stop_watch.start(); |
337 | 28 | SCOPED_ATTACH_TASK(_state); |
338 | 28 | Defer running_defer {[&]() { |
339 | 28 | if (_task_queue) { |
340 | 28 | _task_queue->update_statistics(this, time_spent); |
341 | 28 | } |
342 | 28 | int64_t delta_cpu_time = cpu_time_stop_watch.elapsed_time(); |
343 | 28 | _task_cpu_timer->update(delta_cpu_time); |
344 | 28 | fragment_context->get_query_ctx()->resource_ctx()->cpu_context()->update_cpu_cost_ms( |
345 | 28 | delta_cpu_time); |
346 | | |
347 | | // If task is woke up early, we should terminate all operators, and this task could be closed immediately. |
348 | 28 | if (_wake_up_early) { |
349 | 3 | terminate(); |
350 | 3 | THROW_IF_ERROR(_root->terminate(_state)); |
351 | 3 | THROW_IF_ERROR(_sink->terminate(_state)); |
352 | 3 | _eos = true; |
353 | 3 | *done = true; |
354 | 25 | } else if (_eos && !_spilling && |
355 | 25 | (fragment_context->is_canceled() || !_is_pending_finish())) { |
356 | 7 | *done = true; |
357 | 7 | } |
358 | | // If this run is pended by a spilling request, the block will be output in next run. |
359 | 28 | if (!_spilling) { |
360 | 28 | _block->clear_column_data(_root->row_desc().num_materialized_slots()); |
361 | 28 | } |
362 | 28 | }}; |
363 | 28 | const auto query_id = _state->query_id(); |
364 | | // If this task is already EOS and block is empty (which means we already output all blocks), |
365 | | // just return here. |
366 | 28 | if (_eos && !_spilling) { |
367 | 1 | return Status::OK(); |
368 | 1 | } |
369 | | // If this task is blocked by a spilling request and waken up immediately, the spilling |
370 | | // dependency will not block this task and we should just run here. |
371 | 27 | if (!_block->empty()) { |
372 | 0 | LOG(INFO) << "Query: " << print_id(query_id) << " has pending block, size: " |
373 | 0 | << PrettyPrinter::print_bytes(_block->allocated_bytes()); |
374 | 0 | DCHECK(_spilling); |
375 | 0 | } |
376 | | |
377 | 27 | SCOPED_TIMER(_task_profile->total_time_counter()); |
378 | 27 | SCOPED_TIMER(_exec_timer); |
379 | | |
380 | 27 | DBUG_EXECUTE_IF("fault_inject::PipelineXTask::execute", { |
381 | 27 | Status status = Status::Error<INTERNAL_ERROR>("fault_inject pipeline_task execute failed"); |
382 | 27 | return status; |
383 | 27 | }); |
384 | | // `_wake_up_early` must be after `_wait_to_start()` |
385 | 27 | if (_wait_to_start() || _wake_up_early) { |
386 | 2 | if (config::enable_prefetch_tablet) { |
387 | 2 | RETURN_IF_ERROR(_source->hold_tablets(_state)); |
388 | 2 | } |
389 | 2 | return Status::OK(); |
390 | 2 | } |
391 | | |
392 | | // The status must be runnable |
393 | 25 | if (!_opened && !fragment_context->is_canceled()) { |
394 | 10 | DBUG_EXECUTE_IF("PipelineTask::execute.open_sleep", { |
395 | 10 | auto required_pipeline_id = |
396 | 10 | DebugPoints::instance()->get_debug_param_or_default<int32_t>( |
397 | 10 | "PipelineTask::execute.open_sleep", "pipeline_id", -1); |
398 | 10 | auto required_task_id = DebugPoints::instance()->get_debug_param_or_default<int32_t>( |
399 | 10 | "PipelineTask::execute.open_sleep", "task_id", -1); |
400 | 10 | if (required_pipeline_id == pipeline_id() && required_task_id == task_id()) { |
401 | 10 | LOG(WARNING) << "PipelineTask::execute.open_sleep sleep 5s"; |
402 | 10 | sleep(5); |
403 | 10 | } |
404 | 10 | }); |
405 | | |
406 | 10 | SCOPED_RAW_TIMER(&time_spent); |
407 | 10 | RETURN_IF_ERROR(_open()); |
408 | 10 | } |
409 | | |
410 | 992k | while (!fragment_context->is_canceled()) { |
411 | 992k | SCOPED_RAW_TIMER(&time_spent); |
412 | 992k | Defer defer {[&]() { |
413 | | // If this run is pended by a spilling request, the block will be output in next run. |
414 | 992k | if (!_spilling) { |
415 | 992k | _block->clear_column_data(_root->row_desc().num_materialized_slots()); |
416 | 992k | } |
417 | 992k | }}; |
418 | | // `_wake_up_early` must be after `_is_blocked()` |
419 | 992k | if (_is_blocked() || _wake_up_early) { |
420 | 15 | return Status::OK(); |
421 | 15 | } |
422 | | |
423 | | /// When a task is cancelled, |
424 | | /// its blocking state will be cleared and it will transition to a ready state (though it is not truly ready). |
425 | | /// Here, checking whether it is cancelled to prevent tasks in a blocking state from being re-executed. |
426 | 992k | if (fragment_context->is_canceled()) { |
427 | 0 | break; |
428 | 0 | } |
429 | | |
430 | 992k | if (time_spent > THREAD_TIME_SLICE) { |
431 | 1 | COUNTER_UPDATE(_yield_counts, 1); |
432 | 1 | break; |
433 | 1 | } |
434 | 992k | auto* block = _block.get(); |
435 | | |
436 | 992k | DBUG_EXECUTE_IF("fault_inject::PipelineXTask::executing", { |
437 | 992k | Status status = |
438 | 992k | Status::Error<INTERNAL_ERROR>("fault_inject pipeline_task executing failed"); |
439 | 992k | return status; |
440 | 992k | }); |
441 | | |
442 | | // `_sink->is_finished(_state)` means sink operator should be finished |
443 | 992k | if (_sink->is_finished(_state)) { |
444 | 1 | set_wake_up_early(); |
445 | 1 | return Status::OK(); |
446 | 1 | } |
447 | | |
448 | | // `_dry_run` means sink operator need no more data |
449 | 992k | _eos = _dry_run || _eos; |
450 | 992k | _spilling = false; |
451 | 992k | auto workload_group = _state->workload_group(); |
452 | | // If last run is pended by a spilling request, `_block` is produced with some rows in last |
453 | | // run, so we will resume execution using the block. |
454 | 992k | if (!_eos && _block->empty()) { |
455 | 992k | SCOPED_TIMER(_get_block_timer); |
456 | 992k | if (_state->low_memory_mode()) { |
457 | 1.48k | _sink->set_low_memory_mode(_state); |
458 | 1.48k | _root->set_low_memory_mode(_state); |
459 | 1.48k | } |
460 | 992k | DEFER_RELEASE_RESERVED(); |
461 | 992k | _get_block_counter->update(1); |
462 | 992k | const auto reserve_size = _root->get_reserve_mem_size(_state); |
463 | 992k | _root->reset_reserve_mem_size(_state); |
464 | | |
465 | 992k | if (workload_group && _state->get_query_ctx()->enable_reserve_memory() && |
466 | 992k | reserve_size > 0) { |
467 | 1.49k | auto st = thread_context()->thread_mem_tracker_mgr->try_reserve(reserve_size); |
468 | | |
469 | 1.49k | COUNTER_UPDATE(_memory_reserve_times, 1); |
470 | 1.49k | if (!st.ok() && !_state->enable_force_spill()) { |
471 | 1.49k | COUNTER_UPDATE(_memory_reserve_failed_times, 1); |
472 | 1.49k | auto sink_revokable_mem_size = _sink->revocable_mem_size(_state); |
473 | 1.49k | auto debug_msg = fmt::format( |
474 | 1.49k | "Query: {} , try to reserve: {}, operator name: {}, operator " |
475 | 1.49k | "id: {}, task id: {}, root revocable mem size: {}, sink revocable mem" |
476 | 1.49k | "size: {}, failed: {}", |
477 | 1.49k | print_id(query_id), PrettyPrinter::print_bytes(reserve_size), |
478 | 1.49k | _root->get_name(), _root->node_id(), _state->task_id(), |
479 | 1.49k | PrettyPrinter::print_bytes(_root->revocable_mem_size(_state)), |
480 | 1.49k | PrettyPrinter::print_bytes(sink_revokable_mem_size), st.to_string()); |
481 | | // PROCESS_MEMORY_EXCEEDED error msg alread contains process_mem_log_str |
482 | 1.49k | if (!st.is<ErrorCode::PROCESS_MEMORY_EXCEEDED>()) { |
483 | 1.49k | debug_msg += fmt::format(", debug info: {}", |
484 | 1.49k | GlobalMemoryArbitrator::process_mem_log_str()); |
485 | 1.49k | } |
486 | 1.49k | LOG_EVERY_N(INFO, 100) << debug_msg; |
487 | | // If sink has enough revocable memory, trigger revoke memory |
488 | 1.49k | if (sink_revokable_mem_size >= _state->spill_min_revocable_mem()) { |
489 | 0 | LOG(INFO) << fmt::format( |
490 | 0 | "Query: {} sink: {}, node id: {}, task id: " |
491 | 0 | "{}, revocable mem size: {}", |
492 | 0 | print_id(query_id), _sink->get_name(), _sink->node_id(), |
493 | 0 | _state->task_id(), |
494 | 0 | PrettyPrinter::print_bytes(sink_revokable_mem_size)); |
495 | 0 | ExecEnv::GetInstance()->workload_group_mgr()->add_paused_query( |
496 | 0 | _state->get_query_ctx()->shared_from_this(), reserve_size, st); |
497 | 0 | continue; |
498 | 1.49k | } else { |
499 | | // If reserve failed, not add this query to paused list, because it is very small, will not |
500 | | // consume a lot of memory. But need set low memory mode to indicate that the system should |
501 | | // not use too much memory. |
502 | 1.49k | _state->get_query_ctx()->set_low_memory_mode(); |
503 | 1.49k | } |
504 | 1.49k | } |
505 | 1.49k | } |
506 | | |
507 | 992k | bool eos = false; |
508 | 992k | RETURN_IF_ERROR(_root->get_block_after_projects(_state, block, &eos)); |
509 | 992k | _eos = eos; |
510 | 992k | } |
511 | | |
512 | 992k | if (!_block->empty() || _eos) { |
513 | 14 | SCOPED_TIMER(_sink_timer); |
514 | 14 | Status status = Status::OK(); |
515 | 14 | DEFER_RELEASE_RESERVED(); |
516 | 14 | COUNTER_UPDATE(_memory_reserve_times, 1); |
517 | 14 | if (_state->get_query_ctx()->enable_reserve_memory() && workload_group && |
518 | 14 | !(_wake_up_early || _dry_run)) { |
519 | 1 | const auto sink_reserve_size = _sink->get_reserve_mem_size(_state, _eos); |
520 | 1 | status = sink_reserve_size != 0 |
521 | 1 | ? thread_context()->thread_mem_tracker_mgr->try_reserve( |
522 | 1 | sink_reserve_size) |
523 | 1 | : Status::OK(); |
524 | | |
525 | 1 | auto sink_revocable_mem_size = _sink->revocable_mem_size(_state); |
526 | 1 | if (status.ok() && _state->enable_force_spill() && _sink->is_spillable() && |
527 | 1 | sink_revocable_mem_size >= vectorized::SpillStream::MIN_SPILL_WRITE_BATCH_MEM) { |
528 | 0 | status = Status(ErrorCode::QUERY_MEMORY_EXCEEDED, "Force Spill"); |
529 | 0 | } |
530 | | |
531 | 1 | if (!status.ok()) { |
532 | 1 | COUNTER_UPDATE(_memory_reserve_failed_times, 1); |
533 | 1 | auto debug_msg = fmt::format( |
534 | 1 | "Query: {} try to reserve: {}, sink name: {}, node id: {}, task " |
535 | 1 | "id: " |
536 | 1 | "{}, sink revocable mem size: {}, failed: {}", |
537 | 1 | print_id(query_id), PrettyPrinter::print_bytes(sink_reserve_size), |
538 | 1 | _sink->get_name(), _sink->node_id(), _state->task_id(), |
539 | 1 | PrettyPrinter::print_bytes(sink_revocable_mem_size), |
540 | 1 | status.to_string()); |
541 | | // PROCESS_MEMORY_EXCEEDED error msg alread contains process_mem_log_str |
542 | 1 | if (!status.is<ErrorCode::PROCESS_MEMORY_EXCEEDED>()) { |
543 | 1 | debug_msg += fmt::format(", debug info: {}", |
544 | 1 | GlobalMemoryArbitrator::process_mem_log_str()); |
545 | 1 | } |
546 | | // If the operator is not spillable or it is spillable but not has much memory to spill |
547 | | // not need add to paused list, just let it go. |
548 | 1 | if (sink_revocable_mem_size >= |
549 | 1 | vectorized::SpillStream::MIN_SPILL_WRITE_BATCH_MEM) { |
550 | 0 | VLOG_DEBUG << debug_msg; |
551 | 0 | ExecEnv::GetInstance()->workload_group_mgr()->add_paused_query( |
552 | 0 | _state->get_query_ctx()->shared_from_this(), sink_reserve_size, |
553 | 0 | status); |
554 | 0 | _spilling = true; |
555 | 0 | continue; |
556 | 1 | } else { |
557 | 1 | _state->get_query_ctx()->set_low_memory_mode(); |
558 | 1 | } |
559 | 1 | } |
560 | 1 | } |
561 | | |
562 | 14 | if (_eos) { |
563 | 8 | RETURN_IF_ERROR(close(Status::OK(), false)); |
564 | 8 | } |
565 | | |
566 | 14 | DBUG_EXECUTE_IF("PipelineTask::execute.sink_eos_sleep", { |
567 | 14 | auto required_pipeline_id = |
568 | 14 | DebugPoints::instance()->get_debug_param_or_default<int32_t>( |
569 | 14 | "PipelineTask::execute.sink_eos_sleep", "pipeline_id", -1); |
570 | 14 | auto required_task_id = |
571 | 14 | DebugPoints::instance()->get_debug_param_or_default<int32_t>( |
572 | 14 | "PipelineTask::execute.sink_eos_sleep", "task_id", -1); |
573 | 14 | if (required_pipeline_id == pipeline_id() && required_task_id == task_id()) { |
574 | 14 | LOG(WARNING) << "PipelineTask::execute.sink_eos_sleep sleep 10s"; |
575 | 14 | sleep(10); |
576 | 14 | } |
577 | 14 | }); |
578 | | |
579 | 14 | DBUG_EXECUTE_IF("PipelineTask::execute.terminate", { |
580 | 14 | if (_eos) { |
581 | 14 | auto required_pipeline_id = |
582 | 14 | DebugPoints::instance()->get_debug_param_or_default<int32_t>( |
583 | 14 | "PipelineTask::execute.terminate", "pipeline_id", -1); |
584 | 14 | auto required_task_id = |
585 | 14 | DebugPoints::instance()->get_debug_param_or_default<int32_t>( |
586 | 14 | "PipelineTask::execute.terminate", "task_id", -1); |
587 | 14 | auto required_fragment_id = |
588 | 14 | DebugPoints::instance()->get_debug_param_or_default<int32_t>( |
589 | 14 | "PipelineTask::execute.terminate", "fragment_id", -1); |
590 | 14 | if (required_pipeline_id == pipeline_id() && required_task_id == task_id() && |
591 | 14 | fragment_context->get_fragment_id() == required_fragment_id) { |
592 | 14 | _wake_up_early = true; |
593 | 14 | terminate(); |
594 | 14 | } else if (required_pipeline_id == pipeline_id() && |
595 | 14 | fragment_context->get_fragment_id() == required_fragment_id) { |
596 | 14 | LOG(WARNING) << "PipelineTask::execute.terminate sleep 5s"; |
597 | 14 | sleep(5); |
598 | 14 | } |
599 | 14 | } |
600 | 14 | }); |
601 | 14 | status = _sink->sink(_state, block, _eos); |
602 | | |
603 | 14 | if (status.is<ErrorCode::END_OF_FILE>()) { |
604 | 1 | set_wake_up_early(); |
605 | 1 | return Status::OK(); |
606 | 13 | } else if (!status) { |
607 | 0 | return status; |
608 | 0 | } |
609 | | |
610 | 13 | if (_eos) { // just return, the scheduler will do finish work |
611 | 7 | return Status::OK(); |
612 | 7 | } |
613 | 13 | } |
614 | 992k | } |
615 | | |
616 | 1 | RETURN_IF_ERROR(get_task_queue()->push_back(shared_from_this())); |
617 | 1 | return Status::OK(); |
618 | 1 | } |
619 | | |
620 | 425k | void PipelineTask::stop_if_finished() { |
621 | 425k | auto fragment = _fragment_context.lock(); |
622 | 425k | if (!fragment) { |
623 | 0 | return; |
624 | 0 | } |
625 | 425k | SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(fragment->get_query_ctx()->query_mem_tracker()); |
626 | 425k | if (auto sink = _sink) { |
627 | 425k | if (sink->is_finished(_state)) { |
628 | 1 | set_wake_up_early(); |
629 | 1 | terminate(); |
630 | 1 | } |
631 | 425k | } |
632 | 425k | } |
633 | | |
634 | 1 | Status PipelineTask::finalize() { |
635 | 1 | auto fragment = _fragment_context.lock(); |
636 | 1 | if (!fragment) { |
637 | 0 | return Status::OK(); |
638 | 0 | } |
639 | 1 | SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(fragment->get_query_ctx()->query_mem_tracker()); |
640 | 1 | std::unique_lock<std::mutex> lc(_dependency_lock); |
641 | 1 | RETURN_IF_ERROR(_state_transition(State::FINALIZED)); |
642 | 1 | _sink_shared_state.reset(); |
643 | 1 | _op_shared_states.clear(); |
644 | 1 | _shared_state_map.clear(); |
645 | 1 | _block.reset(); |
646 | 1 | _operators.clear(); |
647 | 1 | _sink.reset(); |
648 | 1 | _pipeline.reset(); |
649 | 1 | return Status::OK(); |
650 | 1 | } |
651 | | |
652 | 14 | Status PipelineTask::close(Status exec_status, bool close_sink) { |
653 | 14 | int64_t close_ns = 0; |
654 | 14 | Status s; |
655 | 14 | { |
656 | 14 | SCOPED_RAW_TIMER(&close_ns); |
657 | 14 | if (close_sink) { |
658 | 6 | s = _sink->close(_state, exec_status); |
659 | 6 | } |
660 | 18 | for (auto& op : _operators) { |
661 | 18 | auto tem = op->close(_state); |
662 | 18 | if (!tem.ok() && s.ok()) { |
663 | 0 | s = tem; |
664 | 0 | } |
665 | 18 | } |
666 | 14 | } |
667 | 14 | if (_opened) { |
668 | 14 | COUNTER_UPDATE(_close_timer, close_ns); |
669 | 14 | COUNTER_UPDATE(_task_profile->total_time_counter(), close_ns); |
670 | 14 | } |
671 | | |
672 | 14 | if (close_sink && _opened) { |
673 | 6 | _task_profile->add_info_string("WakeUpEarly", std::to_string(_wake_up_early.load())); |
674 | 6 | _fresh_profile_counter(); |
675 | 6 | } |
676 | | |
677 | 14 | if (_task_queue) { |
678 | 14 | _task_queue->update_statistics(this, close_ns); |
679 | 14 | } |
680 | 14 | if (close_sink) { |
681 | 6 | RETURN_IF_ERROR(_state_transition(State::FINISHED)); |
682 | 6 | } |
683 | 14 | return s; |
684 | 14 | } |
685 | | |
686 | 18.3k | std::string PipelineTask::debug_string() { |
687 | 18.3k | fmt::memory_buffer debug_string_buffer; |
688 | | |
689 | 18.3k | fmt::format_to(debug_string_buffer, "QueryId: {}\n", print_id(_query_id)); |
690 | 18.3k | fmt::format_to(debug_string_buffer, "InstanceId: {}\n", |
691 | 18.3k | print_id(_state->fragment_instance_id())); |
692 | | |
693 | 18.3k | fmt::format_to(debug_string_buffer, |
694 | 18.3k | "PipelineTask[id = {}, open = {}, eos = {}, state = {}, dry run = " |
695 | 18.3k | "{}, _wake_up_early = {}, time elapsed since last state changing = {}s, spilling" |
696 | 18.3k | " = {}, is running = {}]", |
697 | 18.3k | _index, _opened, _eos, _to_string(_exec_state), _dry_run, _wake_up_early.load(), |
698 | 18.3k | _state_change_watcher.elapsed_time() / NANOS_PER_SEC, _spilling, is_running()); |
699 | 18.3k | std::unique_lock<std::mutex> lc(_dependency_lock); |
700 | 18.3k | auto* cur_blocked_dep = _blocked_dep; |
701 | 18.3k | auto fragment = _fragment_context.lock(); |
702 | 18.3k | if (is_finalized() || !fragment) { |
703 | 5 | fmt::format_to(debug_string_buffer, " pipeline name = {}", _pipeline_name); |
704 | 5 | return fmt::to_string(debug_string_buffer); |
705 | 5 | } |
706 | 18.3k | auto elapsed = fragment->elapsed_time() / NANOS_PER_SEC; |
707 | 18.3k | fmt::format_to(debug_string_buffer, |
708 | 18.3k | " elapse time = {}s, block dependency = [{}]\noperators: ", elapsed, |
709 | 18.3k | cur_blocked_dep && !is_finalized() ? cur_blocked_dep->debug_string() : "NULL"); |
710 | | |
711 | 36.6k | for (size_t i = 0; i < _operators.size(); i++) { |
712 | 18.3k | fmt::format_to(debug_string_buffer, "\n{}", |
713 | 18.3k | _opened && !is_finalized() ? _operators[i]->debug_string(_state, i) |
714 | 18.3k | : _operators[i]->debug_string(i)); |
715 | 18.3k | } |
716 | 18.3k | fmt::format_to(debug_string_buffer, "\n{}\n", |
717 | 18.3k | _opened && !is_finalized() ? _sink->debug_string(_state, _operators.size()) |
718 | 18.3k | : _sink->debug_string(_operators.size())); |
719 | | |
720 | 18.3k | fmt::format_to(debug_string_buffer, "\nRead Dependency Information: \n"); |
721 | | |
722 | 18.3k | size_t i = 0; |
723 | 36.5k | for (; i < _read_dependencies.size(); i++) { |
724 | 36.5k | for (size_t j = 0; j < _read_dependencies[i].size(); j++) { |
725 | 18.2k | fmt::format_to(debug_string_buffer, "{}. {}\n", i, |
726 | 18.2k | _read_dependencies[i][j]->debug_string(i + 1)); |
727 | 18.2k | } |
728 | 18.2k | } |
729 | | |
730 | 18.3k | fmt::format_to(debug_string_buffer, "{}. {}\n", i, |
731 | 18.3k | _memory_sufficient_dependency->debug_string(i++)); |
732 | | |
733 | 18.3k | fmt::format_to(debug_string_buffer, "\nWrite Dependency Information: \n"); |
734 | 36.5k | for (size_t j = 0; j < _write_dependencies.size(); j++, i++) { |
735 | 18.2k | fmt::format_to(debug_string_buffer, "{}. {}\n", i, |
736 | 18.2k | _write_dependencies[j]->debug_string(i + 1)); |
737 | 18.2k | } |
738 | | |
739 | 18.3k | fmt::format_to(debug_string_buffer, "\nRuntime Filter Dependency Information: \n"); |
740 | 36.6k | for (size_t j = 0; j < _filter_dependencies.size(); j++, i++) { |
741 | 18.3k | fmt::format_to(debug_string_buffer, "{}. {}\n", i, |
742 | 18.3k | _filter_dependencies[j]->debug_string(i + 1)); |
743 | 18.3k | } |
744 | | |
745 | 18.3k | fmt::format_to(debug_string_buffer, "\nSpill Dependency Information: \n"); |
746 | 54.8k | for (size_t j = 0; j < _spill_dependencies.size(); j++, i++) { |
747 | 36.5k | fmt::format_to(debug_string_buffer, "{}. {}\n", i, |
748 | 36.5k | _spill_dependencies[j]->debug_string(i + 1)); |
749 | 36.5k | } |
750 | | |
751 | 18.3k | fmt::format_to(debug_string_buffer, "Finish Dependency Information: \n"); |
752 | 54.8k | for (size_t j = 0; j < _finish_dependencies.size(); j++, i++) { |
753 | 36.5k | fmt::format_to(debug_string_buffer, "{}. {}\n", i, |
754 | 36.5k | _finish_dependencies[j]->debug_string(j + 1)); |
755 | 36.5k | } |
756 | 18.3k | return fmt::to_string(debug_string_buffer); |
757 | 18.3k | } |
758 | | |
759 | 0 | size_t PipelineTask::get_revocable_size() const { |
760 | 0 | if (is_finalized() || _running || (_eos && !_spilling)) { |
761 | 0 | return 0; |
762 | 0 | } |
763 | | |
764 | 0 | return _sink->revocable_mem_size(_state); |
765 | 0 | } |
766 | | |
767 | 0 | Status PipelineTask::revoke_memory(const std::shared_ptr<SpillContext>& spill_context) { |
768 | 0 | if (is_finalized()) { |
769 | 0 | if (spill_context) { |
770 | 0 | spill_context->on_task_finished(); |
771 | 0 | VLOG_DEBUG << "Query: " << print_id(_state->query_id()) << ", task: " << ((void*)this) |
772 | 0 | << " finalized"; |
773 | 0 | } |
774 | 0 | return Status::OK(); |
775 | 0 | } |
776 | | |
777 | 0 | const auto revocable_size = _sink->revocable_mem_size(_state); |
778 | 0 | if (revocable_size >= vectorized::SpillStream::MIN_SPILL_WRITE_BATCH_MEM) { |
779 | 0 | RETURN_IF_ERROR(_sink->revoke_memory(_state, spill_context)); |
780 | 0 | } else if (spill_context) { |
781 | 0 | spill_context->on_task_finished(); |
782 | 0 | LOG(INFO) << "Query: " << print_id(_state->query_id()) << ", task: " << ((void*)this) |
783 | 0 | << " has not enough data to revoke: " << revocable_size; |
784 | 0 | } |
785 | 0 | return Status::OK(); |
786 | 0 | } |
787 | | |
788 | 22 | Status PipelineTask::wake_up(Dependency* dep) { |
789 | | // call by dependency |
790 | 22 | DCHECK_EQ(_blocked_dep, dep) << "dep : " << dep->debug_string(0) << "task: " << debug_string(); |
791 | 22 | _blocked_dep = nullptr; |
792 | 22 | auto holder = std::dynamic_pointer_cast<PipelineTask>(shared_from_this()); |
793 | 22 | RETURN_IF_ERROR(_state_transition(PipelineTask::State::RUNNABLE)); |
794 | 22 | RETURN_IF_ERROR(get_task_queue()->push_back(holder)); |
795 | 22 | return Status::OK(); |
796 | 22 | } |
797 | | |
798 | 89 | Status PipelineTask::_state_transition(State new_state) { |
799 | 89 | if (_exec_state != new_state) { |
800 | 84 | _state_change_watcher.reset(); |
801 | 84 | _state_change_watcher.start(); |
802 | 84 | } |
803 | 89 | _task_profile->add_info_string("TaskState", _to_string(new_state)); |
804 | 89 | _task_profile->add_info_string("BlockedByDependency", _blocked_dep ? _blocked_dep->name() : ""); |
805 | 89 | if (!LEGAL_STATE_TRANSITION[(int)new_state].contains(_exec_state)) { |
806 | 17 | return Status::InternalError( |
807 | 17 | "Task state transition from {} to {} is not allowed! Task info: {}", |
808 | 17 | _to_string(_exec_state), _to_string(new_state), debug_string()); |
809 | 17 | } |
810 | 72 | _exec_state = new_state; |
811 | 72 | return Status::OK(); |
812 | 89 | } |
813 | | |
814 | | } // namespace doris::pipeline |