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 void BootstrapRequest
<I
>::send() {
81 get_remote_tag_class();
85 void BootstrapRequest
<I
>::cancel() {
88 Mutex::Locker
locker(m_lock
);
91 if (m_image_sync
!= nullptr) {
92 m_image_sync
->cancel();
97 void BootstrapRequest
<I
>::get_remote_tag_class() {
100 update_progress("GET_REMOTE_TAG_CLASS");
102 Context
*ctx
= create_context_callback
<
103 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_get_remote_tag_class
>(
105 m_journaler
->get_client(librbd::Journal
<>::IMAGE_CLIENT_ID
, &m_client
, ctx
);
108 template <typename I
>
109 void BootstrapRequest
<I
>::handle_get_remote_tag_class(int r
) {
110 dout(20) << ": r=" << r
<< dendl
;
113 derr
<< ": failed to retrieve remote client: " << cpp_strerror(r
) << dendl
;
118 librbd::journal::ClientData client_data
;
119 bufferlist::iterator it
= m_client
.data
.begin();
121 ::decode(client_data
, it
);
122 } catch (const buffer::error
&err
) {
123 derr
<< ": failed to decode remote client meta data: " << err
.what()
129 librbd::journal::ImageClientMeta
*client_meta
=
130 boost::get
<librbd::journal::ImageClientMeta
>(&client_data
.client_meta
);
131 if (client_meta
== nullptr) {
132 derr
<< ": unknown remote client registration" << dendl
;
137 m_remote_tag_class
= client_meta
->tag_class
;
138 dout(10) << ": remote tag class=" << m_remote_tag_class
<< dendl
;
143 template <typename I
>
144 void BootstrapRequest
<I
>::get_client() {
147 update_progress("GET_CLIENT");
149 Context
*ctx
= create_context_callback
<
150 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_get_client
>(
152 m_journaler
->get_client(m_local_mirror_uuid
, &m_client
, ctx
);
155 template <typename I
>
156 void BootstrapRequest
<I
>::handle_get_client(int r
) {
157 dout(20) << ": r=" << r
<< dendl
;
160 dout(10) << ": client not registered" << dendl
;
162 derr
<< ": failed to retrieve client: " << cpp_strerror(r
) << dendl
;
165 } else if (decode_client_meta()) {
166 // skip registration if it already exists
174 template <typename I
>
175 void BootstrapRequest
<I
>::register_client() {
178 update_progress("REGISTER_CLIENT");
180 // record an place-holder record
181 librbd::journal::ClientData client_data
{
182 librbd::journal::MirrorPeerClientMeta
{m_local_image_id
}};
183 bufferlist client_data_bl
;
184 ::encode(client_data
, client_data_bl
);
186 Context
*ctx
= create_context_callback
<
187 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_register_client
>(
189 m_journaler
->register_client(client_data_bl
, ctx
);
192 template <typename I
>
193 void BootstrapRequest
<I
>::handle_register_client(int r
) {
194 dout(20) << ": r=" << r
<< dendl
;
197 derr
<< ": failed to register with remote journal: " << cpp_strerror(r
)
203 *m_client_meta
= librbd::journal::MirrorPeerClientMeta(m_local_image_id
);
207 template <typename I
>
208 void BootstrapRequest
<I
>::open_remote_image() {
211 update_progress("OPEN_REMOTE_IMAGE");
213 Context
*ctx
= create_context_callback
<
214 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_open_remote_image
>(
216 OpenImageRequest
<I
> *request
= OpenImageRequest
<I
>::create(
217 m_remote_io_ctx
, &m_remote_image_ctx
, m_remote_image_id
, false,
222 template <typename I
>
223 void BootstrapRequest
<I
>::handle_open_remote_image(int r
) {
224 dout(20) << ": r=" << r
<< dendl
;
227 derr
<< ": failed to open remote image: " << cpp_strerror(r
) << dendl
;
228 assert(m_remote_image_ctx
== nullptr);
236 template <typename I
>
237 void BootstrapRequest
<I
>::is_primary() {
240 update_progress("OPEN_REMOTE_IMAGE");
242 Context
*ctx
= create_context_callback
<
243 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_is_primary
>(
245 IsPrimaryRequest
<I
> *request
= IsPrimaryRequest
<I
>::create(m_remote_image_ctx
,
250 template <typename I
>
251 void BootstrapRequest
<I
>::handle_is_primary(int r
) {
252 dout(20) << ": r=" << r
<< dendl
;
255 derr
<< ": error querying remote image primary status: " << cpp_strerror(r
)
258 close_remote_image();
263 dout(5) << ": remote image is not primary -- skipping image replay"
265 m_ret_val
= -EREMOTEIO
;
266 update_client_state();
270 if (m_local_image_id
.empty()) {
271 create_local_image();
278 template <typename I
>
279 void BootstrapRequest
<I
>::update_client_state() {
280 if (m_client_meta
->state
== librbd::journal::MIRROR_PEER_STATE_REPLAYING
) {
281 // state already set for replaying upon failover
282 close_remote_image();
287 update_progress("UPDATE_CLIENT_STATE");
289 librbd::journal::MirrorPeerClientMeta
client_meta(*m_client_meta
);
290 client_meta
.state
= librbd::journal::MIRROR_PEER_STATE_REPLAYING
;
292 librbd::journal::ClientData
client_data(client_meta
);
294 ::encode(client_data
, data_bl
);
296 Context
*ctx
= create_context_callback
<
297 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_update_client_state
>(
299 m_journaler
->update_client(data_bl
, ctx
);
302 template <typename I
>
303 void BootstrapRequest
<I
>::handle_update_client_state(int r
) {
304 dout(20) << ": r=" << r
<< dendl
;
306 derr
<< ": failed to update client: " << cpp_strerror(r
) << dendl
;
308 m_client_meta
->state
= librbd::journal::MIRROR_PEER_STATE_REPLAYING
;;
311 close_remote_image();
314 template <typename I
>
315 void BootstrapRequest
<I
>::open_local_image() {
318 update_progress("OPEN_LOCAL_IMAGE");
320 Context
*ctx
= create_context_callback
<
321 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_open_local_image
>(
323 OpenLocalImageRequest
<I
> *request
= OpenLocalImageRequest
<I
>::create(
324 m_local_io_ctx
, m_local_image_ctx
, m_local_image_id
, m_work_queue
,
329 template <typename I
>
330 void BootstrapRequest
<I
>::handle_open_local_image(int r
) {
331 dout(20) << ": r=" << r
<< dendl
;
334 assert(*m_local_image_ctx
== nullptr);
335 dout(10) << ": local image missing" << dendl
;
336 create_local_image();
338 } else if (r
== -EREMOTEIO
) {
339 assert(*m_local_image_ctx
== nullptr);
340 dout(10) << "local image is primary -- skipping image replay" << dendl
;
342 close_remote_image();
345 assert(*m_local_image_ctx
== nullptr);
346 derr
<< ": failed to open local image: " << cpp_strerror(r
) << dendl
;
348 close_remote_image();
352 I
*local_image_ctx
= (*m_local_image_ctx
);
354 RWLock::RLocker
snap_locker(local_image_ctx
->snap_lock
);
355 if (local_image_ctx
->journal
== nullptr) {
356 derr
<< ": local image does not support journaling" << dendl
;
362 r
= (*m_local_image_ctx
)->journal
->is_resync_requested(m_do_resync
);
364 derr
<< ": failed to check if a resync was requested" << dendl
;
372 close_remote_image();
376 if (m_client
.state
== cls::journal::CLIENT_STATE_DISCONNECTED
) {
377 dout(10) << ": client flagged disconnected -- skipping bootstrap" << dendl
;
378 // The caller is expected to detect disconnect initializing remote journal.
380 close_remote_image();
384 update_client_image();
387 template <typename I
>
388 void BootstrapRequest
<I
>::create_local_image() {
391 m_local_image_id
= "";
392 update_progress("CREATE_LOCAL_IMAGE");
394 m_remote_image_ctx
->snap_lock
.get_read();
395 std::string image_name
= m_remote_image_ctx
->name
;
396 m_remote_image_ctx
->snap_lock
.put_read();
398 Context
*ctx
= create_context_callback
<
399 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_create_local_image
>(
401 CreateImageRequest
<I
> *request
= CreateImageRequest
<I
>::create(
402 m_local_io_ctx
, m_work_queue
, m_global_image_id
, m_remote_mirror_uuid
,
403 image_name
, m_remote_image_ctx
, &m_local_image_id
, ctx
);
407 template <typename I
>
408 void BootstrapRequest
<I
>::handle_create_local_image(int r
) {
409 dout(20) << ": r=" << r
<< dendl
;
412 derr
<< ": failed to create local image: " << cpp_strerror(r
) << dendl
;
414 close_remote_image();
421 template <typename I
>
422 void BootstrapRequest
<I
>::update_client_image() {
425 update_progress("UPDATE_CLIENT_IMAGE");
427 if (m_client_meta
->image_id
== (*m_local_image_ctx
)->id
) {
428 // already registered local image with remote journal
432 m_local_image_id
= (*m_local_image_ctx
)->id
;
436 librbd::journal::MirrorPeerClientMeta client_meta
{m_local_image_id
};
437 client_meta
.state
= librbd::journal::MIRROR_PEER_STATE_SYNCING
;
439 librbd::journal::ClientData
client_data(client_meta
);
441 ::encode(client_data
, data_bl
);
443 Context
*ctx
= create_context_callback
<
444 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_update_client_image
>(
446 m_journaler
->update_client(data_bl
, ctx
);
449 template <typename I
>
450 void BootstrapRequest
<I
>::handle_update_client_image(int r
) {
451 dout(20) << ": r=" << r
<< dendl
;
454 derr
<< ": failed to update client: " << cpp_strerror(r
) << dendl
;
461 dout(10) << ": request canceled" << dendl
;
462 m_ret_val
= -ECANCELED
;
467 *m_client_meta
= {m_local_image_id
};
468 m_client_meta
->state
= librbd::journal::MIRROR_PEER_STATE_SYNCING
;
472 template <typename I
>
473 void BootstrapRequest
<I
>::get_remote_tags() {
476 update_progress("GET_REMOTE_TAGS");
478 if (m_client_meta
->state
== librbd::journal::MIRROR_PEER_STATE_SYNCING
) {
479 // optimization -- no need to compare remote tags if we just created
480 // the image locally or sync was interrupted
487 Context
*ctx
= create_context_callback
<
488 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_get_remote_tags
>(this);
489 m_journaler
->get_tags(m_remote_tag_class
, &m_remote_tags
, ctx
);
492 template <typename I
>
493 void BootstrapRequest
<I
>::handle_get_remote_tags(int r
) {
494 dout(20) << ": r=" << r
<< dendl
;
497 derr
<< ": failed to retrieve remote tags: " << cpp_strerror(r
) << dendl
;
504 dout(10) << ": request canceled" << dendl
;
505 m_ret_val
= -ECANCELED
;
510 // At this point, the local image was existing, non-primary, and replaying;
511 // and the remote image is primary. Attempt to link the local image's most
512 // recent tag to the remote image's tag chain.
513 uint64_t local_tag_tid
;
514 librbd::journal::TagData local_tag_data
;
515 I
*local_image_ctx
= (*m_local_image_ctx
);
517 RWLock::RLocker
snap_locker(local_image_ctx
->snap_lock
);
518 if (local_image_ctx
->journal
== nullptr) {
519 derr
<< ": local image does not support journaling" << dendl
;
525 local_tag_tid
= local_image_ctx
->journal
->get_tag_tid();
526 local_tag_data
= local_image_ctx
->journal
->get_tag_data();
527 dout(20) << ": local tag " << local_tag_tid
<< ": "
528 << local_tag_data
<< dendl
;
531 bool remote_tag_data_valid
= false;
532 librbd::journal::TagData remote_tag_data
;
533 boost::optional
<uint64_t> remote_orphan_tag_tid
=
534 boost::make_optional
<uint64_t>(false, 0U);
535 bool reconnect_orphan
= false;
537 // decode the remote tags
538 for (auto &remote_tag
: m_remote_tags
) {
539 if (local_tag_data
.predecessor
.commit_valid
&&
540 local_tag_data
.predecessor
.mirror_uuid
== m_remote_mirror_uuid
&&
541 local_tag_data
.predecessor
.tag_tid
> remote_tag
.tid
) {
542 dout(20) << ": skipping processed predecessor remote tag "
543 << remote_tag
.tid
<< dendl
;
548 bufferlist::iterator it
= remote_tag
.data
.begin();
549 ::decode(remote_tag_data
, it
);
550 remote_tag_data_valid
= true;
551 } catch (const buffer::error
&err
) {
552 derr
<< ": failed to decode remote tag " << remote_tag
.tid
<< ": "
553 << err
.what() << dendl
;
554 m_ret_val
= -EBADMSG
;
559 dout(10) << ": decoded remote tag " << remote_tag
.tid
<< ": "
560 << remote_tag_data
<< dendl
;
562 if (!local_tag_data
.predecessor
.commit_valid
) {
563 // newly synced local image (no predecessor) replays from the first tag
564 if (remote_tag_data
.mirror_uuid
!= librbd::Journal
<>::LOCAL_MIRROR_UUID
) {
565 dout(20) << ": skipping non-primary remote tag" << dendl
;
569 dout(20) << ": using initial primary remote tag" << dendl
;
573 if (local_tag_data
.mirror_uuid
== librbd::Journal
<>::ORPHAN_MIRROR_UUID
) {
574 // demotion last available local epoch
576 if (remote_tag_data
.mirror_uuid
== local_tag_data
.mirror_uuid
&&
577 remote_tag_data
.predecessor
.commit_valid
&&
578 remote_tag_data
.predecessor
.tag_tid
==
579 local_tag_data
.predecessor
.tag_tid
) {
580 // demotion matches remote epoch
582 if (remote_tag_data
.predecessor
.mirror_uuid
== m_local_mirror_uuid
&&
583 local_tag_data
.predecessor
.mirror_uuid
==
584 librbd::Journal
<>::LOCAL_MIRROR_UUID
) {
585 // local demoted and remote has matching event
586 dout(20) << ": found matching local demotion tag" << dendl
;
587 remote_orphan_tag_tid
= remote_tag
.tid
;
591 if (local_tag_data
.predecessor
.mirror_uuid
== m_remote_mirror_uuid
&&
592 remote_tag_data
.predecessor
.mirror_uuid
==
593 librbd::Journal
<>::LOCAL_MIRROR_UUID
) {
594 // remote demoted and local has matching event
595 dout(20) << ": found matching remote demotion tag" << dendl
;
596 remote_orphan_tag_tid
= remote_tag
.tid
;
601 if (remote_tag_data
.mirror_uuid
== librbd::Journal
<>::LOCAL_MIRROR_UUID
&&
602 remote_tag_data
.predecessor
.mirror_uuid
== librbd::Journal
<>::ORPHAN_MIRROR_UUID
&&
603 remote_tag_data
.predecessor
.commit_valid
&& remote_orphan_tag_tid
&&
604 remote_tag_data
.predecessor
.tag_tid
== *remote_orphan_tag_tid
) {
605 // remote promotion tag chained to remote/local demotion tag
606 dout(20) << ": found chained remote promotion tag" << dendl
;
607 reconnect_orphan
= true;
611 // promotion must follow demotion
612 remote_orphan_tag_tid
= boost::none
;
616 if (remote_tag_data_valid
&&
617 local_tag_data
.mirror_uuid
== m_remote_mirror_uuid
) {
618 dout(20) << ": local image is in clean replay state" << dendl
;
619 } else if (reconnect_orphan
) {
620 dout(20) << ": remote image was demoted/promoted" << dendl
;
622 derr
<< ": split-brain detected -- skipping image replay" << dendl
;
631 template <typename I
>
632 void BootstrapRequest
<I
>::image_sync() {
633 if (m_client_meta
->state
== librbd::journal::MIRROR_PEER_STATE_REPLAYING
) {
634 // clean replay state -- no image sync required
635 close_remote_image();
640 update_progress("IMAGE_SYNC");
642 Context
*ctx
= create_context_callback
<
643 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_image_sync
>(
647 Mutex::Locker
locker(m_lock
);
649 m_ret_val
= -ECANCELED
;
651 assert(m_image_sync
== nullptr);
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
);
658 m_image_sync
->send();
663 dout(10) << ": request canceled" << dendl
;
664 close_remote_image();
667 template <typename I
>
668 void BootstrapRequest
<I
>::handle_image_sync(int r
) {
669 dout(20) << ": r=" << r
<< dendl
;
672 Mutex::Locker
locker(m_lock
);
675 m_image_sync
= nullptr;
678 dout(10) << ": request canceled" << dendl
;
679 m_ret_val
= -ECANCELED
;
683 derr
<< ": failed to sync remote image: " << cpp_strerror(r
) << dendl
;
688 close_remote_image();
691 template <typename I
>
692 void BootstrapRequest
<I
>::close_local_image() {
695 update_progress("CLOSE_LOCAL_IMAGE");
697 Context
*ctx
= create_context_callback
<
698 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_close_local_image
>(
700 CloseImageRequest
<I
> *request
= CloseImageRequest
<I
>::create(
701 m_local_image_ctx
, ctx
);
705 template <typename I
>
706 void BootstrapRequest
<I
>::handle_close_local_image(int r
) {
707 dout(20) << ": r=" << r
<< dendl
;
710 derr
<< ": error encountered closing local image: " << cpp_strerror(r
)
714 close_remote_image();
717 template <typename I
>
718 void BootstrapRequest
<I
>::close_remote_image() {
721 update_progress("CLOSE_REMOTE_IMAGE");
723 Context
*ctx
= create_context_callback
<
724 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_close_remote_image
>(
726 CloseImageRequest
<I
> *request
= CloseImageRequest
<I
>::create(
727 &m_remote_image_ctx
, ctx
);
731 template <typename I
>
732 void BootstrapRequest
<I
>::handle_close_remote_image(int r
) {
733 dout(20) << ": r=" << r
<< dendl
;
736 derr
<< ": error encountered closing remote image: " << cpp_strerror(r
)
743 template <typename I
>
744 bool BootstrapRequest
<I
>::decode_client_meta() {
747 librbd::journal::ClientData client_data
;
748 bufferlist::iterator it
= m_client
.data
.begin();
750 ::decode(client_data
, it
);
751 } catch (const buffer::error
&err
) {
752 derr
<< ": failed to decode client meta data: " << err
.what() << dendl
;
756 librbd::journal::MirrorPeerClientMeta
*client_meta
=
757 boost::get
<librbd::journal::MirrorPeerClientMeta
>(&client_data
.client_meta
);
758 if (client_meta
== nullptr) {
759 derr
<< ": unknown peer registration" << dendl
;
761 } else if (!client_meta
->image_id
.empty()) {
762 // have an image id -- use that to open the image
763 m_local_image_id
= client_meta
->image_id
;
766 *m_client_meta
= *client_meta
;
768 dout(20) << ": client found: image_id=" << m_local_image_id
769 << ", client_meta=" << *m_client_meta
<< dendl
;
773 template <typename I
>
774 void BootstrapRequest
<I
>::update_progress(const std::string
&description
) {
775 dout(20) << ": " << description
<< dendl
;
777 if (m_progress_ctx
) {
778 m_progress_ctx
->update_progress(description
);
782 } // namespace image_replayer
783 } // namespace mirror
786 template class rbd::mirror::image_replayer::BootstrapRequest
<librbd::ImageCtx
>;