Coverage Report

Created: 2026-07-22 13:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/s3_util.h
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
#pragma once
19
20
#include <aws/core/Aws.h>
21
#include <aws/core/client/ClientConfiguration.h>
22
#include <aws/s3/S3Errors.h>
23
#include <bvar/bvar.h>
24
#include <fmt/format.h>
25
#include <gen_cpp/AgentService_types.h>
26
#include <gen_cpp/cloud.pb.h>
27
28
#include <functional>
29
#include <map>
30
#include <memory>
31
#include <mutex>
32
#include <string>
33
#include <unordered_map>
34
35
#include "common/status.h"
36
#include "core/string_ref.h"
37
#include "cpp/aws_common.h"
38
#include "cpp/token_bucket_rate_limiter.h"
39
#include "io/fs/obj_storage_client.h"
40
41
namespace Aws::S3 {
42
class S3Client;
43
} // namespace Aws::S3
44
45
namespace bvar {
46
template <typename T>
47
class Adder;
48
}
49
50
namespace doris {
51
52
namespace s3_bvar {
53
extern bvar::LatencyRecorder s3_get_latency;
54
extern bvar::LatencyRecorder s3_put_latency;
55
extern bvar::LatencyRecorder s3_delete_object_latency;
56
extern bvar::LatencyRecorder s3_delete_objects_latency;
57
extern bvar::LatencyRecorder s3_head_latency;
58
extern bvar::LatencyRecorder s3_multi_part_upload_latency;
59
extern bvar::LatencyRecorder s3_list_latency;
60
extern bvar::LatencyRecorder s3_list_object_versions_latency;
61
extern bvar::LatencyRecorder s3_get_bucket_version_latency;
62
extern bvar::LatencyRecorder s3_copy_object_latency;
63
}; // namespace s3_bvar
64
65
std::string hide_access_key(const std::string& ak);
66
67
class S3URI;
68
struct S3ClientConf {
69
    std::string endpoint;
70
    std::string region;
71
    std::string ak;
72
    std::string sk;
73
    std::string token;
74
    // For azure we'd better support the bucket at the first time init azure blob container client
75
    std::string bucket;
76
    io::ObjStorageType provider = io::ObjStorageType::AWS;
77
    int max_connections = -1;
78
    int request_timeout_ms = -1;
79
    int connect_timeout_ms = -1;
80
    bool use_virtual_addressing = true;
81
    // For aws s3, no need to override endpoint
82
    bool need_override_endpoint = true;
83
84
    CredProviderType cred_provider_type = CredProviderType::Default;
85
    std::string role_arn;
86
    std::string external_id;
87
    // True when this client is bound to a Doris internal object storage bucket
88
    // (a storage vault in cloud mode). S3ClientFactory wraps such clients with the
89
    // shared rate limiter; external buckets (S3 load, TVF, external catalogs) are
90
    // returned bare in cloud mode.
91
    bool is_internal_bucket = false;
92
93
    // Full-field identity. get_hash() is only good for picking an unordered_map
94
    // bucket; distinct configurations can collide, so never treat hash equality as
95
    // configuration equality.
96
383k
    bool operator==(const S3ClientConf&) const = default;
97
98
22.3k
    uint64_t get_hash() const {
99
22.3k
        uint64_t hash_code = 0;
100
        // Use crc32_hash(ak + sk) hash to prevent swapped AK/SK order from producing same result.
101
22.3k
        hash_code ^= crc32_hash(ak + sk);
102
22.3k
        hash_code ^= crc32_hash(token);
103
22.3k
        hash_code ^= crc32_hash(endpoint);
104
22.3k
        hash_code ^= crc32_hash(region);
105
22.3k
        hash_code ^= crc32_hash(bucket);
106
22.3k
        hash_code ^= max_connections;
107
22.3k
        hash_code ^= request_timeout_ms;
108
22.3k
        hash_code ^= connect_timeout_ms;
109
22.3k
        hash_code ^= use_virtual_addressing;
110
22.3k
        hash_code ^= static_cast<int>(provider);
111
112
22.3k
        hash_code ^= static_cast<int>(cred_provider_type);
113
22.3k
        hash_code ^= crc32_hash(role_arn);
114
22.3k
        hash_code ^= crc32_hash(external_id);
115
22.3k
        hash_code ^= is_internal_bucket;
116
22.3k
        return hash_code;
117
22.3k
    }
118
119
92
    std::string to_string() const {
120
92
        return fmt::format(
121
92
                "(ak={}, token={}, endpoint={}, region={}, bucket={}, max_connections={}, "
122
92
                "request_timeout_ms={}, connect_timeout_ms={}, use_virtual_addressing={}, "
123
92
                "cred_provider_type={},role_arn={}, external_id={}, is_internal_bucket={}",
124
92
                hide_access_key(ak), token, endpoint, region, bucket, max_connections,
125
92
                request_timeout_ms, connect_timeout_ms, use_virtual_addressing, cred_provider_type,
126
92
                role_arn, external_id, is_internal_bucket);
127
92
    }
128
};
129
130
struct S3ClientConfHash {
131
22.3k
    size_t operator()(const S3ClientConf& conf) const {
132
22.3k
        return static_cast<size_t>(conf.get_hash());
133
22.3k
    }
134
};
135
136
struct S3Conf {
137
    std::string bucket;
138
    std::string prefix;
139
    S3ClientConf client_conf;
140
141
    bool sse_enabled = false;
142
    static S3Conf get_s3_conf(const cloud::ObjectStoreInfoPB&);
143
    static S3Conf get_s3_conf(const TS3StorageParam&);
144
145
35
    std::string to_string() const {
146
35
        return fmt::format("(bucket={}, prefix={}, client_conf={}, sse_enabled={})", bucket, prefix,
147
35
                           client_conf.to_string(), sse_enabled);
148
35
    }
149
};
150
151
class S3ClientFactory {
152
public:
153
    ~S3ClientFactory();
154
155
    static S3ClientFactory& instance();
156
157
    std::shared_ptr<io::ObjStorageClient> create(const S3ClientConf& s3_conf);
158
159
    static Status convert_properties_to_s3_conf(const std::map<std::string, std::string>& prop,
160
                                                const S3URI& s3_uri, S3Conf* s3_conf);
161
162
57
    static Aws::Client::ClientConfiguration& getClientConfiguration() {
163
        // The default constructor of ClientConfiguration will do some http call
164
        // such as Aws::Internal::GetEC2MetadataClient and other init operation,
165
        // which is unnecessary.
166
        // So here we use a static instance, and deep copy every time
167
        // to avoid unnecessary operations.
168
57
        static Aws::Client::ClientConfiguration instance;
169
57
        instance.requestTimeoutMs = config::aws_client_request_timeout_ms;
170
57
        return instance;
171
57
    }
172
173
    std::shared_ptr<Aws::Auth::AWSCredentialsProvider> get_aws_credentials_provider(
174
            const S3ClientConf& s3_conf);
175
176
#ifdef BE_TEST
177
    void set_client_creator_for_test(
178
            std::function<std::shared_ptr<io::ObjStorageClient>(const S3ClientConf&)> creator);
179
180
    void clear_client_creator_for_test();
181
#endif
182
183
private:
184
    std::shared_ptr<io::ObjStorageClient> _create_s3_client(const S3ClientConf& s3_conf);
185
    std::shared_ptr<io::ObjStorageClient> _create_azure_client(const S3ClientConf& s3_conf);
186
    std::shared_ptr<Aws::Auth::AWSCredentialsProvider> _get_aws_credentials_provider_v1(
187
            const S3ClientConf& s3_conf);
188
    std::shared_ptr<Aws::Auth::AWSCredentialsProvider> _get_aws_credentials_provider_v2(
189
            const S3ClientConf& s3_conf);
190
    std::shared_ptr<Aws::Auth::AWSCredentialsProvider> _create_credentials_provider(
191
            CredProviderType type);
192
193
    S3ClientFactory();
194
195
    Aws::SDKOptions _aws_options;
196
    std::mutex _lock;
197
    std::unordered_map<S3ClientConf, std::shared_ptr<io::ObjStorageClient>, S3ClientConfHash>
198
            _cache;
199
    std::string _ca_cert_file_path;
200
#ifdef BE_TEST
201
    std::function<std::shared_ptr<io::ObjStorageClient>(const S3ClientConf&)> _test_client_creator;
202
#endif
203
};
204
205
} // end namespace doris