Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd_mirror / pool_watcher / RefreshImagesRequest.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 "tools/rbd_mirror/pool_watcher/RefreshImagesRequest.h"
5 #include "common/debug.h"
6 #include "common/errno.h"
7 #include "cls/rbd/cls_rbd_client.h"
8 #include "librbd/Utils.h"
9 #include <map>
10
11 #define dout_context g_ceph_context
12 #define dout_subsys ceph_subsys_rbd_mirror
13 #undef dout_prefix
14 #define dout_prefix *_dout << "rbd::mirror::pool_watcher::RefreshImagesRequest " \
15                            << this << " " << __func__ << ": "
16
17 namespace rbd {
18 namespace mirror {
19 namespace pool_watcher {
20
21 static const uint32_t MAX_RETURN = 1024;
22
23 using librbd::util::create_rados_callback;
24
25 template <typename I>
26 void RefreshImagesRequest<I>::send() {
27   m_image_ids->clear();
28   mirror_image_list();
29 }
30
31 template <typename I>
32 void RefreshImagesRequest<I>::mirror_image_list() {
33   dout(10) << dendl;
34
35   librados::ObjectReadOperation op;
36   librbd::cls_client::mirror_image_list_start(&op, m_start_after, MAX_RETURN);
37
38   librados::AioCompletion *aio_comp = create_rados_callback<
39     RefreshImagesRequest<I>,
40     &RefreshImagesRequest<I>::handle_mirror_image_list>(this);
41   int r = m_remote_io_ctx.aio_operate(RBD_MIRRORING, aio_comp, &op, &m_out_bl);
42   assert(r == 0);
43   aio_comp->release();
44 }
45
46 template <typename I>
47 void RefreshImagesRequest<I>::handle_mirror_image_list(int r) {
48   dout(10) << "r=" << r << dendl;
49
50   std::map<std::string, std::string> ids;
51   if (r == 0) {
52     bufferlist::iterator it = m_out_bl.begin();
53     r = librbd::cls_client::mirror_image_list_finish(&it, &ids);
54   }
55
56   if (r < 0 && r != -ENOENT) {
57     derr << "failed to list mirrored images: " << cpp_strerror(r) << dendl;
58     finish(r);
59     return;
60   }
61
62   // store as global -> local image ids
63   for (auto &id : ids) {
64     m_image_ids->emplace(id.second, id.first);
65   }
66
67   if (ids.size() == MAX_RETURN) {
68     m_start_after = ids.rbegin()->first;
69     mirror_image_list();
70     return;
71   }
72
73   finish(0);
74 }
75
76 template <typename I>
77 void RefreshImagesRequest<I>::finish(int r) {
78   dout(10) << "r=" << r << dendl;
79
80   m_on_finish->complete(r);
81   delete this;
82 }
83
84 } // namespace pool_watcher
85 } // namespace mirror
86 } // namespace rbd
87
88 template class rbd::mirror::pool_watcher::RefreshImagesRequest<librbd::ImageCtx>;