summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd_mirror/image_replayer/journal/EventPreprocessor.cc
blob: f5d49048e6d4e2ea2714901313ff4afe66fae90c (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "EventPreprocessor.h"
#include "common/debug.h"
#include "common/dout.h"
#include "common/errno.h"
#include "journal/Journaler.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "librbd/Utils.h"
#include "librbd/asio/ContextWQ.h"
#include "librbd/journal/Types.h"
#include <boost/variant.hpp>

#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_rbd_mirror

#undef dout_prefix
#define dout_prefix *_dout << "rbd::mirror::image_replayer::journal::" \
                           << "EventPreprocessor: " << this << " " << __func__ \
                           << ": "

namespace rbd {
namespace mirror {
namespace image_replayer {
namespace journal {

using librbd::util::create_context_callback;

template <typename I>
EventPreprocessor<I>::EventPreprocessor(I &local_image_ctx,
                                        Journaler &remote_journaler,
                                        const std::string &local_mirror_uuid,
                                        MirrorPeerClientMeta *client_meta,
                                        librbd::asio::ContextWQ *work_queue)
  : m_local_image_ctx(local_image_ctx), m_remote_journaler(remote_journaler),
    m_local_mirror_uuid(local_mirror_uuid), m_client_meta(client_meta),
    m_work_queue(work_queue) {
}

template <typename I>
EventPreprocessor<I>::~EventPreprocessor() {
  ceph_assert(!m_in_progress);
}

template <typename I>
bool EventPreprocessor<I>::is_required(const EventEntry &event_entry) {
  SnapSeqs snap_seqs(m_client_meta->snap_seqs);
  return (prune_snap_map(&snap_seqs) ||
          event_entry.get_event_type() ==
            librbd::journal::EVENT_TYPE_SNAP_RENAME);
}

template <typename I>
void EventPreprocessor<I>::preprocess(EventEntry *event_entry,
                                      Context *on_finish) {
  ceph_assert(!m_in_progress);
  m_in_progress = true;
  m_event_entry = event_entry;
  m_on_finish = on_finish;

  refresh_image();
}

template <typename I>
void EventPreprocessor<I>::refresh_image() {
  dout(20) << dendl;

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

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

  if (r < 0) {
    derr << "error encountered during image refresh: " << cpp_strerror(r)
         << dendl;
    finish(r);
    return;
  }

  preprocess_event();
}

template <typename I>
void EventPreprocessor<I>::preprocess_event() {
  dout(20) << dendl;

  m_snap_seqs = m_client_meta->snap_seqs;
  m_snap_seqs_updated = prune_snap_map(&m_snap_seqs);

  int r = boost::apply_visitor(PreprocessEventVisitor(this),
                               m_event_entry->event);
  if (r < 0) {
    finish(r);
    return;
  }

  update_client();
}

template <typename I>
int EventPreprocessor<I>::preprocess_snap_rename(
    librbd::journal::SnapRenameEvent &event) {
  dout(20) << "remote_snap_id=" << event.snap_id << ", "
           << "src_snap_name=" << event.src_snap_name << ", "
           << "dest_snap_name=" << event.dst_snap_name << dendl;

  auto snap_seq_it = m_snap_seqs.find(event.snap_id);
  if (snap_seq_it != m_snap_seqs.end()) {
    dout(20) << "remapping remote snap id " << snap_seq_it->first << " "
             << "to local snap id " << snap_seq_it->second << dendl;
    event.snap_id = snap_seq_it->second;
    return 0;
  }

  auto snap_id_it = m_local_image_ctx.snap_ids.find({cls::rbd::UserSnapshotNamespace(),
						     event.src_snap_name});
  if (snap_id_it == m_local_image_ctx.snap_ids.end()) {
    dout(20) << "cannot map remote snapshot '" << event.src_snap_name << "' "
             << "to local snapshot" << dendl;
    event.snap_id = CEPH_NOSNAP;
    return -ENOENT;
  }

  dout(20) << "mapping remote snap id " << event.snap_id << " "
           << "to local snap id " << snap_id_it->second << dendl;
  m_snap_seqs_updated = true;
  m_snap_seqs[event.snap_id] = snap_id_it->second;
  event.snap_id = snap_id_it->second;
  return 0;
}

template <typename I>
void EventPreprocessor<I>::update_client() {
  if (!m_snap_seqs_updated) {
    finish(0);
    return;
  }

  dout(20) << dendl;
  librbd::journal::MirrorPeerClientMeta client_meta(*m_client_meta);
  client_meta.snap_seqs = m_snap_seqs;

  librbd::journal::ClientData client_data(client_meta);
  bufferlist data_bl;
  encode(client_data, data_bl);

  Context *ctx = create_context_callback<
    EventPreprocessor<I>, &EventPreprocessor<I>::handle_update_client>(
      this);
  m_remote_journaler.update_client(data_bl, ctx);
}

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

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

  m_client_meta->snap_seqs = m_snap_seqs;
  finish(0);
}

template <typename I>
bool EventPreprocessor<I>::prune_snap_map(SnapSeqs *snap_seqs) {
  bool pruned = false;

  std::shared_lock image_locker{m_local_image_ctx.image_lock};
  for (auto it = snap_seqs->begin(); it != snap_seqs->end(); ) {
    auto current_it(it++);
    if (m_local_image_ctx.snap_info.count(current_it->second) == 0) {
      snap_seqs->erase(current_it);
      pruned = true;
    }
  }
  return pruned;
}

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

  Context *on_finish = m_on_finish;
  m_on_finish = nullptr;
  m_event_entry = nullptr;
  m_in_progress = false;
  m_snap_seqs_updated = false;
  m_work_queue->queue(on_finish, r);
}

} // namespace journal
} // namespace image_replayer
} // namespace mirror
} // namespace rbd

template class rbd::mirror::image_replayer::journal::EventPreprocessor<librbd::ImageCtx>;