Coverage Report

Created: 2024-11-20 21:21

/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
#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 = std::atomic_load_explicit(&_data, std::memory_order_relaxed);
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 = std::atomic_load_explicit(&_data, std::memory_order_relaxed);
59
0
    while (true) {
60
0
        auto new_map = std::make_shared<QueryTracesMap>(*map_ptr);
61
0
        handler(*new_map);
62
0
        if (std::atomic_compare_exchange_strong_explicit(&_data, &map_ptr, new_map,
63
0
                                                         std::memory_order_relaxed,
64
0
                                                         std::memory_order_relaxed)) {
65
0
            break;
66
0
        }
67
0
    }
68
0
}
69
70
0
void PipelineTracerContext::end_query(TUniqueId query_id, uint64_t workload_group) {
71
0
    {
72
0
        std::unique_lock<std::mutex> l(_tg_lock);
73
0
        _id_to_workload_group[query_id] = workload_group;
74
0
    }
75
0
    if (_dump_type == RecordType::PerQuery) {
76
0
        _dump_query(query_id);
77
0
    } else if (_dump_type == RecordType::Periodic) {
78
0
        auto now = MonotonicSeconds();
79
0
        auto interval = now - _last_dump_time;
80
0
        if (interval > _dump_interval_s) {
81
0
            _dump_timeslice();
82
0
        }
83
0
    }
84
0
}
85
86
Status PipelineTracerContext::change_record_params(
87
0
        const std::map<std::string, std::string>& params) {
88
0
    bool effective = false;
89
0
    if (auto it = params.find("type"); it != params.end()) {
90
0
        if (boost::iequals(it->second, "disable") || boost::iequals(it->second, "none")) {
91
0
            _dump_type = RecordType::None;
92
0
            effective = true;
93
0
        } else if (boost::iequals(it->second, "per_query") ||
94
0
                   boost::iequals(it->second, "perquery")) {
95
0
            _dump_type = RecordType::PerQuery;
96
0
            effective = true;
97
0
        } else if (boost::iequals(it->second, "periodic")) {
98
0
            _dump_type = RecordType::Periodic;
99
0
            _last_dump_time = MonotonicSeconds();
100
0
            effective = true;
101
0
        }
102
0
    }
103
104
0
    if (auto it = params.find("dump_interval"); it != params.end()) {
105
0
        _dump_interval_s = std::stoll(it->second); // s as unit
106
0
        effective = true;
107
0
    }
108
109
0
    return effective ? Status::OK()
110
0
                     : Status::InvalidArgument(
111
0
                               "No qualified param in changing tracing record method");
112
0
}
113
114
0
void PipelineTracerContext::_dump_query(TUniqueId query_id) {
115
0
    auto map_ptr = std::atomic_load_explicit(&_data, std::memory_order_relaxed);
116
0
    auto path = _log_dir / fmt::format("query{}", to_string(query_id));
117
0
    int fd = ::open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC,
118
0
                    S_ISGID | S_ISUID | S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
119
0
    if (fd < 0) [[unlikely]] {
120
0
        throw Exception(Status::Error<ErrorCode::CREATE_FILE_ERROR>(
121
0
                "create tracing log file {} failed", path.c_str()));
122
0
    }
123
0
    auto writer = io::LocalFileWriter {path, fd};
124
125
0
    ScheduleRecord record;
126
0
    while ((*map_ptr)[QueryID {query_id}]->try_dequeue(record)) {
127
0
        uint64_t v = 0;
128
0
        {
129
0
            std::unique_lock<std::mutex> l(_tg_lock);
130
0
            v = _id_to_workload_group.at(query_id);
131
0
        }
132
0
        auto tmp_str = record.to_string(v);
133
0
        auto text = Slice {tmp_str};
134
0
        THROW_IF_ERROR(writer.appendv(&text, 1));
135
0
    }
136
137
0
    THROW_IF_ERROR(writer.close());
138
139
0
    _last_dump_time = MonotonicSeconds();
140
141
0
    _update([&](QueryTracesMap& new_map) { _data->erase(QueryID {query_id}); });
142
143
0
    {
144
0
        std::unique_lock<std::mutex> l(_tg_lock);
145
0
        _id_to_workload_group.erase(query_id);
146
0
    }
147
0
}
148
149
0
void PipelineTracerContext::_dump_timeslice() {
150
0
    auto new_map = std::make_shared<QueryTracesMap>();
151
0
    new_map.swap(_data);
152
    //TODO: if long time, per timeslice per file
153
0
    auto path = _log_dir /
154
0
                fmt::format("until{}", std::chrono::steady_clock::now().time_since_epoch().count());
155
0
    int fd = ::open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC,
156
0
                    S_ISGID | S_ISUID | S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
157
0
    if (fd < 0) [[unlikely]] {
158
0
        throw Exception(Status::Error<ErrorCode::CREATE_FILE_ERROR>(
159
0
                "create tracing log file {} failed", path.c_str()));
160
0
    }
161
0
    auto writer = io::LocalFileWriter {path, fd};
162
163
    // dump all query traces in this time window to one file.
164
0
    for (auto& [query_id, trace] : (*new_map)) {
165
0
        ScheduleRecord record;
166
0
        while (trace->try_dequeue(record)) {
167
0
            uint64_t v = 0;
168
0
            {
169
0
                std::unique_lock<std::mutex> l(_tg_lock);
170
0
                v = _id_to_workload_group.at(query_id.query_id);
171
0
            }
172
0
            auto tmp_str = record.to_string(v);
173
0
            auto text = Slice {tmp_str};
174
0
            THROW_IF_ERROR(writer.appendv(&text, 1));
175
0
        }
176
0
    }
177
0
    THROW_IF_ERROR(writer.close());
178
179
0
    _last_dump_time = MonotonicSeconds();
180
181
0
    _id_to_workload_group.clear();
182
0
}
183
} // namespace doris::pipeline