Coverage Report

Created: 2025-09-15 20:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/pipeline/pipeline_tracing.cpp
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
#include "pipeline_tracing.h"
19
20
#include <absl/time/clock.h>
21
#include <fcntl.h>
22
#include <sys/stat.h>
23
24
#include <boost/algorithm/string/predicate.hpp>
25
#include <chrono>
26
#include <cstdint>
27
#include <mutex>
28
#include <string>
29
30
#include "common/config.h"
31
#include "common/exception.h"
32
#include "common/status.h"
33
#include "io/fs/local_file_writer.h"
34
#include "util/time.h"
35
36
namespace doris::pipeline {
37
38
0
void PipelineTracerContext::record(ScheduleRecord record) {
39
0
    if (_dump_type == RecordType::None) [[unlikely]] {
40
0
        return;
41
0
    }
42
43
0
    auto map_ptr = _data.load();
44
0
    auto it = map_ptr->find({record.query_id});
45
0
    if (it != map_ptr->end()) {
46
0
        it->second->enqueue(record);
47
0
    } else {
48
0
        _update([&](QueryTracesMap& new_map) {
49
0
            if (!new_map.contains({record.query_id})) {
50
0
                new_map[{record.query_id}].reset(new OneQueryTraces());
51
0
            }
52
0
            new_map[{record.query_id}]->enqueue(record);
53
0
        });
54
0
    }
55
0
}
56
57
0
void PipelineTracerContext::_update(std::function<void(QueryTracesMap&)>&& handler) {
58
0
    auto map_ptr = _data.load();
59
0
    while (true) {
60
0
        auto new_map = std::make_shared<QueryTracesMap>(*map_ptr);
61
0
        handler(*new_map);
62
0
        if (_data.compare_exchange_strong(map_ptr, new_map)) {
63
0
            break;
64
0
        }
65
0
    }
66
0
}
67
68
0
void PipelineTracerContext::end_query(TUniqueId query_id, uint64_t workload_group) {
69
0
    {
70
0
        std::unique_lock<std::mutex> l(_tg_lock);
71
0
        _id_to_workload_group[query_id] = workload_group;
72
0
    }
73
0
    if (_dump_type == RecordType::PerQuery) {
74
0
        _dump_query(query_id);
75
0
    } else if (_dump_type == RecordType::Periodic) {
76
0
        auto now = MonotonicSeconds();
77
0
        auto interval = now - _last_dump_time;
78
0
        if (interval > _dump_interval_s) {
79
0
            _dump_timeslice();
80
0
        }
81
0
    }
82
0
}
83
84
Status PipelineTracerContext::change_record_params(
85
0
        const std::map<std::string, std::string>& params) {
86
0
    bool effective = false;
87
0
    if (auto it = params.find("type"); it != params.end()) {
88
0
        if (boost::iequals(it->second, "disable") || boost::iequals(it->second, "none")) {
89
0
            _dump_type = RecordType::None;
90
0
            effective = true;
91
0
        } else if (boost::iequals(it->second, "per_query") ||
92
0
                   boost::iequals(it->second, "perquery")) {
93
0
            _dump_type = RecordType::PerQuery;
94
0
            effective = true;
95
0
        } else if (boost::iequals(it->second, "periodic")) {
96
0
            _dump_type = RecordType::Periodic;
97
0
            _last_dump_time = MonotonicSeconds();
98
0
            effective = true;
99
0
        }
100
0
    }
101
102
0
    if (auto it = params.find("dump_interval"); it != params.end()) {
103
0
        _dump_interval_s = std::stoll(it->second); // s as unit
104
0
        effective = true;
105
0
    }
106
107
0
    return effective ? Status::OK()
108
0
                     : Status::InvalidArgument(
109
0
                               "No qualified param in changing tracing record method");
110
0
}
111
112
0
void PipelineTracerContext::_dump_query(TUniqueId query_id) {
113
0
    auto map_ptr = _data.load();
114
0
    auto path = _log_dir / fmt::format("query{}", to_string(query_id));
115
0
    int fd = ::open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC,
116
0
                    S_ISGID | S_ISUID | S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
117
0
    if (fd < 0) [[unlikely]] {
118
0
        throw Exception(Status::Error<ErrorCode::CREATE_FILE_ERROR>(
119
0
                "create tracing log file {} failed", path.c_str()));
120
0
    }
121
0
    auto writer = io::LocalFileWriter {path, fd};
122
123
0
    ScheduleRecord record;
124
0
    while ((*map_ptr)[QueryID {query_id}]->try_dequeue(record)) {
125
0
        uint64_t v = 0;
126
0
        {
127
0
            std::unique_lock<std::mutex> l(_tg_lock);
128
0
            v = _id_to_workload_group.at(query_id);
129
0
        }
130
0
        auto tmp_str = record.to_string(v);
131
0
        auto text = Slice {tmp_str};
132
0
        THROW_IF_ERROR(writer.appendv(&text, 1));
133
0
    }
134
135
0
    THROW_IF_ERROR(writer.close());
136
137
0
    _last_dump_time = MonotonicSeconds();
138
139
0
    _update([&](QueryTracesMap& new_map) { _data.load()->erase(QueryID {query_id}); });
140
141
0
    {
142
0
        std::unique_lock<std::mutex> l(_tg_lock);
143
0
        _id_to_workload_group.erase(query_id);
144
0
    }
145
0
}
146
147
0
void PipelineTracerContext::_dump_timeslice() {
148
0
    auto new_map = std::make_shared<QueryTracesMap>();
149
0
    _data.exchange(new_map);
150
    //TODO: if long time, per timeslice per file
151
0
    auto path = _log_dir /
152
0
                fmt::format("until{}", std::chrono::steady_clock::now().time_since_epoch().count());
153
0
    int fd = ::open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC,
154
0
                    S_ISGID | S_ISUID | S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
155
0
    if (fd < 0) [[unlikely]] {
156
0
        throw Exception(Status::Error<ErrorCode::CREATE_FILE_ERROR>(
157
0
                "create tracing log file {} failed", path.c_str()));
158
0
    }
159
0
    auto writer = io::LocalFileWriter {path, fd};
160
161
    // dump all query traces in this time window to one file.
162
0
    for (auto& [query_id, trace] : (*new_map)) {
163
0
        ScheduleRecord record;
164
0
        while (trace->try_dequeue(record)) {
165
0
            uint64_t v = 0;
166
0
            {
167
0
                std::unique_lock<std::mutex> l(_tg_lock);
168
0
                v = _id_to_workload_group.at(query_id.query_id);
169
0
            }
170
0
            auto tmp_str = record.to_string(v);
171
0
            auto text = Slice {tmp_str};
172
0
            THROW_IF_ERROR(writer.appendv(&text, 1));
173
0
        }
174
0
    }
175
0
    THROW_IF_ERROR(writer.close());
176
177
0
    _last_dump_time = MonotonicSeconds();
178
179
0
    _id_to_workload_group.clear();
180
0
}
181
} // namespace doris::pipeline