Coverage Report

Created: 2026-08-24 11:25

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