Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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 "librbd/Utils.h"
9 #include "librbd/watcher/Utils.h"
10
11 #define dout_subsys ceph_subsys_rbd
12 #undef dout_prefix
13 #define dout_prefix *_dout << "librbd::MirroringWatcher: "
14
15 namespace librbd {
16
17 using namespace mirroring_watcher;
18 using namespace watcher;
19
20 using librbd::util::create_rados_callback;
21
22 namespace {
23
24 static const uint64_t NOTIFY_TIMEOUT_MS = 5000;
25
26 } // anonymous namespace
27
28 template <typename I>
29 MirroringWatcher<I>::MirroringWatcher(librados::IoCtx &io_ctx,
30                                       ContextWQ *work_queue)
31   : Watcher(io_ctx, work_queue, RBD_MIRRORING) {
32 }
33
34 template <typename I>
35 int MirroringWatcher<I>::notify_mode_updated(librados::IoCtx &io_ctx,
36                                               cls::rbd::MirrorMode mirror_mode) {
37   C_SaferCond ctx;
38   notify_mode_updated(io_ctx, mirror_mode, &ctx);
39   return ctx.wait();
40 }
41
42 template <typename I>
43 void MirroringWatcher<I>::notify_mode_updated(librados::IoCtx &io_ctx,
44                                               cls::rbd::MirrorMode mirror_mode,
45                                               Context *on_finish) {
46   CephContext *cct = reinterpret_cast<CephContext*>(io_ctx.cct());
47   ldout(cct, 20) << dendl;
48
49   bufferlist bl;
50   ::encode(NotifyMessage{ModeUpdatedPayload{mirror_mode}}, bl);
51
52   librados::AioCompletion *comp = create_rados_callback(on_finish);
53   int r = io_ctx.aio_notify(RBD_MIRRORING, comp, bl, NOTIFY_TIMEOUT_MS,
54                             nullptr);
55   assert(r == 0);
56   comp->release();
57 }
58
59 template <typename I>
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) {
63   C_SaferCond ctx;
64   notify_image_updated(io_ctx, mirror_image_state, image_id, global_image_id,
65                        &ctx);
66   return ctx.wait();
67 }
68
69 template <typename I>
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,
73     Context *on_finish) {
74
75   CephContext *cct = reinterpret_cast<CephContext*>(io_ctx.cct());
76   ldout(cct, 20) << dendl;
77
78   bufferlist bl;
79   ::encode(NotifyMessage{ImageUpdatedPayload{
80       mirror_image_state, image_id, global_image_id}}, bl);
81
82   librados::AioCompletion *comp = create_rados_callback(on_finish);
83   int r = io_ctx.aio_notify(RBD_MIRRORING, comp, bl, NOTIFY_TIMEOUT_MS,
84                             nullptr);
85   assert(r == 0);
86   comp->release();
87
88 }
89
90 template <typename I>
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;
96
97
98   NotifyMessage notify_message;
99   try {
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()
104                << dendl;
105     Context *ctx = new C_NotifyAck(this, notify_id, handle);
106     ctx->complete(0);
107     return;
108   }
109
110   apply_visitor(watcher::util::HandlePayloadVisitor<MirroringWatcher<I>>(
111                   this, notify_id, handle), notify_message.payload);
112 }
113
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);
120   return true;
121 }
122
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);
130   return true;
131 }
132
133 template <typename I>
134 bool MirroringWatcher<I>::handle_payload(const UnknownPayload &payload,
135                                          Context *on_notify_ack) {
136   return true;
137 }
138
139 } // namespace librbd
140
141 template class librbd::MirroringWatcher<librbd::ImageCtx>;