summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd_mirror/image_sync/SyncPointCreateRequest.cc
blob: 1bd5d77f09418dd7d2781c829f94f1f2b64ab690 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "SyncPointCreateRequest.h"
#include "include/uuid.h"
#include "common/debug.h"
#include "common/errno.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "librbd/Operations.h"
#include "librbd/Utils.h"
#include "tools/rbd_mirror/image_sync/Types.h"
#include "tools/rbd_mirror/image_sync/Utils.h"

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

namespace rbd {
namespace mirror {
namespace image_sync {

using librbd::util::create_context_callback;

template <typename I>
SyncPointCreateRequest<I>::SyncPointCreateRequest(
    I *remote_image_ctx,
    const std::string &local_mirror_uuid,
    SyncPointHandler* sync_point_handler,
    Context *on_finish)
  : m_remote_image_ctx(remote_image_ctx),
    m_local_mirror_uuid(local_mirror_uuid),
    m_sync_point_handler(sync_point_handler),
    m_on_finish(on_finish) {
  m_sync_points_copy = m_sync_point_handler->get_sync_points();
  ceph_assert(m_sync_points_copy.size() < 2);

  // initialize the updated client meta with the new sync point
  m_sync_points_copy.emplace_back();
  if (m_sync_points_copy.size() > 1) {
    m_sync_points_copy.back().from_snap_name =
      m_sync_points_copy.front().snap_name;
  }
}

template <typename I>
void SyncPointCreateRequest<I>::send() {
  send_update_sync_points();
}

template <typename I>
void SyncPointCreateRequest<I>::send_update_sync_points() {
  uuid_d uuid_gen;
  uuid_gen.generate_random();

  auto& sync_point = m_sync_points_copy.back();
  sync_point.snap_name = util::get_snapshot_name_prefix(m_local_mirror_uuid) +
                         uuid_gen.to_string();

  auto ctx = create_context_callback<
    SyncPointCreateRequest<I>,
    &SyncPointCreateRequest<I>::handle_update_sync_points>(this);
  m_sync_point_handler->update_sync_points(
    m_sync_point_handler->get_snap_seqs(), m_sync_points_copy, false, ctx);
}

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

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

  send_refresh_image();
}

template <typename I>
void SyncPointCreateRequest<I>::send_refresh_image() {
  dout(20) << dendl;

  Context *ctx = create_context_callback<
    SyncPointCreateRequest<I>, &SyncPointCreateRequest<I>::handle_refresh_image>(
      this);
  m_remote_image_ctx->state->refresh(ctx);
}

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

  if (r < 0) {
    derr << ": remote image refresh failed: " << cpp_strerror(r) << dendl;
    finish(r);
    return;
  }

  send_create_snap();
}

template <typename I>
void SyncPointCreateRequest<I>::send_create_snap() {
  dout(20) << dendl;

  auto& sync_point = m_sync_points_copy.back();

  Context *ctx = create_context_callback<
    SyncPointCreateRequest<I>, &SyncPointCreateRequest<I>::handle_create_snap>(
      this);
  m_remote_image_ctx->operations->snap_create(
    cls::rbd::UserSnapshotNamespace(), sync_point.snap_name.c_str(),
    librbd::SNAP_CREATE_FLAG_SKIP_NOTIFY_QUIESCE, m_prog_ctx, ctx);
}

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

  if (r == -EEXIST) {
    send_update_sync_points();
    return;
  } else if (r < 0) {
    derr << ": failed to create snapshot: " << cpp_strerror(r) << dendl;
    finish(r);
    return;
  }

  send_final_refresh_image();
}

template <typename I>
void SyncPointCreateRequest<I>::send_final_refresh_image() {
  dout(20) << dendl;

  Context *ctx = create_context_callback<
    SyncPointCreateRequest<I>,
    &SyncPointCreateRequest<I>::handle_final_refresh_image>(this);
  m_remote_image_ctx->state->refresh(ctx);
}

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

  if (r < 0) {
    derr << ": failed to refresh image for snapshot: " << cpp_strerror(r)
         << dendl;
    finish(r);
    return;
  }

  finish(0);
}

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

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

} // namespace image_sync
} // namespace mirror
} // namespace rbd

template class rbd::mirror::image_sync::SyncPointCreateRequest<librbd::ImageCtx>;