be/src/runtime/query_context.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 <gen_cpp/PaloInternalService_types.h> |
21 | | #include <gen_cpp/RuntimeProfile_types.h> |
22 | | #include <gen_cpp/Types_types.h> |
23 | | #include <glog/logging.h> |
24 | | |
25 | | #include <atomic> |
26 | | #include <cstdint> |
27 | | #include <memory> |
28 | | #include <mutex> |
29 | | #include <string> |
30 | | #include <unordered_map> |
31 | | |
32 | | #include "common/config.h" |
33 | | #include "common/factory_creator.h" |
34 | | #include "common/object_pool.h" |
35 | | #include "common/status.h" |
36 | | #include "exec/common/memory.h" |
37 | | #include "exec/runtime_filter/runtime_filter_mgr.h" |
38 | | #include "exec/scan/scanner_scheduler.h" |
39 | | #include "runtime/exec_env.h" |
40 | | #include "runtime/memory/mem_tracker_limiter.h" |
41 | | #include "runtime/runtime_predicate.h" |
42 | | #include "runtime/workload_group/workload_group.h" |
43 | | #include "runtime/workload_management/resource_context.h" |
44 | | #include "util/hash_util.hpp" |
45 | | #include "util/threadpool.h" |
46 | | |
47 | | namespace doris { |
48 | | |
49 | | class PipelineFragmentContext; |
50 | | class PipelineTask; |
51 | | class Dependency; |
52 | | class RecCTEScanLocalState; |
53 | | |
54 | | struct ReportStatusRequest { |
55 | | const Status status; |
56 | | std::vector<RuntimeState*> runtime_states; |
57 | | bool done; |
58 | | TNetworkAddress coord_addr; |
59 | | TUniqueId query_id; |
60 | | int fragment_id; |
61 | | TUniqueId fragment_instance_id; |
62 | | int backend_num; |
63 | | RuntimeState* runtime_state; |
64 | | std::string load_error_url; |
65 | | std::string first_error_msg; |
66 | | std::function<void(const Status&)> cancel_fn; |
67 | | }; |
68 | | |
69 | | enum class QuerySource { |
70 | | INTERNAL_FRONTEND, |
71 | | STREAM_LOAD, |
72 | | GROUP_COMMIT_LOAD, |
73 | | ROUTINE_LOAD, |
74 | | EXTERNAL_CONNECTOR, |
75 | | EXTERNAL_FRONTEND |
76 | | }; |
77 | | |
78 | | const std::string toString(QuerySource query_source); |
79 | | |
80 | | // Save the common components of fragments in a query. |
81 | | // Some components like DescriptorTbl may be very large |
82 | | // that will slow down each execution of fragments when DeSer them every time. |
83 | | class DescriptorTbl; |
84 | | class QueryContext : public std::enable_shared_from_this<QueryContext> { |
85 | | ENABLE_FACTORY_CREATOR(QueryContext); |
86 | | |
87 | | public: |
88 | | static std::shared_ptr<QueryContext> create(TUniqueId query_id, ExecEnv* exec_env, |
89 | | const TQueryOptions& query_options, |
90 | | TNetworkAddress coord_addr, bool is_nereids, |
91 | | TNetworkAddress current_connect_fe, |
92 | | QuerySource query_type); |
93 | | |
94 | | // use QueryContext::create, cannot be made private because of ENABLE_FACTORY_CREATOR::create_shared. |
95 | | QueryContext(TUniqueId query_id, ExecEnv* exec_env, const TQueryOptions& query_options, |
96 | | TNetworkAddress coord_addr, bool is_nereids, TNetworkAddress current_connect_fe, |
97 | | QuerySource query_type); |
98 | | |
99 | | ~QueryContext(); |
100 | | |
101 | | void init_query_task_controller(); |
102 | | |
103 | 0 | ExecEnv* exec_env() const { return _exec_env; } |
104 | | |
105 | 0 | bool is_timeout(timespec now) const { |
106 | 0 | if (_timeout_second <= 0) { |
107 | 0 | return false; |
108 | 0 | } |
109 | 0 | return _query_watcher.elapsed_time_seconds(now) > _timeout_second; |
110 | 0 | } |
111 | | |
112 | 7 | bool is_single_backend_query() const { return _is_single_backend_query; } |
113 | | |
114 | 0 | void set_single_backend_query(bool is_single_backend_query) { |
115 | 0 | _is_single_backend_query = is_single_backend_query; |
116 | 0 | } |
117 | | |
118 | 0 | int64_t get_remaining_query_time_seconds() const { |
119 | 0 | timespec now; |
120 | 0 | clock_gettime(CLOCK_MONOTONIC, &now); |
121 | 0 | if (is_timeout(now)) { |
122 | 0 | return -1; |
123 | 0 | } |
124 | 0 | int64_t elapsed_seconds = _query_watcher.elapsed_time_seconds(now); |
125 | 0 | return _timeout_second - elapsed_seconds; |
126 | 0 | } |
127 | | |
128 | | void set_ready_to_execute(Status reason); |
129 | | |
130 | 4.02M | [[nodiscard]] bool is_cancelled() const { return !_exec_status.ok(); } |
131 | | |
132 | | void cancel_all_pipeline_context(const Status& reason, int fragment_id = -1); |
133 | | std::string print_all_pipeline_context(); |
134 | | void set_pipeline_context(const int fragment_id, |
135 | | std::shared_ptr<PipelineFragmentContext> pip_ctx); |
136 | | void cancel(Status new_status, int fragment_id = -1); |
137 | | |
138 | 26 | [[nodiscard]] Status exec_status() { return _exec_status.status(); } |
139 | | |
140 | | void set_execution_dependency_ready(); |
141 | | |
142 | | void set_memory_sufficient(bool sufficient); |
143 | | |
144 | | void set_ready_to_execute_only(); |
145 | | |
146 | 42 | bool has_runtime_predicate(int source_node_id) { |
147 | 42 | return _runtime_predicates.contains(source_node_id); |
148 | 42 | } |
149 | | |
150 | 0 | RuntimePredicate& get_runtime_predicate(int source_node_id) { |
151 | 0 | DCHECK(has_runtime_predicate(source_node_id)); |
152 | 0 | return _runtime_predicates.find(source_node_id)->second; |
153 | 0 | } |
154 | | |
155 | 0 | void init_runtime_predicates(const std::vector<TTopnFilterDesc>& topn_filter_descs) { |
156 | 0 | for (auto desc : topn_filter_descs) { |
157 | 0 | _runtime_predicates.try_emplace(desc.source_node_id, desc); |
158 | 0 | } |
159 | 0 | } |
160 | | |
161 | | Status set_workload_group(WorkloadGroupPtr& wg); |
162 | | |
163 | 121 | int execution_timeout() const { |
164 | 121 | return _query_options.__isset.execution_timeout ? _query_options.execution_timeout |
165 | 121 | : _query_options.query_timeout; |
166 | 121 | } |
167 | | |
168 | 0 | int32_t runtime_filter_wait_time_ms() const { |
169 | 0 | return _query_options.runtime_filter_wait_time_ms; |
170 | 0 | } |
171 | | |
172 | 0 | int be_exec_version() const { |
173 | 0 | if (!_query_options.__isset.be_exec_version) { |
174 | 0 | return 0; |
175 | 0 | } |
176 | 0 | return _query_options.be_exec_version; |
177 | 0 | } |
178 | | |
179 | 0 | [[nodiscard]] int64_t get_fe_process_uuid() const { |
180 | 0 | return _query_options.__isset.fe_process_uuid ? _query_options.fe_process_uuid : 0; |
181 | 0 | } |
182 | | |
183 | 0 | bool ignore_runtime_filter_error() const { |
184 | 0 | return _query_options.__isset.ignore_runtime_filter_error |
185 | 0 | ? _query_options.ignore_runtime_filter_error |
186 | 0 | : false; |
187 | 0 | } |
188 | | |
189 | 0 | bool enable_force_spill() const { |
190 | 0 | return _query_options.__isset.enable_force_spill && _query_options.enable_force_spill; |
191 | 0 | } |
192 | 92.2k | const TQueryOptions& query_options() const { return _query_options; } |
193 | 0 | bool should_be_shuffled_agg(int node_id) const { |
194 | 0 | return _query_options.__isset.shuffled_agg_ids && |
195 | 0 | std::any_of(_query_options.shuffled_agg_ids.begin(), |
196 | 0 | _query_options.shuffled_agg_ids.end(), |
197 | 0 | [&](const int id) -> bool { return id == node_id; }); |
198 | 0 | } |
199 | | |
200 | | // global runtime filter mgr, the runtime filter have remote target or |
201 | | // need local merge should regist here. before publish() or push_to_remote() |
202 | | // the runtime filter should do the local merge work |
203 | 107 | RuntimeFilterMgr* runtime_filter_mgr() { return _runtime_filter_mgr.get(); } |
204 | | |
205 | 72.1k | TUniqueId query_id() const { return _query_id; } |
206 | | |
207 | 18 | ScannerScheduler* get_scan_scheduler() { return _scan_task_scheduler; } |
208 | | |
209 | 0 | ScannerScheduler* get_remote_scan_scheduler() { return _remote_scan_task_scheduler; } |
210 | | |
211 | 72.1k | Dependency* get_execution_dependency() { return _execution_dependency.get(); } |
212 | 72.2k | Dependency* get_memory_sufficient_dependency() { return _memory_sufficient_dependency.get(); } |
213 | | |
214 | | doris::TaskScheduler* get_pipe_exec_scheduler(); |
215 | | |
216 | | void set_merge_controller_handler( |
217 | 0 | std::shared_ptr<RuntimeFilterMergeControllerEntity>& handler) { |
218 | 0 | _merge_controller_handler = handler; |
219 | 0 | } |
220 | 0 | std::shared_ptr<RuntimeFilterMergeControllerEntity> get_merge_controller_handler() const { |
221 | 0 | return _merge_controller_handler; |
222 | 0 | } |
223 | | |
224 | 72 | bool is_nereids() const { return _is_nereids; } |
225 | 0 | std::shared_ptr<MemShareArbitrator> mem_arb() const { return _mem_arb; } |
226 | | |
227 | 124k | WorkloadGroupPtr workload_group() const { return _resource_ctx->workload_group(); } |
228 | 512k | std::shared_ptr<MemTrackerLimiter> query_mem_tracker() const { |
229 | 512k | DCHECK(_resource_ctx->memory_context()->mem_tracker() != nullptr); |
230 | 512k | return _resource_ctx->memory_context()->mem_tracker(); |
231 | 512k | } |
232 | | |
233 | 11 | int32_t get_slot_count() const { |
234 | 11 | return _query_options.__isset.query_slot_count ? _query_options.query_slot_count : 1; |
235 | 11 | } |
236 | | |
237 | | DescriptorTbl* desc_tbl = nullptr; |
238 | | bool set_rsc_info = false; |
239 | | std::string user; |
240 | | std::string group; |
241 | | TNetworkAddress coord_addr; |
242 | | TNetworkAddress current_connect_fe; |
243 | | TQueryGlobals query_globals; |
244 | 0 | const TQueryGlobals get_query_globals() const { return query_globals; } |
245 | | |
246 | | ObjectPool obj_pool; |
247 | | |
248 | 48.8k | std::shared_ptr<ResourceContext> resource_ctx() { return _resource_ctx; } |
249 | | |
250 | | // plan node id -> TFileScanRangeParams |
251 | | // only for file scan node |
252 | | std::map<int, TFileScanRangeParams> file_scan_range_params_map; |
253 | | |
254 | | void add_using_brpc_stub(const TNetworkAddress& network_address, |
255 | 0 | std::shared_ptr<PBackendService_Stub> brpc_stub) { |
256 | 0 | if (network_address.port == 0) { |
257 | 0 | return; |
258 | 0 | } |
259 | 0 | std::lock_guard<std::mutex> lock(_brpc_stubs_mutex); |
260 | 0 | if (!_using_brpc_stubs.contains(network_address)) { |
261 | 0 | _using_brpc_stubs.emplace(network_address, brpc_stub); |
262 | 0 | } |
263 | |
|
264 | 0 | DCHECK_EQ(_using_brpc_stubs[network_address].get(), brpc_stub.get()); |
265 | 0 | } |
266 | | |
267 | 72.6k | void set_ai_resources(std::map<std::string, TAIResource> ai_resources) { |
268 | 72.6k | _ai_resources = |
269 | 72.6k | std::make_shared<std::map<std::string, TAIResource>>(std::move(ai_resources)); |
270 | 72.6k | } |
271 | | |
272 | 63 | const std::shared_ptr<std::map<std::string, TAIResource>>& get_ai_resources() const { |
273 | 63 | return _ai_resources; |
274 | 63 | } |
275 | | |
276 | | std::unordered_map<TNetworkAddress, std::shared_ptr<PBackendService_Stub>> |
277 | 0 | get_using_brpc_stubs() { |
278 | 0 | std::lock_guard<std::mutex> lock(_brpc_stubs_mutex); |
279 | 0 | return _using_brpc_stubs; |
280 | 0 | } |
281 | | |
282 | 0 | void set_low_memory_mode() { |
283 | | // will not return from low memory mode to non-low memory mode. |
284 | 0 | _resource_ctx->task_controller()->set_low_memory_mode(true); |
285 | 0 | } |
286 | 1.45M | bool low_memory_mode() { return _resource_ctx->task_controller()->low_memory_mode(); } |
287 | | |
288 | 122k | bool is_pure_load_task() { |
289 | 122k | return _query_source == QuerySource::STREAM_LOAD || |
290 | 122k | _query_source == QuerySource::ROUTINE_LOAD || |
291 | 122k | _query_source == QuerySource::GROUP_COMMIT_LOAD; |
292 | 122k | } |
293 | | |
294 | | void set_load_error_url(std::string error_url); |
295 | | std::string get_load_error_url(); |
296 | | void set_first_error_msg(std::string error_msg); |
297 | | std::string get_first_error_msg(); |
298 | | |
299 | | Status send_block_to_cte_scan(const TUniqueId& instance_id, int node_id, |
300 | | const google::protobuf::RepeatedPtrField<doris::PBlock>& pblocks, |
301 | | bool eos); |
302 | | void registe_cte_scan(const TUniqueId& instance_id, int node_id, RecCTEScanLocalState* scan); |
303 | | void deregiste_cte_scan(const TUniqueId& instance_id, int node_id); |
304 | | |
305 | 0 | std::vector<int> get_fragment_ids() { |
306 | 0 | std::vector<int> fragment_ids; |
307 | 0 | for (const auto& it : _fragment_id_to_pipeline_ctx) { |
308 | 0 | fragment_ids.push_back(it.first); |
309 | 0 | } |
310 | 0 | return fragment_ids; |
311 | 0 | } |
312 | | |
313 | | Status reset_global_rf(const google::protobuf::RepeatedField<int32_t>& filter_ids); |
314 | | |
315 | | private: |
316 | | friend class QueryTaskController; |
317 | | |
318 | | int _timeout_second; |
319 | | TUniqueId _query_id; |
320 | | ExecEnv* _exec_env = nullptr; |
321 | | MonotonicStopWatch _query_watcher; |
322 | | bool _is_nereids = false; |
323 | | |
324 | | std::shared_ptr<ResourceContext> _resource_ctx; |
325 | | |
326 | | void _init_resource_context(); |
327 | | void _init_query_mem_tracker(); |
328 | | |
329 | | std::unordered_map<int, RuntimePredicate> _runtime_predicates; |
330 | | |
331 | | std::unique_ptr<RuntimeFilterMgr> _runtime_filter_mgr; |
332 | | const TQueryOptions _query_options; |
333 | | |
334 | | // All pipeline tasks use the same query context to report status. So we need a `_exec_status` |
335 | | // to report the real message if failed. |
336 | | AtomicStatus _exec_status; |
337 | | |
338 | | doris::TaskScheduler* _task_scheduler = nullptr; |
339 | | ScannerScheduler* _scan_task_scheduler = nullptr; |
340 | | ScannerScheduler* _remote_scan_task_scheduler = nullptr; |
341 | | // This dependency indicates if the 2nd phase RPC received from FE. |
342 | | std::unique_ptr<Dependency> _execution_dependency; |
343 | | // This dependency indicates if memory is sufficient to execute. |
344 | | std::unique_ptr<Dependency> _memory_sufficient_dependency; |
345 | | |
346 | | // This shared ptr is never used. It is just a reference to hold the object. |
347 | | // There is a weak ptr in runtime filter manager to reference this object. |
348 | | std::shared_ptr<RuntimeFilterMergeControllerEntity> _merge_controller_handler; |
349 | | |
350 | | std::map<int, std::weak_ptr<PipelineFragmentContext>> _fragment_id_to_pipeline_ctx; |
351 | | std::mutex _pipeline_map_write_lock; |
352 | | |
353 | | std::mutex _profile_mutex; |
354 | | timespec _query_arrival_timestamp; |
355 | | // Distinguish the query source, for query that comes from fe, we will have some memory structure on FE to |
356 | | // help us manage the query. |
357 | | QuerySource _query_source; |
358 | | |
359 | | std::mutex _brpc_stubs_mutex; |
360 | | std::unordered_map<TNetworkAddress, std::shared_ptr<PBackendService_Stub>> _using_brpc_stubs; |
361 | | |
362 | | // when fragment of pipeline is closed, it will register its profile to this map by using add_fragment_profile |
363 | | // flatten profile of one fragment: |
364 | | // Pipeline 0 |
365 | | // PipelineTask 0 |
366 | | // Operator 1 |
367 | | // Operator 2 |
368 | | // Scanner |
369 | | // PipelineTask 1 |
370 | | // Operator 1 |
371 | | // Operator 2 |
372 | | // Scanner |
373 | | // Pipeline 1 |
374 | | // PipelineTask 2 |
375 | | // Operator 3 |
376 | | // PipelineTask 3 |
377 | | // Operator 3 |
378 | | // fragment_id -> list<profile> |
379 | | std::unordered_map<int, std::vector<std::shared_ptr<TRuntimeProfileTree>>> _profile_map; |
380 | | std::unordered_map<int, std::shared_ptr<TRuntimeProfileTree>> _load_channel_profile_map; |
381 | | |
382 | | std::shared_ptr<std::map<std::string, TAIResource>> _ai_resources; |
383 | | |
384 | | void _report_query_profile(); |
385 | | |
386 | | std::unordered_map<int, std::vector<std::shared_ptr<TRuntimeProfileTree>>> |
387 | | _collect_realtime_query_profile(); |
388 | | |
389 | | std::mutex _error_url_lock; |
390 | | std::string _load_error_url; |
391 | | std::string _first_error_msg; |
392 | | |
393 | | bool _is_single_backend_query = false; |
394 | | |
395 | | // file cache context holders |
396 | | std::vector<io::BlockFileCache::QueryFileCacheContextHolderPtr> _query_context_holders; |
397 | | // instance id + node id -> cte scan |
398 | | std::map<std::pair<TUniqueId, int>, RecCTEScanLocalState*> _cte_scan; |
399 | | std::mutex _cte_scan_lock; |
400 | | std::shared_ptr<MemShareArbitrator> _mem_arb = nullptr; |
401 | | |
402 | | public: |
403 | | // when fragment of pipeline is closed, it will register its profile to this map by using add_fragment_profile |
404 | | void add_fragment_profile( |
405 | | int fragment_id, |
406 | | const std::vector<std::shared_ptr<TRuntimeProfileTree>>& pipeline_profile, |
407 | | std::shared_ptr<TRuntimeProfileTree> load_channel_profile); |
408 | | |
409 | | TReportExecStatusParams get_realtime_exec_status(); |
410 | | |
411 | 122k | bool enable_profile() const { |
412 | 122k | return _query_options.__isset.enable_profile && _query_options.enable_profile; |
413 | 122k | } |
414 | | |
415 | 0 | timespec get_query_arrival_timestamp() const { return this->_query_arrival_timestamp; } |
416 | 3 | QuerySource get_query_source() const { return this->_query_source; } |
417 | | |
418 | 0 | TQueryOptions get_query_options() const { return _query_options; } |
419 | | }; |
420 | | |
421 | | } // namespace doris |