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 <atomic> |
25 | | #include <cstdint> |
26 | | #include <memory> |
27 | | #include <mutex> |
28 | | #include <shared_mutex> |
29 | | #include <string> |
30 | | #include <unordered_map> |
31 | | |
32 | | #include "runtime/workload_management/resource_context.h" |
33 | | #include "util/threadpool.h" |
34 | | |
35 | | namespace doris { |
36 | | |
37 | | class Block; |
38 | | class TQueryStatisticsResult; |
39 | | class TReportExecStatusParams; |
40 | | class RuntimeQueryStatisticsMgrTestPeer; |
41 | | |
42 | | class RuntimeQueryStatisticsMgr { |
43 | | public: |
44 | | RuntimeQueryStatisticsMgr(); |
45 | 30 | ~RuntimeQueryStatisticsMgr() = default; |
46 | | |
47 | | static TReportExecStatusParams create_report_exec_status_params( |
48 | | const TUniqueId& q_id, |
49 | | std::unordered_map<int32_t, std::vector<std::shared_ptr<TRuntimeProfileTree>>> |
50 | | fragment_id_to_profile, |
51 | | std::vector<std::shared_ptr<TRuntimeProfileTree>> load_channel_profile, bool is_done); |
52 | | |
53 | | void register_resource_context(std::string query_id, |
54 | | std::shared_ptr<ResourceContext> resource_ctx); |
55 | | |
56 | | void report_runtime_query_statistics(); |
57 | | |
58 | | static TQueryStatisticsResult create_query_statistics_result( |
59 | | const std::shared_ptr<ResourceContext>& resource_ctx); |
60 | | // used for backend_active_tasks |
61 | | void get_active_be_tasks_block(Block* block); |
62 | | Status get_query_statistics(const std::string& query_id, TQueryStatistics* query_stats); |
63 | | |
64 | | // used for MemoryReclamation |
65 | | void get_tasks_resource_context(std::vector<std::shared_ptr<ResourceContext>>& resource_ctxs); |
66 | | |
67 | | // Called by main threads when backend starts. |
68 | | Status start_report_thread(); |
69 | | // Called by main threads when backend stops. |
70 | | void stop_report_thread(); |
71 | | |
72 | | void register_fragment_profile(const TUniqueId& query_id, const TNetworkAddress& const_addr, |
73 | | int32_t fragment_id, |
74 | | std::vector<std::shared_ptr<TRuntimeProfileTree>> p_profiles, |
75 | | std::shared_ptr<TRuntimeProfileTree> load_channel_profile_x); |
76 | | // When query is finished, try to report query profiles to FE. |
77 | | // ATTN: Profile is reported to fe fragment by fragment. |
78 | | void trigger_profile_reporting(); |
79 | | |
80 | | private: |
81 | | friend class RuntimeQueryStatisticsMgrTestPeer; |
82 | | |
83 | | enum class FinalStatisticsAction { KEEP_RESOURCE_CONTEXT, COMPACT_RESOURCE_CONTEXT, REMOVE }; |
84 | | |
85 | | static bool should_remove_finished_query(bool response_received, bool accepted, |
86 | | int64_t elapsed_after_finish_ms, |
87 | | int64_t local_retention_ms, int64_t fe_retention_ms, |
88 | | int64_t no_response_retention_ms); |
89 | | static FinalStatisticsAction final_statistics_action(bool response_received, bool accepted, |
90 | | int64_t elapsed_after_finish_ms, |
91 | | int64_t local_retention_ms, |
92 | | int64_t fe_retention_ms, |
93 | | int64_t no_response_retention_ms); |
94 | | |
95 | | struct PendingFinalStatistics { |
96 | | TNetworkAddress fe_addr; |
97 | | std::shared_ptr<TQueryStatisticsResult> statistics; |
98 | | int64_t finish_time_ms; |
99 | | }; |
100 | | |
101 | | std::shared_mutex _resource_contexts_map_lock; |
102 | | // Query execution may end before its final statistics lifecycle, so the live registry owns |
103 | | // the ResourceContext until acceptance or conversion to a compact retry snapshot. |
104 | | std::map<std::string, std::shared_ptr<ResourceContext>> _resource_contexts_map; |
105 | | // Finished queries shed their ResourceContext after the local timeout, but this compact final |
106 | | // snapshot remains retryable until FE acceptance or the FE-provided audit window expires. |
107 | | std::map<std::string, PendingFinalStatistics> _pending_final_statistics; |
108 | | std::atomic<int64_t> _next_query_statistics_generation; |
109 | | |
110 | | std::atomic_bool started = false; |
111 | | std::mutex _profile_map_lock; |
112 | | |
113 | | // query_id -> {coordinator_addr, {fragment_id -> std::vector<pipeline_profile>}} |
114 | | std::unordered_map< |
115 | | TUniqueId, |
116 | | std::tuple<TNetworkAddress, |
117 | | std::unordered_map<int, std::vector<std::shared_ptr<TRuntimeProfileTree>>>>> |
118 | | _profile_map; |
119 | | |
120 | | std::unordered_map<std::pair<TUniqueId, int32_t>, std::shared_ptr<TRuntimeProfileTree>> |
121 | | _load_channel_profile_map; |
122 | | |
123 | | std::unique_ptr<ThreadPool> _thread_pool; |
124 | | }; |
125 | | |
126 | | } // namespace doris |