1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
9 #include "rgw_bucket.h"
10 #include "rgw_reshard.h"
12 #include "cls/rgw/cls_rgw_client.h"
13 #include "cls/lock/cls_lock_client.h"
14 #include "common/errno.h"
15 #include "common/ceph_json.h"
17 #include "common/dout.h"
19 #include "services/svc_zone.h"
20 #include "services/svc_sys_obj.h"
21 #include "services/svc_tier_rados.h"
23 #define dout_context g_ceph_context
24 #define dout_subsys ceph_subsys_rgw
26 const string reshard_oid_prefix
= "reshard.";
27 const string reshard_lock_name
= "reshard_process";
28 const string bucket_instance_lock_name
= "bucket_instance_lock";
30 /* All primes up to 2000 used to attempt to make dynamic sharding use
31 * a prime numbers of shards. Note: this list also includes 1 for when
32 * 1 shard is the most appropriate, even though 1 is not prime.
34 const std::initializer_list
<uint16_t> RGWBucketReshard::reshard_primes
= {
35 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,
36 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137,
37 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211,
38 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283,
39 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379,
40 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461,
41 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563,
42 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643,
43 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739,
44 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829,
45 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937,
46 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021,
47 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093,
48 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181,
49 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259,
50 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321,
51 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433,
52 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493,
53 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579,
54 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657,
55 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741,
56 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831,
57 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913,
58 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999
61 class BucketReshardShard
{
62 rgw::sal::RGWRadosStore
*store
;
63 const RGWBucketInfo
& bucket_info
;
65 RGWRados::BucketShard bs
;
66 vector
<rgw_cls_bi_entry
> entries
;
67 map
<RGWObjCategory
, rgw_bucket_category_stats
> stats
;
68 deque
<librados::AioCompletion
*>& aio_completions
;
69 uint64_t max_aio_completions
;
70 uint64_t reshard_shard_batch_size
;
72 int wait_next_completion() {
73 librados::AioCompletion
*c
= aio_completions
.front();
74 aio_completions
.pop_front();
76 c
->wait_for_complete();
78 int ret
= c
->get_return_value();
82 derr
<< "ERROR: reshard rados operation failed: " << cpp_strerror(-ret
) << dendl
;
89 int get_completion(librados::AioCompletion
**c
) {
90 if (aio_completions
.size() >= max_aio_completions
) {
91 int ret
= wait_next_completion();
97 *c
= librados::Rados::aio_create_completion(nullptr, nullptr);
98 aio_completions
.push_back(*c
);
104 BucketReshardShard(rgw::sal::RGWRadosStore
*_store
, const RGWBucketInfo
& _bucket_info
,
106 deque
<librados::AioCompletion
*>& _completions
) :
107 store(_store
), bucket_info(_bucket_info
), bs(store
->getRados()),
108 aio_completions(_completions
)
110 num_shard
= (bucket_info
.num_shards
> 0 ? _num_shard
: -1);
111 bs
.init(bucket_info
.bucket
, num_shard
, nullptr /* no RGWBucketInfo */);
113 max_aio_completions
=
114 store
->ctx()->_conf
.get_val
<uint64_t>("rgw_reshard_max_aio");
115 reshard_shard_batch_size
=
116 store
->ctx()->_conf
.get_val
<uint64_t>("rgw_reshard_batch_size");
119 int get_num_shard() {
123 int add_entry(rgw_cls_bi_entry
& entry
, bool account
, RGWObjCategory category
,
124 const rgw_bucket_category_stats
& entry_stats
) {
125 entries
.push_back(entry
);
127 rgw_bucket_category_stats
& target
= stats
[category
];
128 target
.num_entries
+= entry_stats
.num_entries
;
129 target
.total_size
+= entry_stats
.total_size
;
130 target
.total_size_rounded
+= entry_stats
.total_size_rounded
;
131 target
.actual_size
+= entry_stats
.actual_size
;
133 if (entries
.size() >= reshard_shard_batch_size
) {
144 if (entries
.size() == 0) {
148 librados::ObjectWriteOperation op
;
149 for (auto& entry
: entries
) {
150 store
->getRados()->bi_put(op
, bs
, entry
);
152 cls_rgw_bucket_update_stats(op
, false, stats
);
154 librados::AioCompletion
*c
;
155 int ret
= get_completion(&c
);
159 ret
= bs
.bucket_obj
.aio_operate(c
, &op
);
161 derr
<< "ERROR: failed to store entries in target bucket shard (bs=" << bs
.bucket
<< "/" << bs
.shard_id
<< ") error=" << cpp_strerror(-ret
) << dendl
;
171 while (!aio_completions
.empty()) {
172 int r
= wait_next_completion();
179 }; // class BucketReshardShard
182 class BucketReshardManager
{
183 rgw::sal::RGWRadosStore
*store
;
184 const RGWBucketInfo
& target_bucket_info
;
185 deque
<librados::AioCompletion
*> completions
;
186 int num_target_shards
;
187 vector
<BucketReshardShard
*> target_shards
;
190 BucketReshardManager(rgw::sal::RGWRadosStore
*_store
,
191 const RGWBucketInfo
& _target_bucket_info
,
192 int _num_target_shards
) :
193 store(_store
), target_bucket_info(_target_bucket_info
),
194 num_target_shards(_num_target_shards
)
196 target_shards
.resize(num_target_shards
);
197 for (int i
= 0; i
< num_target_shards
; ++i
) {
198 target_shards
[i
] = new BucketReshardShard(store
, target_bucket_info
, i
, completions
);
202 ~BucketReshardManager() {
203 for (auto& shard
: target_shards
) {
204 int ret
= shard
->wait_all_aio();
206 ldout(store
->ctx(), 20) << __func__
<<
207 ": shard->wait_all_aio() returned ret=" << ret
<< dendl
;
212 int add_entry(int shard_index
,
213 rgw_cls_bi_entry
& entry
, bool account
, RGWObjCategory category
,
214 const rgw_bucket_category_stats
& entry_stats
) {
215 int ret
= target_shards
[shard_index
]->add_entry(entry
, account
, category
,
218 derr
<< "ERROR: target_shards.add_entry(" << entry
.idx
<<
219 ") returned error: " << cpp_strerror(-ret
) << dendl
;
228 for (auto& shard
: target_shards
) {
229 int r
= shard
->flush();
231 derr
<< "ERROR: target_shards[" << shard
->get_num_shard() << "].flush() returned error: " << cpp_strerror(-r
) << dendl
;
235 for (auto& shard
: target_shards
) {
236 int r
= shard
->wait_all_aio();
238 derr
<< "ERROR: target_shards[" << shard
->get_num_shard() << "].wait_all_aio() returned error: " << cpp_strerror(-r
) << dendl
;
243 target_shards
.clear();
246 }; // class BucketReshardManager
248 RGWBucketReshard::RGWBucketReshard(rgw::sal::RGWRadosStore
*_store
,
249 const RGWBucketInfo
& _bucket_info
,
250 const map
<string
, bufferlist
>& _bucket_attrs
,
251 RGWBucketReshardLock
* _outer_reshard_lock
) :
252 store(_store
), bucket_info(_bucket_info
), bucket_attrs(_bucket_attrs
),
253 reshard_lock(store
, bucket_info
, true),
254 outer_reshard_lock(_outer_reshard_lock
)
257 int RGWBucketReshard::set_resharding_status(rgw::sal::RGWRadosStore
* store
,
258 const RGWBucketInfo
& bucket_info
,
259 const string
& new_instance_id
,
261 cls_rgw_reshard_status status
)
263 if (new_instance_id
.empty()) {
264 ldout(store
->ctx(), 0) << __func__
<< " missing new bucket instance id" << dendl
;
268 cls_rgw_bucket_instance_entry instance_entry
;
269 instance_entry
.set_status(new_instance_id
, num_shards
, status
);
271 int ret
= store
->getRados()->bucket_set_reshard(bucket_info
, instance_entry
);
273 ldout(store
->ctx(), 0) << "RGWReshard::" << __func__
<< " ERROR: error setting bucket resharding flag on bucket index: "
274 << cpp_strerror(-ret
) << dendl
;
280 // reshard lock assumes lock is held
281 int RGWBucketReshard::clear_resharding(rgw::sal::RGWRadosStore
* store
,
282 const RGWBucketInfo
& bucket_info
)
284 int ret
= clear_index_shard_reshard_status(store
, bucket_info
);
286 ldout(store
->ctx(), 0) << "RGWBucketReshard::" << __func__
<<
287 " ERROR: error clearing reshard status from index shard " <<
288 cpp_strerror(-ret
) << dendl
;
292 cls_rgw_bucket_instance_entry instance_entry
;
293 ret
= store
->getRados()->bucket_set_reshard(bucket_info
, instance_entry
);
295 ldout(store
->ctx(), 0) << "RGWReshard::" << __func__
<<
296 " ERROR: error setting bucket resharding flag on bucket index: " <<
297 cpp_strerror(-ret
) << dendl
;
304 int RGWBucketReshard::clear_index_shard_reshard_status(rgw::sal::RGWRadosStore
* store
,
305 const RGWBucketInfo
& bucket_info
)
307 uint32_t num_shards
= bucket_info
.num_shards
;
309 if (num_shards
< std::numeric_limits
<uint32_t>::max()) {
310 int ret
= set_resharding_status(store
, bucket_info
,
311 bucket_info
.bucket
.bucket_id
,
312 (num_shards
< 1 ? 1 : num_shards
),
313 cls_rgw_reshard_status::NOT_RESHARDING
);
315 ldout(store
->ctx(), 0) << "RGWBucketReshard::" << __func__
<<
316 " ERROR: error clearing reshard status from index shard " <<
317 cpp_strerror(-ret
) << dendl
;
325 static int create_new_bucket_instance(rgw::sal::RGWRadosStore
*store
,
327 const RGWBucketInfo
& bucket_info
,
328 map
<string
, bufferlist
>& attrs
,
329 RGWBucketInfo
& new_bucket_info
)
331 new_bucket_info
= bucket_info
;
333 store
->getRados()->create_bucket_id(&new_bucket_info
.bucket
.bucket_id
);
335 new_bucket_info
.num_shards
= new_num_shards
;
336 new_bucket_info
.objv_tracker
.clear();
338 new_bucket_info
.new_bucket_instance_id
.clear();
339 new_bucket_info
.reshard_status
= cls_rgw_reshard_status::NOT_RESHARDING
;
341 int ret
= store
->svc()->bi
->init_index(new_bucket_info
);
343 cerr
<< "ERROR: failed to init new bucket indexes: " << cpp_strerror(-ret
) << std::endl
;
347 ret
= store
->getRados()->put_bucket_instance_info(new_bucket_info
, true, real_time(), &attrs
);
349 cerr
<< "ERROR: failed to store new bucket instance info: " << cpp_strerror(-ret
) << std::endl
;
356 int RGWBucketReshard::create_new_bucket_instance(int new_num_shards
,
357 RGWBucketInfo
& new_bucket_info
)
359 return ::create_new_bucket_instance(store
, new_num_shards
,
360 bucket_info
, bucket_attrs
, new_bucket_info
);
363 int RGWBucketReshard::cancel()
365 int ret
= reshard_lock
.lock();
370 ret
= clear_resharding();
372 reshard_lock
.unlock();
376 class BucketInfoReshardUpdate
378 rgw::sal::RGWRadosStore
*store
;
379 RGWBucketInfo
& bucket_info
;
380 std::map
<string
, bufferlist
> bucket_attrs
;
382 bool in_progress
{false};
384 int set_status(cls_rgw_reshard_status s
) {
385 bucket_info
.reshard_status
= s
;
386 int ret
= store
->getRados()->put_bucket_instance_info(bucket_info
, false, real_time(), &bucket_attrs
);
388 ldout(store
->ctx(), 0) << "ERROR: failed to write bucket info, ret=" << ret
<< dendl
;
395 BucketInfoReshardUpdate(rgw::sal::RGWRadosStore
*_store
,
396 RGWBucketInfo
& _bucket_info
,
397 map
<string
, bufferlist
>& _bucket_attrs
,
398 const string
& new_bucket_id
) :
400 bucket_info(_bucket_info
),
401 bucket_attrs(_bucket_attrs
)
403 bucket_info
.new_bucket_instance_id
= new_bucket_id
;
406 ~BucketInfoReshardUpdate() {
408 // resharding must not have ended correctly, clean up
410 RGWBucketReshard::clear_index_shard_reshard_status(store
, bucket_info
);
412 lderr(store
->ctx()) << "Error: " << __func__
<<
413 " clear_index_shard_status returned " << ret
<< dendl
;
415 bucket_info
.new_bucket_instance_id
.clear();
417 // clears new_bucket_instance as well
418 set_status(cls_rgw_reshard_status::NOT_RESHARDING
);
423 int ret
= set_status(cls_rgw_reshard_status::IN_PROGRESS
);
432 int ret
= set_status(cls_rgw_reshard_status::DONE
);
442 RGWBucketReshardLock::RGWBucketReshardLock(rgw::sal::RGWRadosStore
* _store
,
443 const std::string
& reshard_lock_oid
,
446 lock_oid(reshard_lock_oid
),
447 ephemeral(_ephemeral
),
448 internal_lock(reshard_lock_name
)
450 const int lock_dur_secs
= store
->ctx()->_conf
.get_val
<uint64_t>(
451 "rgw_reshard_bucket_lock_duration");
452 duration
= std::chrono::seconds(lock_dur_secs
);
454 #define COOKIE_LEN 16
455 char cookie_buf
[COOKIE_LEN
+ 1];
456 gen_rand_alphanumeric(store
->ctx(), cookie_buf
, sizeof(cookie_buf
) - 1);
457 cookie_buf
[COOKIE_LEN
] = '\0';
459 internal_lock
.set_cookie(cookie_buf
);
460 internal_lock
.set_duration(duration
);
463 int RGWBucketReshardLock::lock() {
464 internal_lock
.set_must_renew(false);
467 ret
= internal_lock
.lock_exclusive_ephemeral(&store
->getRados()->reshard_pool_ctx
,
470 ret
= internal_lock
.lock_exclusive(&store
->getRados()->reshard_pool_ctx
, lock_oid
);
473 ldout(store
->ctx(), 0) << "RGWReshardLock::" << __func__
<<
474 " failed to acquire lock on " << lock_oid
<< " ret=" << ret
<< dendl
;
477 reset_time(Clock::now());
482 void RGWBucketReshardLock::unlock() {
483 int ret
= internal_lock
.unlock(&store
->getRados()->reshard_pool_ctx
, lock_oid
);
485 ldout(store
->ctx(), 0) << "WARNING: RGWBucketReshardLock::" << __func__
<<
486 " failed to drop lock on " << lock_oid
<< " ret=" << ret
<< dendl
;
490 int RGWBucketReshardLock::renew(const Clock::time_point
& now
) {
491 internal_lock
.set_must_renew(true);
494 ret
= internal_lock
.lock_exclusive_ephemeral(&store
->getRados()->reshard_pool_ctx
,
497 ret
= internal_lock
.lock_exclusive(&store
->getRados()->reshard_pool_ctx
, lock_oid
);
499 if (ret
< 0) { /* expired or already locked by another processor */
500 std::stringstream error_s
;
501 if (-ENOENT
== ret
) {
502 error_s
<< "ENOENT (lock expired or never initially locked)";
504 error_s
<< ret
<< " (" << cpp_strerror(-ret
) << ")";
506 ldout(store
->ctx(), 5) << __func__
<< "(): failed to renew lock on " <<
507 lock_oid
<< " with error " << error_s
.str() << dendl
;
510 internal_lock
.set_must_renew(false);
513 ldout(store
->ctx(), 20) << __func__
<< "(): successfully renewed lock on " <<
520 int RGWBucketReshard::do_reshard(int num_shards
,
521 RGWBucketInfo
& new_bucket_info
,
525 Formatter
*formatter
)
527 rgw_bucket
& bucket
= bucket_info
.bucket
;
532 (*out
) << "tenant: " << bucket_info
.bucket
.tenant
<< std::endl
;
533 (*out
) << "bucket name: " << bucket_info
.bucket
.name
<< std::endl
;
534 (*out
) << "old bucket instance id: " << bucket_info
.bucket
.bucket_id
<<
536 (*out
) << "new bucket instance id: " << new_bucket_info
.bucket
.bucket_id
<<
540 /* update bucket info -- in progress*/
541 list
<rgw_cls_bi_entry
> entries
;
543 if (max_entries
< 0) {
544 ldout(store
->ctx(), 0) << __func__
<<
545 ": can't reshard, negative max_entries" << dendl
;
549 // NB: destructor cleans up sharding state if reshard does not
550 // complete successfully
551 BucketInfoReshardUpdate
bucket_info_updater(store
, bucket_info
, bucket_attrs
, new_bucket_info
.bucket
.bucket_id
);
553 ret
= bucket_info_updater
.start();
555 ldout(store
->ctx(), 0) << __func__
<< ": failed to update bucket info ret=" << ret
<< dendl
;
559 int num_target_shards
= (new_bucket_info
.num_shards
> 0 ? new_bucket_info
.num_shards
: 1);
561 BucketReshardManager
target_shards_mgr(store
, new_bucket_info
, num_target_shards
);
563 bool verbose_json_out
= verbose
&& (formatter
!= nullptr) && (out
!= nullptr);
565 if (verbose_json_out
) {
566 formatter
->open_array_section("entries");
569 uint64_t total_entries
= 0;
571 if (!verbose_json_out
&& out
) {
572 (*out
) << "total entries:";
575 const int num_source_shards
=
576 (bucket_info
.num_shards
> 0 ? bucket_info
.num_shards
: 1);
578 for (int i
= 0; i
< num_source_shards
; ++i
) {
579 bool is_truncated
= true;
581 while (is_truncated
) {
583 ret
= store
->getRados()->bi_list(bucket
, i
, string(), marker
, max_entries
, &entries
, &is_truncated
);
584 if (ret
< 0 && ret
!= -ENOENT
) {
585 derr
<< "ERROR: bi_list(): " << cpp_strerror(-ret
) << dendl
;
589 for (auto iter
= entries
.begin(); iter
!= entries
.end(); ++iter
) {
590 rgw_cls_bi_entry
& entry
= *iter
;
591 if (verbose_json_out
) {
592 formatter
->open_object_section("entry");
594 encode_json("shard_id", i
, formatter
);
595 encode_json("num_entry", total_entries
, formatter
);
596 encode_json("entry", entry
, formatter
);
603 cls_rgw_obj_key cls_key
;
604 RGWObjCategory category
;
605 rgw_bucket_category_stats stats
;
606 bool account
= entry
.get_info(&cls_key
, &category
, &stats
);
607 rgw_obj_key
key(cls_key
);
608 rgw_obj
obj(new_bucket_info
.bucket
, key
);
610 if (key
.ns
== RGW_OBJ_NS_MULTIPART
&& mp
.from_meta(key
.name
)) {
611 // place the multipart .meta object on the same shard as its head object
612 obj
.index_hash_source
= mp
.get_key();
614 int ret
= store
->getRados()->get_target_shard_id(new_bucket_info
, obj
.get_hash_object(), &target_shard_id
);
616 lderr(store
->ctx()) << "ERROR: get_target_shard_id() returned ret=" << ret
<< dendl
;
620 int shard_index
= (target_shard_id
> 0 ? target_shard_id
: 0);
622 ret
= target_shards_mgr
.add_entry(shard_index
, entry
, account
,
628 Clock::time_point now
= Clock::now();
629 if (reshard_lock
.should_renew(now
)) {
630 // assume outer locks have timespans at least the size of ours, so
631 // can call inside conditional
632 if (outer_reshard_lock
) {
633 ret
= outer_reshard_lock
->renew(now
);
638 ret
= reshard_lock
.renew(now
);
640 lderr(store
->ctx()) << "Error renewing bucket lock: " << ret
<< dendl
;
644 if (verbose_json_out
) {
645 formatter
->close_section();
646 formatter
->flush(*out
);
647 } else if (out
&& !(total_entries
% 1000)) {
648 (*out
) << " " << total_entries
;
654 if (verbose_json_out
) {
655 formatter
->close_section();
656 formatter
->flush(*out
);
658 (*out
) << " " << total_entries
<< std::endl
;
661 ret
= target_shards_mgr
.finish();
663 lderr(store
->ctx()) << "ERROR: failed to reshard" << dendl
;
667 ret
= store
->ctl()->bucket
->link_bucket(new_bucket_info
.owner
, new_bucket_info
.bucket
, bucket_info
.creation_time
, null_yield
);
669 lderr(store
->ctx()) << "failed to link new bucket instance (bucket_id=" << new_bucket_info
.bucket
.bucket_id
<< ": " << cpp_strerror(-ret
) << ")" << dendl
;
673 ret
= bucket_info_updater
.complete();
675 ldout(store
->ctx(), 0) << __func__
<< ": failed to update bucket info ret=" << ret
<< dendl
;
676 /* don't error out, reshard process succeeded */
680 // NB: some error clean-up is done by ~BucketInfoReshardUpdate
681 } // RGWBucketReshard::do_reshard
683 int RGWBucketReshard::get_status(list
<cls_rgw_bucket_instance_entry
> *status
)
685 return store
->svc()->bi_rados
->get_reshard_status(bucket_info
, status
);
689 int RGWBucketReshard::execute(int num_shards
, int max_op_entries
,
690 bool verbose
, ostream
*out
, Formatter
*formatter
,
691 RGWReshard
* reshard_log
)
693 int ret
= reshard_lock
.lock();
698 RGWBucketInfo new_bucket_info
;
699 ret
= create_new_bucket_instance(num_shards
, new_bucket_info
);
701 // shard state is uncertain, but this will attempt to remove them anyway
706 ret
= reshard_log
->update(bucket_info
, new_bucket_info
);
712 // set resharding status of current bucket_info & shards with
713 // information about planned resharding
714 ret
= set_resharding_status(new_bucket_info
.bucket
.bucket_id
,
715 num_shards
, cls_rgw_reshard_status::IN_PROGRESS
);
720 ret
= do_reshard(num_shards
,
723 verbose
, out
, formatter
);
728 // at this point we've done the main work; we'll make a best-effort
729 // to clean-up but will not indicate any errors encountered
731 reshard_lock
.unlock();
733 // resharding successful, so remove old bucket index shards; use
734 // best effort and don't report out an error; the lock isn't needed
735 // at this point since all we're using a best effor to to remove old
737 ret
= store
->svc()->bi
->clean_index(bucket_info
);
739 lderr(store
->ctx()) << "Error: " << __func__
<<
740 " failed to clean up old shards; " <<
741 "RGWRados::clean_bucket_index returned " << ret
<< dendl
;
744 ret
= store
->ctl()->bucket
->remove_bucket_instance_info(bucket_info
.bucket
,
745 bucket_info
, null_yield
);
747 lderr(store
->ctx()) << "Error: " << __func__
<<
748 " failed to clean old bucket info object \"" <<
749 bucket_info
.bucket
.get_key() <<
750 "\"created after successful resharding with error " << ret
<< dendl
;
753 ldout(store
->ctx(), 1) << __func__
<<
754 " INFO: reshard of bucket \"" << bucket_info
.bucket
.name
<< "\" from \"" <<
755 bucket_info
.bucket
.get_key() << "\" to \"" <<
756 new_bucket_info
.bucket
.get_key() << "\" completed successfully" << dendl
;
762 reshard_lock
.unlock();
764 // since the real problem is the issue that led to this error code
765 // path, we won't touch ret and instead use another variable to
766 // temporarily error codes
767 int ret2
= store
->svc()->bi
->clean_index(new_bucket_info
);
769 lderr(store
->ctx()) << "Error: " << __func__
<<
770 " failed to clean up shards from failed incomplete resharding; " <<
771 "RGWRados::clean_bucket_index returned " << ret2
<< dendl
;
774 ret2
= store
->ctl()->bucket
->remove_bucket_instance_info(new_bucket_info
.bucket
,
778 lderr(store
->ctx()) << "Error: " << __func__
<<
779 " failed to clean bucket info object \"" <<
780 new_bucket_info
.bucket
.get_key() <<
781 "\"created during incomplete resharding with error " << ret2
<< dendl
;
788 RGWReshard::RGWReshard(rgw::sal::RGWRadosStore
* _store
, bool _verbose
, ostream
*_out
,
789 Formatter
*_formatter
) :
790 store(_store
), instance_lock(bucket_instance_lock_name
),
791 verbose(_verbose
), out(_out
), formatter(_formatter
)
793 num_logshards
= store
->ctx()->_conf
.get_val
<uint64_t>("rgw_reshard_num_logs");
796 string
RGWReshard::get_logshard_key(const string
& tenant
,
797 const string
& bucket_name
)
799 return tenant
+ ":" + bucket_name
;
802 #define MAX_RESHARD_LOGSHARDS_PRIME 7877
804 void RGWReshard::get_bucket_logshard_oid(const string
& tenant
, const string
& bucket_name
, string
*oid
)
806 string key
= get_logshard_key(tenant
, bucket_name
);
808 uint32_t sid
= ceph_str_hash_linux(key
.c_str(), key
.size());
809 uint32_t sid2
= sid
^ ((sid
& 0xFF) << 24);
810 sid
= sid2
% MAX_RESHARD_LOGSHARDS_PRIME
% num_logshards
;
812 get_logshard_oid(int(sid
), oid
);
815 int RGWReshard::add(cls_rgw_reshard_entry
& entry
)
817 if (!store
->svc()->zone
->can_reshard()) {
818 ldout(store
->ctx(), 20) << __func__
<< " Resharding is disabled" << dendl
;
824 get_bucket_logshard_oid(entry
.tenant
, entry
.bucket_name
, &logshard_oid
);
826 librados::ObjectWriteOperation op
;
827 cls_rgw_reshard_add(op
, entry
);
829 int ret
= rgw_rados_operate(store
->getRados()->reshard_pool_ctx
, logshard_oid
, &op
, null_yield
);
831 lderr(store
->ctx()) << "ERROR: failed to add entry to reshard log, oid=" << logshard_oid
<< " tenant=" << entry
.tenant
<< " bucket=" << entry
.bucket_name
<< dendl
;
837 int RGWReshard::update(const RGWBucketInfo
& bucket_info
, const RGWBucketInfo
& new_bucket_info
)
839 cls_rgw_reshard_entry entry
;
840 entry
.bucket_name
= bucket_info
.bucket
.name
;
841 entry
.bucket_id
= bucket_info
.bucket
.bucket_id
;
842 entry
.tenant
= bucket_info
.owner
.tenant
;
844 int ret
= get(entry
);
849 entry
.new_instance_id
= new_bucket_info
.bucket
.name
+ ":" + new_bucket_info
.bucket
.bucket_id
;
853 ldout(store
->ctx(), 0) << __func__
<< ":Error in updating entry bucket " << entry
.bucket_name
<< ": " <<
854 cpp_strerror(-ret
) << dendl
;
861 int RGWReshard::list(int logshard_num
, string
& marker
, uint32_t max
, std::list
<cls_rgw_reshard_entry
>& entries
, bool *is_truncated
)
865 get_logshard_oid(logshard_num
, &logshard_oid
);
867 int ret
= cls_rgw_reshard_list(store
->getRados()->reshard_pool_ctx
, logshard_oid
, marker
, max
, entries
, is_truncated
);
870 if (ret
== -ENOENT
) {
871 *is_truncated
= false;
874 lderr(store
->ctx()) << "ERROR: failed to list reshard log entries, oid=" << logshard_oid
<< dendl
;
875 if (ret
== -EACCES
) {
876 lderr(store
->ctx()) << "access denied to pool " << store
->svc()->zone
->get_zone_params().reshard_pool
877 << ". Fix the pool access permissions of your client" << dendl
;
885 int RGWReshard::get(cls_rgw_reshard_entry
& entry
)
889 get_bucket_logshard_oid(entry
.tenant
, entry
.bucket_name
, &logshard_oid
);
891 int ret
= cls_rgw_reshard_get(store
->getRados()->reshard_pool_ctx
, logshard_oid
, entry
);
893 if (ret
!= -ENOENT
) {
894 lderr(store
->ctx()) << "ERROR: failed to get entry from reshard log, oid=" << logshard_oid
<< " tenant=" << entry
.tenant
<<
895 " bucket=" << entry
.bucket_name
<< dendl
;
903 int RGWReshard::remove(cls_rgw_reshard_entry
& entry
)
907 get_bucket_logshard_oid(entry
.tenant
, entry
.bucket_name
, &logshard_oid
);
909 librados::ObjectWriteOperation op
;
910 cls_rgw_reshard_remove(op
, entry
);
912 int ret
= rgw_rados_operate(store
->getRados()->reshard_pool_ctx
, logshard_oid
, &op
, null_yield
);
914 lderr(store
->ctx()) << "ERROR: failed to remove entry from reshard log, oid=" << logshard_oid
<< " tenant=" << entry
.tenant
<< " bucket=" << entry
.bucket_name
<< dendl
;
921 int RGWReshard::clear_bucket_resharding(const string
& bucket_instance_oid
, cls_rgw_reshard_entry
& entry
)
923 int ret
= cls_rgw_clear_bucket_resharding(store
->getRados()->reshard_pool_ctx
, bucket_instance_oid
);
925 lderr(store
->ctx()) << "ERROR: failed to clear bucket resharding, bucket_instance_oid=" << bucket_instance_oid
<< dendl
;
932 int RGWReshardWait::wait(optional_yield y
)
934 std::unique_lock
lock(mutex
);
940 #ifdef HAVE_BOOST_CONTEXT
942 auto& context
= y
.get_io_context();
943 auto& yield
= y
.get_yield_context();
945 Waiter
waiter(context
);
946 waiters
.push_back(waiter
);
949 waiter
.timer
.expires_after(duration
);
951 boost::system::error_code ec
;
952 waiter
.timer
.async_wait(yield
[ec
]);
955 waiters
.erase(waiters
.iterator_to(waiter
));
960 cond
.wait_for(lock
, duration
);
969 void RGWReshardWait::stop()
971 std::scoped_lock
lock(mutex
);
974 for (auto& waiter
: waiters
) {
975 // unblock any waiters with ECANCELED
976 waiter
.timer
.cancel();
980 int RGWReshard::process_single_logshard(int logshard_num
)
983 bool truncated
= true;
985 CephContext
*cct
= store
->ctx();
986 constexpr uint32_t max_entries
= 1000;
989 get_logshard_oid(logshard_num
, &logshard_oid
);
991 RGWBucketReshardLock
logshard_lock(store
, logshard_oid
, false);
993 int ret
= logshard_lock
.lock();
995 ldout(store
->ctx(), 5) << __func__
<< "(): failed to acquire lock on " <<
996 logshard_oid
<< ", ret = " << ret
<<dendl
;
1001 std::list
<cls_rgw_reshard_entry
> entries
;
1002 ret
= list(logshard_num
, marker
, max_entries
, entries
, &truncated
);
1004 ldout(cct
, 10) << "cannot list all reshards in logshard oid=" <<
1005 logshard_oid
<< dendl
;
1009 for(auto& entry
: entries
) { // logshard entries
1010 if(entry
.new_instance_id
.empty()) {
1012 ldout(store
->ctx(), 20) << __func__
<< " resharding " <<
1013 entry
.bucket_name
<< dendl
;
1016 RGWBucketInfo bucket_info
;
1017 map
<string
, bufferlist
> attrs
;
1019 ret
= store
->getRados()->get_bucket_info(store
->svc(),
1020 entry
.tenant
, entry
.bucket_name
,
1021 bucket_info
, nullptr,
1022 null_yield
, &attrs
);
1023 if (ret
< 0 || bucket_info
.bucket
.bucket_id
!= entry
.bucket_id
) {
1025 ldout(cct
, 0) << __func__
<<
1026 ": Error in get_bucket_info for bucket " << entry
.bucket_name
<<
1027 ": " << cpp_strerror(-ret
) << dendl
;
1028 if (ret
!= -ENOENT
) {
1029 // any error other than ENOENT will abort
1033 ldout(cct
,0) << __func__
<<
1034 ": Bucket: " << entry
.bucket_name
<<
1035 " already resharded by someone, skipping " << dendl
;
1038 // we've encountered a reshard queue entry for an apparently
1039 // non-existent bucket; let's try to recover by cleaning up
1040 ldout(cct
, 0) << __func__
<<
1041 ": removing reshard queue entry for a resharded or non-existent bucket" <<
1042 entry
.bucket_name
<< dendl
;
1044 ret
= remove(entry
);
1046 ldout(cct
, 0) << __func__
<<
1047 ": Error removing non-existent bucket " <<
1048 entry
.bucket_name
<< " from resharding queue: " <<
1049 cpp_strerror(-ret
) << dendl
;
1053 // we cleaned up, move on to the next entry
1054 goto finished_entry
;
1057 RGWBucketReshard
br(store
, bucket_info
, attrs
, nullptr);
1058 ret
= br
.execute(entry
.new_num_shards
, max_entries
, false, nullptr,
1061 ldout(store
->ctx(), 0) << __func__
<<
1062 ": Error during resharding bucket " << entry
.bucket_name
<< ":" <<
1063 cpp_strerror(-ret
)<< dendl
;
1067 ldout(store
->ctx(), 20) << __func__
<<
1068 " removing reshard queue entry for bucket " << entry
.bucket_name
<<
1071 ret
= remove(entry
);
1073 ldout(cct
, 0) << __func__
<< ": Error removing bucket " <<
1074 entry
.bucket_name
<< " from resharding queue: " <<
1075 cpp_strerror(-ret
) << dendl
;
1078 } // if new instance id is empty
1082 Clock::time_point now
= Clock::now();
1083 if (logshard_lock
.should_renew(now
)) {
1084 ret
= logshard_lock
.renew(now
);
1090 entry
.get_key(&marker
);
1092 } while (truncated
);
1094 logshard_lock
.unlock();
1099 void RGWReshard::get_logshard_oid(int shard_num
, string
*logshard
)
1102 snprintf(buf
, sizeof(buf
), "%010u", (unsigned)shard_num
);
1104 string
objname(reshard_oid_prefix
);
1105 *logshard
= objname
+ buf
;
1108 int RGWReshard::process_all_logshards()
1110 if (!store
->svc()->zone
->can_reshard()) {
1111 ldout(store
->ctx(), 20) << __func__
<< " Resharding is disabled" << dendl
;
1116 for (int i
= 0; i
< num_logshards
; i
++) {
1118 get_logshard_oid(i
, &logshard
);
1120 ldout(store
->ctx(), 20) << "processing logshard = " << logshard
<< dendl
;
1122 ret
= process_single_logshard(i
);
1124 ldout(store
->ctx(), 20) << "finish processing logshard = " << logshard
<< " , ret = " << ret
<< dendl
;
1130 bool RGWReshard::going_down()
1135 void RGWReshard::start_processor()
1137 worker
= new ReshardWorker(store
->ctx(), this);
1138 worker
->create("rgw_reshard");
1141 void RGWReshard::stop_processor()
1152 void *RGWReshard::ReshardWorker::entry() {
1154 utime_t start
= ceph_clock_now();
1155 reshard
->process_all_logshards();
1157 if (reshard
->going_down())
1160 utime_t end
= ceph_clock_now();
1162 int secs
= cct
->_conf
.get_val
<uint64_t>("rgw_reshard_thread_interval");
1164 if (secs
<= end
.sec())
1165 continue; // next round
1169 std::unique_lock locker
{lock
};
1170 cond
.wait_for(locker
, std::chrono::seconds(secs
));
1171 } while (!reshard
->going_down());
1176 void RGWReshard::ReshardWorker::stop()
1178 std::lock_guard l
{lock
};