1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "ImageCopyRequest.h"
5 #include "ObjectCopyRequest.h"
6 #include "include/stringify.h"
7 #include "common/errno.h"
8 #include "common/Timer.h"
9 #include "journal/Journaler.h"
10 #include "librbd/Utils.h"
11 #include "tools/rbd_mirror/ProgressContext.h"
13 #define dout_context g_ceph_context
14 #define dout_subsys ceph_subsys_rbd_mirror
16 #define dout_prefix *_dout << "rbd::mirror::image_sync::ImageCopyRequest: " \
17 << this << " " << __func__
21 namespace image_sync {
23 using librbd::util::create_context_callback;
24 using librbd::util::unique_lock_name;
27 ImageCopyRequest<I>::ImageCopyRequest(I *local_image_ctx, I *remote_image_ctx,
28 SafeTimer *timer, Mutex *timer_lock,
30 MirrorPeerClientMeta *client_meta,
31 MirrorPeerSyncPoint *sync_point,
33 ProgressContext *progress_ctx)
34 : BaseRequest("rbd::mirror::image_sync::ImageCopyRequest",
35 local_image_ctx->cct, on_finish),
36 m_local_image_ctx(local_image_ctx), m_remote_image_ctx(remote_image_ctx),
37 m_timer(timer), m_timer_lock(timer_lock), m_journaler(journaler),
38 m_client_meta(client_meta), m_sync_point(sync_point),
39 m_progress_ctx(progress_ctx),
40 m_lock(unique_lock_name("ImageCopyRequest::m_lock", this)),
41 m_updating_sync_point(false), m_update_sync_ctx(nullptr),
42 m_update_sync_point_interval(m_local_image_ctx->cct->_conf->template get_val<double>(
43 "rbd_mirror_sync_point_update_age")),
44 m_client_meta_copy(*client_meta) {
45 assert(!m_client_meta_copy.sync_points.empty());
49 void ImageCopyRequest<I>::send() {
50 int r = compute_snap_map();
56 send_update_max_object_count();
60 void ImageCopyRequest<I>::cancel() {
61 Mutex::Locker locker(m_lock);
68 void ImageCopyRequest<I>::send_update_max_object_count() {
69 uint64_t max_objects = m_client_meta->sync_object_count;
71 RWLock::RLocker snap_locker(m_remote_image_ctx->snap_lock);
72 max_objects = std::max(max_objects,
73 m_remote_image_ctx->get_object_count(CEPH_NOSNAP));
74 for (auto snap_id : m_remote_image_ctx->snaps) {
75 max_objects = std::max(max_objects,
76 m_remote_image_ctx->get_object_count(snap_id));
80 if (max_objects <= m_client_meta->sync_object_count) {
85 update_progress("UPDATE_MAX_OBJECT_COUNT");
87 dout(20) << ": sync_object_count=" << max_objects << dendl;
89 m_client_meta_copy = *m_client_meta;
90 m_client_meta_copy.sync_object_count = max_objects;
92 bufferlist client_data_bl;
93 librbd::journal::ClientData client_data(m_client_meta_copy);
94 ::encode(client_data, client_data_bl);
96 Context *ctx = create_context_callback<
97 ImageCopyRequest<I>, &ImageCopyRequest<I>::handle_update_max_object_count>(
99 m_journaler->update_client(client_data_bl, ctx);
102 template <typename I>
103 void ImageCopyRequest<I>::handle_update_max_object_count(int r) {
104 dout(20) << ": r=" << r << dendl;
107 Mutex::Locker locker(m_lock);
109 dout(10) << ": image copy canceled" << dendl;
115 if (r != -ECANCELED) {
116 derr << ": failed to update client data: " << cpp_strerror(r) << dendl;
122 // update provided meta structure to reflect reality
123 m_client_meta->sync_object_count = m_client_meta_copy.sync_object_count;
125 send_object_copies();
128 template <typename I>
129 void ImageCopyRequest<I>::send_object_copies() {
130 CephContext *cct = m_local_image_ctx->cct;
133 if (m_sync_point->object_number) {
134 m_object_no = *m_sync_point->object_number + 1;
136 m_end_object_no = m_client_meta->sync_object_count;
138 dout(20) << ": start_object=" << m_object_no << ", "
139 << "end_object=" << m_end_object_no << dendl;
141 update_progress("COPY_OBJECT");
145 Mutex::Locker locker(m_lock);
146 for (int i = 0; i < cct->_conf->get_val<int64_t>("rbd_concurrent_management_ops"); ++i) {
147 send_next_object_copy();
148 if (m_ret_val < 0 && m_current_ops == 0) {
152 complete = (m_current_ops == 0);
155 m_update_sync_ctx = new FunctionContext([this](int r) {
156 this->send_update_sync_point();
162 Mutex::Locker timer_locker(*m_timer_lock);
163 if (m_update_sync_ctx) {
164 m_update_sync_ctx = m_timer->add_event_after(
165 m_update_sync_point_interval,
171 send_flush_sync_point();
175 template <typename I>
176 void ImageCopyRequest<I>::send_next_object_copy() {
177 assert(m_lock.is_locked());
179 if (m_canceled && m_ret_val == 0) {
180 dout(10) << ": image copy canceled" << dendl;
181 m_ret_val = -ECANCELED;
184 if (m_ret_val < 0 || m_object_no >= m_end_object_no) {
188 uint64_t ono = m_object_no++;
190 dout(20) << ": object_num=" << ono << dendl;
194 Context *ctx = create_context_callback<
195 ImageCopyRequest<I>, &ImageCopyRequest<I>::handle_object_copy>(this);
196 ObjectCopyRequest<I> *req = ObjectCopyRequest<I>::create(
197 m_local_image_ctx, m_remote_image_ctx, &m_snap_map, ono, ctx);
201 template <typename I>
202 void ImageCopyRequest<I>::handle_object_copy(int r) {
203 dout(20) << ": r=" << r << dendl;
208 Mutex::Locker locker(m_lock);
209 assert(m_current_ops > 0);
212 percent = 100 * m_object_no / m_end_object_no;
215 derr << ": object copy failed: " << cpp_strerror(r) << dendl;
216 if (m_ret_val == 0) {
221 send_next_object_copy();
222 complete = (m_current_ops == 0);
225 update_progress("COPY_OBJECT " + stringify(percent) + "%", false);
228 bool do_flush = true;
230 Mutex::Locker timer_locker(*m_timer_lock);
231 Mutex::Locker locker(m_lock);
232 if (!m_updating_sync_point) {
233 if (m_update_sync_ctx != nullptr) {
234 m_timer->cancel_event(m_update_sync_ctx);
235 m_update_sync_ctx = nullptr;
243 send_flush_sync_point();
248 template <typename I>
249 void ImageCopyRequest<I>::send_update_sync_point() {
250 Mutex::Locker l(m_lock);
252 m_update_sync_ctx = nullptr;
254 if (m_canceled || m_ret_val < 0 || m_current_ops == 0) {
258 if (m_sync_point->object_number &&
259 (m_object_no-1) == m_sync_point->object_number.get()) {
260 // update sync point did not progress since last sync
264 m_updating_sync_point = true;
266 m_client_meta_copy = *m_client_meta;
267 m_sync_point->object_number = m_object_no - 1;
269 CephContext *cct = m_local_image_ctx->cct;
270 ldout(cct, 20) << ": sync_point=" << *m_sync_point << dendl;
272 bufferlist client_data_bl;
273 librbd::journal::ClientData client_data(*m_client_meta);
274 ::encode(client_data, client_data_bl);
276 Context *ctx = create_context_callback<
277 ImageCopyRequest<I>, &ImageCopyRequest<I>::handle_update_sync_point>(
279 m_journaler->update_client(client_data_bl, ctx);
282 template <typename I>
283 void ImageCopyRequest<I>::handle_update_sync_point(int r) {
284 CephContext *cct = m_local_image_ctx->cct;
285 ldout(cct, 20) << ": r=" << r << dendl;
288 *m_client_meta = m_client_meta_copy;
289 lderr(cct) << ": failed to update client data: " << cpp_strerror(r)
295 Mutex::Locker l(m_lock);
296 m_updating_sync_point = false;
298 complete = m_current_ops == 0 || m_canceled || m_ret_val < 0;
301 m_update_sync_ctx = new FunctionContext([this](int r) {
302 this->send_update_sync_point();
308 Mutex::Locker timer_lock(*m_timer_lock);
309 if (m_update_sync_ctx) {
310 m_timer->add_event_after(m_update_sync_point_interval,
314 send_flush_sync_point();
318 template <typename I>
319 void ImageCopyRequest<I>::send_flush_sync_point() {
325 update_progress("FLUSH_SYNC_POINT");
327 m_client_meta_copy = *m_client_meta;
328 if (m_object_no > 0) {
329 m_sync_point->object_number = m_object_no - 1;
331 m_sync_point->object_number = boost::none;
334 dout(20) << ": sync_point=" << *m_sync_point << dendl;
336 bufferlist client_data_bl;
337 librbd::journal::ClientData client_data(m_client_meta_copy);
338 ::encode(client_data, client_data_bl);
340 Context *ctx = create_context_callback<
341 ImageCopyRequest<I>, &ImageCopyRequest<I>::handle_flush_sync_point>(
343 m_journaler->update_client(client_data_bl, ctx);
346 template <typename I>
347 void ImageCopyRequest<I>::handle_flush_sync_point(int r) {
348 dout(20) << ": r=" << r << dendl;
351 *m_client_meta = m_client_meta_copy;
353 derr << ": failed to update client data: " << cpp_strerror(r)
362 template <typename I>
363 int ImageCopyRequest<I>::compute_snap_map() {
365 librados::snap_t snap_id_start = 0;
366 librados::snap_t snap_id_end;
368 RWLock::RLocker snap_locker(m_remote_image_ctx->snap_lock);
369 snap_id_end = m_remote_image_ctx->get_snap_id(
370 cls::rbd::UserSnapshotNamespace(), m_sync_point->snap_name);
371 if (snap_id_end == CEPH_NOSNAP) {
372 derr << ": failed to locate snapshot: "
373 << m_sync_point->snap_name << dendl;
377 if (!m_sync_point->from_snap_name.empty()) {
378 snap_id_start = m_remote_image_ctx->get_snap_id(
379 cls::rbd::UserSnapshotNamespace(), m_sync_point->from_snap_name);
380 if (snap_id_start == CEPH_NOSNAP) {
381 derr << ": failed to locate from snapshot: "
382 << m_sync_point->from_snap_name << dendl;
389 for (auto it = m_client_meta->snap_seqs.begin();
390 it != m_client_meta->snap_seqs.end(); ++it) {
391 snap_ids.insert(snap_ids.begin(), it->second);
392 if (it->first < snap_id_start) {
394 } else if (it->first > snap_id_end) {
398 m_snap_map[it->first] = snap_ids;
401 if (m_snap_map.empty()) {
402 derr << ": failed to map snapshots within boundary" << dendl;
409 template <typename I>
410 void ImageCopyRequest<I>::update_progress(const std::string &description,
412 dout(20) << ": " << description << dendl;
414 if (m_progress_ctx) {
415 m_progress_ctx->update_progress("IMAGE_COPY/" + description, flush);
419 } // namespace image_sync
420 } // namespace mirror
423 template class rbd::mirror::image_sync::ImageCopyRequest<librbd::ImageCtx>;