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 MirrorPeerClientMeta
*client_meta
,
57 rbd::mirror::ProgressContext
*progress_ctx
)
58 : BaseRequest("rbd::mirror::image_replayer::BootstrapRequest",
59 reinterpret_cast<CephContext
*>(local_io_ctx
.cct()), on_finish
),
60 m_local_io_ctx(local_io_ctx
), m_remote_io_ctx(remote_io_ctx
),
61 m_instance_watcher(instance_watcher
), m_local_image_ctx(local_image_ctx
),
62 m_local_image_id(local_image_id
), m_remote_image_id(remote_image_id
),
63 m_global_image_id(global_image_id
), m_work_queue(work_queue
),
64 m_timer(timer
), m_timer_lock(timer_lock
),
65 m_local_mirror_uuid(local_mirror_uuid
),
66 m_remote_mirror_uuid(remote_mirror_uuid
), m_journaler(journaler
),
67 m_client_meta(client_meta
), m_progress_ctx(progress_ctx
),
68 m_do_resync(do_resync
),
69 m_lock(unique_lock_name("BootstrapRequest::m_lock", this)) {
73 BootstrapRequest
<I
>::~BootstrapRequest() {
74 assert(m_remote_image_ctx
== nullptr);
78 bool BootstrapRequest
<I
>::is_syncing() const {
79 Mutex::Locker
locker(m_lock
);
80 return (m_image_sync
!= nullptr);
84 void BootstrapRequest
<I
>::send() {
87 get_remote_tag_class();
91 void BootstrapRequest
<I
>::cancel() {
94 Mutex::Locker
locker(m_lock
);
97 if (m_image_sync
!= nullptr) {
98 m_image_sync
->cancel();
102 template <typename I
>
103 void BootstrapRequest
<I
>::get_remote_tag_class() {
106 update_progress("GET_REMOTE_TAG_CLASS");
108 Context
*ctx
= create_context_callback
<
109 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_get_remote_tag_class
>(
111 m_journaler
->get_client(librbd::Journal
<>::IMAGE_CLIENT_ID
, &m_client
, ctx
);
114 template <typename I
>
115 void BootstrapRequest
<I
>::handle_get_remote_tag_class(int r
) {
116 dout(20) << ": r=" << r
<< dendl
;
119 derr
<< ": failed to retrieve remote client: " << cpp_strerror(r
) << dendl
;
124 librbd::journal::ClientData client_data
;
125 bufferlist::iterator it
= m_client
.data
.begin();
127 ::decode(client_data
, it
);
128 } catch (const buffer::error
&err
) {
129 derr
<< ": failed to decode remote client meta data: " << err
.what()
135 librbd::journal::ImageClientMeta
*client_meta
=
136 boost::get
<librbd::journal::ImageClientMeta
>(&client_data
.client_meta
);
137 if (client_meta
== nullptr) {
138 derr
<< ": unknown remote client registration" << dendl
;
143 m_remote_tag_class
= client_meta
->tag_class
;
144 dout(10) << ": remote tag class=" << m_remote_tag_class
<< dendl
;
149 template <typename I
>
150 void BootstrapRequest
<I
>::get_client() {
153 update_progress("GET_CLIENT");
155 Context
*ctx
= create_context_callback
<
156 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_get_client
>(
158 m_journaler
->get_client(m_local_mirror_uuid
, &m_client
, ctx
);
161 template <typename I
>
162 void BootstrapRequest
<I
>::handle_get_client(int r
) {
163 dout(20) << ": r=" << r
<< dendl
;
166 dout(10) << ": client not registered" << dendl
;
168 derr
<< ": failed to retrieve client: " << cpp_strerror(r
) << dendl
;
171 } else if (decode_client_meta()) {
172 // skip registration if it already exists
180 template <typename I
>
181 void BootstrapRequest
<I
>::register_client() {
184 update_progress("REGISTER_CLIENT");
186 // record an place-holder record
187 librbd::journal::ClientData client_data
{
188 librbd::journal::MirrorPeerClientMeta
{m_local_image_id
}};
189 bufferlist client_data_bl
;
190 ::encode(client_data
, client_data_bl
);
192 Context
*ctx
= create_context_callback
<
193 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_register_client
>(
195 m_journaler
->register_client(client_data_bl
, ctx
);
198 template <typename I
>
199 void BootstrapRequest
<I
>::handle_register_client(int r
) {
200 dout(20) << ": r=" << r
<< dendl
;
203 derr
<< ": failed to register with remote journal: " << cpp_strerror(r
)
209 *m_client_meta
= librbd::journal::MirrorPeerClientMeta(m_local_image_id
);
213 template <typename I
>
214 void BootstrapRequest
<I
>::open_remote_image() {
217 update_progress("OPEN_REMOTE_IMAGE");
219 Context
*ctx
= create_context_callback
<
220 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_open_remote_image
>(
222 OpenImageRequest
<I
> *request
= OpenImageRequest
<I
>::create(
223 m_remote_io_ctx
, &m_remote_image_ctx
, m_remote_image_id
, false,
228 template <typename I
>
229 void BootstrapRequest
<I
>::handle_open_remote_image(int r
) {
230 dout(20) << ": r=" << r
<< dendl
;
233 derr
<< ": failed to open remote image: " << cpp_strerror(r
) << dendl
;
234 assert(m_remote_image_ctx
== nullptr);
242 template <typename I
>
243 void BootstrapRequest
<I
>::is_primary() {
246 update_progress("OPEN_REMOTE_IMAGE");
248 Context
*ctx
= create_context_callback
<
249 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_is_primary
>(
251 IsPrimaryRequest
<I
> *request
= IsPrimaryRequest
<I
>::create(m_remote_image_ctx
,
256 template <typename I
>
257 void BootstrapRequest
<I
>::handle_is_primary(int r
) {
258 dout(20) << ": r=" << r
<< dendl
;
261 derr
<< ": error querying remote image primary status: " << cpp_strerror(r
)
264 close_remote_image();
269 dout(5) << ": remote image is not primary -- skipping image replay"
271 m_ret_val
= -EREMOTEIO
;
272 update_client_state();
276 if (m_local_image_id
.empty()) {
277 create_local_image();
284 template <typename I
>
285 void BootstrapRequest
<I
>::update_client_state() {
286 if (m_client_meta
->state
== librbd::journal::MIRROR_PEER_STATE_REPLAYING
) {
287 // state already set for replaying upon failover
288 close_remote_image();
293 update_progress("UPDATE_CLIENT_STATE");
295 librbd::journal::MirrorPeerClientMeta
client_meta(*m_client_meta
);
296 client_meta
.state
= librbd::journal::MIRROR_PEER_STATE_REPLAYING
;
298 librbd::journal::ClientData
client_data(client_meta
);
300 ::encode(client_data
, data_bl
);
302 Context
*ctx
= create_context_callback
<
303 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_update_client_state
>(
305 m_journaler
->update_client(data_bl
, ctx
);
308 template <typename I
>
309 void BootstrapRequest
<I
>::handle_update_client_state(int r
) {
310 dout(20) << ": r=" << r
<< dendl
;
312 derr
<< ": failed to update client: " << cpp_strerror(r
) << dendl
;
314 m_client_meta
->state
= librbd::journal::MIRROR_PEER_STATE_REPLAYING
;;
317 close_remote_image();
320 template <typename I
>
321 void BootstrapRequest
<I
>::open_local_image() {
324 update_progress("OPEN_LOCAL_IMAGE");
326 Context
*ctx
= create_context_callback
<
327 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_open_local_image
>(
329 OpenLocalImageRequest
<I
> *request
= OpenLocalImageRequest
<I
>::create(
330 m_local_io_ctx
, m_local_image_ctx
, m_local_image_id
, m_work_queue
,
335 template <typename I
>
336 void BootstrapRequest
<I
>::handle_open_local_image(int r
) {
337 dout(20) << ": r=" << r
<< dendl
;
340 assert(*m_local_image_ctx
== nullptr);
341 dout(10) << ": local image missing" << dendl
;
342 create_local_image();
344 } else if (r
== -EREMOTEIO
) {
345 assert(*m_local_image_ctx
== nullptr);
346 dout(10) << "local image is primary -- skipping image replay" << dendl
;
348 close_remote_image();
351 assert(*m_local_image_ctx
== nullptr);
352 derr
<< ": failed to open local image: " << cpp_strerror(r
) << dendl
;
354 close_remote_image();
358 I
*local_image_ctx
= (*m_local_image_ctx
);
360 RWLock::RLocker
snap_locker(local_image_ctx
->snap_lock
);
361 if (local_image_ctx
->journal
== nullptr) {
362 derr
<< ": local image does not support journaling" << dendl
;
368 r
= (*m_local_image_ctx
)->journal
->is_resync_requested(m_do_resync
);
370 derr
<< ": failed to check if a resync was requested" << dendl
;
378 close_remote_image();
382 if (m_client
.state
== cls::journal::CLIENT_STATE_DISCONNECTED
) {
383 dout(10) << ": client flagged disconnected -- skipping bootstrap" << dendl
;
384 // The caller is expected to detect disconnect initializing remote journal.
386 close_remote_image();
390 update_client_image();
393 template <typename I
>
394 void BootstrapRequest
<I
>::create_local_image() {
397 m_local_image_id
= "";
398 update_progress("CREATE_LOCAL_IMAGE");
400 m_remote_image_ctx
->snap_lock
.get_read();
401 std::string image_name
= m_remote_image_ctx
->name
;
402 m_remote_image_ctx
->snap_lock
.put_read();
404 Context
*ctx
= create_context_callback
<
405 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_create_local_image
>(
407 CreateImageRequest
<I
> *request
= CreateImageRequest
<I
>::create(
408 m_local_io_ctx
, m_work_queue
, m_global_image_id
, m_remote_mirror_uuid
,
409 image_name
, m_remote_image_ctx
, &m_local_image_id
, ctx
);
413 template <typename I
>
414 void BootstrapRequest
<I
>::handle_create_local_image(int r
) {
415 dout(20) << ": r=" << r
<< dendl
;
418 derr
<< ": failed to create local image: " << cpp_strerror(r
) << dendl
;
420 close_remote_image();
427 template <typename I
>
428 void BootstrapRequest
<I
>::update_client_image() {
431 update_progress("UPDATE_CLIENT_IMAGE");
433 if (m_client_meta
->image_id
== (*m_local_image_ctx
)->id
) {
434 // already registered local image with remote journal
438 m_local_image_id
= (*m_local_image_ctx
)->id
;
442 librbd::journal::MirrorPeerClientMeta client_meta
{m_local_image_id
};
443 client_meta
.state
= librbd::journal::MIRROR_PEER_STATE_SYNCING
;
445 librbd::journal::ClientData
client_data(client_meta
);
447 ::encode(client_data
, data_bl
);
449 Context
*ctx
= create_context_callback
<
450 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_update_client_image
>(
452 m_journaler
->update_client(data_bl
, ctx
);
455 template <typename I
>
456 void BootstrapRequest
<I
>::handle_update_client_image(int r
) {
457 dout(20) << ": r=" << r
<< dendl
;
460 derr
<< ": failed to update client: " << cpp_strerror(r
) << dendl
;
467 dout(10) << ": request canceled" << dendl
;
468 m_ret_val
= -ECANCELED
;
473 *m_client_meta
= {m_local_image_id
};
474 m_client_meta
->state
= librbd::journal::MIRROR_PEER_STATE_SYNCING
;
478 template <typename I
>
479 void BootstrapRequest
<I
>::get_remote_tags() {
482 update_progress("GET_REMOTE_TAGS");
484 if (m_client_meta
->state
== librbd::journal::MIRROR_PEER_STATE_SYNCING
) {
485 // optimization -- no need to compare remote tags if we just created
486 // the image locally or sync was interrupted
493 Context
*ctx
= create_context_callback
<
494 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_get_remote_tags
>(this);
495 m_journaler
->get_tags(m_remote_tag_class
, &m_remote_tags
, ctx
);
498 template <typename I
>
499 void BootstrapRequest
<I
>::handle_get_remote_tags(int r
) {
500 dout(20) << ": r=" << r
<< dendl
;
503 derr
<< ": failed to retrieve remote tags: " << cpp_strerror(r
) << dendl
;
510 dout(10) << ": request canceled" << dendl
;
511 m_ret_val
= -ECANCELED
;
516 // At this point, the local image was existing, non-primary, and replaying;
517 // and the remote image is primary. Attempt to link the local image's most
518 // recent tag to the remote image's tag chain.
519 uint64_t local_tag_tid
;
520 librbd::journal::TagData local_tag_data
;
521 I
*local_image_ctx
= (*m_local_image_ctx
);
523 RWLock::RLocker
snap_locker(local_image_ctx
->snap_lock
);
524 if (local_image_ctx
->journal
== nullptr) {
525 derr
<< ": local image does not support journaling" << dendl
;
531 local_tag_tid
= local_image_ctx
->journal
->get_tag_tid();
532 local_tag_data
= local_image_ctx
->journal
->get_tag_data();
533 dout(20) << ": local tag " << local_tag_tid
<< ": "
534 << local_tag_data
<< dendl
;
537 bool remote_tag_data_valid
= false;
538 librbd::journal::TagData remote_tag_data
;
539 boost::optional
<uint64_t> remote_orphan_tag_tid
=
540 boost::make_optional
<uint64_t>(false, 0U);
541 bool reconnect_orphan
= false;
543 // decode the remote tags
544 for (auto &remote_tag
: m_remote_tags
) {
545 if (local_tag_data
.predecessor
.commit_valid
&&
546 local_tag_data
.predecessor
.mirror_uuid
== m_remote_mirror_uuid
&&
547 local_tag_data
.predecessor
.tag_tid
> remote_tag
.tid
) {
548 dout(20) << ": skipping processed predecessor remote tag "
549 << remote_tag
.tid
<< dendl
;
554 bufferlist::iterator it
= remote_tag
.data
.begin();
555 ::decode(remote_tag_data
, it
);
556 remote_tag_data_valid
= true;
557 } catch (const buffer::error
&err
) {
558 derr
<< ": failed to decode remote tag " << remote_tag
.tid
<< ": "
559 << err
.what() << dendl
;
560 m_ret_val
= -EBADMSG
;
565 dout(10) << ": decoded remote tag " << remote_tag
.tid
<< ": "
566 << remote_tag_data
<< dendl
;
568 if (!local_tag_data
.predecessor
.commit_valid
) {
569 // newly synced local image (no predecessor) replays from the first tag
570 if (remote_tag_data
.mirror_uuid
!= librbd::Journal
<>::LOCAL_MIRROR_UUID
) {
571 dout(20) << ": skipping non-primary remote tag" << dendl
;
575 dout(20) << ": using initial primary remote tag" << dendl
;
579 if (local_tag_data
.mirror_uuid
== librbd::Journal
<>::ORPHAN_MIRROR_UUID
) {
580 // demotion last available local epoch
582 if (remote_tag_data
.mirror_uuid
== local_tag_data
.mirror_uuid
&&
583 remote_tag_data
.predecessor
.commit_valid
&&
584 remote_tag_data
.predecessor
.tag_tid
==
585 local_tag_data
.predecessor
.tag_tid
) {
586 // demotion matches remote epoch
588 if (remote_tag_data
.predecessor
.mirror_uuid
== m_local_mirror_uuid
&&
589 local_tag_data
.predecessor
.mirror_uuid
==
590 librbd::Journal
<>::LOCAL_MIRROR_UUID
) {
591 // local demoted and remote has matching event
592 dout(20) << ": found matching local demotion tag" << dendl
;
593 remote_orphan_tag_tid
= remote_tag
.tid
;
597 if (local_tag_data
.predecessor
.mirror_uuid
== m_remote_mirror_uuid
&&
598 remote_tag_data
.predecessor
.mirror_uuid
==
599 librbd::Journal
<>::LOCAL_MIRROR_UUID
) {
600 // remote demoted and local has matching event
601 dout(20) << ": found matching remote demotion tag" << dendl
;
602 remote_orphan_tag_tid
= remote_tag
.tid
;
607 if (remote_tag_data
.mirror_uuid
== librbd::Journal
<>::LOCAL_MIRROR_UUID
&&
608 remote_tag_data
.predecessor
.mirror_uuid
== librbd::Journal
<>::ORPHAN_MIRROR_UUID
&&
609 remote_tag_data
.predecessor
.commit_valid
&& remote_orphan_tag_tid
&&
610 remote_tag_data
.predecessor
.tag_tid
== *remote_orphan_tag_tid
) {
611 // remote promotion tag chained to remote/local demotion tag
612 dout(20) << ": found chained remote promotion tag" << dendl
;
613 reconnect_orphan
= true;
617 // promotion must follow demotion
618 remote_orphan_tag_tid
= boost::none
;
622 if (remote_tag_data_valid
&&
623 local_tag_data
.mirror_uuid
== m_remote_mirror_uuid
) {
624 dout(20) << ": local image is in clean replay state" << dendl
;
625 } else if (reconnect_orphan
) {
626 dout(20) << ": remote image was demoted/promoted" << dendl
;
628 derr
<< ": split-brain detected -- skipping image replay" << dendl
;
637 template <typename I
>
638 void BootstrapRequest
<I
>::image_sync() {
639 if (m_client_meta
->state
== librbd::journal::MIRROR_PEER_STATE_REPLAYING
) {
640 // clean replay state -- no image sync required
641 close_remote_image();
647 Mutex::Locker
locker(m_lock
);
649 m_ret_val
= -ECANCELED
;
651 assert(m_image_sync
== nullptr);
653 Context
*ctx
= create_context_callback
<
654 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_image_sync
>(this);
655 m_image_sync
= ImageSync
<I
>::create(
656 *m_local_image_ctx
, m_remote_image_ctx
, m_timer
, m_timer_lock
,
657 m_local_mirror_uuid
, m_journaler
, m_client_meta
, m_work_queue
,
658 m_instance_watcher
, ctx
, m_progress_ctx
);
663 update_progress("IMAGE_SYNC");
666 m_image_sync
->send();
671 dout(10) << ": request canceled" << dendl
;
672 close_remote_image();
675 template <typename I
>
676 void BootstrapRequest
<I
>::handle_image_sync(int r
) {
677 dout(20) << ": r=" << r
<< dendl
;
680 Mutex::Locker
locker(m_lock
);
682 m_image_sync
= nullptr;
685 dout(10) << ": request canceled" << dendl
;
686 m_ret_val
= -ECANCELED
;
690 derr
<< ": failed to sync remote image: " << cpp_strerror(r
) << dendl
;
695 close_remote_image();
698 template <typename I
>
699 void BootstrapRequest
<I
>::close_local_image() {
702 update_progress("CLOSE_LOCAL_IMAGE");
704 Context
*ctx
= create_context_callback
<
705 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_close_local_image
>(
707 CloseImageRequest
<I
> *request
= CloseImageRequest
<I
>::create(
708 m_local_image_ctx
, ctx
);
712 template <typename I
>
713 void BootstrapRequest
<I
>::handle_close_local_image(int r
) {
714 dout(20) << ": r=" << r
<< dendl
;
717 derr
<< ": error encountered closing local image: " << cpp_strerror(r
)
721 close_remote_image();
724 template <typename I
>
725 void BootstrapRequest
<I
>::close_remote_image() {
728 update_progress("CLOSE_REMOTE_IMAGE");
730 Context
*ctx
= create_context_callback
<
731 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_close_remote_image
>(
733 CloseImageRequest
<I
> *request
= CloseImageRequest
<I
>::create(
734 &m_remote_image_ctx
, ctx
);
738 template <typename I
>
739 void BootstrapRequest
<I
>::handle_close_remote_image(int r
) {
740 dout(20) << ": r=" << r
<< dendl
;
743 derr
<< ": error encountered closing remote image: " << cpp_strerror(r
)
750 template <typename I
>
751 bool BootstrapRequest
<I
>::decode_client_meta() {
754 librbd::journal::ClientData client_data
;
755 bufferlist::iterator it
= m_client
.data
.begin();
757 ::decode(client_data
, it
);
758 } catch (const buffer::error
&err
) {
759 derr
<< ": failed to decode client meta data: " << err
.what() << dendl
;
763 librbd::journal::MirrorPeerClientMeta
*client_meta
=
764 boost::get
<librbd::journal::MirrorPeerClientMeta
>(&client_data
.client_meta
);
765 if (client_meta
== nullptr) {
766 derr
<< ": unknown peer registration" << dendl
;
768 } else if (!client_meta
->image_id
.empty()) {
769 // have an image id -- use that to open the image
770 m_local_image_id
= client_meta
->image_id
;
773 *m_client_meta
= *client_meta
;
775 dout(20) << ": client found: image_id=" << m_local_image_id
776 << ", client_meta=" << *m_client_meta
<< dendl
;
780 template <typename I
>
781 void BootstrapRequest
<I
>::update_progress(const std::string
&description
) {
782 dout(20) << ": " << description
<< dendl
;
784 if (m_progress_ctx
) {
785 m_progress_ctx
->update_progress(description
);
789 } // namespace image_replayer
790 } // namespace mirror
793 template class rbd::mirror::image_replayer::BootstrapRequest
<librbd::ImageCtx
>;