Coverage Report

Created: 2025-06-07 21:54

/root/doris/be/src/pipeline/pipeline_tracing.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 "pipeline_tracing.h"
19
20
#include <absl/time/clock.h>
21
#include <fcntl.h>
22
23
#include <boost/algorithm/string/predicate.hpp>
24
#include <chrono>
25
#include <cstdint>
26
#include <mutex>
27
#include <string>
28
29
#include "common/config.h"
30
#include "common/exception.h"
31
#include "common/status.h"
32
#include "io/fs/local_file_writer.h"
33
#include "util/time.h"
34
35
namespace doris::pipeline {
36
37
0
void PipelineTracerContext::record(ScheduleRecord record) {
38
0
    if (_dump_type == RecordType::None) [[unlikely]] {
39
0
        return;
40
0
    }
41
0
    if (_datas.contains(record.query_id)) {
42
0
        _datas[record.query_id].enqueue(record);
43
0
    } else {
44
0
        std::unique_lock<std::mutex> l(_data_lock); // add new item, may rehash
45
0
        _datas[record.query_id].enqueue(record);
46
0
    }
47
0
}
48
49
0
void PipelineTracerContext::end_query(TUniqueId query_id, uint64_t workload_group) {
50
0
    {
51
0
        std::unique_lock<std::mutex> l(_tg_lock);
52
0
        _id_to_workload_group[query_id] = workload_group;
53
0
    }
54
0
    if (_dump_type == RecordType::PerQuery) {
55
0
        _dump(query_id);
56
0
    } else if (_dump_type == RecordType::Periodic) {
57
0
        auto now = MonotonicSeconds();
58
0
        auto interval = now - _last_dump_time;
59
0
        if (interval > _dump_interval_s) {
60
0
            _dump(query_id);
61
0
        }
62
0
    }
63
0
}
64
65
Status PipelineTracerContext::change_record_params(
66
0
        const std::map<std::string, std::string>& params) {
67
0
    bool effective = false;
68
0
    if (auto it = params.find("type"); it != params.end()) {
69
0
        if (boost::iequals(it->second, "disable") || boost::iequals(it->second, "none")) {
70
0
            _dump_type = RecordType::None;
71
0
            effective = true;
72
0
        } else if (boost::iequals(it->second, "per_query") ||
73
0
                   boost::iequals(it->second, "perquery")) {
74
0
            _dump_type = RecordType::PerQuery;
75
0
            effective = true;
76
0
        } else if (boost::iequals(it->second, "periodic")) {
77
0
            _dump_type = RecordType::Periodic;
78
0
            _last_dump_time = MonotonicSeconds();
79
0
            effective = true;
80
0
        }
81
0
    }
82
83
0
    if (auto it = params.find("dump_interval"); it != params.end()) {
84
0
        _dump_interval_s = std::stoll(it->second); // s as unit
85
0
        effective = true;
86
0
    }
87
88
0
    return effective ? Status::OK()
89
0
                     : Status::InvalidArgument(
90
0
                               "No qualified param in changing tracing record method");
91
0
}
92
93
0
void PipelineTracerContext::_dump(TUniqueId query_id) {
94
0
    if (_dump_type == RecordType::None) {
95
0
        return;
96
0
    }
97
98
0
    std::filesystem::path log_dir = fmt::format("{}/pipe_tracing", getenv("LOG_DIR"));
99
    //TODO: when dump, now could append records but can't add new query. try use better grained locks.
100
0
    std::unique_lock<std::mutex> l(_data_lock); // can't rehash
101
0
    if (_dump_type == RecordType::PerQuery) {
102
0
        auto path = log_dir / fmt::format("query{}", to_string(query_id));
103
0
        int fd = ::open(
104
0
                path.c_str(), O_CREAT | O_WRONLY | O_TRUNC,
105
0
                S_ISGID | S_ISUID | S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
106
0
        if (fd < 0) [[unlikely]] {
107
0
            throw Exception(Status::Error<ErrorCode::CREATE_FILE_ERROR>(
108
0
                    "create tracing log file {} failed", path.c_str()));
109
0
        }
110
0
        auto writer = io::LocalFileWriter {path, fd};
111
112
0
        ScheduleRecord record;
113
0
        while (_datas[query_id].try_dequeue(record)) {
114
0
            uint64_t v = 0;
115
0
            {
116
0
                std::unique_lock<std::mutex> l(_tg_lock);
117
0
                v = _id_to_workload_group[query_id];
118
0
            }
119
0
            auto tmp_str = record.to_string(v);
120
0
            auto text = Slice {tmp_str};
121
0
            THROW_IF_ERROR(writer.appendv(&text, 1));
122
0
        }
123
124
0
        THROW_IF_ERROR(writer.finalize());
125
0
        THROW_IF_ERROR(writer.close());
126
0
    } else if (_dump_type == RecordType::Periodic) {
127
0
        auto path =
128
0
                log_dir /
129
0
                fmt::format("until{}", std::chrono::steady_clock::now().time_since_epoch().count());
130
0
        int fd = ::open(
131
0
                path.c_str(), O_CREAT | O_WRONLY | O_TRUNC,
132
0
                S_ISGID | S_ISUID | S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
133
0
        if (fd < 0) [[unlikely]] {
134
0
            throw Exception(Status::Error<ErrorCode::CREATE_FILE_ERROR>(
135
0
                    "create tracing log file {} failed", path.c_str()));
136
0
        }
137
0
        auto writer = io::LocalFileWriter {path, fd};
138
139
0
        for (auto& [id, trace] : _datas) {
140
0
            ScheduleRecord record;
141
0
            while (trace.try_dequeue(record)) {
142
0
                uint64_t v = 0;
143
0
                {
144
0
                    std::unique_lock<std::mutex> l(_tg_lock);
145
0
                    v = _id_to_workload_group[query_id];
146
0
                }
147
0
                auto tmp_str = record.to_string(v);
148
0
                auto text = Slice {tmp_str};
149
0
                THROW_IF_ERROR(writer.appendv(&text, 1));
150
0
            }
151
0
        }
152
0
        THROW_IF_ERROR(writer.finalize());
153
0
        THROW_IF_ERROR(writer.close());
154
155
0
        _last_dump_time = MonotonicSeconds();
156
0
    }
157
158
0
    _datas.erase(query_id);
159
0
    {
160
0
        std::unique_lock<std::mutex> l(_tg_lock);
161
0
        _id_to_workload_group.erase(query_id);
162
0
    }
163
0
}
164
} // namespace doris::pipeline