1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "InstanceWatcher.h"
5 #include "include/stringify.h"
6 #include "common/debug.h"
7 #include "common/errno.h"
8 #include "cls/rbd/cls_rbd_client.h"
9 #include "librbd/ManagedLock.h"
10 #include "librbd/Utils.h"
11 #include "InstanceReplayer.h"
12 #include "ImageSyncThrottler.h"
14 #define dout_context g_ceph_context
15 #define dout_subsys ceph_subsys_rbd_mirror
17 #define dout_prefix *_dout << "rbd::mirror::InstanceWatcher: "
22 using namespace instance_watcher
;
24 using librbd::util::create_async_context_callback
;
25 using librbd::util::create_context_callback
;
26 using librbd::util::create_rados_callback
;
27 using librbd::util::unique_lock_name
;
31 struct C_GetInstances
: public Context
{
32 std::vector
<std::string
> *instance_ids
;
36 C_GetInstances(std::vector
<std::string
> *instance_ids
, Context
*on_finish
)
37 : instance_ids(instance_ids
), on_finish(on_finish
) {
40 void finish(int r
) override
{
41 dout(20) << "C_GetInstances: " << this << " " << __func__
<< ": r=" << r
45 bufferlist::iterator it
= out_bl
.begin();
46 r
= librbd::cls_client::mirror_instances_list_finish(&it
, instance_ids
);
47 } else if (r
== -ENOENT
) {
50 on_finish
->complete(r
);
55 struct C_RemoveInstanceRequest
: public Context
{
56 InstanceWatcher
<I
> instance_watcher
;
59 C_RemoveInstanceRequest(librados::IoCtx
&io_ctx
, ContextWQ
*work_queue
,
60 const std::string
&instance_id
, Context
*on_finish
)
61 : instance_watcher(io_ctx
, work_queue
, nullptr, instance_id
),
62 on_finish(on_finish
) {
66 dout(20) << "C_RemoveInstanceRequest: " << this << " " << __func__
<< dendl
;
68 instance_watcher
.remove(this);
71 void finish(int r
) override
{
72 dout(20) << "C_RemoveInstanceRequest: " << this << " " << __func__
<< ": r="
76 on_finish
->complete(r
);
80 } // anonymous namespace
83 struct InstanceWatcher
<I
>::C_NotifyInstanceRequest
: public Context
{
84 InstanceWatcher
<I
> *instance_watcher
;
85 std::string instance_id
;
90 std::unique_ptr
<librbd::watcher::Notifier
> notifier
;
91 librbd::watcher::NotifyResponse response
;
92 bool canceling
= false;
94 C_NotifyInstanceRequest(InstanceWatcher
<I
> *instance_watcher
,
95 const std::string
&instance_id
, uint64_t request_id
,
96 bufferlist
&&bl
, Context
*on_finish
)
97 : instance_watcher(instance_watcher
), instance_id(instance_id
),
98 request_id(request_id
), bl(bl
), on_finish(on_finish
),
99 send_to_leader(instance_id
.empty()) {
100 dout(20) << "C_NotifyInstanceRequest: " << this << " " << __func__
101 << ": instance_watcher=" << instance_watcher
<< ", instance_id="
102 << instance_id
<< ", request_id=" << request_id
<< dendl
;
104 assert(instance_watcher
->m_lock
.is_locked());
106 if (!send_to_leader
) {
107 assert((!instance_id
.empty()));
108 notifier
.reset(new librbd::watcher::Notifier(
109 instance_watcher
->m_work_queue
,
110 instance_watcher
->m_ioctx
,
111 RBD_MIRROR_INSTANCE_PREFIX
+ instance_id
));
114 instance_watcher
->m_notify_op_tracker
.start_op();
115 auto result
= instance_watcher
->m_notify_ops
.insert(
116 std::make_pair(instance_id
, this)).second
;
121 dout(20) << "C_NotifyInstanceRequest: " << this << " " << __func__
<< dendl
;
123 assert(instance_watcher
->m_lock
.is_locked());
126 dout(20) << "C_NotifyInstanceRequest: " << this << " " << __func__
127 << ": canceling" << dendl
;
128 instance_watcher
->m_work_queue
->queue(this, -ECANCELED
);
132 if (send_to_leader
) {
133 if (instance_watcher
->m_leader_instance_id
.empty()) {
134 dout(20) << "C_NotifyInstanceRequest: " << this << " " << __func__
135 << ": suspending" << dendl
;
136 instance_watcher
->suspend_notify_request(this);
140 if (instance_watcher
->m_leader_instance_id
!= instance_id
) {
141 auto count
= instance_watcher
->m_notify_ops
.erase(
142 std::make_pair(instance_id
, this));
145 instance_id
= instance_watcher
->m_leader_instance_id
;
147 auto result
= instance_watcher
->m_notify_ops
.insert(
148 std::make_pair(instance_id
, this)).second
;
151 notifier
.reset(new librbd::watcher::Notifier(
152 instance_watcher
->m_work_queue
,
153 instance_watcher
->m_ioctx
,
154 RBD_MIRROR_INSTANCE_PREFIX
+ instance_id
));
158 dout(20) << "C_NotifyInstanceRequest: " << this << " " << __func__
159 << ": sendding to " << instance_id
<< dendl
;
160 notifier
->notify(bl
, &response
, this);
164 dout(20) << "C_NotifyInstanceRequest: " << this << " " << __func__
<< dendl
;
166 assert(instance_watcher
->m_lock
.is_locked());
169 instance_watcher
->unsuspend_notify_request(this);
172 void finish(int r
) override
{
173 dout(20) << "C_NotifyInstanceRequest: " << this << " " << __func__
<< ": r="
176 if (r
== 0 || r
== -ETIMEDOUT
) {
178 for (auto &it
: response
.acks
) {
179 auto &bl
= it
.second
;
180 if (it
.second
.length() == 0) {
181 dout(20) << "C_NotifyInstanceRequest: " << this << " " << __func__
182 << ": no payload in ack, ignoring" << dendl
;
186 auto iter
= bl
.begin();
187 NotifyAckPayload ack
;
189 if (ack
.instance_id
!= instance_watcher
->get_instance_id()) {
190 derr
<< "C_NotifyInstanceRequest: " << this << " " << __func__
191 << ": ack instance_id (" << ack
.instance_id
<< ") "
192 << "does not match, ignoring" << dendl
;
195 if (ack
.request_id
!= request_id
) {
196 derr
<< "C_NotifyInstanceRequest: " << this << " " << __func__
197 << ": ack request_id (" << ack
.request_id
<< ") "
198 << "does not match, ignoring" << dendl
;
204 } catch (const buffer::error
&err
) {
205 derr
<< "C_NotifyInstanceRequest: " << this << " " << __func__
206 << ": failed to decode ack: " << err
.what() << dendl
;
212 if (r
== -ETIMEDOUT
) {
213 derr
<< "C_NotifyInstanceRequest: " << this << " " << __func__
214 << ": resending after timeout" << dendl
;
215 Mutex::Locker
locker(instance_watcher
->m_lock
);
222 if (r
== -ESTALE
&& send_to_leader
) {
223 derr
<< "C_NotifyInstanceRequest: " << this << " " << __func__
224 << ": resending due to leader change" << dendl
;
225 Mutex::Locker
locker(instance_watcher
->m_lock
);
232 on_finish
->complete(r
);
235 Mutex::Locker
locker(instance_watcher
->m_lock
);
236 auto result
= instance_watcher
->m_notify_ops
.erase(
237 std::make_pair(instance_id
, this));
239 instance_watcher
->m_notify_op_tracker
.finish_op();
245 void complete(int r
) override
{
250 template <typename I
>
251 struct InstanceWatcher
<I
>::C_SyncRequest
: public Context
{
252 InstanceWatcher
<I
> *instance_watcher
;
255 Context
*on_complete
= nullptr;
256 C_NotifyInstanceRequest
*req
= nullptr;
258 C_SyncRequest(InstanceWatcher
<I
> *instance_watcher
,
259 const std::string
&sync_id
, Context
*on_start
)
260 : instance_watcher(instance_watcher
), sync_id(sync_id
),
262 dout(20) << "C_SyncRequest: " << this << " " << __func__
<< ": sync_id="
266 void finish(int r
) override
{
267 dout(20) << "C_SyncRequest: " << this << " " << __func__
<< ": r="
270 if (on_start
!= nullptr) {
271 instance_watcher
->handle_notify_sync_request(this, r
);
273 instance_watcher
->handle_notify_sync_complete(this, r
);
279 void complete(int r
) override
{
285 #define dout_prefix *_dout << "rbd::mirror::InstanceWatcher: " \
286 << this << " " << __func__ << ": "
287 template <typename I
>
288 void InstanceWatcher
<I
>::get_instances(librados::IoCtx
&io_ctx
,
289 std::vector
<std::string
> *instance_ids
,
290 Context
*on_finish
) {
291 librados::ObjectReadOperation op
;
292 librbd::cls_client::mirror_instances_list_start(&op
);
293 C_GetInstances
*ctx
= new C_GetInstances(instance_ids
, on_finish
);
294 librados::AioCompletion
*aio_comp
= create_rados_callback(ctx
);
296 int r
= io_ctx
.aio_operate(RBD_MIRROR_LEADER
, aio_comp
, &op
, &ctx
->out_bl
);
301 template <typename I
>
302 void InstanceWatcher
<I
>::remove_instance(librados::IoCtx
&io_ctx
,
303 ContextWQ
*work_queue
,
304 const std::string
&instance_id
,
305 Context
*on_finish
) {
306 auto req
= new C_RemoveInstanceRequest
<I
>(io_ctx
, work_queue
, instance_id
,
311 template <typename I
>
312 InstanceWatcher
<I
> *InstanceWatcher
<I
>::create(
313 librados::IoCtx
&io_ctx
, ContextWQ
*work_queue
,
314 InstanceReplayer
<I
> *instance_replayer
) {
315 return new InstanceWatcher
<I
>(io_ctx
, work_queue
, instance_replayer
,
316 stringify(io_ctx
.get_instance_id()));
319 template <typename I
>
320 InstanceWatcher
<I
>::InstanceWatcher(librados::IoCtx
&io_ctx
,
321 ContextWQ
*work_queue
,
322 InstanceReplayer
<I
> *instance_replayer
,
323 const std::string
&instance_id
)
324 : Watcher(io_ctx
, work_queue
, RBD_MIRROR_INSTANCE_PREFIX
+ instance_id
),
325 m_instance_replayer(instance_replayer
), m_instance_id(instance_id
),
326 m_lock(unique_lock_name("rbd::mirror::InstanceWatcher::m_lock", this)),
327 m_instance_lock(librbd::ManagedLock
<I
>::create(
328 m_ioctx
, m_work_queue
, m_oid
, this, librbd::managed_lock::EXCLUSIVE
, true,
329 m_cct
->_conf
->rbd_blacklist_expire_seconds
)) {
332 template <typename I
>
333 InstanceWatcher
<I
>::~InstanceWatcher() {
334 assert(m_notify_ops
.empty());
335 assert(m_notify_op_tracker
.empty());
336 assert(m_suspended_ops
.empty());
337 assert(m_inflight_sync_reqs
.empty());
338 assert(m_image_sync_throttler
== nullptr);
339 m_instance_lock
->destroy();
342 template <typename I
>
343 int InstanceWatcher
<I
>::init() {
344 C_SaferCond init_ctx
;
346 return init_ctx
.wait();
349 template <typename I
>
350 void InstanceWatcher
<I
>::init(Context
*on_finish
) {
351 dout(20) << "instance_id=" << m_instance_id
<< dendl
;
353 Mutex::Locker
locker(m_lock
);
355 assert(m_on_finish
== nullptr);
356 m_on_finish
= on_finish
;
362 template <typename I
>
363 void InstanceWatcher
<I
>::shut_down() {
364 C_SaferCond shut_down_ctx
;
365 shut_down(&shut_down_ctx
);
366 int r
= shut_down_ctx
.wait();
370 template <typename I
>
371 void InstanceWatcher
<I
>::shut_down(Context
*on_finish
) {
374 Mutex::Locker
locker(m_lock
);
376 assert(m_on_finish
== nullptr);
377 m_on_finish
= on_finish
;
383 template <typename I
>
384 void InstanceWatcher
<I
>::remove(Context
*on_finish
) {
387 Mutex::Locker
locker(m_lock
);
389 assert(m_on_finish
== nullptr);
390 m_on_finish
= on_finish
;
394 get_instance_locker();
397 template <typename I
>
398 void InstanceWatcher
<I
>::notify_image_acquire(
399 const std::string
&instance_id
, const std::string
&global_image_id
,
400 const std::string
&peer_mirror_uuid
, const std::string
&peer_image_id
,
401 Context
*on_notify_ack
) {
402 dout(20) << "instance_id=" << instance_id
<< ", global_image_id="
403 << global_image_id
<< dendl
;
405 Mutex::Locker
locker(m_lock
);
407 assert(m_on_finish
== nullptr);
409 if (instance_id
== m_instance_id
) {
410 handle_image_acquire(global_image_id
, peer_mirror_uuid
, peer_image_id
,
413 uint64_t request_id
= ++m_request_seq
;
415 ::encode(NotifyMessage
{ImageAcquirePayload
{
416 request_id
, global_image_id
, peer_mirror_uuid
, peer_image_id
}}, bl
);
417 auto req
= new C_NotifyInstanceRequest(this, instance_id
, request_id
,
418 std::move(bl
), on_notify_ack
);
423 template <typename I
>
424 void InstanceWatcher
<I
>::notify_image_release(
425 const std::string
&instance_id
, const std::string
&global_image_id
,
426 const std::string
&peer_mirror_uuid
, const std::string
&peer_image_id
,
427 bool schedule_delete
, Context
*on_notify_ack
) {
428 dout(20) << "instance_id=" << instance_id
<< ", global_image_id="
429 << global_image_id
<< dendl
;
431 Mutex::Locker
locker(m_lock
);
433 assert(m_on_finish
== nullptr);
435 if (instance_id
== m_instance_id
) {
436 handle_image_release(global_image_id
, peer_mirror_uuid
, peer_image_id
,
437 schedule_delete
, on_notify_ack
);
439 uint64_t request_id
= ++m_request_seq
;
441 ::encode(NotifyMessage
{ImageReleasePayload
{
442 request_id
, global_image_id
, peer_mirror_uuid
, peer_image_id
,
443 schedule_delete
}}, bl
);
444 auto req
= new C_NotifyInstanceRequest(this, instance_id
, request_id
,
445 std::move(bl
), on_notify_ack
);
450 template <typename I
>
451 void InstanceWatcher
<I
>::notify_sync_request(const std::string
&sync_id
,
452 Context
*on_sync_start
) {
453 dout(20) << "sync_id=" << sync_id
<< dendl
;
455 Mutex::Locker
locker(m_lock
);
457 assert(m_inflight_sync_reqs
.count(sync_id
) == 0);
459 uint64_t request_id
= ++m_request_seq
;
462 ::encode(NotifyMessage
{SyncRequestPayload
{request_id
, sync_id
}}, bl
);
464 auto sync_ctx
= new C_SyncRequest(this, sync_id
, on_sync_start
);
465 sync_ctx
->req
= new C_NotifyInstanceRequest(this, "", request_id
,
466 std::move(bl
), sync_ctx
);
468 m_inflight_sync_reqs
[sync_id
] = sync_ctx
;
469 sync_ctx
->req
->send();
472 template <typename I
>
473 bool InstanceWatcher
<I
>::cancel_sync_request(const std::string
&sync_id
) {
474 dout(20) << "sync_id=" << sync_id
<< dendl
;
476 Mutex::Locker
locker(m_lock
);
478 auto it
= m_inflight_sync_reqs
.find(sync_id
);
479 if (it
== m_inflight_sync_reqs
.end()) {
483 auto sync_ctx
= it
->second
;
485 if (sync_ctx
->on_start
== nullptr) {
489 assert(sync_ctx
->req
!= nullptr);
490 sync_ctx
->req
->cancel();
494 template <typename I
>
495 void InstanceWatcher
<I
>::notify_sync_start(const std::string
&instance_id
,
496 const std::string
&sync_id
) {
497 dout(20) << "sync_id=" << sync_id
<< dendl
;
499 Mutex::Locker
locker(m_lock
);
501 uint64_t request_id
= ++m_request_seq
;
504 ::encode(NotifyMessage
{SyncStartPayload
{request_id
, sync_id
}}, bl
);
506 auto ctx
= new FunctionContext(
507 [this, sync_id
] (int r
) {
508 dout(20) << "finish: sync_id=" << sync_id
<< ", r=" << r
<< dendl
;
509 Mutex::Locker
locker(m_lock
);
510 if (r
!= -ESTALE
&& m_image_sync_throttler
!= nullptr) {
511 m_image_sync_throttler
->finish_op(sync_id
);
514 auto req
= new C_NotifyInstanceRequest(this, instance_id
, request_id
,
519 template <typename I
>
520 void InstanceWatcher
<I
>::notify_sync_complete(const std::string
&sync_id
) {
521 dout(20) << "sync_id=" << sync_id
<< dendl
;
523 Mutex::Locker
locker(m_lock
);
525 auto it
= m_inflight_sync_reqs
.find(sync_id
);
526 assert(it
!= m_inflight_sync_reqs
.end());
528 auto sync_ctx
= it
->second
;
529 assert(sync_ctx
->req
== nullptr);
531 m_inflight_sync_reqs
.erase(it
);
532 m_work_queue
->queue(sync_ctx
, 0);
535 template <typename I
>
536 void InstanceWatcher
<I
>::handle_notify_sync_request(C_SyncRequest
*sync_ctx
,
538 dout(20) << "sync_id=" << sync_ctx
->sync_id
<< ", r=" << r
<< dendl
;
540 Context
*on_start
= nullptr;
542 Mutex::Locker
locker(m_lock
);
544 assert(sync_ctx
->req
!= nullptr);
545 assert(sync_ctx
->on_start
!= nullptr);
547 if (sync_ctx
->req
->canceling
) {
551 std::swap(sync_ctx
->on_start
, on_start
);
552 sync_ctx
->req
= nullptr;
555 on_start
->complete(r
== -ECANCELED
? r
: 0);
557 if (r
== -ECANCELED
) {
558 notify_sync_complete(sync_ctx
->sync_id
);
562 template <typename I
>
563 void InstanceWatcher
<I
>::handle_notify_sync_complete(C_SyncRequest
*sync_ctx
,
565 dout(20) << "sync_id=" << sync_ctx
->sync_id
<< ", r=" << r
<< dendl
;
567 if (sync_ctx
->on_complete
!= nullptr) {
568 sync_ctx
->on_complete
->complete(r
);
572 template <typename I
>
573 void InstanceWatcher
<I
>::print_sync_status(Formatter
*f
, stringstream
*ss
) {
576 Mutex::Locker
locker(m_lock
);
577 if (m_image_sync_throttler
!= nullptr) {
578 m_image_sync_throttler
->print_status(f
, ss
);
582 template <typename I
>
583 void InstanceWatcher
<I
>::handle_acquire_leader() {
586 Mutex::Locker
locker(m_lock
);
588 assert(m_image_sync_throttler
== nullptr);
589 m_image_sync_throttler
= ImageSyncThrottler
<I
>::create();
591 m_leader_instance_id
= m_instance_id
;
592 unsuspend_notify_requests();
595 template <typename I
>
596 void InstanceWatcher
<I
>::handle_release_leader() {
599 Mutex::Locker
locker(m_lock
);
601 assert(m_image_sync_throttler
!= nullptr);
603 m_leader_instance_id
.clear();
605 m_image_sync_throttler
->drain(-ESTALE
);
606 m_image_sync_throttler
->destroy();
607 m_image_sync_throttler
= nullptr;
610 template <typename I
>
611 void InstanceWatcher
<I
>::handle_update_leader(
612 const std::string
&leader_instance_id
) {
613 dout(20) << "leader_instance_id=" << leader_instance_id
<< dendl
;
615 Mutex::Locker
locker(m_lock
);
617 m_leader_instance_id
= leader_instance_id
;
619 if (!m_leader_instance_id
.empty()) {
620 unsuspend_notify_requests();
624 template <typename I
>
625 void InstanceWatcher
<I
>::cancel_notify_requests(
626 const std::string
&instance_id
) {
627 dout(20) << "instance_id=" << instance_id
<< dendl
;
629 Mutex::Locker
locker(m_lock
);
631 for (auto op
: m_notify_ops
) {
632 if (op
.first
== instance_id
&& !op
.second
->send_to_leader
) {
638 template <typename I
>
639 void InstanceWatcher
<I
>::register_instance() {
640 assert(m_lock
.is_locked());
644 librados::ObjectWriteOperation op
;
645 librbd::cls_client::mirror_instances_add(&op
, m_instance_id
);
646 librados::AioCompletion
*aio_comp
= create_rados_callback
<
647 InstanceWatcher
<I
>, &InstanceWatcher
<I
>::handle_register_instance
>(this);
649 int r
= m_ioctx
.aio_operate(RBD_MIRROR_LEADER
, aio_comp
, &op
);
654 template <typename I
>
655 void InstanceWatcher
<I
>::handle_register_instance(int r
) {
656 dout(20) << "r=" << r
<< dendl
;
658 Context
*on_finish
= nullptr;
660 Mutex::Locker
locker(m_lock
);
663 create_instance_object();
667 derr
<< "error registering instance: " << cpp_strerror(r
) << dendl
;
669 std::swap(on_finish
, m_on_finish
);
671 on_finish
->complete(r
);
675 template <typename I
>
676 void InstanceWatcher
<I
>::create_instance_object() {
679 assert(m_lock
.is_locked());
681 librados::ObjectWriteOperation op
;
684 librados::AioCompletion
*aio_comp
= create_rados_callback
<
686 &InstanceWatcher
<I
>::handle_create_instance_object
>(this);
687 int r
= m_ioctx
.aio_operate(m_oid
, aio_comp
, &op
);
692 template <typename I
>
693 void InstanceWatcher
<I
>::handle_create_instance_object(int r
) {
694 dout(20) << "r=" << r
<< dendl
;
696 Mutex::Locker
locker(m_lock
);
699 derr
<< "error creating " << m_oid
<< " object: " << cpp_strerror(r
)
703 unregister_instance();
710 template <typename I
>
711 void InstanceWatcher
<I
>::register_watch() {
714 assert(m_lock
.is_locked());
716 Context
*ctx
= create_async_context_callback(
717 m_work_queue
, create_context_callback
<
718 InstanceWatcher
<I
>, &InstanceWatcher
<I
>::handle_register_watch
>(this));
720 librbd::Watcher::register_watch(ctx
);
723 template <typename I
>
724 void InstanceWatcher
<I
>::handle_register_watch(int r
) {
725 dout(20) << "r=" << r
<< dendl
;
727 Mutex::Locker
locker(m_lock
);
730 derr
<< "error registering instance watcher for " << m_oid
<< " object: "
731 << cpp_strerror(r
) << dendl
;
734 remove_instance_object();
741 template <typename I
>
742 void InstanceWatcher
<I
>::acquire_lock() {
745 assert(m_lock
.is_locked());
747 Context
*ctx
= create_async_context_callback(
748 m_work_queue
, create_context_callback
<
749 InstanceWatcher
<I
>, &InstanceWatcher
<I
>::handle_acquire_lock
>(this));
751 m_instance_lock
->acquire_lock(ctx
);
754 template <typename I
>
755 void InstanceWatcher
<I
>::handle_acquire_lock(int r
) {
756 dout(20) << "r=" << r
<< dendl
;
758 Context
*on_finish
= nullptr;
760 Mutex::Locker
locker(m_lock
);
764 derr
<< "error acquiring instance lock: " << cpp_strerror(r
) << dendl
;
771 std::swap(on_finish
, m_on_finish
);
774 on_finish
->complete(r
);
777 template <typename I
>
778 void InstanceWatcher
<I
>::release_lock() {
781 assert(m_lock
.is_locked());
783 Context
*ctx
= create_async_context_callback(
784 m_work_queue
, create_context_callback
<
785 InstanceWatcher
<I
>, &InstanceWatcher
<I
>::handle_release_lock
>(this));
787 m_instance_lock
->shut_down(ctx
);
790 template <typename I
>
791 void InstanceWatcher
<I
>::handle_release_lock(int r
) {
792 dout(20) << "r=" << r
<< dendl
;
794 Mutex::Locker
locker(m_lock
);
797 derr
<< "error releasing instance lock: " << cpp_strerror(r
) << dendl
;
803 template <typename I
>
804 void InstanceWatcher
<I
>::unregister_watch() {
807 assert(m_lock
.is_locked());
809 Context
*ctx
= create_async_context_callback(
810 m_work_queue
, create_context_callback
<
811 InstanceWatcher
<I
>, &InstanceWatcher
<I
>::handle_unregister_watch
>(this));
813 librbd::Watcher::unregister_watch(ctx
);
816 template <typename I
>
817 void InstanceWatcher
<I
>::handle_unregister_watch(int r
) {
818 dout(20) << "r=" << r
<< dendl
;
821 derr
<< "error unregistering instance watcher for " << m_oid
<< " object: "
822 << cpp_strerror(r
) << dendl
;
825 Mutex::Locker
locker(m_lock
);
826 remove_instance_object();
829 template <typename I
>
830 void InstanceWatcher
<I
>::remove_instance_object() {
831 assert(m_lock
.is_locked());
835 librados::ObjectWriteOperation op
;
838 librados::AioCompletion
*aio_comp
= create_rados_callback
<
840 &InstanceWatcher
<I
>::handle_remove_instance_object
>(this);
841 int r
= m_ioctx
.aio_operate(m_oid
, aio_comp
, &op
);
846 template <typename I
>
847 void InstanceWatcher
<I
>::handle_remove_instance_object(int r
) {
848 dout(20) << "r=" << r
<< dendl
;
850 if (m_removing
&& r
== -ENOENT
) {
855 derr
<< "error removing " << m_oid
<< " object: " << cpp_strerror(r
)
859 Mutex::Locker
locker(m_lock
);
860 unregister_instance();
863 template <typename I
>
864 void InstanceWatcher
<I
>::unregister_instance() {
867 assert(m_lock
.is_locked());
869 librados::ObjectWriteOperation op
;
870 librbd::cls_client::mirror_instances_remove(&op
, m_instance_id
);
871 librados::AioCompletion
*aio_comp
= create_rados_callback
<
872 InstanceWatcher
<I
>, &InstanceWatcher
<I
>::handle_unregister_instance
>(this);
874 int r
= m_ioctx
.aio_operate(RBD_MIRROR_LEADER
, aio_comp
, &op
);
879 template <typename I
>
880 void InstanceWatcher
<I
>::handle_unregister_instance(int r
) {
881 dout(20) << "r=" << r
<< dendl
;
884 derr
<< "error unregistering instance: " << cpp_strerror(r
) << dendl
;
887 Mutex::Locker
locker(m_lock
);
888 wait_for_notify_ops();
891 template <typename I
>
892 void InstanceWatcher
<I
>::wait_for_notify_ops() {
895 assert(m_lock
.is_locked());
897 for (auto op
: m_notify_ops
) {
901 Context
*ctx
= create_async_context_callback(
902 m_work_queue
, create_context_callback
<
903 InstanceWatcher
<I
>, &InstanceWatcher
<I
>::handle_wait_for_notify_ops
>(this));
905 m_notify_op_tracker
.wait_for_ops(ctx
);
908 template <typename I
>
909 void InstanceWatcher
<I
>::handle_wait_for_notify_ops(int r
) {
910 dout(20) << "r=" << r
<< dendl
;
914 Context
*on_finish
= nullptr;
916 Mutex::Locker
locker(m_lock
);
918 assert(m_notify_ops
.empty());
920 std::swap(on_finish
, m_on_finish
);
927 on_finish
->complete(r
);
930 template <typename I
>
931 void InstanceWatcher
<I
>::get_instance_locker() {
934 assert(m_lock
.is_locked());
936 Context
*ctx
= create_async_context_callback(
937 m_work_queue
, create_context_callback
<
938 InstanceWatcher
<I
>, &InstanceWatcher
<I
>::handle_get_instance_locker
>(this));
940 m_instance_lock
->get_locker(&m_instance_locker
, ctx
);
943 template <typename I
>
944 void InstanceWatcher
<I
>::handle_get_instance_locker(int r
) {
945 dout(20) << "r=" << r
<< dendl
;
947 Mutex::Locker
locker(m_lock
);
951 derr
<< "error retrieving instance locker: " << cpp_strerror(r
) << dendl
;
953 remove_instance_object();
957 break_instance_lock();
960 template <typename I
>
961 void InstanceWatcher
<I
>::break_instance_lock() {
964 assert(m_lock
.is_locked());
966 Context
*ctx
= create_async_context_callback(
967 m_work_queue
, create_context_callback
<
968 InstanceWatcher
<I
>, &InstanceWatcher
<I
>::handle_break_instance_lock
>(this));
970 m_instance_lock
->break_lock(m_instance_locker
, true, ctx
);
973 template <typename I
>
974 void InstanceWatcher
<I
>::handle_break_instance_lock(int r
) {
975 dout(20) << "r=" << r
<< dendl
;
977 Mutex::Locker
locker(m_lock
);
981 derr
<< "error breaking instance lock: " << cpp_strerror(r
) << dendl
;
983 remove_instance_object();
987 remove_instance_object();
990 template <typename I
>
991 void InstanceWatcher
<I
>::suspend_notify_request(C_NotifyInstanceRequest
*req
) {
992 dout(20) << req
<< dendl
;
994 assert(m_lock
.is_locked());
996 auto result
= m_suspended_ops
.insert(req
).second
;
1000 template <typename I
>
1001 bool InstanceWatcher
<I
>::unsuspend_notify_request(
1002 C_NotifyInstanceRequest
*req
) {
1003 dout(20) << req
<< dendl
;
1005 assert(m_lock
.is_locked());
1007 auto result
= m_suspended_ops
.erase(req
);
1016 template <typename I
>
1017 void InstanceWatcher
<I
>::unsuspend_notify_requests() {
1020 assert(m_lock
.is_locked());
1022 std::set
<C_NotifyInstanceRequest
*> suspended_ops
;
1023 std::swap(m_suspended_ops
, suspended_ops
);
1025 for (auto op
: suspended_ops
) {
1030 template <typename I
>
1031 Context
*InstanceWatcher
<I
>::prepare_request(const std::string
&instance_id
,
1032 uint64_t request_id
,
1033 C_NotifyAck
*on_notify_ack
) {
1034 dout(20) << "instance_id=" << instance_id
<< ", request_id=" << request_id
1037 Mutex::Locker
locker(m_lock
);
1039 Context
*ctx
= nullptr;
1040 Request
request(instance_id
, request_id
);
1041 auto it
= m_requests
.find(request
);
1043 if (it
!= m_requests
.end()) {
1044 dout(20) << "duplicate for in-progress request" << dendl
;
1045 delete it
->on_notify_ack
;
1046 m_requests
.erase(it
);
1048 ctx
= create_async_context_callback(
1049 m_work_queue
, new FunctionContext(
1050 [this, instance_id
, request_id
] (int r
) {
1051 complete_request(instance_id
, request_id
, r
);
1055 request
.on_notify_ack
= on_notify_ack
;
1056 m_requests
.insert(request
);
1060 template <typename I
>
1061 void InstanceWatcher
<I
>::complete_request(const std::string
&instance_id
,
1062 uint64_t request_id
, int r
) {
1063 dout(20) << "instance_id=" << instance_id
<< ", request_id=" << request_id
1066 C_NotifyAck
*on_notify_ack
;
1068 Mutex::Locker
locker(m_lock
);
1069 Request
request(instance_id
, request_id
);
1070 auto it
= m_requests
.find(request
);
1071 assert(it
!= m_requests
.end());
1072 on_notify_ack
= it
->on_notify_ack
;
1073 m_requests
.erase(it
);
1076 ::encode(NotifyAckPayload(instance_id
, request_id
, r
), on_notify_ack
->out
);
1077 on_notify_ack
->complete(0);
1080 template <typename I
>
1081 void InstanceWatcher
<I
>::handle_notify(uint64_t notify_id
, uint64_t handle
,
1082 uint64_t notifier_id
, bufferlist
&bl
) {
1083 dout(20) << "notify_id=" << notify_id
<< ", handle=" << handle
<< ", "
1084 << "notifier_id=" << notifier_id
<< dendl
;
1086 auto ctx
= new C_NotifyAck(this, notify_id
, handle
);
1088 NotifyMessage notify_message
;
1090 bufferlist::iterator iter
= bl
.begin();
1091 ::decode(notify_message
, iter
);
1092 } catch (const buffer::error
&err
) {
1093 derr
<< "error decoding image notification: " << err
.what() << dendl
;
1098 apply_visitor(HandlePayloadVisitor(this, stringify(notifier_id
), ctx
),
1099 notify_message
.payload
);
1102 template <typename I
>
1103 void InstanceWatcher
<I
>::handle_image_acquire(
1104 const std::string
&global_image_id
, const std::string
&peer_mirror_uuid
,
1105 const std::string
&peer_image_id
, Context
*on_finish
) {
1106 dout(20) << "global_image_id=" << global_image_id
<< dendl
;
1108 auto ctx
= new FunctionContext(
1109 [this, global_image_id
, peer_mirror_uuid
, peer_image_id
,
1110 on_finish
] (int r
) {
1111 m_instance_replayer
->acquire_image(this, global_image_id
,
1112 peer_mirror_uuid
, peer_image_id
,
1114 m_notify_op_tracker
.finish_op();
1117 m_notify_op_tracker
.start_op();
1118 m_work_queue
->queue(ctx
, 0);
1121 template <typename I
>
1122 void InstanceWatcher
<I
>::handle_image_release(
1123 const std::string
&global_image_id
, const std::string
&peer_mirror_uuid
,
1124 const std::string
&peer_image_id
, bool schedule_delete
, Context
*on_finish
) {
1125 dout(20) << "global_image_id=" << global_image_id
<< dendl
;
1127 auto ctx
= new FunctionContext(
1128 [this, global_image_id
, peer_mirror_uuid
, peer_image_id
, schedule_delete
,
1129 on_finish
] (int r
) {
1130 m_instance_replayer
->release_image(global_image_id
, peer_mirror_uuid
,
1131 peer_image_id
, schedule_delete
,
1133 m_notify_op_tracker
.finish_op();
1136 m_notify_op_tracker
.start_op();
1137 m_work_queue
->queue(ctx
, 0);
1140 template <typename I
>
1141 void InstanceWatcher
<I
>::handle_sync_request(const std::string
&instance_id
,
1142 const std::string
&sync_id
,
1143 Context
*on_finish
) {
1144 dout(20) << "instance_id=" << instance_id
<< ", sync_id=" << sync_id
<< dendl
;
1146 Mutex::Locker
locker(m_lock
);
1148 if (m_image_sync_throttler
== nullptr) {
1149 dout(20) << "sync request for non-leader" << dendl
;
1150 m_work_queue
->queue(on_finish
, -ESTALE
);
1154 Context
*on_start
= create_async_context_callback(
1155 m_work_queue
, new FunctionContext(
1156 [this, instance_id
, sync_id
, on_finish
] (int r
) {
1157 dout(20) << "handle_sync_request: finish: instance_id=" << instance_id
1158 << ", sync_id=" << sync_id
<< ", r=" << r
<< dendl
;
1160 notify_sync_start(instance_id
, sync_id
);
1162 on_finish
->complete(r
);
1164 m_image_sync_throttler
->start_op(sync_id
, on_start
);
1167 template <typename I
>
1168 void InstanceWatcher
<I
>::handle_sync_start(const std::string
&instance_id
,
1169 const std::string
&sync_id
,
1170 Context
*on_finish
) {
1171 dout(20) << "instance_id=" << instance_id
<< ", sync_id=" << sync_id
<< dendl
;
1173 Mutex::Locker
locker(m_lock
);
1175 auto it
= m_inflight_sync_reqs
.find(sync_id
);
1176 if (it
== m_inflight_sync_reqs
.end()) {
1177 dout(20) << "not found" << dendl
;
1178 m_work_queue
->queue(on_finish
, 0);
1182 auto sync_ctx
= it
->second
;
1184 if (sync_ctx
->on_complete
!= nullptr) {
1185 dout(20) << "duplicate request" << dendl
;
1186 m_work_queue
->queue(sync_ctx
->on_complete
, -ESTALE
);
1189 sync_ctx
->on_complete
= on_finish
;
1192 template <typename I
>
1193 void InstanceWatcher
<I
>::handle_payload(const std::string
&instance_id
,
1194 const ImageAcquirePayload
&payload
,
1195 C_NotifyAck
*on_notify_ack
) {
1196 dout(20) << "image_acquire: instance_id=" << instance_id
<< ", "
1197 << "request_id=" << payload
.request_id
<< dendl
;
1199 auto on_finish
= prepare_request(instance_id
, payload
.request_id
,
1201 if (on_finish
!= nullptr) {
1202 handle_image_acquire(payload
.global_image_id
, payload
.peer_mirror_uuid
,
1203 payload
.peer_image_id
, on_finish
);
1207 template <typename I
>
1208 void InstanceWatcher
<I
>::handle_payload(const std::string
&instance_id
,
1209 const ImageReleasePayload
&payload
,
1210 C_NotifyAck
*on_notify_ack
) {
1211 dout(20) << "image_release: instance_id=" << instance_id
<< ", "
1212 << "request_id=" << payload
.request_id
<< dendl
;
1214 auto on_finish
= prepare_request(instance_id
, payload
.request_id
,
1216 if (on_finish
!= nullptr) {
1217 handle_image_release(payload
.global_image_id
, payload
.peer_mirror_uuid
,
1218 payload
.peer_image_id
, payload
.schedule_delete
,
1223 template <typename I
>
1224 void InstanceWatcher
<I
>::handle_payload(const std::string
&instance_id
,
1225 const SyncRequestPayload
&payload
,
1226 C_NotifyAck
*on_notify_ack
) {
1227 dout(20) << "sync_request: instance_id=" << instance_id
<< ", "
1228 << "request_id=" << payload
.request_id
<< dendl
;
1230 auto on_finish
= prepare_request(instance_id
, payload
.request_id
,
1232 if (on_finish
== nullptr) {
1236 handle_sync_request(instance_id
, payload
.sync_id
, on_finish
);
1239 template <typename I
>
1240 void InstanceWatcher
<I
>::handle_payload(const std::string
&instance_id
,
1241 const SyncStartPayload
&payload
,
1242 C_NotifyAck
*on_notify_ack
) {
1243 dout(20) << "sync_start: instance_id=" << instance_id
<< ", "
1244 << "request_id=" << payload
.request_id
<< dendl
;
1246 auto on_finish
= prepare_request(instance_id
, payload
.request_id
,
1248 if (on_finish
== nullptr) {
1252 handle_sync_start(instance_id
, payload
.sync_id
, on_finish
);
1255 template <typename I
>
1256 void InstanceWatcher
<I
>::handle_payload(const std::string
&instance_id
,
1257 const UnknownPayload
&payload
,
1258 C_NotifyAck
*on_notify_ack
) {
1259 dout(20) << "unknown: instance_id=" << instance_id
<< dendl
;
1261 on_notify_ack
->complete(0);
1264 } // namespace mirror
1267 template class rbd::mirror::InstanceWatcher
<librbd::ImageCtx
>;