Coverage Report

Created: 2026-08-05 13:32

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