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 | | int reset_s3_rate_limiter(S3RateLimitType type, size_t max_speed, size_t max_burst, size_t limit); |
67 | | |
68 | | bool acquire_s3_qps_rate_limit(S3RateLimitType type); |
69 | | bool acquire_s3_qps_rate_limit(bool is_internal_bucket, S3RateLimitType type); |
70 | | bool acquire_s3_bytes_rate_limit(S3RateLimitType type, size_t bytes); |
71 | | bool acquire_s3_bytes_rate_limit(bool is_internal_bucket, S3RateLimitType type, size_t bytes); |
72 | | bool acquire_s3_rate_limit(S3RateLimitType type, size_t bytes); |
73 | | bool acquire_s3_rate_limit(bool is_internal_bucket, S3RateLimitType type, size_t bytes); |
74 | | // Refresh GET/PUT QPS and bytes limiters from the current configs and startup CPU count. |
75 | | void refresh_s3_rate_limiters(); |
76 | | |
77 | | struct S3RateLimiterConfig { |
78 | | int64_t token_per_second = 0; |
79 | | int64_t bucket_tokens = 0; |
80 | | int64_t token_limit = 0; |
81 | | }; |
82 | | |
83 | | #ifdef BE_TEST |
84 | | void set_s3_rate_limiter_cpu_cores_for_test(int64_t cpu_cores); |
85 | | void reset_s3_rate_limiters_from_config_for_test(); |
86 | | S3RateLimiterConfig get_effective_s3_rate_limiter_config(S3RateLimitType type); |
87 | | S3RateLimiterConfig get_effective_s3_bytes_rate_limiter_config(S3RateLimitType type); |
88 | | #endif |
89 | | |
90 | | class S3URI; |
91 | | struct S3ClientConf { |
92 | | std::string endpoint; |
93 | | std::string region; |
94 | | std::string ak; |
95 | | std::string sk; |
96 | | std::string token; |
97 | | // For azure we'd better support the bucket at the first time init azure blob container client |
98 | | std::string bucket; |
99 | | io::ObjStorageType provider = io::ObjStorageType::AWS; |
100 | | int max_connections = -1; |
101 | | int request_timeout_ms = -1; |
102 | | int connect_timeout_ms = -1; |
103 | | bool use_virtual_addressing = true; |
104 | | // For aws s3, no need to override endpoint |
105 | | bool need_override_endpoint = true; |
106 | | |
107 | | CredProviderType cred_provider_type = CredProviderType::Default; |
108 | | std::string role_arn; |
109 | | std::string external_id; |
110 | | // Whether the client is bound to a Doris internal object storage bucket in cloud mode. |
111 | | bool is_internal_bucket = false; |
112 | | |
113 | 659k | bool operator==(const S3ClientConf&) const = default; |
114 | | |
115 | 38.6k | uint64_t get_hash() const { |
116 | 38.6k | uint64_t hash_code = 0; |
117 | | // Use crc32_hash(ak + sk) hash to prevent swapped AK/SK order from producing same result. |
118 | 38.6k | hash_code ^= crc32_hash(ak + sk); |
119 | 38.6k | hash_code ^= crc32_hash(token); |
120 | 38.6k | hash_code ^= crc32_hash(endpoint); |
121 | 38.6k | hash_code ^= crc32_hash(region); |
122 | 38.6k | hash_code ^= crc32_hash(bucket); |
123 | 38.6k | hash_code ^= max_connections; |
124 | 38.6k | hash_code ^= request_timeout_ms; |
125 | 38.6k | hash_code ^= connect_timeout_ms; |
126 | 38.6k | hash_code ^= use_virtual_addressing; |
127 | 38.6k | hash_code ^= static_cast<int>(provider); |
128 | | |
129 | 38.6k | hash_code ^= static_cast<int>(cred_provider_type); |
130 | 38.6k | hash_code ^= crc32_hash(role_arn); |
131 | 38.6k | hash_code ^= crc32_hash(external_id); |
132 | 38.6k | hash_code ^= is_internal_bucket; |
133 | 38.6k | return hash_code; |
134 | 38.6k | } |
135 | | |
136 | 122 | std::string to_string() const { |
137 | 122 | return fmt::format( |
138 | 122 | "(ak={}, token={}, endpoint={}, region={}, bucket={}, max_connections={}, " |
139 | 122 | "request_timeout_ms={}, connect_timeout_ms={}, use_virtual_addressing={}, " |
140 | 122 | "cred_provider_type={},role_arn={}, external_id={}, is_internal_bucket={}", |
141 | 122 | hide_access_key(ak), token, endpoint, region, bucket, max_connections, |
142 | 122 | request_timeout_ms, connect_timeout_ms, use_virtual_addressing, cred_provider_type, |
143 | 122 | role_arn, external_id, is_internal_bucket); |
144 | 122 | } |
145 | | }; |
146 | | |
147 | | struct S3ClientConfHash { |
148 | 38.6k | size_t operator()(const S3ClientConf& conf) const { |
149 | 38.6k | return static_cast<size_t>(conf.get_hash()); |
150 | 38.6k | } |
151 | | }; |
152 | | |
153 | | struct S3Conf { |
154 | | std::string bucket; |
155 | | std::string prefix; |
156 | | S3ClientConf client_conf; |
157 | | |
158 | | bool sse_enabled = false; |
159 | | static S3Conf get_s3_conf(const cloud::ObjectStoreInfoPB&); |
160 | | static S3Conf get_s3_conf(const TS3StorageParam&); |
161 | | |
162 | 36 | std::string to_string() const { |
163 | 36 | return fmt::format("(bucket={}, prefix={}, client_conf={}, sse_enabled={})", bucket, prefix, |
164 | 36 | client_conf.to_string(), sse_enabled); |
165 | 36 | } |
166 | | }; |
167 | | |
168 | | class S3ClientFactory { |
169 | | public: |
170 | | ~S3ClientFactory(); |
171 | | |
172 | | static S3ClientFactory& instance(); |
173 | | |
174 | | std::shared_ptr<io::ObjStorageClient> create(const S3ClientConf& s3_conf); |
175 | | |
176 | | static Status convert_properties_to_s3_conf(const std::map<std::string, std::string>& prop, |
177 | | const S3URI& s3_uri, S3Conf* s3_conf); |
178 | | |
179 | 86 | static Aws::Client::ClientConfiguration& getClientConfiguration() { |
180 | | // The default constructor of ClientConfiguration will do some http call |
181 | | // such as Aws::Internal::GetEC2MetadataClient and other init operation, |
182 | | // which is unnecessary. |
183 | | // So here we use a static instance, and deep copy every time |
184 | | // to avoid unnecessary operations. |
185 | 86 | static Aws::Client::ClientConfiguration instance; |
186 | 86 | instance.requestTimeoutMs = config::aws_client_request_timeout_ms; |
187 | 86 | return instance; |
188 | 86 | } |
189 | | |
190 | | S3RateLimiterHolder* rate_limiter(S3RateLimitType type); |
191 | | S3RateLimiterHolder* bytes_rate_limiter(S3RateLimitType type); |
192 | | |
193 | | std::shared_ptr<Aws::Auth::AWSCredentialsProvider> get_aws_credentials_provider( |
194 | | const S3ClientConf& s3_conf); |
195 | | |
196 | | #ifdef BE_TEST |
197 | | void set_client_creator_for_test( |
198 | | std::function<std::shared_ptr<io::ObjStorageClient>(const S3ClientConf&)> creator); |
199 | | |
200 | | void clear_client_creator_for_test(); |
201 | | #endif |
202 | | |
203 | | private: |
204 | | std::shared_ptr<io::ObjStorageClient> _create_s3_client(const S3ClientConf& s3_conf); |
205 | | std::shared_ptr<io::ObjStorageClient> _create_azure_client(const S3ClientConf& s3_conf); |
206 | | std::shared_ptr<Aws::Auth::AWSCredentialsProvider> _get_aws_credentials_provider_v1( |
207 | | const S3ClientConf& s3_conf); |
208 | | std::shared_ptr<Aws::Auth::AWSCredentialsProvider> _get_aws_credentials_provider_v2( |
209 | | const S3ClientConf& s3_conf); |
210 | | std::shared_ptr<Aws::Auth::AWSCredentialsProvider> _create_credentials_provider( |
211 | | CredProviderType type); |
212 | | |
213 | | S3ClientFactory(); |
214 | | |
215 | | Aws::SDKOptions _aws_options; |
216 | | std::mutex _lock; |
217 | | std::unordered_map<S3ClientConf, std::shared_ptr<io::ObjStorageClient>, S3ClientConfHash> |
218 | | _cache; |
219 | | std::string _ca_cert_file_path; |
220 | | std::array<std::unique_ptr<S3RateLimiterHolder>, 2> _rate_limiters; |
221 | | std::array<std::unique_ptr<S3RateLimiterHolder>, 2> _bytes_rate_limiters; |
222 | | #ifdef BE_TEST |
223 | | std::function<std::shared_ptr<io::ObjStorageClient>(const S3ClientConf&)> _test_client_creator; |
224 | | #endif |
225 | | }; |
226 | | |
227 | | } // end namespace doris |