/root/doris/be/src/pipeline/dependency.h
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 | | #pragma once |
19 | | |
20 | | #include <concurrentqueue.h> |
21 | | #include <sqltypes.h> |
22 | | |
23 | | #include <atomic> |
24 | | #include <functional> |
25 | | #include <memory> |
26 | | #include <mutex> |
27 | | #include <thread> |
28 | | #include <utility> |
29 | | |
30 | | #include "common/config.h" |
31 | | #include "common/logging.h" |
32 | | #include "gen_cpp/internal_service.pb.h" |
33 | | #include "gutil/integral_types.h" |
34 | | #include "pipeline/common/agg_utils.h" |
35 | | #include "pipeline/common/join_utils.h" |
36 | | #include "pipeline/common/set_utils.h" |
37 | | #include "pipeline/exec/data_queue.h" |
38 | | #include "pipeline/exec/join/process_hash_table_probe.h" |
39 | | #include "util/brpc_closure.h" |
40 | | #include "util/stack_util.h" |
41 | | #include "vec/common/sort/partition_sorter.h" |
42 | | #include "vec/common/sort/sorter.h" |
43 | | #include "vec/core/block.h" |
44 | | #include "vec/core/types.h" |
45 | | #include "vec/spill/spill_stream.h" |
46 | | |
47 | | namespace doris::vectorized { |
48 | | class AggFnEvaluator; |
49 | | class VSlotRef; |
50 | | } // namespace doris::vectorized |
51 | | |
52 | | namespace doris::pipeline { |
53 | | #include "common/compile_check_begin.h" |
54 | | class Dependency; |
55 | | class PipelineTask; |
56 | | struct BasicSharedState; |
57 | | using DependencySPtr = std::shared_ptr<Dependency>; |
58 | | class LocalExchangeSourceLocalState; |
59 | | |
60 | | static constexpr auto SLOW_DEPENDENCY_THRESHOLD = 60 * 1000L * 1000L * 1000L; |
61 | | static constexpr auto TIME_UNIT_DEPENDENCY_LOG = 30 * 1000L * 1000L * 1000L; |
62 | | static_assert(TIME_UNIT_DEPENDENCY_LOG < SLOW_DEPENDENCY_THRESHOLD); |
63 | | |
64 | | struct BasicSharedState { |
65 | | ENABLE_FACTORY_CREATOR(BasicSharedState) |
66 | | |
67 | | template <class TARGET> |
68 | 104k | TARGET* cast() { |
69 | 104k | DCHECK(dynamic_cast<TARGET*>(this)) |
70 | 0 | << " Mismatch type! Current type is " << typeid(*this).name() |
71 | 0 | << " and expect type is" << typeid(TARGET).name(); |
72 | 104k | return reinterpret_cast<TARGET*>(this); |
73 | 104k | } Unexecuted instantiation: _ZN5doris8pipeline16BasicSharedState4castINS0_26MaterializationSharedStateEEEPT_v _ZN5doris8pipeline16BasicSharedState4castINS0_19HashJoinSharedStateEEEPT_v Line | Count | Source | 68 | 104k | TARGET* cast() { | 69 | 104k | DCHECK(dynamic_cast<TARGET*>(this)) | 70 | 0 | << " Mismatch type! Current type is " << typeid(*this).name() | 71 | 0 | << " and expect type is" << typeid(TARGET).name(); | 72 | 104k | return reinterpret_cast<TARGET*>(this); | 73 | 104k | } |
_ZN5doris8pipeline16BasicSharedState4castINS0_30PartitionedHashJoinSharedStateEEEPT_v Line | Count | Source | 68 | 3 | TARGET* cast() { | 69 | 3 | DCHECK(dynamic_cast<TARGET*>(this)) | 70 | 0 | << " Mismatch type! Current type is " << typeid(*this).name() | 71 | 0 | << " and expect type is" << typeid(TARGET).name(); | 72 | 3 | return reinterpret_cast<TARGET*>(this); | 73 | 3 | } |
_ZN5doris8pipeline16BasicSharedState4castINS0_15SortSharedStateEEEPT_v Line | Count | Source | 68 | 34 | TARGET* cast() { | 69 | 34 | DCHECK(dynamic_cast<TARGET*>(this)) | 70 | 0 | << " Mismatch type! Current type is " << typeid(*this).name() | 71 | 0 | << " and expect type is" << typeid(TARGET).name(); | 72 | 34 | return reinterpret_cast<TARGET*>(this); | 73 | 34 | } |
_ZN5doris8pipeline16BasicSharedState4castINS0_20SpillSortSharedStateEEEPT_v Line | Count | Source | 68 | 13 | TARGET* cast() { | 69 | 13 | DCHECK(dynamic_cast<TARGET*>(this)) | 70 | 0 | << " Mismatch type! Current type is " << typeid(*this).name() | 71 | 0 | << " and expect type is" << typeid(TARGET).name(); | 72 | 13 | return reinterpret_cast<TARGET*>(this); | 73 | 13 | } |
Unexecuted instantiation: _ZN5doris8pipeline16BasicSharedState4castINS0_25NestedLoopJoinSharedStateEEEPT_v _ZN5doris8pipeline16BasicSharedState4castINS0_19AnalyticSharedStateEEEPT_v Line | Count | Source | 68 | 18 | TARGET* cast() { | 69 | 18 | DCHECK(dynamic_cast<TARGET*>(this)) | 70 | 0 | << " Mismatch type! Current type is " << typeid(*this).name() | 71 | 0 | << " and expect type is" << typeid(TARGET).name(); | 72 | 18 | return reinterpret_cast<TARGET*>(this); | 73 | 18 | } |
_ZN5doris8pipeline16BasicSharedState4castINS0_14AggSharedStateEEEPT_v Line | Count | Source | 68 | 70 | TARGET* cast() { | 69 | 70 | DCHECK(dynamic_cast<TARGET*>(this)) | 70 | 0 | << " Mismatch type! Current type is " << typeid(*this).name() | 71 | 0 | << " and expect type is" << typeid(TARGET).name(); | 72 | 70 | return reinterpret_cast<TARGET*>(this); | 73 | 70 | } |
_ZN5doris8pipeline16BasicSharedState4castINS0_25PartitionedAggSharedStateEEEPT_v Line | Count | Source | 68 | 16 | TARGET* cast() { | 69 | 16 | DCHECK(dynamic_cast<TARGET*>(this)) | 70 | 0 | << " Mismatch type! Current type is " << typeid(*this).name() | 71 | 0 | << " and expect type is" << typeid(TARGET).name(); | 72 | 16 | return reinterpret_cast<TARGET*>(this); | 73 | 16 | } |
_ZN5doris8pipeline16BasicSharedState4castINS0_16UnionSharedStateEEEPT_v Line | Count | Source | 68 | 4 | TARGET* cast() { | 69 | 4 | DCHECK(dynamic_cast<TARGET*>(this)) | 70 | 0 | << " Mismatch type! Current type is " << typeid(*this).name() | 71 | 0 | << " and expect type is" << typeid(TARGET).name(); | 72 | 4 | return reinterpret_cast<TARGET*>(this); | 73 | 4 | } |
_ZN5doris8pipeline16BasicSharedState4castINS0_28PartitionSortNodeSharedStateEEEPT_v Line | Count | Source | 68 | 204 | TARGET* cast() { | 69 | 204 | DCHECK(dynamic_cast<TARGET*>(this)) | 70 | 0 | << " Mismatch type! Current type is " << typeid(*this).name() | 71 | 0 | << " and expect type is" << typeid(TARGET).name(); | 72 | 204 | return reinterpret_cast<TARGET*>(this); | 73 | 204 | } |
Unexecuted instantiation: _ZN5doris8pipeline16BasicSharedState4castINS0_20MultiCastSharedStateEEEPT_v _ZN5doris8pipeline16BasicSharedState4castINS0_14SetSharedStateEEEPT_v Line | Count | Source | 68 | 33 | TARGET* cast() { | 69 | 33 | DCHECK(dynamic_cast<TARGET*>(this)) | 70 | 0 | << " Mismatch type! Current type is " << typeid(*this).name() | 71 | 0 | << " and expect type is" << typeid(TARGET).name(); | 72 | 33 | return reinterpret_cast<TARGET*>(this); | 73 | 33 | } |
Unexecuted instantiation: _ZN5doris8pipeline16BasicSharedState4castINS0_24LocalExchangeSharedStateEEEPT_v _ZN5doris8pipeline16BasicSharedState4castIS1_EEPT_v Line | Count | Source | 68 | 10 | TARGET* cast() { | 69 | 10 | DCHECK(dynamic_cast<TARGET*>(this)) | 70 | 0 | << " Mismatch type! Current type is " << typeid(*this).name() | 71 | 0 | << " and expect type is" << typeid(TARGET).name(); | 72 | 10 | return reinterpret_cast<TARGET*>(this); | 73 | 10 | } |
_ZN5doris8pipeline16BasicSharedState4castINS0_20DataQueueSharedStateEEEPT_v Line | Count | Source | 68 | 6 | TARGET* cast() { | 69 | 6 | DCHECK(dynamic_cast<TARGET*>(this)) | 70 | 0 | << " Mismatch type! Current type is " << typeid(*this).name() | 71 | 0 | << " and expect type is" << typeid(TARGET).name(); | 72 | 6 | return reinterpret_cast<TARGET*>(this); | 73 | 6 | } |
|
74 | | template <class TARGET> |
75 | | const TARGET* cast() const { |
76 | | DCHECK(dynamic_cast<const TARGET*>(this)) |
77 | | << " Mismatch type! Current type is " << typeid(*this).name() |
78 | | << " and expect type is" << typeid(TARGET).name(); |
79 | | return reinterpret_cast<const TARGET*>(this); |
80 | | } |
81 | | std::vector<DependencySPtr> source_deps; |
82 | | std::vector<DependencySPtr> sink_deps; |
83 | | int id = 0; |
84 | | std::set<int> related_op_ids; |
85 | | |
86 | 78.3k | virtual ~BasicSharedState() = default; |
87 | | |
88 | | void create_source_dependencies(int num_sources, int operator_id, int node_id, |
89 | | const std::string& name); |
90 | | virtual Dependency* create_source_dependency(int operator_id, int node_id, |
91 | | const std::string& name); |
92 | | |
93 | | Dependency* create_sink_dependency(int dest_id, int node_id, const std::string& name); |
94 | 24 | std::vector<DependencySPtr> get_dep_by_channel_id(int channel_id) { |
95 | 24 | DCHECK_LT(channel_id, source_deps.size()); |
96 | 24 | return {source_deps[channel_id]}; |
97 | 24 | } |
98 | | }; |
99 | | |
100 | | class Dependency : public std::enable_shared_from_this<Dependency> { |
101 | | public: |
102 | | ENABLE_FACTORY_CREATOR(Dependency); |
103 | | Dependency(int id, int node_id, std::string name, bool ready = false) |
104 | 392k | : _id(id), _node_id(node_id), _name(std::move(name)), _ready(ready) {} |
105 | 392k | virtual ~Dependency() = default; |
106 | | |
107 | 1 | [[nodiscard]] int id() const { return _id; } |
108 | 104k | [[nodiscard]] virtual std::string name() const { return _name; } |
109 | 4 | BasicSharedState* shared_state() { return _shared_state; } |
110 | 156k | void set_shared_state(BasicSharedState* shared_state) { _shared_state = shared_state; } |
111 | | virtual std::string debug_string(int indentation_level = 0); |
112 | 708M | bool ready() const { return _ready; } |
113 | | |
114 | | // Start the watcher. We use it to count how long this dependency block the current pipeline task. |
115 | 25 | void start_watcher() { _watcher.start(); } |
116 | 104k | [[nodiscard]] int64_t watcher_elapse_time() { return _watcher.elapsed_time(); } |
117 | | |
118 | | // Which dependency current pipeline task is blocked by. `nullptr` if this dependency is ready. |
119 | | [[nodiscard]] Dependency* is_blocked_by(std::shared_ptr<PipelineTask> task = nullptr); |
120 | | // Notify downstream pipeline tasks this dependency is ready. |
121 | | void set_ready(); |
122 | 52.6k | void set_ready_to_read(int channel_id = 0) { |
123 | 52.6k | DCHECK_LT(channel_id, _shared_state->source_deps.size()) << debug_string(); |
124 | 52.6k | _shared_state->source_deps[channel_id]->set_ready(); |
125 | 52.6k | } |
126 | 0 | void set_ready_to_write() { |
127 | 0 | DCHECK_EQ(_shared_state->sink_deps.size(), 1) << debug_string(); |
128 | 0 | _shared_state->sink_deps.front()->set_ready(); |
129 | 0 | } |
130 | | |
131 | | // Notify downstream pipeline tasks this dependency is blocked. |
132 | 1.91k | void block() { |
133 | 1.91k | if (_always_ready) { |
134 | 11 | return; |
135 | 11 | } |
136 | 1.90k | std::unique_lock<std::mutex> lc(_always_ready_lock); |
137 | 1.90k | if (_always_ready) { |
138 | 0 | return; |
139 | 0 | } |
140 | 1.90k | _ready = false; |
141 | 1.90k | } |
142 | | |
143 | 147 | void set_always_ready() { |
144 | 147 | if (_always_ready) { |
145 | 38 | return; |
146 | 38 | } |
147 | 109 | std::unique_lock<std::mutex> lc(_always_ready_lock); |
148 | 109 | if (_always_ready) { |
149 | 0 | return; |
150 | 0 | } |
151 | 109 | _always_ready = true; |
152 | 109 | set_ready(); |
153 | 109 | } |
154 | | |
155 | | protected: |
156 | | void _add_block_task(std::shared_ptr<PipelineTask> task); |
157 | | |
158 | | const int _id; |
159 | | const int _node_id; |
160 | | const std::string _name; |
161 | | std::atomic<bool> _ready; |
162 | | |
163 | | BasicSharedState* _shared_state = nullptr; |
164 | | MonotonicStopWatch _watcher; |
165 | | |
166 | | std::mutex _task_lock; |
167 | | std::vector<std::weak_ptr<PipelineTask>> _blocked_task; |
168 | | |
169 | | // If `_always_ready` is true, `block()` will never block tasks. |
170 | | std::atomic<bool> _always_ready = false; |
171 | | std::mutex _always_ready_lock; |
172 | | }; |
173 | | |
174 | | struct FakeSharedState final : public BasicSharedState { |
175 | | ENABLE_FACTORY_CREATOR(FakeSharedState) |
176 | | }; |
177 | | |
178 | | class CountedFinishDependency final : public Dependency { |
179 | | public: |
180 | | using SharedState = FakeSharedState; |
181 | | CountedFinishDependency(int id, int node_id, std::string name) |
182 | 78.0k | : Dependency(id, node_id, std::move(name), true) {} |
183 | | |
184 | 8 | void add(uint32_t count = 1) { |
185 | 8 | std::unique_lock<std::mutex> l(_mtx); |
186 | 8 | if (!_counter) { |
187 | 7 | block(); |
188 | 7 | } |
189 | 8 | _counter += count; |
190 | 8 | } |
191 | | |
192 | 6 | void sub() { |
193 | 6 | std::unique_lock<std::mutex> l(_mtx); |
194 | 6 | _counter--; |
195 | 6 | if (!_counter) { |
196 | 5 | set_ready(); |
197 | 5 | } |
198 | 6 | } |
199 | | |
200 | | std::string debug_string(int indentation_level = 0) override; |
201 | | |
202 | | private: |
203 | | std::mutex _mtx; |
204 | | uint32_t _counter = 0; |
205 | | }; |
206 | | |
207 | | struct RuntimeFilterTimerQueue; |
208 | | class RuntimeFilterTimer { |
209 | | public: |
210 | | RuntimeFilterTimer(int64_t registration_time, int32_t wait_time_ms, |
211 | | std::shared_ptr<Dependency> parent, bool force_wait_timeout = false) |
212 | | : _parent(std::move(parent)), |
213 | | _registration_time(registration_time), |
214 | | _wait_time_ms(wait_time_ms), |
215 | 2 | _force_wait_timeout(force_wait_timeout) {} |
216 | | |
217 | | // Called by runtime filter producer. |
218 | | void call_ready(); |
219 | | |
220 | | // Called by RuntimeFilterTimerQueue which is responsible for checking if this rf is timeout. |
221 | | void call_timeout(); |
222 | | |
223 | 2 | int64_t registration_time() const { return _registration_time; } |
224 | 2 | int32_t wait_time_ms() const { return _wait_time_ms; } |
225 | | |
226 | | void set_local_runtime_filter_dependencies( |
227 | 0 | const std::vector<std::shared_ptr<Dependency>>& deps) { |
228 | 0 | _local_runtime_filter_dependencies = deps; |
229 | 0 | } |
230 | | |
231 | | bool should_be_check_timeout(); |
232 | | |
233 | 2 | bool force_wait_timeout() { return _force_wait_timeout; } |
234 | | |
235 | | private: |
236 | | friend struct RuntimeFilterTimerQueue; |
237 | | std::shared_ptr<Dependency> _parent = nullptr; |
238 | | std::vector<std::shared_ptr<Dependency>> _local_runtime_filter_dependencies; |
239 | | std::mutex _lock; |
240 | | int64_t _registration_time; |
241 | | const int32_t _wait_time_ms; |
242 | | // true only for group_commit_scan_operator |
243 | | bool _force_wait_timeout; |
244 | | }; |
245 | | |
246 | | struct RuntimeFilterTimerQueue { |
247 | | constexpr static int64_t interval = 10; |
248 | 1 | void run() { _thread.detach(); } |
249 | | void start(); |
250 | | |
251 | 0 | void stop() { |
252 | 0 | _stop = true; |
253 | 0 | cv.notify_all(); |
254 | 0 | wait_for_shutdown(); |
255 | 0 | } |
256 | | |
257 | 0 | void wait_for_shutdown() const { |
258 | 0 | while (!_shutdown) { |
259 | 0 | std::this_thread::sleep_for(std::chrono::milliseconds(interval)); |
260 | 0 | } |
261 | 0 | } |
262 | | |
263 | 0 | ~RuntimeFilterTimerQueue() = default; |
264 | 1 | RuntimeFilterTimerQueue() { _thread = std::thread(&RuntimeFilterTimerQueue::start, this); } |
265 | 1 | void push_filter_timer(std::vector<std::shared_ptr<pipeline::RuntimeFilterTimer>>&& filter) { |
266 | 1 | std::unique_lock<std::mutex> lc(_que_lock); |
267 | 1 | _que.insert(_que.end(), filter.begin(), filter.end()); |
268 | 1 | cv.notify_all(); |
269 | 1 | } |
270 | | |
271 | | std::thread _thread; |
272 | | std::condition_variable cv; |
273 | | std::mutex cv_m; |
274 | | std::mutex _que_lock; |
275 | | std::atomic_bool _stop = false; |
276 | | std::atomic_bool _shutdown = false; |
277 | | std::list<std::shared_ptr<pipeline::RuntimeFilterTimer>> _que; |
278 | | }; |
279 | | |
280 | | struct AggSharedState : public BasicSharedState { |
281 | | ENABLE_FACTORY_CREATOR(AggSharedState) |
282 | | public: |
283 | 40 | AggSharedState() { |
284 | 40 | agg_data = std::make_unique<AggregatedDataVariants>(); |
285 | 40 | agg_arena_pool = std::make_unique<vectorized::Arena>(); |
286 | 40 | } |
287 | 40 | ~AggSharedState() override { |
288 | 40 | if (!probe_expr_ctxs.empty()) { |
289 | 30 | _close_with_serialized_key(); |
290 | 30 | } else { |
291 | 10 | _close_without_key(); |
292 | 10 | } |
293 | 40 | } |
294 | | |
295 | | Status reset_hash_table(); |
296 | | |
297 | | bool do_limit_filter(vectorized::Block* block, size_t num_rows, |
298 | | const std::vector<int>* key_locs = nullptr); |
299 | | void build_limit_heap(size_t hash_table_size); |
300 | | |
301 | | // We should call this function only at 1st phase. |
302 | | // 1st phase: is_merge=true, only have one SlotRef. |
303 | | // 2nd phase: is_merge=false, maybe have multiple exprs. |
304 | | static int get_slot_column_id(const vectorized::AggFnEvaluator* evaluator); |
305 | | |
306 | | AggregatedDataVariantsUPtr agg_data = nullptr; |
307 | | std::unique_ptr<AggregateDataContainer> aggregate_data_container; |
308 | | ArenaUPtr agg_arena_pool; |
309 | | std::vector<vectorized::AggFnEvaluator*> aggregate_evaluators; |
310 | | // group by k1,k2 |
311 | | vectorized::VExprContextSPtrs probe_expr_ctxs; |
312 | | size_t input_num_rows = 0; |
313 | | std::vector<vectorized::AggregateDataPtr> values; |
314 | | /// The total size of the row from the aggregate functions. |
315 | | size_t total_size_of_aggregate_states = 0; |
316 | | size_t align_aggregate_states = 1; |
317 | | /// The offset to the n-th aggregate function in a row of aggregate functions. |
318 | | vectorized::Sizes offsets_of_aggregate_states; |
319 | | std::vector<size_t> make_nullable_keys; |
320 | | |
321 | | bool agg_data_created_without_key = false; |
322 | | bool enable_spill = false; |
323 | | bool reach_limit = false; |
324 | | |
325 | | int64_t limit = -1; |
326 | | bool do_sort_limit = false; |
327 | | vectorized::MutableColumns limit_columns; |
328 | | int limit_columns_min = -1; |
329 | | vectorized::PaddedPODArray<uint8_t> need_computes; |
330 | | std::vector<uint8_t> cmp_res; |
331 | | std::vector<int> order_directions; |
332 | | std::vector<int> null_directions; |
333 | | |
334 | | struct HeapLimitCursor { |
335 | | HeapLimitCursor(int row_id, vectorized::MutableColumns& limit_columns, |
336 | | std::vector<int>& order_directions, std::vector<int>& null_directions) |
337 | | : _row_id(row_id), |
338 | | _limit_columns(limit_columns), |
339 | | _order_directions(order_directions), |
340 | 32 | _null_directions(null_directions) {} |
341 | | |
342 | | HeapLimitCursor(const HeapLimitCursor& other) = default; |
343 | | |
344 | | HeapLimitCursor(HeapLimitCursor&& other) noexcept |
345 | | : _row_id(other._row_id), |
346 | | _limit_columns(other._limit_columns), |
347 | | _order_directions(other._order_directions), |
348 | 152 | _null_directions(other._null_directions) {} |
349 | | |
350 | 0 | HeapLimitCursor& operator=(const HeapLimitCursor& other) noexcept { |
351 | 0 | _row_id = other._row_id; |
352 | 0 | return *this; |
353 | 0 | } |
354 | | |
355 | 129 | HeapLimitCursor& operator=(HeapLimitCursor&& other) noexcept { |
356 | 129 | _row_id = other._row_id; |
357 | 129 | return *this; |
358 | 129 | } |
359 | | |
360 | 79 | bool operator<(const HeapLimitCursor& rhs) const { |
361 | 85 | for (int i = 0; i < _limit_columns.size(); ++i) { |
362 | 79 | const auto& _limit_column = _limit_columns[i]; |
363 | 79 | auto res = _limit_column->compare_at(_row_id, rhs._row_id, *_limit_column, |
364 | 79 | _null_directions[i]) * |
365 | 79 | _order_directions[i]; |
366 | 79 | if (res < 0) { |
367 | 46 | return true; |
368 | 46 | } else if (res > 0) { |
369 | 27 | return false; |
370 | 27 | } |
371 | 79 | } |
372 | 6 | return false; |
373 | 79 | } |
374 | | |
375 | | int _row_id; |
376 | | vectorized::MutableColumns& _limit_columns; |
377 | | std::vector<int>& _order_directions; |
378 | | std::vector<int>& _null_directions; |
379 | | }; |
380 | | |
381 | | std::priority_queue<HeapLimitCursor> limit_heap; |
382 | | |
383 | | // Refresh the top limit heap with a new row |
384 | | void refresh_top_limit(size_t row_id, const vectorized::ColumnRawPtrs& key_columns); |
385 | | |
386 | | private: |
387 | | vectorized::MutableColumns _get_keys_hash_table(); |
388 | | |
389 | 30 | void _close_with_serialized_key() { |
390 | 30 | std::visit(vectorized::Overload {[&](std::monostate& arg) -> void { |
391 | | // Do nothing |
392 | 0 | }, |
393 | 30 | [&](auto& agg_method) -> void { |
394 | 30 | auto& data = *agg_method.hash_table; |
395 | 91 | data.for_each_mapped([&](auto& mapped) { |
396 | 91 | if (mapped) { |
397 | 91 | static_cast<void>(_destroy_agg_status(mapped)); |
398 | 91 | mapped = nullptr; |
399 | 91 | } |
400 | 91 | }); Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized16MethodSerializedI9PHHashMapINS_9StringRefEPc11DefaultHashIS9_vEEEEEEvS3_ENKUlS3_E_clISA_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIh9PHHashMapIhPc9HashCRC32IhEEEEEEvS3_ENKUlS3_E_clIS9_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIt9PHHashMapItPc9HashCRC32ItEEEEEEvS3_ENKUlS3_E_clIS9_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIj9PHHashMapIjPc9HashCRC32IjEEEEEEvS3_ENKUlS3_E_clIS9_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIm9PHHashMapImPc9HashCRC32ImEEEEEEvS3_ENKUlS3_E_clIS9_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized19MethodStringNoCacheINS_13StringHashMapIPcNS_9AllocatorILb1ELb1ELb0ENS_22DefaultMemoryAllocatorEEEEEEEEEvS3_ENKUlS3_E_clIS9_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIN4wide7integerILm128EjEE9PHHashMapISA_Pc9HashCRC32ISA_EEEEEEvS3_ENKUlS3_E_clISC_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIN4wide7integerILm256EjEE9PHHashMapISA_Pc9HashCRC32ISA_EEEEEEvS3_ENKUlS3_E_clISC_EEDaS3_ _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIj9PHHashMapIjPc14HashMixWrapperIj9HashCRC32IjEEEEEEEvS3_ENKUlS3_E_clIS9_EEDaS3_ Line | Count | Source | 395 | 16 | data.for_each_mapped([&](auto& mapped) { | 396 | 16 | if (mapped) { | 397 | 16 | static_cast<void>(_destroy_agg_status(mapped)); | 398 | 16 | mapped = nullptr; | 399 | 16 | } | 400 | 16 | }); |
_ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIm9PHHashMapImPc14HashMixWrapperIm9HashCRC32ImEEEEEEEvS3_ENKUlS3_E_clIS9_EEDaS3_ Line | Count | Source | 395 | 55 | data.for_each_mapped([&](auto& mapped) { | 396 | 55 | if (mapped) { | 397 | 55 | static_cast<void>(_destroy_agg_status(mapped)); | 398 | 55 | mapped = nullptr; | 399 | 55 | } | 400 | 55 | }); |
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIhNS6_15DataWithNullKeyI9PHHashMapIhPc9HashCRC32IhEEEEEEEEEEvS3_ENKUlS3_E_clISB_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberItNS6_15DataWithNullKeyI9PHHashMapItPc9HashCRC32ItEEEEEEEEEEvS3_ENKUlS3_E_clISB_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIjNS6_15DataWithNullKeyI9PHHashMapIjPc9HashCRC32IjEEEEEEEEEEvS3_ENKUlS3_E_clISB_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberImNS6_15DataWithNullKeyI9PHHashMapImPc9HashCRC32ImEEEEEEEEEEvS3_ENKUlS3_E_clISB_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIjNS6_15DataWithNullKeyI9PHHashMapIjPc14HashMixWrapperIj9HashCRC32IjEEEEEEEEEEEvS3_ENKUlS3_E_clISB_EEDaS3_ _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberImNS6_15DataWithNullKeyI9PHHashMapImPc14HashMixWrapperIm9HashCRC32ImEEEEEEEEEEEvS3_ENKUlS3_E_clISB_EEDaS3_ Line | Count | Source | 395 | 20 | data.for_each_mapped([&](auto& mapped) { | 396 | 20 | if (mapped) { | 397 | 20 | static_cast<void>(_destroy_agg_status(mapped)); | 398 | 20 | mapped = nullptr; | 399 | 20 | } | 400 | 20 | }); |
Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIN4wide7integerILm128EjEENS6_15DataWithNullKeyI9PHHashMapISB_Pc9HashCRC32ISB_EEEEEEEEEEvS3_ENKUlS3_E_clISE_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIN4wide7integerILm256EjEENS6_15DataWithNullKeyI9PHHashMapISB_Pc9HashCRC32ISB_EEEEEEEEEEvS3_ENKUlS3_E_clISE_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_19MethodStringNoCacheINS6_15DataWithNullKeyINS_13StringHashMapIPcNS_9AllocatorILb1ELb1ELb0ENS_22DefaultMemoryAllocatorEEEEEEEEEEEEEvS3_ENKUlS3_E_clISB_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodKeysFixedI9PHHashMapImPc9HashCRC32ImEEEEEEvS3_ENKUlS3_E_clIS9_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodKeysFixedI9PHHashMapIN4wide7integerILm128EjEEPc9HashCRC32ISB_EEEEEEvS3_ENKUlS3_E_clISC_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodKeysFixedI9PHHashMapIN4wide7integerILm256EjEEPc9HashCRC32ISB_EEEEEEvS3_ENKUlS3_E_clISC_EEDaS3_ Unexecuted instantiation: _ZZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodKeysFixedI9PHHashMapINS6_7UInt136EPc9HashCRC32IS9_EEEEEEvS3_ENKUlS3_E_clISA_EEDaS3_ |
401 | 30 | if (data.has_null_key_data()) { |
402 | 5 | auto st = _destroy_agg_status( |
403 | 5 | data.template get_null_key_data< |
404 | 5 | vectorized::AggregateDataPtr>()); |
405 | 5 | if (!st) { |
406 | 0 | throw Exception(st.code(), st.to_string()); |
407 | 0 | } |
408 | 5 | } |
409 | 30 | }}, Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized16MethodSerializedI9PHHashMapINS_9StringRefEPc11DefaultHashIS9_vEEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIh9PHHashMapIhPc9HashCRC32IhEEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIt9PHHashMapItPc9HashCRC32ItEEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIj9PHHashMapIjPc9HashCRC32IjEEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIm9PHHashMapImPc9HashCRC32ImEEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized19MethodStringNoCacheINS_13StringHashMapIPcNS_9AllocatorILb1ELb1ELb0ENS_22DefaultMemoryAllocatorEEEEEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIN4wide7integerILm128EjEE9PHHashMapISA_Pc9HashCRC32ISA_EEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIN4wide7integerILm256EjEE9PHHashMapISA_Pc9HashCRC32ISA_EEEEEEvS3_ _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIj9PHHashMapIjPc14HashMixWrapperIj9HashCRC32IjEEEEEEEvS3_ Line | Count | Source | 393 | 11 | [&](auto& agg_method) -> void { | 394 | 11 | auto& data = *agg_method.hash_table; | 395 | 11 | data.for_each_mapped([&](auto& mapped) { | 396 | 11 | if (mapped) { | 397 | 11 | static_cast<void>(_destroy_agg_status(mapped)); | 398 | 11 | mapped = nullptr; | 399 | 11 | } | 400 | 11 | }); | 401 | 11 | if (data.has_null_key_data()) { | 402 | 0 | auto st = _destroy_agg_status( | 403 | 0 | data.template get_null_key_data< | 404 | 0 | vectorized::AggregateDataPtr>()); | 405 | 0 | if (!st) { | 406 | 0 | throw Exception(st.code(), st.to_string()); | 407 | 0 | } | 408 | 0 | } | 409 | 11 | }}, |
_ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodOneNumberIm9PHHashMapImPc14HashMixWrapperIm9HashCRC32ImEEEEEEEvS3_ Line | Count | Source | 393 | 14 | [&](auto& agg_method) -> void { | 394 | 14 | auto& data = *agg_method.hash_table; | 395 | 14 | data.for_each_mapped([&](auto& mapped) { | 396 | 14 | if (mapped) { | 397 | 14 | static_cast<void>(_destroy_agg_status(mapped)); | 398 | 14 | mapped = nullptr; | 399 | 14 | } | 400 | 14 | }); | 401 | 14 | if (data.has_null_key_data()) { | 402 | 0 | auto st = _destroy_agg_status( | 403 | 0 | data.template get_null_key_data< | 404 | 0 | vectorized::AggregateDataPtr>()); | 405 | 0 | if (!st) { | 406 | 0 | throw Exception(st.code(), st.to_string()); | 407 | 0 | } | 408 | 0 | } | 409 | 14 | }}, |
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIhNS6_15DataWithNullKeyI9PHHashMapIhPc9HashCRC32IhEEEEEEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberItNS6_15DataWithNullKeyI9PHHashMapItPc9HashCRC32ItEEEEEEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIjNS6_15DataWithNullKeyI9PHHashMapIjPc9HashCRC32IjEEEEEEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberImNS6_15DataWithNullKeyI9PHHashMapImPc9HashCRC32ImEEEEEEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIjNS6_15DataWithNullKeyI9PHHashMapIjPc14HashMixWrapperIj9HashCRC32IjEEEEEEEEEEEvS3_ _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberImNS6_15DataWithNullKeyI9PHHashMapImPc14HashMixWrapperIm9HashCRC32ImEEEEEEEEEEEvS3_ Line | Count | Source | 393 | 5 | [&](auto& agg_method) -> void { | 394 | 5 | auto& data = *agg_method.hash_table; | 395 | 5 | data.for_each_mapped([&](auto& mapped) { | 396 | 5 | if (mapped) { | 397 | 5 | static_cast<void>(_destroy_agg_status(mapped)); | 398 | 5 | mapped = nullptr; | 399 | 5 | } | 400 | 5 | }); | 401 | 5 | if (data.has_null_key_data()) { | 402 | 5 | auto st = _destroy_agg_status( | 403 | 5 | data.template get_null_key_data< | 404 | 5 | vectorized::AggregateDataPtr>()); | 405 | 5 | if (!st) { | 406 | 0 | throw Exception(st.code(), st.to_string()); | 407 | 0 | } | 408 | 5 | } | 409 | 5 | }}, |
Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIN4wide7integerILm128EjEENS6_15DataWithNullKeyI9PHHashMapISB_Pc9HashCRC32ISB_EEEEEEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_15MethodOneNumberIN4wide7integerILm256EjEENS6_15DataWithNullKeyI9PHHashMapISB_Pc9HashCRC32ISB_EEEEEEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized26MethodSingleNullableColumnINS6_19MethodStringNoCacheINS6_15DataWithNullKeyINS_13StringHashMapIPcNS_9AllocatorILb1ELb1ELb0ENS_22DefaultMemoryAllocatorEEEEEEEEEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodKeysFixedI9PHHashMapImPc9HashCRC32ImEEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodKeysFixedI9PHHashMapIN4wide7integerILm128EjEEPc9HashCRC32ISB_EEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodKeysFixedI9PHHashMapIN4wide7integerILm256EjEEPc9HashCRC32ISB_EEEEEEvS3_ Unexecuted instantiation: _ZZN5doris8pipeline14AggSharedState26_close_with_serialized_keyEvENKUlRT_E_clINS_10vectorized15MethodKeysFixedI9PHHashMapINS6_7UInt136EPc9HashCRC32IS9_EEEEEEvS3_ |
410 | 30 | agg_data->method_variant); |
411 | 30 | } |
412 | | |
413 | 10 | void _close_without_key() { |
414 | | //because prepare maybe failed, and couldn't create agg data. |
415 | | //but finally call close to destory agg data, if agg data has bitmapValue |
416 | | //will be core dump, it's not initialized |
417 | 10 | if (agg_data_created_without_key) { |
418 | 8 | static_cast<void>(_destroy_agg_status(agg_data->without_key)); |
419 | 8 | agg_data_created_without_key = false; |
420 | 8 | } |
421 | 10 | } |
422 | | Status _destroy_agg_status(vectorized::AggregateDataPtr data); |
423 | | }; |
424 | | |
425 | | struct BasicSpillSharedState { |
426 | 56 | virtual ~BasicSpillSharedState() = default; |
427 | | |
428 | | // These two counters are shared to spill source operators as the initial value |
429 | | // of 'SpillWriteFileCurrentBytes' and 'SpillWriteFileCurrentCount'. |
430 | | // Total bytes of spill data written to disk file(after serialized) |
431 | | RuntimeProfile::Counter* _spill_write_file_total_size = nullptr; |
432 | | RuntimeProfile::Counter* _spill_file_total_count = nullptr; |
433 | | |
434 | 31 | void setup_shared_profile(RuntimeProfile* sink_profile) { |
435 | 31 | _spill_file_total_count = |
436 | 31 | ADD_COUNTER_WITH_LEVEL(sink_profile, "SpillWriteFileTotalCount", TUnit::UNIT, 1); |
437 | 31 | _spill_write_file_total_size = |
438 | 31 | ADD_COUNTER_WITH_LEVEL(sink_profile, "SpillWriteFileBytes", TUnit::BYTES, 1); |
439 | 31 | } |
440 | | |
441 | | virtual void update_spill_stream_profiles(RuntimeProfile* source_profile) = 0; |
442 | | }; |
443 | | |
444 | | struct AggSpillPartition; |
445 | | struct PartitionedAggSharedState : public BasicSharedState, |
446 | | public BasicSpillSharedState, |
447 | | public std::enable_shared_from_this<PartitionedAggSharedState> { |
448 | | ENABLE_FACTORY_CREATOR(PartitionedAggSharedState) |
449 | | |
450 | 12 | PartitionedAggSharedState() = default; |
451 | 12 | ~PartitionedAggSharedState() override = default; |
452 | | |
453 | | void update_spill_stream_profiles(RuntimeProfile* source_profile) override; |
454 | | |
455 | | void init_spill_params(size_t spill_partition_count); |
456 | | |
457 | | void close(); |
458 | | |
459 | | AggSharedState* in_mem_shared_state = nullptr; |
460 | | std::shared_ptr<BasicSharedState> in_mem_shared_state_sptr; |
461 | | |
462 | | size_t partition_count; |
463 | | size_t max_partition_index; |
464 | | bool is_spilled = false; |
465 | | std::atomic_bool is_closed = false; |
466 | | std::deque<std::shared_ptr<AggSpillPartition>> spill_partitions; |
467 | | |
468 | 1.04M | size_t get_partition_index(size_t hash_value) const { return hash_value % partition_count; } |
469 | | }; |
470 | | |
471 | | struct AggSpillPartition { |
472 | | static constexpr int64_t AGG_SPILL_FILE_SIZE = 1024 * 1024 * 1024; // 1G |
473 | | |
474 | 352 | AggSpillPartition() = default; |
475 | | |
476 | | void close(); |
477 | | |
478 | | Status get_spill_stream(RuntimeState* state, int node_id, RuntimeProfile* profile, |
479 | | vectorized::SpillStreamSPtr& spilling_stream); |
480 | | |
481 | 64 | Status flush_if_full() { |
482 | 64 | DCHECK(spilling_stream_); |
483 | 64 | Status status; |
484 | | // avoid small spill files |
485 | 64 | if (spilling_stream_->get_written_bytes() >= AGG_SPILL_FILE_SIZE) { |
486 | 0 | status = spilling_stream_->spill_eof(); |
487 | 0 | spilling_stream_.reset(); |
488 | 0 | } |
489 | 64 | return status; |
490 | 64 | } |
491 | | |
492 | 352 | Status finish_current_spilling(bool eos = false) { |
493 | 352 | if (spilling_stream_) { |
494 | 100 | if (eos || spilling_stream_->get_written_bytes() >= AGG_SPILL_FILE_SIZE) { |
495 | 48 | auto status = spilling_stream_->spill_eof(); |
496 | 48 | spilling_stream_.reset(); |
497 | 48 | return status; |
498 | 48 | } |
499 | 100 | } |
500 | 304 | return Status::OK(); |
501 | 352 | } |
502 | | |
503 | | std::deque<vectorized::SpillStreamSPtr> spill_streams_; |
504 | | vectorized::SpillStreamSPtr spilling_stream_; |
505 | | }; |
506 | | using AggSpillPartitionSPtr = std::shared_ptr<AggSpillPartition>; |
507 | | struct SortSharedState : public BasicSharedState { |
508 | | ENABLE_FACTORY_CREATOR(SortSharedState) |
509 | | public: |
510 | | std::shared_ptr<vectorized::Sorter> sorter; |
511 | | }; |
512 | | |
513 | | struct SpillSortSharedState : public BasicSharedState, |
514 | | public BasicSpillSharedState, |
515 | | public std::enable_shared_from_this<SpillSortSharedState> { |
516 | | ENABLE_FACTORY_CREATOR(SpillSortSharedState) |
517 | | |
518 | 10 | SpillSortSharedState() = default; |
519 | 10 | ~SpillSortSharedState() override = default; |
520 | | |
521 | 5 | void update_spill_block_batch_row_count(RuntimeState* state, const vectorized::Block* block) { |
522 | 5 | auto rows = block->rows(); |
523 | 5 | if (rows > 0 && 0 == avg_row_bytes) { |
524 | 4 | avg_row_bytes = std::max((std::size_t)1, block->bytes() / rows); |
525 | 4 | spill_block_batch_row_count = |
526 | 4 | (state->spill_sort_batch_bytes() + avg_row_bytes - 1) / avg_row_bytes; |
527 | 4 | LOG(INFO) << "spill sort block batch row count: " << spill_block_batch_row_count; |
528 | 4 | } |
529 | 5 | } |
530 | | |
531 | | void update_spill_stream_profiles(RuntimeProfile* source_profile) override; |
532 | | |
533 | | void close(); |
534 | | |
535 | | SortSharedState* in_mem_shared_state = nullptr; |
536 | | bool enable_spill = false; |
537 | | bool is_spilled = false; |
538 | | std::atomic_bool is_closed = false; |
539 | | std::shared_ptr<BasicSharedState> in_mem_shared_state_sptr; |
540 | | |
541 | | std::deque<vectorized::SpillStreamSPtr> sorted_streams; |
542 | | size_t avg_row_bytes = 0; |
543 | | size_t spill_block_batch_row_count; |
544 | | }; |
545 | | |
546 | | struct UnionSharedState : public BasicSharedState { |
547 | | ENABLE_FACTORY_CREATOR(UnionSharedState) |
548 | | |
549 | | public: |
550 | 1 | UnionSharedState(int child_count = 1) : data_queue(child_count), _child_count(child_count) {}; |
551 | 0 | int child_count() const { return _child_count; } |
552 | | DataQueue data_queue; |
553 | | const int _child_count; |
554 | | }; |
555 | | |
556 | | struct DataQueueSharedState : public BasicSharedState { |
557 | | ENABLE_FACTORY_CREATOR(DataQueueSharedState) |
558 | | public: |
559 | | DataQueue data_queue; |
560 | | }; |
561 | | |
562 | | class MultiCastDataStreamer; |
563 | | |
564 | | struct MultiCastSharedState : public BasicSharedState, |
565 | | public BasicSpillSharedState, |
566 | | public std::enable_shared_from_this<MultiCastSharedState> { |
567 | | MultiCastSharedState(ObjectPool* pool, int cast_sender_count, int node_id); |
568 | | std::unique_ptr<pipeline::MultiCastDataStreamer> multi_cast_data_streamer; |
569 | | |
570 | | void update_spill_stream_profiles(RuntimeProfile* source_profile) override; |
571 | | }; |
572 | | |
573 | | struct AnalyticSharedState : public BasicSharedState { |
574 | | ENABLE_FACTORY_CREATOR(AnalyticSharedState) |
575 | | |
576 | | public: |
577 | 9 | AnalyticSharedState() = default; |
578 | | std::queue<vectorized::Block> blocks_buffer; |
579 | | std::mutex buffer_mutex; |
580 | | bool sink_eos = false; |
581 | | std::mutex sink_eos_lock; |
582 | | }; |
583 | | |
584 | | struct JoinSharedState : public BasicSharedState { |
585 | | // For some join case, we can apply a short circuit strategy |
586 | | // 1. _has_null_in_build_side = true |
587 | | // 2. build side rows is empty, Join op is: inner join/right outer join/left semi/right semi/right anti |
588 | | bool _has_null_in_build_side = false; |
589 | | bool short_circuit_for_probe = false; |
590 | | // for some join, when build side rows is empty, we could return directly by add some additional null data in probe table. |
591 | | bool empty_right_table_need_probe_dispose = false; |
592 | | JoinOpVariants join_op_variants; |
593 | | }; |
594 | | |
595 | | struct HashJoinSharedState : public JoinSharedState { |
596 | | ENABLE_FACTORY_CREATOR(HashJoinSharedState) |
597 | 78.1k | HashJoinSharedState() { |
598 | 78.1k | hash_table_variant_vector.push_back(std::make_shared<JoinDataVariants>()); |
599 | 78.1k | } |
600 | 1 | HashJoinSharedState(int num_instances) { |
601 | 1 | source_deps.resize(num_instances, nullptr); |
602 | 1 | hash_table_variant_vector.resize(num_instances, nullptr); |
603 | 9 | for (int i = 0; i < num_instances; i++) { |
604 | 8 | hash_table_variant_vector[i] = std::make_shared<JoinDataVariants>(); |
605 | 8 | } |
606 | 1 | } |
607 | | std::shared_ptr<vectorized::Arena> arena = std::make_shared<vectorized::Arena>(); |
608 | | |
609 | | const std::vector<TupleDescriptor*> build_side_child_desc; |
610 | | size_t build_exprs_size = 0; |
611 | | std::shared_ptr<vectorized::Block> build_block; |
612 | | std::shared_ptr<std::vector<uint32_t>> build_indexes_null; |
613 | | |
614 | | // Used by shared hash table |
615 | | // For probe operator, hash table in _hash_table_variants is read-only if visited flags is not |
616 | | // used. (visited flags will be used only in right / full outer join). |
617 | | // |
618 | | // For broadcast join, although hash table is read-only, some states in `_hash_table_variants` |
619 | | // are still could be written. For example, serialized keys will be written in a continuous |
620 | | // memory in `_hash_table_variants`. So before execution, we should use a local _hash_table_variants |
621 | | // which has a shared hash table in it. |
622 | | std::vector<std::shared_ptr<JoinDataVariants>> hash_table_variant_vector; |
623 | | }; |
624 | | |
625 | | struct PartitionedHashJoinSharedState |
626 | | : public HashJoinSharedState, |
627 | | public BasicSpillSharedState, |
628 | | public std::enable_shared_from_this<PartitionedHashJoinSharedState> { |
629 | | ENABLE_FACTORY_CREATOR(PartitionedHashJoinSharedState) |
630 | | |
631 | 0 | void update_spill_stream_profiles(RuntimeProfile* source_profile) override { |
632 | 0 | for (auto& stream : spilled_streams) { |
633 | 0 | if (stream) { |
634 | 0 | stream->update_shared_profiles(source_profile); |
635 | 0 | } |
636 | 0 | } |
637 | 0 | } |
638 | | |
639 | | std::unique_ptr<RuntimeState> inner_runtime_state; |
640 | | std::shared_ptr<HashJoinSharedState> inner_shared_state; |
641 | | std::vector<std::unique_ptr<vectorized::MutableBlock>> partitioned_build_blocks; |
642 | | std::vector<vectorized::SpillStreamSPtr> spilled_streams; |
643 | | bool need_to_spill = false; |
644 | | }; |
645 | | |
646 | | struct NestedLoopJoinSharedState : public JoinSharedState { |
647 | | ENABLE_FACTORY_CREATOR(NestedLoopJoinSharedState) |
648 | | // if true, left child has no more rows to process |
649 | | bool left_side_eos = false; |
650 | | // Visited flags for each row in build side. |
651 | | vectorized::MutableColumns build_side_visited_flags; |
652 | | // List of build blocks, constructed in prepare() |
653 | | vectorized::Blocks build_blocks; |
654 | | }; |
655 | | |
656 | | struct PartitionSortNodeSharedState : public BasicSharedState { |
657 | | ENABLE_FACTORY_CREATOR(PartitionSortNodeSharedState) |
658 | | public: |
659 | | std::queue<vectorized::Block> blocks_buffer; |
660 | | std::mutex buffer_mutex; |
661 | | std::vector<std::unique_ptr<vectorized::PartitionSorter>> partition_sorts; |
662 | | bool sink_eos = false; |
663 | | std::mutex sink_eos_lock; |
664 | | std::mutex prepared_finish_lock; |
665 | | }; |
666 | | |
667 | | struct SetSharedState : public BasicSharedState { |
668 | | ENABLE_FACTORY_CREATOR(SetSharedState) |
669 | | public: |
670 | | /// default init |
671 | | vectorized::Block build_block; // build to source |
672 | | //record element size in hashtable |
673 | | int64_t valid_element_in_hash_tbl = 0; |
674 | | //first: idx mapped to column types |
675 | | //second: column_id, could point to origin column or cast column |
676 | | std::unordered_map<int, int> build_col_idx; |
677 | | |
678 | | //// shared static states (shared, decided in prepare/open...) |
679 | | |
680 | | /// init in setup_local_state |
681 | | std::unique_ptr<SetDataVariants> hash_table_variants = |
682 | | std::make_unique<SetDataVariants>(); // the real data HERE. |
683 | | std::vector<bool> build_not_ignore_null; |
684 | | |
685 | | // The SET operator's child might have different nullable attributes. |
686 | | // If a calculation involves both nullable and non-nullable columns, the final output should be a nullable column |
687 | | Status update_build_not_ignore_null(const vectorized::VExprContextSPtrs& ctxs); |
688 | | |
689 | | size_t get_hash_table_size() const; |
690 | | /// init in both upstream side. |
691 | | //The i-th result expr list refers to the i-th child. |
692 | | std::vector<vectorized::VExprContextSPtrs> child_exprs_lists; |
693 | | |
694 | | /// init in build side |
695 | | size_t child_quantity; |
696 | | vectorized::VExprContextSPtrs build_child_exprs; |
697 | | std::vector<Dependency*> probe_finished_children_dependency; |
698 | | |
699 | | /// init in probe side |
700 | | std::vector<vectorized::VExprContextSPtrs> probe_child_exprs_lists; |
701 | | |
702 | | std::atomic<bool> ready_for_read = false; |
703 | | |
704 | | /// called in setup_local_state |
705 | | Status hash_table_init(); |
706 | | }; |
707 | | |
708 | | enum class ExchangeType : uint8_t { |
709 | | NOOP = 0, |
710 | | // Shuffle data by Crc32HashPartitioner<LocalExchangeChannelIds>. |
711 | | HASH_SHUFFLE = 1, |
712 | | // Round-robin passthrough data blocks. |
713 | | PASSTHROUGH = 2, |
714 | | // Shuffle data by Crc32HashPartitioner<ShuffleChannelIds> (e.g. same as storage engine). |
715 | | BUCKET_HASH_SHUFFLE = 3, |
716 | | // Passthrough data blocks to all channels. |
717 | | BROADCAST = 4, |
718 | | // Passthrough data to channels evenly in an adaptive way. |
719 | | ADAPTIVE_PASSTHROUGH = 5, |
720 | | // Send all data to the first channel. |
721 | | PASS_TO_ONE = 6, |
722 | | }; |
723 | | |
724 | 63 | inline std::string get_exchange_type_name(ExchangeType idx) { |
725 | 63 | switch (idx) { |
726 | 13 | case ExchangeType::NOOP: |
727 | 13 | return "NOOP"; |
728 | 49 | case ExchangeType::HASH_SHUFFLE: |
729 | 49 | return "HASH_SHUFFLE"; |
730 | 1 | case ExchangeType::PASSTHROUGH: |
731 | 1 | return "PASSTHROUGH"; |
732 | 0 | case ExchangeType::BUCKET_HASH_SHUFFLE: |
733 | 0 | return "BUCKET_HASH_SHUFFLE"; |
734 | 0 | case ExchangeType::BROADCAST: |
735 | 0 | return "BROADCAST"; |
736 | 0 | case ExchangeType::ADAPTIVE_PASSTHROUGH: |
737 | 0 | return "ADAPTIVE_PASSTHROUGH"; |
738 | 0 | case ExchangeType::PASS_TO_ONE: |
739 | 0 | return "PASS_TO_ONE"; |
740 | 63 | } |
741 | 0 | throw Exception(Status::FatalError("__builtin_unreachable")); |
742 | 63 | } |
743 | | |
744 | | struct DataDistribution { |
745 | 156k | DataDistribution(ExchangeType type) : distribution_type(type) {} |
746 | | DataDistribution(ExchangeType type, const std::vector<TExpr>& partition_exprs_) |
747 | 52 | : distribution_type(type), partition_exprs(partition_exprs_) {} |
748 | 0 | DataDistribution(const DataDistribution& other) = default; |
749 | 5 | bool need_local_exchange() const { return distribution_type != ExchangeType::NOOP; } |
750 | 5 | DataDistribution& operator=(const DataDistribution& other) = default; |
751 | | ExchangeType distribution_type; |
752 | | std::vector<TExpr> partition_exprs; |
753 | | }; |
754 | | |
755 | | class ExchangerBase; |
756 | | |
757 | | struct LocalExchangeSharedState : public BasicSharedState { |
758 | | public: |
759 | | ENABLE_FACTORY_CREATOR(LocalExchangeSharedState); |
760 | | LocalExchangeSharedState(int num_instances); |
761 | | ~LocalExchangeSharedState() override; |
762 | | std::unique_ptr<ExchangerBase> exchanger {}; |
763 | | std::vector<RuntimeProfile::Counter*> mem_counters; |
764 | | std::atomic<int64_t> mem_usage = 0; |
765 | | std::atomic<size_t> _buffer_mem_limit = config::local_exchange_buffer_mem_limit; |
766 | | // We need to make sure to add mem_usage first and then enqueue, otherwise sub mem_usage may cause negative mem_usage during concurrent dequeue. |
767 | | std::mutex le_lock; |
768 | | void sub_running_sink_operators(); |
769 | | void sub_running_source_operators(); |
770 | 10 | void _set_always_ready() { |
771 | 40 | for (auto& dep : source_deps) { |
772 | 40 | DCHECK(dep); |
773 | 40 | dep->set_always_ready(); |
774 | 40 | } |
775 | 10 | for (auto& dep : sink_deps) { |
776 | 10 | DCHECK(dep); |
777 | 10 | dep->set_always_ready(); |
778 | 10 | } |
779 | 10 | } |
780 | | |
781 | 0 | Dependency* get_sink_dep_by_channel_id(int channel_id) { return nullptr; } |
782 | | |
783 | 129 | void set_ready_to_read(int channel_id) { |
784 | 129 | auto& dep = source_deps[channel_id]; |
785 | 129 | DCHECK(dep) << channel_id; |
786 | 129 | dep->set_ready(); |
787 | 129 | } |
788 | | |
789 | 161 | void add_mem_usage(int channel_id, size_t delta) { mem_counters[channel_id]->update(delta); } |
790 | | |
791 | 125 | void sub_mem_usage(int channel_id, size_t delta) { |
792 | 125 | mem_counters[channel_id]->update(-(int64_t)delta); |
793 | 125 | } |
794 | | |
795 | 114 | void add_total_mem_usage(size_t delta) { |
796 | 114 | if (cast_set<int64_t>(mem_usage.fetch_add(delta) + delta) > _buffer_mem_limit) { |
797 | 15 | sink_deps.front()->block(); |
798 | 15 | } |
799 | 114 | } |
800 | | |
801 | 114 | void sub_total_mem_usage(size_t delta) { |
802 | 114 | auto prev_usage = mem_usage.fetch_sub(delta); |
803 | 114 | DCHECK_GE(prev_usage - delta, 0) << "prev_usage: " << prev_usage << " delta: " << delta; |
804 | 114 | if (cast_set<int64_t>(prev_usage - delta) <= _buffer_mem_limit) { |
805 | 102 | sink_deps.front()->set_ready(); |
806 | 102 | } |
807 | 114 | } |
808 | | |
809 | 0 | void set_low_memory_mode(RuntimeState* state) { |
810 | 0 | _buffer_mem_limit = std::min<int64_t>(config::local_exchange_buffer_mem_limit, |
811 | 0 | state->low_memory_mode_buffer_limit()); |
812 | 0 | } |
813 | | }; |
814 | | |
815 | | //struct LocalMergeExchangeSharedState : public LocalExchangeSharedState { |
816 | | // ENABLE_FACTORY_CREATOR(LocalMergeExchangeSharedState); |
817 | | // LocalMergeExchangeSharedState(int num_instances) |
818 | | // : LocalExchangeSharedState(num_instances), |
819 | | // _each_queue_limit(config::local_exchange_buffer_mem_limit / num_instances) {} |
820 | | // |
821 | | // void create_dependencies(int local_exchange_id) override { |
822 | | // sink_deps.resize(source_deps.size()); |
823 | | // for (size_t i = 0; i < source_deps.size(); i++) { |
824 | | // source_deps[i] = |
825 | | // std::make_shared<Dependency>(local_exchange_id, local_exchange_id, |
826 | | // "LOCAL_MERGE_EXCHANGE_OPERATOR_DEPENDENCY"); |
827 | | // source_deps[i]->set_shared_state(this); |
828 | | // sink_deps[i] = std::make_shared<Dependency>( |
829 | | // local_exchange_id, local_exchange_id, |
830 | | // "LOCAL_MERGE_EXCHANGE_OPERATOR_SINK_DEPENDENCY", true); |
831 | | // sink_deps[i]->set_shared_state(this); |
832 | | // } |
833 | | // } |
834 | | // |
835 | | // void sub_total_mem_usage(size_t delta) override { mem_usage.fetch_sub(delta); } |
836 | | // void add_total_mem_usage(size_t delta) override { mem_usage.fetch_add(delta); } |
837 | | // |
838 | | // void add_mem_usage(int channel_id, size_t delta) override { |
839 | | // LocalExchangeSharedState::add_mem_usage(channel_id, delta); |
840 | | // if (mem_counters[channel_id]->value() > _each_queue_limit.load()) { |
841 | | // sink_deps[channel_id]->block(); |
842 | | // } |
843 | | // } |
844 | | // |
845 | | // void sub_mem_usage(int channel_id, size_t delta) override { |
846 | | // LocalExchangeSharedState::sub_mem_usage(channel_id, delta); |
847 | | // if (mem_counters[channel_id]->value() <= _each_queue_limit.load()) { |
848 | | // sink_deps[channel_id]->set_ready(); |
849 | | // } |
850 | | // } |
851 | | // |
852 | | // void set_low_memory_mode(RuntimeState* state) override { |
853 | | // _buffer_mem_limit = std::min<int64_t>(config::local_exchange_buffer_mem_limit, |
854 | | // state->low_memory_mode_buffer_limit()); |
855 | | // _each_queue_limit = std::max<int64_t>(64 * 1024, _buffer_mem_limit / source_deps.size()); |
856 | | // } |
857 | | // |
858 | | // Dependency* get_sink_dep_by_channel_id(int channel_id) override { |
859 | | // return sink_deps[channel_id].get(); |
860 | | // } |
861 | | // |
862 | | // std::vector<DependencySPtr> get_dep_by_channel_id(int channel_id) override { |
863 | | // return source_deps; |
864 | | // } |
865 | | // |
866 | | //private: |
867 | | // std::atomic_int64_t _each_queue_limit; |
868 | | //}; |
869 | | |
870 | | //class QueryGlobalDependency final : public Dependency { |
871 | | // ENABLE_FACTORY_CREATOR(QueryGlobalDependency); |
872 | | // QueryGlobalDependency(std::string name, bool ready = false) : Dependency(-1, -1, name, ready) {} |
873 | | // ~QueryGlobalDependency() override = default; |
874 | | // Dependency* is_blocked_by(PipelineTask* task = nullptr) override; |
875 | | //}; |
876 | | |
877 | | struct FetchRpcStruct { |
878 | | std::shared_ptr<PBackendService_Stub> stub; |
879 | | PMultiGetRequestV2 request; |
880 | | std::shared_ptr<doris::DummyBrpcCallback<PMultiGetResponseV2>> callback; |
881 | | MonotonicStopWatch rpc_timer; |
882 | | }; |
883 | | |
884 | | struct MaterializationSharedState : public BasicSharedState { |
885 | | ENABLE_FACTORY_CREATOR(MaterializationSharedState) |
886 | | public: |
887 | 4 | MaterializationSharedState() = default; |
888 | | |
889 | | Status init_multi_requests(const TMaterializationNode& tnode, RuntimeState* state); |
890 | | Status create_muiltget_result(const vectorized::Columns& columns, bool eos, bool gc_id_map); |
891 | | Status merge_multi_response(vectorized::Block* block); |
892 | | |
893 | | Dependency* create_source_dependency(int operator_id, int node_id, |
894 | | const std::string& name) override; |
895 | | |
896 | | bool rpc_struct_inited = false; |
897 | | Status rpc_status = Status::OK(); |
898 | | bool last_block = false; |
899 | | // empty materialization sink block not need to merge block |
900 | | bool need_merge_block = true; |
901 | | vectorized::Block origin_block; |
902 | | // The rowid column of the origin block. should be replaced by the column of the result block. |
903 | | std::vector<int> rowid_locs; |
904 | | std::vector<vectorized::MutableBlock> response_blocks; |
905 | | std::map<int64_t, FetchRpcStruct> rpc_struct_map; |
906 | | // Register each line in which block to ensure the order of the result. |
907 | | // Zero means NULL value. |
908 | | std::vector<std::vector<int64_t>> block_order_results; |
909 | | }; |
910 | | #include "common/compile_check_end.h" |
911 | | } // namespace doris::pipeline |