forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3_obj_client.cpp
More file actions
379 lines (328 loc) · 14.2 KB
/
Copy paths3_obj_client.cpp
File metadata and controls
379 lines (328 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "recycler/s3_obj_client.h"
#include <aws/s3/S3Client.h>
#include <aws/s3/model/DeleteObjectRequest.h>
#include <aws/s3/model/DeleteObjectsRequest.h>
#include <aws/s3/model/GetBucketLifecycleConfigurationRequest.h>
#include <aws/s3/model/GetBucketVersioningRequest.h>
#include <aws/s3/model/HeadObjectRequest.h>
#include <aws/s3/model/ListObjectsV2Request.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <ranges>
#include "common/config.h"
#include "common/logging.h"
#include "common/stopwatch.h"
#include "cpp/s3_rate_limiter.h"
#include "cpp/sync_point.h"
#include "recycler/s3_accessor.h"
#include "recycler/util.h"
namespace doris::cloud {
[[maybe_unused]] static Aws::Client::AWSError<Aws::S3::S3Errors> s3_error_factory() {
return {Aws::S3::S3Errors::INTERNAL_FAILURE, "exceeds limit", "exceeds limit", false};
}
template <typename Func>
auto s3_rate_limit(S3RateLimitType op, Func callback) -> decltype(callback()) {
using T = decltype(callback());
if (!config::enable_s3_rate_limiter) {
return callback();
}
auto sleep_duration = AccessorRateLimiter::instance().rate_limiter(op)->add(1);
if (sleep_duration < 0) {
return T(s3_error_factory());
}
return callback();
}
template <typename Func>
auto s3_get_rate_limit(Func callback) -> decltype(callback()) {
return s3_rate_limit(S3RateLimitType::GET, std::move(callback));
}
template <typename Func>
auto s3_put_rate_limit(Func callback) -> decltype(callback()) {
return s3_rate_limit(S3RateLimitType::PUT, std::move(callback));
}
class S3ObjListIterator final : public ObjectListIterator {
public:
S3ObjListIterator(std::shared_ptr<Aws::S3::S3Client> client, std::string bucket,
std::string prefix, std::string endpoint)
: client_(std::move(client)), endpoint_(std::move(endpoint)) {
req_.WithBucket(std::move(bucket)).WithPrefix(std::move(prefix));
TEST_SYNC_POINT_CALLBACK("S3ObjListIterator", &req_);
}
~S3ObjListIterator() override = default;
bool is_valid() override { return is_valid_; }
bool has_next() override {
if (!is_valid_) {
return false;
}
if (!results_.empty()) {
return true;
}
if (!has_more_) {
return false;
}
auto outcome = s3_get_rate_limit([&]() {
SCOPED_BVAR_LATENCY(s3_bvar::s3_list_latency);
return client_->ListObjectsV2(req_);
});
if (!outcome.IsSuccess()) {
LOG_WARNING("failed to list objects")
.tag("endpoint", endpoint_)
.tag("bucket", req_.GetBucket())
.tag("prefix", req_.GetPrefix())
.tag("responseCode", static_cast<int>(outcome.GetError().GetResponseCode()))
.tag("error", outcome.GetError().GetMessage());
is_valid_ = false;
return false;
}
if (outcome.GetResult().GetIsTruncated() &&
outcome.GetResult().GetNextContinuationToken().empty()) {
LOG_WARNING("failed to list objects, isTruncated but no continuation token")
.tag("endpoint", endpoint_)
.tag("bucket", req_.GetBucket())
.tag("prefix", req_.GetPrefix());
is_valid_ = false;
return false;
}
has_more_ = outcome.GetResult().GetIsTruncated();
req_.SetContinuationToken(std::move(
const_cast<std::string&&>(outcome.GetResult().GetNextContinuationToken())));
auto&& content = outcome.GetResult().GetContents();
DCHECK(!(has_more_ && content.empty())) << has_more_ << ' ' << content.empty();
results_.reserve(content.size());
for (auto&& obj : std::ranges::reverse_view(content)) {
DCHECK(obj.GetKey().starts_with(req_.GetPrefix()))
<< obj.GetKey() << ' ' << req_.GetPrefix();
results_.emplace_back(
ObjectMeta {.key = std::move(const_cast<std::string&&>(obj.GetKey())),
.size = obj.GetSize(),
.mtime_s = obj.GetLastModified().Seconds()});
}
return !results_.empty();
}
std::optional<ObjectMeta> next() override {
std::optional<ObjectMeta> res;
if (!has_next()) {
return res;
}
res = std::move(results_.back());
results_.pop_back();
return res;
}
private:
std::shared_ptr<Aws::S3::S3Client> client_;
Aws::S3::Model::ListObjectsV2Request req_;
std::vector<ObjectMeta> results_;
std::string endpoint_;
bool is_valid_ {true};
bool has_more_ {true};
};
static constexpr size_t MaxDeleteBatch = 1000;
S3ObjClient::~S3ObjClient() = default;
ObjectStorageResponse S3ObjClient::put_object(ObjectStoragePathRef path, std::string_view stream) {
Aws::S3::Model::PutObjectRequest request;
request.WithBucket(path.bucket).WithKey(path.key);
auto input = Aws::MakeShared<Aws::StringStream>("S3Accessor");
*input << stream;
request.SetBody(input);
auto outcome = s3_put_rate_limit([&]() {
SCOPED_BVAR_LATENCY(s3_bvar::s3_put_latency);
return s3_client_->PutObject(request);
});
if (!outcome.IsSuccess()) {
LOG_WARNING("failed to put object")
.tag("endpoint", endpoint_)
.tag("bucket", path.bucket)
.tag("key", path.key)
.tag("responseCode", static_cast<int>(outcome.GetError().GetResponseCode()))
.tag("error", outcome.GetError().GetMessage());
return -1;
}
return 0;
}
ObjectStorageResponse S3ObjClient::head_object(ObjectStoragePathRef path, ObjectMeta* res) {
Aws::S3::Model::HeadObjectRequest request;
request.WithBucket(path.bucket).WithKey(path.key);
auto outcome = s3_get_rate_limit([&]() {
SCOPED_BVAR_LATENCY(s3_bvar::s3_head_latency);
return s3_client_->HeadObject(request);
});
if (outcome.IsSuccess()) {
res->key = path.key;
res->size = outcome.GetResult().GetContentLength();
res->mtime_s = outcome.GetResult().GetLastModified().Seconds();
return 0;
} else if (outcome.GetError().GetResponseCode() == Aws::Http::HttpResponseCode::NOT_FOUND) {
return 1;
} else {
LOG_WARNING("failed to head object")
.tag("endpoint", endpoint_)
.tag("bucket", path.bucket)
.tag("key", path.key)
.tag("responseCode", static_cast<int>(outcome.GetError().GetResponseCode()))
.tag("error", outcome.GetError().GetMessage());
return -1;
}
}
std::unique_ptr<ObjectListIterator> S3ObjClient::list_objects(ObjectStoragePathRef path) {
return std::make_unique<S3ObjListIterator>(s3_client_, path.bucket, path.key, endpoint_);
}
ObjectStorageResponse S3ObjClient::delete_objects(const std::string& bucket,
std::vector<std::string> keys,
ObjClientOptions option) {
if (keys.empty()) {
return {0};
}
Aws::S3::Model::DeleteObjectsRequest delete_request;
delete_request.SetBucket(bucket);
auto issue_delete = [&bucket, &delete_request,
this](std::vector<Aws::S3::Model::ObjectIdentifier> objects) -> int {
if (objects.size() == 1) {
return delete_object({.bucket = bucket, .key = objects[0].GetKey()}).ret;
}
Aws::S3::Model::Delete del;
del.WithObjects(std::move(objects)).SetQuiet(true);
delete_request.SetDelete(std::move(del));
auto delete_outcome = s3_put_rate_limit([&]() {
SCOPED_BVAR_LATENCY(s3_bvar::s3_delete_objects_latency);
return s3_client_->DeleteObjects(delete_request);
});
if (!delete_outcome.IsSuccess()) {
LOG_WARNING("failed to delete objects")
.tag("endpoint", endpoint_)
.tag("bucket", bucket)
.tag("key[0]", delete_request.GetDelete().GetObjects().front().GetKey())
.tag("responseCode",
static_cast<int>(delete_outcome.GetError().GetResponseCode()))
.tag("error", delete_outcome.GetError().GetMessage());
return -1;
}
if (!delete_outcome.IsSuccess()) {
LOG_WARNING("failed to delete objects")
.tag("endpoint", endpoint_)
.tag("bucket", bucket)
.tag("key[0]", delete_request.GetDelete().GetObjects().front().GetKey())
.tag("responseCode",
static_cast<int>(delete_outcome.GetError().GetResponseCode()))
.tag("error", delete_outcome.GetError().GetMessage());
return -1;
}
return 0;
};
int ret = 0;
// `DeleteObjectsRequest` can only contain 1000 keys at most.
std::vector<Aws::S3::Model::ObjectIdentifier> objects;
size_t delete_batch_size = MaxDeleteBatch;
TEST_INJECTION_POINT_CALLBACK("S3ObjClient::delete_objects", &delete_batch_size);
// std::views::chunk(1000)
for (auto&& key : keys) {
objects.emplace_back().SetKey(std::move(key));
if (objects.size() < delete_batch_size) {
continue;
}
ret = issue_delete(std::move(objects));
if (ret != 0) {
return {ret};
}
}
if (!objects.empty()) {
ret = issue_delete(std::move(objects));
}
return {ret};
}
ObjectStorageResponse S3ObjClient::delete_object(ObjectStoragePathRef path) {
Aws::S3::Model::DeleteObjectRequest request;
request.WithBucket(path.bucket).WithKey(path.key);
auto outcome = s3_put_rate_limit([&]() {
SCOPED_BVAR_LATENCY(s3_bvar::s3_delete_object_latency);
return s3_client_->DeleteObject(request);
});
TEST_SYNC_POINT_CALLBACK("S3ObjClient::delete_object", &outcome);
if (!outcome.IsSuccess()) {
LOG_WARNING("failed to delete object")
.tag("endpoint", endpoint_)
.tag("bucket", path.bucket)
.tag("key", path.key)
.tag("responseCode", static_cast<int>(outcome.GetError().GetResponseCode()))
.tag("error", outcome.GetError().GetMessage())
.tag("exception", outcome.GetError().GetExceptionName());
if (outcome.GetError().GetResponseCode() == Aws::Http::HttpResponseCode::NOT_FOUND) {
return {ObjectStorageResponse::NOT_FOUND, outcome.GetError().GetMessage()};
}
return {ObjectStorageResponse::UNDEFINED, outcome.GetError().GetMessage()};
}
return {ObjectStorageResponse::OK};
}
ObjectStorageResponse S3ObjClient::delete_objects_recursively(ObjectStoragePathRef path,
ObjClientOptions option,
int64_t expiration_time) {
return delete_objects_recursively_(path, option, expiration_time, MaxDeleteBatch);
}
ObjectStorageResponse S3ObjClient::get_life_cycle(const std::string& bucket,
int64_t* expiration_days) {
Aws::S3::Model::GetBucketLifecycleConfigurationRequest request;
request.SetBucket(bucket);
auto outcome = s3_get_rate_limit(
[&]() { return s3_client_->GetBucketLifecycleConfiguration(request); });
bool has_lifecycle = false;
if (outcome.IsSuccess()) {
const auto& rules = outcome.GetResult().GetRules();
for (const auto& rule : rules) {
if (rule.NoncurrentVersionExpirationHasBeenSet()) {
has_lifecycle = true;
*expiration_days = rule.GetNoncurrentVersionExpiration().GetNoncurrentDays();
}
}
} else {
LOG_WARNING("Err for check interval: failed to get bucket lifecycle")
.tag("endpoint", endpoint_)
.tag("bucket", bucket)
.tag("responseCode", static_cast<int>(outcome.GetError().GetResponseCode()))
.tag("error", outcome.GetError().GetMessage());
return -1;
}
if (!has_lifecycle) {
LOG_WARNING("Err for check interval: bucket doesn't have lifecycle configuration")
.tag("endpoint", endpoint_)
.tag("bucket", bucket);
return -1;
}
return 0;
}
ObjectStorageResponse S3ObjClient::check_versioning(const std::string& bucket) {
Aws::S3::Model::GetBucketVersioningRequest request;
request.SetBucket(bucket);
auto outcome = s3_get_rate_limit([&]() { return s3_client_->GetBucketVersioning(request); });
if (outcome.IsSuccess()) {
const auto& versioning_configuration = outcome.GetResult().GetStatus();
if (versioning_configuration != Aws::S3::Model::BucketVersioningStatus::Enabled) {
LOG_WARNING("Err for check interval: bucket doesn't enable bucket versioning")
.tag("endpoint", endpoint_)
.tag("bucket", bucket);
return -1;
}
} else {
LOG_WARNING("Err for check interval: failed to get status of bucket versioning")
.tag("endpoint", endpoint_)
.tag("bucket", bucket)
.tag("responseCode", static_cast<int>(outcome.GetError().GetResponseCode()))
.tag("error", outcome.GetError().GetMessage());
return -1;
}
return 0;
}
} // namespace doris::cloud