Coverage Report

Created: 2026-03-11 11:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/runtime/fragment_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/FrontendService_types.h>
21
#include <gen_cpp/QueryPlanExtra_types.h>
22
#include <gen_cpp/Types_types.h>
23
#include <gen_cpp/types.pb.h>
24
25
#include <cstdint>
26
#include <functional>
27
#include <iosfwd>
28
#include <memory>
29
#include <mutex>
30
#include <string>
31
#include <unordered_map>
32
#include <vector>
33
34
#include "common/be_mock_util.h"
35
#include "common/metrics/metrics.h"
36
#include "common/status.h"
37
#include "exec/runtime_filter/runtime_filter_mgr.h"
38
#include "runtime/query_context.h"
39
#include "service/http/rest_monitor_iface.h"
40
#include "util/countdown_latch.h"
41
#include "util/hash_util.hpp" // IWYU pragma: keep
42
43
namespace butil {
44
class IOBufAsZeroCopyInputStream;
45
}
46
47
namespace doris {
48
#include "common/compile_check_begin.h"
49
extern bvar::Adder<uint64_t> g_fragment_executing_count;
50
extern bvar::Status<uint64_t> g_fragment_last_active_time;
51
52
class PipelineFragmentContext;
53
class QueryContext;
54
class ExecEnv;
55
class ThreadPool;
56
class PExecPlanFragmentStartRequest;
57
class PMergeFilterRequest;
58
class RuntimeProfile;
59
class RuntimeState;
60
class TPipelineFragmentParams;
61
class TPipelineInstanceParams;
62
class TScanColumnDesc;
63
class TScanOpenParams;
64
class Thread;
65
class WorkloadQueryInfo;
66
67
std::string to_load_error_http_path(const std::string& file_name);
68
69
template <typename Key, typename Value, typename ValueType>
70
class ConcurrentContextMap {
71
public:
72
    using ApplyFunction = std::function<Status(phmap::flat_hash_map<Key, Value>&)>;
73
    ConcurrentContextMap();
74
    Value find(const Key& query_id);
75
    void insert(const Key& query_id, std::shared_ptr<ValueType>);
76
    void clear();
77
    void erase(const Key& query_id);
78
13
    size_t num_items() const {
79
13
        size_t n = 0;
80
1.66k
        for (auto& pair : _internal_map) {
81
1.66k
            std::shared_lock lock(*pair.first);
82
1.66k
            auto& map = pair.second;
83
1.66k
            n += map.size();
84
1.66k
        }
85
13
        return n;
86
13
    }
_ZNK5doris20ConcurrentContextMapINS_9TUniqueIdESt8weak_ptrINS_12QueryContextEES3_E9num_itemsEv
Line
Count
Source
78
13
    size_t num_items() const {
79
13
        size_t n = 0;
80
1.66k
        for (auto& pair : _internal_map) {
81
1.66k
            std::shared_lock lock(*pair.first);
82
1.66k
            auto& map = pair.second;
83
1.66k
            n += map.size();
84
1.66k
        }
85
13
        return n;
86
13
    }
Unexecuted instantiation: _ZNK5doris20ConcurrentContextMapISt4pairINS_9TUniqueIdEiESt10shared_ptrINS_23PipelineFragmentContextEES5_E9num_itemsEv
87
39
    void apply(ApplyFunction&& function) {
88
4.99k
        for (auto& pair : _internal_map) {
89
            // TODO: Now only the cancel worker do the GC the _query_ctx_map. each query must
90
            // do erase the finish query unless in _query_ctx_map. Rethink the logic is ok
91
4.99k
            std::unique_lock lock(*pair.first);
92
4.99k
            static_cast<void>(function(pair.second));
93
4.99k
        }
94
39
    }
_ZN5doris20ConcurrentContextMapISt4pairINS_9TUniqueIdEiESt10shared_ptrINS_23PipelineFragmentContextEES5_E5applyEOSt8functionIFNS_6StatusERN5phmap13flat_hash_mapIS3_S6_NSA_4HashIS3_EENSA_7EqualToIS3_EESaIS1_IKS3_S6_EEEEEE
Line
Count
Source
87
13
    void apply(ApplyFunction&& function) {
88
1.66k
        for (auto& pair : _internal_map) {
89
            // TODO: Now only the cancel worker do the GC the _query_ctx_map. each query must
90
            // do erase the finish query unless in _query_ctx_map. Rethink the logic is ok
91
1.66k
            std::unique_lock lock(*pair.first);
92
1.66k
            static_cast<void>(function(pair.second));
93
1.66k
        }
94
13
    }
_ZN5doris20ConcurrentContextMapINS_9TUniqueIdESt8weak_ptrINS_12QueryContextEES3_E5applyEOSt8functionIFNS_6StatusERN5phmap13flat_hash_mapIS1_S4_NS8_4HashIS1_EENS8_7EqualToIS1_EESaISt4pairIKS1_S4_EEEEEE
Line
Count
Source
87
26
    void apply(ApplyFunction&& function) {
88
3.32k
        for (auto& pair : _internal_map) {
89
            // TODO: Now only the cancel worker do the GC the _query_ctx_map. each query must
90
            // do erase the finish query unless in _query_ctx_map. Rethink the logic is ok
91
3.32k
            std::unique_lock lock(*pair.first);
92
3.32k
            static_cast<void>(function(pair.second));
93
3.32k
        }
94
26
    }
95
96
    Status apply_if_not_exists(const Key& query_id, std::shared_ptr<ValueType>& query_ctx,
97
                               ApplyFunction&& function);
98
99
private:
100
    // The lock should only be used to protect the structures in fragment manager. Has to be
101
    // used in a very small scope because it may dead lock. For example, if the _lock is used
102
    // in prepare stage, the call path is  prepare --> expr prepare --> may call allocator
103
    // when allocate failed, allocator may call query_is_cancelled, query is callced will also
104
    // call _lock, so that there is dead lock.
105
    std::vector<std::pair<std::unique_ptr<std::shared_mutex>, phmap::flat_hash_map<Key, Value>>>
106
            _internal_map;
107
};
108
109
// This class used to manage all the fragment execute in this instance
110
class FragmentMgr : public RestMonitorIface {
111
public:
112
    using FinishCallback = std::function<void(RuntimeState*, Status*)>;
113
114
    FragmentMgr(ExecEnv* exec_env);
115
    ~FragmentMgr() override;
116
117
    void stop();
118
119
    // execute one plan fragment
120
121
    Status exec_plan_fragment(const TPipelineFragmentParams& params, const QuerySource query_type,
122
                              const TPipelineFragmentParamsList& parent);
123
124
    void remove_pipeline_context(std::pair<TUniqueId, int> key);
125
    void remove_query_context(const TUniqueId& key);
126
127
    // `is_prepare_success` is used by invoker to ensure callback can be handle correctly (eg. stream_load_executor)
128
    Status exec_plan_fragment(const TPipelineFragmentParams& params, const QuerySource query_type,
129
                              const FinishCallback& cb, const TPipelineFragmentParamsList& parent,
130
                              std::shared_ptr<bool> is_prepare_success = nullptr);
131
132
    Status start_query_execution(const PExecPlanFragmentStartRequest* request);
133
134
    Status trigger_pipeline_context_report(const ReportStatusRequest,
135
                                           std::shared_ptr<PipelineFragmentContext>&&);
136
137
    // Can be used in both version.
138
    MOCK_FUNCTION void cancel_query(const TUniqueId query_id, const Status reason);
139
140
    void cancel_worker();
141
142
    void debug(std::stringstream& ss) override;
143
144
    // input: TQueryPlanInfo fragment_instance_id
145
    // output: selected_columns
146
    // execute external query, all query info are packed in TScanOpenParams
147
    Status exec_external_plan_fragment(const TScanOpenParams& params,
148
                                       const TQueryPlanInfo& t_query_plan_info,
149
                                       const TUniqueId& query_id,
150
                                       const TUniqueId& fragment_instance_id,
151
                                       std::vector<TScanColumnDesc>* selected_columns);
152
153
    Status apply_filterv2(const PPublishFilterRequestV2* request,
154
                          butil::IOBufAsZeroCopyInputStream* attach_data);
155
156
    Status merge_filter(const PMergeFilterRequest* request,
157
                        butil::IOBufAsZeroCopyInputStream* attach_data);
158
159
    Status send_filter_size(const PSendFilterSizeRequest* request);
160
161
    Status sync_filter_size(const PSyncFilterSizeRequest* request);
162
163
    std::string to_http_path(const std::string& file_name);
164
165
    void coordinator_callback(const ReportStatusRequest& req);
166
167
0
    ThreadPool* get_thread_pool() { return _thread_pool.get(); }
168
169
    // When fragment mgr is going to stop, the _stop_background_threads_latch is set to 0
170
    // and other module that use fragment mgr's thread pool should get this signal and exit.
171
0
    bool shutting_down() { return _stop_background_threads_latch.count() == 0; }
172
173
0
    int32_t running_query_num() { return cast_set<int32_t>(_query_ctx_map.num_items()); }
174
175
    std::string dump_pipeline_tasks(int64_t duration = 0);
176
    std::string dump_pipeline_tasks(TUniqueId& query_id);
177
178
    void get_runtime_query_info(std::vector<std::weak_ptr<ResourceContext>>* _resource_ctx_list);
179
180
    Status get_realtime_exec_status(const TUniqueId& query_id,
181
                                    TReportExecStatusParams* exec_status);
182
    // get the query statistics of with a given query id
183
    Status get_query_statistics(const TUniqueId& query_id, TQueryStatistics* query_stats);
184
185
    std::shared_ptr<QueryContext> get_query_ctx(const TUniqueId& query_id);
186
187
    Status transmit_rec_cte_block(const TUniqueId& query_id, const TUniqueId& instance_id,
188
                                  int node_id,
189
                                  const google::protobuf::RepeatedPtrField<PBlock>& pblocks,
190
                                  bool eos);
191
192
    Status rerun_fragment(const TUniqueId& query_id, int fragment,
193
                          PRerunFragmentParams_Opcode stage);
194
195
    Status reset_global_rf(const TUniqueId& query_id,
196
                           const google::protobuf::RepeatedField<int32_t>& filter_ids);
197
198
private:
199
    struct BrpcItem {
200
        TNetworkAddress network_address;
201
        std::vector<std::weak_ptr<QueryContext>> queries;
202
    };
203
204
    Status _get_or_create_query_ctx(const TPipelineFragmentParams& params,
205
                                    const TPipelineFragmentParamsList& parent,
206
                                    QuerySource query_type,
207
                                    std::shared_ptr<QueryContext>& query_ctx);
208
209
    void _collect_timeout_queries_and_brpc_items(
210
            std::vector<TUniqueId>& queries_timeout,
211
            std::unordered_map<std::shared_ptr<PBackendService_Stub>, BrpcItem>&
212
                    brpc_stub_with_queries,
213
            timespec now);
214
215
    void _collect_invalid_queries(
216
            std::vector<TUniqueId>& queries_lost_coordinator,
217
            std::vector<TUniqueId>& queries_pipeline_task_leak,
218
            const std::map<int64_t, std::unordered_set<TUniqueId>>& running_queries_on_all_fes,
219
            const std::map<TNetworkAddress, FrontendInfo>& running_fes,
220
            timespec check_invalid_query_last_timestamp);
221
222
    void _check_brpc_available(const std::shared_ptr<PBackendService_Stub>& brpc_stub,
223
                               const BrpcItem& brpc_item);
224
225
    // This is input params
226
    ExecEnv* _exec_env = nullptr;
227
228
    // (QueryID, FragmentID) -> PipelineFragmentContext
229
    ConcurrentContextMap<std::pair<TUniqueId, int>, std::shared_ptr<PipelineFragmentContext>,
230
                         PipelineFragmentContext>
231
            _pipeline_map;
232
233
    // query id -> QueryContext
234
    ConcurrentContextMap<TUniqueId, std::weak_ptr<QueryContext>, QueryContext> _query_ctx_map;
235
    // keep query ctx do not delete immediately to make rf coordinator merge filter work well after query eos
236
    ConcurrentContextMap<TUniqueId, std::shared_ptr<QueryContext>, QueryContext>
237
            _query_ctx_map_delay_delete;
238
239
    CountDownLatch _stop_background_threads_latch;
240
    std::shared_ptr<Thread> _cancel_thread;
241
    // This pool is used as global async task pool
242
    std::unique_ptr<ThreadPool> _thread_pool;
243
244
    std::shared_ptr<MetricEntity> _entity;
245
    UIntGauge* timeout_canceled_fragment_count = nullptr;
246
};
247
248
uint64_t get_fragment_executing_count();
249
uint64_t get_fragment_last_active_time();
250
#include "common/compile_check_end.h"
251
} // namespace doris