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