/root/doris/be/src/pipeline/pipeline_task.h
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 | | #pragma once |
19 | | |
20 | | #include <cstdint> |
21 | | #include <memory> |
22 | | #include <string> |
23 | | #include <vector> |
24 | | |
25 | | #include "common/status.h" |
26 | | #include "pipeline/dependency.h" |
27 | | #include "pipeline/exec/operator.h" |
28 | | #include "pipeline/exec/spill_utils.h" |
29 | | #include "pipeline/pipeline.h" |
30 | | #include "util/runtime_profile.h" |
31 | | #include "util/stopwatch.hpp" |
32 | | #include "vec/core/block.h" |
33 | | |
34 | | namespace doris { |
35 | | class QueryContext; |
36 | | class RuntimeState; |
37 | | namespace pipeline { |
38 | | class PipelineFragmentContext; |
39 | | } // namespace pipeline |
40 | | } // namespace doris |
41 | | |
42 | | namespace doris::pipeline { |
43 | | |
44 | | class MultiCoreTaskQueue; |
45 | | class PriorityTaskQueue; |
46 | | class Dependency; |
47 | | |
48 | | class PipelineTask : public std::enable_shared_from_this<PipelineTask> { |
49 | | public: |
50 | | PipelineTask(PipelinePtr& pipeline, uint32_t task_id, RuntimeState* state, |
51 | | std::shared_ptr<PipelineFragmentContext> fragment_context, |
52 | | RuntimeProfile* parent_profile, |
53 | | std::map<int, std::pair<std::shared_ptr<BasicSharedState>, |
54 | | std::vector<std::shared_ptr<Dependency>>>> |
55 | | shared_state_map, |
56 | | int task_idx); |
57 | | |
58 | | ~PipelineTask(); |
59 | | |
60 | | Status prepare(const std::vector<TScanRangeParams>& scan_range, const int sender_id, |
61 | | const TDataSink& tsink); |
62 | | |
63 | | Status execute(bool* done); |
64 | | |
65 | | // if the pipeline create a bunch of pipeline task |
66 | | // must be call after all pipeline task is finish to release resource |
67 | | Status close(Status exec_status, bool close_sink = true); |
68 | | |
69 | 0 | std::weak_ptr<PipelineFragmentContext>& fragment_context() { return _fragment_context; } |
70 | | |
71 | 26 | int get_thread_id(int num_threads) const { |
72 | 26 | return _thread_id == -1 ? _thread_id : _thread_id % num_threads; |
73 | 26 | } |
74 | | |
75 | 0 | PipelineTask& set_thread_id(int thread_id) { |
76 | 0 | _thread_id = thread_id; |
77 | 0 | COUNTER_UPDATE(_core_change_times, 1); |
78 | 0 | return *this; |
79 | 0 | } |
80 | | |
81 | | Status finalize(); |
82 | | |
83 | | std::string debug_string(); |
84 | | |
85 | 0 | std::shared_ptr<BasicSharedState> get_source_shared_state() { |
86 | 0 | return _op_shared_states.contains(_source->operator_id()) |
87 | 0 | ? _op_shared_states[_source->operator_id()] |
88 | 0 | : nullptr; |
89 | 0 | } |
90 | | |
91 | | /** |
92 | | * Pipeline task is blockable means it will be blocked in the next run. So we should put it into |
93 | | * the blocking task scheduler. |
94 | | */ |
95 | | bool is_blockable() const; |
96 | | |
97 | | /** |
98 | | * `shared_state` is shared by different pipeline tasks. This function aims to establish |
99 | | * connections across related tasks. |
100 | | * |
101 | | * There are 2 kinds of relationships to share state by tasks. |
102 | | * 1. For regular operators, for example, Aggregation, we use the AggSinkOperator to create a |
103 | | * shared state and then inject it into downstream task which contains the corresponding |
104 | | * AggSourceOperator. |
105 | | * 2. For multiple-sink-single-source operator, for example, Set operations, the shared state is |
106 | | * created once and shared by multiple sink operators and single source operator. For this |
107 | | * case, we use the first sink operator create shared state and then inject into all of other |
108 | | * tasks. |
109 | | */ |
110 | | bool inject_shared_state(std::shared_ptr<BasicSharedState> shared_state); |
111 | | |
112 | 17 | std::shared_ptr<BasicSharedState> get_sink_shared_state() { return _sink_shared_state; } |
113 | | |
114 | 17 | BasicSharedState* get_op_shared_state(int id) { |
115 | 17 | if (!_op_shared_states.contains(id)) { |
116 | 15 | return nullptr; |
117 | 15 | } |
118 | 2 | return _op_shared_states[id].get(); |
119 | 17 | } |
120 | | |
121 | | Status wake_up(Dependency* dep); |
122 | | |
123 | 0 | DataSinkOperatorPtr sink() const { return _sink; } |
124 | | |
125 | 0 | int task_id() const { return _index; }; |
126 | 37.8k | bool is_finalized() const { return _exec_state == State::FINALIZED; } |
127 | | |
128 | 5 | void set_wake_up_early() { _wake_up_early = true; } |
129 | | |
130 | | // Execution phase should be terminated. This is called if this task is canceled or waken up early. |
131 | | void terminate(); |
132 | | |
133 | | // 1 used for update priority queue |
134 | | // note(wb) an ugly implementation, need refactor later |
135 | | // 1.1 pipeline task |
136 | 0 | void inc_runtime_ns(uint64_t delta_time) { this->_runtime += delta_time; } |
137 | 26 | uint64_t get_runtime_ns() const { return this->_runtime; } |
138 | | |
139 | | // 1.2 priority queue's queue level |
140 | 10 | void update_queue_level(int queue_level) { this->_queue_level = queue_level; } |
141 | 0 | int get_queue_level() const { return this->_queue_level; } |
142 | | |
143 | 26 | void put_in_runnable_queue() { |
144 | 26 | _schedule_time++; |
145 | 26 | _wait_worker_watcher.start(); |
146 | 26 | } |
147 | | |
148 | 10 | void pop_out_runnable_queue() { _wait_worker_watcher.stop(); } |
149 | | |
150 | 12.6k | bool is_running() { return _running.load(); } |
151 | 0 | PipelineTask& set_running(bool running) { |
152 | 0 | _running.exchange(running); |
153 | 0 | return *this; |
154 | 0 | } |
155 | | |
156 | 0 | RuntimeState* runtime_state() const { return _state; } |
157 | | |
158 | 0 | std::string task_name() const { return fmt::format("task{}({})", _index, _pipeline->_name); } |
159 | | |
160 | | // TODO: Maybe we do not need this safe code anymore |
161 | | void stop_if_finished(); |
162 | | |
163 | 0 | PipelineId pipeline_id() const { return _pipeline->id(); } |
164 | | [[nodiscard]] size_t get_revocable_size() const; |
165 | | [[nodiscard]] Status revoke_memory(const std::shared_ptr<SpillContext>& spill_context); |
166 | | |
167 | 23 | Status blocked(Dependency* dependency) { |
168 | 23 | DCHECK_EQ(_blocked_dep, nullptr) << "task: " << debug_string(); |
169 | 23 | _blocked_dep = dependency; |
170 | 23 | return _state_transition(PipelineTask::State::BLOCKED); |
171 | 23 | } |
172 | | |
173 | | private: |
174 | | // Whether this task is blocked before execution (FE 2-phase commit trigger, runtime filters) |
175 | | bool _wait_to_start(); |
176 | | // Whether this task is blocked during execution (read dependency, write dependency) |
177 | | bool _is_blocked(); |
178 | | // Whether this task is blocked after execution (pending finish dependency) |
179 | | bool _is_pending_finish(); |
180 | | |
181 | | Status _extract_dependencies(); |
182 | | void _init_profile(); |
183 | | void _fresh_profile_counter(); |
184 | | Status _open(); |
185 | | Status _prepare(); |
186 | | |
187 | | // Operator `op` try to reserve memory before executing. Return false if reserve failed |
188 | | // otherwise return true. |
189 | | bool _try_to_reserve_memory(const size_t reserve_size, OperatorBase* op); |
190 | | |
191 | | const TUniqueId _query_id; |
192 | | const uint32_t _index; |
193 | | PipelinePtr _pipeline; |
194 | | bool _opened; |
195 | | RuntimeState* _state = nullptr; |
196 | | int _thread_id = -1; |
197 | | uint32_t _schedule_time = 0; |
198 | | std::unique_ptr<vectorized::Block> _block; |
199 | | |
200 | | std::weak_ptr<PipelineFragmentContext> _fragment_context; |
201 | | |
202 | | // used for priority queue |
203 | | // it may be visited by different thread but there is no race condition |
204 | | // so no need to add lock |
205 | | uint64_t _runtime = 0; |
206 | | // it's visited in one thread, so no need to thread synchronization |
207 | | // 1 get task, (set _queue_level/_core_id) |
208 | | // 2 exe task |
209 | | // 3 update task statistics(update _queue_level/_core_id) |
210 | | int _queue_level = 0; |
211 | | |
212 | | bool _need_to_revoke_memory = false; |
213 | | std::shared_ptr<SpillContext> _spill_context; |
214 | | |
215 | | RuntimeProfile* _parent_profile = nullptr; |
216 | | std::unique_ptr<RuntimeProfile> _task_profile; |
217 | | RuntimeProfile::Counter* _task_cpu_timer = nullptr; |
218 | | RuntimeProfile::Counter* _prepare_timer = nullptr; |
219 | | RuntimeProfile::Counter* _open_timer = nullptr; |
220 | | RuntimeProfile::Counter* _exec_timer = nullptr; |
221 | | RuntimeProfile::Counter* _get_block_timer = nullptr; |
222 | | RuntimeProfile::Counter* _get_block_counter = nullptr; |
223 | | RuntimeProfile::Counter* _sink_timer = nullptr; |
224 | | RuntimeProfile::Counter* _close_timer = nullptr; |
225 | | RuntimeProfile::Counter* _schedule_counts = nullptr; |
226 | | MonotonicStopWatch _wait_worker_watcher; |
227 | | RuntimeProfile::Counter* _wait_worker_timer = nullptr; |
228 | | // TODO we should calculate the time between when really runnable and runnable |
229 | | RuntimeProfile::Counter* _yield_counts = nullptr; |
230 | | RuntimeProfile::Counter* _core_change_times = nullptr; |
231 | | RuntimeProfile::Counter* _memory_reserve_times = nullptr; |
232 | | RuntimeProfile::Counter* _memory_reserve_failed_times = nullptr; |
233 | | |
234 | | Operators _operators; // left is _source, right is _root |
235 | | OperatorXBase* _source; |
236 | | OperatorXBase* _root; |
237 | | DataSinkOperatorPtr _sink; |
238 | | |
239 | | // `_read_dependencies` is stored as same order as `_operators` |
240 | | std::vector<std::vector<Dependency*>> _read_dependencies; |
241 | | std::vector<Dependency*> _write_dependencies; |
242 | | std::vector<Dependency*> _finish_dependencies; |
243 | | std::vector<Dependency*> _execution_dependencies; |
244 | | |
245 | | // All shared states of this pipeline task. |
246 | | std::map<int, std::shared_ptr<BasicSharedState>> _op_shared_states; |
247 | | std::shared_ptr<BasicSharedState> _sink_shared_state; |
248 | | std::vector<TScanRangeParams> _scan_ranges; |
249 | | std::map<int, |
250 | | std::pair<std::shared_ptr<BasicSharedState>, std::vector<std::shared_ptr<Dependency>>>> |
251 | | _shared_state_map; |
252 | | int _task_idx; |
253 | | bool _dry_run = false; |
254 | | MOCK_REMOVE(const) |
255 | | unsigned long long _exec_time_slice = config::pipeline_task_exec_time_slice * NANOS_PER_MILLIS; |
256 | | Dependency* _blocked_dep = nullptr; |
257 | | |
258 | | Dependency* _memory_sufficient_dependency; |
259 | | std::mutex _dependency_lock; |
260 | | |
261 | | std::atomic<bool> _running {false}; |
262 | | std::atomic<bool> _eos {false}; |
263 | | std::atomic<bool> _wake_up_early {false}; |
264 | | // PipelineTask maybe hold by TaskQueue |
265 | | std::shared_ptr<MemTrackerLimiter> _query_mem_tracker; |
266 | | |
267 | | /** |
268 | | * |
269 | | * INITED -----> RUNNABLE -------------------------+----> FINISHED ---+---> FINALIZED |
270 | | * ^ | | |
271 | | * | | | |
272 | | * +----------- BLOCKED <--------+------------------+ |
273 | | */ |
274 | | enum class State : int { |
275 | | INITED, |
276 | | RUNNABLE, |
277 | | BLOCKED, |
278 | | FINISHED, |
279 | | FINALIZED, |
280 | | }; |
281 | | const std::vector<std::set<State>> LEGAL_STATE_TRANSITION = { |
282 | | {}, // Target state is INITED |
283 | | {State::INITED, State::RUNNABLE, State::BLOCKED}, // Target state is RUNNABLE |
284 | | {State::RUNNABLE, State::FINISHED}, // Target state is BLOCKED |
285 | | {State::RUNNABLE}, // Target state is FINISHED |
286 | | {State::INITED, State::FINISHED}}; // Target state is FINALIZED |
287 | | |
288 | 12.7k | std::string _to_string(State state) const { |
289 | 12.7k | switch (state) { |
290 | 17 | case State::INITED: |
291 | 17 | return "INITED"; |
292 | 12.6k | case State::RUNNABLE: |
293 | 12.6k | return "RUNNABLE"; |
294 | 39 | case State::BLOCKED: |
295 | 39 | return "BLOCKED"; |
296 | 21 | case State::FINISHED: |
297 | 21 | return "FINISHED"; |
298 | 19 | case State::FINALIZED: |
299 | 19 | return "FINALIZED"; |
300 | 0 | default: |
301 | 0 | __builtin_unreachable(); |
302 | 12.7k | } |
303 | 12.7k | } |
304 | | |
305 | | Status _state_transition(State new_state); |
306 | | std::atomic<State> _exec_state = State::INITED; |
307 | | MonotonicStopWatch _state_change_watcher; |
308 | | std::atomic<bool> _spilling = false; |
309 | | const std::string _pipeline_name; |
310 | | }; |
311 | | |
312 | | using PipelineTaskSPtr = std::shared_ptr<PipelineTask>; |
313 | | |
314 | | } // namespace doris::pipeline |