/
BootstrapRequest.cc
713 lines (582 loc) · 19.5 KB
/
BootstrapRequest.cc
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "include/compat.h"
#include "BootstrapRequest.h"
#include "CloseImageRequest.h"
#include "CreateImageRequest.h"
#include "OpenImageRequest.h"
#include "OpenLocalImageRequest.h"
#include "common/debug.h"
#include "common/dout.h"
#include "common/errno.h"
#include "common/WorkQueue.h"
#include "cls/rbd/cls_rbd_client.h"
#include "journal/Journaler.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "librbd/internal.h"
#include "librbd/Journal.h"
#include "librbd/Utils.h"
#include "librbd/journal/Types.h"
#include "tools/rbd_mirror/ImageSync.h"
#include "tools/rbd_mirror/ProgressContext.h"
#include "tools/rbd_mirror/ImageSyncThrottler.h"
#define dout_subsys ceph_subsys_rbd_mirror
#undef dout_prefix
#define dout_prefix *_dout << "rbd::mirror::image_replayer::BootstrapRequest: " \
<< this << " " << __func__
namespace rbd {
namespace mirror {
namespace image_replayer {
using librbd::util::create_context_callback;
using librbd::util::create_rados_ack_callback;
using librbd::util::unique_lock_name;
template <typename I>
BootstrapRequest<I>::BootstrapRequest(
librados::IoCtx &local_io_ctx,
librados::IoCtx &remote_io_ctx,
std::shared_ptr<ImageSyncThrottler<I>> image_sync_throttler,
I **local_image_ctx,
const std::string &local_image_name,
const std::string &remote_image_id,
const std::string &global_image_id,
ContextWQ *work_queue, SafeTimer *timer,
Mutex *timer_lock,
const std::string &local_mirror_uuid,
const std::string &remote_mirror_uuid,
Journaler *journaler,
MirrorPeerClientMeta *client_meta,
Context *on_finish,
rbd::mirror::ProgressContext *progress_ctx)
: BaseRequest("rbd::mirror::image_replayer::BootstrapRequest",
reinterpret_cast<CephContext*>(local_io_ctx.cct()), on_finish),
m_local_io_ctx(local_io_ctx), m_remote_io_ctx(remote_io_ctx),
m_image_sync_throttler(image_sync_throttler),
m_local_image_ctx(local_image_ctx), m_local_image_name(local_image_name),
m_remote_image_id(remote_image_id), m_global_image_id(global_image_id),
m_work_queue(work_queue), m_timer(timer), m_timer_lock(timer_lock),
m_local_mirror_uuid(local_mirror_uuid),
m_remote_mirror_uuid(remote_mirror_uuid), m_journaler(journaler),
m_client_meta(client_meta), m_progress_ctx(progress_ctx),
m_lock(unique_lock_name("BootstrapRequest::m_lock", this)) {
}
template <typename I>
BootstrapRequest<I>::~BootstrapRequest() {
assert(m_remote_image_ctx == nullptr);
}
template <typename I>
void BootstrapRequest<I>::send() {
get_local_image_id();
}
template <typename I>
void BootstrapRequest<I>::cancel() {
dout(20) << dendl;
Mutex::Locker locker(m_lock);
m_canceled = true;
m_image_sync_throttler->cancel_sync(m_local_io_ctx, m_local_image_id);
}
template <typename I>
void BootstrapRequest<I>::get_local_image_id() {
dout(20) << dendl;
update_progress("GET_LOCAL_IMAGE_ID");
// attempt to cross-reference a local image by the global image id
librados::ObjectReadOperation op;
librbd::cls_client::mirror_image_get_image_id_start(&op, m_global_image_id);
librados::AioCompletion *aio_comp = create_rados_ack_callback<
BootstrapRequest<I>, &BootstrapRequest<I>::handle_get_local_image_id>(
this);
int r = m_local_io_ctx.aio_operate(RBD_MIRRORING, aio_comp, &op, &m_out_bl);
assert(r == 0);
aio_comp->release();
}
template <typename I>
void BootstrapRequest<I>::handle_get_local_image_id(int r) {
dout(20) << ": r=" << r << dendl;
if (r == 0) {
bufferlist::iterator iter = m_out_bl.begin();
r = librbd::cls_client::mirror_image_get_image_id_finish(
&iter, &m_local_image_id);
}
if (r == -ENOENT) {
dout(10) << ": image not registered locally" << dendl;
} else if (r < 0) {
derr << ": failed to retrieve local image id: " << cpp_strerror(r) << dendl;
finish(r);
return;
}
get_remote_tag_class();
}
template <typename I>
void BootstrapRequest<I>::get_remote_tag_class() {
dout(20) << dendl;
update_progress("GET_REMOTE_TAG_CLASS");
Context *ctx = create_context_callback<
BootstrapRequest<I>, &BootstrapRequest<I>::handle_get_remote_tag_class>(
this);
m_journaler->get_client(librbd::Journal<>::IMAGE_CLIENT_ID, &m_client, ctx);
}
template <typename I>
void BootstrapRequest<I>::handle_get_remote_tag_class(int r) {
dout(20) << ": r=" << r << dendl;
if (r < 0) {
derr << ": failed to retrieve remote client: " << cpp_strerror(r) << dendl;
finish(r);
return;
}
librbd::journal::ClientData client_data;
bufferlist::iterator it = m_client.data.begin();
try {
::decode(client_data, it);
} catch (const buffer::error &err) {
derr << ": failed to decode remote client meta data: " << err.what()
<< dendl;
finish(-EBADMSG);
return;
}
librbd::journal::ImageClientMeta *client_meta =
boost::get<librbd::journal::ImageClientMeta>(&client_data.client_meta);
if (client_meta == nullptr) {
derr << ": unknown remote client registration" << dendl;
finish(-EINVAL);
return;
}
m_remote_tag_class = client_meta->tag_class;
dout(10) << ": remote tag class=" << m_remote_tag_class << dendl;
get_client();
}
template <typename I>
void BootstrapRequest<I>::get_client() {
dout(20) << dendl;
update_progress("GET_CLIENT");
Context *ctx = create_context_callback<
BootstrapRequest<I>, &BootstrapRequest<I>::handle_get_client>(
this);
m_journaler->get_client(m_local_mirror_uuid, &m_client, ctx);
}
template <typename I>
void BootstrapRequest<I>::handle_get_client(int r) {
dout(20) << ": r=" << r << dendl;
if (r == -ENOENT) {
dout(10) << ": client not registered" << dendl;
} else if (r < 0) {
derr << ": failed to retrieve client: " << cpp_strerror(r) << dendl;
finish(r);
return;
} else if (m_client.state == cls::journal::CLIENT_STATE_DISCONNECTED) {
dout(0) << ": client flagged disconnected, re-registering" << dendl;
unregister_client();
return;
} else if (decode_client_meta()) {
// skip registration if it already exists
open_remote_image();
return;
}
register_client();
}
template <typename I>
void BootstrapRequest<I>::unregister_client() {
dout(20) << dendl;
update_progress("UNREGISTER_CLIENT");
Context *ctx = create_context_callback<
BootstrapRequest<I>, &BootstrapRequest<I>::handle_unregister_client>(
this);
m_journaler->unregister_client(ctx);
}
template <typename I>
void BootstrapRequest<I>::handle_unregister_client(int r) {
dout(20) << ": r=" << r << dendl;
if (r < 0) {
derr << ": failed to unregister with remote journal: " << cpp_strerror(r)
<< dendl;
finish(r);
return;
}
register_client();
}
template <typename I>
void BootstrapRequest<I>::register_client() {
dout(20) << dendl;
update_progress("REGISTER_CLIENT");
// record an place-holder record
librbd::journal::ClientData client_data{
librbd::journal::MirrorPeerClientMeta{m_local_image_id}};
bufferlist client_data_bl;
::encode(client_data, client_data_bl);
Context *ctx = create_context_callback<
BootstrapRequest<I>, &BootstrapRequest<I>::handle_register_client>(
this);
m_journaler->register_client(client_data_bl, ctx);
}
template <typename I>
void BootstrapRequest<I>::handle_register_client(int r) {
dout(20) << ": r=" << r << dendl;
if (r < 0) {
derr << ": failed to register with remote journal: " << cpp_strerror(r)
<< dendl;
finish(r);
return;
}
*m_client_meta = librbd::journal::MirrorPeerClientMeta(m_local_image_id);
open_remote_image();
}
template <typename I>
void BootstrapRequest<I>::open_remote_image() {
dout(20) << dendl;
update_progress("OPEN_REMOTE_IMAGE");
Context *ctx = create_context_callback<
BootstrapRequest<I>, &BootstrapRequest<I>::handle_open_remote_image>(
this);
OpenImageRequest<I> *request = OpenImageRequest<I>::create(
m_remote_io_ctx, &m_remote_image_ctx, m_remote_image_id, false,
m_work_queue, ctx);
request->send();
}
template <typename I>
void BootstrapRequest<I>::handle_open_remote_image(int r) {
// deduce the class type for the journal to support unit tests
typedef typename std::decay<decltype(*I::journal)>::type Journal;
dout(20) << ": r=" << r << dendl;
if (r < 0) {
derr << ": failed to open remote image: " << cpp_strerror(r) << dendl;
assert(m_remote_image_ctx == nullptr);
finish(r);
return;
}
// TODO: make async
bool tag_owner;
r = Journal::is_tag_owner(m_remote_image_ctx, &tag_owner);
if (r < 0) {
derr << ": failed to query remote image primary status: " << cpp_strerror(r)
<< dendl;
m_ret_val = r;
close_remote_image();
return;
}
if (!tag_owner) {
dout(5) << ": remote image is not primary -- skipping image replay"
<< dendl;
m_ret_val = -EREMOTEIO;
close_remote_image();
return;
}
// default local image name to the remote image name if not provided
if (m_local_image_name.empty()) {
m_local_image_name = m_remote_image_ctx->name;
}
if (m_local_image_id.empty()) {
create_local_image();
return;
}
open_local_image();
}
template <typename I>
void BootstrapRequest<I>::open_local_image() {
dout(20) << dendl;
update_progress("OPEN_LOCAL_IMAGE");
Context *ctx = create_context_callback<
BootstrapRequest<I>, &BootstrapRequest<I>::handle_open_local_image>(
this);
OpenLocalImageRequest<I> *request = OpenLocalImageRequest<I>::create(
m_local_io_ctx, m_local_image_ctx,
(!m_local_image_id.empty() ? std::string() : m_local_image_name),
m_local_image_id, m_work_queue, ctx);
request->send();
}
template <typename I>
void BootstrapRequest<I>::handle_open_local_image(int r) {
dout(20) << ": r=" << r << dendl;
if (r == -ENOENT) {
assert(*m_local_image_ctx == nullptr);
dout(10) << ": local image missing" << dendl;
create_local_image();
return;
} else if (r == -EREMOTEIO) {
assert(*m_local_image_ctx == nullptr);
dout(10) << "local image is primary -- skipping image replay" << dendl;
m_ret_val = r;
close_remote_image();
return;
} else if (r < 0) {
assert(*m_local_image_ctx == nullptr);
derr << ": failed to open local image: " << cpp_strerror(r) << dendl;
m_ret_val = r;
close_remote_image();
return;
}
update_client();
}
template <typename I>
void BootstrapRequest<I>::remove_local_image() {
dout(20) << dendl;
update_progress("REMOVE_LOCAL_IMAGE");
// TODO
}
template <typename I>
void BootstrapRequest<I>::handle_remove_local_image(int r) {
dout(20) << ": r=" << r << dendl;
// TODO
}
template <typename I>
void BootstrapRequest<I>::create_local_image() {
dout(20) << dendl;
m_local_image_id = "";
update_progress("CREATE_LOCAL_IMAGE");
Context *ctx = create_context_callback<
BootstrapRequest<I>, &BootstrapRequest<I>::handle_create_local_image>(
this);
CreateImageRequest<I> *request = CreateImageRequest<I>::create(
m_local_io_ctx, m_work_queue, m_global_image_id, m_remote_mirror_uuid,
m_local_image_name, m_remote_image_ctx, ctx);
request->send();
}
template <typename I>
void BootstrapRequest<I>::handle_create_local_image(int r) {
dout(20) << ": r=" << r << dendl;
if (r < 0) {
derr << ": failed to create local image: " << cpp_strerror(r) << dendl;
m_ret_val = r;
close_remote_image();
return;
}
m_created_local_image = true;
open_local_image();
}
template <typename I>
void BootstrapRequest<I>::update_client() {
dout(20) << dendl;
update_progress("UPDATE_CLIENT");
if (m_client_meta->image_id == (*m_local_image_ctx)->id) {
// already registered local image with remote journal
get_remote_tags();
return;
}
m_local_image_id = (*m_local_image_ctx)->id;
dout(20) << dendl;
librbd::journal::MirrorPeerClientMeta client_meta;
client_meta.image_id = m_local_image_id;
librbd::journal::ClientData client_data(client_meta);
bufferlist data_bl;
::encode(client_data, data_bl);
Context *ctx = create_context_callback<
BootstrapRequest<I>, &BootstrapRequest<I>::handle_update_client>(
this);
m_journaler->update_client(data_bl, ctx);
}
template <typename I>
void BootstrapRequest<I>::handle_update_client(int r) {
dout(20) << ": r=" << r << dendl;
if (r < 0) {
derr << ": failed to update client: " << cpp_strerror(r) << dendl;
m_ret_val = r;
close_local_image();
return;
}
if (m_canceled) {
dout(10) << ": request canceled" << dendl;
m_ret_val = -ECANCELED;
close_local_image();
return;
}
m_client_meta->image_id = m_local_image_id;
get_remote_tags();
}
template <typename I>
void BootstrapRequest<I>::get_remote_tags() {
dout(20) << dendl;
update_progress("GET_REMOTE_TAGS");
if (m_created_local_image) {
// optimization -- no need to compare remote tags if we just created
// the image locally
image_sync();
return;
}
dout(20) << dendl;
Context *ctx = create_context_callback<
BootstrapRequest<I>, &BootstrapRequest<I>::handle_get_remote_tags>(this);
m_journaler->get_tags(m_remote_tag_class, &m_remote_tags, ctx);
}
template <typename I>
void BootstrapRequest<I>::handle_get_remote_tags(int r) {
dout(20) << ": r=" << r << dendl;
if (r < 0) {
derr << ": failed to retrieve remote tags: " << cpp_strerror(r) << dendl;
m_ret_val = r;
close_local_image();
return;
}
if (m_canceled) {
dout(10) << ": request canceled" << dendl;
m_ret_val = -ECANCELED;
close_local_image();
return;
}
// decode the remote tags
librbd::journal::TagData remote_tag_data;
for (auto &tag : m_remote_tags) {
try {
bufferlist::iterator it = tag.data.begin();
::decode(remote_tag_data, it);
} catch (const buffer::error &err) {
derr << ": failed to decode remote tag: " << err.what() << dendl;
m_ret_val = -EBADMSG;
close_local_image();
return;
}
dout(10) << ": decoded remote tag: " << remote_tag_data << dendl;
if (remote_tag_data.mirror_uuid == librbd::Journal<>::ORPHAN_MIRROR_UUID &&
remote_tag_data.predecessor_mirror_uuid == m_local_mirror_uuid) {
// remote tag is chained off a local tag demotion
break;
}
}
// At this point, the local image was existing and non-primary and the remote
// image is primary. Attempt to link the local image's most recent tag
// to the remote image's tag chain.
I *local_image_ctx = (*m_local_image_ctx);
{
RWLock::RLocker snap_locker(local_image_ctx->snap_lock);
if (local_image_ctx->journal == nullptr) {
derr << ": local image does not support journaling" << dendl;
m_ret_val = -EINVAL;
close_local_image();
return;
}
librbd::journal::TagData tag_data =
local_image_ctx->journal->get_tag_data();
dout(20) << ": local tag data: " << tag_data << dendl;
if (tag_data.mirror_uuid == librbd::Journal<>::ORPHAN_MIRROR_UUID &&
remote_tag_data.mirror_uuid == librbd::Journal<>::ORPHAN_MIRROR_UUID &&
remote_tag_data.predecessor_mirror_uuid == m_local_mirror_uuid) {
dout(20) << ": local image was demoted" << dendl;
} else if (tag_data.mirror_uuid == m_remote_mirror_uuid &&
m_client_meta->state == librbd::journal::MIRROR_PEER_STATE_REPLAYING) {
dout(20) << ": local image is in clean replay state" << dendl;
} else if (m_client_meta->state == librbd::journal::MIRROR_PEER_STATE_SYNCING) {
dout(20) << ": previous sync was canceled" << dendl;
} else {
derr << ": split-brain detected -- skipping image replay" << dendl;
m_ret_val = -EEXIST;
close_local_image();
return;
}
}
image_sync();
}
template <typename I>
void BootstrapRequest<I>::image_sync() {
dout(20) << dendl;
update_progress("IMAGE_SYNC");
if (m_client_meta->state == librbd::journal::MIRROR_PEER_STATE_REPLAYING) {
// clean replay state -- no image sync required
close_remote_image();
return;
}
dout(20) << dendl;
Context *ctx = create_context_callback<
BootstrapRequest<I>, &BootstrapRequest<I>::handle_image_sync>(
this);
{
Mutex::Locker locker(m_lock);
if (!m_canceled) {
m_image_sync_throttler->start_sync(*m_local_image_ctx,
m_remote_image_ctx, m_timer,
m_timer_lock,
m_local_mirror_uuid, m_journaler,
m_client_meta, m_work_queue, ctx,
m_progress_ctx);
return;
}
}
dout(10) << ": request canceled" << dendl;
m_ret_val = -ECANCELED;
close_remote_image();
}
template <typename I>
void BootstrapRequest<I>::handle_image_sync(int r) {
dout(20) << ": r=" << r << dendl;
if (m_canceled) {
dout(10) << ": request canceled" << dendl;
m_ret_val = -ECANCELED;
}
if (r < 0) {
derr << ": failed to sync remote image: " << cpp_strerror(r) << dendl;
m_ret_val = r;
}
close_remote_image();
}
template <typename I>
void BootstrapRequest<I>::close_local_image() {
dout(20) << dendl;
update_progress("CLOSE_LOCAL_IMAGE");
Context *ctx = create_context_callback<
BootstrapRequest<I>, &BootstrapRequest<I>::handle_close_local_image>(
this);
CloseImageRequest<I> *request = CloseImageRequest<I>::create(
m_local_image_ctx, m_work_queue, false, ctx);
request->send();
}
template <typename I>
void BootstrapRequest<I>::handle_close_local_image(int r) {
dout(20) << ": r=" << r << dendl;
if (r < 0) {
derr << ": error encountered closing local image: " << cpp_strerror(r)
<< dendl;
}
close_remote_image();
}
template <typename I>
void BootstrapRequest<I>::close_remote_image() {
dout(20) << dendl;
update_progress("CLOSE_REMOTE_IMAGE");
Context *ctx = create_context_callback<
BootstrapRequest<I>, &BootstrapRequest<I>::handle_close_remote_image>(
this);
CloseImageRequest<I> *request = CloseImageRequest<I>::create(
&m_remote_image_ctx, m_work_queue, false, ctx);
request->send();
}
template <typename I>
void BootstrapRequest<I>::handle_close_remote_image(int r) {
dout(20) << ": r=" << r << dendl;
if (r < 0) {
derr << ": error encountered closing remote image: " << cpp_strerror(r)
<< dendl;
}
finish(m_ret_val);
}
template <typename I>
bool BootstrapRequest<I>::decode_client_meta() {
dout(20) << dendl;
librbd::journal::ClientData client_data;
bufferlist::iterator it = m_client.data.begin();
try {
::decode(client_data, it);
} catch (const buffer::error &err) {
derr << ": failed to decode client meta data: " << err.what() << dendl;
return false;
}
librbd::journal::MirrorPeerClientMeta *client_meta =
boost::get<librbd::journal::MirrorPeerClientMeta>(&client_data.client_meta);
if (client_meta == nullptr) {
derr << ": unknown peer registration" << dendl;
return false;
} else if (!client_meta->image_id.empty()) {
// have an image id -- use that to open the image
m_local_image_id = client_meta->image_id;
}
*m_client_meta = *client_meta;
dout(20) << ": client found: image_id=" << m_local_image_id
<< ", client_meta=" << *m_client_meta << dendl;
return true;
}
template <typename I>
void BootstrapRequest<I>::update_progress(const std::string &description) {
dout(20) << ": " << description << dendl;
if (m_progress_ctx) {
m_progress_ctx->update_progress(description);
}
}
} // namespace image_replayer
} // namespace mirror
} // namespace rbd
template class rbd::mirror::image_replayer::BootstrapRequest<librbd::ImageCtx>;