]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
4 | #include "include/compat.h" | |
5 | #include "BootstrapRequest.h" | |
6 | #include "CloseImageRequest.h" | |
7 | #include "CreateImageRequest.h" | |
8 | #include "IsPrimaryRequest.h" | |
9 | #include "OpenImageRequest.h" | |
10 | #include "OpenLocalImageRequest.h" | |
11 | #include "common/debug.h" | |
12 | #include "common/dout.h" | |
13 | #include "common/errno.h" | |
14 | #include "common/WorkQueue.h" | |
15 | #include "cls/rbd/cls_rbd_client.h" | |
16 | #include "journal/Journaler.h" | |
17 | #include "librbd/ImageCtx.h" | |
18 | #include "librbd/ImageState.h" | |
19 | #include "librbd/internal.h" | |
20 | #include "librbd/Journal.h" | |
21 | #include "librbd/Utils.h" | |
22 | #include "librbd/journal/Types.h" | |
23 | #include "tools/rbd_mirror/ProgressContext.h" | |
31f18b77 | 24 | #include "tools/rbd_mirror/ImageSync.h" |
7c673cae FG |
25 | |
26 | #define dout_context g_ceph_context | |
27 | #define dout_subsys ceph_subsys_rbd_mirror | |
28 | #undef dout_prefix | |
29 | #define dout_prefix *_dout << "rbd::mirror::image_replayer::BootstrapRequest: " \ | |
30 | << this << " " << __func__ | |
31 | ||
32 | namespace rbd { | |
33 | namespace mirror { | |
34 | namespace image_replayer { | |
35 | ||
36 | using librbd::util::create_context_callback; | |
37 | using librbd::util::create_rados_callback; | |
38 | using librbd::util::unique_lock_name; | |
39 | ||
40 | template <typename I> | |
41 | BootstrapRequest<I>::BootstrapRequest( | |
42 | librados::IoCtx &local_io_ctx, | |
43 | librados::IoCtx &remote_io_ctx, | |
31f18b77 | 44 | InstanceWatcher<I> *instance_watcher, |
7c673cae FG |
45 | I **local_image_ctx, |
46 | const std::string &local_image_id, | |
47 | const std::string &remote_image_id, | |
48 | const std::string &global_image_id, | |
49 | ContextWQ *work_queue, SafeTimer *timer, | |
50 | Mutex *timer_lock, | |
51 | const std::string &local_mirror_uuid, | |
52 | const std::string &remote_mirror_uuid, | |
53 | Journaler *journaler, | |
54 | MirrorPeerClientMeta *client_meta, | |
55 | Context *on_finish, | |
56 | bool *do_resync, | |
57 | rbd::mirror::ProgressContext *progress_ctx) | |
58 | : BaseRequest("rbd::mirror::image_replayer::BootstrapRequest", | |
59 | reinterpret_cast<CephContext*>(local_io_ctx.cct()), on_finish), | |
60 | m_local_io_ctx(local_io_ctx), m_remote_io_ctx(remote_io_ctx), | |
31f18b77 FG |
61 | m_instance_watcher(instance_watcher), m_local_image_ctx(local_image_ctx), |
62 | m_local_image_id(local_image_id), m_remote_image_id(remote_image_id), | |
63 | m_global_image_id(global_image_id), m_work_queue(work_queue), | |
64 | m_timer(timer), m_timer_lock(timer_lock), | |
7c673cae FG |
65 | m_local_mirror_uuid(local_mirror_uuid), |
66 | m_remote_mirror_uuid(remote_mirror_uuid), m_journaler(journaler), | |
67 | m_client_meta(client_meta), m_progress_ctx(progress_ctx), | |
68 | m_do_resync(do_resync), | |
69 | m_lock(unique_lock_name("BootstrapRequest::m_lock", this)) { | |
70 | } | |
71 | ||
72 | template <typename I> | |
73 | BootstrapRequest<I>::~BootstrapRequest() { | |
74 | assert(m_remote_image_ctx == nullptr); | |
75 | } | |
76 | ||
c07f9fc5 FG |
77 | template <typename I> |
78 | bool BootstrapRequest<I>::is_syncing() const { | |
79 | Mutex::Locker locker(m_lock); | |
80 | return (m_image_sync != nullptr); | |
81 | } | |
82 | ||
7c673cae FG |
83 | template <typename I> |
84 | void BootstrapRequest<I>::send() { | |
85 | *m_do_resync = false; | |
86 | ||
87 | get_remote_tag_class(); | |
88 | } | |
89 | ||
90 | template <typename I> | |
91 | void BootstrapRequest<I>::cancel() { | |
92 | dout(20) << dendl; | |
93 | ||
94 | Mutex::Locker locker(m_lock); | |
95 | m_canceled = true; | |
96 | ||
31f18b77 FG |
97 | if (m_image_sync != nullptr) { |
98 | m_image_sync->cancel(); | |
99 | } | |
7c673cae FG |
100 | } |
101 | ||
102 | template <typename I> | |
103 | void BootstrapRequest<I>::get_remote_tag_class() { | |
104 | dout(20) << dendl; | |
105 | ||
106 | update_progress("GET_REMOTE_TAG_CLASS"); | |
107 | ||
108 | Context *ctx = create_context_callback< | |
109 | BootstrapRequest<I>, &BootstrapRequest<I>::handle_get_remote_tag_class>( | |
110 | this); | |
111 | m_journaler->get_client(librbd::Journal<>::IMAGE_CLIENT_ID, &m_client, ctx); | |
112 | } | |
113 | ||
114 | template <typename I> | |
115 | void BootstrapRequest<I>::handle_get_remote_tag_class(int r) { | |
116 | dout(20) << ": r=" << r << dendl; | |
117 | ||
118 | if (r < 0) { | |
119 | derr << ": failed to retrieve remote client: " << cpp_strerror(r) << dendl; | |
120 | finish(r); | |
121 | return; | |
122 | } | |
123 | ||
124 | librbd::journal::ClientData client_data; | |
125 | bufferlist::iterator it = m_client.data.begin(); | |
126 | try { | |
127 | ::decode(client_data, it); | |
128 | } catch (const buffer::error &err) { | |
129 | derr << ": failed to decode remote client meta data: " << err.what() | |
130 | << dendl; | |
131 | finish(-EBADMSG); | |
132 | return; | |
133 | } | |
134 | ||
135 | librbd::journal::ImageClientMeta *client_meta = | |
136 | boost::get<librbd::journal::ImageClientMeta>(&client_data.client_meta); | |
137 | if (client_meta == nullptr) { | |
138 | derr << ": unknown remote client registration" << dendl; | |
139 | finish(-EINVAL); | |
140 | return; | |
141 | } | |
142 | ||
143 | m_remote_tag_class = client_meta->tag_class; | |
144 | dout(10) << ": remote tag class=" << m_remote_tag_class << dendl; | |
145 | ||
d2e6a577 FG |
146 | open_remote_image(); |
147 | } | |
148 | ||
149 | template <typename I> | |
150 | void BootstrapRequest<I>::open_remote_image() { | |
151 | dout(20) << dendl; | |
152 | ||
153 | update_progress("OPEN_REMOTE_IMAGE"); | |
154 | ||
155 | Context *ctx = create_context_callback< | |
156 | BootstrapRequest<I>, &BootstrapRequest<I>::handle_open_remote_image>( | |
157 | this); | |
158 | OpenImageRequest<I> *request = OpenImageRequest<I>::create( | |
159 | m_remote_io_ctx, &m_remote_image_ctx, m_remote_image_id, false, | |
160 | ctx); | |
161 | request->send(); | |
162 | } | |
163 | ||
164 | template <typename I> | |
165 | void BootstrapRequest<I>::handle_open_remote_image(int r) { | |
166 | dout(20) << ": r=" << r << dendl; | |
167 | ||
168 | if (r < 0) { | |
169 | derr << ": failed to open remote image: " << cpp_strerror(r) << dendl; | |
170 | assert(m_remote_image_ctx == nullptr); | |
171 | finish(r); | |
172 | return; | |
173 | } | |
174 | ||
7c673cae FG |
175 | get_client(); |
176 | } | |
177 | ||
178 | template <typename I> | |
179 | void BootstrapRequest<I>::get_client() { | |
180 | dout(20) << dendl; | |
181 | ||
182 | update_progress("GET_CLIENT"); | |
183 | ||
184 | Context *ctx = create_context_callback< | |
185 | BootstrapRequest<I>, &BootstrapRequest<I>::handle_get_client>( | |
186 | this); | |
187 | m_journaler->get_client(m_local_mirror_uuid, &m_client, ctx); | |
188 | } | |
189 | ||
190 | template <typename I> | |
191 | void BootstrapRequest<I>::handle_get_client(int r) { | |
192 | dout(20) << ": r=" << r << dendl; | |
193 | ||
194 | if (r == -ENOENT) { | |
195 | dout(10) << ": client not registered" << dendl; | |
196 | } else if (r < 0) { | |
197 | derr << ": failed to retrieve client: " << cpp_strerror(r) << dendl; | |
d2e6a577 FG |
198 | m_ret_val = r; |
199 | close_remote_image(); | |
7c673cae FG |
200 | return; |
201 | } else if (decode_client_meta()) { | |
202 | // skip registration if it already exists | |
d2e6a577 | 203 | is_primary(); |
7c673cae FG |
204 | return; |
205 | } | |
206 | ||
207 | register_client(); | |
208 | } | |
209 | ||
210 | template <typename I> | |
211 | void BootstrapRequest<I>::register_client() { | |
212 | dout(20) << dendl; | |
213 | ||
214 | update_progress("REGISTER_CLIENT"); | |
215 | ||
3efd9988 FG |
216 | librbd::journal::MirrorPeerClientMeta mirror_peer_client_meta{ |
217 | m_local_image_id}; | |
218 | mirror_peer_client_meta.state = librbd::journal::MIRROR_PEER_STATE_REPLAYING; | |
219 | ||
220 | librbd::journal::ClientData client_data{mirror_peer_client_meta}; | |
7c673cae FG |
221 | bufferlist client_data_bl; |
222 | ::encode(client_data, client_data_bl); | |
223 | ||
224 | Context *ctx = create_context_callback< | |
225 | BootstrapRequest<I>, &BootstrapRequest<I>::handle_register_client>( | |
226 | this); | |
227 | m_journaler->register_client(client_data_bl, ctx); | |
228 | } | |
229 | ||
230 | template <typename I> | |
231 | void BootstrapRequest<I>::handle_register_client(int r) { | |
232 | dout(20) << ": r=" << r << dendl; | |
233 | ||
234 | if (r < 0) { | |
235 | derr << ": failed to register with remote journal: " << cpp_strerror(r) | |
236 | << dendl; | |
d2e6a577 FG |
237 | m_ret_val = r; |
238 | close_remote_image(); | |
7c673cae FG |
239 | return; |
240 | } | |
241 | ||
d2e6a577 | 242 | m_client = {}; |
7c673cae | 243 | *m_client_meta = librbd::journal::MirrorPeerClientMeta(m_local_image_id); |
3efd9988 FG |
244 | m_client_meta->state = librbd::journal::MIRROR_PEER_STATE_REPLAYING; |
245 | ||
7c673cae FG |
246 | is_primary(); |
247 | } | |
248 | ||
249 | template <typename I> | |
250 | void BootstrapRequest<I>::is_primary() { | |
251 | dout(20) << dendl; | |
252 | ||
253 | update_progress("OPEN_REMOTE_IMAGE"); | |
254 | ||
255 | Context *ctx = create_context_callback< | |
256 | BootstrapRequest<I>, &BootstrapRequest<I>::handle_is_primary>( | |
257 | this); | |
258 | IsPrimaryRequest<I> *request = IsPrimaryRequest<I>::create(m_remote_image_ctx, | |
259 | &m_primary, ctx); | |
260 | request->send(); | |
261 | } | |
262 | ||
263 | template <typename I> | |
264 | void BootstrapRequest<I>::handle_is_primary(int r) { | |
265 | dout(20) << ": r=" << r << dendl; | |
266 | ||
267 | if (r < 0) { | |
268 | derr << ": error querying remote image primary status: " << cpp_strerror(r) | |
269 | << dendl; | |
270 | m_ret_val = r; | |
271 | close_remote_image(); | |
272 | return; | |
273 | } | |
274 | ||
275 | if (!m_primary) { | |
276 | dout(5) << ": remote image is not primary -- skipping image replay" | |
277 | << dendl; | |
278 | m_ret_val = -EREMOTEIO; | |
279 | update_client_state(); | |
280 | return; | |
281 | } | |
282 | ||
283 | if (m_local_image_id.empty()) { | |
d2e6a577 | 284 | update_client_image(); |
7c673cae FG |
285 | return; |
286 | } | |
287 | ||
288 | open_local_image(); | |
289 | } | |
290 | ||
291 | template <typename I> | |
292 | void BootstrapRequest<I>::update_client_state() { | |
293 | if (m_client_meta->state == librbd::journal::MIRROR_PEER_STATE_REPLAYING) { | |
294 | // state already set for replaying upon failover | |
295 | close_remote_image(); | |
296 | return; | |
297 | } | |
298 | ||
299 | dout(20) << dendl; | |
300 | update_progress("UPDATE_CLIENT_STATE"); | |
301 | ||
302 | librbd::journal::MirrorPeerClientMeta client_meta(*m_client_meta); | |
303 | client_meta.state = librbd::journal::MIRROR_PEER_STATE_REPLAYING; | |
304 | ||
305 | librbd::journal::ClientData client_data(client_meta); | |
306 | bufferlist data_bl; | |
307 | ::encode(client_data, data_bl); | |
308 | ||
309 | Context *ctx = create_context_callback< | |
310 | BootstrapRequest<I>, &BootstrapRequest<I>::handle_update_client_state>( | |
311 | this); | |
312 | m_journaler->update_client(data_bl, ctx); | |
313 | } | |
314 | ||
315 | template <typename I> | |
316 | void BootstrapRequest<I>::handle_update_client_state(int r) { | |
317 | dout(20) << ": r=" << r << dendl; | |
318 | if (r < 0) { | |
319 | derr << ": failed to update client: " << cpp_strerror(r) << dendl; | |
320 | } else { | |
d2e6a577 | 321 | m_client_meta->state = librbd::journal::MIRROR_PEER_STATE_REPLAYING; |
7c673cae FG |
322 | } |
323 | ||
324 | close_remote_image(); | |
325 | } | |
326 | ||
327 | template <typename I> | |
328 | void BootstrapRequest<I>::open_local_image() { | |
329 | dout(20) << dendl; | |
330 | ||
331 | update_progress("OPEN_LOCAL_IMAGE"); | |
332 | ||
333 | Context *ctx = create_context_callback< | |
334 | BootstrapRequest<I>, &BootstrapRequest<I>::handle_open_local_image>( | |
335 | this); | |
336 | OpenLocalImageRequest<I> *request = OpenLocalImageRequest<I>::create( | |
337 | m_local_io_ctx, m_local_image_ctx, m_local_image_id, m_work_queue, | |
338 | ctx); | |
339 | request->send(); | |
340 | } | |
341 | ||
342 | template <typename I> | |
343 | void BootstrapRequest<I>::handle_open_local_image(int r) { | |
344 | dout(20) << ": r=" << r << dendl; | |
345 | ||
346 | if (r == -ENOENT) { | |
347 | assert(*m_local_image_ctx == nullptr); | |
348 | dout(10) << ": local image missing" << dendl; | |
d2e6a577 | 349 | unregister_client(); |
7c673cae FG |
350 | return; |
351 | } else if (r == -EREMOTEIO) { | |
352 | assert(*m_local_image_ctx == nullptr); | |
353 | dout(10) << "local image is primary -- skipping image replay" << dendl; | |
354 | m_ret_val = r; | |
355 | close_remote_image(); | |
356 | return; | |
357 | } else if (r < 0) { | |
358 | assert(*m_local_image_ctx == nullptr); | |
359 | derr << ": failed to open local image: " << cpp_strerror(r) << dendl; | |
360 | m_ret_val = r; | |
361 | close_remote_image(); | |
362 | return; | |
363 | } | |
364 | ||
365 | I *local_image_ctx = (*m_local_image_ctx); | |
366 | { | |
367 | RWLock::RLocker snap_locker(local_image_ctx->snap_lock); | |
368 | if (local_image_ctx->journal == nullptr) { | |
369 | derr << ": local image does not support journaling" << dendl; | |
370 | m_ret_val = -EINVAL; | |
371 | close_local_image(); | |
372 | return; | |
373 | } | |
374 | ||
375 | r = (*m_local_image_ctx)->journal->is_resync_requested(m_do_resync); | |
376 | if (r < 0) { | |
377 | derr << ": failed to check if a resync was requested" << dendl; | |
378 | m_ret_val = r; | |
379 | close_local_image(); | |
380 | return; | |
381 | } | |
382 | } | |
383 | ||
384 | if (*m_do_resync) { | |
385 | close_remote_image(); | |
386 | return; | |
387 | } | |
388 | ||
389 | if (m_client.state == cls::journal::CLIENT_STATE_DISCONNECTED) { | |
390 | dout(10) << ": client flagged disconnected -- skipping bootstrap" << dendl; | |
391 | // The caller is expected to detect disconnect initializing remote journal. | |
392 | m_ret_val = 0; | |
393 | close_remote_image(); | |
394 | return; | |
395 | } | |
396 | ||
d2e6a577 | 397 | get_remote_tags(); |
7c673cae FG |
398 | } |
399 | ||
400 | template <typename I> | |
d2e6a577 | 401 | void BootstrapRequest<I>::unregister_client() { |
7c673cae | 402 | dout(20) << dendl; |
d2e6a577 | 403 | update_progress("UNREGISTER_CLIENT"); |
7c673cae FG |
404 | |
405 | m_local_image_id = ""; | |
7c673cae | 406 | Context *ctx = create_context_callback< |
d2e6a577 | 407 | BootstrapRequest<I>, &BootstrapRequest<I>::handle_unregister_client>( |
7c673cae | 408 | this); |
d2e6a577 | 409 | m_journaler->unregister_client(ctx); |
7c673cae FG |
410 | } |
411 | ||
412 | template <typename I> | |
d2e6a577 | 413 | void BootstrapRequest<I>::handle_unregister_client(int r) { |
7c673cae | 414 | dout(20) << ": r=" << r << dendl; |
7c673cae | 415 | if (r < 0) { |
d2e6a577 FG |
416 | derr << ": failed to unregister with remote journal: " << cpp_strerror(r) |
417 | << dendl; | |
7c673cae FG |
418 | m_ret_val = r; |
419 | close_remote_image(); | |
420 | return; | |
421 | } | |
422 | ||
d2e6a577 FG |
423 | *m_client_meta = librbd::journal::MirrorPeerClientMeta(""); |
424 | register_client(); | |
7c673cae FG |
425 | } |
426 | ||
427 | template <typename I> | |
428 | void BootstrapRequest<I>::update_client_image() { | |
429 | dout(20) << dendl; | |
7c673cae FG |
430 | update_progress("UPDATE_CLIENT_IMAGE"); |
431 | ||
d2e6a577 FG |
432 | assert(m_local_image_id.empty()); |
433 | m_local_image_id = librbd::util::generate_image_id<I>(m_local_io_ctx); | |
7c673cae FG |
434 | |
435 | librbd::journal::MirrorPeerClientMeta client_meta{m_local_image_id}; | |
436 | client_meta.state = librbd::journal::MIRROR_PEER_STATE_SYNCING; | |
437 | ||
438 | librbd::journal::ClientData client_data(client_meta); | |
439 | bufferlist data_bl; | |
440 | ::encode(client_data, data_bl); | |
441 | ||
442 | Context *ctx = create_context_callback< | |
443 | BootstrapRequest<I>, &BootstrapRequest<I>::handle_update_client_image>( | |
444 | this); | |
445 | m_journaler->update_client(data_bl, ctx); | |
446 | } | |
447 | ||
448 | template <typename I> | |
449 | void BootstrapRequest<I>::handle_update_client_image(int r) { | |
450 | dout(20) << ": r=" << r << dendl; | |
451 | ||
452 | if (r < 0) { | |
453 | derr << ": failed to update client: " << cpp_strerror(r) << dendl; | |
454 | m_ret_val = r; | |
d2e6a577 | 455 | close_remote_image(); |
7c673cae FG |
456 | return; |
457 | } | |
458 | ||
459 | if (m_canceled) { | |
460 | dout(10) << ": request canceled" << dendl; | |
461 | m_ret_val = -ECANCELED; | |
d2e6a577 | 462 | close_remote_image(); |
7c673cae FG |
463 | return; |
464 | } | |
465 | ||
466 | *m_client_meta = {m_local_image_id}; | |
467 | m_client_meta->state = librbd::journal::MIRROR_PEER_STATE_SYNCING; | |
d2e6a577 FG |
468 | create_local_image(); |
469 | } | |
470 | ||
471 | template <typename I> | |
472 | void BootstrapRequest<I>::create_local_image() { | |
473 | dout(20) << dendl; | |
474 | update_progress("CREATE_LOCAL_IMAGE"); | |
475 | ||
476 | m_remote_image_ctx->snap_lock.get_read(); | |
477 | std::string image_name = m_remote_image_ctx->name; | |
478 | m_remote_image_ctx->snap_lock.put_read(); | |
479 | ||
480 | Context *ctx = create_context_callback< | |
481 | BootstrapRequest<I>, &BootstrapRequest<I>::handle_create_local_image>( | |
482 | this); | |
483 | CreateImageRequest<I> *request = CreateImageRequest<I>::create( | |
484 | m_local_io_ctx, m_work_queue, m_global_image_id, m_remote_mirror_uuid, | |
485 | image_name, m_local_image_id, m_remote_image_ctx, ctx); | |
486 | request->send(); | |
487 | } | |
488 | ||
489 | template <typename I> | |
490 | void BootstrapRequest<I>::handle_create_local_image(int r) { | |
491 | dout(20) << ": r=" << r << dendl; | |
492 | ||
493 | if (r < 0) { | |
494 | derr << ": failed to create local image: " << cpp_strerror(r) << dendl; | |
495 | m_ret_val = r; | |
496 | close_remote_image(); | |
497 | return; | |
498 | } | |
499 | ||
500 | open_local_image(); | |
7c673cae FG |
501 | } |
502 | ||
503 | template <typename I> | |
504 | void BootstrapRequest<I>::get_remote_tags() { | |
7c673cae FG |
505 | if (m_client_meta->state == librbd::journal::MIRROR_PEER_STATE_SYNCING) { |
506 | // optimization -- no need to compare remote tags if we just created | |
507 | // the image locally or sync was interrupted | |
508 | image_sync(); | |
509 | return; | |
510 | } | |
511 | ||
512 | dout(20) << dendl; | |
3efd9988 | 513 | update_progress("GET_REMOTE_TAGS"); |
7c673cae FG |
514 | |
515 | Context *ctx = create_context_callback< | |
516 | BootstrapRequest<I>, &BootstrapRequest<I>::handle_get_remote_tags>(this); | |
517 | m_journaler->get_tags(m_remote_tag_class, &m_remote_tags, ctx); | |
518 | } | |
519 | ||
520 | template <typename I> | |
521 | void BootstrapRequest<I>::handle_get_remote_tags(int r) { | |
522 | dout(20) << ": r=" << r << dendl; | |
523 | ||
524 | if (r < 0) { | |
525 | derr << ": failed to retrieve remote tags: " << cpp_strerror(r) << dendl; | |
526 | m_ret_val = r; | |
527 | close_local_image(); | |
528 | return; | |
529 | } | |
530 | ||
531 | if (m_canceled) { | |
532 | dout(10) << ": request canceled" << dendl; | |
533 | m_ret_val = -ECANCELED; | |
534 | close_local_image(); | |
535 | return; | |
536 | } | |
537 | ||
538 | // At this point, the local image was existing, non-primary, and replaying; | |
539 | // and the remote image is primary. Attempt to link the local image's most | |
540 | // recent tag to the remote image's tag chain. | |
541 | uint64_t local_tag_tid; | |
542 | librbd::journal::TagData local_tag_data; | |
543 | I *local_image_ctx = (*m_local_image_ctx); | |
544 | { | |
545 | RWLock::RLocker snap_locker(local_image_ctx->snap_lock); | |
546 | if (local_image_ctx->journal == nullptr) { | |
547 | derr << ": local image does not support journaling" << dendl; | |
548 | m_ret_val = -EINVAL; | |
549 | close_local_image(); | |
550 | return; | |
551 | } | |
552 | ||
553 | local_tag_tid = local_image_ctx->journal->get_tag_tid(); | |
554 | local_tag_data = local_image_ctx->journal->get_tag_data(); | |
555 | dout(20) << ": local tag " << local_tag_tid << ": " | |
556 | << local_tag_data << dendl; | |
557 | } | |
558 | ||
559 | bool remote_tag_data_valid = false; | |
560 | librbd::journal::TagData remote_tag_data; | |
561 | boost::optional<uint64_t> remote_orphan_tag_tid = | |
562 | boost::make_optional<uint64_t>(false, 0U); | |
563 | bool reconnect_orphan = false; | |
564 | ||
565 | // decode the remote tags | |
566 | for (auto &remote_tag : m_remote_tags) { | |
567 | if (local_tag_data.predecessor.commit_valid && | |
568 | local_tag_data.predecessor.mirror_uuid == m_remote_mirror_uuid && | |
569 | local_tag_data.predecessor.tag_tid > remote_tag.tid) { | |
570 | dout(20) << ": skipping processed predecessor remote tag " | |
571 | << remote_tag.tid << dendl; | |
572 | continue; | |
573 | } | |
574 | ||
575 | try { | |
576 | bufferlist::iterator it = remote_tag.data.begin(); | |
577 | ::decode(remote_tag_data, it); | |
578 | remote_tag_data_valid = true; | |
579 | } catch (const buffer::error &err) { | |
580 | derr << ": failed to decode remote tag " << remote_tag.tid << ": " | |
581 | << err.what() << dendl; | |
582 | m_ret_val = -EBADMSG; | |
583 | close_local_image(); | |
584 | return; | |
585 | } | |
586 | ||
587 | dout(10) << ": decoded remote tag " << remote_tag.tid << ": " | |
588 | << remote_tag_data << dendl; | |
589 | ||
590 | if (!local_tag_data.predecessor.commit_valid) { | |
591 | // newly synced local image (no predecessor) replays from the first tag | |
592 | if (remote_tag_data.mirror_uuid != librbd::Journal<>::LOCAL_MIRROR_UUID) { | |
593 | dout(20) << ": skipping non-primary remote tag" << dendl; | |
594 | continue; | |
595 | } | |
596 | ||
597 | dout(20) << ": using initial primary remote tag" << dendl; | |
598 | break; | |
599 | } | |
600 | ||
601 | if (local_tag_data.mirror_uuid == librbd::Journal<>::ORPHAN_MIRROR_UUID) { | |
602 | // demotion last available local epoch | |
603 | ||
604 | if (remote_tag_data.mirror_uuid == local_tag_data.mirror_uuid && | |
605 | remote_tag_data.predecessor.commit_valid && | |
606 | remote_tag_data.predecessor.tag_tid == | |
607 | local_tag_data.predecessor.tag_tid) { | |
608 | // demotion matches remote epoch | |
609 | ||
610 | if (remote_tag_data.predecessor.mirror_uuid == m_local_mirror_uuid && | |
611 | local_tag_data.predecessor.mirror_uuid == | |
612 | librbd::Journal<>::LOCAL_MIRROR_UUID) { | |
613 | // local demoted and remote has matching event | |
614 | dout(20) << ": found matching local demotion tag" << dendl; | |
615 | remote_orphan_tag_tid = remote_tag.tid; | |
616 | continue; | |
617 | } | |
618 | ||
619 | if (local_tag_data.predecessor.mirror_uuid == m_remote_mirror_uuid && | |
620 | remote_tag_data.predecessor.mirror_uuid == | |
621 | librbd::Journal<>::LOCAL_MIRROR_UUID) { | |
622 | // remote demoted and local has matching event | |
623 | dout(20) << ": found matching remote demotion tag" << dendl; | |
624 | remote_orphan_tag_tid = remote_tag.tid; | |
625 | continue; | |
626 | } | |
627 | } | |
628 | ||
629 | if (remote_tag_data.mirror_uuid == librbd::Journal<>::LOCAL_MIRROR_UUID && | |
630 | remote_tag_data.predecessor.mirror_uuid == librbd::Journal<>::ORPHAN_MIRROR_UUID && | |
631 | remote_tag_data.predecessor.commit_valid && remote_orphan_tag_tid && | |
632 | remote_tag_data.predecessor.tag_tid == *remote_orphan_tag_tid) { | |
633 | // remote promotion tag chained to remote/local demotion tag | |
634 | dout(20) << ": found chained remote promotion tag" << dendl; | |
635 | reconnect_orphan = true; | |
636 | break; | |
637 | } | |
638 | ||
639 | // promotion must follow demotion | |
640 | remote_orphan_tag_tid = boost::none; | |
641 | } | |
642 | } | |
643 | ||
644 | if (remote_tag_data_valid && | |
645 | local_tag_data.mirror_uuid == m_remote_mirror_uuid) { | |
646 | dout(20) << ": local image is in clean replay state" << dendl; | |
647 | } else if (reconnect_orphan) { | |
648 | dout(20) << ": remote image was demoted/promoted" << dendl; | |
649 | } else { | |
650 | derr << ": split-brain detected -- skipping image replay" << dendl; | |
651 | m_ret_val = -EEXIST; | |
652 | close_local_image(); | |
653 | return; | |
654 | } | |
655 | ||
656 | image_sync(); | |
657 | } | |
658 | ||
659 | template <typename I> | |
660 | void BootstrapRequest<I>::image_sync() { | |
661 | if (m_client_meta->state == librbd::journal::MIRROR_PEER_STATE_REPLAYING) { | |
662 | // clean replay state -- no image sync required | |
663 | close_remote_image(); | |
664 | return; | |
665 | } | |
666 | ||
667 | dout(20) << dendl; | |
7c673cae FG |
668 | { |
669 | Mutex::Locker locker(m_lock); | |
31f18b77 FG |
670 | if (m_canceled) { |
671 | m_ret_val = -ECANCELED; | |
672 | } else { | |
673 | assert(m_image_sync == nullptr); | |
c07f9fc5 FG |
674 | |
675 | Context *ctx = create_context_callback< | |
676 | BootstrapRequest<I>, &BootstrapRequest<I>::handle_image_sync>(this); | |
31f18b77 FG |
677 | m_image_sync = ImageSync<I>::create( |
678 | *m_local_image_ctx, m_remote_image_ctx, m_timer, m_timer_lock, | |
679 | m_local_mirror_uuid, m_journaler, m_client_meta, m_work_queue, | |
680 | m_instance_watcher, ctx, m_progress_ctx); | |
681 | ||
682 | m_image_sync->get(); | |
c07f9fc5 FG |
683 | |
684 | m_lock.Unlock(); | |
685 | update_progress("IMAGE_SYNC"); | |
686 | m_lock.Lock(); | |
687 | ||
31f18b77 | 688 | m_image_sync->send(); |
7c673cae FG |
689 | return; |
690 | } | |
691 | } | |
692 | ||
693 | dout(10) << ": request canceled" << dendl; | |
7c673cae FG |
694 | close_remote_image(); |
695 | } | |
696 | ||
697 | template <typename I> | |
698 | void BootstrapRequest<I>::handle_image_sync(int r) { | |
699 | dout(20) << ": r=" << r << dendl; | |
700 | ||
31f18b77 FG |
701 | { |
702 | Mutex::Locker locker(m_lock); | |
31f18b77 FG |
703 | m_image_sync->put(); |
704 | m_image_sync = nullptr; | |
705 | ||
706 | if (m_canceled) { | |
707 | dout(10) << ": request canceled" << dendl; | |
708 | m_ret_val = -ECANCELED; | |
709 | } | |
710 | ||
711 | if (r < 0) { | |
712 | derr << ": failed to sync remote image: " << cpp_strerror(r) << dendl; | |
713 | m_ret_val = r; | |
714 | } | |
7c673cae FG |
715 | } |
716 | ||
717 | close_remote_image(); | |
718 | } | |
719 | ||
720 | template <typename I> | |
721 | void BootstrapRequest<I>::close_local_image() { | |
722 | dout(20) << dendl; | |
723 | ||
724 | update_progress("CLOSE_LOCAL_IMAGE"); | |
725 | ||
726 | Context *ctx = create_context_callback< | |
727 | BootstrapRequest<I>, &BootstrapRequest<I>::handle_close_local_image>( | |
728 | this); | |
729 | CloseImageRequest<I> *request = CloseImageRequest<I>::create( | |
730 | m_local_image_ctx, ctx); | |
731 | request->send(); | |
732 | } | |
733 | ||
734 | template <typename I> | |
735 | void BootstrapRequest<I>::handle_close_local_image(int r) { | |
736 | dout(20) << ": r=" << r << dendl; | |
737 | ||
738 | if (r < 0) { | |
739 | derr << ": error encountered closing local image: " << cpp_strerror(r) | |
740 | << dendl; | |
741 | } | |
742 | ||
743 | close_remote_image(); | |
744 | } | |
745 | ||
746 | template <typename I> | |
747 | void BootstrapRequest<I>::close_remote_image() { | |
748 | dout(20) << dendl; | |
749 | ||
750 | update_progress("CLOSE_REMOTE_IMAGE"); | |
751 | ||
752 | Context *ctx = create_context_callback< | |
753 | BootstrapRequest<I>, &BootstrapRequest<I>::handle_close_remote_image>( | |
754 | this); | |
755 | CloseImageRequest<I> *request = CloseImageRequest<I>::create( | |
756 | &m_remote_image_ctx, ctx); | |
757 | request->send(); | |
758 | } | |
759 | ||
760 | template <typename I> | |
761 | void BootstrapRequest<I>::handle_close_remote_image(int r) { | |
762 | dout(20) << ": r=" << r << dendl; | |
763 | ||
764 | if (r < 0) { | |
765 | derr << ": error encountered closing remote image: " << cpp_strerror(r) | |
766 | << dendl; | |
767 | } | |
768 | ||
769 | finish(m_ret_val); | |
770 | } | |
771 | ||
772 | template <typename I> | |
773 | bool BootstrapRequest<I>::decode_client_meta() { | |
774 | dout(20) << dendl; | |
775 | ||
776 | librbd::journal::ClientData client_data; | |
777 | bufferlist::iterator it = m_client.data.begin(); | |
778 | try { | |
779 | ::decode(client_data, it); | |
780 | } catch (const buffer::error &err) { | |
781 | derr << ": failed to decode client meta data: " << err.what() << dendl; | |
782 | return false; | |
783 | } | |
784 | ||
785 | librbd::journal::MirrorPeerClientMeta *client_meta = | |
786 | boost::get<librbd::journal::MirrorPeerClientMeta>(&client_data.client_meta); | |
787 | if (client_meta == nullptr) { | |
788 | derr << ": unknown peer registration" << dendl; | |
789 | return false; | |
790 | } else if (!client_meta->image_id.empty()) { | |
791 | // have an image id -- use that to open the image | |
792 | m_local_image_id = client_meta->image_id; | |
793 | } | |
794 | ||
795 | *m_client_meta = *client_meta; | |
796 | ||
797 | dout(20) << ": client found: image_id=" << m_local_image_id | |
798 | << ", client_meta=" << *m_client_meta << dendl; | |
799 | return true; | |
800 | } | |
801 | ||
802 | template <typename I> | |
803 | void BootstrapRequest<I>::update_progress(const std::string &description) { | |
804 | dout(20) << ": " << description << dendl; | |
805 | ||
806 | if (m_progress_ctx) { | |
807 | m_progress_ctx->update_progress(description); | |
808 | } | |
809 | } | |
810 | ||
811 | } // namespace image_replayer | |
812 | } // namespace mirror | |
813 | } // namespace rbd | |
814 | ||
815 | template class rbd::mirror::image_replayer::BootstrapRequest<librbd::ImageCtx>; |