]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd_mirror/image_replayer/IsPrimaryRequest.cc
update sources to 12.2.7
[ceph.git] / 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 r = -ENOENT;
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 if (r == -ENOENT) {
76 dout(5) << ": image is not mirrored" << dendl;
77 } else {
78 derr << ": failed to retrieve image mirror state: " << cpp_strerror(r)
79 << dendl;
80 }
81
82 finish(r);
83 }
84
85 template <typename I>
86 void IsPrimaryRequest<I>::send_is_tag_owner() {
87 // deduce the class type for the journal to support unit tests
88 using Journal = typename std::decay<
89 typename std::remove_pointer<decltype(std::declval<I>().journal)>
90 ::type>::type;
91
92 dout(20) << dendl;
93
94 Context *ctx = create_context_callback<
95 IsPrimaryRequest<I>, &IsPrimaryRequest<I>::handle_is_tag_owner>(this);
96
97 Journal::is_tag_owner(m_image_ctx, m_primary, ctx);
98 }
99
100 template <typename I>
101 void IsPrimaryRequest<I>::handle_is_tag_owner(int r) {
102 dout(20) << ": r=" << r << dendl;
103
104 if (r < 0) {
105 derr << ": failed to query remote image tag owner: " << cpp_strerror(r)
106 << dendl;
107 }
108
109 finish(r);
110 }
111
112 template <typename I>
113 void IsPrimaryRequest<I>::finish(int r) {
114 dout(20) << ": r=" << r << dendl;
115
116 m_on_finish->complete(r);
117 delete this;
118 }
119
120 } // namespace image_replayer
121 } // namespace mirror
122 } // namespace rbd
123
124 template class rbd::mirror::image_replayer::IsPrimaryRequest<librbd::ImageCtx>;