be/src/runtime/runtime_query_statistics_mgr.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/Data_types.h> |
21 | | #include <gen_cpp/RuntimeProfile_types.h> |
22 | | #include <gen_cpp/Types_types.h> |
23 | | |
24 | | #include <cstdint> |
25 | | #include <memory> |
26 | | #include <mutex> |
27 | | #include <shared_mutex> |
28 | | #include <string> |
29 | | #include <unordered_map> |
30 | | |
31 | | #include "runtime/workload_management/resource_context.h" |
32 | | #include "util/threadpool.h" |
33 | | |
34 | | namespace doris { |
35 | | |
36 | | class Block; |
37 | | |
38 | | class RuntimeQueryStatisticsMgr { |
39 | | public: |
40 | 18 | RuntimeQueryStatisticsMgr() = default; |
41 | 14 | ~RuntimeQueryStatisticsMgr() = default; |
42 | | |
43 | | static TReportExecStatusParams create_report_exec_status_params( |
44 | | const TUniqueId& q_id, |
45 | | std::unordered_map<int32_t, std::vector<std::shared_ptr<TRuntimeProfileTree>>> |
46 | | fragment_id_to_profile, |
47 | | std::vector<std::shared_ptr<TRuntimeProfileTree>> load_channel_profile, bool is_done); |
48 | | |
49 | | void register_resource_context(std::string query_id, |
50 | | std::shared_ptr<ResourceContext> resource_ctx); |
51 | | |
52 | | void report_runtime_query_statistics(); |
53 | | |
54 | | // used for backend_active_tasks |
55 | | void get_active_be_tasks_block(Block* block); |
56 | | Status get_query_statistics(const std::string& query_id, TQueryStatistics* query_stats); |
57 | | |
58 | | // used for MemoryReclamation |
59 | | void get_tasks_resource_context(std::vector<std::shared_ptr<ResourceContext>>& resource_ctxs); |
60 | | |
61 | | // Called by main threads when backend starts. |
62 | | Status start_report_thread(); |
63 | | // Called by main threads when backend stops. |
64 | | void stop_report_thread(); |
65 | | |
66 | | void register_fragment_profile(const TUniqueId& query_id, const TNetworkAddress& const_addr, |
67 | | int32_t fragment_id, |
68 | | std::vector<std::shared_ptr<TRuntimeProfileTree>> p_profiles, |
69 | | std::shared_ptr<TRuntimeProfileTree> load_channel_profile_x); |
70 | | // When query is finished, try to report query profiles to FE. |
71 | | // ATTN: Profile is reported to fe fragment by fragment. |
72 | | void trigger_profile_reporting(); |
73 | | |
74 | | private: |
75 | | std::shared_mutex _resource_contexts_map_lock; |
76 | | // Must be shared_ptr of ResourceContext, because ResourceContext can only be removed from |
77 | | // _resource_contexts_map after QueryStatistics is reported to FE, |
78 | | // at which time the Query may have ended. |
79 | | std::map<std::string, std::shared_ptr<ResourceContext>> _resource_contexts_map; |
80 | | |
81 | | std::atomic_bool started = false; |
82 | | std::mutex _profile_map_lock; |
83 | | |
84 | | // query_id -> {coordinator_addr, {fragment_id -> std::vector<pipeline_profile>}} |
85 | | std::unordered_map< |
86 | | TUniqueId, |
87 | | std::tuple<TNetworkAddress, |
88 | | std::unordered_map<int, std::vector<std::shared_ptr<TRuntimeProfileTree>>>>> |
89 | | _profile_map; |
90 | | |
91 | | std::unordered_map<std::pair<TUniqueId, int32_t>, std::shared_ptr<TRuntimeProfileTree>> |
92 | | _load_channel_profile_map; |
93 | | |
94 | | std::unique_ptr<ThreadPool> _thread_pool; |
95 | | }; |
96 | | |
97 | | } // namespace doris |