Coverage Report

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