Coverage Report

Created: 2026-09-02 17:47

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