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 dout(5) << ": remote image is not mirrored" << dendl
;
199 m_ret_val
= -EREMOTEIO
;
200 close_remote_image();
203 derr
<< ": error querying remote image primary status: " << cpp_strerror(r
)
206 close_remote_image();
211 if (m_local_image_id
.empty()) {
212 // no local image and remote isn't primary -- don't sync it
213 dout(5) << ": remote image is not primary -- not syncing"
215 m_ret_val
= -EREMOTEIO
;
216 close_remote_image();
218 } else if (m_client_meta
->state
!=
219 librbd::journal::MIRROR_PEER_STATE_REPLAYING
) {
220 // ensure we attempt to re-sync to remote if it's re-promoted
221 dout(5) << ": remote image is not primary -- sync interrupted"
223 m_ret_val
= -EREMOTEIO
;
224 update_client_state();
229 if (!m_client_meta
->image_id
.empty()) {
230 // have an image id -- use that to open the image since a deletion (resync)
231 // will leave the old image id registered in the peer
232 m_local_image_id
= m_client_meta
->image_id
;
235 if (m_local_image_id
.empty()) {
236 // prepare to create local image
237 update_client_image();
244 template <typename I
>
245 void BootstrapRequest
<I
>::update_client_state() {
247 update_progress("UPDATE_CLIENT_STATE");
249 librbd::journal::MirrorPeerClientMeta
client_meta(*m_client_meta
);
250 client_meta
.state
= librbd::journal::MIRROR_PEER_STATE_REPLAYING
;
252 librbd::journal::ClientData
client_data(client_meta
);
254 ::encode(client_data
, data_bl
);
256 Context
*ctx
= create_context_callback
<
257 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_update_client_state
>(
259 m_journaler
->update_client(data_bl
, ctx
);
262 template <typename I
>
263 void BootstrapRequest
<I
>::handle_update_client_state(int r
) {
264 dout(20) << ": r=" << r
<< dendl
;
266 derr
<< ": failed to update client: " << cpp_strerror(r
) << dendl
;
268 m_client_meta
->state
= librbd::journal::MIRROR_PEER_STATE_REPLAYING
;
271 close_remote_image();
274 template <typename I
>
275 void BootstrapRequest
<I
>::open_local_image() {
278 update_progress("OPEN_LOCAL_IMAGE");
280 Context
*ctx
= create_context_callback
<
281 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_open_local_image
>(
283 OpenLocalImageRequest
<I
> *request
= OpenLocalImageRequest
<I
>::create(
284 m_local_io_ctx
, m_local_image_ctx
, m_local_image_id
, m_work_queue
,
289 template <typename I
>
290 void BootstrapRequest
<I
>::handle_open_local_image(int r
) {
291 dout(20) << ": r=" << r
<< dendl
;
294 assert(*m_local_image_ctx
== nullptr);
295 dout(10) << ": local image missing" << dendl
;
298 } else if (r
== -EREMOTEIO
) {
299 assert(*m_local_image_ctx
== nullptr);
300 dout(10) << "local image is primary -- skipping image replay" << dendl
;
302 close_remote_image();
305 assert(*m_local_image_ctx
== nullptr);
306 derr
<< ": failed to open local image: " << cpp_strerror(r
) << dendl
;
308 close_remote_image();
312 I
*local_image_ctx
= (*m_local_image_ctx
);
314 local_image_ctx
->snap_lock
.get_read();
315 if (local_image_ctx
->journal
== nullptr) {
316 local_image_ctx
->snap_lock
.put_read();
318 derr
<< ": local image does not support journaling" << dendl
;
324 r
= (*m_local_image_ctx
)->journal
->is_resync_requested(m_do_resync
);
326 local_image_ctx
->snap_lock
.put_read();
328 derr
<< ": failed to check if a resync was requested" << dendl
;
334 m_local_tag_tid
= local_image_ctx
->journal
->get_tag_tid();
335 m_local_tag_data
= local_image_ctx
->journal
->get_tag_data();
336 dout(10) << ": local tag=" << m_local_tag_tid
<< ", "
337 << "local tag data=" << m_local_tag_data
<< dendl
;
338 local_image_ctx
->snap_lock
.put_read();
341 if (m_local_tag_data
.mirror_uuid
!= m_remote_mirror_uuid
&& !m_primary
) {
342 // if the local mirror is not linked to the (now) non-primary image,
343 // stop the replay. Otherwise, we ignore that the remote is non-primary
344 // so that we can replay the demotion
345 dout(5) << ": remote image is not primary -- skipping image replay"
347 m_ret_val
= -EREMOTEIO
;
353 close_remote_image();
357 if (*m_client_state
== cls::journal::CLIENT_STATE_DISCONNECTED
) {
358 dout(10) << ": client flagged disconnected -- skipping bootstrap" << dendl
;
359 // The caller is expected to detect disconnect initializing remote journal.
361 close_remote_image();
368 template <typename I
>
369 void BootstrapRequest
<I
>::unregister_client() {
371 update_progress("UNREGISTER_CLIENT");
373 m_local_image_id
= "";
374 Context
*ctx
= create_context_callback
<
375 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_unregister_client
>(
377 m_journaler
->unregister_client(ctx
);
380 template <typename I
>
381 void BootstrapRequest
<I
>::handle_unregister_client(int r
) {
382 dout(20) << ": r=" << r
<< dendl
;
384 derr
<< ": failed to unregister with remote journal: " << cpp_strerror(r
)
387 close_remote_image();
391 *m_client_meta
= librbd::journal::MirrorPeerClientMeta("");
395 template <typename I
>
396 void BootstrapRequest
<I
>::register_client() {
399 update_progress("REGISTER_CLIENT");
401 assert(m_local_image_id
.empty());
402 librbd::journal::MirrorPeerClientMeta mirror_peer_client_meta
;
403 mirror_peer_client_meta
.state
= librbd::journal::MIRROR_PEER_STATE_REPLAYING
;
405 librbd::journal::ClientData client_data
{mirror_peer_client_meta
};
406 bufferlist client_data_bl
;
407 ::encode(client_data
, client_data_bl
);
409 Context
*ctx
= create_context_callback
<
410 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_register_client
>(
412 m_journaler
->register_client(client_data_bl
, ctx
);
415 template <typename I
>
416 void BootstrapRequest
<I
>::handle_register_client(int r
) {
417 dout(20) << ": r=" << r
<< dendl
;
420 derr
<< ": failed to register with remote journal: " << cpp_strerror(r
)
423 close_remote_image();
427 *m_client_state
= cls::journal::CLIENT_STATE_CONNECTED
;
428 *m_client_meta
= librbd::journal::MirrorPeerClientMeta();
429 m_client_meta
->state
= librbd::journal::MIRROR_PEER_STATE_REPLAYING
;
434 template <typename I
>
435 void BootstrapRequest
<I
>::update_client_image() {
437 update_progress("UPDATE_CLIENT_IMAGE");
439 assert(m_local_image_id
.empty());
440 m_local_image_id
= librbd::util::generate_image_id
<I
>(m_local_io_ctx
);
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
;
462 close_remote_image();
467 dout(10) << ": request canceled" << dendl
;
468 m_ret_val
= -ECANCELED
;
469 close_remote_image();
473 *m_client_meta
= {m_local_image_id
};
474 m_client_meta
->state
= librbd::journal::MIRROR_PEER_STATE_SYNCING
;
475 create_local_image();
478 template <typename I
>
479 void BootstrapRequest
<I
>::create_local_image() {
481 update_progress("CREATE_LOCAL_IMAGE");
483 m_remote_image_ctx
->snap_lock
.get_read();
484 std::string image_name
= m_remote_image_ctx
->name
;
485 m_remote_image_ctx
->snap_lock
.put_read();
487 Context
*ctx
= create_context_callback
<
488 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_create_local_image
>(
490 CreateImageRequest
<I
> *request
= CreateImageRequest
<I
>::create(
491 m_local_io_ctx
, m_work_queue
, m_global_image_id
, m_remote_mirror_uuid
,
492 image_name
, m_local_image_id
, m_remote_image_ctx
, ctx
);
496 template <typename I
>
497 void BootstrapRequest
<I
>::handle_create_local_image(int r
) {
498 dout(20) << ": r=" << r
<< dendl
;
501 derr
<< ": failed to create local image: " << cpp_strerror(r
) << dendl
;
503 close_remote_image();
510 template <typename I
>
511 void BootstrapRequest
<I
>::get_remote_tags() {
512 if (m_client_meta
->state
== librbd::journal::MIRROR_PEER_STATE_SYNCING
) {
513 // optimization -- no need to compare remote tags if we just created
514 // the image locally or sync was interrupted
520 update_progress("GET_REMOTE_TAGS");
522 Context
*ctx
= create_context_callback
<
523 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_get_remote_tags
>(this);
524 m_journaler
->get_tags(m_remote_tag_class
, &m_remote_tags
, ctx
);
527 template <typename I
>
528 void BootstrapRequest
<I
>::handle_get_remote_tags(int r
) {
529 dout(20) << ": r=" << r
<< dendl
;
532 derr
<< ": failed to retrieve remote tags: " << cpp_strerror(r
) << dendl
;
539 dout(10) << ": request canceled" << dendl
;
540 m_ret_val
= -ECANCELED
;
545 // At this point, the local image was existing, non-primary, and replaying;
546 // and the remote image is primary. Attempt to link the local image's most
547 // recent tag to the remote image's tag chain.
548 bool remote_tag_data_valid
= false;
549 librbd::journal::TagData remote_tag_data
;
550 boost::optional
<uint64_t> remote_orphan_tag_tid
=
551 boost::make_optional
<uint64_t>(false, 0U);
552 bool reconnect_orphan
= false;
554 // decode the remote tags
555 for (auto &remote_tag
: m_remote_tags
) {
556 if (m_local_tag_data
.predecessor
.commit_valid
&&
557 m_local_tag_data
.predecessor
.mirror_uuid
== m_remote_mirror_uuid
&&
558 m_local_tag_data
.predecessor
.tag_tid
> remote_tag
.tid
) {
559 dout(15) << ": skipping processed predecessor remote tag "
560 << remote_tag
.tid
<< dendl
;
565 bufferlist::iterator it
= remote_tag
.data
.begin();
566 ::decode(remote_tag_data
, it
);
567 remote_tag_data_valid
= true;
568 } catch (const buffer::error
&err
) {
569 derr
<< ": failed to decode remote tag " << remote_tag
.tid
<< ": "
570 << err
.what() << dendl
;
571 m_ret_val
= -EBADMSG
;
576 dout(10) << ": decoded remote tag " << remote_tag
.tid
<< ": "
577 << remote_tag_data
<< dendl
;
579 if (!m_local_tag_data
.predecessor
.commit_valid
) {
580 // newly synced local image (no predecessor) replays from the first tag
581 if (remote_tag_data
.mirror_uuid
!= librbd::Journal
<>::LOCAL_MIRROR_UUID
) {
582 dout(20) << ": skipping non-primary remote tag" << dendl
;
586 dout(20) << ": using initial primary remote tag" << dendl
;
590 if (m_local_tag_data
.mirror_uuid
== librbd::Journal
<>::ORPHAN_MIRROR_UUID
) {
591 // demotion last available local epoch
593 if (remote_tag_data
.mirror_uuid
== m_local_tag_data
.mirror_uuid
&&
594 remote_tag_data
.predecessor
.commit_valid
&&
595 remote_tag_data
.predecessor
.tag_tid
==
596 m_local_tag_data
.predecessor
.tag_tid
) {
597 // demotion matches remote epoch
599 if (remote_tag_data
.predecessor
.mirror_uuid
== m_local_mirror_uuid
&&
600 m_local_tag_data
.predecessor
.mirror_uuid
==
601 librbd::Journal
<>::LOCAL_MIRROR_UUID
) {
602 // local demoted and remote has matching event
603 dout(20) << ": found matching local demotion tag" << dendl
;
604 remote_orphan_tag_tid
= remote_tag
.tid
;
608 if (m_local_tag_data
.predecessor
.mirror_uuid
== m_remote_mirror_uuid
&&
609 remote_tag_data
.predecessor
.mirror_uuid
==
610 librbd::Journal
<>::LOCAL_MIRROR_UUID
) {
611 // remote demoted and local has matching event
612 dout(20) << ": found matching remote demotion tag" << dendl
;
613 remote_orphan_tag_tid
= remote_tag
.tid
;
618 if (remote_tag_data
.mirror_uuid
== librbd::Journal
<>::LOCAL_MIRROR_UUID
&&
619 remote_tag_data
.predecessor
.mirror_uuid
== librbd::Journal
<>::ORPHAN_MIRROR_UUID
&&
620 remote_tag_data
.predecessor
.commit_valid
&& remote_orphan_tag_tid
&&
621 remote_tag_data
.predecessor
.tag_tid
== *remote_orphan_tag_tid
) {
622 // remote promotion tag chained to remote/local demotion tag
623 dout(20) << ": found chained remote promotion tag" << dendl
;
624 reconnect_orphan
= true;
628 // promotion must follow demotion
629 remote_orphan_tag_tid
= boost::none
;
633 if (remote_tag_data_valid
&&
634 m_local_tag_data
.mirror_uuid
== m_remote_mirror_uuid
) {
635 dout(10) << ": local image is in clean replay state" << dendl
;
636 } else if (reconnect_orphan
) {
637 dout(20) << ": remote image was demoted/promoted" << dendl
;
639 derr
<< ": split-brain detected -- skipping image replay" << dendl
;
648 template <typename I
>
649 void BootstrapRequest
<I
>::image_sync() {
650 if (m_client_meta
->state
== librbd::journal::MIRROR_PEER_STATE_REPLAYING
) {
651 // clean replay state -- no image sync required
652 close_remote_image();
658 Mutex::Locker
locker(m_lock
);
660 m_ret_val
= -ECANCELED
;
662 assert(m_image_sync
== nullptr);
664 Context
*ctx
= create_context_callback
<
665 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_image_sync
>(this);
666 m_image_sync
= ImageSync
<I
>::create(
667 *m_local_image_ctx
, m_remote_image_ctx
, m_timer
, m_timer_lock
,
668 m_local_mirror_uuid
, m_journaler
, m_client_meta
, m_work_queue
,
669 m_instance_watcher
, ctx
, m_progress_ctx
);
674 update_progress("IMAGE_SYNC");
677 m_image_sync
->send();
682 dout(10) << ": request canceled" << dendl
;
683 close_remote_image();
686 template <typename I
>
687 void BootstrapRequest
<I
>::handle_image_sync(int r
) {
688 dout(20) << ": r=" << r
<< dendl
;
691 Mutex::Locker
locker(m_lock
);
693 m_image_sync
= nullptr;
696 dout(10) << ": request canceled" << dendl
;
697 m_ret_val
= -ECANCELED
;
701 derr
<< ": failed to sync remote image: " << cpp_strerror(r
) << dendl
;
706 close_remote_image();
709 template <typename I
>
710 void BootstrapRequest
<I
>::close_local_image() {
713 update_progress("CLOSE_LOCAL_IMAGE");
715 Context
*ctx
= create_context_callback
<
716 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_close_local_image
>(
718 CloseImageRequest
<I
> *request
= CloseImageRequest
<I
>::create(
719 m_local_image_ctx
, ctx
);
723 template <typename I
>
724 void BootstrapRequest
<I
>::handle_close_local_image(int r
) {
725 dout(20) << ": r=" << r
<< dendl
;
728 derr
<< ": error encountered closing local image: " << cpp_strerror(r
)
732 close_remote_image();
735 template <typename I
>
736 void BootstrapRequest
<I
>::close_remote_image() {
739 update_progress("CLOSE_REMOTE_IMAGE");
741 Context
*ctx
= create_context_callback
<
742 BootstrapRequest
<I
>, &BootstrapRequest
<I
>::handle_close_remote_image
>(
744 CloseImageRequest
<I
> *request
= CloseImageRequest
<I
>::create(
745 &m_remote_image_ctx
, ctx
);
749 template <typename I
>
750 void BootstrapRequest
<I
>::handle_close_remote_image(int r
) {
751 dout(20) << ": r=" << r
<< dendl
;
754 derr
<< ": error encountered closing remote image: " << cpp_strerror(r
)
761 template <typename I
>
762 void BootstrapRequest
<I
>::update_progress(const std::string
&description
) {
763 dout(20) << ": " << description
<< dendl
;
765 if (m_progress_ctx
) {
766 m_progress_ctx
->update_progress(description
);
770 } // namespace image_replayer
771 } // namespace mirror
774 template class rbd::mirror::image_replayer::BootstrapRequest
<librbd::ImageCtx
>;