forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecycler_batch_delete_test.cpp
More file actions
403 lines (310 loc) · 13.8 KB
/
Copy pathrecycler_batch_delete_test.cpp
File metadata and controls
403 lines (310 loc) · 13.8 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// 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 <gtest/gtest.h>
#include <atomic>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "common/config.h"
#include "common/logging.h"
#include "common/simple_thread_pool.h"
#include "recycler/obj_storage_client.h"
using namespace doris;
namespace doris::cloud {
// Mock ObjectListIterator for testing
class MockObjectListIterator : public ObjectListIterator {
public:
MockObjectListIterator(std::vector<ObjectMeta> objects, int fail_after = -1)
: objects_(std::move(objects)), fail_after_(fail_after) {}
bool is_valid() override { return is_valid_; }
bool has_next() override {
if (!is_valid_) return false;
return current_index_ < objects_.size();
}
std::optional<ObjectMeta> next() override {
if (!is_valid_ || current_index_ >= objects_.size()) {
return std::nullopt;
}
// Simulate iterator becoming invalid after certain number of calls
if (fail_after_ >= 0 && static_cast<int>(current_index_) >= fail_after_) {
is_valid_ = false;
return std::nullopt;
}
return objects_[current_index_++];
}
void set_invalid() { is_valid_ = false; }
private:
std::vector<ObjectMeta> objects_;
size_t current_index_ = 0;
bool is_valid_ = true;
int fail_after_ = -1; // -1 means never fail
};
// Mock ObjStorageClient for testing delete_objects_recursively_
class MockObjStorageClient : public ObjStorageClient {
public:
MockObjStorageClient(std::vector<ObjectMeta> objects, int iterator_fail_after = -1)
: objects_(std::move(objects)), iterator_fail_after_(iterator_fail_after) {}
ObjectStorageResponse put_object(ObjectStoragePathRef path, std::string_view stream) override {
return {0};
}
ObjectStorageResponse head_object(ObjectStoragePathRef path, ObjectMeta* res) override {
return {0};
}
std::unique_ptr<ObjectListIterator> list_objects(ObjectStoragePathRef path) override {
return std::make_unique<MockObjectListIterator>(objects_, iterator_fail_after_);
}
ObjectStorageResponse delete_objects(const std::string& bucket, std::vector<std::string> keys,
ObjClientOptions option) override {
delete_calls_++;
total_keys_deleted_ += keys.size();
// Simulate delete failure if configured
if (fail_delete_after_ >= 0 && delete_calls_ > fail_delete_after_) {
return {-1, "simulated delete failure"};
}
return {0};
}
ObjectStorageResponse delete_object(ObjectStoragePathRef path) override { return {0}; }
ObjectStorageResponse delete_objects_recursively(ObjectStoragePathRef path,
ObjClientOptions option,
int64_t expiration_time = 0) override {
return delete_objects_recursively_(path, option, expiration_time, 1000);
}
ObjectStorageResponse get_life_cycle(const std::string& bucket,
int64_t* expiration_days) override {
return {0};
}
ObjectStorageResponse check_versioning(const std::string& bucket) override { return {0}; }
ObjectStorageResponse abort_multipart_upload(ObjectStoragePathRef path,
const std::string& upload_id) override {
return {0};
}
// Test helper methods
int get_delete_calls() const { return delete_calls_; }
size_t get_total_keys_deleted() const { return total_keys_deleted_; }
void set_fail_delete_after(int n) { fail_delete_after_ = n; }
private:
std::vector<ObjectMeta> objects_;
int iterator_fail_after_ = -1;
std::atomic<int> delete_calls_ {0};
std::atomic<size_t> total_keys_deleted_ {0};
int fail_delete_after_ = -1; // -1 means never fail
};
class RecyclerBatchDeleteTest : public testing::Test {
protected:
void SetUp() override {
thread_pool_ = std::make_shared<SimpleThreadPool>(4);
thread_pool_->start();
}
void TearDown() override {
if (thread_pool_) {
thread_pool_->stop();
}
}
std::vector<ObjectMeta> generate_objects(size_t count) {
std::vector<ObjectMeta> objects;
objects.reserve(count);
for (size_t i = 0; i < count; ++i) {
objects.push_back(ObjectMeta {
.key = "test_key_" + std::to_string(i),
.size = 100,
.mtime_s = 0,
});
}
return objects;
}
std::shared_ptr<SimpleThreadPool> thread_pool_;
};
// Test 1: Basic batch processing with multiple batches
TEST_F(RecyclerBatchDeleteTest, MultipleBatches) {
// Save original config and set small batch size for testing
int32_t original_config = config::recycler_max_tasks_per_batch;
config::recycler_max_tasks_per_batch = 3; // 3 tasks per batch
// Create 10 objects, with batch_size=2 (keys per task), max_tasks_per_batch=3
// Expected: 10 objects / 2 keys per task = 5 tasks
// 5 tasks / 3 tasks per batch = 2 batches (3 tasks + 2 tasks)
auto objects = generate_objects(10);
MockObjStorageClient client(objects);
ObjClientOptions options;
options.executor = thread_pool_;
// Use batch_size=2 to create more tasks
auto response = client.delete_objects_recursively_(
{.bucket = "test_bucket", .key = "test_prefix"}, options, 0, 2);
EXPECT_EQ(response.ret, 0);
EXPECT_EQ(client.get_delete_calls(), 5); // 10 objects / 2 = 5 delete calls
EXPECT_EQ(client.get_total_keys_deleted(), 10); // All 10 keys deleted
// Restore config
config::recycler_max_tasks_per_batch = original_config;
}
// Test 2: Iterator becomes invalid during iteration
TEST_F(RecyclerBatchDeleteTest, IteratorInvalidMidway) {
int32_t original_config = config::recycler_max_tasks_per_batch;
config::recycler_max_tasks_per_batch = 100;
// Create 20 objects but iterator fails after 10
auto objects = generate_objects(20);
MockObjStorageClient client(objects, 10); // fail_after=10
ObjClientOptions options;
options.executor = thread_pool_;
auto response = client.delete_objects_recursively_(
{.bucket = "test_bucket", .key = "test_prefix"}, options, 0, 5);
// Should return error because iterator became invalid
EXPECT_EQ(response.ret, -1);
// Should have processed some objects before failure
EXPECT_GT(client.get_total_keys_deleted(), 0);
EXPECT_LT(client.get_total_keys_deleted(), 20);
config::recycler_max_tasks_per_batch = original_config;
}
// Test 3: Delete operation fails (triggers cancel)
TEST_F(RecyclerBatchDeleteTest, DeleteFailureTriggersCancel) {
int32_t original_config = config::recycler_max_tasks_per_batch;
config::recycler_max_tasks_per_batch = 10;
auto objects = generate_objects(30);
MockObjStorageClient client(objects);
client.set_fail_delete_after(2); // Fail after 2 successful deletes
ObjClientOptions options;
options.executor = thread_pool_;
auto response = client.delete_objects_recursively_(
{.bucket = "test_bucket", .key = "test_prefix"}, options, 0, 5);
// Should return error because delete failed
EXPECT_EQ(response.ret, -1);
config::recycler_max_tasks_per_batch = original_config;
}
// Test 4: Empty object list
TEST_F(RecyclerBatchDeleteTest, EmptyObjectList) {
int32_t original_config = config::recycler_max_tasks_per_batch;
config::recycler_max_tasks_per_batch = 100;
std::vector<ObjectMeta> empty_objects;
MockObjStorageClient client(empty_objects);
ObjClientOptions options;
options.executor = thread_pool_;
auto response = client.delete_objects_recursively_(
{.bucket = "test_bucket", .key = "test_prefix"}, options, 0, 1000);
EXPECT_EQ(response.ret, 0);
EXPECT_EQ(client.get_delete_calls(), 0);
EXPECT_EQ(client.get_total_keys_deleted(), 0);
config::recycler_max_tasks_per_batch = original_config;
}
// Test 5: Objects less than batch_size
TEST_F(RecyclerBatchDeleteTest, ObjectsLessThanBatchSize) {
int32_t original_config = config::recycler_max_tasks_per_batch;
config::recycler_max_tasks_per_batch = 100;
auto objects = generate_objects(5);
MockObjStorageClient client(objects);
ObjClientOptions options;
options.executor = thread_pool_;
// batch_size=1000, but only 5 objects
auto response = client.delete_objects_recursively_(
{.bucket = "test_bucket", .key = "test_prefix"}, options, 0, 1000);
EXPECT_EQ(response.ret, 0);
EXPECT_EQ(client.get_delete_calls(), 1); // All 5 keys in one delete call
EXPECT_EQ(client.get_total_keys_deleted(), 5);
config::recycler_max_tasks_per_batch = original_config;
}
// Test 6: Exact batch boundary
TEST_F(RecyclerBatchDeleteTest, ExactBatchBoundary) {
int32_t original_config = config::recycler_max_tasks_per_batch;
config::recycler_max_tasks_per_batch = 2; // 2 tasks per batch
// 8 objects with batch_size=2 = 4 tasks
// 4 tasks with max_tasks_per_batch=2 = exactly 2 batches
auto objects = generate_objects(8);
MockObjStorageClient client(objects);
ObjClientOptions options;
options.executor = thread_pool_;
auto response = client.delete_objects_recursively_(
{.bucket = "test_bucket", .key = "test_prefix"}, options, 0, 2);
EXPECT_EQ(response.ret, 0);
EXPECT_EQ(client.get_delete_calls(), 4); // 8 / 2 = 4 tasks
EXPECT_EQ(client.get_total_keys_deleted(), 8);
config::recycler_max_tasks_per_batch = original_config;
}
// Test 7: Invalid config value (negative)
TEST_F(RecyclerBatchDeleteTest, InvalidConfigNegative) {
int32_t original_config = config::recycler_max_tasks_per_batch;
config::recycler_max_tasks_per_batch = -1; // Invalid negative value
auto objects = generate_objects(10);
MockObjStorageClient client(objects);
ObjClientOptions options;
options.executor = thread_pool_;
// Should use default value 1000 and still work
auto response = client.delete_objects_recursively_(
{.bucket = "test_bucket", .key = "test_prefix"}, options, 0, 5);
EXPECT_EQ(response.ret, 0);
EXPECT_EQ(client.get_total_keys_deleted(), 10);
config::recycler_max_tasks_per_batch = original_config;
}
// Test 8: Invalid config value (zero)
TEST_F(RecyclerBatchDeleteTest, InvalidConfigZero) {
int32_t original_config = config::recycler_max_tasks_per_batch;
config::recycler_max_tasks_per_batch = 0; // Invalid zero value
auto objects = generate_objects(10);
MockObjStorageClient client(objects);
ObjClientOptions options;
options.executor = thread_pool_;
// Should use default value 1000 and still work
auto response = client.delete_objects_recursively_(
{.bucket = "test_bucket", .key = "test_prefix"}, options, 0, 5);
EXPECT_EQ(response.ret, 0);
EXPECT_EQ(client.get_total_keys_deleted(), 10);
config::recycler_max_tasks_per_batch = original_config;
}
// Test 9: Expiration time filtering
TEST_F(RecyclerBatchDeleteTest, ExpirationTimeFiltering) {
int32_t original_config = config::recycler_max_tasks_per_batch;
config::recycler_max_tasks_per_batch = 100;
std::vector<ObjectMeta> objects;
// Create 10 objects: 5 with old mtime (should be deleted), 5 with new mtime (should be kept)
for (int i = 0; i < 5; ++i) {
objects.push_back(ObjectMeta {
.key = "old_key_" + std::to_string(i),
.size = 100,
.mtime_s = 100, // Old timestamp
});
}
for (int i = 0; i < 5; ++i) {
objects.push_back(ObjectMeta {
.key = "new_key_" + std::to_string(i),
.size = 100,
.mtime_s = 1000, // New timestamp
});
}
MockObjStorageClient client(objects);
ObjClientOptions options;
options.executor = thread_pool_;
// Set expiration_time=500, so only objects with mtime_s <= 500 should be deleted
auto response = client.delete_objects_recursively_(
{.bucket = "test_bucket", .key = "test_prefix"}, options, 500, 1000);
EXPECT_EQ(response.ret, 0);
EXPECT_EQ(client.get_total_keys_deleted(), 5); // Only old objects deleted
config::recycler_max_tasks_per_batch = original_config;
}
// Test 10: Iterator invalid at start (empty batch scenario)
TEST_F(RecyclerBatchDeleteTest, IteratorInvalidAtStart) {
int32_t original_config = config::recycler_max_tasks_per_batch;
config::recycler_max_tasks_per_batch = 100;
// Iterator fails immediately (fail_after=0)
auto objects = generate_objects(10);
MockObjStorageClient client(objects, 0);
ObjClientOptions options;
options.executor = thread_pool_;
auto response = client.delete_objects_recursively_(
{.bucket = "test_bucket", .key = "test_prefix"}, options, 0, 5);
// Should return error because iterator was invalid from the start
EXPECT_EQ(response.ret, -1);
EXPECT_EQ(client.get_delete_calls(), 0);
config::recycler_max_tasks_per_batch = original_config;
}
} // namespace doris::cloud