]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd_mirror/image_sync/ImageCopyRequest.cc
dbc2560acd0254188d9809f459d4599d017ad0f6
[ceph.git] / ceph / src / tools / rbd_mirror / image_sync / ImageCopyRequest.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 "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"
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_sync::ImageCopyRequest: " \
17 << this << " " << __func__
18
19 namespace rbd {
20 namespace mirror {
21 namespace image_sync {
22
23 using librbd::util::create_context_callback;
24 using librbd::util::unique_lock_name;
25
26 template <typename I>
27 ImageCopyRequest<I>::ImageCopyRequest(I *local_image_ctx, I *remote_image_ctx,
28 SafeTimer *timer, Mutex *timer_lock,
29 Journaler *journaler,
30 MirrorPeerClientMeta *client_meta,
31 MirrorPeerSyncPoint *sync_point,
32 Context *on_finish,
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->rbd_mirror_sync_point_update_age),
43 m_client_meta_copy(*client_meta) {
44 assert(!m_client_meta_copy.sync_points.empty());
45 }
46
47 template <typename I>
48 void ImageCopyRequest<I>::send() {
49 int r = compute_snap_map();
50 if (r < 0) {
51 finish(r);
52 return;
53 }
54
55 send_update_max_object_count();
56 }
57
58 template <typename I>
59 void ImageCopyRequest<I>::cancel() {
60 Mutex::Locker locker(m_lock);
61
62 dout(20) << dendl;
63 m_canceled = true;
64 }
65
66 template <typename I>
67 void ImageCopyRequest<I>::send_update_max_object_count() {
68 uint64_t max_objects = m_client_meta->sync_object_count;
69 {
70 RWLock::RLocker snap_locker(m_remote_image_ctx->snap_lock);
71 max_objects = std::max(max_objects,
72 m_remote_image_ctx->get_object_count(CEPH_NOSNAP));
73 for (auto snap_id : m_remote_image_ctx->snaps) {
74 max_objects = std::max(max_objects,
75 m_remote_image_ctx->get_object_count(snap_id));
76 }
77 }
78
79 if (max_objects <= m_client_meta->sync_object_count) {
80 send_object_copies();
81 return;
82 }
83
84 update_progress("UPDATE_MAX_OBJECT_COUNT");
85
86 dout(20) << ": sync_object_count=" << max_objects << dendl;
87
88 m_client_meta_copy = *m_client_meta;
89 m_client_meta_copy.sync_object_count = max_objects;
90
91 bufferlist client_data_bl;
92 librbd::journal::ClientData client_data(m_client_meta_copy);
93 ::encode(client_data, client_data_bl);
94
95 Context *ctx = create_context_callback<
96 ImageCopyRequest<I>, &ImageCopyRequest<I>::handle_update_max_object_count>(
97 this);
98 m_journaler->update_client(client_data_bl, ctx);
99 }
100
101 template <typename I>
102 void ImageCopyRequest<I>::handle_update_max_object_count(int r) {
103 dout(20) << ": r=" << r << dendl;
104
105 if (r == 0) {
106 Mutex::Locker locker(m_lock);
107 if (m_canceled) {
108 dout(10) << ": image copy canceled" << dendl;
109 r = -ECANCELED;
110 }
111 }
112
113 if (r < 0) {
114 if (r != -ECANCELED) {
115 derr << ": failed to update client data: " << cpp_strerror(r) << dendl;
116 }
117 finish(r);
118 return;
119 }
120
121 // update provided meta structure to reflect reality
122 m_client_meta->sync_object_count = m_client_meta_copy.sync_object_count;
123
124 send_object_copies();
125 }
126
127 template <typename I>
128 void ImageCopyRequest<I>::send_object_copies() {
129 CephContext *cct = m_local_image_ctx->cct;
130
131 m_object_no = 0;
132 if (m_sync_point->object_number) {
133 m_object_no = *m_sync_point->object_number + 1;
134 }
135 m_end_object_no = m_client_meta->sync_object_count;
136
137 dout(20) << ": start_object=" << m_object_no << ", "
138 << "end_object=" << m_end_object_no << dendl;
139
140 update_progress("COPY_OBJECT");
141
142 bool complete;
143 {
144 Mutex::Locker locker(m_lock);
145 for (int i = 0; i < cct->_conf->rbd_concurrent_management_ops; ++i) {
146 send_next_object_copy();
147 if (m_ret_val < 0 && m_current_ops == 0) {
148 break;
149 }
150 }
151 complete = (m_current_ops == 0);
152
153 if (!complete) {
154 m_update_sync_ctx = new FunctionContext([this](int r) {
155 this->send_update_sync_point();
156 });
157 }
158 }
159
160 {
161 Mutex::Locker timer_locker(*m_timer_lock);
162 if (m_update_sync_ctx) {
163 m_timer->add_event_after(m_update_sync_point_interval,
164 m_update_sync_ctx);
165 }
166 }
167
168 if (complete) {
169 send_flush_sync_point();
170 }
171 }
172
173 template <typename I>
174 void ImageCopyRequest<I>::send_next_object_copy() {
175 assert(m_lock.is_locked());
176
177 if (m_canceled && m_ret_val == 0) {
178 dout(10) << ": image copy canceled" << dendl;
179 m_ret_val = -ECANCELED;
180 }
181
182 if (m_ret_val < 0 || m_object_no >= m_end_object_no) {
183 return;
184 }
185
186 uint64_t ono = m_object_no++;
187
188 dout(20) << ": object_num=" << ono << dendl;
189
190 ++m_current_ops;
191
192 Context *ctx = create_context_callback<
193 ImageCopyRequest<I>, &ImageCopyRequest<I>::handle_object_copy>(this);
194 ObjectCopyRequest<I> *req = ObjectCopyRequest<I>::create(
195 m_local_image_ctx, m_remote_image_ctx, &m_snap_map, ono, ctx);
196 req->send();
197 }
198
199 template <typename I>
200 void ImageCopyRequest<I>::handle_object_copy(int r) {
201 dout(20) << ": r=" << r << dendl;
202
203 int percent;
204 bool complete;
205 {
206 Mutex::Locker locker(m_lock);
207 assert(m_current_ops > 0);
208 --m_current_ops;
209
210 percent = 100 * m_object_no / m_end_object_no;
211
212 if (r < 0) {
213 derr << ": object copy failed: " << cpp_strerror(r) << dendl;
214 if (m_ret_val == 0) {
215 m_ret_val = r;
216 }
217 }
218
219 send_next_object_copy();
220 complete = (m_current_ops == 0);
221 }
222
223 update_progress("COPY_OBJECT " + stringify(percent) + "%", false);
224
225 if (complete) {
226 bool do_flush = true;
227 {
228 Mutex::Locker timer_locker(*m_timer_lock);
229 Mutex::Locker locker(m_lock);
230 if (!m_updating_sync_point) {
231 if (m_update_sync_ctx != nullptr) {
232 m_timer->cancel_event(m_update_sync_ctx);
233 m_update_sync_ctx = nullptr;
234 }
235 } else {
236 do_flush = false;
237 }
238 }
239
240 if (do_flush) {
241 send_flush_sync_point();
242 }
243 }
244 }
245
246 template <typename I>
247 void ImageCopyRequest<I>::send_update_sync_point() {
248 Mutex::Locker l(m_lock);
249
250 m_update_sync_ctx = nullptr;
251
252 if (m_canceled || m_ret_val < 0 || m_current_ops == 0) {
253 return;
254 }
255
256 if (m_sync_point->object_number &&
257 (m_object_no-1) == m_sync_point->object_number.get()) {
258 // update sync point did not progress since last sync
259 return;
260 }
261
262 m_updating_sync_point = true;
263
264 m_client_meta_copy = *m_client_meta;
265 m_sync_point->object_number = m_object_no - 1;
266
267 CephContext *cct = m_local_image_ctx->cct;
268 ldout(cct, 20) << ": sync_point=" << *m_sync_point << dendl;
269
270 bufferlist client_data_bl;
271 librbd::journal::ClientData client_data(*m_client_meta);
272 ::encode(client_data, client_data_bl);
273
274 Context *ctx = create_context_callback<
275 ImageCopyRequest<I>, &ImageCopyRequest<I>::handle_update_sync_point>(
276 this);
277 m_journaler->update_client(client_data_bl, ctx);
278 }
279
280 template <typename I>
281 void ImageCopyRequest<I>::handle_update_sync_point(int r) {
282 CephContext *cct = m_local_image_ctx->cct;
283 ldout(cct, 20) << ": r=" << r << dendl;
284
285 if (r < 0) {
286 *m_client_meta = m_client_meta_copy;
287 lderr(cct) << ": failed to update client data: " << cpp_strerror(r)
288 << dendl;
289 }
290
291 bool complete;
292 {
293 Mutex::Locker l(m_lock);
294 m_updating_sync_point = false;
295
296 complete = m_current_ops == 0 || m_canceled || m_ret_val < 0;
297
298 if (!complete) {
299 m_update_sync_ctx = new FunctionContext([this](int r) {
300 this->send_update_sync_point();
301 });
302 }
303 }
304
305 if (!complete) {
306 Mutex::Locker timer_lock(*m_timer_lock);
307 if (m_update_sync_ctx) {
308 m_timer->add_event_after(m_update_sync_point_interval,
309 m_update_sync_ctx);
310 }
311 } else {
312 send_flush_sync_point();
313 }
314 }
315
316 template <typename I>
317 void ImageCopyRequest<I>::send_flush_sync_point() {
318 if (m_ret_val < 0) {
319 finish(m_ret_val);
320 return;
321 }
322
323 update_progress("FLUSH_SYNC_POINT");
324
325 m_client_meta_copy = *m_client_meta;
326 if (m_object_no > 0) {
327 m_sync_point->object_number = m_object_no - 1;
328 } else {
329 m_sync_point->object_number = boost::none;
330 }
331
332 dout(20) << ": sync_point=" << *m_sync_point << dendl;
333
334 bufferlist client_data_bl;
335 librbd::journal::ClientData client_data(m_client_meta_copy);
336 ::encode(client_data, client_data_bl);
337
338 Context *ctx = create_context_callback<
339 ImageCopyRequest<I>, &ImageCopyRequest<I>::handle_flush_sync_point>(
340 this);
341 m_journaler->update_client(client_data_bl, ctx);
342 }
343
344 template <typename I>
345 void ImageCopyRequest<I>::handle_flush_sync_point(int r) {
346 dout(20) << ": r=" << r << dendl;
347
348 if (r < 0) {
349 *m_client_meta = m_client_meta_copy;
350
351 derr << ": failed to update client data: " << cpp_strerror(r)
352 << dendl;
353 finish(r);
354 return;
355 }
356
357 finish(0);
358 }
359
360 template <typename I>
361 int ImageCopyRequest<I>::compute_snap_map() {
362
363 librados::snap_t snap_id_start = 0;
364 librados::snap_t snap_id_end;
365 {
366 RWLock::RLocker snap_locker(m_remote_image_ctx->snap_lock);
367 snap_id_end = m_remote_image_ctx->get_snap_id(
368 cls::rbd::UserSnapshotNamespace(), m_sync_point->snap_name);
369 if (snap_id_end == CEPH_NOSNAP) {
370 derr << ": failed to locate snapshot: "
371 << m_sync_point->snap_name << dendl;
372 return -ENOENT;
373 }
374
375 if (!m_sync_point->from_snap_name.empty()) {
376 snap_id_start = m_remote_image_ctx->get_snap_id(
377 cls::rbd::UserSnapshotNamespace(), m_sync_point->from_snap_name);
378 if (snap_id_start == CEPH_NOSNAP) {
379 derr << ": failed to locate from snapshot: "
380 << m_sync_point->from_snap_name << dendl;
381 return -ENOENT;
382 }
383 }
384 }
385
386 SnapIds snap_ids;
387 for (auto it = m_client_meta->snap_seqs.begin();
388 it != m_client_meta->snap_seqs.end(); ++it) {
389 snap_ids.insert(snap_ids.begin(), it->second);
390 if (it->first < snap_id_start) {
391 continue;
392 } else if (it->first > snap_id_end) {
393 break;
394 }
395
396 m_snap_map[it->first] = snap_ids;
397 }
398
399 if (m_snap_map.empty()) {
400 derr << ": failed to map snapshots within boundary" << dendl;
401 return -EINVAL;
402 }
403
404 return 0;
405 }
406
407 template <typename I>
408 void ImageCopyRequest<I>::update_progress(const std::string &description,
409 bool flush) {
410 dout(20) << ": " << description << dendl;
411
412 if (m_progress_ctx) {
413 m_progress_ctx->update_progress("IMAGE_COPY/" + description, flush);
414 }
415 }
416
417 } // namespace image_sync
418 } // namespace mirror
419 } // namespace rbd
420
421 template class rbd::mirror::image_sync::ImageCopyRequest<librbd::ImageCtx>;