summaryrefslogtreecommitdiffstats
path: root/src/librbd/mirror/EnableRequest.cc
blob: a5c5b1255fac8c57dddb233981ac9833267994e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "librbd/mirror/EnableRequest.h"
#include "common/dout.h"
#include "common/errno.h"
#include "cls/rbd/cls_rbd_client.h"
#include "librbd/ImageState.h"
#include "librbd/Journal.h"
#include "librbd/MirroringWatcher.h"
#include "librbd/Utils.h"

#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd::mirror::EnableRequest: "

namespace librbd {
namespace mirror {

using util::create_context_callback;
using util::create_rados_callback;

template <typename I>
EnableRequest<I>::EnableRequest(librados::IoCtx &io_ctx,
                                const std::string &image_id,
                                const std::string &non_primary_global_image_id,
                                ContextWQ *op_work_queue, Context *on_finish)
  : m_io_ctx(io_ctx), m_image_id(image_id),
    m_non_primary_global_image_id(non_primary_global_image_id),
    m_op_work_queue(op_work_queue), m_on_finish(on_finish),
    m_cct(reinterpret_cast<CephContext*>(io_ctx.cct())) {
}

template <typename I>
void EnableRequest<I>::send() {
  send_get_mirror_image();
}

template <typename I>
void EnableRequest<I>::send_get_mirror_image() {
  ldout(m_cct, 10) << this << " " << __func__ << dendl;

  librados::ObjectReadOperation op;
  cls_client::mirror_image_get_start(&op, m_image_id);

  using klass = EnableRequest<I>;
  librados::AioCompletion *comp =
    create_rados_callback<klass, &klass::handle_get_mirror_image>(this);
  m_out_bl.clear();
  int r = m_io_ctx.aio_operate(RBD_MIRRORING, comp, &op, &m_out_bl);
  ceph_assert(r == 0);
  comp->release();
}

template <typename I>
Context *EnableRequest<I>::handle_get_mirror_image(int *result) {
  ldout(m_cct, 10) << this << " " << __func__ << ": r=" << *result << dendl;

  if (*result == 0) {
    auto iter = m_out_bl.cbegin();
    *result = cls_client::mirror_image_get_finish(&iter, &m_mirror_image);
  }

  if (*result == 0) {
    if (m_mirror_image.state == cls::rbd::MIRROR_IMAGE_STATE_ENABLED) {
      ldout(m_cct, 10) << this << " " << __func__
                       << ": mirroring is already enabled" << dendl;
    } else {
      lderr(m_cct) << "currently disabling" << dendl;
      *result = -EINVAL;
    }
    return m_on_finish;
  }

  if (*result != -ENOENT) {
    lderr(m_cct) << "failed to retrieve mirror image: " << cpp_strerror(*result)
                 << dendl;
    return m_on_finish;
  }

  *result = 0;
  m_mirror_image.state = cls::rbd::MIRROR_IMAGE_STATE_ENABLED;
  if (m_non_primary_global_image_id.empty()) {
    uuid_d uuid_gen;
    uuid_gen.generate_random();
    m_mirror_image.global_image_id = uuid_gen.to_string();
  } else {
    m_mirror_image.global_image_id = m_non_primary_global_image_id;
  }

  send_get_tag_owner();
  return nullptr;
}

template <typename I>
void EnableRequest<I>::send_get_tag_owner() {
  if (!m_non_primary_global_image_id.empty()) {
    send_set_mirror_image();
    return;
  }
  ldout(m_cct, 10) << this << " " << __func__ << dendl;

  using klass = EnableRequest<I>;
  Context *ctx = create_context_callback<
      klass, &klass::handle_get_tag_owner>(this);
  librbd::Journal<>::is_tag_owner(m_io_ctx, m_image_id, &m_is_primary,
                                  m_op_work_queue, ctx);
}

template <typename I>
Context *EnableRequest<I>::handle_get_tag_owner(int *result) {
  ldout(m_cct, 10) << this << " " << __func__ << ": r=" << *result << dendl;

  if (*result < 0) {
    lderr(m_cct) << "failed to check tag ownership: " << cpp_strerror(*result)
                 << dendl;
    return m_on_finish;
  }

  if (!m_is_primary) {
    lderr(m_cct) << "last journal tag not owned by local cluster" << dendl;
    *result = -EINVAL;
    return m_on_finish;
  }

  send_set_mirror_image();
  return nullptr;
}

template <typename I>
void EnableRequest<I>::send_set_mirror_image() {
  ldout(m_cct, 10) << this << " " << __func__ << dendl;

  librados::ObjectWriteOperation op;
  cls_client::mirror_image_set(&op, m_image_id, m_mirror_image);

  using klass = EnableRequest<I>;
  librados::AioCompletion *comp =
    create_rados_callback<klass, &klass::handle_set_mirror_image>(this);
  m_out_bl.clear();
  int r = m_io_ctx.aio_operate(RBD_MIRRORING, comp, &op);
  ceph_assert(r == 0);
  comp->release();
}

template <typename I>
Context *EnableRequest<I>::handle_set_mirror_image(int *result) {
  ldout(m_cct, 10) << this << " " << __func__ << ": r=" << *result << dendl;

  if (*result < 0) {
    lderr(m_cct) << "failed to enable mirroring: " << cpp_strerror(*result)
                 << dendl;
    return m_on_finish;
  }

  send_notify_mirroring_watcher();
  return nullptr;
}

template <typename I>
void EnableRequest<I>::send_notify_mirroring_watcher() {
  ldout(m_cct, 10) << this << " " << __func__ << dendl;

  using klass = EnableRequest<I>;
  Context *ctx = create_context_callback<
    klass, &klass::handle_notify_mirroring_watcher>(this);

  MirroringWatcher<>::notify_image_updated(m_io_ctx,
                                           cls::rbd::MIRROR_IMAGE_STATE_ENABLED,
                                           m_image_id,
                                           m_mirror_image.global_image_id, ctx);
}

template <typename I>
Context *EnableRequest<I>::handle_notify_mirroring_watcher(int *result) {
  ldout(m_cct, 10) << this << " " << __func__ << ": r=" << *result << dendl;

  if (*result < 0) {
    lderr(m_cct) << "failed to send update notification: "
                 << cpp_strerror(*result) << dendl;
    *result = 0;
  }

  return m_on_finish;
}

} // namespace mirror
} // namespace librbd

template class librbd::mirror::EnableRequest<librbd::ImageCtx>;