1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "include/compat.h"
5 #include "BootstrapRequest.h"
6 #include "CloseImageRequest.h"
7 #include "CreateImageRequest.h"
8 #include "IsPrimaryRequest.h"
9 #include "OpenImageRequest.h"
10 #include "OpenLocalImageRequest.h"
11 #include "common/debug.h"
12 #include "common/dout.h"
13 #include "common/errno.h"
14 #include "common/WorkQueue.h"
15 #include "cls/rbd/cls_rbd_client.h"
16 #include "journal/Journaler.h"
17 #include "librbd/ImageCtx.h"
18 #include "librbd/ImageState.h"
19 #include "librbd/internal.h"
20 #include "librbd/Journal.h"
21 #include "librbd/Utils.h"
22 #include "librbd/journal/Types.h"
23 #include "tools/rbd_mirror/ProgressContext.h"
24 #include "tools/rbd_mirror/ImageSync.h"
26 #define dout_context g_ceph_context
27 #define dout_subsys ceph_subsys_rbd_mirror
29 #define dout_prefix *_dout << "rbd::mirror::image_replayer::BootstrapRequest: " \
30 << this << " " << __func__
34 namespace image_replayer
{
36 using librbd::util::create_context_callback
;
37 using librbd::util::create_rados_callback
;
38 using librbd::util::unique_lock_name
;
41 BootstrapRequest
<I
>::BootstrapRequest(
42 librados::IoCtx
&local_io_ctx
,
43 librados::IoCtx
&remote_io_ctx
,
44 InstanceWatcher
<I
> *instance_watcher
,
46 const std::string
&local_image_id
,
47 const std::string
&remote_image_id
,
48 const std::string
&global_image_id
,
49 ContextWQ
*work_queue
, SafeTimer
*timer
,
51 const std::string
&local_mirror_uuid
,
52 const std::string
&remote_mirror_uuid
,
54 cls::journal::ClientState
*client_state
,
55 MirrorPeerClientMeta
*client_meta
,
58 rbd::mirror::ProgressContext
*progress_ctx
)
59 : BaseRequest("rbd::mirror::image_replayer::BootstrapRequest",
60 reinterpret_cast<CephContext
*>(local_io_ctx
.cct()), on_finish
),
61 m_local_io_ctx(local_io_ctx
), m_remote_io_ctx(remote_io_ctx
),
62 m_instance_watcher(instance_watcher
), m_local_image_ctx(local_image_ctx
),
63 m_local_image_id(local_image_id
), m_remote_image_id(remote_image_id
),
64 m_global_image_id(global_image_id
), m_work_queue(work_queue
),
65 m_timer(timer
), m_timer_lock(timer_lock
),
66 m_local_mirror_uuid(local_mirror_uuid
),
67 m_remote_mirror_uuid(remote_mirror_uuid
), m_journaler(journaler
),
68 m_client_state(client_state
), m_client_meta(client_meta
),
69 m_progress_ctx(progress_ctx
), m_do_resync(do_resync
),
70 m_lock(unique_lock_name("BootstrapRequest::m_lock", this)) {
74 BootstrapRequest
<I
>::~BootstrapRequest() {
75 assert(m_remote_image_ctx
== nullptr);
79 bool BootstrapRequest
<I
>::is_syncing() const {
80 Mutex::Locker
locker(m_lock
);
81 return (m_image_sync
!= nullptr);
85 void BootstrapRequest
<I
>::send() {
88 get_remote_tag_class();
92 void BootstrapRequest
<I
>::cancel() {
95 Mutex::Locker
locker(m_lock
);
98 if (m_image_sync
!= nullptr) {
99 m_image_sync
->cancel();
103 template <typename I
>
104 void BootstrapRequest
<I
>::get_remote_tag_class() {
107 update_progress("GET_REMOTE_TAG_CLASS");
109 Context
*ctx
= create_context_callback
<
110 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_get_remote_tag_class
>(
112 m_journaler
->get_client(librbd::Journal
<>::IMAGE_CLIENT_ID
, &m_client
, ctx
);
115 template <typename I
>
116 void BootstrapRequest
<I
>::handle_get_remote_tag_class(int r
) {
117 dout(20) << ": r=" << r
<< dendl
;
120 derr
<< ": failed to retrieve remote client: " << cpp_strerror(r
) << dendl
;
125 librbd::journal::ClientData client_data
;
126 bufferlist::iterator it
= m_client
.data
.begin();
128 ::decode(client_data
, it
);
129 } catch (const buffer::error
&err
) {
130 derr
<< ": failed to decode remote client meta data: " << err
.what()
136 librbd::journal::ImageClientMeta
*client_meta
=
137 boost::get
<librbd::journal::ImageClientMeta
>(&client_data
.client_meta
);
138 if (client_meta
== nullptr) {
139 derr
<< ": unknown remote client registration" << dendl
;
144 m_remote_tag_class
= client_meta
->tag_class
;
145 dout(10) << ": remote tag class=" << m_remote_tag_class
<< dendl
;
150 template <typename I
>
151 void BootstrapRequest
<I
>::open_remote_image() {
154 update_progress("OPEN_REMOTE_IMAGE");
156 Context
*ctx
= create_context_callback
<
157 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_open_remote_image
>(
159 OpenImageRequest
<I
> *request
= OpenImageRequest
<I
>::create(
160 m_remote_io_ctx
, &m_remote_image_ctx
, m_remote_image_id
, false,
165 template <typename I
>
166 void BootstrapRequest
<I
>::handle_open_remote_image(int r
) {
167 dout(20) << ": r=" << r
<< dendl
;
170 derr
<< ": failed to open remote image: " << cpp_strerror(r
) << dendl
;
171 assert(m_remote_image_ctx
== nullptr);
179 template <typename I
>
180 void BootstrapRequest
<I
>::is_primary() {
183 update_progress("OPEN_REMOTE_IMAGE");
185 Context
*ctx
= create_context_callback
<
186 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_is_primary
>(
188 IsPrimaryRequest
<I
> *request
= IsPrimaryRequest
<I
>::create(m_remote_image_ctx
,
193 template <typename I
>
194 void BootstrapRequest
<I
>::handle_is_primary(int r
) {
195 dout(20) << ": r=" << r
<< dendl
;
198 derr
<< ": error querying remote image primary status: " << cpp_strerror(r
)
201 close_remote_image();
206 dout(5) << ": remote image is not primary -- skipping image replay"
208 m_ret_val
= -EREMOTEIO
;
209 update_client_state();
213 if (!m_client_meta
->image_id
.empty()) {
214 // have an image id -- use that to open the image since a deletion (resync)
215 // will leave the old image id registered in the peer
216 m_local_image_id
= m_client_meta
->image_id
;
219 if (m_local_image_id
.empty()) {
220 update_client_image();
227 template <typename I
>
228 void BootstrapRequest
<I
>::update_client_state() {
229 if (m_client_meta
->state
== librbd::journal::MIRROR_PEER_STATE_REPLAYING
) {
230 // state already set for replaying upon failover
231 close_remote_image();
236 update_progress("UPDATE_CLIENT_STATE");
238 librbd::journal::MirrorPeerClientMeta
client_meta(*m_client_meta
);
239 client_meta
.state
= librbd::journal::MIRROR_PEER_STATE_REPLAYING
;
241 librbd::journal::ClientData
client_data(client_meta
);
243 ::encode(client_data
, data_bl
);
245 Context
*ctx
= create_context_callback
<
246 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_update_client_state
>(
248 m_journaler
->update_client(data_bl
, ctx
);
251 template <typename I
>
252 void BootstrapRequest
<I
>::handle_update_client_state(int r
) {
253 dout(20) << ": r=" << r
<< dendl
;
255 derr
<< ": failed to update client: " << cpp_strerror(r
) << dendl
;
257 m_client_meta
->state
= librbd::journal::MIRROR_PEER_STATE_REPLAYING
;
260 close_remote_image();
263 template <typename I
>
264 void BootstrapRequest
<I
>::open_local_image() {
267 update_progress("OPEN_LOCAL_IMAGE");
269 Context
*ctx
= create_context_callback
<
270 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_open_local_image
>(
272 OpenLocalImageRequest
<I
> *request
= OpenLocalImageRequest
<I
>::create(
273 m_local_io_ctx
, m_local_image_ctx
, m_local_image_id
, m_work_queue
,
278 template <typename I
>
279 void BootstrapRequest
<I
>::handle_open_local_image(int r
) {
280 dout(20) << ": r=" << r
<< dendl
;
283 assert(*m_local_image_ctx
== nullptr);
284 dout(10) << ": local image missing" << dendl
;
287 } else if (r
== -EREMOTEIO
) {
288 assert(*m_local_image_ctx
== nullptr);
289 dout(10) << "local image is primary -- skipping image replay" << dendl
;
291 close_remote_image();
294 assert(*m_local_image_ctx
== nullptr);
295 derr
<< ": failed to open local image: " << cpp_strerror(r
) << dendl
;
297 close_remote_image();
301 I
*local_image_ctx
= (*m_local_image_ctx
);
303 RWLock::RLocker
snap_locker(local_image_ctx
->snap_lock
);
304 if (local_image_ctx
->journal
== nullptr) {
305 derr
<< ": local image does not support journaling" << dendl
;
311 r
= (*m_local_image_ctx
)->journal
->is_resync_requested(m_do_resync
);
313 derr
<< ": failed to check if a resync was requested" << dendl
;
321 close_remote_image();
325 if (*m_client_state
== cls::journal::CLIENT_STATE_DISCONNECTED
) {
326 dout(10) << ": client flagged disconnected -- skipping bootstrap" << dendl
;
327 // The caller is expected to detect disconnect initializing remote journal.
329 close_remote_image();
336 template <typename I
>
337 void BootstrapRequest
<I
>::unregister_client() {
339 update_progress("UNREGISTER_CLIENT");
341 m_local_image_id
= "";
342 Context
*ctx
= create_context_callback
<
343 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_unregister_client
>(
345 m_journaler
->unregister_client(ctx
);
348 template <typename I
>
349 void BootstrapRequest
<I
>::handle_unregister_client(int r
) {
350 dout(20) << ": r=" << r
<< dendl
;
352 derr
<< ": failed to unregister with remote journal: " << cpp_strerror(r
)
355 close_remote_image();
359 *m_client_meta
= librbd::journal::MirrorPeerClientMeta("");
363 template <typename I
>
364 void BootstrapRequest
<I
>::register_client() {
367 update_progress("REGISTER_CLIENT");
369 librbd::journal::MirrorPeerClientMeta mirror_peer_client_meta
{
371 mirror_peer_client_meta
.state
= librbd::journal::MIRROR_PEER_STATE_REPLAYING
;
373 librbd::journal::ClientData client_data
{mirror_peer_client_meta
};
374 bufferlist client_data_bl
;
375 ::encode(client_data
, client_data_bl
);
377 Context
*ctx
= create_context_callback
<
378 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_register_client
>(
380 m_journaler
->register_client(client_data_bl
, ctx
);
383 template <typename I
>
384 void BootstrapRequest
<I
>::handle_register_client(int r
) {
385 dout(20) << ": r=" << r
<< dendl
;
388 derr
<< ": failed to register with remote journal: " << cpp_strerror(r
)
391 close_remote_image();
395 *m_client_state
= cls::journal::CLIENT_STATE_CONNECTED
;
396 *m_client_meta
= librbd::journal::MirrorPeerClientMeta(m_local_image_id
);
397 m_client_meta
->state
= librbd::journal::MIRROR_PEER_STATE_REPLAYING
;
402 template <typename I
>
403 void BootstrapRequest
<I
>::update_client_image() {
405 update_progress("UPDATE_CLIENT_IMAGE");
407 assert(m_local_image_id
.empty());
408 m_local_image_id
= librbd::util::generate_image_id
<I
>(m_local_io_ctx
);
410 librbd::journal::MirrorPeerClientMeta client_meta
{m_local_image_id
};
411 client_meta
.state
= librbd::journal::MIRROR_PEER_STATE_SYNCING
;
413 librbd::journal::ClientData
client_data(client_meta
);
415 ::encode(client_data
, data_bl
);
417 Context
*ctx
= create_context_callback
<
418 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_update_client_image
>(
420 m_journaler
->update_client(data_bl
, ctx
);
423 template <typename I
>
424 void BootstrapRequest
<I
>::handle_update_client_image(int r
) {
425 dout(20) << ": r=" << r
<< dendl
;
428 derr
<< ": failed to update client: " << cpp_strerror(r
) << dendl
;
430 close_remote_image();
435 dout(10) << ": request canceled" << dendl
;
436 m_ret_val
= -ECANCELED
;
437 close_remote_image();
441 *m_client_meta
= {m_local_image_id
};
442 m_client_meta
->state
= librbd::journal::MIRROR_PEER_STATE_SYNCING
;
443 create_local_image();
446 template <typename I
>
447 void BootstrapRequest
<I
>::create_local_image() {
449 update_progress("CREATE_LOCAL_IMAGE");
451 m_remote_image_ctx
->snap_lock
.get_read();
452 std::string image_name
= m_remote_image_ctx
->name
;
453 m_remote_image_ctx
->snap_lock
.put_read();
455 Context
*ctx
= create_context_callback
<
456 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_create_local_image
>(
458 CreateImageRequest
<I
> *request
= CreateImageRequest
<I
>::create(
459 m_local_io_ctx
, m_work_queue
, m_global_image_id
, m_remote_mirror_uuid
,
460 image_name
, m_local_image_id
, m_remote_image_ctx
, ctx
);
464 template <typename I
>
465 void BootstrapRequest
<I
>::handle_create_local_image(int r
) {
466 dout(20) << ": r=" << r
<< dendl
;
469 derr
<< ": failed to create local image: " << cpp_strerror(r
) << dendl
;
471 close_remote_image();
478 template <typename I
>
479 void BootstrapRequest
<I
>::get_remote_tags() {
480 if (m_client_meta
->state
== librbd::journal::MIRROR_PEER_STATE_SYNCING
) {
481 // optimization -- no need to compare remote tags if we just created
482 // the image locally or sync was interrupted
488 update_progress("GET_REMOTE_TAGS");
490 Context
*ctx
= create_context_callback
<
491 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_get_remote_tags
>(this);
492 m_journaler
->get_tags(m_remote_tag_class
, &m_remote_tags
, ctx
);
495 template <typename I
>
496 void BootstrapRequest
<I
>::handle_get_remote_tags(int r
) {
497 dout(20) << ": r=" << r
<< dendl
;
500 derr
<< ": failed to retrieve remote tags: " << cpp_strerror(r
) << dendl
;
507 dout(10) << ": request canceled" << dendl
;
508 m_ret_val
= -ECANCELED
;
513 // At this point, the local image was existing, non-primary, and replaying;
514 // and the remote image is primary. Attempt to link the local image's most
515 // recent tag to the remote image's tag chain.
516 uint64_t local_tag_tid
;
517 librbd::journal::TagData local_tag_data
;
518 I
*local_image_ctx
= (*m_local_image_ctx
);
520 RWLock::RLocker
snap_locker(local_image_ctx
->snap_lock
);
521 if (local_image_ctx
->journal
== nullptr) {
522 derr
<< ": local image does not support journaling" << dendl
;
528 local_tag_tid
= local_image_ctx
->journal
->get_tag_tid();
529 local_tag_data
= local_image_ctx
->journal
->get_tag_data();
530 dout(20) << ": local tag " << local_tag_tid
<< ": "
531 << local_tag_data
<< dendl
;
534 bool remote_tag_data_valid
= false;
535 librbd::journal::TagData remote_tag_data
;
536 boost::optional
<uint64_t> remote_orphan_tag_tid
=
537 boost::make_optional
<uint64_t>(false, 0U);
538 bool reconnect_orphan
= false;
540 // decode the remote tags
541 for (auto &remote_tag
: m_remote_tags
) {
542 if (local_tag_data
.predecessor
.commit_valid
&&
543 local_tag_data
.predecessor
.mirror_uuid
== m_remote_mirror_uuid
&&
544 local_tag_data
.predecessor
.tag_tid
> remote_tag
.tid
) {
545 dout(20) << ": skipping processed predecessor remote tag "
546 << remote_tag
.tid
<< dendl
;
551 bufferlist::iterator it
= remote_tag
.data
.begin();
552 ::decode(remote_tag_data
, it
);
553 remote_tag_data_valid
= true;
554 } catch (const buffer::error
&err
) {
555 derr
<< ": failed to decode remote tag " << remote_tag
.tid
<< ": "
556 << err
.what() << dendl
;
557 m_ret_val
= -EBADMSG
;
562 dout(10) << ": decoded remote tag " << remote_tag
.tid
<< ": "
563 << remote_tag_data
<< dendl
;
565 if (!local_tag_data
.predecessor
.commit_valid
) {
566 // newly synced local image (no predecessor) replays from the first tag
567 if (remote_tag_data
.mirror_uuid
!= librbd::Journal
<>::LOCAL_MIRROR_UUID
) {
568 dout(20) << ": skipping non-primary remote tag" << dendl
;
572 dout(20) << ": using initial primary remote tag" << dendl
;
576 if (local_tag_data
.mirror_uuid
== librbd::Journal
<>::ORPHAN_MIRROR_UUID
) {
577 // demotion last available local epoch
579 if (remote_tag_data
.mirror_uuid
== local_tag_data
.mirror_uuid
&&
580 remote_tag_data
.predecessor
.commit_valid
&&
581 remote_tag_data
.predecessor
.tag_tid
==
582 local_tag_data
.predecessor
.tag_tid
) {
583 // demotion matches remote epoch
585 if (remote_tag_data
.predecessor
.mirror_uuid
== m_local_mirror_uuid
&&
586 local_tag_data
.predecessor
.mirror_uuid
==
587 librbd::Journal
<>::LOCAL_MIRROR_UUID
) {
588 // local demoted and remote has matching event
589 dout(20) << ": found matching local demotion tag" << dendl
;
590 remote_orphan_tag_tid
= remote_tag
.tid
;
594 if (local_tag_data
.predecessor
.mirror_uuid
== m_remote_mirror_uuid
&&
595 remote_tag_data
.predecessor
.mirror_uuid
==
596 librbd::Journal
<>::LOCAL_MIRROR_UUID
) {
597 // remote demoted and local has matching event
598 dout(20) << ": found matching remote demotion tag" << dendl
;
599 remote_orphan_tag_tid
= remote_tag
.tid
;
604 if (remote_tag_data
.mirror_uuid
== librbd::Journal
<>::LOCAL_MIRROR_UUID
&&
605 remote_tag_data
.predecessor
.mirror_uuid
== librbd::Journal
<>::ORPHAN_MIRROR_UUID
&&
606 remote_tag_data
.predecessor
.commit_valid
&& remote_orphan_tag_tid
&&
607 remote_tag_data
.predecessor
.tag_tid
== *remote_orphan_tag_tid
) {
608 // remote promotion tag chained to remote/local demotion tag
609 dout(20) << ": found chained remote promotion tag" << dendl
;
610 reconnect_orphan
= true;
614 // promotion must follow demotion
615 remote_orphan_tag_tid
= boost::none
;
619 if (remote_tag_data_valid
&&
620 local_tag_data
.mirror_uuid
== m_remote_mirror_uuid
) {
621 dout(20) << ": local image is in clean replay state" << dendl
;
622 } else if (reconnect_orphan
) {
623 dout(20) << ": remote image was demoted/promoted" << dendl
;
625 derr
<< ": split-brain detected -- skipping image replay" << dendl
;
634 template <typename I
>
635 void BootstrapRequest
<I
>::image_sync() {
636 if (m_client_meta
->state
== librbd::journal::MIRROR_PEER_STATE_REPLAYING
) {
637 // clean replay state -- no image sync required
638 close_remote_image();
644 Mutex::Locker
locker(m_lock
);
646 m_ret_val
= -ECANCELED
;
648 assert(m_image_sync
== nullptr);
650 Context
*ctx
= create_context_callback
<
651 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_image_sync
>(this);
652 m_image_sync
= ImageSync
<I
>::create(
653 *m_local_image_ctx
, m_remote_image_ctx
, m_timer
, m_timer_lock
,
654 m_local_mirror_uuid
, m_journaler
, m_client_meta
, m_work_queue
,
655 m_instance_watcher
, ctx
, m_progress_ctx
);
660 update_progress("IMAGE_SYNC");
663 m_image_sync
->send();
668 dout(10) << ": request canceled" << dendl
;
669 close_remote_image();
672 template <typename I
>
673 void BootstrapRequest
<I
>::handle_image_sync(int r
) {
674 dout(20) << ": r=" << r
<< dendl
;
677 Mutex::Locker
locker(m_lock
);
679 m_image_sync
= nullptr;
682 dout(10) << ": request canceled" << dendl
;
683 m_ret_val
= -ECANCELED
;
687 derr
<< ": failed to sync remote image: " << cpp_strerror(r
) << dendl
;
692 close_remote_image();
695 template <typename I
>
696 void BootstrapRequest
<I
>::close_local_image() {
699 update_progress("CLOSE_LOCAL_IMAGE");
701 Context
*ctx
= create_context_callback
<
702 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_close_local_image
>(
704 CloseImageRequest
<I
> *request
= CloseImageRequest
<I
>::create(
705 m_local_image_ctx
, ctx
);
709 template <typename I
>
710 void BootstrapRequest
<I
>::handle_close_local_image(int r
) {
711 dout(20) << ": r=" << r
<< dendl
;
714 derr
<< ": error encountered closing local image: " << cpp_strerror(r
)
718 close_remote_image();
721 template <typename I
>
722 void BootstrapRequest
<I
>::close_remote_image() {
725 update_progress("CLOSE_REMOTE_IMAGE");
727 Context
*ctx
= create_context_callback
<
728 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_close_remote_image
>(
730 CloseImageRequest
<I
> *request
= CloseImageRequest
<I
>::create(
731 &m_remote_image_ctx
, ctx
);
735 template <typename I
>
736 void BootstrapRequest
<I
>::handle_close_remote_image(int r
) {
737 dout(20) << ": r=" << r
<< dendl
;
740 derr
<< ": error encountered closing remote image: " << cpp_strerror(r
)
747 template <typename I
>
748 void BootstrapRequest
<I
>::update_progress(const std::string
&description
) {
749 dout(20) << ": " << description
<< dendl
;
751 if (m_progress_ctx
) {
752 m_progress_ctx
->update_progress(description
);
756 } // namespace image_replayer
757 } // namespace mirror
760 template class rbd::mirror::image_replayer::BootstrapRequest
<librbd::ImageCtx
>;