Coverage Report

Created: 2026-09-13 01:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/io/fs/hdfs_file_system.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/hdfs_file_system.h"
19
20
#include <errno.h>
21
#include <fcntl.h>
22
#include <gen_cpp/PlanNodes_types.h>
23
24
#include <algorithm>
25
#include <filesystem>
26
#include <map>
27
#include <mutex>
28
#include <ostream>
29
#include <unordered_map>
30
#include <utility>
31
32
#include "common/config.h"
33
#include "common/status.h"
34
#include "core/pod_array.h"
35
#include "io/fs/err_utils.h"
36
#include "io/fs/hdfs/hdfs_mgr.h"
37
#include "io/fs/hdfs_file_reader.h"
38
#include "io/fs/hdfs_file_writer.h"
39
#include "io/fs/local_file_system.h"
40
#include "io/hdfs_builder.h"
41
#include "io/hdfs_util.h"
42
#include "runtime/exec_env.h"
43
#include "util/bvar_helper.h"
44
#include "util/obj_lru_cache.h"
45
#include "util/slice.h"
46
47
namespace doris::io {
48
49
#ifndef CHECK_HDFS_HANDLER
50
#define CHECK_HDFS_HANDLER(handler)                        \
51
5
    if (!handler) {                                        \
52
5
        return Status::IOError("init Hdfs handler error"); \
53
5
    }
54
#endif
55
56
Result<std::shared_ptr<HdfsFileSystem>> HdfsFileSystem::create(
57
        const std::map<std::string, std::string>& properties, std::string fs_name, std::string id,
58
1
        std::string root_path) {
59
1
    return HdfsFileSystem::create(parse_properties(properties), std::move(fs_name), std::move(id),
60
1
                                  std::move(root_path));
61
1
}
62
63
Result<std::shared_ptr<HdfsFileSystem>> HdfsFileSystem::create(const THdfsParams& hdfs_params,
64
                                                               std::string fs_name, std::string id,
65
1
                                                               std::string root_path) {
66
1
#ifdef USE_HADOOP_HDFS
67
1
    if (!config::enable_java_support) {
68
1
        return ResultError(Status::InternalError(
69
1
                "hdfs file system is not enabled, you can change be config enable_java_support to "
70
1
                "true."));
71
1
    }
72
0
#endif
73
0
    std::shared_ptr<HdfsFileSystem> fs(new HdfsFileSystem(hdfs_params, std::move(fs_name),
74
0
                                                          std::move(id), std::move(root_path)));
75
0
    RETURN_IF_ERROR_RESULT(fs->init());
76
0
    return fs;
77
0
}
78
79
HdfsFileSystem::HdfsFileSystem(const THdfsParams& hdfs_params, std::string fs_name, std::string id,
80
                               std::string root_path)
81
5
        : RemoteFileSystem(std::move(root_path), std::move(id), FileSystemType::HDFS),
82
5
          _hdfs_params(hdfs_params),
83
5
          _fs_name(std::move(fs_name)) {
84
5
    if (_fs_name.empty()) {
85
0
        _fs_name = hdfs_params.fs_name;
86
0
    }
87
5
}
88
89
5
HdfsFileSystem::~HdfsFileSystem() = default;
90
91
0
Status HdfsFileSystem::init() {
92
0
    RETURN_IF_ERROR(ExecEnv::GetInstance()->hdfs_mgr()->get_or_create_fs(_hdfs_params, _fs_name,
93
0
                                                                         &_fs_handler));
94
0
    if (!_fs_handler) {
95
0
        return Status::InternalError("failed to init Hdfs handler with, please check hdfs params.");
96
0
    }
97
0
    return Status::OK();
98
0
}
99
100
Status HdfsFileSystem::create_file_impl(const Path& file, FileWriterPtr* writer,
101
0
                                        const FileWriterOptions* opts) {
102
0
    auto res = io::HdfsFileWriter::create(file, _fs_handler, _fs_name, opts);
103
0
    if (res.has_value()) {
104
0
        *writer = std::move(res).value();
105
0
        return Status::OK();
106
0
    } else {
107
0
        return std::move(res).error();
108
0
    }
109
0
}
110
111
Status HdfsFileSystem::open_file_internal(const Path& file, FileReaderSPtr* reader,
112
1
                                          const FileReaderOptions& opts) {
113
1
    CHECK_HDFS_HANDLER(_fs_handler);
114
0
    *reader = DORIS_TRY(HdfsFileReader::create(file, _fs_handler->hdfs_fs, _fs_name, opts));
115
0
    return Status::OK();
116
0
}
117
118
1
Status HdfsFileSystem::create_directory_impl(const Path& dir, bool failed_if_exists) {
119
1
    CHECK_HDFS_HANDLER(_fs_handler);
120
0
    Path real_path = convert_path(dir, _fs_name);
121
0
    int res;
122
0
    {
123
0
        SCOPED_BVAR_LATENCY(hdfs_bvar::hdfs_create_dir_latency);
124
0
        res = hdfsCreateDirectory(_fs_handler->hdfs_fs, real_path.string().c_str());
125
0
    }
126
0
    if (res == -1) {
127
0
        return Status::IOError("failed to create directory {}: {}", dir.native(), hdfs_error());
128
0
    }
129
0
    return Status::OK();
130
0
}
131
132
0
Status HdfsFileSystem::delete_file_impl(const Path& file) {
133
0
    return delete_internal(file, 0);
134
0
}
135
136
0
Status HdfsFileSystem::delete_directory_impl(const Path& dir) {
137
0
    return delete_internal(dir, 1);
138
0
}
139
140
0
Status HdfsFileSystem::batch_delete_impl(const std::vector<Path>& files) {
141
0
    for (auto& file : files) {
142
0
        RETURN_IF_ERROR(delete_file_impl(file));
143
0
    }
144
0
    return Status::OK();
145
0
}
146
147
0
Status HdfsFileSystem::delete_internal(const Path& path, int is_recursive) {
148
0
    bool exists = true;
149
0
    RETURN_IF_ERROR(exists_impl(path, &exists));
150
0
    if (!exists) {
151
0
        return Status::OK();
152
0
    }
153
0
    CHECK_HDFS_HANDLER(_fs_handler);
154
0
    Path real_path = convert_path(path, _fs_name);
155
0
    int res = hdfsDelete(_fs_handler->hdfs_fs, real_path.string().c_str(), is_recursive);
156
0
    if (res == -1) {
157
0
        return Status::IOError("failed to delete directory {}: {}", path.native(), hdfs_error());
158
0
    }
159
0
    return Status::OK();
160
0
}
161
162
1
Status HdfsFileSystem::exists_impl(const Path& path, bool* res) const {
163
1
    CHECK_HDFS_HANDLER(_fs_handler);
164
0
    Path real_path = convert_path(path, _fs_name);
165
0
    int is_exists = hdfsExists(_fs_handler->hdfs_fs, real_path.string().c_str());
166
0
#ifdef USE_HADOOP_HDFS
167
    // when calling hdfsExists() and return non-zero code,
168
    // if errno is ENOENT, which means the file does not exist.
169
    // if errno is not ENOENT, which means it encounter other error, should return.
170
    // NOTE: not for libhdfs3 since it only runs on MaxOS, don't have to support it.
171
    //
172
    // See details:
173
    //  https://github.com/apache/hadoop/blob/5cda162a804fb0cfc2a5ac0058ab407662c5fb00/
174
    //  hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/hdfs.c#L1923-L1924
175
0
    if (is_exists != 0 && errno != ENOENT) {
176
0
        char* root_cause = hdfsGetLastExceptionRootCause();
177
0
        return Status::IOError("failed to check path existence {}: {}", path.native(),
178
0
                               (root_cause ? root_cause : "unknown"));
179
0
    }
180
0
#endif
181
0
    *res = (is_exists == 0);
182
0
    return Status::OK();
183
0
}
184
185
1
Status HdfsFileSystem::file_size_impl(const Path& path, int64_t* file_size) const {
186
1
    CHECK_HDFS_HANDLER(_fs_handler);
187
0
    Path real_path = convert_path(path, _fs_name);
188
0
    SCOPED_BVAR_LATENCY(hdfs_bvar::hdfs_get_path_info_latency);
189
0
    hdfsFileInfo* file_info = hdfsGetPathInfo(_fs_handler->hdfs_fs, real_path.string().c_str());
190
0
    if (file_info == nullptr) {
191
0
        return Status::IOError("failed to get file size of {}: {}", path.native(), hdfs_error());
192
0
    }
193
0
    *file_size = file_info->mSize;
194
0
    hdfsFreeFileInfo(file_info, 1);
195
0
    return Status::OK();
196
0
}
197
198
Status HdfsFileSystem::list_impl(const Path& path, bool only_file, std::vector<FileInfo>* files,
199
0
                                 bool* exists) {
200
0
    RETURN_IF_ERROR(exists_impl(path, exists));
201
0
    if (!(*exists)) {
202
0
        return Status::OK();
203
0
    }
204
205
0
    CHECK_HDFS_HANDLER(_fs_handler);
206
0
    Path real_path = convert_path(path, _fs_name);
207
0
    int numEntries = 0;
208
0
    hdfsFileInfo* hdfs_file_info =
209
0
            hdfsListDirectory(_fs_handler->hdfs_fs, real_path.c_str(), &numEntries);
210
0
    if (hdfs_file_info == nullptr) {
211
0
        return Status::IOError("failed to list files/directors {}: {}", path.native(),
212
0
                               hdfs_error());
213
0
    }
214
0
    for (int idx = 0; idx < numEntries; ++idx) {
215
0
        auto& file = hdfs_file_info[idx];
216
0
        if (only_file && file.mKind == kObjectKindDirectory) {
217
0
            continue;
218
0
        }
219
0
        auto& file_info = files->emplace_back();
220
0
        std::string_view fname(file.mName);
221
0
        fname.remove_prefix(fname.rfind('/') + 1);
222
0
        file_info.file_name = fname;
223
0
        file_info.file_size = file.mSize;
224
0
        file_info.is_file = (file.mKind != kObjectKindDirectory);
225
0
    }
226
0
    hdfsFreeFileInfo(hdfs_file_info, numEntries);
227
0
    return Status::OK();
228
0
}
229
230
1
Status HdfsFileSystem::rename_impl(const Path& orig_name, const Path& new_name) {
231
1
    CHECK_HDFS_HANDLER(_fs_handler);
232
0
    Path normal_orig_name = convert_path(orig_name, _fs_name);
233
0
    Path normal_new_name = convert_path(new_name, _fs_name);
234
0
    int ret = hdfsRename(_fs_handler->hdfs_fs, normal_orig_name.c_str(), normal_new_name.c_str());
235
0
    if (ret == 0) {
236
0
        LOG(INFO) << "finished to rename file. orig: " << normal_orig_name
237
0
                  << ", new: " << normal_new_name;
238
0
        return Status::OK();
239
0
    } else {
240
0
        return Status::IOError("fail to rename from {} to {}: {}", normal_orig_name.native(),
241
0
                               normal_new_name.native(), hdfs_error());
242
0
    }
243
0
    return Status::OK();
244
0
}
245
246
0
Status HdfsFileSystem::upload_impl(const Path& local_file, const Path& remote_file) {
247
    // 1. open local file for read
248
0
    FileSystemSPtr local_fs = global_local_filesystem();
249
0
    FileReaderSPtr local_reader = nullptr;
250
0
    RETURN_IF_ERROR(local_fs->open_file(local_file, &local_reader));
251
0
    int64_t file_len = local_reader->size();
252
0
    if (file_len == -1) {
253
0
        return Status::IOError("failed to get size of file: {}", local_file.string());
254
0
    }
255
256
    // 2. open remote file for write
257
0
    FileWriterPtr hdfs_writer = nullptr;
258
0
    RETURN_IF_ERROR(create_file_impl(remote_file, &hdfs_writer, nullptr));
259
260
0
    constexpr size_t buf_sz = 1024 * 1024;
261
0
    char read_buf[buf_sz];
262
0
    size_t left_len = file_len;
263
0
    size_t read_offset = 0;
264
0
    size_t bytes_read = 0;
265
0
    while (left_len > 0) {
266
0
        size_t read_len = left_len > buf_sz ? buf_sz : left_len;
267
0
        RETURN_IF_ERROR(local_reader->read_at(read_offset, {read_buf, read_len}, &bytes_read));
268
0
        RETURN_IF_ERROR(hdfs_writer->append({read_buf, read_len}));
269
270
0
        read_offset += read_len;
271
0
        left_len -= read_len;
272
0
    }
273
274
0
    return hdfs_writer->close();
275
0
}
276
277
Status HdfsFileSystem::batch_upload_impl(const std::vector<Path>& local_files,
278
0
                                         const std::vector<Path>& remote_files) {
279
0
    DCHECK(local_files.size() == remote_files.size());
280
0
    for (int i = 0; i < local_files.size(); ++i) {
281
0
        RETURN_IF_ERROR(upload_impl(local_files[i], remote_files[i]));
282
0
    }
283
0
    return Status::OK();
284
0
}
285
286
0
Status HdfsFileSystem::download_impl(const Path& remote_file, const Path& local_file) {
287
    // 1. open remote file for read
288
0
    FileReaderSPtr hdfs_reader = nullptr;
289
0
    RETURN_IF_ERROR(open_file_internal(remote_file, &hdfs_reader, FileReaderOptions::DEFAULT));
290
291
    // 2. remove the existing local file if exist
292
0
    if (std::filesystem::remove(local_file)) {
293
0
        LOG(INFO) << "remove the previously exist local file: " << local_file;
294
0
    }
295
296
    // 3. open local file for write
297
0
    FileSystemSPtr local_fs = global_local_filesystem();
298
0
    FileWriterPtr local_writer = nullptr;
299
0
    RETURN_IF_ERROR(local_fs->create_file(local_file, &local_writer));
300
301
    // 4. read remote and write to local
302
0
    LOG(INFO) << "read remote file: " << remote_file << " to local: " << local_file;
303
0
    constexpr size_t buf_sz = 1024 * 1024;
304
0
    PODArray<char> read_buf;
305
0
    read_buf.resize(buf_sz);
306
0
    size_t cur_offset = 0;
307
0
    while (true) {
308
0
        size_t read_len = 0;
309
0
        Slice file_slice(read_buf.data(), buf_sz);
310
0
        RETURN_IF_ERROR(hdfs_reader->read_at(cur_offset, file_slice, &read_len));
311
0
        cur_offset += read_len;
312
0
        if (read_len == 0) {
313
0
            break;
314
0
        }
315
316
0
        RETURN_IF_ERROR(local_writer->append({read_buf.data(), read_len}));
317
0
    }
318
0
    return local_writer->close();
319
0
}
320
321
} // namespace doris::io