Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd_mirror / image_replayer / IsPrimaryRequest.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 "IsPrimaryRequest.h"
5 #include "common/errno.h"
6 #include "common/WorkQueue.h"
7 #include "cls/rbd/cls_rbd_client.h"
8 #include "librbd/ImageCtx.h"
9 #include "librbd/Journal.h"
10 #include "librbd/Utils.h"
11 #include <type_traits>
12
13 #define dout_context g_ceph_context
14 #define dout_subsys ceph_subsys_rbd_mirror
15 #undef dout_prefix
16 #define dout_prefix *_dout << "rbd::mirror::image_replayer::IsPrimaryRequest: " \
17                            << this << " " << __func__ << " "
18
19 namespace rbd {
20 namespace mirror {
21 namespace image_replayer {
22
23 using librbd::util::create_context_callback;
24 using librbd::util::create_rados_callback;
25
26 template <typename I>
27 IsPrimaryRequest<I>::IsPrimaryRequest(I *image_ctx, bool *primary,
28                                       Context *on_finish)
29   : m_image_ctx(image_ctx), m_primary(primary), m_on_finish(on_finish) {
30 }
31
32 template <typename I>
33 void IsPrimaryRequest<I>::send() {
34   send_get_mirror_state();
35 }
36
37 template <typename I>
38 void IsPrimaryRequest<I>::send_get_mirror_state() {
39   dout(20) << dendl;
40
41   librados::ObjectReadOperation op;
42   librbd::cls_client::mirror_image_get_start(&op, m_image_ctx->id);
43
44   librados::AioCompletion *aio_comp = create_rados_callback<
45     IsPrimaryRequest<I>, &IsPrimaryRequest<I>::handle_get_mirror_state>(this);
46   int r = m_image_ctx->md_ctx.aio_operate(RBD_MIRRORING, aio_comp, &op,
47                                           &m_out_bl);
48   assert(r == 0);
49   aio_comp->release();
50 }
51
52 template <typename I>
53 void IsPrimaryRequest<I>::handle_get_mirror_state(int r) {
54   dout(20) << ": r=" << r << dendl;
55
56   cls::rbd::MirrorImage mirror_image;
57   if (r == 0) {
58     bufferlist::iterator iter = m_out_bl.begin();
59     r = librbd::cls_client::mirror_image_get_finish(&iter, &mirror_image);
60     if (r == 0) {
61       if (mirror_image.state == cls::rbd::MIRROR_IMAGE_STATE_ENABLED) {
62         send_is_tag_owner();
63         return;
64       } else if (mirror_image.state == cls::rbd::MIRROR_IMAGE_STATE_DISABLING) {
65         dout(5) << ": image mirroring is being disabled" << dendl;
66         *m_primary = false;
67       } else {
68         derr << ": image mirroring is disabled" << dendl;
69         r = -EINVAL;
70       }
71     } else {
72       derr << ": failed to decode image mirror state: " << cpp_strerror(r)
73            << dendl;
74     }
75   } else {
76     derr << ": failed to retrieve image mirror state: " << cpp_strerror(r)
77          << dendl;
78   }
79
80   finish(r);
81 }
82
83 template <typename I>
84 void IsPrimaryRequest<I>::send_is_tag_owner() {
85   // deduce the class type for the journal to support unit tests
86   using Journal = typename std::decay<
87     typename std::remove_pointer<decltype(std::declval<I>().journal)>
88     ::type>::type;
89
90   dout(20) << dendl;
91
92   Context *ctx = create_context_callback<
93     IsPrimaryRequest<I>, &IsPrimaryRequest<I>::handle_is_tag_owner>(this);
94
95   Journal::is_tag_owner(m_image_ctx, m_primary, ctx);
96 }
97
98 template <typename I>
99 void IsPrimaryRequest<I>::handle_is_tag_owner(int r) {
100   dout(20) << ": r=" << r << dendl;
101
102   if (r < 0) {
103     derr << ": failed to query remote image tag owner: " << cpp_strerror(r)
104          << dendl;
105   }
106
107   finish(r);
108 }
109
110 template <typename I>
111 void IsPrimaryRequest<I>::finish(int r) {
112   dout(20) << ": r=" << r << dendl;
113
114   m_on_finish->complete(r);
115   delete this;
116 }
117
118 } // namespace image_replayer
119 } // namespace mirror
120 } // namespace rbd
121
122 template class rbd::mirror::image_replayer::IsPrimaryRequest<librbd::ImageCtx>;