1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "librbd/MirroringWatcher.h"
5 #include "include/rbd_types.h"
6 #include "include/rados/librados.hpp"
7 #include "common/errno.h"
8 #include "common/Cond.h"
9 #include "librbd/Utils.h"
10 #include "librbd/watcher/Utils.h"
12 #define dout_subsys ceph_subsys_rbd
14 #define dout_prefix *_dout << "librbd::MirroringWatcher: "
18 using namespace mirroring_watcher
;
19 using namespace watcher
;
21 using librbd::util::create_rados_callback
;
25 static const uint64_t NOTIFY_TIMEOUT_MS
= 5000;
27 } // anonymous namespace
30 MirroringWatcher
<I
>::MirroringWatcher(librados::IoCtx
&io_ctx
,
31 asio::ContextWQ
*work_queue
)
32 : Watcher(io_ctx
, work_queue
, RBD_MIRRORING
) {
36 int MirroringWatcher
<I
>::notify_mode_updated(librados::IoCtx
&io_ctx
,
37 cls::rbd::MirrorMode mirror_mode
) {
39 notify_mode_updated(io_ctx
, mirror_mode
, &ctx
);
44 void MirroringWatcher
<I
>::notify_mode_updated(librados::IoCtx
&io_ctx
,
45 cls::rbd::MirrorMode mirror_mode
,
47 CephContext
*cct
= reinterpret_cast<CephContext
*>(io_ctx
.cct());
48 ldout(cct
, 20) << dendl
;
51 encode(NotifyMessage
{ModeUpdatedPayload
{mirror_mode
}}, bl
);
53 librados::AioCompletion
*comp
= create_rados_callback(on_finish
);
54 int r
= io_ctx
.aio_notify(RBD_MIRRORING
, comp
, bl
, NOTIFY_TIMEOUT_MS
,
61 int MirroringWatcher
<I
>::notify_image_updated(
62 librados::IoCtx
&io_ctx
, cls::rbd::MirrorImageState mirror_image_state
,
63 const std::string
&image_id
, const std::string
&global_image_id
) {
65 notify_image_updated(io_ctx
, mirror_image_state
, image_id
, global_image_id
,
71 void MirroringWatcher
<I
>::notify_image_updated(
72 librados::IoCtx
&io_ctx
, cls::rbd::MirrorImageState mirror_image_state
,
73 const std::string
&image_id
, const std::string
&global_image_id
,
76 CephContext
*cct
= reinterpret_cast<CephContext
*>(io_ctx
.cct());
77 ldout(cct
, 20) << dendl
;
80 encode(NotifyMessage
{ImageUpdatedPayload
{
81 mirror_image_state
, image_id
, global_image_id
}}, bl
);
83 librados::AioCompletion
*comp
= create_rados_callback(on_finish
);
84 int r
= io_ctx
.aio_notify(RBD_MIRRORING
, comp
, bl
, NOTIFY_TIMEOUT_MS
,
92 void MirroringWatcher
<I
>::handle_notify(uint64_t notify_id
, uint64_t handle
,
93 uint64_t notifier_id
, bufferlist
&bl
) {
94 CephContext
*cct
= this->m_cct
;
95 ldout(cct
, 15) << ": notify_id=" << notify_id
<< ", "
96 << "handle=" << handle
<< dendl
;
99 NotifyMessage notify_message
;
101 auto iter
= bl
.cbegin();
102 decode(notify_message
, iter
);
103 } catch (const buffer::error
&err
) {
104 lderr(cct
) << ": error decoding image notification: " << err
.what()
106 Context
*ctx
= new C_NotifyAck(this, notify_id
, handle
);
111 apply_visitor(watcher::util::HandlePayloadVisitor
<MirroringWatcher
<I
>>(
112 this, notify_id
, handle
), notify_message
.payload
);
115 template <typename I
>
116 bool MirroringWatcher
<I
>::handle_payload(const ModeUpdatedPayload
&payload
,
117 Context
*on_notify_ack
) {
118 CephContext
*cct
= this->m_cct
;
119 ldout(cct
, 20) << ": mode updated: " << payload
.mirror_mode
<< dendl
;
120 handle_mode_updated(payload
.mirror_mode
);
124 template <typename I
>
125 bool MirroringWatcher
<I
>::handle_payload(const ImageUpdatedPayload
&payload
,
126 Context
*on_notify_ack
) {
127 CephContext
*cct
= this->m_cct
;
128 ldout(cct
, 20) << ": image state updated" << dendl
;
129 handle_image_updated(payload
.mirror_image_state
, payload
.image_id
,
130 payload
.global_image_id
);
134 template <typename I
>
135 bool MirroringWatcher
<I
>::handle_payload(const UnknownPayload
&payload
,
136 Context
*on_notify_ack
) {
140 } // namespace librbd
142 template class librbd::MirroringWatcher
<librbd::ImageCtx
>;