1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
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>
13 #define dout_context g_ceph_context
14 #define dout_subsys ceph_subsys_rbd_mirror
16 #define dout_prefix *_dout << "rbd::mirror::image_replayer::IsPrimaryRequest: " \
17 << this << " " << __func__ << " "
21 namespace image_replayer
{
23 using librbd::util::create_context_callback
;
24 using librbd::util::create_rados_callback
;
27 IsPrimaryRequest
<I
>::IsPrimaryRequest(I
*image_ctx
, bool *primary
,
29 : m_image_ctx(image_ctx
), m_primary(primary
), m_on_finish(on_finish
) {
33 void IsPrimaryRequest
<I
>::send() {
34 send_get_mirror_state();
38 void IsPrimaryRequest
<I
>::send_get_mirror_state() {
41 librados::ObjectReadOperation op
;
42 librbd::cls_client::mirror_image_get_start(&op
, m_image_ctx
->id
);
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
,
53 void IsPrimaryRequest
<I
>::handle_get_mirror_state(int r
) {
54 dout(20) << ": r=" << r
<< dendl
;
56 cls::rbd::MirrorImage mirror_image
;
58 bufferlist::iterator iter
= m_out_bl
.begin();
59 r
= librbd::cls_client::mirror_image_get_finish(&iter
, &mirror_image
);
61 if (mirror_image
.state
== cls::rbd::MIRROR_IMAGE_STATE_ENABLED
) {
64 } else if (mirror_image
.state
== cls::rbd::MIRROR_IMAGE_STATE_DISABLING
) {
65 dout(5) << ": image mirroring is being disabled" << dendl
;
68 derr
<< ": image mirroring is disabled" << dendl
;
72 derr
<< ": failed to decode image mirror state: " << cpp_strerror(r
)
76 derr
<< ": failed to retrieve image mirror state: " << cpp_strerror(r
)
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
)>
92 Context
*ctx
= create_context_callback
<
93 IsPrimaryRequest
<I
>, &IsPrimaryRequest
<I
>::handle_is_tag_owner
>(this);
95 Journal::is_tag_owner(m_image_ctx
, m_primary
, ctx
);
99 void IsPrimaryRequest
<I
>::handle_is_tag_owner(int r
) {
100 dout(20) << ": r=" << r
<< dendl
;
103 derr
<< ": failed to query remote image tag owner: " << cpp_strerror(r
)
110 template <typename I
>
111 void IsPrimaryRequest
<I
>::finish(int r
) {
112 dout(20) << ": r=" << r
<< dendl
;
114 m_on_finish
->complete(r
);
118 } // namespace image_replayer
119 } // namespace mirror
122 template class rbd::mirror::image_replayer::IsPrimaryRequest
<librbd::ImageCtx
>;