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 "librbd/Utils.h"
9 #include "librbd/watcher/Utils.h"
11 #define dout_subsys ceph_subsys_rbd
13 #define dout_prefix *_dout << "librbd::MirroringWatcher: "
17 using namespace mirroring_watcher
;
18 using namespace watcher
;
20 using librbd::util::create_rados_callback
;
24 static const uint64_t NOTIFY_TIMEOUT_MS
= 5000;
26 } // anonymous namespace
29 MirroringWatcher
<I
>::MirroringWatcher(librados::IoCtx
&io_ctx
,
30 ContextWQ
*work_queue
)
31 : Watcher(io_ctx
, work_queue
, RBD_MIRRORING
) {
35 int MirroringWatcher
<I
>::notify_mode_updated(librados::IoCtx
&io_ctx
,
36 cls::rbd::MirrorMode mirror_mode
) {
38 notify_mode_updated(io_ctx
, mirror_mode
, &ctx
);
43 void MirroringWatcher
<I
>::notify_mode_updated(librados::IoCtx
&io_ctx
,
44 cls::rbd::MirrorMode mirror_mode
,
46 CephContext
*cct
= reinterpret_cast<CephContext
*>(io_ctx
.cct());
47 ldout(cct
, 20) << dendl
;
50 ::encode(NotifyMessage
{ModeUpdatedPayload
{mirror_mode
}}, bl
);
52 librados::AioCompletion
*comp
= create_rados_callback(on_finish
);
53 int r
= io_ctx
.aio_notify(RBD_MIRRORING
, comp
, bl
, NOTIFY_TIMEOUT_MS
,
60 int MirroringWatcher
<I
>::notify_image_updated(
61 librados::IoCtx
&io_ctx
, cls::rbd::MirrorImageState mirror_image_state
,
62 const std::string
&image_id
, const std::string
&global_image_id
) {
64 notify_image_updated(io_ctx
, mirror_image_state
, image_id
, global_image_id
,
70 void MirroringWatcher
<I
>::notify_image_updated(
71 librados::IoCtx
&io_ctx
, cls::rbd::MirrorImageState mirror_image_state
,
72 const std::string
&image_id
, const std::string
&global_image_id
,
75 CephContext
*cct
= reinterpret_cast<CephContext
*>(io_ctx
.cct());
76 ldout(cct
, 20) << dendl
;
79 ::encode(NotifyMessage
{ImageUpdatedPayload
{
80 mirror_image_state
, image_id
, global_image_id
}}, bl
);
82 librados::AioCompletion
*comp
= create_rados_callback(on_finish
);
83 int r
= io_ctx
.aio_notify(RBD_MIRRORING
, comp
, bl
, NOTIFY_TIMEOUT_MS
,
91 void MirroringWatcher
<I
>::handle_notify(uint64_t notify_id
, uint64_t handle
,
92 uint64_t notifier_id
, bufferlist
&bl
) {
93 CephContext
*cct
= this->m_cct
;
94 ldout(cct
, 15) << ": notify_id=" << notify_id
<< ", "
95 << "handle=" << handle
<< dendl
;
98 NotifyMessage notify_message
;
100 bufferlist::iterator iter
= bl
.begin();
101 ::decode(notify_message
, iter
);
102 } catch (const buffer::error
&err
) {
103 lderr(cct
) << ": error decoding image notification: " << err
.what()
105 Context
*ctx
= new C_NotifyAck(this, notify_id
, handle
);
110 apply_visitor(watcher::util::HandlePayloadVisitor
<MirroringWatcher
<I
>>(
111 this, notify_id
, handle
), notify_message
.payload
);
114 template <typename I
>
115 bool MirroringWatcher
<I
>::handle_payload(const ModeUpdatedPayload
&payload
,
116 Context
*on_notify_ack
) {
117 CephContext
*cct
= this->m_cct
;
118 ldout(cct
, 20) << ": mode updated: " << payload
.mirror_mode
<< dendl
;
119 handle_mode_updated(payload
.mirror_mode
);
123 template <typename I
>
124 bool MirroringWatcher
<I
>::handle_payload(const ImageUpdatedPayload
&payload
,
125 Context
*on_notify_ack
) {
126 CephContext
*cct
= this->m_cct
;
127 ldout(cct
, 20) << ": image state updated" << dendl
;
128 handle_image_updated(payload
.mirror_image_state
, payload
.image_id
,
129 payload
.global_image_id
);
133 template <typename I
>
134 bool MirroringWatcher
<I
>::handle_payload(const UnknownPayload
&payload
,
135 Context
*on_notify_ack
) {
139 } // namespace librbd
141 template class librbd::MirroringWatcher
<librbd::ImageCtx
>;