summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd_mirror/image_map/LoadRequest.cc
blob: 46564a1607a2b1d944836bc2f421b85040f21a9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "common/debug.h"
#include "common/errno.h"

#include "librbd/Utils.h"
#include "include/rbd_types.h"
#include "cls/rbd/cls_rbd_client.h"

#include "UpdateRequest.h"
#include "LoadRequest.h"

#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_rbd_mirror
#undef dout_prefix
#define dout_prefix *_dout << "rbd::mirror::image_map::LoadRequest: "   \
                           << this << " " << __func__

namespace rbd {
namespace mirror {
namespace image_map {

static const uint32_t MAX_RETURN = 1024;

using librbd::util::create_rados_callback;
using librbd::util::create_context_callback;

template<typename I>
LoadRequest<I>::LoadRequest(librados::IoCtx &ioctx,
                            std::map<std::string, cls::rbd::MirrorImageMap> *image_mapping,
                            Context *on_finish)
  : m_ioctx(ioctx),
    m_image_mapping(image_mapping),
    m_on_finish(on_finish) {
}

template<typename I>
void LoadRequest<I>::send() {
  dout(20) << dendl;

  image_map_list();
}

template<typename I>
void LoadRequest<I>::image_map_list() {
  dout(20) << dendl;

  librados::ObjectReadOperation op;
  librbd::cls_client::mirror_image_map_list_start(&op, m_start_after, MAX_RETURN);

  librados::AioCompletion *aio_comp = create_rados_callback<
    LoadRequest, &LoadRequest::handle_image_map_list>(this);

  m_out_bl.clear();
  int r = m_ioctx.aio_operate(RBD_MIRROR_LEADER, aio_comp, &op, &m_out_bl);
  ceph_assert(r == 0);
  aio_comp->release();
}

template<typename I>
void LoadRequest<I>::handle_image_map_list(int r) {
  dout(20) << ": r=" << r << dendl;

  std::map<std::string, cls::rbd::MirrorImageMap> image_mapping;
  if (r == 0) {
    auto it = m_out_bl.cbegin();
    r = librbd::cls_client::mirror_image_map_list_finish(&it, &image_mapping);
  }

  if (r < 0) {
    derr << ": failed to get image map: " << cpp_strerror(r) << dendl;
    finish(r);
    return;
  }

  m_image_mapping->insert(image_mapping.begin(), image_mapping.end());

  if (image_mapping.size() == MAX_RETURN) {
    m_start_after = image_mapping.rbegin()->first;
    image_map_list();
    return;
  }

  mirror_image_list();
}

template<typename I>
void LoadRequest<I>::mirror_image_list() {
  dout(20) << dendl;

  librados::ObjectReadOperation op;
  librbd::cls_client::mirror_image_list_start(&op, m_start_after, MAX_RETURN);

  m_out_bl.clear();
  librados::AioCompletion *aio_comp = create_rados_callback<
    LoadRequest<I>,
    &LoadRequest<I>::handle_mirror_image_list>(this);
  int r = m_ioctx.aio_operate(RBD_MIRRORING, aio_comp, &op, &m_out_bl);
  ceph_assert(r == 0);
  aio_comp->release();
}

template<typename I>
void LoadRequest<I>::handle_mirror_image_list(int r) {
  dout(20) << ": r=" << r << dendl;

  std::map<std::string, std::string> ids;
  if (r == 0) {
    auto it = m_out_bl.cbegin();
    r = librbd::cls_client::mirror_image_list_finish(&it, &ids);
  }

  if (r < 0 && r != -ENOENT) {
    derr << "failed to list mirrored images: " << cpp_strerror(r) << dendl;
    finish(r);
    return;
  }

  for (auto &id : ids) {
    m_global_image_ids.emplace(id.second);
  }

  if (ids.size() == MAX_RETURN) {
    m_start_after = ids.rbegin()->first;
    mirror_image_list();
    return;
  }

  cleanup_image_map();
}

template<typename I>
void LoadRequest<I>::cleanup_image_map() {
  dout(20) << dendl;

  std::set<std::string> map_removals;

  auto it = m_image_mapping->begin();
  while (it != m_image_mapping->end()) {
    if (m_global_image_ids.count(it->first) > 0) {
      ++it;
      continue;
    }
    map_removals.emplace(it->first);
    it = m_image_mapping->erase(it);
  }

  if (map_removals.size() == 0) {
    finish(0);
    return;
  }

  auto ctx = create_context_callback<
     LoadRequest<I>,
     &LoadRequest<I>::finish>(this);
  image_map::UpdateRequest<I> *req = image_map::UpdateRequest<I>::create(
    m_ioctx, {}, std::move(map_removals), ctx);
  req->send();
}

template<typename I>
void LoadRequest<I>::finish(int r) {
  dout(20) << ": r=" << r << dendl;

  m_on_finish->complete(r);
  delete this;
}

} // namespace image_map
} // namespace mirror
} // namespace rbd

template class rbd::mirror::image_map::LoadRequest<librbd::ImageCtx>;