]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/MirroringWatcher.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / librbd / MirroringWatcher.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
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"
11
12 #define dout_subsys ceph_subsys_rbd
13 #undef dout_prefix
14 #define dout_prefix *_dout << "librbd::MirroringWatcher: "
15
16 namespace librbd {
17
18 using namespace mirroring_watcher;
19 using namespace watcher;
20
21 using librbd::util::create_rados_callback;
22
23 namespace {
24
25 static const uint64_t NOTIFY_TIMEOUT_MS = 5000;
26
27 } // anonymous namespace
28
29 template <typename I>
30 MirroringWatcher<I>::MirroringWatcher(librados::IoCtx &io_ctx,
31 ContextWQ *work_queue)
32 : Watcher(io_ctx, work_queue, RBD_MIRRORING) {
33 }
34
35 template <typename I>
36 int MirroringWatcher<I>::notify_mode_updated(librados::IoCtx &io_ctx,
37 cls::rbd::MirrorMode mirror_mode) {
38 C_SaferCond ctx;
39 notify_mode_updated(io_ctx, mirror_mode, &ctx);
40 return ctx.wait();
41 }
42
43 template <typename I>
44 void MirroringWatcher<I>::notify_mode_updated(librados::IoCtx &io_ctx,
45 cls::rbd::MirrorMode mirror_mode,
46 Context *on_finish) {
47 CephContext *cct = reinterpret_cast<CephContext*>(io_ctx.cct());
48 ldout(cct, 20) << dendl;
49
50 bufferlist bl;
51 encode(NotifyMessage{ModeUpdatedPayload{mirror_mode}}, bl);
52
53 librados::AioCompletion *comp = create_rados_callback(on_finish);
54 int r = io_ctx.aio_notify(RBD_MIRRORING, comp, bl, NOTIFY_TIMEOUT_MS,
55 nullptr);
56 ceph_assert(r == 0);
57 comp->release();
58 }
59
60 template <typename I>
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) {
64 C_SaferCond ctx;
65 notify_image_updated(io_ctx, mirror_image_state, image_id, global_image_id,
66 &ctx);
67 return ctx.wait();
68 }
69
70 template <typename I>
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,
74 Context *on_finish) {
75
76 CephContext *cct = reinterpret_cast<CephContext*>(io_ctx.cct());
77 ldout(cct, 20) << dendl;
78
79 bufferlist bl;
80 encode(NotifyMessage{ImageUpdatedPayload{
81 mirror_image_state, image_id, global_image_id}}, bl);
82
83 librados::AioCompletion *comp = create_rados_callback(on_finish);
84 int r = io_ctx.aio_notify(RBD_MIRRORING, comp, bl, NOTIFY_TIMEOUT_MS,
85 nullptr);
86 ceph_assert(r == 0);
87 comp->release();
88
89 }
90
91 template <typename I>
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;
97
98
99 NotifyMessage notify_message;
100 try {
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()
105 << dendl;
106 Context *ctx = new C_NotifyAck(this, notify_id, handle);
107 ctx->complete(0);
108 return;
109 }
110
111 apply_visitor(watcher::util::HandlePayloadVisitor<MirroringWatcher<I>>(
112 this, notify_id, handle), notify_message.payload);
113 }
114
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);
121 return true;
122 }
123
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);
131 return true;
132 }
133
134 template <typename I>
135 bool MirroringWatcher<I>::handle_payload(const UnknownPayload &payload,
136 Context *on_notify_ack) {
137 return true;
138 }
139
140 } // namespace librbd
141
142 template class librbd::MirroringWatcher<librbd::ImageCtx>;