Coverage Report

Created: 2026-08-06 20:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/io/fs/local_file_reader.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 "io/fs/local_file_reader.h"
19
20
#include <bthread/bthread.h>
21
#include <butil/iobuf.h>
22
// IWYU pragma: no_include <bthread/errno.h>
23
#include <bvar/bvar.h>
24
#include <errno.h> // IWYU pragma: keep
25
#include <fmt/format.h>
26
#include <glog/logging.h>
27
#include <unistd.h>
28
29
#include <algorithm>
30
#include <atomic>
31
#include <cstring>
32
#include <string>
33
#include <utility>
34
35
#include "common/compiler_util.h" // IWYU pragma: keep
36
#include "common/metrics/doris_metrics.h"
37
#include "cpp/sync_point.h"
38
#include "io/fs/err_utils.h"
39
#include "runtime/thread_context.h"
40
#include "runtime/workload_group/workload_group.h"
41
#include "runtime/workload_management/io_throttle.h"
42
#include "runtime/workload_management/resource_context.h"
43
#include "storage/data_dir.h"
44
#include "storage/olap_common.h"
45
#include "storage/options.h"
46
#include "util/async_io.h"
47
#include "util/debug_points.h"
48
#include "util/defer_op.h"
49
50
namespace doris {
51
namespace io {
52
// 1: initing 2: inited 0: before init
53
std::atomic_int BeConfDataDirReader::be_config_data_dir_list_state = 0;
54
55
std::vector<doris::DataDirInfo> BeConfDataDirReader::be_config_data_dir_list;
56
57
void BeConfDataDirReader::get_data_dir_by_file_path(io::Path* file_path,
58
19.5k
                                                    std::string* data_dir_arg) {
59
19.5k
    int state = be_config_data_dir_list_state.load(std::memory_order_acquire);
60
19.5k
    if (state == 0) [[unlikely]] {
61
19.5k
        return;
62
18.4E
    } else if (state == 1) [[unlikely]] {
63
0
        be_config_data_dir_list_state.wait(1);
64
0
    }
65
66
18.4E
    for (const auto& data_dir_info : be_config_data_dir_list) {
67
0
        if (data_dir_info.path.size() >= file_path->string().size()) {
68
0
            continue;
69
0
        }
70
0
        if (file_path->string().compare(0, data_dir_info.path.size(), data_dir_info.path) == 0) {
71
0
            *data_dir_arg = data_dir_info.path;
72
0
            break;
73
0
        }
74
0
    }
75
18.4E
}
76
77
void BeConfDataDirReader::init_be_conf_data_dir(
78
        const std::vector<doris::StorePath>& store_paths,
79
        const std::vector<doris::StorePath>& spill_store_paths,
80
0
        const std::vector<doris::CachePath>& cache_paths) {
81
0
    be_config_data_dir_list_state.store(1, std::memory_order_release);
82
0
    Defer defer {[]() {
83
0
        be_config_data_dir_list_state.store(2, std::memory_order_release);
84
0
        be_config_data_dir_list_state.notify_all();
85
0
    }};
86
0
    for (int i = 0; i < store_paths.size(); i++) {
87
0
        DataDirInfo data_dir_info;
88
0
        data_dir_info.path = store_paths[i].path;
89
0
        data_dir_info.storage_medium = store_paths[i].storage_medium;
90
0
        data_dir_info.data_dir_type = DataDirType::OLAP_DATA_DIR;
91
0
        data_dir_info.metric_name = "local_data_dir_" + std::to_string(i);
92
0
        be_config_data_dir_list.push_back(data_dir_info);
93
0
    }
94
95
0
    for (int i = 0; i < spill_store_paths.size(); i++) {
96
0
        doris::DataDirInfo data_dir_info;
97
0
        data_dir_info.path = spill_store_paths[i].path;
98
0
        data_dir_info.storage_medium = spill_store_paths[i].storage_medium;
99
0
        data_dir_info.data_dir_type = doris::DataDirType::SPILL_DISK_DIR;
100
0
        data_dir_info.metric_name = "spill_data_dir_" + std::to_string(i);
101
0
        be_config_data_dir_list.push_back(data_dir_info);
102
0
    }
103
104
0
    for (int i = 0; i < cache_paths.size(); i++) {
105
0
        doris::DataDirInfo data_dir_info;
106
0
        data_dir_info.path = cache_paths[i].path;
107
0
        data_dir_info.storage_medium = TStorageMedium::REMOTE_CACHE;
108
0
        data_dir_info.data_dir_type = doris::DataDirType::DATA_CACHE_DIR;
109
0
        data_dir_info.metric_name = "local_cache_dir_" + std::to_string(i);
110
0
        be_config_data_dir_list.push_back(data_dir_info);
111
0
    }
112
113
0
    std::sort(be_config_data_dir_list.begin(), be_config_data_dir_list.end(),
114
0
              [](const DataDirInfo& a, const DataDirInfo& b) {
115
0
                  return a.path.length() > b.path.length();
116
0
              });
117
0
}
118
119
LocalFileReader::LocalFileReader(Path path, size_t file_size, int fd)
120
19.5k
        : _fd(fd), _path(std::move(path)), _file_size(file_size) {
121
19.5k
    _data_dir_path = "";
122
19.5k
    BeConfDataDirReader::get_data_dir_by_file_path(&_path, &_data_dir_path);
123
19.5k
    DorisMetrics::instance()->local_file_open_reading->increment(1);
124
19.5k
    DorisMetrics::instance()->local_file_reader_total->increment(1);
125
19.5k
}
126
127
19.5k
LocalFileReader::~LocalFileReader() {
128
19.5k
    WARN_IF_ERROR(close(), fmt::format("Failed to close file {}", _path.native()));
129
19.5k
}
130
131
23.0k
Status LocalFileReader::close() {
132
23.0k
    bool expected = false;
133
23.0k
    if (_closed.compare_exchange_strong(expected, true, std::memory_order_acq_rel)) {
134
19.5k
        DorisMetrics::instance()->local_file_open_reading->increment(-1);
135
19.5k
        int res = -1;
136
19.5k
        if (bthread_self() == 0) {
137
19.5k
            res = ::close(_fd);
138
19.5k
        } else {
139
3
            auto task = [&] { res = ::close(_fd); };
140
3
            AsyncIO::run_task(task, io::FileSystemType::LOCAL);
141
3
        }
142
19.5k
        if (-1 == res) {
143
0
            std::string err = errno_to_str();
144
0
            return localfs_error(errno, fmt::format("failed to close {}", _path.native()));
145
0
        }
146
19.5k
        _fd = -1;
147
19.5k
    }
148
23.0k
    return Status::OK();
149
23.0k
}
150
151
Status LocalFileReader::read_at_impl(size_t offset, Slice result, size_t* bytes_read,
152
340k
                                     const IOContext* /*io_ctx*/) {
153
340k
    TEST_SYNC_POINT_RETURN_WITH_VALUE("LocalFileReader::read_at_impl",
154
340k
                                      Status::IOError("inject io error"));
155
340k
    if (closed()) [[unlikely]] {
156
0
        return Status::InternalError("read closed file: ", _path.native());
157
0
    }
158
159
340k
    if (offset > _file_size) {
160
0
        return Status::InternalError(
161
0
                "offset exceeds file size(offset: {}, file size: {}, path: {})", offset, _file_size,
162
0
                _path.native());
163
0
    }
164
340k
    size_t bytes_req = result.size;
165
340k
    char* to = result.data;
166
340k
    bytes_req = std::min(bytes_req, _file_size - offset);
167
340k
    *bytes_read = 0;
168
169
340k
    LIMIT_LOCAL_SCAN_IO(get_data_dir_path(), bytes_read);
170
171
680k
    while (bytes_req != 0) {
172
340k
        auto res = SYNC_POINT_HOOK_RETURN_VALUE(::pread(_fd, to, bytes_req, offset),
173
340k
                                                "LocalFileReader::pread", _fd, to);
174
340k
        DBUG_EXECUTE_IF("LocalFileReader::read_at_impl.io_error", {
175
340k
            auto sub_path = dp->param<std::string>("sub_path", "");
176
340k
            if ((sub_path.empty() && _path.filename().compare(kTestFilePath)) ||
177
340k
                (!sub_path.empty() && _path.native().find(sub_path) != std::string::npos)) {
178
340k
                res = -1;
179
340k
                errno = EIO;
180
340k
                LOG(WARNING) << Status::IOError("debug read io error: {}", _path.native());
181
340k
            }
182
340k
        });
183
340k
        if (UNLIKELY(-1 == res && errno != EINTR)) {
184
1
            return localfs_error(errno, fmt::format("failed to read {}", _path.native()));
185
1
        }
186
340k
        if (UNLIKELY(res == 0)) {
187
0
            return Status::InternalError("cannot read from {}: unexpected EOF", _path.native());
188
0
        }
189
340k
        if (res > 0) {
190
340k
            to += res;
191
340k
            offset += res;
192
340k
            bytes_req -= res;
193
340k
            *bytes_read += res;
194
340k
        }
195
340k
    }
196
340k
    DorisMetrics::instance()->local_bytes_read_total->increment(*bytes_read);
197
340k
    return Status::OK();
198
340k
}
199
200
Status LocalFileReader::read_at_iobuf_impl(size_t offset, size_t bytes_req, butil::IOBuf* out,
201
7
                                           size_t* bytes_read, const IOContext* /*io_ctx*/) {
202
7
    TEST_SYNC_POINT_RETURN_WITH_VALUE("LocalFileReader::read_at_iobuf_impl",
203
7
                                      Status::IOError("inject io error"));
204
7
    if (out == nullptr || bytes_read == nullptr) {
205
0
        return Status::InvalidArgument("read_at_iobuf requires non-null out and bytes_read");
206
0
    }
207
7
    if (closed()) [[unlikely]] {
208
0
        return Status::InternalError("read closed file: ", _path.native());
209
0
    }
210
211
7
    if (offset > _file_size) {
212
0
        return Status::InternalError(
213
0
                "offset exceeds file size(offset: {}, file size: {}, path: {})", offset, _file_size,
214
0
                _path.native());
215
0
    }
216
7
    bytes_req = std::min(bytes_req, _file_size - offset);
217
7
    *bytes_read = 0;
218
7
    if (bytes_req == 0) {
219
0
        return Status::OK();
220
0
    }
221
222
7
    LIMIT_LOCAL_SCAN_IO(get_data_dir_path(), bytes_read);
223
224
7
    butil::IOPortal portal;
225
14
    while (bytes_req != 0) {
226
7
        ssize_t res =
227
7
                portal.pappend_from_file_descriptor(_fd, static_cast<off_t>(offset), bytes_req);
228
7
        if (UNLIKELY(-1 == res && errno != EINTR)) {
229
0
            return localfs_error(errno, fmt::format("failed to read {}", _path.native()));
230
0
        }
231
7
        if (UNLIKELY(res == 0)) {
232
0
            return Status::InternalError("cannot read from {}: unexpected EOF", _path.native());
233
0
        }
234
7
        if (res > 0) {
235
7
            offset += static_cast<size_t>(res);
236
7
            bytes_req -= static_cast<size_t>(res);
237
7
            *bytes_read += static_cast<size_t>(res);
238
7
        }
239
7
    }
240
7
    out->append(portal);
241
7
    DorisMetrics::instance()->local_bytes_read_total->increment(*bytes_read);
242
7
    return Status::OK();
243
7
}
244
245
} // namespace io
246
} // namespace doris