Coverage Report

Created: 2026-10-22 05:48

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