Coverage Report

Created: 2026-03-15 17:28

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