]> git.proxmox.com Git - ceph.git/blame - 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
CommitLineData
7c673cae
FG
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
19namespace rbd {
20namespace mirror {
21namespace image_replayer {
22
23using librbd::util::create_context_callback;
24using librbd::util::create_rados_callback;
25
26template <typename I>
27IsPrimaryRequest<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
32template <typename I>
33void IsPrimaryRequest<I>::send() {
34 send_get_mirror_state();
35}
36
37template <typename I>
38void 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
52template <typename I>
53void 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;
28e407b8 66 r = -ENOENT;
7c673cae
FG
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 }
28e407b8
AA
75 } else if (r == -ENOENT) {
76 dout(5) << ": image is not mirrored" << dendl;
7c673cae
FG
77 } else {
78 derr << ": failed to retrieve image mirror state: " << cpp_strerror(r)
79 << dendl;
80 }
81
82 finish(r);
83}
84
85template <typename I>
86void 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
100template <typename I>
101void 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
112template <typename I>
113void 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
124template class rbd::mirror::image_replayer::IsPrimaryRequest<librbd::ImageCtx>;