Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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->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());
46 }
47
48 template <typename I>
49 void ImageCopyRequest<I>::send() {
50   int r = compute_snap_map();
51   if (r < 0) {
52     finish(r);
53     return;
54   }
55
56   send_update_max_object_count();
57 }
58
59 template <typename I>
60 void ImageCopyRequest<I>::cancel() {
61   Mutex::Locker locker(m_lock);
62
63   dout(20) << dendl;
64   m_canceled = true;
65 }
66
67 template <typename I>
68 void ImageCopyRequest<I>::send_update_max_object_count() {
69   uint64_t max_objects = m_client_meta->sync_object_count;
70   {
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));
77     }
78   }
79
80   if (max_objects <= m_client_meta->sync_object_count) {
81     send_object_copies();
82     return;
83   }
84
85   update_progress("UPDATE_MAX_OBJECT_COUNT");
86
87   dout(20) << ": sync_object_count=" << max_objects << dendl;
88
89   m_client_meta_copy = *m_client_meta;
90   m_client_meta_copy.sync_object_count = max_objects;
91
92   bufferlist client_data_bl;
93   librbd::journal::ClientData client_data(m_client_meta_copy);
94   ::encode(client_data, client_data_bl);
95
96   Context *ctx = create_context_callback<
97     ImageCopyRequest<I>, &ImageCopyRequest<I>::handle_update_max_object_count>(
98       this);
99   m_journaler->update_client(client_data_bl, ctx);
100 }
101
102 template <typename I>
103 void ImageCopyRequest<I>::handle_update_max_object_count(int r) {
104   dout(20) << ": r=" << r << dendl;
105
106   if (r == 0) {
107     Mutex::Locker locker(m_lock);
108     if (m_canceled) {
109       dout(10) << ": image copy canceled" << dendl;
110       r = -ECANCELED;
111     }
112   }
113
114   if (r < 0) {
115     if (r != -ECANCELED) {
116       derr << ": failed to update client data: " << cpp_strerror(r) << dendl;
117     }
118     finish(r);
119     return;
120   }
121
122   // update provided meta structure to reflect reality
123   m_client_meta->sync_object_count = m_client_meta_copy.sync_object_count;
124
125   send_object_copies();
126 }
127
128 template <typename I>
129 void ImageCopyRequest<I>::send_object_copies() {
130   CephContext *cct = m_local_image_ctx->cct;
131
132   m_object_no = 0;
133   if (m_sync_point->object_number) {
134     m_object_no = *m_sync_point->object_number + 1;
135   }
136   m_end_object_no = m_client_meta->sync_object_count;
137
138   dout(20) << ": start_object=" << m_object_no << ", "
139            << "end_object=" << m_end_object_no << dendl;
140
141   update_progress("COPY_OBJECT");
142
143   bool complete;
144   {
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) {
149         break;
150       }
151     }
152     complete = (m_current_ops == 0);
153
154     if (!complete) {
155       m_update_sync_ctx = new FunctionContext([this](int r) {
156           this->send_update_sync_point();
157       });
158     }
159   }
160
161   {
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,
166         m_update_sync_ctx);
167     }
168   }
169
170   if (complete) {
171     send_flush_sync_point();
172   }
173 }
174
175 template <typename I>
176 void ImageCopyRequest<I>::send_next_object_copy() {
177   assert(m_lock.is_locked());
178
179   if (m_canceled && m_ret_val == 0) {
180     dout(10) << ": image copy canceled" << dendl;
181     m_ret_val = -ECANCELED;
182   }
183
184   if (m_ret_val < 0 || m_object_no >= m_end_object_no) {
185     return;
186   }
187
188   uint64_t ono = m_object_no++;
189
190   dout(20) << ": object_num=" << ono << dendl;
191
192   ++m_current_ops;
193
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);
198   req->send();
199 }
200
201 template <typename I>
202 void ImageCopyRequest<I>::handle_object_copy(int r) {
203   dout(20) << ": r=" << r << dendl;
204
205   int percent;
206   bool complete;
207   {
208     Mutex::Locker locker(m_lock);
209     assert(m_current_ops > 0);
210     --m_current_ops;
211
212     percent = 100 * m_object_no / m_end_object_no;
213
214     if (r < 0) {
215       derr << ": object copy failed: " << cpp_strerror(r) << dendl;
216       if (m_ret_val == 0) {
217         m_ret_val = r;
218       }
219     }
220
221     send_next_object_copy();
222     complete = (m_current_ops == 0);
223   }
224
225   update_progress("COPY_OBJECT " + stringify(percent) + "%", false);
226
227   if (complete) {
228     bool do_flush = true;
229     {
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;
236         }
237       } else {
238         do_flush = false;
239       }
240     }
241
242     if (do_flush) {
243       send_flush_sync_point();
244     }
245   }
246 }
247
248 template <typename I>
249 void ImageCopyRequest<I>::send_update_sync_point() {
250   Mutex::Locker l(m_lock);
251
252   m_update_sync_ctx = nullptr;
253
254   if (m_canceled || m_ret_val < 0 || m_current_ops == 0) {
255     return;
256   }
257
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
261     return;
262   }
263
264   m_updating_sync_point = true;
265
266   m_client_meta_copy = *m_client_meta;
267   m_sync_point->object_number = m_object_no - 1;
268
269   CephContext *cct = m_local_image_ctx->cct;
270   ldout(cct, 20) << ": sync_point=" << *m_sync_point << dendl;
271
272   bufferlist client_data_bl;
273   librbd::journal::ClientData client_data(*m_client_meta);
274   ::encode(client_data, client_data_bl);
275
276   Context *ctx = create_context_callback<
277     ImageCopyRequest<I>, &ImageCopyRequest<I>::handle_update_sync_point>(
278       this);
279   m_journaler->update_client(client_data_bl, ctx);
280 }
281
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;
286
287   if (r < 0) {
288     *m_client_meta = m_client_meta_copy;
289     lderr(cct) << ": failed to update client data: " << cpp_strerror(r)
290                << dendl;
291   }
292
293   bool complete;
294   {
295     Mutex::Locker l(m_lock);
296     m_updating_sync_point = false;
297
298     complete = m_current_ops == 0 || m_canceled || m_ret_val < 0;
299
300     if (!complete) {
301       m_update_sync_ctx = new FunctionContext([this](int r) {
302           this->send_update_sync_point();
303       });
304     }
305   }
306
307   if (!complete) {
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,
311                                m_update_sync_ctx);
312     }
313   } else {
314     send_flush_sync_point();
315   }
316 }
317
318 template <typename I>
319 void ImageCopyRequest<I>::send_flush_sync_point() {
320   if (m_ret_val < 0) {
321     finish(m_ret_val);
322     return;
323   }
324
325   update_progress("FLUSH_SYNC_POINT");
326
327   m_client_meta_copy = *m_client_meta;
328   if (m_object_no > 0) {
329     m_sync_point->object_number = m_object_no - 1;
330   } else {
331     m_sync_point->object_number = boost::none;
332   }
333
334   dout(20) << ": sync_point=" << *m_sync_point << dendl;
335
336   bufferlist client_data_bl;
337   librbd::journal::ClientData client_data(m_client_meta_copy);
338   ::encode(client_data, client_data_bl);
339
340   Context *ctx = create_context_callback<
341     ImageCopyRequest<I>, &ImageCopyRequest<I>::handle_flush_sync_point>(
342       this);
343   m_journaler->update_client(client_data_bl, ctx);
344 }
345
346 template <typename I>
347 void ImageCopyRequest<I>::handle_flush_sync_point(int r) {
348   dout(20) << ": r=" << r << dendl;
349
350   if (r < 0) {
351     *m_client_meta = m_client_meta_copy;
352
353     derr << ": failed to update client data: " << cpp_strerror(r)
354          << dendl;
355     finish(r);
356     return;
357   }
358
359   finish(0);
360 }
361
362 template <typename I>
363 int ImageCopyRequest<I>::compute_snap_map() {
364
365   librados::snap_t snap_id_start = 0;
366   librados::snap_t snap_id_end;
367   {
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;
374       return -ENOENT;
375     }
376
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;
383         return -ENOENT;
384       }
385     }
386   }
387
388   SnapIds snap_ids;
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) {
393       continue;
394     } else if (it->first > snap_id_end) {
395       break;
396     }
397
398     m_snap_map[it->first] = snap_ids;
399   }
400
401   if (m_snap_map.empty()) {
402     derr << ": failed to map snapshots within boundary" << dendl;
403     return -EINVAL;
404   }
405
406   return 0;
407 }
408
409 template <typename I>
410 void ImageCopyRequest<I>::update_progress(const std::string &description,
411                                           bool flush) {
412   dout(20) << ": " << description << dendl;
413
414   if (m_progress_ctx) {
415     m_progress_ctx->update_progress("IMAGE_COPY/" + description, flush);
416   }
417 }
418
419 } // namespace image_sync
420 } // namespace mirror
421 } // namespace rbd
422
423 template class rbd::mirror::image_sync::ImageCopyRequest<librbd::ImageCtx>;