summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd_mirror/image_map/UpdateRequest.cc
blob: 799c5670f97d9353b5d959b032b4ee1fabfccab8 (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
// -*- 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"

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

namespace rbd {
namespace mirror {
namespace image_map {

using librbd::util::create_rados_callback;

static const uint32_t MAX_UPDATE = 256;

template <typename I>
UpdateRequest<I>::UpdateRequest(librados::IoCtx &ioctx,
                                std::map<std::string, cls::rbd::MirrorImageMap> &&update_mapping,
                                std::set<std::string> &&remove_global_image_ids, Context *on_finish)
  : m_ioctx(ioctx),
    m_update_mapping(update_mapping),
    m_remove_global_image_ids(remove_global_image_ids),
    m_on_finish(on_finish) {
}

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

  update_image_map();
}

template <typename I>
void UpdateRequest<I>::update_image_map() {
  dout(20) << dendl;

  if (m_update_mapping.empty() && m_remove_global_image_ids.empty()) {
    finish(0);
    return;
  }

  uint32_t nr_updates = 0;
  librados::ObjectWriteOperation op;

  auto it1 = m_update_mapping.begin();
  while (it1 != m_update_mapping.end() && nr_updates++ < MAX_UPDATE) {
    librbd::cls_client::mirror_image_map_update(&op, it1->first, it1->second);
    it1 = m_update_mapping.erase(it1);
  }

  auto it2 = m_remove_global_image_ids.begin();
  while (it2 != m_remove_global_image_ids.end() && nr_updates++ < MAX_UPDATE) {
    librbd::cls_client::mirror_image_map_remove(&op, *it2);
    it2 = m_remove_global_image_ids.erase(it2);
  }

  librados::AioCompletion *aio_comp = create_rados_callback<
    UpdateRequest, &UpdateRequest::handle_update_image_map>(this);
  int r = m_ioctx.aio_operate(RBD_MIRROR_LEADER, aio_comp, &op);
  ceph_assert(r == 0);
  aio_comp->release();
}

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

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

  update_image_map();
}

template <typename I>
void UpdateRequest<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::UpdateRequest<librbd::ImageCtx>;