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/client/auth/aws_credential_factory.h" |
39 | | #include "cpp/client/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 | | std::string hide_access_key(const std::string& ak); |
53 | | |
54 | | class S3URI; |
55 | | struct S3ClientConf { |
56 | | std::string endpoint; |
57 | | std::string region; |
58 | | std::string ak; |
59 | | std::string sk; |
60 | | std::string token; |
61 | | // For azure we'd better support the bucket at the first time init azure blob container client |
62 | | std::string bucket; |
63 | | io::ObjStorageType provider = io::ObjStorageType::AWS; |
64 | | int max_connections = -1; |
65 | | int request_timeout_ms = -1; |
66 | | int connect_timeout_ms = -1; |
67 | | bool use_virtual_addressing = true; |
68 | | // For aws s3, no need to override endpoint |
69 | | bool need_override_endpoint = true; |
70 | | |
71 | | CredProviderType cred_provider_type = CredProviderType::Default; |
72 | | std::string role_arn; |
73 | | std::string external_id; |
74 | | // True when this client is bound to a Doris internal object storage bucket |
75 | | // (a storage vault in cloud mode). S3ClientFactory wraps such clients with the |
76 | | // shared rate limiter; external buckets (S3 load, TVF, external catalogs) are |
77 | | // returned bare in cloud mode. |
78 | | bool is_internal_bucket = false; |
79 | | |
80 | | // Full-field identity. get_hash() is only good for picking an unordered_map |
81 | | // bucket; distinct configurations can collide, so never treat hash equality as |
82 | | // configuration equality. |
83 | 34.2k | bool operator==(const S3ClientConf&) const = default; |
84 | | |
85 | 1.97k | uint64_t get_hash() const { |
86 | 1.97k | uint64_t hash_code = 0; |
87 | | // Use crc32_hash(ak + sk) hash to prevent swapped AK/SK order from producing same result. |
88 | 1.97k | hash_code ^= crc32_hash(ak + sk); |
89 | 1.97k | hash_code ^= crc32_hash(token); |
90 | 1.97k | hash_code ^= crc32_hash(endpoint); |
91 | 1.97k | hash_code ^= crc32_hash(region); |
92 | 1.97k | hash_code ^= crc32_hash(bucket); |
93 | 1.97k | hash_code ^= max_connections; |
94 | 1.97k | hash_code ^= request_timeout_ms; |
95 | 1.97k | hash_code ^= connect_timeout_ms; |
96 | 1.97k | hash_code ^= use_virtual_addressing; |
97 | 1.97k | hash_code ^= static_cast<int>(provider); |
98 | | |
99 | 1.97k | hash_code ^= static_cast<int>(cred_provider_type); |
100 | 1.97k | hash_code ^= crc32_hash(role_arn); |
101 | 1.97k | hash_code ^= crc32_hash(external_id); |
102 | 1.97k | hash_code ^= is_internal_bucket; |
103 | 1.97k | return hash_code; |
104 | 1.97k | } |
105 | | |
106 | 63 | std::string to_string() const { |
107 | 63 | return fmt::format( |
108 | 63 | "(ak={}, token={}, endpoint={}, region={}, bucket={}, max_connections={}, " |
109 | 63 | "request_timeout_ms={}, connect_timeout_ms={}, use_virtual_addressing={}, " |
110 | 63 | "cred_provider_type={},role_arn={}, external_id={}, is_internal_bucket={}", |
111 | 63 | hide_access_key(ak), token.empty() ? "" : "******", endpoint, region, bucket, |
112 | 63 | max_connections, request_timeout_ms, connect_timeout_ms, use_virtual_addressing, |
113 | 63 | cred_provider_type, role_arn, external_id, is_internal_bucket); |
114 | 63 | } |
115 | | }; |
116 | | |
117 | | struct S3ClientConfHash { |
118 | 1.97k | size_t operator()(const S3ClientConf& conf) const { |
119 | 1.97k | return static_cast<size_t>(conf.get_hash()); |
120 | 1.97k | } |
121 | | }; |
122 | | |
123 | | struct S3Conf { |
124 | | std::string bucket; |
125 | | std::string prefix; |
126 | | S3ClientConf client_conf; |
127 | | |
128 | | bool sse_enabled = false; |
129 | | static S3Conf get_s3_conf(const cloud::ObjectStoreInfoPB&); |
130 | | static S3Conf get_s3_conf(const TS3StorageParam&); |
131 | | |
132 | 35 | std::string to_string() const { |
133 | 35 | return fmt::format("(bucket={}, prefix={}, client_conf={}, sse_enabled={})", bucket, prefix, |
134 | 35 | client_conf.to_string(), sse_enabled); |
135 | 35 | } |
136 | | }; |
137 | | |
138 | | class S3ClientFactory { |
139 | | public: |
140 | | ~S3ClientFactory(); |
141 | | |
142 | | static S3ClientFactory& instance(); |
143 | | |
144 | | Result<std::shared_ptr<io::ObjStorageClient>> create(const S3ClientConf& s3_conf); |
145 | | |
146 | | static Status convert_properties_to_s3_conf(const std::map<std::string, std::string>& prop, |
147 | | const S3URI& s3_uri, S3Conf* s3_conf); |
148 | | |
149 | 84 | static Aws::Client::ClientConfiguration& getClientConfiguration() { |
150 | | // The default constructor of ClientConfiguration will do some http call |
151 | | // such as Aws::Internal::GetEC2MetadataClient and other init operation, |
152 | | // which is unnecessary. |
153 | | // So here we use a static instance, and deep copy every time |
154 | | // to avoid unnecessary operations. |
155 | 84 | static Aws::Client::ClientConfiguration instance; |
156 | 84 | instance.requestTimeoutMs = config::aws_client_request_timeout_ms; |
157 | 84 | return instance; |
158 | 84 | } |
159 | | |
160 | | AwsCredentialResult create_aws_credentials_provider(const S3ClientConf& s3_conf); |
161 | | |
162 | | #ifdef BE_TEST |
163 | | void set_client_creator_for_test( |
164 | | std::function<std::shared_ptr<io::ObjStorageClient>(const S3ClientConf&)> creator); |
165 | | |
166 | | void clear_client_creator_for_test(); |
167 | | #endif |
168 | | |
169 | | private: |
170 | | Result<std::shared_ptr<io::ObjStorageBackend>> _create_s3_backend(const S3ClientConf& s3_conf); |
171 | | Result<std::shared_ptr<io::ObjStorageBackend>> _create_azure_backend( |
172 | | const S3ClientConf& s3_conf); |
173 | | S3ClientFactory(); |
174 | | |
175 | | Aws::SDKOptions _aws_options; |
176 | | std::mutex _lock; |
177 | | std::unordered_map<S3ClientConf, std::shared_ptr<io::ObjStorageClient>, S3ClientConfHash> |
178 | | _cache; |
179 | | std::string _ca_cert_file_path; |
180 | | #ifdef BE_TEST |
181 | | std::function<std::shared_ptr<io::ObjStorageClient>(const S3ClientConf&)> _test_client_creator; |
182 | | #endif |
183 | | }; |
184 | | |
185 | | } // end namespace doris |