Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd_mirror / image_replayer / PrepareLocalImageRequest.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 "tools/rbd_mirror/image_replayer/PrepareLocalImageRequest.h"
5 #include "include/rados/librados.hpp"
6 #include "cls/rbd/cls_rbd_client.h"
7 #include "common/errno.h"
8 #include "librbd/ImageCtx.h"
9 #include "librbd/Journal.h"
10 #include "librbd/Utils.h"
11 #include "tools/rbd_mirror/Threads.h"
12 #include "tools/rbd_mirror/image_replayer/GetMirrorImageIdRequest.h"
13 #include <type_traits>
14
15 #define dout_context g_ceph_context
16 #define dout_subsys ceph_subsys_rbd_mirror
17 #undef dout_prefix
18 #define dout_prefix *_dout << "rbd::mirror::image_replayer::" \
19                            << "PrepareLocalImageRequest: " << this << " " \
20                            << __func__ << ": "
21
22 namespace rbd {
23 namespace mirror {
24 namespace image_replayer {
25
26 using librbd::util::create_context_callback;
27 using librbd::util::create_rados_callback;
28
29 template <typename I>
30 void PrepareLocalImageRequest<I>::send() {
31   dout(20) << dendl;
32   get_local_image_id();
33 }
34
35 template <typename I>
36 void PrepareLocalImageRequest<I>::get_local_image_id() {
37   dout(20) << dendl;
38
39   Context *ctx = create_context_callback<
40     PrepareLocalImageRequest<I>,
41     &PrepareLocalImageRequest<I>::handle_get_local_image_id>(this);
42   auto req = GetMirrorImageIdRequest<I>::create(m_io_ctx, m_global_image_id,
43                                                 m_local_image_id, ctx);
44   req->send();
45 }
46
47 template <typename I>
48 void PrepareLocalImageRequest<I>::handle_get_local_image_id(int r) {
49   dout(20) << "r=" << r << ", "
50            << "local_image_id=" << *m_local_image_id << dendl;
51
52   if (r < 0) {
53     finish(r);
54     return;
55   }
56
57   get_mirror_state();
58 }
59
60 template <typename I>
61 void PrepareLocalImageRequest<I>::get_mirror_state() {
62   dout(20) << dendl;
63
64   librados::ObjectReadOperation op;
65   librbd::cls_client::mirror_image_get_start(&op, *m_local_image_id);
66
67   m_out_bl.clear();
68   librados::AioCompletion *aio_comp = create_rados_callback<
69     PrepareLocalImageRequest<I>,
70     &PrepareLocalImageRequest<I>::handle_get_mirror_state>(this);
71   int r = m_io_ctx.aio_operate(RBD_MIRRORING, aio_comp, &op, &m_out_bl);
72   assert(r == 0);
73   aio_comp->release();
74 }
75
76 template <typename I>
77 void PrepareLocalImageRequest<I>::handle_get_mirror_state(int r) {
78   dout(20) << ": r=" << r << dendl;
79
80   cls::rbd::MirrorImage mirror_image;
81   if (r == 0) {
82     bufferlist::iterator iter = m_out_bl.begin();
83     r = librbd::cls_client::mirror_image_get_finish(&iter, &mirror_image);
84   }
85
86   if (r < 0) {
87     derr << "failed to retrieve image mirror state: " << cpp_strerror(r)
88          << dendl;
89     finish(r);
90     return;
91   }
92
93   // TODO save current mirror state to determine if we should
94   // delete a partially formed image
95   // (e.g. MIRROR_IMAGE_STATE_CREATING/DELETING)
96
97   get_tag_owner();
98 }
99
100 template <typename I>
101 void PrepareLocalImageRequest<I>::get_tag_owner() {
102   // deduce the class type for the journal to support unit tests
103   using Journal = typename std::decay<
104     typename std::remove_pointer<decltype(std::declval<I>().journal)>
105     ::type>::type;
106
107   dout(20) << dendl;
108
109   Context *ctx = create_context_callback<
110     PrepareLocalImageRequest<I>,
111     &PrepareLocalImageRequest<I>::handle_get_tag_owner>(this);
112   Journal::get_tag_owner(m_io_ctx, *m_local_image_id, m_tag_owner,
113                          m_work_queue, ctx);
114 }
115
116 template <typename I>
117 void PrepareLocalImageRequest<I>::handle_get_tag_owner(int r) {
118   dout(20) << "r=" << r << ", "
119            << "tag_owner=" << *m_tag_owner << dendl;
120
121   if (r < 0) {
122     derr << "failed to retrieve journal tag owner: " << cpp_strerror(r)
123          << dendl;
124     finish(r);
125     return;
126   }
127
128   finish(0);
129 }
130
131 template <typename I>
132 void PrepareLocalImageRequest<I>::finish(int r) {
133   dout(20) << "r=" << r << dendl;
134
135   m_on_finish->complete(r);
136   delete this;
137 }
138
139 } // namespace image_replayer
140 } // namespace mirror
141 } // namespace rbd
142
143 template class rbd::mirror::image_replayer::PrepareLocalImageRequest<librbd::ImageCtx>;