1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
4 #include <boost/optional.hpp>
6 #include "common/ceph_json.h"
7 #include "common/RWLock.h"
8 #include "common/RefCountedObj.h"
9 #include "common/WorkQueue.h"
10 #include "common/Throttle.h"
11 #include "common/admin_socket.h"
12 #include "common/errno.h"
14 #include "rgw_common.h"
15 #include "rgw_rados.h"
18 #include "rgw_metadata.h"
19 #include "rgw_mdlog_types.h"
20 #include "rgw_rest_conn.h"
21 #include "rgw_tools.h"
22 #include "rgw_cr_rados.h"
23 #include "rgw_cr_rest.h"
24 #include "rgw_http_client.h"
25 #include "rgw_sync_trace.h"
27 #include "cls/lock/cls_lock_client.h"
29 #include "services/svc_zone.h"
30 #include "services/svc_mdlog.h"
31 #include "services/svc_meta.h"
32 #include "services/svc_cls.h"
34 #include <boost/asio/yield.hpp>
36 #define dout_subsys ceph_subsys_rgw
39 #define dout_prefix (*_dout << "meta sync: ")
41 static string mdlog_sync_status_oid
= "mdlog.sync-status";
42 static string mdlog_sync_status_shard_prefix
= "mdlog.sync-status.shard";
43 static string mdlog_sync_full_sync_index_prefix
= "meta.full-sync.index";
45 RGWSyncErrorLogger::RGWSyncErrorLogger(rgw::sal::RGWRadosStore
*_store
, const string
&oid_prefix
, int _num_shards
) : store(_store
), num_shards(_num_shards
) {
46 for (int i
= 0; i
< num_shards
; i
++) {
47 oids
.push_back(get_shard_oid(oid_prefix
, i
));
50 string
RGWSyncErrorLogger::get_shard_oid(const string
& oid_prefix
, int shard_id
) {
51 char buf
[oid_prefix
.size() + 16];
52 snprintf(buf
, sizeof(buf
), "%s.%d", oid_prefix
.c_str(), shard_id
);
56 RGWCoroutine
*RGWSyncErrorLogger::log_error_cr(const string
& source_zone
, const string
& section
, const string
& name
, uint32_t error_code
, const string
& message
) {
59 rgw_sync_error_info
info(source_zone
, error_code
, message
);
62 store
->svc()->cls
->timelog
.prepare_entry(entry
, real_clock::now(), section
, name
, bl
);
64 uint32_t shard_id
= ++counter
% num_shards
;
67 return new RGWRadosTimelogAddCR(store
, oids
[shard_id
], entry
);
70 void RGWSyncBackoff::update_wait_time()
75 cur_wait
= (cur_wait
<< 1);
77 if (cur_wait
>= max_secs
) {
82 void RGWSyncBackoff::backoff_sleep()
88 void RGWSyncBackoff::backoff(RGWCoroutine
*op
)
91 op
->wait(utime_t(cur_wait
, 0));
94 int RGWBackoffControlCR::operate() {
96 // retry the operation until it succeeds
99 std::lock_guard l
{lock
};
105 std::lock_guard l
{lock
};
112 if (retcode
!= -EBUSY
&& retcode
!= -EAGAIN
) {
113 ldout(cct
, 0) << "ERROR: RGWBackoffControlCR called coroutine returned " << retcode
<< dendl
;
115 return set_cr_error(retcode
);
121 yield backoff
.backoff(this);
124 // run an optional finisher
125 yield
call(alloc_finisher_cr());
127 ldout(cct
, 0) << "ERROR: call to finisher_cr() failed: retcode=" << retcode
<< dendl
;
128 return set_cr_error(retcode
);
130 return set_cr_done();
135 void rgw_mdlog_info::decode_json(JSONObj
*obj
) {
136 JSONDecoder::decode_json("num_objects", num_shards
, obj
);
137 JSONDecoder::decode_json("period", period
, obj
);
138 JSONDecoder::decode_json("realm_epoch", realm_epoch
, obj
);
141 void rgw_mdlog_entry::decode_json(JSONObj
*obj
) {
142 JSONDecoder::decode_json("id", id
, obj
);
143 JSONDecoder::decode_json("section", section
, obj
);
144 JSONDecoder::decode_json("name", name
, obj
);
146 JSONDecoder::decode_json("timestamp", ut
, obj
);
147 timestamp
= ut
.to_real_time();
148 JSONDecoder::decode_json("data", log_data
, obj
);
151 void rgw_mdlog_shard_data::decode_json(JSONObj
*obj
) {
152 JSONDecoder::decode_json("marker", marker
, obj
);
153 JSONDecoder::decode_json("truncated", truncated
, obj
);
154 JSONDecoder::decode_json("entries", entries
, obj
);
157 int RGWShardCollectCR::operate() {
159 while (spawn_next()) {
162 while (current_running
>= max_concurrent
) {
164 yield
wait_for_child();
165 if (collect_next(&child_ret
)) {
167 if (child_ret
< 0 && child_ret
!= -ENOENT
) {
168 ldout(cct
, 10) << __func__
<< ": failed to fetch log status, ret=" << child_ret
<< dendl
;
174 while (current_running
> 0) {
176 yield
wait_for_child();
177 if (collect_next(&child_ret
)) {
179 if (child_ret
< 0 && child_ret
!= -ENOENT
) {
180 ldout(cct
, 10) << __func__
<< ": failed to fetch log status, ret=" << child_ret
<< dendl
;
186 return set_cr_error(status
);
188 return set_cr_done();
193 class RGWReadRemoteMDLogInfoCR
: public RGWShardCollectCR
{
194 RGWMetaSyncEnv
*sync_env
;
196 const std::string
& period
;
198 map
<int, RGWMetadataLogInfo
> *mdlog_info
;
201 #define READ_MDLOG_MAX_CONCURRENT 10
204 RGWReadRemoteMDLogInfoCR(RGWMetaSyncEnv
*_sync_env
,
205 const std::string
& period
, int _num_shards
,
206 map
<int, RGWMetadataLogInfo
> *_mdlog_info
) : RGWShardCollectCR(_sync_env
->cct
, READ_MDLOG_MAX_CONCURRENT
),
208 period(period
), num_shards(_num_shards
),
209 mdlog_info(_mdlog_info
), shard_id(0) {}
210 bool spawn_next() override
;
213 class RGWListRemoteMDLogCR
: public RGWShardCollectCR
{
214 RGWMetaSyncEnv
*sync_env
;
216 const std::string
& period
;
217 map
<int, string
> shards
;
218 int max_entries_per_shard
;
219 map
<int, rgw_mdlog_shard_data
> *result
;
221 map
<int, string
>::iterator iter
;
222 #define READ_MDLOG_MAX_CONCURRENT 10
225 RGWListRemoteMDLogCR(RGWMetaSyncEnv
*_sync_env
,
226 const std::string
& period
, map
<int, string
>& _shards
,
227 int _max_entries_per_shard
,
228 map
<int, rgw_mdlog_shard_data
> *_result
) : RGWShardCollectCR(_sync_env
->cct
, READ_MDLOG_MAX_CONCURRENT
),
229 sync_env(_sync_env
), period(period
),
230 max_entries_per_shard(_max_entries_per_shard
),
232 shards
.swap(_shards
);
233 iter
= shards
.begin();
235 bool spawn_next() override
;
238 RGWRemoteMetaLog::~RGWRemoteMetaLog()
243 int RGWRemoteMetaLog::read_log_info(rgw_mdlog_info
*log_info
)
245 rgw_http_param_pair pairs
[] = { { "type", "metadata" },
248 int ret
= conn
->get_json_resource("/admin/log", pairs
, *log_info
);
250 ldpp_dout(dpp
, 0) << "ERROR: failed to fetch mdlog info" << dendl
;
254 ldpp_dout(dpp
, 20) << "remote mdlog, num_shards=" << log_info
->num_shards
<< dendl
;
259 int RGWRemoteMetaLog::read_master_log_shards_info(const string
&master_period
, map
<int, RGWMetadataLogInfo
> *shards_info
)
261 if (store
->svc()->zone
->is_meta_master()) {
265 rgw_mdlog_info log_info
;
266 int ret
= read_log_info(&log_info
);
271 return run(new RGWReadRemoteMDLogInfoCR(&sync_env
, master_period
, log_info
.num_shards
, shards_info
));
274 int RGWRemoteMetaLog::read_master_log_shards_next(const string
& period
, map
<int, string
> shard_markers
, map
<int, rgw_mdlog_shard_data
> *result
)
276 if (store
->svc()->zone
->is_meta_master()) {
280 return run(new RGWListRemoteMDLogCR(&sync_env
, period
, shard_markers
, 1, result
));
283 int RGWRemoteMetaLog::init()
285 conn
= store
->svc()->zone
->get_master_conn();
287 int ret
= http_manager
.start();
289 ldpp_dout(dpp
, 0) << "failed in http_manager.start() ret=" << ret
<< dendl
;
293 error_logger
= new RGWSyncErrorLogger(store
, RGW_SYNC_ERROR_LOG_SHARD_PREFIX
, ERROR_LOGGER_SHARDS
);
295 init_sync_env(&sync_env
);
297 tn
= sync_env
.sync_tracer
->add_node(sync_env
.sync_tracer
->root_node
, "meta");
302 void RGWRemoteMetaLog::finish()
308 #define CLONE_MAX_ENTRIES 100
310 int RGWMetaSyncStatusManager::init()
312 if (store
->svc()->zone
->is_meta_master()) {
316 if (!store
->svc()->zone
->get_master_conn()) {
317 lderr(store
->ctx()) << "no REST connection to master zone" << dendl
;
321 int r
= rgw_init_ioctx(store
->getRados()->get_rados_handle(), store
->svc()->zone
->get_zone_params().log_pool
, ioctx
, true);
323 lderr(store
->ctx()) << "ERROR: failed to open log pool (" << store
->svc()->zone
->get_zone_params().log_pool
<< " ret=" << r
<< dendl
;
327 r
= master_log
.init();
329 lderr(store
->ctx()) << "ERROR: failed to init remote log, r=" << r
<< dendl
;
333 RGWMetaSyncEnv
& sync_env
= master_log
.get_sync_env();
335 rgw_meta_sync_status sync_status
;
336 r
= read_sync_status(&sync_status
);
337 if (r
< 0 && r
!= -ENOENT
) {
338 lderr(store
->ctx()) << "ERROR: failed to read sync status, r=" << r
<< dendl
;
342 int num_shards
= sync_status
.sync_info
.num_shards
;
344 for (int i
= 0; i
< num_shards
; i
++) {
345 shard_objs
[i
] = rgw_raw_obj(store
->svc()->zone
->get_zone_params().log_pool
, sync_env
.shard_obj_name(i
));
348 std::unique_lock wl
{ts_to_shard_lock
};
349 for (int i
= 0; i
< num_shards
; i
++) {
350 clone_markers
.push_back(string());
359 unsigned RGWMetaSyncStatusManager::get_subsys() const
364 std::ostream
& RGWMetaSyncStatusManager::gen_prefix(std::ostream
& out
) const
366 return out
<< "meta sync: ";
369 void RGWMetaSyncEnv::init(const DoutPrefixProvider
*_dpp
, CephContext
*_cct
, rgw::sal::RGWRadosStore
*_store
, RGWRESTConn
*_conn
,
370 RGWAsyncRadosProcessor
*_async_rados
, RGWHTTPManager
*_http_manager
,
371 RGWSyncErrorLogger
*_error_logger
, RGWSyncTraceManager
*_sync_tracer
) {
376 async_rados
= _async_rados
;
377 http_manager
= _http_manager
;
378 error_logger
= _error_logger
;
379 sync_tracer
= _sync_tracer
;
382 string
RGWMetaSyncEnv::status_oid()
384 return mdlog_sync_status_oid
;
387 string
RGWMetaSyncEnv::shard_obj_name(int shard_id
)
389 char buf
[mdlog_sync_status_shard_prefix
.size() + 16];
390 snprintf(buf
, sizeof(buf
), "%s.%d", mdlog_sync_status_shard_prefix
.c_str(), shard_id
);
395 class RGWAsyncReadMDLogEntries
: public RGWAsyncRadosRequest
{
396 rgw::sal::RGWRadosStore
*store
;
397 RGWMetadataLog
*mdlog
;
402 int _send_request() override
{
408 mdlog
->init_list_entries(shard_id
, from_time
, end_time
, marker
, &handle
);
410 int ret
= mdlog
->list_entries(handle
, max_entries
, entries
, &marker
, &truncated
);
412 mdlog
->complete_list_entries(handle
);
418 list
<cls_log_entry
> entries
;
421 RGWAsyncReadMDLogEntries(RGWCoroutine
*caller
, RGWAioCompletionNotifier
*cn
, rgw::sal::RGWRadosStore
*_store
,
422 RGWMetadataLog
* mdlog
, int _shard_id
,
423 std::string _marker
, int _max_entries
)
424 : RGWAsyncRadosRequest(caller
, cn
), store(_store
), mdlog(mdlog
),
425 shard_id(_shard_id
), max_entries(_max_entries
), marker(std::move(_marker
)) {}
428 class RGWReadMDLogEntriesCR
: public RGWSimpleCoroutine
{
429 RGWMetaSyncEnv
*sync_env
;
430 RGWMetadataLog
*const mdlog
;
435 list
<cls_log_entry
> *entries
;
438 RGWAsyncReadMDLogEntries
*req
{nullptr};
441 RGWReadMDLogEntriesCR(RGWMetaSyncEnv
*_sync_env
, RGWMetadataLog
* mdlog
,
442 int _shard_id
, string
*_marker
, int _max_entries
,
443 list
<cls_log_entry
> *_entries
, bool *_truncated
)
444 : RGWSimpleCoroutine(_sync_env
->cct
), sync_env(_sync_env
), mdlog(mdlog
),
445 shard_id(_shard_id
), pmarker(_marker
), max_entries(_max_entries
),
446 entries(_entries
), truncated(_truncated
) {}
448 ~RGWReadMDLogEntriesCR() override
{
454 int send_request() override
{
456 req
= new RGWAsyncReadMDLogEntries(this, stack
->create_completion_notifier(),
457 sync_env
->store
, mdlog
, shard_id
, marker
,
459 sync_env
->async_rados
->queue(req
);
463 int request_complete() override
{
464 *pmarker
= std::move(req
->marker
);
465 *entries
= std::move(req
->entries
);
466 *truncated
= req
->truncated
;
467 return req
->get_ret_status();
472 class RGWReadRemoteMDLogShardInfoCR
: public RGWCoroutine
{
474 RGWRESTReadResource
*http_op
;
476 const std::string
& period
;
478 RGWMetadataLogInfo
*shard_info
;
481 RGWReadRemoteMDLogShardInfoCR(RGWMetaSyncEnv
*env
, const std::string
& period
,
482 int _shard_id
, RGWMetadataLogInfo
*_shard_info
)
483 : RGWCoroutine(env
->store
->ctx()), env(env
), http_op(NULL
),
484 period(period
), shard_id(_shard_id
), shard_info(_shard_info
) {}
486 int operate() override
{
487 auto store
= env
->store
;
488 RGWRESTConn
*conn
= store
->svc()->zone
->get_master_conn();
492 snprintf(buf
, sizeof(buf
), "%d", shard_id
);
493 rgw_http_param_pair pairs
[] = { { "type" , "metadata" },
495 { "period", period
.c_str() },
499 string p
= "/admin/log/";
501 http_op
= new RGWRESTReadResource(conn
, p
, pairs
, NULL
,
504 init_new_io(http_op
);
506 int ret
= http_op
->aio_read();
508 ldpp_dout(env
->dpp
, 0) << "ERROR: failed to read from " << p
<< dendl
;
509 log_error() << "failed to send http operation: " << http_op
->to_str() << " ret=" << ret
<< std::endl
;
511 return set_cr_error(ret
);
517 int ret
= http_op
->wait(shard_info
, null_yield
);
520 return set_cr_error(ret
);
522 return set_cr_done();
529 RGWCoroutine
* create_read_remote_mdlog_shard_info_cr(RGWMetaSyncEnv
*env
,
530 const std::string
& period
,
532 RGWMetadataLogInfo
* info
)
534 return new RGWReadRemoteMDLogShardInfoCR(env
, period
, shard_id
, info
);
537 class RGWListRemoteMDLogShardCR
: public RGWSimpleCoroutine
{
538 RGWMetaSyncEnv
*sync_env
;
539 RGWRESTReadResource
*http_op
;
541 const std::string
& period
;
544 uint32_t max_entries
;
545 rgw_mdlog_shard_data
*result
;
548 RGWListRemoteMDLogShardCR(RGWMetaSyncEnv
*env
, const std::string
& period
,
549 int _shard_id
, const string
& _marker
, uint32_t _max_entries
,
550 rgw_mdlog_shard_data
*_result
)
551 : RGWSimpleCoroutine(env
->store
->ctx()), sync_env(env
), http_op(NULL
),
552 period(period
), shard_id(_shard_id
), marker(_marker
), max_entries(_max_entries
), result(_result
) {}
554 int send_request() override
{
555 RGWRESTConn
*conn
= sync_env
->conn
;
558 snprintf(buf
, sizeof(buf
), "%d", shard_id
);
560 char max_entries_buf
[32];
561 snprintf(max_entries_buf
, sizeof(max_entries_buf
), "%d", (int)max_entries
);
563 const char *marker_key
= (marker
.empty() ? "" : "marker");
565 rgw_http_param_pair pairs
[] = { { "type", "metadata" },
567 { "period", period
.c_str() },
568 { "max-entries", max_entries_buf
},
569 { marker_key
, marker
.c_str() },
572 string p
= "/admin/log/";
574 http_op
= new RGWRESTReadResource(conn
, p
, pairs
, NULL
, sync_env
->http_manager
);
575 init_new_io(http_op
);
577 int ret
= http_op
->aio_read();
579 ldpp_dout(sync_env
->dpp
, 0) << "ERROR: failed to read from " << p
<< dendl
;
580 log_error() << "failed to send http operation: " << http_op
->to_str() << " ret=" << ret
<< std::endl
;
588 int request_complete() override
{
589 int ret
= http_op
->wait(result
, null_yield
);
591 if (ret
< 0 && ret
!= -ENOENT
) {
592 ldpp_dout(sync_env
->dpp
, 0) << "ERROR: failed to list remote mdlog shard, ret=" << ret
<< dendl
;
599 RGWCoroutine
* create_list_remote_mdlog_shard_cr(RGWMetaSyncEnv
*env
,
600 const std::string
& period
,
602 const std::string
& marker
,
603 uint32_t max_entries
,
604 rgw_mdlog_shard_data
*result
)
606 return new RGWListRemoteMDLogShardCR(env
, period
, shard_id
, marker
,
607 max_entries
, result
);
610 bool RGWReadRemoteMDLogInfoCR::spawn_next() {
611 if (shard_id
>= num_shards
) {
614 spawn(new RGWReadRemoteMDLogShardInfoCR(sync_env
, period
, shard_id
, &(*mdlog_info
)[shard_id
]), false);
619 bool RGWListRemoteMDLogCR::spawn_next() {
620 if (iter
== shards
.end()) {
624 spawn(new RGWListRemoteMDLogShardCR(sync_env
, period
, iter
->first
, iter
->second
, max_entries_per_shard
, &(*result
)[iter
->first
]), false);
629 class RGWInitSyncStatusCoroutine
: public RGWCoroutine
{
630 RGWMetaSyncEnv
*sync_env
;
632 rgw_meta_sync_info status
;
633 vector
<RGWMetadataLogInfo
> shards_info
;
634 boost::intrusive_ptr
<RGWContinuousLeaseCR
> lease_cr
;
635 boost::intrusive_ptr
<RGWCoroutinesStack
> lease_stack
;
637 RGWInitSyncStatusCoroutine(RGWMetaSyncEnv
*_sync_env
,
638 const rgw_meta_sync_info
&status
)
639 : RGWCoroutine(_sync_env
->store
->ctx()), sync_env(_sync_env
),
640 status(status
), shards_info(status
.num_shards
),
641 lease_cr(nullptr), lease_stack(nullptr) {}
643 ~RGWInitSyncStatusCoroutine() override
{
649 int operate() override
{
653 set_status("acquiring sync lock");
654 uint32_t lock_duration
= cct
->_conf
->rgw_sync_lease_period
;
655 string lock_name
= "sync_lock";
656 rgw::sal::RGWRadosStore
*store
= sync_env
->store
;
657 lease_cr
.reset(new RGWContinuousLeaseCR(sync_env
->async_rados
, store
,
658 rgw_raw_obj(store
->svc()->zone
->get_zone_params().log_pool
, sync_env
->status_oid()),
659 lock_name
, lock_duration
, this));
660 lease_stack
.reset(spawn(lease_cr
.get(), false));
662 while (!lease_cr
->is_locked()) {
663 if (lease_cr
->is_done()) {
664 ldpp_dout(sync_env
->dpp
, 5) << "lease cr failed, done early " << dendl
;
665 set_status("lease lock failed, early abort");
666 return set_cr_error(lease_cr
->get_ret_status());
672 set_status("writing sync status");
673 rgw::sal::RGWRadosStore
*store
= sync_env
->store
;
674 call(new RGWSimpleRadosWriteCR
<rgw_meta_sync_info
>(sync_env
->async_rados
, store
->svc()->sysobj
,
675 rgw_raw_obj(store
->svc()->zone
->get_zone_params().log_pool
, sync_env
->status_oid()),
680 set_status("failed to write sync status");
681 ldpp_dout(sync_env
->dpp
, 0) << "ERROR: failed to write sync status, retcode=" << retcode
<< dendl
;
682 yield lease_cr
->go_down();
683 return set_cr_error(retcode
);
685 /* fetch current position in logs */
686 set_status("fetching remote log position");
688 for (int i
= 0; i
< (int)status
.num_shards
; i
++) {
689 spawn(new RGWReadRemoteMDLogShardInfoCR(sync_env
, status
.period
, i
,
690 &shards_info
[i
]), false);
694 drain_all_but_stack(lease_stack
.get()); /* the lease cr still needs to run */
697 set_status("updating sync status");
698 for (int i
= 0; i
< (int)status
.num_shards
; i
++) {
699 rgw_meta_sync_marker marker
;
700 RGWMetadataLogInfo
& info
= shards_info
[i
];
701 marker
.next_step_marker
= info
.marker
;
702 marker
.timestamp
= info
.last_update
;
703 rgw::sal::RGWRadosStore
*store
= sync_env
->store
;
704 spawn(new RGWSimpleRadosWriteCR
<rgw_meta_sync_marker
>(sync_env
->async_rados
,
705 store
->svc()->sysobj
,
706 rgw_raw_obj(store
->svc()->zone
->get_zone_params().log_pool
, sync_env
->shard_obj_name(i
)),
711 set_status("changing sync state: build full sync maps");
712 status
.state
= rgw_meta_sync_info::StateBuildingFullSyncMaps
;
713 rgw::sal::RGWRadosStore
*store
= sync_env
->store
;
714 call(new RGWSimpleRadosWriteCR
<rgw_meta_sync_info
>(sync_env
->async_rados
, store
->svc()->sysobj
,
715 rgw_raw_obj(store
->svc()->zone
->get_zone_params().log_pool
, sync_env
->status_oid()),
718 set_status("drop lock lease");
719 yield lease_cr
->go_down();
720 while (collect(&ret
, NULL
)) {
722 return set_cr_error(ret
);
727 return set_cr_done();
733 class RGWReadSyncStatusMarkersCR
: public RGWShardCollectCR
{
734 static constexpr int MAX_CONCURRENT_SHARDS
= 16;
737 const int num_shards
;
739 map
<uint32_t, rgw_meta_sync_marker
>& markers
;
742 RGWReadSyncStatusMarkersCR(RGWMetaSyncEnv
*env
, int num_shards
,
743 map
<uint32_t, rgw_meta_sync_marker
>& markers
)
744 : RGWShardCollectCR(env
->cct
, MAX_CONCURRENT_SHARDS
),
745 env(env
), num_shards(num_shards
), markers(markers
)
747 bool spawn_next() override
;
750 bool RGWReadSyncStatusMarkersCR::spawn_next()
752 if (shard_id
>= num_shards
) {
755 using CR
= RGWSimpleRadosReadCR
<rgw_meta_sync_marker
>;
756 rgw_raw_obj obj
{env
->store
->svc()->zone
->get_zone_params().log_pool
,
757 env
->shard_obj_name(shard_id
)};
758 spawn(new CR(env
->async_rados
, env
->store
->svc()->sysobj
, obj
, &markers
[shard_id
]), false);
763 class RGWReadSyncStatusCoroutine
: public RGWCoroutine
{
764 RGWMetaSyncEnv
*sync_env
;
765 rgw_meta_sync_status
*sync_status
;
768 RGWReadSyncStatusCoroutine(RGWMetaSyncEnv
*_sync_env
,
769 rgw_meta_sync_status
*_status
)
770 : RGWCoroutine(_sync_env
->cct
), sync_env(_sync_env
), sync_status(_status
)
772 int operate() override
;
775 int RGWReadSyncStatusCoroutine::operate()
779 using ReadInfoCR
= RGWSimpleRadosReadCR
<rgw_meta_sync_info
>;
781 bool empty_on_enoent
= false; // fail on ENOENT
782 rgw_raw_obj obj
{sync_env
->store
->svc()->zone
->get_zone_params().log_pool
,
783 sync_env
->status_oid()};
784 call(new ReadInfoCR(sync_env
->async_rados
, sync_env
->store
->svc()->sysobj
, obj
,
785 &sync_status
->sync_info
, empty_on_enoent
));
788 ldpp_dout(sync_env
->dpp
, 4) << "failed to read sync status info with "
789 << cpp_strerror(retcode
) << dendl
;
790 return set_cr_error(retcode
);
792 // read shard markers
793 using ReadMarkersCR
= RGWReadSyncStatusMarkersCR
;
794 yield
call(new ReadMarkersCR(sync_env
, sync_status
->sync_info
.num_shards
,
795 sync_status
->sync_markers
));
797 ldpp_dout(sync_env
->dpp
, 4) << "failed to read sync status markers with "
798 << cpp_strerror(retcode
) << dendl
;
799 return set_cr_error(retcode
);
801 return set_cr_done();
806 class RGWFetchAllMetaCR
: public RGWCoroutine
{
807 RGWMetaSyncEnv
*sync_env
;
814 list
<string
> sections
;
815 list
<string
>::iterator sections_iter
;
817 struct meta_list_result
{
821 bool truncated
{false};
823 void decode_json(JSONObj
*obj
) {
824 JSONDecoder::decode_json("keys", keys
, obj
);
825 JSONDecoder::decode_json("marker", marker
, obj
);
826 JSONDecoder::decode_json("count", count
, obj
);
827 JSONDecoder::decode_json("truncated", truncated
, obj
);
830 list
<string
>::iterator iter
;
832 std::unique_ptr
<RGWShardedOmapCRManager
> entries_index
;
834 boost::intrusive_ptr
<RGWContinuousLeaseCR
> lease_cr
;
835 boost::intrusive_ptr
<RGWCoroutinesStack
> lease_stack
;
841 map
<uint32_t, rgw_meta_sync_marker
>& markers
;
843 RGWSyncTraceNodeRef tn
;
846 RGWFetchAllMetaCR(RGWMetaSyncEnv
*_sync_env
, int _num_shards
,
847 map
<uint32_t, rgw_meta_sync_marker
>& _markers
,
848 RGWSyncTraceNodeRef
& _tn_parent
) : RGWCoroutine(_sync_env
->cct
), sync_env(_sync_env
),
849 num_shards(_num_shards
),
850 ret_status(0), lease_cr(nullptr), lease_stack(nullptr),
851 lost_lock(false), failed(false), markers(_markers
) {
852 tn
= sync_env
->sync_tracer
->add_node(_tn_parent
, "fetch_all_meta");
855 ~RGWFetchAllMetaCR() override
{
858 void append_section_from_set(set
<string
>& all_sections
, const string
& name
) {
859 set
<string
>::iterator iter
= all_sections
.find(name
);
860 if (iter
!= all_sections
.end()) {
861 sections
.emplace_back(std::move(*iter
));
862 all_sections
.erase(iter
);
866 * meta sync should go in the following order: user, bucket.instance, bucket
867 * then whatever other sections exist (if any)
869 void rearrange_sections() {
870 set
<string
> all_sections
;
871 std::move(sections
.begin(), sections
.end(),
872 std::inserter(all_sections
, all_sections
.end()));
875 append_section_from_set(all_sections
, "user");
876 append_section_from_set(all_sections
, "bucket.instance");
877 append_section_from_set(all_sections
, "bucket");
879 std::move(all_sections
.begin(), all_sections
.end(),
880 std::back_inserter(sections
));
883 int operate() override
{
884 RGWRESTConn
*conn
= sync_env
->conn
;
888 set_status(string("acquiring lock (") + sync_env
->status_oid() + ")");
889 uint32_t lock_duration
= cct
->_conf
->rgw_sync_lease_period
;
890 string lock_name
= "sync_lock";
891 lease_cr
.reset(new RGWContinuousLeaseCR(sync_env
->async_rados
,
893 rgw_raw_obj(sync_env
->store
->svc()->zone
->get_zone_params().log_pool
, sync_env
->status_oid()),
894 lock_name
, lock_duration
, this));
895 lease_stack
.reset(spawn(lease_cr
.get(), false));
897 while (!lease_cr
->is_locked()) {
898 if (lease_cr
->is_done()) {
899 ldpp_dout(sync_env
->dpp
, 5) << "lease cr failed, done early " << dendl
;
900 set_status("failed acquiring lock");
901 return set_cr_error(lease_cr
->get_ret_status());
906 entries_index
.reset(new RGWShardedOmapCRManager(sync_env
->async_rados
, sync_env
->store
, this, num_shards
,
907 sync_env
->store
->svc()->zone
->get_zone_params().log_pool
,
908 mdlog_sync_full_sync_index_prefix
));
910 call(new RGWReadRESTResourceCR
<list
<string
> >(cct
, conn
, sync_env
->http_manager
,
911 "/admin/metadata", NULL
, §ions
));
913 if (get_ret_status() < 0) {
914 ldpp_dout(sync_env
->dpp
, 0) << "ERROR: failed to fetch metadata sections" << dendl
;
915 yield entries_index
->finish();
916 yield lease_cr
->go_down();
918 return set_cr_error(get_ret_status());
920 rearrange_sections();
921 sections_iter
= sections
.begin();
922 for (; sections_iter
!= sections
.end(); ++sections_iter
) {
925 #define META_FULL_SYNC_CHUNK_SIZE "1000"
926 string entrypoint
= string("/admin/metadata/") + *sections_iter
;
927 rgw_http_param_pair pairs
[] = { { "max-entries", META_FULL_SYNC_CHUNK_SIZE
},
928 { "marker", result
.marker
.c_str() },
931 call(new RGWReadRESTResourceCR
<meta_list_result
>(cct
, conn
, sync_env
->http_manager
,
932 entrypoint
, pairs
, &result
));
934 ret_status
= get_ret_status();
935 if (ret_status
== -ENOENT
) {
936 set_retcode(0); /* reset coroutine status so that we don't return it */
939 if (ret_status
< 0) {
940 tn
->log(0, SSTR("ERROR: failed to fetch metadata section: " << *sections_iter
));
941 yield entries_index
->finish();
942 yield lease_cr
->go_down();
944 return set_cr_error(ret_status
);
946 iter
= result
.keys
.begin();
947 for (; iter
!= result
.keys
.end(); ++iter
) {
948 if (!lease_cr
->is_locked()) {
952 yield
; // allow entries_index consumer to make progress
954 tn
->log(20, SSTR("list metadata: section=" << *sections_iter
<< " key=" << *iter
));
955 string s
= *sections_iter
+ ":" + *iter
;
957 rgw::sal::RGWRadosStore
*store
= sync_env
->store
;
958 int ret
= store
->ctl()->meta
.mgr
->get_shard_id(*sections_iter
, *iter
, &shard_id
);
960 tn
->log(0, SSTR("ERROR: could not determine shard id for " << *sections_iter
<< ":" << *iter
));
964 if (!entries_index
->append(s
, shard_id
)) {
968 } while (result
.truncated
);
971 if (!entries_index
->finish()) {
976 for (map
<uint32_t, rgw_meta_sync_marker
>::iterator iter
= markers
.begin(); iter
!= markers
.end(); ++iter
) {
977 int shard_id
= (int)iter
->first
;
978 rgw_meta_sync_marker
& marker
= iter
->second
;
979 marker
.total_entries
= entries_index
->get_total_entries(shard_id
);
980 spawn(new RGWSimpleRadosWriteCR
<rgw_meta_sync_marker
>(sync_env
->async_rados
, sync_env
->store
->svc()->sysobj
,
981 rgw_raw_obj(sync_env
->store
->svc()->zone
->get_zone_params().log_pool
, sync_env
->shard_obj_name(shard_id
)),
986 drain_all_but_stack(lease_stack
.get()); /* the lease cr still needs to run */
988 yield lease_cr
->go_down();
991 while (collect(&ret
, NULL
)) {
993 return set_cr_error(ret
);
999 yield
return set_cr_error(-EIO
);
1002 yield
return set_cr_error(-EBUSY
);
1005 if (ret_status
< 0) {
1006 yield
return set_cr_error(ret_status
);
1009 yield
return set_cr_done();
1015 static string
full_sync_index_shard_oid(int shard_id
)
1017 char buf
[mdlog_sync_full_sync_index_prefix
.size() + 16];
1018 snprintf(buf
, sizeof(buf
), "%s.%d", mdlog_sync_full_sync_index_prefix
.c_str(), shard_id
);
1022 class RGWReadRemoteMetadataCR
: public RGWCoroutine
{
1023 RGWMetaSyncEnv
*sync_env
;
1025 RGWRESTReadResource
*http_op
;
1032 RGWSyncTraceNodeRef tn
;
1035 RGWReadRemoteMetadataCR(RGWMetaSyncEnv
*_sync_env
,
1036 const string
& _section
, const string
& _key
, bufferlist
*_pbl
,
1037 const RGWSyncTraceNodeRef
& _tn_parent
) : RGWCoroutine(_sync_env
->cct
), sync_env(_sync_env
),
1042 tn
= sync_env
->sync_tracer
->add_node(_tn_parent
, "read_remote_meta",
1043 section
+ ":" + key
);
1046 int operate() override
{
1047 RGWRESTConn
*conn
= sync_env
->conn
;
1050 rgw_http_param_pair pairs
[] = { { "key" , key
.c_str()},
1053 string p
= string("/admin/metadata/") + section
+ "/" + key
;
1055 http_op
= new RGWRESTReadResource(conn
, p
, pairs
, NULL
, sync_env
->http_manager
);
1057 init_new_io(http_op
);
1059 int ret
= http_op
->aio_read();
1061 ldpp_dout(sync_env
->dpp
, 0) << "ERROR: failed to fetch mdlog data" << dendl
;
1062 log_error() << "failed to send http operation: " << http_op
->to_str() << " ret=" << ret
<< std::endl
;
1064 return set_cr_error(ret
);
1070 int ret
= http_op
->wait(pbl
, null_yield
);
1073 return set_cr_error(ret
);
1075 return set_cr_done();
1082 class RGWAsyncMetaStoreEntry
: public RGWAsyncRadosRequest
{
1083 rgw::sal::RGWRadosStore
*store
;
1087 int _send_request() override
{
1088 int ret
= store
->ctl()->meta
.mgr
->put(raw_key
, bl
, null_yield
, RGWMDLogSyncType::APPLY_ALWAYS
);
1090 ldout(store
->ctx(), 0) << "ERROR: can't store key: " << raw_key
<< " ret=" << ret
<< dendl
;
1096 RGWAsyncMetaStoreEntry(RGWCoroutine
*caller
, RGWAioCompletionNotifier
*cn
, rgw::sal::RGWRadosStore
*_store
,
1097 const string
& _raw_key
,
1098 bufferlist
& _bl
) : RGWAsyncRadosRequest(caller
, cn
), store(_store
),
1099 raw_key(_raw_key
), bl(_bl
) {}
1103 class RGWMetaStoreEntryCR
: public RGWSimpleCoroutine
{
1104 RGWMetaSyncEnv
*sync_env
;
1108 RGWAsyncMetaStoreEntry
*req
;
1111 RGWMetaStoreEntryCR(RGWMetaSyncEnv
*_sync_env
,
1112 const string
& _raw_key
,
1113 bufferlist
& _bl
) : RGWSimpleCoroutine(_sync_env
->cct
), sync_env(_sync_env
),
1114 raw_key(_raw_key
), bl(_bl
), req(NULL
) {
1117 ~RGWMetaStoreEntryCR() override
{
1123 int send_request() override
{
1124 req
= new RGWAsyncMetaStoreEntry(this, stack
->create_completion_notifier(),
1125 sync_env
->store
, raw_key
, bl
);
1126 sync_env
->async_rados
->queue(req
);
1130 int request_complete() override
{
1131 return req
->get_ret_status();
1135 class RGWAsyncMetaRemoveEntry
: public RGWAsyncRadosRequest
{
1136 rgw::sal::RGWRadosStore
*store
;
1139 int _send_request() override
{
1140 int ret
= store
->ctl()->meta
.mgr
->remove(raw_key
, null_yield
);
1142 ldout(store
->ctx(), 0) << "ERROR: can't remove key: " << raw_key
<< " ret=" << ret
<< dendl
;
1148 RGWAsyncMetaRemoveEntry(RGWCoroutine
*caller
, RGWAioCompletionNotifier
*cn
, rgw::sal::RGWRadosStore
*_store
,
1149 const string
& _raw_key
) : RGWAsyncRadosRequest(caller
, cn
), store(_store
),
1150 raw_key(_raw_key
) {}
1154 class RGWMetaRemoveEntryCR
: public RGWSimpleCoroutine
{
1155 RGWMetaSyncEnv
*sync_env
;
1158 RGWAsyncMetaRemoveEntry
*req
;
1161 RGWMetaRemoveEntryCR(RGWMetaSyncEnv
*_sync_env
,
1162 const string
& _raw_key
) : RGWSimpleCoroutine(_sync_env
->cct
), sync_env(_sync_env
),
1163 raw_key(_raw_key
), req(NULL
) {
1166 ~RGWMetaRemoveEntryCR() override
{
1172 int send_request() override
{
1173 req
= new RGWAsyncMetaRemoveEntry(this, stack
->create_completion_notifier(),
1174 sync_env
->store
, raw_key
);
1175 sync_env
->async_rados
->queue(req
);
1179 int request_complete() override
{
1180 int r
= req
->get_ret_status();
1188 #define META_SYNC_UPDATE_MARKER_WINDOW 10
1191 int RGWLastCallerWinsCR::operate() {
1192 RGWCoroutine
*call_cr
;
1197 yield
call(call_cr
);
1198 /* cr might have been modified at this point */
1200 return set_cr_done();
1205 class RGWMetaSyncShardMarkerTrack
: public RGWSyncShardMarkerTrack
<string
, string
> {
1206 RGWMetaSyncEnv
*sync_env
;
1209 rgw_meta_sync_marker sync_marker
;
1211 RGWSyncTraceNodeRef tn
;
1214 RGWMetaSyncShardMarkerTrack(RGWMetaSyncEnv
*_sync_env
,
1215 const string
& _marker_oid
,
1216 const rgw_meta_sync_marker
& _marker
,
1217 RGWSyncTraceNodeRef
& _tn
) : RGWSyncShardMarkerTrack(META_SYNC_UPDATE_MARKER_WINDOW
),
1218 sync_env(_sync_env
),
1219 marker_oid(_marker_oid
),
1220 sync_marker(_marker
),
1223 RGWCoroutine
*store_marker(const string
& new_marker
, uint64_t index_pos
, const real_time
& timestamp
) override
{
1224 sync_marker
.marker
= new_marker
;
1225 if (index_pos
> 0) {
1226 sync_marker
.pos
= index_pos
;
1229 if (!real_clock::is_zero(timestamp
)) {
1230 sync_marker
.timestamp
= timestamp
;
1233 ldpp_dout(sync_env
->dpp
, 20) << __func__
<< "(): updating marker marker_oid=" << marker_oid
<< " marker=" << new_marker
<< " realm_epoch=" << sync_marker
.realm_epoch
<< dendl
;
1234 tn
->log(20, SSTR("new marker=" << new_marker
));
1235 rgw::sal::RGWRadosStore
*store
= sync_env
->store
;
1236 return new RGWSimpleRadosWriteCR
<rgw_meta_sync_marker
>(sync_env
->async_rados
,
1237 store
->svc()->sysobj
,
1238 rgw_raw_obj(store
->svc()->zone
->get_zone_params().log_pool
, marker_oid
),
1242 RGWOrderCallCR
*allocate_order_control_cr() override
{
1243 return new RGWLastCallerWinsCR(sync_env
->cct
);
1247 RGWMetaSyncSingleEntryCR::RGWMetaSyncSingleEntryCR(RGWMetaSyncEnv
*_sync_env
,
1248 const string
& _raw_key
, const string
& _entry_marker
,
1249 const RGWMDLogStatus
& _op_status
,
1250 RGWMetaSyncShardMarkerTrack
*_marker_tracker
, const RGWSyncTraceNodeRef
& _tn_parent
) : RGWCoroutine(_sync_env
->cct
),
1251 sync_env(_sync_env
),
1252 raw_key(_raw_key
), entry_marker(_entry_marker
),
1253 op_status(_op_status
),
1254 pos(0), sync_status(0),
1255 marker_tracker(_marker_tracker
), tries(0) {
1256 error_injection
= (sync_env
->cct
->_conf
->rgw_sync_meta_inject_err_probability
> 0);
1257 tn
= sync_env
->sync_tracer
->add_node(_tn_parent
, "entry", raw_key
);
1260 int RGWMetaSyncSingleEntryCR::operate() {
1262 #define NUM_TRANSIENT_ERROR_RETRIES 10
1264 if (error_injection
&&
1265 rand() % 10000 < cct
->_conf
->rgw_sync_meta_inject_err_probability
* 10000.0) {
1266 return set_cr_error(-EIO
);
1269 if (op_status
!= MDLOG_STATUS_COMPLETE
) {
1270 tn
->log(20, "skipping pending operation");
1271 yield
call(marker_tracker
->finish(entry_marker
));
1273 return set_cr_error(retcode
);
1275 return set_cr_done();
1277 tn
->set_flag(RGW_SNS_FLAG_ACTIVE
);
1278 for (tries
= 0; tries
< NUM_TRANSIENT_ERROR_RETRIES
; tries
++) {
1280 pos
= raw_key
.find(':');
1281 section
= raw_key
.substr(0, pos
);
1282 key
= raw_key
.substr(pos
+ 1);
1283 tn
->log(10, SSTR("fetching remote metadata entry" << (tries
== 0 ? "" : " (retry)")));
1284 call(new RGWReadRemoteMetadataCR(sync_env
, section
, key
, &md_bl
, tn
));
1287 sync_status
= retcode
;
1289 if (sync_status
== -ENOENT
) {
1290 /* FIXME: do we need to remove the entry from the local zone? */
1294 if ((sync_status
== -EAGAIN
|| sync_status
== -ECANCELED
) && (tries
< NUM_TRANSIENT_ERROR_RETRIES
- 1)) {
1295 ldpp_dout(sync_env
->dpp
, 20) << *this << ": failed to fetch remote metadata: " << section
<< ":" << key
<< ", will retry" << dendl
;
1299 if (sync_status
< 0) {
1300 tn
->log(10, SSTR("failed to send read remote metadata entry: section=" << section
<< " key=" << key
<< " status=" << sync_status
));
1301 log_error() << "failed to send read remote metadata entry: section=" << section
<< " key=" << key
<< " status=" << sync_status
<< std::endl
;
1302 yield
call(sync_env
->error_logger
->log_error_cr(sync_env
->conn
->get_remote_id(), section
, key
, -sync_status
,
1303 string("failed to read remote metadata entry: ") + cpp_strerror(-sync_status
)));
1304 return set_cr_error(sync_status
);
1311 for (tries
= 0; tries
< NUM_TRANSIENT_ERROR_RETRIES
; tries
++) {
1312 if (sync_status
!= -ENOENT
) {
1313 tn
->log(10, SSTR("storing local metadata entry"));
1314 yield
call(new RGWMetaStoreEntryCR(sync_env
, raw_key
, md_bl
));
1316 tn
->log(10, SSTR("removing local metadata entry"));
1317 yield
call(new RGWMetaRemoveEntryCR(sync_env
, raw_key
));
1319 if ((retcode
== -EAGAIN
|| retcode
== -ECANCELED
) && (tries
< NUM_TRANSIENT_ERROR_RETRIES
- 1)) {
1320 ldpp_dout(sync_env
->dpp
, 20) << *this << ": failed to store metadata: " << section
<< ":" << key
<< ", got retcode=" << retcode
<< dendl
;
1326 sync_status
= retcode
;
1328 if (sync_status
== 0 && marker_tracker
) {
1330 yield
call(marker_tracker
->finish(entry_marker
));
1331 sync_status
= retcode
;
1333 if (sync_status
< 0) {
1334 tn
->log(10, SSTR("failed, status=" << sync_status
));
1335 return set_cr_error(sync_status
);
1337 tn
->log(10, "success");
1338 return set_cr_done();
1343 class RGWCloneMetaLogCoroutine
: public RGWCoroutine
{
1344 RGWMetaSyncEnv
*sync_env
;
1345 RGWMetadataLog
*mdlog
;
1347 const std::string
& period
;
1350 bool truncated
= false;
1353 int max_entries
= CLONE_MAX_ENTRIES
;
1355 RGWRESTReadResource
*http_op
= nullptr;
1356 boost::intrusive_ptr
<RGWMetadataLogInfoCompletion
> completion
;
1358 RGWMetadataLogInfo shard_info
;
1359 rgw_mdlog_shard_data data
;
1362 RGWCloneMetaLogCoroutine(RGWMetaSyncEnv
*_sync_env
, RGWMetadataLog
* mdlog
,
1363 const std::string
& period
, int _id
,
1364 const string
& _marker
, string
*_new_marker
)
1365 : RGWCoroutine(_sync_env
->cct
), sync_env(_sync_env
), mdlog(mdlog
),
1366 period(period
), shard_id(_id
), marker(_marker
), new_marker(_new_marker
) {
1368 *new_marker
= marker
;
1371 ~RGWCloneMetaLogCoroutine() override
{
1376 completion
->cancel();
1380 int operate() override
;
1383 int state_read_shard_status();
1384 int state_read_shard_status_complete();
1385 int state_send_rest_request();
1386 int state_receive_rest_response();
1387 int state_store_mdlog_entries();
1388 int state_store_mdlog_entries_complete();
1391 class RGWMetaSyncShardCR
: public RGWCoroutine
{
1392 RGWMetaSyncEnv
*sync_env
;
1394 const rgw_pool
& pool
;
1395 const std::string
& period
; //< currently syncing period id
1396 const epoch_t realm_epoch
; //< realm_epoch of period
1397 RGWMetadataLog
* mdlog
; //< log of syncing period
1399 rgw_meta_sync_marker
& sync_marker
;
1400 boost::optional
<rgw_meta_sync_marker
> temp_marker
; //< for pending updates
1403 const std::string
& period_marker
; //< max marker stored in next period
1405 RGWRadosGetOmapKeysCR::ResultPtr omapkeys
;
1406 std::set
<std::string
> entries
;
1407 std::set
<std::string
>::iterator iter
;
1411 RGWMetaSyncShardMarkerTrack
*marker_tracker
= nullptr;
1413 list
<cls_log_entry
> log_entries
;
1414 list
<cls_log_entry
>::iterator log_iter
;
1415 bool truncated
= false;
1417 string mdlog_marker
;
1419 rgw_mdlog_entry mdlog_entry
;
1421 ceph::mutex inc_lock
= ceph::make_mutex("RGWMetaSyncShardCR::inc_lock");
1422 ceph::condition_variable inc_cond
;
1424 boost::asio::coroutine incremental_cr
;
1425 boost::asio::coroutine full_cr
;
1427 boost::intrusive_ptr
<RGWContinuousLeaseCR
> lease_cr
;
1428 boost::intrusive_ptr
<RGWCoroutinesStack
> lease_stack
;
1430 bool lost_lock
= false;
1432 bool *reset_backoff
;
1434 // hold a reference to the cr stack while it's in the map
1435 using StackRef
= boost::intrusive_ptr
<RGWCoroutinesStack
>;
1436 map
<StackRef
, string
> stack_to_pos
;
1437 map
<string
, string
> pos_to_prev
;
1439 bool can_adjust_marker
= false;
1440 bool done_with_period
= false;
1442 int total_entries
= 0;
1444 RGWSyncTraceNodeRef tn
;
1446 RGWMetaSyncShardCR(RGWMetaSyncEnv
*_sync_env
, const rgw_pool
& _pool
,
1447 const std::string
& period
, epoch_t realm_epoch
,
1448 RGWMetadataLog
* mdlog
, uint32_t _shard_id
,
1449 rgw_meta_sync_marker
& _marker
,
1450 const std::string
& period_marker
, bool *_reset_backoff
,
1451 RGWSyncTraceNodeRef
& _tn
)
1452 : RGWCoroutine(_sync_env
->cct
), sync_env(_sync_env
), pool(_pool
),
1453 period(period
), realm_epoch(realm_epoch
), mdlog(mdlog
),
1454 shard_id(_shard_id
), sync_marker(_marker
),
1455 period_marker(period_marker
),
1456 reset_backoff(_reset_backoff
), tn(_tn
) {
1457 *reset_backoff
= false;
1460 ~RGWMetaSyncShardCR() override
{
1461 delete marker_tracker
;
1467 void set_marker_tracker(RGWMetaSyncShardMarkerTrack
*mt
) {
1468 delete marker_tracker
;
1469 marker_tracker
= mt
;
1472 int operate() override
{
1475 switch (sync_marker
.state
) {
1476 case rgw_meta_sync_marker::FullSync
:
1479 ldpp_dout(sync_env
->dpp
, 10) << "sync: full_sync: shard_id=" << shard_id
<< " r=" << r
<< dendl
;
1480 return set_cr_error(r
);
1483 case rgw_meta_sync_marker::IncrementalSync
:
1484 r
= incremental_sync();
1486 ldpp_dout(sync_env
->dpp
, 10) << "sync: incremental_sync: shard_id=" << shard_id
<< " r=" << r
<< dendl
;
1487 return set_cr_error(r
);
1496 void collect_children()
1499 RGWCoroutinesStack
*child
;
1500 while (collect_next(&child_ret
, &child
)) {
1501 auto iter
= stack_to_pos
.find(child
);
1502 if (iter
== stack_to_pos
.end()) {
1503 /* some other stack that we don't care about */
1507 string
& pos
= iter
->second
;
1509 if (child_ret
< 0) {
1510 ldpp_dout(sync_env
->dpp
, 0) << *this << ": child operation stack=" << child
<< " entry=" << pos
<< " returned " << child_ret
<< dendl
;
1513 map
<string
, string
>::iterator prev_iter
= pos_to_prev
.find(pos
);
1514 ceph_assert(prev_iter
!= pos_to_prev
.end());
1517 * we should get -EAGAIN for transient errors, for which we want to retry, so we don't
1518 * update the marker and abort. We'll get called again for these. Permanent errors will be
1519 * handled by marking the entry at the error log shard, so that we retry on it separately
1521 if (child_ret
== -EAGAIN
) {
1522 can_adjust_marker
= false;
1525 if (pos_to_prev
.size() == 1) {
1526 if (can_adjust_marker
) {
1527 sync_marker
.marker
= pos
;
1529 pos_to_prev
.erase(prev_iter
);
1531 ceph_assert(pos_to_prev
.size() > 1);
1532 pos_to_prev
.erase(prev_iter
);
1533 prev_iter
= pos_to_prev
.begin();
1534 if (can_adjust_marker
) {
1535 sync_marker
.marker
= prev_iter
->second
;
1539 ldpp_dout(sync_env
->dpp
, 4) << *this << ": adjusting marker pos=" << sync_marker
.marker
<< dendl
;
1540 stack_to_pos
.erase(iter
);
1545 #define OMAP_GET_MAX_ENTRIES 100
1546 int max_entries
= OMAP_GET_MAX_ENTRIES
;
1548 set_status("full_sync");
1549 tn
->log(10, "start full sync");
1550 oid
= full_sync_index_shard_oid(shard_id
);
1551 can_adjust_marker
= true;
1554 uint32_t lock_duration
= cct
->_conf
->rgw_sync_lease_period
;
1555 string lock_name
= "sync_lock";
1556 rgw::sal::RGWRadosStore
*store
= sync_env
->store
;
1557 lease_cr
.reset(new RGWContinuousLeaseCR(sync_env
->async_rados
, store
,
1558 rgw_raw_obj(pool
, sync_env
->shard_obj_name(shard_id
)),
1559 lock_name
, lock_duration
, this));
1560 lease_stack
.reset(spawn(lease_cr
.get(), false));
1563 while (!lease_cr
->is_locked()) {
1564 if (lease_cr
->is_done()) {
1566 tn
->log(5, "failed to take lease");
1567 return lease_cr
->get_ret_status();
1572 tn
->log(10, "took lease");
1574 /* lock succeeded, a retry now should avoid previous backoff status */
1575 *reset_backoff
= true;
1577 /* prepare marker tracker */
1578 set_marker_tracker(new RGWMetaSyncShardMarkerTrack(sync_env
,
1579 sync_env
->shard_obj_name(shard_id
),
1582 marker
= sync_marker
.marker
;
1584 total_entries
= sync_marker
.pos
;
1588 if (!lease_cr
->is_locked()) {
1589 tn
->log(10, "lost lease");
1593 omapkeys
= std::make_shared
<RGWRadosGetOmapKeysCR::Result
>();
1594 yield
call(new RGWRadosGetOmapKeysCR(sync_env
->store
, rgw_raw_obj(pool
, oid
),
1595 marker
, max_entries
, omapkeys
));
1597 ldpp_dout(sync_env
->dpp
, 0) << "ERROR: " << __func__
<< "(): RGWRadosGetOmapKeysCR() returned ret=" << retcode
<< dendl
;
1598 tn
->log(0, SSTR("ERROR: failed to list omap keys, status=" << retcode
));
1599 yield lease_cr
->go_down();
1603 entries
= std::move(omapkeys
->entries
);
1604 tn
->log(20, SSTR("retrieved " << entries
.size() << " entries to sync"));
1605 if (entries
.size() > 0) {
1606 tn
->set_flag(RGW_SNS_FLAG_ACTIVE
); /* actually have entries to sync */
1608 iter
= entries
.begin();
1609 for (; iter
!= entries
.end(); ++iter
) {
1611 tn
->log(20, SSTR("full sync: " << marker
));
1613 if (!marker_tracker
->start(marker
, total_entries
, real_time())) {
1614 tn
->log(0, SSTR("ERROR: cannot start syncing " << marker
<< ". Duplicate entry?"));
1616 // fetch remote and write locally
1618 RGWCoroutinesStack
*stack
= spawn(new RGWMetaSyncSingleEntryCR(sync_env
, marker
, marker
, MDLOG_STATUS_COMPLETE
, marker_tracker
, tn
), false);
1619 // stack_to_pos holds a reference to the stack
1620 stack_to_pos
[stack
] = marker
;
1621 pos_to_prev
[marker
] = marker
;
1626 } while (omapkeys
->more
&& can_adjust_marker
);
1628 tn
->unset_flag(RGW_SNS_FLAG_ACTIVE
); /* actually have entries to sync */
1630 while (num_spawned() > 1) {
1631 yield
wait_for_child();
1636 /* update marker to reflect we're done with full sync */
1637 if (can_adjust_marker
) {
1638 // apply updates to a temporary marker, or operate() will send us
1639 // to incremental_sync() after we yield
1640 temp_marker
= sync_marker
;
1641 temp_marker
->state
= rgw_meta_sync_marker::IncrementalSync
;
1642 temp_marker
->marker
= std::move(temp_marker
->next_step_marker
);
1643 temp_marker
->next_step_marker
.clear();
1644 temp_marker
->realm_epoch
= realm_epoch
;
1645 ldpp_dout(sync_env
->dpp
, 4) << *this << ": saving marker pos=" << temp_marker
->marker
<< " realm_epoch=" << realm_epoch
<< dendl
;
1647 using WriteMarkerCR
= RGWSimpleRadosWriteCR
<rgw_meta_sync_marker
>;
1648 yield
call(new WriteMarkerCR(sync_env
->async_rados
, sync_env
->store
->svc()->sysobj
,
1649 rgw_raw_obj(pool
, sync_env
->shard_obj_name(shard_id
)),
1654 ldpp_dout(sync_env
->dpp
, 0) << "ERROR: failed to set sync marker: retcode=" << retcode
<< dendl
;
1655 yield lease_cr
->go_down();
1662 * if we reached here, it means that lost_lock is true, otherwise the state
1663 * change in the previous block will prevent us from reaching here
1666 yield lease_cr
->go_down();
1672 if (!can_adjust_marker
) {
1680 tn
->log(10, "full sync complete");
1682 // apply the sync marker update
1683 ceph_assert(temp_marker
);
1684 sync_marker
= std::move(*temp_marker
);
1685 temp_marker
= boost::none
;
1686 // must not yield after this point!
1692 int incremental_sync() {
1693 reenter(&incremental_cr
) {
1694 set_status("incremental_sync");
1695 tn
->log(10, "start incremental sync");
1696 can_adjust_marker
= true;
1698 if (!lease_cr
) { /* could have had a lease_cr lock from previous state */
1700 uint32_t lock_duration
= cct
->_conf
->rgw_sync_lease_period
;
1701 string lock_name
= "sync_lock";
1702 rgw::sal::RGWRadosStore
*store
= sync_env
->store
;
1703 lease_cr
.reset( new RGWContinuousLeaseCR(sync_env
->async_rados
, store
,
1704 rgw_raw_obj(pool
, sync_env
->shard_obj_name(shard_id
)),
1705 lock_name
, lock_duration
, this));
1706 lease_stack
.reset(spawn(lease_cr
.get(), false));
1709 while (!lease_cr
->is_locked()) {
1710 if (lease_cr
->is_done()) {
1712 tn
->log(10, "failed to take lease");
1713 return lease_cr
->get_ret_status();
1719 tn
->log(10, "took lease");
1720 // if the period has advanced, we can't use the existing marker
1721 if (sync_marker
.realm_epoch
< realm_epoch
) {
1722 ldpp_dout(sync_env
->dpp
, 4) << "clearing marker=" << sync_marker
.marker
1723 << " from old realm_epoch=" << sync_marker
.realm_epoch
1724 << " (now " << realm_epoch
<< ')' << dendl
;
1725 sync_marker
.realm_epoch
= realm_epoch
;
1726 sync_marker
.marker
.clear();
1728 mdlog_marker
= sync_marker
.marker
;
1729 set_marker_tracker(new RGWMetaSyncShardMarkerTrack(sync_env
,
1730 sync_env
->shard_obj_name(shard_id
),
1734 * mdlog_marker: the remote sync marker positiion
1735 * sync_marker: the local sync marker position
1736 * max_marker: the max mdlog position that we fetched
1737 * marker: the current position we try to sync
1738 * period_marker: the last marker before the next period begins (optional)
1740 marker
= max_marker
= sync_marker
.marker
;
1743 if (!lease_cr
->is_locked()) {
1745 tn
->log(10, "lost lease");
1748 #define INCREMENTAL_MAX_ENTRIES 100
1749 ldpp_dout(sync_env
->dpp
, 20) << __func__
<< ":" << __LINE__
<< ": shard_id=" << shard_id
<< " mdlog_marker=" << mdlog_marker
<< " sync_marker.marker=" << sync_marker
.marker
<< " period_marker=" << period_marker
<< dendl
;
1750 if (!period_marker
.empty() && period_marker
<= mdlog_marker
) {
1751 tn
->log(10, SSTR("finished syncing current period: mdlog_marker=" << mdlog_marker
<< " sync_marker=" << sync_marker
.marker
<< " period_marker=" << period_marker
));
1752 done_with_period
= true;
1755 if (mdlog_marker
<= max_marker
) {
1756 /* we're at the tip, try to bring more entries */
1757 ldpp_dout(sync_env
->dpp
, 20) << __func__
<< ":" << __LINE__
<< ": shard_id=" << shard_id
<< " syncing mdlog for shard_id=" << shard_id
<< dendl
;
1758 yield
call(new RGWCloneMetaLogCoroutine(sync_env
, mdlog
,
1760 mdlog_marker
, &mdlog_marker
));
1763 tn
->log(10, SSTR(*this << ": failed to fetch more log entries, retcode=" << retcode
));
1764 yield lease_cr
->go_down();
1766 *reset_backoff
= false; // back off and try again later
1769 *reset_backoff
= true; /* if we got to this point, all systems function */
1770 if (mdlog_marker
> max_marker
) {
1771 tn
->set_flag(RGW_SNS_FLAG_ACTIVE
); /* actually have entries to sync */
1772 tn
->log(20, SSTR("mdlog_marker=" << mdlog_marker
<< " sync_marker=" << sync_marker
.marker
));
1773 marker
= max_marker
;
1774 yield
call(new RGWReadMDLogEntriesCR(sync_env
, mdlog
, shard_id
,
1775 &max_marker
, INCREMENTAL_MAX_ENTRIES
,
1776 &log_entries
, &truncated
));
1778 tn
->log(10, SSTR("failed to list mdlog entries, retcode=" << retcode
));
1779 yield lease_cr
->go_down();
1781 *reset_backoff
= false; // back off and try again later
1784 for (log_iter
= log_entries
.begin(); log_iter
!= log_entries
.end() && !done_with_period
; ++log_iter
) {
1785 if (!period_marker
.empty() && period_marker
<= log_iter
->id
) {
1786 done_with_period
= true;
1787 if (period_marker
< log_iter
->id
) {
1788 tn
->log(10, SSTR("found key=" << log_iter
->id
1789 << " past period_marker=" << period_marker
));
1792 ldpp_dout(sync_env
->dpp
, 10) << "found key at period_marker=" << period_marker
<< dendl
;
1793 // sync this entry, then return control to RGWMetaSyncCR
1795 if (!mdlog_entry
.convert_from(*log_iter
)) {
1796 tn
->log(0, SSTR("ERROR: failed to convert mdlog entry, shard_id=" << shard_id
<< " log_entry: " << log_iter
->id
<< ":" << log_iter
->section
<< ":" << log_iter
->name
<< ":" << log_iter
->timestamp
<< " ... skipping entry"));
1799 tn
->log(20, SSTR("log_entry: " << log_iter
->id
<< ":" << log_iter
->section
<< ":" << log_iter
->name
<< ":" << log_iter
->timestamp
));
1800 if (!marker_tracker
->start(log_iter
->id
, 0, log_iter
->timestamp
.to_real_time())) {
1801 ldpp_dout(sync_env
->dpp
, 0) << "ERROR: cannot start syncing " << log_iter
->id
<< ". Duplicate entry?" << dendl
;
1803 raw_key
= log_iter
->section
+ ":" + log_iter
->name
;
1805 RGWCoroutinesStack
*stack
= spawn(new RGWMetaSyncSingleEntryCR(sync_env
, raw_key
, log_iter
->id
, mdlog_entry
.log_data
.status
, marker_tracker
, tn
), false);
1807 // stack_to_pos holds a reference to the stack
1808 stack_to_pos
[stack
] = log_iter
->id
;
1809 pos_to_prev
[log_iter
->id
] = marker
;
1812 marker
= log_iter
->id
;
1816 ldpp_dout(sync_env
->dpp
, 20) << __func__
<< ":" << __LINE__
<< ": shard_id=" << shard_id
<< " mdlog_marker=" << mdlog_marker
<< " max_marker=" << max_marker
<< " sync_marker.marker=" << sync_marker
.marker
<< " period_marker=" << period_marker
<< dendl
;
1817 if (done_with_period
) {
1818 // return control to RGWMetaSyncCR and advance to the next period
1819 tn
->log(10, SSTR(*this << ": done with period"));
1822 if (mdlog_marker
== max_marker
&& can_adjust_marker
) {
1823 tn
->unset_flag(RGW_SNS_FLAG_ACTIVE
);
1824 #define INCREMENTAL_INTERVAL 20
1825 yield
wait(utime_t(INCREMENTAL_INTERVAL
, 0));
1827 } while (can_adjust_marker
);
1829 tn
->unset_flag(RGW_SNS_FLAG_ACTIVE
);
1831 while (num_spawned() > 1) {
1832 yield
wait_for_child();
1836 yield lease_cr
->go_down();
1844 if (!can_adjust_marker
) {
1848 return set_cr_done();
1855 class RGWMetaSyncShardControlCR
: public RGWBackoffControlCR
1857 RGWMetaSyncEnv
*sync_env
;
1859 const rgw_pool
& pool
;
1860 const std::string
& period
;
1861 epoch_t realm_epoch
;
1862 RGWMetadataLog
* mdlog
;
1864 rgw_meta_sync_marker sync_marker
;
1865 const std::string period_marker
;
1867 RGWSyncTraceNodeRef tn
;
1869 static constexpr bool exit_on_error
= false; // retry on all errors
1871 RGWMetaSyncShardControlCR(RGWMetaSyncEnv
*_sync_env
, const rgw_pool
& _pool
,
1872 const std::string
& period
, epoch_t realm_epoch
,
1873 RGWMetadataLog
* mdlog
, uint32_t _shard_id
,
1874 const rgw_meta_sync_marker
& _marker
,
1875 std::string
&& period_marker
,
1876 RGWSyncTraceNodeRef
& _tn_parent
)
1877 : RGWBackoffControlCR(_sync_env
->cct
, exit_on_error
), sync_env(_sync_env
),
1878 pool(_pool
), period(period
), realm_epoch(realm_epoch
), mdlog(mdlog
),
1879 shard_id(_shard_id
), sync_marker(_marker
),
1880 period_marker(std::move(period_marker
)) {
1881 tn
= sync_env
->sync_tracer
->add_node(_tn_parent
, "shard",
1882 std::to_string(shard_id
));
1885 RGWCoroutine
*alloc_cr() override
{
1886 return new RGWMetaSyncShardCR(sync_env
, pool
, period
, realm_epoch
, mdlog
,
1887 shard_id
, sync_marker
, period_marker
, backoff_ptr(), tn
);
1890 RGWCoroutine
*alloc_finisher_cr() override
{
1891 rgw::sal::RGWRadosStore
*store
= sync_env
->store
;
1892 return new RGWSimpleRadosReadCR
<rgw_meta_sync_marker
>(sync_env
->async_rados
, store
->svc()->sysobj
,
1893 rgw_raw_obj(pool
, sync_env
->shard_obj_name(shard_id
)),
1898 class RGWMetaSyncCR
: public RGWCoroutine
{
1899 RGWMetaSyncEnv
*sync_env
;
1900 const rgw_pool
& pool
;
1901 RGWPeriodHistory::Cursor cursor
; //< sync position in period history
1902 RGWPeriodHistory::Cursor next
; //< next period in history
1903 rgw_meta_sync_status sync_status
;
1904 RGWSyncTraceNodeRef tn
;
1906 std::mutex mutex
; //< protect access to shard_crs
1908 // TODO: it should be enough to hold a reference on the stack only, as calling
1909 // RGWCoroutinesStack::wakeup() doesn't refer to the RGWCoroutine if it has
1910 // already completed
1911 using ControlCRRef
= boost::intrusive_ptr
<RGWMetaSyncShardControlCR
>;
1912 using StackRef
= boost::intrusive_ptr
<RGWCoroutinesStack
>;
1913 using RefPair
= std::pair
<ControlCRRef
, StackRef
>;
1914 map
<int, RefPair
> shard_crs
;
1918 RGWMetaSyncCR(RGWMetaSyncEnv
*_sync_env
, const RGWPeriodHistory::Cursor
&cursor
,
1919 const rgw_meta_sync_status
& _sync_status
, RGWSyncTraceNodeRef
& _tn
)
1920 : RGWCoroutine(_sync_env
->cct
), sync_env(_sync_env
),
1921 pool(sync_env
->store
->svc()->zone
->get_zone_params().log_pool
),
1922 cursor(cursor
), sync_status(_sync_status
), tn(_tn
) {}
1927 int operate() override
{
1929 // loop through one period at a time
1930 tn
->log(1, "start");
1932 if (cursor
== sync_env
->store
->svc()->mdlog
->get_period_history()->get_current()) {
1933 next
= RGWPeriodHistory::Cursor
{};
1935 ldpp_dout(sync_env
->dpp
, 10) << "RGWMetaSyncCR on current period="
1936 << cursor
.get_period().get_id() << dendl
;
1938 ldpp_dout(sync_env
->dpp
, 10) << "RGWMetaSyncCR with no period" << dendl
;
1943 ldpp_dout(sync_env
->dpp
, 10) << "RGWMetaSyncCR on period="
1944 << cursor
.get_period().get_id() << ", next="
1945 << next
.get_period().get_id() << dendl
;
1949 // get the mdlog for the current period (may be empty)
1950 auto& period_id
= sync_status
.sync_info
.period
;
1951 auto realm_epoch
= sync_status
.sync_info
.realm_epoch
;
1952 auto mdlog
= sync_env
->store
->svc()->mdlog
->get_log(period_id
);
1954 tn
->log(1, SSTR("realm epoch=" << realm_epoch
<< " period id=" << period_id
));
1956 // prevent wakeup() from accessing shard_crs while we're spawning them
1957 std::lock_guard
<std::mutex
> lock(mutex
);
1959 // sync this period on each shard
1960 for (const auto& m
: sync_status
.sync_markers
) {
1961 uint32_t shard_id
= m
.first
;
1962 auto& marker
= m
.second
;
1964 std::string period_marker
;
1966 // read the maximum marker from the next period's sync status
1967 period_marker
= next
.get_period().get_sync_status()[shard_id
];
1968 if (period_marker
.empty()) {
1969 // no metadata changes have occurred on this shard, skip it
1970 ldpp_dout(sync_env
->dpp
, 10) << "RGWMetaSyncCR: skipping shard " << shard_id
1971 << " with empty period marker" << dendl
;
1976 using ShardCR
= RGWMetaSyncShardControlCR
;
1977 auto cr
= new ShardCR(sync_env
, pool
, period_id
, realm_epoch
,
1978 mdlog
, shard_id
, marker
,
1979 std::move(period_marker
), tn
);
1980 auto stack
= spawn(cr
, false);
1981 shard_crs
[shard_id
] = RefPair
{cr
, stack
};
1984 // wait for each shard to complete
1985 while (ret
== 0 && num_spawned() > 0) {
1986 yield
wait_for_child();
1987 collect(&ret
, nullptr);
1991 // drop shard cr refs under lock
1992 std::lock_guard
<std::mutex
> lock(mutex
);
1996 return set_cr_error(ret
);
1998 // advance to the next period
2002 // write the updated sync info
2003 sync_status
.sync_info
.period
= cursor
.get_period().get_id();
2004 sync_status
.sync_info
.realm_epoch
= cursor
.get_epoch();
2005 yield
call(new RGWSimpleRadosWriteCR
<rgw_meta_sync_info
>(sync_env
->async_rados
,
2006 sync_env
->store
->svc()->sysobj
,
2007 rgw_raw_obj(pool
, sync_env
->status_oid()),
2008 sync_status
.sync_info
));
2014 void wakeup(int shard_id
) {
2015 std::lock_guard
<std::mutex
> lock(mutex
);
2016 auto iter
= shard_crs
.find(shard_id
);
2017 if (iter
== shard_crs
.end()) {
2020 iter
->second
.first
->wakeup();
2024 void RGWRemoteMetaLog::init_sync_env(RGWMetaSyncEnv
*env
) {
2026 env
->cct
= store
->ctx();
2029 env
->async_rados
= async_rados
;
2030 env
->http_manager
= &http_manager
;
2031 env
->error_logger
= error_logger
;
2032 env
->sync_tracer
= store
->getRados()->get_sync_tracer();
2035 int RGWRemoteMetaLog::read_sync_status(rgw_meta_sync_status
*sync_status
)
2037 if (store
->svc()->zone
->is_meta_master()) {
2040 // cannot run concurrently with run_sync(), so run in a separate manager
2041 RGWCoroutinesManager
crs(store
->ctx(), store
->getRados()->get_cr_registry());
2042 RGWHTTPManager
http_manager(store
->ctx(), crs
.get_completion_mgr());
2043 int ret
= http_manager
.start();
2045 ldpp_dout(dpp
, 0) << "failed in http_manager.start() ret=" << ret
<< dendl
;
2048 RGWMetaSyncEnv sync_env_local
= sync_env
;
2049 sync_env_local
.http_manager
= &http_manager
;
2050 tn
->log(20, "read sync status");
2051 ret
= crs
.run(new RGWReadSyncStatusCoroutine(&sync_env_local
, sync_status
));
2052 http_manager
.stop();
2056 int RGWRemoteMetaLog::init_sync_status()
2058 if (store
->svc()->zone
->is_meta_master()) {
2062 rgw_mdlog_info mdlog_info
;
2063 int r
= read_log_info(&mdlog_info
);
2065 lderr(store
->ctx()) << "ERROR: fail to fetch master log info (r=" << r
<< ")" << dendl
;
2069 rgw_meta_sync_info sync_info
;
2070 sync_info
.num_shards
= mdlog_info
.num_shards
;
2071 auto cursor
= store
->svc()->mdlog
->get_period_history()->get_current();
2073 sync_info
.period
= cursor
.get_period().get_id();
2074 sync_info
.realm_epoch
= cursor
.get_epoch();
2077 return run(new RGWInitSyncStatusCoroutine(&sync_env
, sync_info
));
2080 int RGWRemoteMetaLog::store_sync_info(const rgw_meta_sync_info
& sync_info
)
2082 tn
->log(20, "store sync info");
2083 return run(new RGWSimpleRadosWriteCR
<rgw_meta_sync_info
>(async_rados
, store
->svc()->sysobj
,
2084 rgw_raw_obj(store
->svc()->zone
->get_zone_params().log_pool
, sync_env
.status_oid()),
2088 // return a cursor to the period at our sync position
2089 static RGWPeriodHistory::Cursor
get_period_at(rgw::sal::RGWRadosStore
* store
,
2090 const rgw_meta_sync_info
& info
)
2092 if (info
.period
.empty()) {
2093 // return an empty cursor with error=0
2094 return RGWPeriodHistory::Cursor
{};
2097 // look for an existing period in our history
2098 auto cursor
= store
->svc()->mdlog
->get_period_history()->lookup(info
.realm_epoch
);
2100 // verify that the period ids match
2101 auto& existing
= cursor
.get_period().get_id();
2102 if (existing
!= info
.period
) {
2103 lderr(store
->ctx()) << "ERROR: sync status period=" << info
.period
2104 << " does not match period=" << existing
2105 << " in history at realm epoch=" << info
.realm_epoch
<< dendl
;
2106 return RGWPeriodHistory::Cursor
{-EEXIST
};
2111 // read the period from rados or pull it from the master
2113 int r
= store
->svc()->mdlog
->pull_period(info
.period
, period
);
2115 lderr(store
->ctx()) << "ERROR: failed to read period id "
2116 << info
.period
<< ": " << cpp_strerror(r
) << dendl
;
2117 return RGWPeriodHistory::Cursor
{r
};
2119 // attach the period to our history
2120 cursor
= store
->svc()->mdlog
->get_period_history()->attach(std::move(period
));
2122 r
= cursor
.get_error();
2123 lderr(store
->ctx()) << "ERROR: failed to read period history back to "
2124 << info
.period
<< ": " << cpp_strerror(r
) << dendl
;
2129 int RGWRemoteMetaLog::run_sync()
2131 if (store
->svc()->zone
->is_meta_master()) {
2137 // get shard count and oldest log period from master
2138 rgw_mdlog_info mdlog_info
;
2141 ldpp_dout(dpp
, 1) << __func__
<< "(): going down" << dendl
;
2144 r
= read_log_info(&mdlog_info
);
2145 if (r
== -EIO
|| r
== -ENOENT
) {
2146 // keep retrying if master isn't alive or hasn't initialized the log
2147 ldpp_dout(dpp
, 10) << __func__
<< "(): waiting for master.." << dendl
;
2148 backoff
.backoff_sleep();
2153 lderr(store
->ctx()) << "ERROR: fail to fetch master log info (r=" << r
<< ")" << dendl
;
2159 rgw_meta_sync_status sync_status
;
2162 ldpp_dout(dpp
, 1) << __func__
<< "(): going down" << dendl
;
2165 r
= run(new RGWReadSyncStatusCoroutine(&sync_env
, &sync_status
));
2166 if (r
< 0 && r
!= -ENOENT
) {
2167 ldpp_dout(dpp
, 0) << "ERROR: failed to fetch sync status r=" << r
<< dendl
;
2171 if (!mdlog_info
.period
.empty()) {
2172 // restart sync if the remote has a period, but:
2173 // a) our status does not, or
2174 // b) our sync period comes before the remote's oldest log period
2175 if (sync_status
.sync_info
.period
.empty() ||
2176 sync_status
.sync_info
.realm_epoch
< mdlog_info
.realm_epoch
) {
2177 sync_status
.sync_info
.state
= rgw_meta_sync_info::StateInit
;
2179 if (sync_status
.sync_info
.period
.empty()) {
2180 reason
= "period is empty";
2182 reason
= SSTR("sync_info realm epoch is behind: " << sync_status
.sync_info
.realm_epoch
<< " < " << mdlog_info
.realm_epoch
);
2184 tn
->log(1, "initialize sync (reason: " + reason
+ ")");
2185 ldpp_dout(dpp
, 1) << "epoch=" << sync_status
.sync_info
.realm_epoch
2186 << " in sync status comes before remote's oldest mdlog epoch="
2187 << mdlog_info
.realm_epoch
<< ", restarting sync" << dendl
;
2191 if (sync_status
.sync_info
.state
== rgw_meta_sync_info::StateInit
) {
2192 ldpp_dout(dpp
, 20) << __func__
<< "(): init" << dendl
;
2193 sync_status
.sync_info
.num_shards
= mdlog_info
.num_shards
;
2194 auto cursor
= store
->svc()->mdlog
->get_period_history()->get_current();
2196 // run full sync, then start incremental from the current period/epoch
2197 sync_status
.sync_info
.period
= cursor
.get_period().get_id();
2198 sync_status
.sync_info
.realm_epoch
= cursor
.get_epoch();
2200 r
= run(new RGWInitSyncStatusCoroutine(&sync_env
, sync_status
.sync_info
));
2202 backoff
.backoff_sleep();
2207 ldpp_dout(dpp
, 0) << "ERROR: failed to init sync status r=" << r
<< dendl
;
2211 } while (sync_status
.sync_info
.state
== rgw_meta_sync_info::StateInit
);
2213 auto num_shards
= sync_status
.sync_info
.num_shards
;
2214 if (num_shards
!= mdlog_info
.num_shards
) {
2215 lderr(store
->ctx()) << "ERROR: can't sync, mismatch between num shards, master num_shards=" << mdlog_info
.num_shards
<< " local num_shards=" << num_shards
<< dendl
;
2219 RGWPeriodHistory::Cursor cursor
;
2221 r
= run(new RGWReadSyncStatusCoroutine(&sync_env
, &sync_status
));
2222 if (r
< 0 && r
!= -ENOENT
) {
2223 tn
->log(0, SSTR("ERROR: failed to fetch sync status r=" << r
));
2227 switch ((rgw_meta_sync_info::SyncState
)sync_status
.sync_info
.state
) {
2228 case rgw_meta_sync_info::StateBuildingFullSyncMaps
:
2229 tn
->log(20, "building full sync maps");
2230 r
= run(new RGWFetchAllMetaCR(&sync_env
, num_shards
, sync_status
.sync_markers
, tn
));
2231 if (r
== -EBUSY
|| r
== -EAGAIN
) {
2232 backoff
.backoff_sleep();
2237 tn
->log(0, SSTR("ERROR: failed to fetch all metadata keys (r=" << r
<< ")"));
2241 sync_status
.sync_info
.state
= rgw_meta_sync_info::StateSync
;
2242 r
= store_sync_info(sync_status
.sync_info
);
2244 tn
->log(0, SSTR("ERROR: failed to update sync status (r=" << r
<< ")"));
2248 case rgw_meta_sync_info::StateSync
:
2249 tn
->log(20, "sync");
2250 // find our position in the period history (if any)
2251 cursor
= get_period_at(store
, sync_status
.sync_info
);
2252 r
= cursor
.get_error();
2256 meta_sync_cr
= new RGWMetaSyncCR(&sync_env
, cursor
, sync_status
, tn
);
2257 r
= run(meta_sync_cr
);
2259 tn
->log(0, "ERROR: failed to fetch all metadata keys");
2264 tn
->log(0, "ERROR: bad sync state!");
2267 } while (!going_down
);
2272 void RGWRemoteMetaLog::wakeup(int shard_id
)
2274 if (!meta_sync_cr
) {
2277 meta_sync_cr
->wakeup(shard_id
);
2280 int RGWCloneMetaLogCoroutine::operate()
2285 ldpp_dout(sync_env
->dpp
, 20) << __func__
<< ": shard_id=" << shard_id
<< ": init request" << dendl
;
2286 return state_init();
2289 ldpp_dout(sync_env
->dpp
, 20) << __func__
<< ": shard_id=" << shard_id
<< ": reading shard status" << dendl
;
2290 return state_read_shard_status();
2293 ldpp_dout(sync_env
->dpp
, 20) << __func__
<< ": shard_id=" << shard_id
<< ": reading shard status complete" << dendl
;
2294 return state_read_shard_status_complete();
2297 ldpp_dout(sync_env
->dpp
, 20) << __func__
<< ": shard_id=" << shard_id
<< ": sending rest request" << dendl
;
2298 return state_send_rest_request();
2301 ldpp_dout(sync_env
->dpp
, 20) << __func__
<< ": shard_id=" << shard_id
<< ": receiving rest response" << dendl
;
2302 return state_receive_rest_response();
2305 ldpp_dout(sync_env
->dpp
, 20) << __func__
<< ": shard_id=" << shard_id
<< ": storing mdlog entries" << dendl
;
2306 return state_store_mdlog_entries();
2308 } while (truncated
);
2310 ldpp_dout(sync_env
->dpp
, 20) << __func__
<< ": shard_id=" << shard_id
<< ": storing mdlog entries complete" << dendl
;
2311 return state_store_mdlog_entries_complete();
2318 int RGWCloneMetaLogCoroutine::state_init()
2320 data
= rgw_mdlog_shard_data();
2325 int RGWCloneMetaLogCoroutine::state_read_shard_status()
2327 const bool add_ref
= false; // default constructs with refs=1
2329 completion
.reset(new RGWMetadataLogInfoCompletion(
2330 [this](int ret
, const cls_log_header
& header
) {
2332 if (ret
!= -ENOENT
) {
2333 ldpp_dout(sync_env
->dpp
, 1) << "ERROR: failed to read mdlog info with "
2334 << cpp_strerror(ret
) << dendl
;
2337 shard_info
.marker
= header
.max_marker
;
2338 shard_info
.last_update
= header
.max_time
.to_real_time();
2340 // wake up parent stack
2344 int ret
= mdlog
->get_info_async(shard_id
, completion
.get());
2346 ldpp_dout(sync_env
->dpp
, 0) << "ERROR: mdlog->get_info_async() returned ret=" << ret
<< dendl
;
2347 return set_cr_error(ret
);
2353 int RGWCloneMetaLogCoroutine::state_read_shard_status_complete()
2357 ldpp_dout(sync_env
->dpp
, 20) << "shard_id=" << shard_id
<< " marker=" << shard_info
.marker
<< " last_update=" << shard_info
.last_update
<< dendl
;
2359 marker
= shard_info
.marker
;
2364 int RGWCloneMetaLogCoroutine::state_send_rest_request()
2366 RGWRESTConn
*conn
= sync_env
->conn
;
2369 snprintf(buf
, sizeof(buf
), "%d", shard_id
);
2371 char max_entries_buf
[32];
2372 snprintf(max_entries_buf
, sizeof(max_entries_buf
), "%d", max_entries
);
2374 const char *marker_key
= (marker
.empty() ? "" : "marker");
2376 rgw_http_param_pair pairs
[] = { { "type", "metadata" },
2378 { "period", period
.c_str() },
2379 { "max-entries", max_entries_buf
},
2380 { marker_key
, marker
.c_str() },
2383 http_op
= new RGWRESTReadResource(conn
, "/admin/log", pairs
, NULL
, sync_env
->http_manager
);
2385 init_new_io(http_op
);
2387 int ret
= http_op
->aio_read();
2389 ldpp_dout(sync_env
->dpp
, 0) << "ERROR: failed to fetch mdlog data" << dendl
;
2390 log_error() << "failed to send http operation: " << http_op
->to_str() << " ret=" << ret
<< std::endl
;
2393 return set_cr_error(ret
);
2399 int RGWCloneMetaLogCoroutine::state_receive_rest_response()
2401 int ret
= http_op
->wait(&data
, null_yield
);
2403 error_stream
<< "http operation failed: " << http_op
->to_str() << " status=" << http_op
->get_http_status() << std::endl
;
2404 ldpp_dout(sync_env
->dpp
, 5) << "failed to wait for op, ret=" << ret
<< dendl
;
2407 return set_cr_error(ret
);
2412 ldpp_dout(sync_env
->dpp
, 20) << "remote mdlog, shard_id=" << shard_id
<< " num of shard entries: " << data
.entries
.size() << dendl
;
2414 truncated
= ((int)data
.entries
.size() == max_entries
);
2416 if (data
.entries
.empty()) {
2418 *new_marker
= marker
;
2420 return set_cr_done();
2424 *new_marker
= data
.entries
.back().id
;
2431 int RGWCloneMetaLogCoroutine::state_store_mdlog_entries()
2433 list
<cls_log_entry
> dest_entries
;
2435 vector
<rgw_mdlog_entry
>::iterator iter
;
2436 for (iter
= data
.entries
.begin(); iter
!= data
.entries
.end(); ++iter
) {
2437 rgw_mdlog_entry
& entry
= *iter
;
2438 ldpp_dout(sync_env
->dpp
, 20) << "entry: name=" << entry
.name
<< dendl
;
2440 cls_log_entry dest_entry
;
2441 dest_entry
.id
= entry
.id
;
2442 dest_entry
.section
= entry
.section
;
2443 dest_entry
.name
= entry
.name
;
2444 dest_entry
.timestamp
= utime_t(entry
.timestamp
);
2446 encode(entry
.log_data
, dest_entry
.data
);
2448 dest_entries
.push_back(dest_entry
);
2453 RGWAioCompletionNotifier
*cn
= stack
->create_completion_notifier();
2455 int ret
= mdlog
->store_entries_in_shard(dest_entries
, shard_id
, cn
->completion());
2458 ldpp_dout(sync_env
->dpp
, 10) << "failed to store md log entries shard_id=" << shard_id
<< " ret=" << ret
<< dendl
;
2459 return set_cr_error(ret
);
2464 int RGWCloneMetaLogCoroutine::state_store_mdlog_entries_complete()
2466 return set_cr_done();