Coverage Report

Created: 2026-06-09 14:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/io/file_factory.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/file_factory.h"
19
20
#include <gen_cpp/PaloInternalService_types.h>
21
#include <gen_cpp/PlanNodes_types.h>
22
#include <gen_cpp/Types_types.h>
23
24
#include <mutex>
25
#include <utility>
26
27
#include "common/cast_set.h"
28
#include "common/config.h"
29
#include "common/status.h"
30
#include "io/fs/broker_file_system.h"
31
#include "io/fs/broker_file_writer.h"
32
#include "io/fs/file_reader.h"
33
#include "io/fs/file_system.h"
34
#include "io/fs/hdfs/hdfs_mgr.h"
35
#include "io/fs/hdfs_file_reader.h"
36
#include "io/fs/hdfs_file_system.h"
37
#include "io/fs/hdfs_file_writer.h"
38
#include "io/fs/http_file_reader.h"
39
#include "io/fs/http_file_system.h"
40
#include "io/fs/local_file_system.h"
41
#include "io/fs/multi_table_pipe.h"
42
#include "io/fs/s3_file_reader.h"
43
#include "io/fs/s3_file_system.h"
44
#include "io/fs/s3_file_writer.h"
45
#include "io/fs/stream_load_pipe.h"
46
#include "io/hdfs_builder.h"
47
#include "io/hdfs_util.h"
48
#include "load/stream_load/new_load_stream_mgr.h"
49
#include "load/stream_load/stream_load_context.h"
50
#include "runtime/exec_env.h"
51
#include "runtime/runtime_state.h"
52
#include "util/s3_uri.h"
53
#include "util/s3_util.h"
54
#include "util/uid_util.h"
55
56
namespace doris {
57
58
constexpr std::string_view RANDOM_CACHE_BASE_PATH = "random";
59
60
io::FileReaderOptions FileFactory::get_reader_options(const TQueryOptions& option,
61
79
                                                      const io::FileDescription& fd) {
62
79
    io::FileReaderOptions opts {
63
79
            .cache_base_path {},
64
79
            .file_size = fd.file_size,
65
79
            .mtime = fd.mtime,
66
79
    };
67
79
    if (config::enable_file_cache && option.__isset.enable_file_cache && option.enable_file_cache &&
68
79
        fd.file_cache_admission) {
69
0
        opts.cache_type = io::FileCachePolicy::FILE_BLOCK_CACHE;
70
0
    }
71
79
    if (option.__isset.file_cache_base_path &&
72
79
        option.file_cache_base_path != RANDOM_CACHE_BASE_PATH) {
73
0
        opts.cache_base_path = option.file_cache_base_path;
74
0
    }
75
79
    return opts;
76
79
}
77
78
0
int32_t get_broker_index(const std::vector<TNetworkAddress>& brokers, const std::string& path) {
79
0
    if (brokers.empty()) {
80
0
        return -1;
81
0
    }
82
83
    // firstly find local broker
84
0
    const auto local_host = BackendOptions::get_localhost();
85
0
    for (int32_t i = 0; i < brokers.size(); ++i) {
86
0
        if (brokers[i].hostname == local_host) {
87
0
            return i;
88
0
        }
89
0
    }
90
91
    // secondly select broker by hash of file path
92
0
    auto key = HashUtil::hash(path.data(), cast_set<uint32_t>(path.size()), 0);
93
94
0
    return key % brokers.size();
95
0
}
96
97
Result<io::FileSystemSPtr> FileFactory::create_fs(const io::FSPropertiesRef& fs_properties,
98
1
                                                  const io::FileDescription& file_description) {
99
1
    switch (fs_properties.type) {
100
1
    case TFileType::FILE_LOCAL:
101
1
        return io::global_local_filesystem();
102
0
    case TFileType::FILE_BROKER: {
103
0
        auto index = get_broker_index(*fs_properties.broker_addresses, file_description.path);
104
0
        if (index < 0) {
105
0
            return ResultError(Status::InternalError("empty broker_addresses"));
106
0
        }
107
0
        LOG_INFO("select broker: {} for file {}", (*fs_properties.broker_addresses)[index].hostname,
108
0
                 file_description.path);
109
0
        return io::BrokerFileSystem::create((*fs_properties.broker_addresses)[index],
110
0
                                            *fs_properties.properties, io::FileSystem::TMP_FS_ID);
111
0
    }
112
0
    case TFileType::FILE_S3: {
113
0
        S3URI s3_uri(file_description.path);
114
0
        RETURN_IF_ERROR_RESULT(s3_uri.parse());
115
0
        S3Conf s3_conf;
116
0
        RETURN_IF_ERROR_RESULT(S3ClientFactory::convert_properties_to_s3_conf(
117
0
                *fs_properties.properties, s3_uri, &s3_conf));
118
0
        return io::S3FileSystem::create(std::move(s3_conf), io::FileSystem::TMP_FS_ID);
119
0
    }
120
0
    case TFileType::FILE_HDFS: {
121
0
        std::string fs_name = _get_fs_name(file_description);
122
0
        return io::HdfsFileSystem::create(*fs_properties.properties, fs_name,
123
0
                                          io::FileSystem::TMP_FS_ID, nullptr);
124
0
    }
125
0
    case TFileType::FILE_HTTP: {
126
0
        const auto& kv = *fs_properties.properties;
127
0
        auto it = kv.find("uri");
128
0
        if (it == kv.end() || it->second.empty()) {
129
0
            return ResultError(Status::InternalError("http fs must set uri property"));
130
0
        }
131
0
        return io::HttpFileSystem::create(it->second, io::FileSystem::TMP_FS_ID, kv);
132
0
    }
133
0
    default:
134
0
        return ResultError(Status::InternalError("unsupported fs type: {}",
135
0
                                                 std::to_string(fs_properties.type)));
136
1
    }
137
1
}
138
139
0
std::string FileFactory::_get_fs_name(const io::FileDescription& file_description) {
140
    // If the destination path contains a schema, use the schema directly.
141
    // If not, use origin file_description.fs_name
142
    // Because the default fsname in file_description.fs_name maybe different from
143
    // file's.
144
    // example:
145
    //    hdfs://host:port/path1/path2  --> hdfs://host:port
146
    //    hdfs://nameservice/path1/path2 --> hdfs://nameservice
147
0
    std::string fs_name = file_description.fs_name;
148
0
    std::string::size_type idx = file_description.path.find("://");
149
0
    if (idx != std::string::npos) {
150
0
        idx = file_description.path.find('/', idx + 3);
151
0
        if (idx != std::string::npos) {
152
0
            fs_name = file_description.path.substr(0, idx);
153
0
        }
154
0
    }
155
0
    return fs_name;
156
0
}
157
158
Result<io::FileWriterPtr> FileFactory::create_file_writer(
159
        TFileType::type type, ExecEnv* env, const std::vector<TNetworkAddress>& broker_addresses,
160
        const std::map<std::string, std::string>& properties, const std::string& path,
161
0
        const io::FileWriterOptions& options) {
162
0
    io::FileWriterPtr file_writer;
163
0
    switch (type) {
164
0
    case TFileType::FILE_LOCAL: {
165
0
        RETURN_IF_ERROR_RESULT(io::global_local_filesystem()->create_file(path, &file_writer));
166
0
        return file_writer;
167
0
    }
168
0
    case TFileType::FILE_BROKER: {
169
0
        auto index = get_broker_index(broker_addresses, path);
170
0
        if (index < 0) {
171
0
            return ResultError(Status::InternalError("empty broker_addresses"));
172
0
        }
173
0
        LOG_INFO("select broker: {} for file {}", broker_addresses[index].hostname, path);
174
0
        return io::BrokerFileWriter::create(env, broker_addresses[index], properties, path);
175
0
    }
176
0
    case TFileType::FILE_S3: {
177
0
        S3URI s3_uri(path);
178
0
        RETURN_IF_ERROR_RESULT(s3_uri.parse());
179
0
        S3Conf s3_conf;
180
0
        RETURN_IF_ERROR_RESULT(
181
0
                S3ClientFactory::convert_properties_to_s3_conf(properties, s3_uri, &s3_conf));
182
0
        auto client = std::make_shared<io::ObjClientHolder>(std::move(s3_conf.client_conf));
183
0
        RETURN_IF_ERROR_RESULT(client->init());
184
0
        return std::make_unique<io::S3FileWriter>(std::move(client), std::move(s3_conf.bucket),
185
0
                                                  s3_uri.get_key(), &options);
186
0
    }
187
0
    case TFileType::FILE_HDFS: {
188
0
        THdfsParams hdfs_params = parse_properties(properties);
189
0
        std::shared_ptr<io::HdfsHandler> handler;
190
0
        RETURN_IF_ERROR_RESULT(ExecEnv::GetInstance()->hdfs_mgr()->get_or_create_fs(
191
0
                hdfs_params, hdfs_params.fs_name, &handler));
192
0
        return io::HdfsFileWriter::create(path, handler, hdfs_params.fs_name, &options);
193
0
    }
194
0
    default:
195
0
        return ResultError(
196
0
                Status::InternalError("unsupported file writer type: {}", std::to_string(type)));
197
0
    }
198
0
}
199
200
Result<io::FileReaderSPtr> FileFactory::create_file_reader(
201
        const io::FileSystemProperties& system_properties,
202
        const io::FileDescription& file_description, const io::FileReaderOptions& reader_options,
203
79
        RuntimeProfile* profile) {
204
79
    auto reader_res = _create_file_reader_internal(system_properties, file_description,
205
79
                                                   reader_options, profile);
206
79
    if (!reader_res.has_value()) {
207
0
        return unexpected(std::move(reader_res).error());
208
0
    }
209
79
    return std::move(reader_res).value();
210
79
}
211
212
Result<io::FileReaderSPtr> FileFactory::_create_file_reader_internal(
213
        const io::FileSystemProperties& system_properties,
214
        const io::FileDescription& file_description, const io::FileReaderOptions& reader_options,
215
79
        RuntimeProfile* profile) {
216
79
    TFileType::type type = system_properties.system_type;
217
79
    switch (type) {
218
79
    case TFileType::FILE_LOCAL: {
219
79
        io::FileReaderSPtr file_reader;
220
79
        RETURN_IF_ERROR_RESULT(io::global_local_filesystem()->open_file(
221
79
                file_description.path, &file_reader, &reader_options));
222
79
        return file_reader;
223
79
    }
224
0
    case TFileType::FILE_S3: {
225
0
        S3URI s3_uri(file_description.path);
226
0
        RETURN_IF_ERROR_RESULT(s3_uri.parse());
227
0
        S3Conf s3_conf;
228
0
        RETURN_IF_ERROR_RESULT(S3ClientFactory::convert_properties_to_s3_conf(
229
0
                system_properties.properties, s3_uri, &s3_conf));
230
0
        auto client_holder = std::make_shared<io::ObjClientHolder>(s3_conf.client_conf);
231
0
        RETURN_IF_ERROR_RESULT(client_holder->init());
232
0
        return io::S3FileReader::create(std::move(client_holder), s3_conf.bucket, s3_uri.get_key(),
233
0
                                        file_description.file_size, profile)
234
0
                .and_then([&](auto&& reader) {
235
0
                    return io::create_cached_file_reader(std::move(reader), reader_options);
236
0
                });
237
0
    }
238
0
    case TFileType::FILE_HDFS: {
239
0
        std::shared_ptr<io::HdfsHandler> handler;
240
        // FIXME(plat1ko): Explain the difference between `system_properties.hdfs_params.fs_name`
241
        // and `file_description.fs_name`, it's so confused.
242
0
        const auto* fs_name = &file_description.fs_name;
243
0
        if (fs_name->empty()) {
244
0
            fs_name = &system_properties.hdfs_params.fs_name;
245
0
        }
246
0
        RETURN_IF_ERROR_RESULT(ExecEnv::GetInstance()->hdfs_mgr()->get_or_create_fs(
247
0
                system_properties.hdfs_params, *fs_name, &handler));
248
0
        return io::HdfsFileReader::create(file_description.path, handler->hdfs_fs, *fs_name,
249
0
                                          reader_options, profile)
250
0
                .and_then([&](auto&& reader) {
251
0
                    return io::create_cached_file_reader(std::move(reader), reader_options);
252
0
                });
253
0
    }
254
0
    case TFileType::FILE_BROKER: {
255
0
        auto index = get_broker_index(system_properties.broker_addresses, file_description.path);
256
0
        if (index < 0) {
257
0
            return ResultError(Status::InternalError("empty broker_addresses"));
258
0
        }
259
0
        LOG_INFO("select broker: {} for file {}",
260
0
                 system_properties.broker_addresses[index].hostname, file_description.path);
261
        // TODO(plat1ko): Create `FileReader` without FS
262
0
        return io::BrokerFileSystem::create(system_properties.broker_addresses[index],
263
0
                                            system_properties.properties, io::FileSystem::TMP_FS_ID)
264
0
                .and_then([&](auto&& fs) -> Result<io::FileReaderSPtr> {
265
0
                    io::FileReaderSPtr file_reader;
266
0
                    RETURN_IF_ERROR_RESULT(
267
0
                            fs->open_file(file_description.path, &file_reader, &reader_options));
268
0
                    return file_reader;
269
0
                });
270
0
    }
271
0
    case TFileType::FILE_HTTP: {
272
0
        return io::HttpFileReader::create(file_description.path, system_properties.properties,
273
0
                                          reader_options, profile)
274
0
                .and_then([&](auto&& reader) {
275
0
                    return io::create_cached_file_reader(std::move(reader), reader_options);
276
0
                });
277
0
    }
278
0
    default:
279
0
        return ResultError(
280
0
                Status::InternalError("unsupported file reader type: {}", std::to_string(type)));
281
79
    }
282
79
}
283
284
// file scan node/stream load pipe
285
Status FileFactory::create_pipe_reader(const TUniqueId& load_id, io::FileReaderSPtr* file_reader,
286
0
                                       RuntimeState* runtime_state, bool need_schema) {
287
0
    auto stream_load_ctx = ExecEnv::GetInstance()->new_load_stream_mgr()->get(load_id);
288
0
    if (!stream_load_ctx) {
289
0
        return Status::InternalError("unknown stream load id: {}", UniqueId(load_id).to_string());
290
0
    }
291
0
    if (need_schema) {
292
0
        RETURN_IF_ERROR(stream_load_ctx->allocate_schema_buffer());
293
        // Here, a portion of the data is processed to parse column information
294
0
        auto pipe = std::make_shared<io::StreamLoadPipe>(
295
0
                io::kMaxPipeBufferedBytes /* max_buffered_bytes */, 64 * 1024 /* min_chunk_size */,
296
0
                stream_load_ctx->schema_buffer()->pos /* total_length */);
297
0
        stream_load_ctx->schema_buffer()->flip();
298
0
        RETURN_IF_ERROR(pipe->append(stream_load_ctx->schema_buffer()));
299
0
        RETURN_IF_ERROR(pipe->finish());
300
0
        *file_reader = std::move(pipe);
301
0
    } else {
302
0
        *file_reader = stream_load_ctx->pipe;
303
0
    }
304
305
0
    return Status::OK();
306
0
}
307
308
} // namespace doris