Coverage Report

Created: 2025-04-30 19:30

/root/doris/be/src/runtime/fragment_mgr.cpp
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
#include "runtime/fragment_mgr.h"
19
20
#include <bvar/latency_recorder.h>
21
#include <exprs/runtime_filter.h>
22
#include <fmt/format.h>
23
#include <gen_cpp/DorisExternalService_types.h>
24
#include <gen_cpp/FrontendService.h>
25
#include <gen_cpp/FrontendService_types.h>
26
#include <gen_cpp/HeartbeatService_types.h>
27
#include <gen_cpp/Metrics_types.h>
28
#include <gen_cpp/PaloInternalService_types.h>
29
#include <gen_cpp/PlanNodes_types.h>
30
#include <gen_cpp/Planner_types.h>
31
#include <gen_cpp/QueryPlanExtra_types.h>
32
#include <gen_cpp/RuntimeProfile_types.h>
33
#include <gen_cpp/Types_types.h>
34
#include <gen_cpp/internal_service.pb.h>
35
#include <pthread.h>
36
#include <sys/time.h>
37
#include <thrift/TApplicationException.h>
38
#include <thrift/Thrift.h>
39
#include <thrift/protocol/TDebugProtocol.h>
40
#include <thrift/transport/TTransportException.h>
41
#include <unistd.h>
42
43
#include <algorithm>
44
#include <cstddef>
45
#include <ctime>
46
47
#include "common/status.h"
48
// IWYU pragma: no_include <bits/chrono.h>
49
#include <chrono> // IWYU pragma: keep
50
#include <cstdint>
51
#include <map>
52
#include <memory>
53
#include <mutex>
54
#include <sstream>
55
#include <unordered_map>
56
#include <unordered_set>
57
#include <utility>
58
59
#include "common/config.h"
60
#include "common/logging.h"
61
#include "common/object_pool.h"
62
#include "common/utils.h"
63
#include "io/fs/stream_load_pipe.h"
64
#include "pipeline/pipeline_fragment_context.h"
65
#include "runtime/client_cache.h"
66
#include "runtime/descriptors.h"
67
#include "runtime/exec_env.h"
68
#include "runtime/frontend_info.h"
69
#include "runtime/primitive_type.h"
70
#include "runtime/query_context.h"
71
#include "runtime/runtime_filter_mgr.h"
72
#include "runtime/runtime_query_statistics_mgr.h"
73
#include "runtime/runtime_state.h"
74
#include "runtime/stream_load/new_load_stream_mgr.h"
75
#include "runtime/stream_load/stream_load_context.h"
76
#include "runtime/stream_load/stream_load_executor.h"
77
#include "runtime/thread_context.h"
78
#include "runtime/types.h"
79
#include "runtime/workload_group/workload_group.h"
80
#include "runtime/workload_group/workload_group_manager.h"
81
#include "runtime/workload_management/workload_query_info.h"
82
#include "service/backend_options.h"
83
#include "util/debug_points.h"
84
#include "util/debug_util.h"
85
#include "util/doris_metrics.h"
86
#include "util/network_util.h"
87
#include "util/runtime_profile.h"
88
#include "util/thread.h"
89
#include "util/threadpool.h"
90
#include "util/thrift_util.h"
91
#include "util/uid_util.h"
92
#include "vec/runtime/shared_hash_table_controller.h"
93
94
namespace doris {
95
96
DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(fragment_instance_count, MetricUnit::NOUNIT);
97
DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(timeout_canceled_fragment_count, MetricUnit::NOUNIT);
98
99
bvar::LatencyRecorder g_fragmentmgr_prepare_latency("doris_FragmentMgr", "prepare");
100
101
bvar::Adder<uint64_t> g_fragment_executing_count("fragment_executing_count");
102
bvar::Status<uint64_t> g_fragment_last_active_time(
103
        "fragment_last_active_time", duration_cast<std::chrono::milliseconds>(
104
                                             std::chrono::system_clock::now().time_since_epoch())
105
                                             .count());
106
107
0
uint64_t get_fragment_executing_count() {
108
0
    return g_fragment_executing_count.get_value();
109
0
}
110
0
uint64_t get_fragment_last_active_time() {
111
0
    return g_fragment_last_active_time.get_value();
112
0
}
113
114
0
std::string to_load_error_http_path(const std::string& file_name) {
115
0
    if (file_name.empty()) {
116
0
        return "";
117
0
    }
118
0
    if (file_name.compare(0, 4, "http") == 0) {
119
0
        return file_name;
120
0
    }
121
0
    std::stringstream url;
122
0
    url << "http://" << get_host_port(BackendOptions::get_localhost(), config::webserver_port)
123
0
        << "/api/_load_error_log?"
124
0
        << "file=" << file_name;
125
0
    return url.str();
126
0
}
127
128
using apache::thrift::TException;
129
using apache::thrift::transport::TTransportException;
130
131
static Status _do_fetch_running_queries_rpc(const FrontendInfo& fe_info,
132
0
                                            std::unordered_set<TUniqueId>& query_set) {
133
0
    TFetchRunningQueriesResult rpc_result;
134
0
    TFetchRunningQueriesRequest rpc_request;
135
136
0
    Status client_status;
137
0
    const int32 timeout_ms = 3 * 1000;
138
0
    FrontendServiceConnection rpc_client(ExecEnv::GetInstance()->frontend_client_cache(),
139
0
                                         fe_info.info.coordinator_address, timeout_ms,
140
0
                                         &client_status);
141
    // Abort this fe.
142
0
    if (!client_status.ok()) {
143
0
        LOG_WARNING("Failed to get client for {}, reason is {}",
144
0
                    PrintThriftNetworkAddress(fe_info.info.coordinator_address),
145
0
                    client_status.to_string());
146
0
        return Status::InternalError("Failed to get client for {}, reason is {}",
147
0
                                     PrintThriftNetworkAddress(fe_info.info.coordinator_address),
148
0
                                     client_status.to_string());
149
0
    }
150
151
    // do rpc
152
0
    try {
153
0
        try {
154
0
            rpc_client->fetchRunningQueries(rpc_result, rpc_request);
155
0
        } catch (const apache::thrift::transport::TTransportException& e) {
156
0
            LOG_WARNING("Transport exception reason: {}, reopening", e.what());
157
0
            client_status = rpc_client.reopen(config::thrift_rpc_timeout_ms);
158
0
            if (!client_status.ok()) {
159
0
                LOG_WARNING("Reopen failed, reason: {}", client_status.to_string_no_stack());
160
0
                return Status::InternalError("Reopen failed, reason: {}",
161
0
                                             client_status.to_string_no_stack());
162
0
            }
163
164
0
            rpc_client->fetchRunningQueries(rpc_result, rpc_request);
165
0
        }
166
0
    } catch (apache::thrift::TException& e) {
167
        // During upgrading cluster or meet any other network error.
168
0
        LOG_WARNING("Failed to fetch running queries from {}, reason: {}",
169
0
                    PrintThriftNetworkAddress(fe_info.info.coordinator_address), e.what());
170
0
        return Status::InternalError("Failed to fetch running queries from {}, reason: {}",
171
0
                                     PrintThriftNetworkAddress(fe_info.info.coordinator_address),
172
0
                                     e.what());
173
0
    }
174
175
    // Avoid logic error in frontend.
176
0
    if (!rpc_result.__isset.status || rpc_result.status.status_code != TStatusCode::OK) {
177
0
        LOG_WARNING("Failed to fetch running queries from {}, reason: {}",
178
0
                    PrintThriftNetworkAddress(fe_info.info.coordinator_address),
179
0
                    doris::to_string(rpc_result.status.status_code));
180
0
        return Status::InternalError("Failed to fetch running queries from {}, reason: {}",
181
0
                                     PrintThriftNetworkAddress(fe_info.info.coordinator_address),
182
0
                                     doris::to_string(rpc_result.status.status_code));
183
0
    }
184
185
0
    if (!rpc_result.__isset.running_queries) {
186
0
        return Status::InternalError("Failed to fetch running queries from {}, reason: {}",
187
0
                                     PrintThriftNetworkAddress(fe_info.info.coordinator_address),
188
0
                                     "running_queries is not set");
189
0
    }
190
191
0
    query_set = std::unordered_set<TUniqueId>(rpc_result.running_queries.begin(),
192
0
                                              rpc_result.running_queries.end());
193
0
    return Status::OK();
194
0
};
195
196
0
static std::map<int64_t, std::unordered_set<TUniqueId>> _get_all_running_queries_from_fe() {
197
0
    const std::map<TNetworkAddress, FrontendInfo>& running_fes =
198
0
            ExecEnv::GetInstance()->get_running_frontends();
199
200
0
    std::map<int64_t, std::unordered_set<TUniqueId>> result;
201
0
    std::vector<FrontendInfo> qualified_fes;
202
203
0
    for (const auto& fe : running_fes) {
204
        // Only consider normal frontend.
205
0
        if (fe.first.port != 0 && fe.second.info.process_uuid != 0) {
206
0
            qualified_fes.push_back(fe.second);
207
0
        } else {
208
0
            return {};
209
0
        }
210
0
    }
211
212
0
    for (const auto& fe_addr : qualified_fes) {
213
0
        const int64_t process_uuid = fe_addr.info.process_uuid;
214
0
        std::unordered_set<TUniqueId> query_set;
215
0
        Status st = _do_fetch_running_queries_rpc(fe_addr, query_set);
216
0
        if (!st.ok()) {
217
            // Empty result, cancel worker will not do anything
218
0
            return {};
219
0
        }
220
221
        // frontend_info and process_uuid has been checked in rpc threads.
222
0
        result[process_uuid] = query_set;
223
0
    }
224
225
0
    return result;
226
0
}
227
228
1
inline uint32_t get_map_id(const TUniqueId& query_id, size_t capacity) {
229
1
    uint32_t value = HashUtil::hash(&query_id.lo, 8, 0);
230
1
    value = HashUtil::hash(&query_id.hi, 8, value);
231
1
    return value % capacity;
232
1
}
233
234
0
inline uint32_t get_map_id(std::pair<TUniqueId, int> key, size_t capacity) {
235
0
    uint32_t value = HashUtil::hash(&key.first.lo, 8, 0);
236
0
    value = HashUtil::hash(&key.first.hi, 8, value);
237
0
    return value % capacity;
238
0
}
239
240
template <typename Key, typename Value, typename ValueType>
241
8
ConcurrentContextMap<Key, Value, ValueType>::ConcurrentContextMap() {
242
8
    _internal_map.resize(config::num_query_ctx_map_partitions);
243
1.03k
    for (size_t i = 0; i < config::num_query_ctx_map_partitions; i++) {
244
1.02k
        _internal_map[i] = {std::make_unique<std::shared_mutex>(),
245
1.02k
                            phmap::flat_hash_map<Key, Value>()};
246
1.02k
    }
247
8
}
_ZN5doris20ConcurrentContextMapISt4pairINS_9TUniqueIdEiESt10shared_ptrINS_8pipeline23PipelineFragmentContextEES6_EC2Ev
Line
Count
Source
241
4
ConcurrentContextMap<Key, Value, ValueType>::ConcurrentContextMap() {
242
4
    _internal_map.resize(config::num_query_ctx_map_partitions);
243
516
    for (size_t i = 0; i < config::num_query_ctx_map_partitions; i++) {
244
512
        _internal_map[i] = {std::make_unique<std::shared_mutex>(),
245
512
                            phmap::flat_hash_map<Key, Value>()};
246
512
    }
247
4
}
_ZN5doris20ConcurrentContextMapINS_9TUniqueIdESt8weak_ptrINS_12QueryContextEES3_EC2Ev
Line
Count
Source
241
4
ConcurrentContextMap<Key, Value, ValueType>::ConcurrentContextMap() {
242
4
    _internal_map.resize(config::num_query_ctx_map_partitions);
243
516
    for (size_t i = 0; i < config::num_query_ctx_map_partitions; i++) {
244
512
        _internal_map[i] = {std::make_unique<std::shared_mutex>(),
245
512
                            phmap::flat_hash_map<Key, Value>()};
246
512
    }
247
4
}
248
249
template <typename Key, typename Value, typename ValueType>
250
1
Value ConcurrentContextMap<Key, Value, ValueType>::find(const Key& query_id) {
251
1
    auto id = get_map_id(query_id, _internal_map.size());
252
1
    {
253
1
        std::shared_lock lock(*_internal_map[id].first);
254
1
        auto& map = _internal_map[id].second;
255
1
        auto search = map.find(query_id);
256
1
        if (search != map.end()) {
257
0
            return search->second;
258
0
        }
259
1
        return std::shared_ptr<ValueType>(nullptr);
260
1
    }
261
1
}
_ZN5doris20ConcurrentContextMapINS_9TUniqueIdESt8weak_ptrINS_12QueryContextEES3_E4findERKS1_
Line
Count
Source
250
1
Value ConcurrentContextMap<Key, Value, ValueType>::find(const Key& query_id) {
251
1
    auto id = get_map_id(query_id, _internal_map.size());
252
1
    {
253
1
        std::shared_lock lock(*_internal_map[id].first);
254
1
        auto& map = _internal_map[id].second;
255
1
        auto search = map.find(query_id);
256
1
        if (search != map.end()) {
257
0
            return search->second;
258
0
        }
259
1
        return std::shared_ptr<ValueType>(nullptr);
260
1
    }
261
1
}
Unexecuted instantiation: _ZN5doris20ConcurrentContextMapISt4pairINS_9TUniqueIdEiESt10shared_ptrINS_8pipeline23PipelineFragmentContextEES6_E4findERKS3_
262
263
template <typename Key, typename Value, typename ValueType>
264
Status ConcurrentContextMap<Key, Value, ValueType>::apply_if_not_exists(
265
0
        const Key& query_id, std::shared_ptr<ValueType>& query_ctx, ApplyFunction&& function) {
266
0
    auto id = get_map_id(query_id, _internal_map.size());
267
0
    {
268
0
        std::unique_lock lock(*_internal_map[id].first);
269
0
        auto& map = _internal_map[id].second;
270
0
        auto search = map.find(query_id);
271
0
        if (search != map.end()) {
272
0
            query_ctx = search->second.lock();
273
0
        }
274
0
        if (!query_ctx) {
275
0
            return function(map);
276
0
        }
277
0
        return Status::OK();
278
0
    }
279
0
}
280
281
template <typename Key, typename Value, typename ValueType>
282
0
void ConcurrentContextMap<Key, Value, ValueType>::erase(const Key& query_id) {
283
0
    auto id = get_map_id(query_id, _internal_map.size());
284
0
    {
285
0
        std::unique_lock lock(*_internal_map[id].first);
286
0
        auto& map = _internal_map[id].second;
287
0
        map.erase(query_id);
288
0
    }
289
0
}
Unexecuted instantiation: _ZN5doris20ConcurrentContextMapISt4pairINS_9TUniqueIdEiESt10shared_ptrINS_8pipeline23PipelineFragmentContextEES6_E5eraseERKS3_
Unexecuted instantiation: _ZN5doris20ConcurrentContextMapINS_9TUniqueIdESt8weak_ptrINS_12QueryContextEES3_E5eraseERKS1_
290
291
template <typename Key, typename Value, typename ValueType>
292
void ConcurrentContextMap<Key, Value, ValueType>::insert(const Key& query_id,
293
0
                                                         std::shared_ptr<ValueType> query_ctx) {
294
0
    auto id = get_map_id(query_id, _internal_map.size());
295
0
    {
296
0
        std::unique_lock lock(*_internal_map[id].first);
297
0
        auto& map = _internal_map[id].second;
298
0
        map.insert({query_id, query_ctx});
299
0
    }
300
0
}
301
302
template <typename Key, typename Value, typename ValueType>
303
8
void ConcurrentContextMap<Key, Value, ValueType>::clear() {
304
1.02k
    for (auto& pair : _internal_map) {
305
1.02k
        std::unique_lock lock(*pair.first);
306
1.02k
        auto& map = pair.second;
307
1.02k
        map.clear();
308
1.02k
    }
309
8
}
_ZN5doris20ConcurrentContextMapINS_9TUniqueIdESt8weak_ptrINS_12QueryContextEES3_E5clearEv
Line
Count
Source
303
4
void ConcurrentContextMap<Key, Value, ValueType>::clear() {
304
512
    for (auto& pair : _internal_map) {
305
512
        std::unique_lock lock(*pair.first);
306
512
        auto& map = pair.second;
307
512
        map.clear();
308
512
    }
309
4
}
_ZN5doris20ConcurrentContextMapISt4pairINS_9TUniqueIdEiESt10shared_ptrINS_8pipeline23PipelineFragmentContextEES6_E5clearEv
Line
Count
Source
303
4
void ConcurrentContextMap<Key, Value, ValueType>::clear() {
304
512
    for (auto& pair : _internal_map) {
305
512
        std::unique_lock lock(*pair.first);
306
512
        auto& map = pair.second;
307
512
        map.clear();
308
512
    }
309
4
}
310
311
FragmentMgr::FragmentMgr(ExecEnv* exec_env)
312
4
        : _exec_env(exec_env), _stop_background_threads_latch(1) {
313
4
    _entity = DorisMetrics::instance()->metric_registry()->register_entity("FragmentMgr");
314
4
    INT_UGAUGE_METRIC_REGISTER(_entity, timeout_canceled_fragment_count);
315
316
4
    auto s = Thread::create(
317
4
            "FragmentMgr", "cancel_timeout_plan_fragment", [this]() { this->cancel_worker(); },
318
4
            &_cancel_thread);
319
4
    CHECK(s.ok()) << s.to_string();
320
321
4
    s = ThreadPoolBuilder("FragmentMgrAsyncWorkThreadPool")
322
4
                .set_min_threads(config::fragment_mgr_asynic_work_pool_thread_num_min)
323
4
                .set_max_threads(config::fragment_mgr_asynic_work_pool_thread_num_max)
324
4
                .set_max_queue_size(config::fragment_mgr_asynic_work_pool_queue_size)
325
4
                .build(&_thread_pool);
326
4
    CHECK(s.ok()) << s.to_string();
327
4
}
328
329
4
FragmentMgr::~FragmentMgr() = default;
330
331
4
void FragmentMgr::stop() {
332
4
    DEREGISTER_HOOK_METRIC(fragment_instance_count);
333
4
    _stop_background_threads_latch.count_down();
334
4
    if (_cancel_thread) {
335
4
        _cancel_thread->join();
336
4
    }
337
338
    // Only me can delete
339
4
    _query_ctx_map.clear();
340
4
    _pipeline_map.clear();
341
4
    _thread_pool->shutdown();
342
4
}
343
344
0
std::string FragmentMgr::to_http_path(const std::string& file_name) {
345
0
    std::stringstream url;
346
0
    url << "http://" << BackendOptions::get_localhost() << ":" << config::webserver_port
347
0
        << "/api/_download_load?"
348
0
        << "token=" << _exec_env->token() << "&file=" << file_name;
349
0
    return url.str();
350
0
}
351
352
Status FragmentMgr::trigger_pipeline_context_report(
353
0
        const ReportStatusRequest req, std::shared_ptr<pipeline::PipelineFragmentContext>&& ctx) {
354
0
    return _thread_pool->submit_func([this, req, ctx]() {
355
0
        SCOPED_ATTACH_TASK(ctx->get_query_ctx()->query_mem_tracker);
356
0
        coordinator_callback(req);
357
0
        if (!req.done) {
358
0
            ctx->refresh_next_report_time();
359
0
        }
360
0
    });
361
0
}
362
363
// There can only be one of these callbacks in-flight at any moment, because
364
// it is only invoked from the executor's reporting thread.
365
// Also, the reported status will always reflect the most recent execution status,
366
// including the final status when execution finishes.
367
0
void FragmentMgr::coordinator_callback(const ReportStatusRequest& req) {
368
0
    DCHECK(req.status.ok() || req.done); // if !status.ok() => done
369
0
    if (req.coord_addr.hostname == "external") {
370
        // External query (flink/spark read tablets) not need to report to FE.
371
0
        return;
372
0
    }
373
0
    Status exec_status = req.status;
374
0
    Status coord_status;
375
0
    FrontendServiceConnection coord(_exec_env->frontend_client_cache(), req.coord_addr,
376
0
                                    &coord_status);
377
0
    if (!coord_status.ok()) {
378
0
        std::stringstream ss;
379
0
        UniqueId uid(req.query_id.hi, req.query_id.lo);
380
0
        static_cast<void>(req.cancel_fn(Status::InternalError(
381
0
                "query_id: {}, couldn't get a client for {}, reason is {}", uid.to_string(),
382
0
                PrintThriftNetworkAddress(req.coord_addr), coord_status.to_string())));
383
0
        return;
384
0
    }
385
386
0
    TReportExecStatusParams params;
387
0
    params.protocol_version = FrontendServiceVersion::V1;
388
0
    params.__set_query_id(req.query_id);
389
0
    params.__set_backend_num(req.backend_num);
390
0
    params.__set_fragment_instance_id(req.fragment_instance_id);
391
0
    params.__set_fragment_id(req.fragment_id);
392
0
    params.__set_status(exec_status.to_thrift());
393
0
    params.__set_done(req.done);
394
0
    params.__set_query_type(req.runtime_state->query_type());
395
0
    params.__isset.profile = false;
396
397
0
    DCHECK(req.runtime_state != nullptr);
398
399
0
    if (req.runtime_state->query_type() == TQueryType::LOAD) {
400
0
        params.__set_loaded_rows(req.runtime_state->num_rows_load_total());
401
0
        params.__set_loaded_bytes(req.runtime_state->num_bytes_load_total());
402
0
    } else {
403
0
        DCHECK(!req.runtime_states.empty());
404
0
        if (!req.runtime_state->output_files().empty()) {
405
0
            params.__isset.delta_urls = true;
406
0
            for (auto& it : req.runtime_state->output_files()) {
407
0
                params.delta_urls.push_back(to_http_path(it));
408
0
            }
409
0
        }
410
0
        if (!params.delta_urls.empty()) {
411
0
            params.__isset.delta_urls = true;
412
0
        }
413
0
    }
414
415
    // load rows
416
0
    static std::string s_dpp_normal_all = "dpp.norm.ALL";
417
0
    static std::string s_dpp_abnormal_all = "dpp.abnorm.ALL";
418
0
    static std::string s_unselected_rows = "unselected.rows";
419
0
    int64_t num_rows_load_success = 0;
420
0
    int64_t num_rows_load_filtered = 0;
421
0
    int64_t num_rows_load_unselected = 0;
422
0
    if (req.runtime_state->num_rows_load_total() > 0 ||
423
0
        req.runtime_state->num_rows_load_filtered() > 0 ||
424
0
        req.runtime_state->num_finished_range() > 0) {
425
0
        params.__isset.load_counters = true;
426
427
0
        num_rows_load_success = req.runtime_state->num_rows_load_success();
428
0
        num_rows_load_filtered = req.runtime_state->num_rows_load_filtered();
429
0
        num_rows_load_unselected = req.runtime_state->num_rows_load_unselected();
430
0
        params.__isset.fragment_instance_reports = true;
431
0
        TFragmentInstanceReport t;
432
0
        t.__set_fragment_instance_id(req.runtime_state->fragment_instance_id());
433
0
        t.__set_num_finished_range(req.runtime_state->num_finished_range());
434
0
        t.__set_loaded_rows(req.runtime_state->num_rows_load_total());
435
0
        t.__set_loaded_bytes(req.runtime_state->num_bytes_load_total());
436
0
        params.fragment_instance_reports.push_back(t);
437
0
    } else if (!req.runtime_states.empty()) {
438
0
        for (auto* rs : req.runtime_states) {
439
0
            if (rs->num_rows_load_total() > 0 || rs->num_rows_load_filtered() > 0 ||
440
0
                req.runtime_state->num_finished_range() > 0) {
441
0
                params.__isset.load_counters = true;
442
0
                num_rows_load_success += rs->num_rows_load_success();
443
0
                num_rows_load_filtered += rs->num_rows_load_filtered();
444
0
                num_rows_load_unselected += rs->num_rows_load_unselected();
445
0
                params.__isset.fragment_instance_reports = true;
446
0
                TFragmentInstanceReport t;
447
0
                t.__set_fragment_instance_id(rs->fragment_instance_id());
448
0
                t.__set_num_finished_range(rs->num_finished_range());
449
0
                t.__set_loaded_rows(rs->num_rows_load_total());
450
0
                t.__set_loaded_bytes(rs->num_bytes_load_total());
451
0
                params.fragment_instance_reports.push_back(t);
452
0
            }
453
0
        }
454
0
    }
455
0
    params.load_counters.emplace(s_dpp_normal_all, std::to_string(num_rows_load_success));
456
0
    params.load_counters.emplace(s_dpp_abnormal_all, std::to_string(num_rows_load_filtered));
457
0
    params.load_counters.emplace(s_unselected_rows, std::to_string(num_rows_load_unselected));
458
459
0
    if (!req.load_error_url.empty()) {
460
0
        params.__set_tracking_url(req.load_error_url);
461
0
    }
462
0
    for (auto* rs : req.runtime_states) {
463
0
        if (rs->wal_id() > 0) {
464
0
            params.__set_txn_id(rs->wal_id());
465
0
            params.__set_label(rs->import_label());
466
0
        }
467
0
    }
468
0
    if (!req.runtime_state->export_output_files().empty()) {
469
0
        params.__isset.export_files = true;
470
0
        params.export_files = req.runtime_state->export_output_files();
471
0
    } else if (!req.runtime_states.empty()) {
472
0
        for (auto* rs : req.runtime_states) {
473
0
            if (!rs->export_output_files().empty()) {
474
0
                params.__isset.export_files = true;
475
0
                params.export_files.insert(params.export_files.end(),
476
0
                                           rs->export_output_files().begin(),
477
0
                                           rs->export_output_files().end());
478
0
            }
479
0
        }
480
0
    }
481
0
    if (auto tci = req.runtime_state->tablet_commit_infos(); !tci.empty()) {
482
0
        params.__isset.commitInfos = true;
483
0
        params.commitInfos.insert(params.commitInfos.end(), tci.begin(), tci.end());
484
0
    } else if (!req.runtime_states.empty()) {
485
0
        for (auto* rs : req.runtime_states) {
486
0
            if (auto rs_tci = rs->tablet_commit_infos(); !rs_tci.empty()) {
487
0
                params.__isset.commitInfos = true;
488
0
                params.commitInfos.insert(params.commitInfos.end(), rs_tci.begin(), rs_tci.end());
489
0
            }
490
0
        }
491
0
    }
492
0
    if (auto eti = req.runtime_state->error_tablet_infos(); !eti.empty()) {
493
0
        params.__isset.errorTabletInfos = true;
494
0
        params.errorTabletInfos.insert(params.errorTabletInfos.end(), eti.begin(), eti.end());
495
0
    } else if (!req.runtime_states.empty()) {
496
0
        for (auto* rs : req.runtime_states) {
497
0
            if (auto rs_eti = rs->error_tablet_infos(); !rs_eti.empty()) {
498
0
                params.__isset.errorTabletInfos = true;
499
0
                params.errorTabletInfos.insert(params.errorTabletInfos.end(), rs_eti.begin(),
500
0
                                               rs_eti.end());
501
0
            }
502
0
        }
503
0
    }
504
0
    if (auto hpu = req.runtime_state->hive_partition_updates(); !hpu.empty()) {
505
0
        params.__isset.hive_partition_updates = true;
506
0
        params.hive_partition_updates.insert(params.hive_partition_updates.end(), hpu.begin(),
507
0
                                             hpu.end());
508
0
    } else if (!req.runtime_states.empty()) {
509
0
        for (auto* rs : req.runtime_states) {
510
0
            if (auto rs_hpu = rs->hive_partition_updates(); !rs_hpu.empty()) {
511
0
                params.__isset.hive_partition_updates = true;
512
0
                params.hive_partition_updates.insert(params.hive_partition_updates.end(),
513
0
                                                     rs_hpu.begin(), rs_hpu.end());
514
0
            }
515
0
        }
516
0
    }
517
0
    if (auto icd = req.runtime_state->iceberg_commit_datas(); !icd.empty()) {
518
0
        params.__isset.iceberg_commit_datas = true;
519
0
        params.iceberg_commit_datas.insert(params.iceberg_commit_datas.end(), icd.begin(),
520
0
                                           icd.end());
521
0
    } else if (!req.runtime_states.empty()) {
522
0
        for (auto* rs : req.runtime_states) {
523
0
            if (auto rs_icd = rs->iceberg_commit_datas(); !rs_icd.empty()) {
524
0
                params.__isset.iceberg_commit_datas = true;
525
0
                params.iceberg_commit_datas.insert(params.iceberg_commit_datas.end(),
526
0
                                                   rs_icd.begin(), rs_icd.end());
527
0
            }
528
0
        }
529
0
    }
530
531
    // Send new errors to coordinator
532
0
    req.runtime_state->get_unreported_errors(&(params.error_log));
533
0
    params.__isset.error_log = (!params.error_log.empty());
534
535
0
    if (_exec_env->cluster_info()->backend_id != 0) {
536
0
        params.__set_backend_id(_exec_env->cluster_info()->backend_id);
537
0
    }
538
539
0
    TReportExecStatusResult res;
540
0
    Status rpc_status;
541
542
0
    VLOG_DEBUG << "reportExecStatus params is "
543
0
               << apache::thrift::ThriftDebugString(params).c_str();
544
0
    if (!exec_status.ok()) {
545
0
        LOG(WARNING) << "report error status: " << exec_status.msg()
546
0
                     << " to coordinator: " << req.coord_addr
547
0
                     << ", query id: " << print_id(req.query_id);
548
0
    }
549
0
    try {
550
0
        try {
551
0
            coord->reportExecStatus(res, params);
552
0
        } catch ([[maybe_unused]] TTransportException& e) {
553
#ifndef ADDRESS_SANITIZER
554
            LOG(WARNING) << "Retrying ReportExecStatus. query id: " << print_id(req.query_id)
555
                         << ", instance id: " << print_id(req.fragment_instance_id) << " to "
556
                         << req.coord_addr << ", err: " << e.what();
557
#endif
558
0
            rpc_status = coord.reopen();
559
560
0
            if (!rpc_status.ok()) {
561
                // we need to cancel the execution of this fragment
562
0
                req.cancel_fn(rpc_status);
563
0
                return;
564
0
            }
565
0
            coord->reportExecStatus(res, params);
566
0
        }
567
568
0
        rpc_status = Status::create<false>(res.status);
569
0
    } catch (TException& e) {
570
0
        rpc_status = Status::InternalError("ReportExecStatus() to {} failed: {}",
571
0
                                           PrintThriftNetworkAddress(req.coord_addr), e.what());
572
0
    }
573
574
0
    if (!rpc_status.ok()) {
575
0
        LOG_INFO("Going to cancel query {} since report exec status got rpc failed: {}",
576
0
                 print_id(req.query_id), rpc_status.to_string());
577
        // we need to cancel the execution of this fragment
578
0
        req.cancel_fn(rpc_status);
579
0
    }
580
0
}
581
582
0
static void empty_function(RuntimeState*, Status*) {}
583
584
Status FragmentMgr::exec_plan_fragment(const TExecPlanFragmentParams& params,
585
0
                                       const QuerySource query_source) {
586
0
    return Status::InternalError("Non-pipeline is disabled!");
587
0
}
588
589
Status FragmentMgr::exec_plan_fragment(const TPipelineFragmentParams& params,
590
0
                                       const QuerySource query_source) {
591
0
    if (params.txn_conf.need_txn) {
592
0
        std::shared_ptr<StreamLoadContext> stream_load_ctx =
593
0
                std::make_shared<StreamLoadContext>(_exec_env);
594
0
        stream_load_ctx->db = params.txn_conf.db;
595
0
        stream_load_ctx->db_id = params.txn_conf.db_id;
596
0
        stream_load_ctx->table = params.txn_conf.tbl;
597
0
        stream_load_ctx->txn_id = params.txn_conf.txn_id;
598
0
        stream_load_ctx->id = UniqueId(params.query_id);
599
0
        stream_load_ctx->put_result.__set_pipeline_params(params);
600
0
        stream_load_ctx->use_streaming = true;
601
0
        stream_load_ctx->load_type = TLoadType::MANUL_LOAD;
602
0
        stream_load_ctx->load_src_type = TLoadSourceType::RAW;
603
0
        stream_load_ctx->label = params.import_label;
604
0
        stream_load_ctx->format = TFileFormatType::FORMAT_CSV_PLAIN;
605
0
        stream_load_ctx->timeout_second = 3600;
606
0
        stream_load_ctx->auth.token = params.txn_conf.token;
607
0
        stream_load_ctx->need_commit_self = true;
608
0
        stream_load_ctx->need_rollback = true;
609
0
        auto pipe = std::make_shared<io::StreamLoadPipe>(
610
0
                io::kMaxPipeBufferedBytes /* max_buffered_bytes */, 64 * 1024 /* min_chunk_size */,
611
0
                -1 /* total_length */, true /* use_proto */);
612
0
        stream_load_ctx->body_sink = pipe;
613
0
        stream_load_ctx->pipe = pipe;
614
0
        stream_load_ctx->max_filter_ratio = params.txn_conf.max_filter_ratio;
615
616
0
        RETURN_IF_ERROR(
617
0
                _exec_env->new_load_stream_mgr()->put(stream_load_ctx->id, stream_load_ctx));
618
619
0
        RETURN_IF_ERROR(_exec_env->stream_load_executor()->execute_plan_fragment(stream_load_ctx));
620
0
        return Status::OK();
621
0
    } else {
622
0
        return exec_plan_fragment(params, query_source, empty_function);
623
0
    }
624
0
}
625
626
// Stage 2. prepare finished. then get FE instruction to execute
627
0
Status FragmentMgr::start_query_execution(const PExecPlanFragmentStartRequest* request) {
628
0
    TUniqueId query_id;
629
0
    query_id.__set_hi(request->query_id().hi());
630
0
    query_id.__set_lo(request->query_id().lo());
631
0
    auto q_ctx = get_query_ctx(query_id);
632
0
    if (q_ctx) {
633
0
        q_ctx->set_ready_to_execute(Status::OK());
634
0
        LOG_INFO("Query {} start execution", print_id(query_id));
635
0
    } else {
636
0
        return Status::InternalError(
637
0
                "Failed to get query fragments context. Query may be "
638
0
                "timeout or be cancelled. host: {}",
639
0
                BackendOptions::get_localhost());
640
0
    }
641
0
    return Status::OK();
642
0
}
643
644
void FragmentMgr::remove_pipeline_context(
645
0
        std::shared_ptr<pipeline::PipelineFragmentContext> f_context) {
646
0
    auto query_id = f_context->get_query_id();
647
0
    int64 now = duration_cast<std::chrono::milliseconds>(
648
0
                        std::chrono::system_clock::now().time_since_epoch())
649
0
                        .count();
650
0
    g_fragment_executing_count << -1;
651
0
    g_fragment_last_active_time.set_value(now);
652
653
0
    _pipeline_map.erase({query_id, f_context->get_fragment_id()});
654
0
}
655
656
1
std::shared_ptr<QueryContext> FragmentMgr::get_query_ctx(const TUniqueId& query_id) {
657
1
    auto val = _query_ctx_map.find(query_id);
658
1
    if (auto q_ctx = val.lock()) {
659
0
        return q_ctx;
660
0
    }
661
1
    return nullptr;
662
1
}
663
664
Status FragmentMgr::_get_or_create_query_ctx(const TPipelineFragmentParams& params,
665
                                             TUniqueId query_id, bool pipeline,
666
                                             QuerySource query_source,
667
0
                                             std::shared_ptr<QueryContext>& query_ctx) {
668
0
    DBUG_EXECUTE_IF("FragmentMgr._get_query_ctx.failed", {
669
0
        return Status::InternalError("FragmentMgr._get_query_ctx.failed, query id {}",
670
0
                                     print_id(query_id));
671
0
    });
672
673
    // Find _query_ctx_map, in case some other request has already
674
    // create the query fragments context.
675
0
    query_ctx = get_query_ctx(query_id);
676
0
    if (params.is_simplified_param) {
677
        // Get common components from _query_ctx_map
678
0
        if (!query_ctx) {
679
0
            return Status::InternalError(
680
0
                    "Failed to get query fragments context. Query {} may be timeout or be "
681
0
                    "cancelled. host: {}",
682
0
                    print_id(query_id), BackendOptions::get_localhost());
683
0
        }
684
0
    } else {
685
0
        if (!query_ctx) {
686
0
            RETURN_IF_ERROR(_query_ctx_map.apply_if_not_exists(
687
0
                    query_id, query_ctx,
688
0
                    [&](phmap::flat_hash_map<TUniqueId, std::weak_ptr<QueryContext>>& map)
689
0
                            -> Status {
690
0
                        WorkloadGroupPtr workload_group_ptr = nullptr;
691
0
                        std::string wg_info_str = "Workload Group not set";
692
0
                        if (params.__isset.workload_groups && !params.workload_groups.empty()) {
693
0
                            uint64_t wg_id = params.workload_groups[0].id;
694
0
                            workload_group_ptr = _exec_env->workload_group_mgr()->get_group(wg_id);
695
0
                            if (workload_group_ptr != nullptr) {
696
0
                                wg_info_str = workload_group_ptr->debug_string();
697
0
                            } else {
698
0
                                wg_info_str = "set wg but not find it in be";
699
0
                            }
700
0
                        }
701
702
                        // First time a fragment of a query arrived. print logs.
703
0
                        LOG(INFO) << "query_id: " << print_id(query_id)
704
0
                                  << ", coord_addr: " << params.coord
705
0
                                  << ", total fragment num on current host: "
706
0
                                  << params.fragment_num_on_host
707
0
                                  << ", fe process uuid: " << params.query_options.fe_process_uuid
708
0
                                  << ", query type: " << params.query_options.query_type
709
0
                                  << ", report audit fe:" << params.current_connect_fe
710
0
                                  << ", use wg:" << wg_info_str;
711
712
                        // This may be a first fragment request of the query.
713
                        // Create the query fragments context.
714
0
                        query_ctx = QueryContext::create_shared(
715
0
                                query_id, _exec_env, params.query_options, params.coord, pipeline,
716
0
                                params.is_nereids, params.current_connect_fe, query_source);
717
0
                        SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(query_ctx->query_mem_tracker);
718
0
                        RETURN_IF_ERROR(DescriptorTbl::create(
719
0
                                &(query_ctx->obj_pool), params.desc_tbl, &(query_ctx->desc_tbl)));
720
                        // set file scan range params
721
0
                        if (params.__isset.file_scan_params) {
722
0
                            query_ctx->file_scan_range_params_map = params.file_scan_params;
723
0
                        }
724
725
0
                        query_ctx->query_globals = params.query_globals;
726
727
0
                        if (params.__isset.resource_info) {
728
0
                            query_ctx->user = params.resource_info.user;
729
0
                            query_ctx->group = params.resource_info.group;
730
0
                            query_ctx->set_rsc_info = true;
731
0
                        }
732
733
0
                        _set_scan_concurrency(params, query_ctx.get());
734
735
0
                        if (workload_group_ptr != nullptr) {
736
0
                            RETURN_IF_ERROR(workload_group_ptr->add_query(query_id, query_ctx));
737
0
                            query_ctx->set_workload_group(workload_group_ptr);
738
0
                            _exec_env->runtime_query_statistics_mgr()->set_workload_group_id(
739
0
                                    print_id(query_id), workload_group_ptr->id());
740
0
                        }
741
                        // There is some logic in query ctx's dctor, we could not check if exists and delete the
742
                        // temp query ctx now. For example, the query id maybe removed from workload group's queryset.
743
0
                        map.insert({query_id, query_ctx});
744
0
                        return Status::OK();
745
0
                    }));
746
0
        }
747
0
    }
748
0
    return Status::OK();
749
0
}
750
751
Status FragmentMgr::exec_plan_fragment(const TExecPlanFragmentParams& params,
752
0
                                       QuerySource query_source, const FinishCallback& cb) {
753
0
    return Status::InternalError("Non-pipeline is disabled!");
754
0
}
755
756
0
std::string FragmentMgr::dump_pipeline_tasks(int64_t duration) {
757
0
    fmt::memory_buffer debug_string_buffer;
758
0
    size_t i = 0;
759
0
    {
760
0
        fmt::format_to(debug_string_buffer,
761
0
                       "{} pipeline fragment contexts are still running! duration_limit={}\n",
762
0
                       _pipeline_map.num_items(), duration);
763
0
        timespec now;
764
0
        clock_gettime(CLOCK_MONOTONIC, &now);
765
766
0
        _pipeline_map.apply([&](phmap::flat_hash_map<
767
0
                                    std::pair<TUniqueId, int>,
768
0
                                    std::shared_ptr<pipeline::PipelineFragmentContext>>& map)
769
0
                                    -> Status {
770
0
            for (auto& it : map) {
771
0
                auto elapsed = it.second->elapsed_time() / 1000000000.0;
772
0
                if (elapsed < duration) {
773
                    // Only display tasks which has been running for more than {duration} seconds.
774
0
                    continue;
775
0
                }
776
0
                auto timeout_second = it.second->timeout_second();
777
0
                fmt::format_to(
778
0
                        debug_string_buffer,
779
0
                        "No.{} (elapse_second={}s, query_timeout_second={}s, is_timeout={}) : {}\n",
780
0
                        i, elapsed, timeout_second, it.second->is_timeout(now),
781
0
                        it.second->debug_string());
782
0
                i++;
783
0
            }
784
0
            return Status::OK();
785
0
        });
786
0
    }
787
0
    return fmt::to_string(debug_string_buffer);
788
0
}
789
790
0
std::string FragmentMgr::dump_pipeline_tasks(TUniqueId& query_id) {
791
0
    if (auto q_ctx = get_query_ctx(query_id)) {
792
0
        return q_ctx->print_all_pipeline_context();
793
0
    } else {
794
0
        return fmt::format(
795
0
                "Dump pipeline tasks failed: Query context (query id = {}) not found. \n",
796
0
                print_id(query_id));
797
0
    }
798
0
}
799
800
Status FragmentMgr::exec_plan_fragment(const TPipelineFragmentParams& params,
801
0
                                       QuerySource query_source, const FinishCallback& cb) {
802
0
    VLOG_ROW << "query: " << print_id(params.query_id) << " exec_plan_fragment params is "
803
0
             << apache::thrift::ThriftDebugString(params).c_str();
804
    // sometimes TExecPlanFragmentParams debug string is too long and glog
805
    // will truncate the log line, so print query options seperately for debuggin purpose
806
0
    VLOG_ROW << "query: " << print_id(params.query_id) << "query options is "
807
0
             << apache::thrift::ThriftDebugString(params.query_options).c_str();
808
809
0
    std::shared_ptr<QueryContext> query_ctx;
810
0
    RETURN_IF_ERROR(
811
0
            _get_or_create_query_ctx(params, params.query_id, true, query_source, query_ctx));
812
0
    SCOPED_ATTACH_TASK(query_ctx.get());
813
0
    int64_t duration_ns = 0;
814
0
    std::shared_ptr<pipeline::PipelineFragmentContext> context =
815
0
            std::make_shared<pipeline::PipelineFragmentContext>(
816
0
                    query_ctx->query_id(), params.fragment_id, query_ctx, _exec_env, cb,
817
0
                    std::bind<Status>(std::mem_fn(&FragmentMgr::trigger_pipeline_context_report),
818
0
                                      this, std::placeholders::_1, std::placeholders::_2));
819
0
    {
820
0
        SCOPED_RAW_TIMER(&duration_ns);
821
0
        Status prepare_st = Status::OK();
822
0
        ASSIGN_STATUS_IF_CATCH_EXCEPTION(prepare_st = context->prepare(params, _thread_pool.get()),
823
0
                                         prepare_st);
824
0
        if (!prepare_st.ok()) {
825
0
            query_ctx->cancel(prepare_st, params.fragment_id);
826
0
            query_ctx->set_execution_dependency_ready();
827
0
            return prepare_st;
828
0
        }
829
0
    }
830
0
    g_fragmentmgr_prepare_latency << (duration_ns / 1000);
831
832
0
    DBUG_EXECUTE_IF("FragmentMgr.exec_plan_fragment.failed",
833
0
                    { return Status::Aborted("FragmentMgr.exec_plan_fragment.failed"); });
834
835
0
    std::shared_ptr<RuntimeFilterMergeControllerEntity> handler;
836
0
    RETURN_IF_ERROR(_runtimefilter_controller.add_entity(
837
0
            params.local_params[0], params.query_id, params.query_options, &handler,
838
0
            RuntimeFilterParamsContext::create(context->get_runtime_state())));
839
0
    if (handler) {
840
0
        query_ctx->set_merge_controller_handler(handler);
841
0
    }
842
843
0
    {
844
0
        int64 now = duration_cast<std::chrono::milliseconds>(
845
0
                            std::chrono::system_clock::now().time_since_epoch())
846
0
                            .count();
847
0
        g_fragment_executing_count << 1;
848
0
        g_fragment_last_active_time.set_value(now);
849
850
        // (query_id, fragment_id) is executed only on one BE, locks _pipeline_map.
851
0
        auto res = _pipeline_map.find({params.query_id, params.fragment_id});
852
0
        if (res != nullptr) {
853
0
            return Status::InternalError(
854
0
                    "exec_plan_fragment query_id({}) input duplicated fragment_id({})",
855
0
                    print_id(params.query_id), params.fragment_id);
856
0
        }
857
0
        _pipeline_map.insert({params.query_id, params.fragment_id}, context);
858
0
    }
859
860
0
    if (!params.__isset.need_wait_execution_trigger || !params.need_wait_execution_trigger) {
861
0
        query_ctx->set_ready_to_execute_only();
862
0
    }
863
864
0
    query_ctx->set_pipeline_context(params.fragment_id, context);
865
866
0
    RETURN_IF_ERROR(context->submit());
867
0
    return Status::OK();
868
0
}
869
870
template <typename Param>
871
0
void FragmentMgr::_set_scan_concurrency(const Param& params, QueryContext* query_ctx) {
872
#ifndef BE_TEST
873
    // If the token is set, the scan task will use limited_scan_pool in scanner scheduler.
874
    // Otherwise, the scan task will use local/remote scan pool in scanner scheduler
875
    if (params.query_options.__isset.resource_limit &&
876
        params.query_options.resource_limit.__isset.cpu_limit) {
877
        query_ctx->set_thread_token(params.query_options.resource_limit.cpu_limit, false);
878
    }
879
#endif
880
0
}
881
882
1
void FragmentMgr::cancel_query(const TUniqueId query_id, const Status reason) {
883
1
    std::shared_ptr<QueryContext> query_ctx = nullptr;
884
1
    {
885
1
        if (auto q_ctx = get_query_ctx(query_id)) {
886
0
            query_ctx = q_ctx;
887
1
        } else {
888
1
            LOG(WARNING) << "Query " << print_id(query_id)
889
1
                         << " does not exists, failed to cancel it";
890
1
            return;
891
1
        }
892
1
    }
893
0
    query_ctx->cancel(reason);
894
0
    _query_ctx_map.erase(query_id);
895
0
    LOG(INFO) << "Query " << print_id(query_id)
896
0
              << " is cancelled and removed. Reason: " << reason.to_string();
897
0
}
898
899
4
void FragmentMgr::cancel_worker() {
900
4
    LOG(INFO) << "FragmentMgr cancel worker start working.";
901
902
4
    timespec check_invalid_query_last_timestamp;
903
4
    clock_gettime(CLOCK_MONOTONIC, &check_invalid_query_last_timestamp);
904
905
4
    do {
906
4
        std::vector<TUniqueId> queries_lost_coordinator;
907
4
        std::vector<TUniqueId> queries_timeout;
908
4
        std::vector<TUniqueId> queries_pipeline_task_leak;
909
        // Fe process uuid -> set<QueryId>
910
4
        std::map<int64_t, std::unordered_set<TUniqueId>> running_queries_on_all_fes;
911
4
        const std::map<TNetworkAddress, FrontendInfo>& running_fes =
912
4
                ExecEnv::GetInstance()->get_running_frontends();
913
914
4
        timespec now;
915
4
        clock_gettime(CLOCK_MONOTONIC, &now);
916
917
4
        if (config::enable_pipeline_task_leakage_detect &&
918
4
            now.tv_sec - check_invalid_query_last_timestamp.tv_sec >
919
0
                    config::pipeline_task_leakage_detect_period_secs) {
920
0
            check_invalid_query_last_timestamp = now;
921
0
            running_queries_on_all_fes = _get_all_running_queries_from_fe();
922
4
        } else {
923
4
            running_queries_on_all_fes.clear();
924
4
        }
925
926
4
        std::vector<std::shared_ptr<pipeline::PipelineFragmentContext>> ctx;
927
4
        _pipeline_map.apply(
928
4
                [&](phmap::flat_hash_map<std::pair<TUniqueId, int>,
929
4
                                         std::shared_ptr<pipeline::PipelineFragmentContext>>& map)
930
512
                        -> Status {
931
512
                    ctx.reserve(ctx.size() + map.size());
932
512
                    for (auto& pipeline_itr : map) {
933
0
                        ctx.push_back(pipeline_itr.second);
934
0
                    }
935
512
                    return Status::OK();
936
512
                });
937
4
        for (auto& c : ctx) {
938
0
            c->clear_finished_tasks();
939
0
        }
940
941
4
        {
942
4
            _query_ctx_map.apply(
943
4
                    [&](phmap::flat_hash_map<TUniqueId, std::weak_ptr<QueryContext>>& map)
944
512
                            -> Status {
945
512
                        for (auto it = map.begin(); it != map.end();) {
946
0
                            if (auto q_ctx = it->second.lock()) {
947
0
                                if (q_ctx->is_timeout(now)) {
948
0
                                    LOG_WARNING("Query {} is timeout", print_id(it->first));
949
0
                                    queries_timeout.push_back(it->first);
950
0
                                }
951
0
                                ++it;
952
0
                            } else {
953
0
                                it = map.erase(it);
954
0
                            }
955
0
                        }
956
512
                        return Status::OK();
957
512
                    });
958
959
            // We use a very conservative cancel strategy.
960
            // 0. If there are no running frontends, do not cancel any queries.
961
            // 1. If query's process uuid is zero, do not cancel
962
            // 2. If same process uuid, do not cancel
963
            // 3. If fe has zero process uuid, do not cancel
964
4
            if (running_fes.empty() && _query_ctx_map.num_items() != 0) {
965
0
                LOG_EVERY_N(WARNING, 10)
966
0
                        << "Could not find any running frontends, maybe we are upgrading or "
967
0
                           "starting? "
968
0
                        << "We will not cancel any outdated queries in this situation.";
969
4
            } else {
970
4
                _query_ctx_map.apply([&](phmap::flat_hash_map<TUniqueId,
971
4
                                                              std::weak_ptr<QueryContext>>& map)
972
512
                                             -> Status {
973
512
                    for (const auto& it : map) {
974
0
                        if (auto q_ctx = it.second.lock()) {
975
0
                            const int64_t fe_process_uuid = q_ctx->get_fe_process_uuid();
976
977
0
                            if (fe_process_uuid == 0) {
978
                                // zero means this query is from a older version fe or
979
                                // this fe is starting
980
0
                                continue;
981
0
                            }
982
983
                            // If the query is not running on the any frontends, cancel it.
984
0
                            if (auto itr = running_queries_on_all_fes.find(fe_process_uuid);
985
0
                                itr != running_queries_on_all_fes.end()) {
986
                                // Query not found on this frontend, and the query arrives before the last check
987
0
                                if (itr->second.find(it.first) == itr->second.end() &&
988
                                    // tv_nsec represents the number of nanoseconds that have elapsed since the time point stored in tv_sec.
989
                                    // tv_sec is enough, we do not need to check tv_nsec.
990
0
                                    q_ctx->get_query_arrival_timestamp().tv_sec <
991
0
                                            check_invalid_query_last_timestamp.tv_sec &&
992
0
                                    q_ctx->get_query_source() == QuerySource::INTERNAL_FRONTEND) {
993
0
                                    queries_pipeline_task_leak.push_back(q_ctx->query_id());
994
0
                                    LOG_INFO(
995
0
                                            "Query {}, type {} is not found on any frontends, "
996
0
                                            "maybe it "
997
0
                                            "is leaked.",
998
0
                                            print_id(q_ctx->query_id()),
999
0
                                            toString(q_ctx->get_query_source()));
1000
0
                                    continue;
1001
0
                                }
1002
0
                            }
1003
1004
0
                            auto itr = running_fes.find(q_ctx->coord_addr);
1005
0
                            if (itr != running_fes.end()) {
1006
0
                                if (fe_process_uuid == itr->second.info.process_uuid ||
1007
0
                                    itr->second.info.process_uuid == 0) {
1008
0
                                    continue;
1009
0
                                } else {
1010
0
                                    LOG_WARNING(
1011
0
                                            "Coordinator of query {} restarted, going to cancel "
1012
0
                                            "it.",
1013
0
                                            print_id(q_ctx->query_id()));
1014
0
                                }
1015
0
                            } else {
1016
                                // In some rear cases, the rpc port of follower is not updated in time,
1017
                                // then the port of this follower will be zero, but acutally it is still running,
1018
                                // and be has already received the query from follower.
1019
                                // So we need to check if host is in running_fes.
1020
0
                                bool fe_host_is_standing =
1021
0
                                        std::any_of(running_fes.begin(), running_fes.end(),
1022
0
                                                    [&q_ctx](const auto& fe) {
1023
0
                                                        return fe.first.hostname ==
1024
0
                                                                       q_ctx->coord_addr.hostname &&
1025
0
                                                               fe.first.port == 0;
1026
0
                                                    });
1027
0
                                if (fe_host_is_standing) {
1028
0
                                    LOG_WARNING(
1029
0
                                            "Coordinator {}:{} is not found, but its host is still "
1030
0
                                            "running with an unstable brpc port, not going to "
1031
0
                                            "cancel "
1032
0
                                            "it.",
1033
0
                                            q_ctx->coord_addr.hostname, q_ctx->coord_addr.port,
1034
0
                                            print_id(q_ctx->query_id()));
1035
0
                                    continue;
1036
0
                                } else {
1037
0
                                    LOG_WARNING(
1038
0
                                            "Could not find target coordinator {}:{} of query {}, "
1039
0
                                            "going to "
1040
0
                                            "cancel it.",
1041
0
                                            q_ctx->coord_addr.hostname, q_ctx->coord_addr.port,
1042
0
                                            print_id(q_ctx->query_id()));
1043
0
                                }
1044
0
                            }
1045
0
                        }
1046
                        // Coordinator of this query has already dead or query context has been released.
1047
0
                        queries_lost_coordinator.push_back(it.first);
1048
0
                    }
1049
512
                    return Status::OK();
1050
512
                });
1051
4
            }
1052
4
        }
1053
1054
4
        if (!queries_lost_coordinator.empty()) {
1055
0
            LOG(INFO) << "There are " << queries_lost_coordinator.size()
1056
0
                      << " queries need to be cancelled, coordinator dead or restarted.";
1057
0
        }
1058
1059
4
        for (const auto& qid : queries_timeout) {
1060
0
            cancel_query(qid,
1061
0
                         Status::Error<ErrorCode::TIMEOUT>(
1062
0
                                 "FragmentMgr cancel worker going to cancel timeout instance "));
1063
0
        }
1064
1065
4
        for (const auto& qid : queries_pipeline_task_leak) {
1066
            // Cancel the query, and maybe try to report debug info to fe so that we can
1067
            // collect debug info by sql or http api instead of search log.
1068
0
            cancel_query(qid, Status::Error<ErrorCode::ILLEGAL_STATE>(
1069
0
                                      "Potential pipeline task leakage"));
1070
0
        }
1071
1072
4
        for (const auto& qid : queries_lost_coordinator) {
1073
0
            cancel_query(qid, Status::Error<ErrorCode::CANCELLED>(
1074
0
                                      "Source frontend is not running or restarted"));
1075
0
        }
1076
1077
4
    } while (!_stop_background_threads_latch.wait_for(std::chrono::seconds(1)));
1078
4
    LOG(INFO) << "FragmentMgr cancel worker is going to exit.";
1079
4
}
1080
1081
0
void FragmentMgr::debug(std::stringstream& ss) {}
1082
/*
1083
 * 1. resolve opaqued_query_plan to thrift structure
1084
 * 2. build TExecPlanFragmentParams
1085
 */
1086
Status FragmentMgr::exec_external_plan_fragment(const TScanOpenParams& params,
1087
                                                const TQueryPlanInfo& t_query_plan_info,
1088
                                                const TUniqueId& query_id,
1089
                                                const TUniqueId& fragment_instance_id,
1090
0
                                                std::vector<TScanColumnDesc>* selected_columns) {
1091
    // set up desc tbl
1092
0
    DescriptorTbl* desc_tbl = nullptr;
1093
0
    ObjectPool obj_pool;
1094
0
    Status st = DescriptorTbl::create(&obj_pool, t_query_plan_info.desc_tbl, &desc_tbl);
1095
0
    if (!st.ok()) {
1096
0
        LOG(WARNING) << "open context error: extract DescriptorTbl failure";
1097
0
        std::stringstream msg;
1098
0
        msg << " create DescriptorTbl error, should not be modified after returned Doris FE "
1099
0
               "processed";
1100
0
        return Status::InvalidArgument(msg.str());
1101
0
    }
1102
0
    TupleDescriptor* tuple_desc = desc_tbl->get_tuple_descriptor(0);
1103
0
    if (tuple_desc == nullptr) {
1104
0
        LOG(WARNING) << "open context error: extract TupleDescriptor failure";
1105
0
        std::stringstream msg;
1106
0
        msg << " get  TupleDescriptor error, should not be modified after returned Doris FE "
1107
0
               "processed";
1108
0
        return Status::InvalidArgument(msg.str());
1109
0
    }
1110
    // process selected columns form slots
1111
0
    for (const SlotDescriptor* slot : tuple_desc->slots()) {
1112
0
        TScanColumnDesc col;
1113
0
        col.__set_name(slot->col_name());
1114
0
        col.__set_type(to_thrift(slot->type().type));
1115
0
        selected_columns->emplace_back(std::move(col));
1116
0
    }
1117
1118
0
    VLOG_QUERY << "BackendService execute open()  TQueryPlanInfo: "
1119
0
               << apache::thrift::ThriftDebugString(t_query_plan_info);
1120
    // assign the param used to execute PlanFragment
1121
0
    TPipelineFragmentParams exec_fragment_params;
1122
0
    exec_fragment_params.protocol_version = (PaloInternalServiceVersion::type)0;
1123
0
    exec_fragment_params.__set_is_simplified_param(false);
1124
0
    exec_fragment_params.__set_fragment(t_query_plan_info.plan_fragment);
1125
0
    exec_fragment_params.__set_desc_tbl(t_query_plan_info.desc_tbl);
1126
1127
    // assign the param used for executing of PlanFragment-self
1128
0
    TPipelineInstanceParams fragment_exec_params;
1129
0
    exec_fragment_params.query_id = query_id;
1130
0
    fragment_exec_params.fragment_instance_id = fragment_instance_id;
1131
0
    exec_fragment_params.coord.hostname = "external";
1132
0
    std::map<::doris::TPlanNodeId, std::vector<TScanRangeParams>> per_node_scan_ranges;
1133
0
    std::vector<TScanRangeParams> scan_ranges;
1134
0
    std::vector<int64_t> tablet_ids = params.tablet_ids;
1135
0
    TNetworkAddress address;
1136
0
    address.hostname = BackendOptions::get_localhost();
1137
0
    address.port = doris::config::be_port;
1138
0
    std::map<int64_t, TTabletVersionInfo> tablet_info = t_query_plan_info.tablet_info;
1139
0
    for (auto tablet_id : params.tablet_ids) {
1140
0
        TPaloScanRange scan_range;
1141
0
        scan_range.db_name = params.database;
1142
0
        scan_range.table_name = params.table;
1143
0
        auto iter = tablet_info.find(tablet_id);
1144
0
        if (iter != tablet_info.end()) {
1145
0
            TTabletVersionInfo info = iter->second;
1146
0
            scan_range.tablet_id = tablet_id;
1147
0
            scan_range.version = std::to_string(info.version);
1148
            // Useless but it is required field in TPaloScanRange
1149
0
            scan_range.version_hash = "0";
1150
0
            scan_range.schema_hash = std::to_string(info.schema_hash);
1151
0
            scan_range.hosts.push_back(address);
1152
0
        } else {
1153
0
            std::stringstream msg;
1154
0
            msg << "tablet_id: " << tablet_id << " not found";
1155
0
            LOG(WARNING) << "tablet_id [ " << tablet_id << " ] not found";
1156
0
            return Status::NotFound(msg.str());
1157
0
        }
1158
0
        TScanRange doris_scan_range;
1159
0
        doris_scan_range.__set_palo_scan_range(scan_range);
1160
0
        TScanRangeParams scan_range_params;
1161
0
        scan_range_params.scan_range = doris_scan_range;
1162
0
        scan_ranges.push_back(scan_range_params);
1163
0
    }
1164
0
    per_node_scan_ranges.insert(std::make_pair((::doris::TPlanNodeId)0, scan_ranges));
1165
0
    fragment_exec_params.per_node_scan_ranges = per_node_scan_ranges;
1166
0
    exec_fragment_params.local_params.push_back(fragment_exec_params);
1167
0
    TQueryOptions query_options;
1168
0
    query_options.batch_size = params.batch_size;
1169
0
    query_options.execution_timeout = params.execution_timeout;
1170
0
    query_options.mem_limit = params.mem_limit;
1171
0
    query_options.query_type = TQueryType::EXTERNAL;
1172
0
    query_options.be_exec_version = BeExecVersionManager::get_newest_version();
1173
0
    exec_fragment_params.__set_query_options(query_options);
1174
0
    VLOG_ROW << "external exec_plan_fragment params is "
1175
0
             << apache::thrift::ThriftDebugString(exec_fragment_params).c_str();
1176
0
    return exec_plan_fragment(exec_fragment_params, QuerySource::EXTERNAL_CONNECTOR);
1177
0
}
1178
1179
Status FragmentMgr::apply_filterv2(const PPublishFilterRequestV2* request,
1180
0
                                   butil::IOBufAsZeroCopyInputStream* attach_data) {
1181
0
    int64_t start_apply = MonotonicMillis();
1182
1183
0
    std::shared_ptr<pipeline::PipelineFragmentContext> pip_context;
1184
0
    QueryThreadContext query_thread_context;
1185
1186
0
    RuntimeFilterMgr* runtime_filter_mgr = nullptr;
1187
1188
0
    const auto& fragment_ids = request->fragment_ids();
1189
0
    {
1190
0
        for (auto fragment_id : fragment_ids) {
1191
0
            pip_context =
1192
0
                    _pipeline_map.find({UniqueId(request->query_id()).to_thrift(), fragment_id});
1193
0
            if (pip_context == nullptr) {
1194
0
                continue;
1195
0
            }
1196
1197
0
            DCHECK(pip_context != nullptr);
1198
0
            runtime_filter_mgr = pip_context->get_query_ctx()->runtime_filter_mgr();
1199
0
            query_thread_context = {pip_context->get_query_ctx()->query_id(),
1200
0
                                    pip_context->get_query_ctx()->query_mem_tracker,
1201
0
                                    pip_context->get_query_ctx()->workload_group()};
1202
0
            break;
1203
0
        }
1204
0
    }
1205
1206
0
    if (runtime_filter_mgr == nullptr) {
1207
        // all instance finished
1208
0
        return Status::OK();
1209
0
    }
1210
1211
0
    SCOPED_ATTACH_TASK(query_thread_context);
1212
    // 1. get the target filters
1213
0
    std::vector<std::shared_ptr<IRuntimeFilter>> filters;
1214
0
    RETURN_IF_ERROR(runtime_filter_mgr->get_consume_filters(request->filter_id(), filters));
1215
1216
    // 2. create the filter wrapper to replace or ignore the target filters
1217
0
    if (!filters.empty()) {
1218
0
        UpdateRuntimeFilterParamsV2 params {request, attach_data, filters[0]->column_type()};
1219
0
        std::shared_ptr<RuntimePredicateWrapper> filter_wrapper;
1220
0
        RETURN_IF_ERROR(IRuntimeFilter::create_wrapper(&params, &filter_wrapper));
1221
1222
0
        std::ranges::for_each(filters, [&](auto& filter) {
1223
0
            filter->update_filter(
1224
0
                    filter_wrapper, request->merge_time(), start_apply,
1225
0
                    request->has_local_merge_time() ? request->local_merge_time() : 0);
1226
0
        });
1227
0
    }
1228
1229
0
    return Status::OK();
1230
0
}
1231
1232
0
Status FragmentMgr::send_filter_size(const PSendFilterSizeRequest* request) {
1233
0
    UniqueId queryid = request->query_id();
1234
1235
0
    std::shared_ptr<QueryContext> query_ctx;
1236
0
    {
1237
0
        TUniqueId query_id;
1238
0
        query_id.__set_hi(queryid.hi);
1239
0
        query_id.__set_lo(queryid.lo);
1240
0
        if (auto q_ctx = get_query_ctx(query_id)) {
1241
0
            query_ctx = q_ctx;
1242
0
        } else {
1243
0
            return Status::EndOfFile(
1244
0
                    "Send filter size failed: Query context (query-id: {}) not found, maybe "
1245
0
                    "finished",
1246
0
                    queryid.to_string());
1247
0
        }
1248
0
    }
1249
1250
0
    std::shared_ptr<RuntimeFilterMergeControllerEntity> filter_controller;
1251
0
    RETURN_IF_ERROR(_runtimefilter_controller.acquire(queryid, &filter_controller));
1252
0
    auto merge_status = filter_controller->send_filter_size(query_ctx, request);
1253
0
    return merge_status;
1254
0
}
1255
1256
0
Status FragmentMgr::sync_filter_size(const PSyncFilterSizeRequest* request) {
1257
0
    UniqueId queryid = request->query_id();
1258
0
    std::shared_ptr<QueryContext> query_ctx;
1259
0
    {
1260
0
        TUniqueId query_id;
1261
0
        query_id.__set_hi(queryid.hi);
1262
0
        query_id.__set_lo(queryid.lo);
1263
0
        if (auto q_ctx = get_query_ctx(query_id)) {
1264
0
            query_ctx = q_ctx;
1265
0
        } else {
1266
0
            return Status::EndOfFile(
1267
0
                    "Sync filter size failed: Query context (query-id: {}) already finished",
1268
0
                    queryid.to_string());
1269
0
        }
1270
0
    }
1271
0
    return query_ctx->runtime_filter_mgr()->sync_filter_size(request);
1272
0
}
1273
1274
Status FragmentMgr::merge_filter(const PMergeFilterRequest* request,
1275
0
                                 butil::IOBufAsZeroCopyInputStream* attach_data) {
1276
0
    UniqueId queryid = request->query_id();
1277
1278
0
    std::shared_ptr<QueryContext> query_ctx;
1279
0
    {
1280
0
        TUniqueId query_id;
1281
0
        query_id.__set_hi(queryid.hi);
1282
0
        query_id.__set_lo(queryid.lo);
1283
0
        if (auto q_ctx = get_query_ctx(query_id)) {
1284
0
            query_ctx = q_ctx;
1285
0
        } else {
1286
0
            return Status::EndOfFile(
1287
0
                    "Merge filter size failed: Query context (query-id: {}) already finished",
1288
0
                    queryid.to_string());
1289
0
        }
1290
0
    }
1291
0
    SCOPED_ATTACH_TASK(query_ctx.get());
1292
0
    std::shared_ptr<RuntimeFilterMergeControllerEntity> filter_controller;
1293
0
    RETURN_IF_ERROR(_runtimefilter_controller.acquire(queryid, &filter_controller));
1294
0
    auto merge_status = filter_controller->merge(query_ctx, request, attach_data);
1295
0
    return merge_status;
1296
0
}
1297
1298
0
void FragmentMgr::get_runtime_query_info(std::vector<WorkloadQueryInfo>* query_info_list) {
1299
0
    _query_ctx_map.apply(
1300
0
            [&](phmap::flat_hash_map<TUniqueId, std::weak_ptr<QueryContext>>& map) -> Status {
1301
0
                for (auto iter = map.begin(); iter != map.end();) {
1302
0
                    if (auto q_ctx = iter->second.lock()) {
1303
0
                        WorkloadQueryInfo workload_query_info;
1304
0
                        workload_query_info.query_id = print_id(iter->first);
1305
0
                        workload_query_info.tquery_id = iter->first;
1306
0
                        workload_query_info.wg_id = q_ctx->workload_group() == nullptr
1307
0
                                                            ? -1
1308
0
                                                            : q_ctx->workload_group()->id();
1309
0
                        query_info_list->push_back(workload_query_info);
1310
0
                        iter++;
1311
0
                    } else {
1312
0
                        iter = map.erase(iter);
1313
0
                    }
1314
0
                }
1315
0
                return Status::OK();
1316
0
            });
1317
0
}
1318
1319
Status FragmentMgr::get_realtime_exec_status(const TUniqueId& query_id,
1320
0
                                             TReportExecStatusParams* exec_status) {
1321
0
    if (exec_status == nullptr) {
1322
0
        return Status::InvalidArgument("exes_status is nullptr");
1323
0
    }
1324
1325
0
    std::shared_ptr<QueryContext> query_context = get_query_ctx(query_id);
1326
0
    if (query_context == nullptr) {
1327
0
        return Status::NotFound("Query {} not found or released", print_id(query_id));
1328
0
    }
1329
1330
0
    *exec_status = query_context->get_realtime_exec_status();
1331
1332
0
    return Status::OK();
1333
0
}
1334
1335
} // namespace doris