]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd_mirror/pool_watcher/RefreshImagesRequest.cc
import ceph nautilus 14.2.2
[ceph.git] / 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 m_out_bl.clear();
39 librados::AioCompletion *aio_comp = create_rados_callback<
40 RefreshImagesRequest<I>,
41 &RefreshImagesRequest<I>::handle_mirror_image_list>(this);
42 int r = m_remote_io_ctx.aio_operate(RBD_MIRRORING, aio_comp, &op, &m_out_bl);
43 ceph_assert(r == 0);
44 aio_comp->release();
45 }
46
47 template <typename I>
48 void RefreshImagesRequest<I>::handle_mirror_image_list(int r) {
49 dout(10) << "r=" << r << dendl;
50
51 std::map<std::string, std::string> ids;
52 if (r == 0) {
53 auto it = m_out_bl.cbegin();
54 r = librbd::cls_client::mirror_image_list_finish(&it, &ids);
55 }
56
57 if (r < 0 && r != -ENOENT) {
58 derr << "failed to list mirrored images: " << cpp_strerror(r) << dendl;
59 finish(r);
60 return;
61 }
62
63 // store as global -> local image ids
64 for (auto &id : ids) {
65 m_image_ids->emplace(id.second, id.first);
66 }
67
68 if (ids.size() == MAX_RETURN) {
69 m_start_after = ids.rbegin()->first;
70 mirror_image_list();
71 return;
72 }
73
74 finish(0);
75 }
76
77 template <typename I>
78 void RefreshImagesRequest<I>::finish(int r) {
79 dout(10) << "r=" << r << dendl;
80
81 m_on_finish->complete(r);
82 delete this;
83 }
84
85 } // namespace pool_watcher
86 } // namespace mirror
87 } // namespace rbd
88
89 template class rbd::mirror::pool_watcher::RefreshImagesRequest<librbd::ImageCtx>;