-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageWatcher.cc
1558 lines (1338 loc) · 55.4 KB
/
ImageWatcher.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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "librbd/ImageWatcher.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "librbd/internal.h"
#include "librbd/TaskFinisher.h"
#include "librbd/Types.h"
#include "librbd/Utils.h"
#include "librbd/asio/ContextWQ.h"
#include "librbd/exclusive_lock/Policy.h"
#include "librbd/image_watcher/NotifyLockOwner.h"
#include "librbd/io/AioCompletion.h"
#include "include/encoding.h"
#include "common/errno.h"
#include <boost/bind/bind.hpp>
#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd::ImageWatcher: "
namespace librbd {
using namespace image_watcher;
using namespace watch_notify;
using util::create_async_context_callback;
using util::create_context_callback;
using util::create_rados_callback;
using ceph::encode;
using ceph::decode;
using namespace boost::placeholders;
static const double RETRY_DELAY_SECONDS = 1.0;
template <typename I>
struct ImageWatcher<I>::C_ProcessPayload : public Context {
ImageWatcher *image_watcher;
uint64_t notify_id;
uint64_t handle;
std::unique_ptr<watch_notify::Payload> payload;
C_ProcessPayload(ImageWatcher *image_watcher, uint64_t notify_id,
uint64_t handle,
std::unique_ptr<watch_notify::Payload> &&payload)
: image_watcher(image_watcher), notify_id(notify_id), handle(handle),
payload(std::move(payload)) {
}
void finish(int r) override {
image_watcher->m_async_op_tracker.start_op();
if (image_watcher->notifications_blocked()) {
// requests are blocked -- just ack the notification
bufferlist bl;
image_watcher->acknowledge_notify(notify_id, handle, bl);
} else {
image_watcher->process_payload(notify_id, handle, payload.get());
}
image_watcher->m_async_op_tracker.finish_op();
}
};
template <typename I>
ImageWatcher<I>::ImageWatcher(I &image_ctx)
: Watcher(image_ctx.md_ctx, image_ctx.op_work_queue, image_ctx.header_oid),
m_image_ctx(image_ctx),
m_task_finisher(new TaskFinisher<Task>(*m_image_ctx.cct)),
m_async_request_lock(ceph::make_shared_mutex(
util::unique_lock_name("librbd::ImageWatcher::m_async_request_lock", this))),
m_owner_client_id_lock(ceph::make_mutex(
util::unique_lock_name("librbd::ImageWatcher::m_owner_client_id_lock", this)))
{
}
template <typename I>
ImageWatcher<I>::~ImageWatcher()
{
delete m_task_finisher;
}
template <typename I>
void ImageWatcher<I>::unregister_watch(Context *on_finish) {
CephContext *cct = m_image_ctx.cct;
ldout(cct, 10) << this << " unregistering image watcher" << dendl;
cancel_async_requests();
// flush the task finisher queue before completing
on_finish = create_async_context_callback(m_task_finisher, on_finish);
on_finish = new LambdaContext([this, on_finish](int r) {
cancel_quiesce_requests();
m_task_finisher->cancel_all();
m_async_op_tracker.wait_for_ops(on_finish);
});
Watcher::unregister_watch(on_finish);
}
template <typename I>
void ImageWatcher<I>::block_notifies(Context *on_finish) {
CephContext *cct = m_image_ctx.cct;
ldout(cct, 10) << this << " " << __func__ << dendl;
on_finish = new LambdaContext([this, on_finish](int r) {
cancel_async_requests();
on_finish->complete(r);
});
Watcher::block_notifies(on_finish);
}
template <typename I>
void ImageWatcher<I>::schedule_async_progress(const AsyncRequestId &request,
uint64_t offset, uint64_t total) {
auto ctx = new LambdaContext([this, request, offset, total](int r) {
if (r != -ECANCELED) {
notify_async_progress(request, offset, total);
}
});
m_task_finisher->queue(Task(TASK_CODE_ASYNC_PROGRESS, request), ctx);
}
template <typename I>
int ImageWatcher<I>::notify_async_progress(const AsyncRequestId &request,
uint64_t offset, uint64_t total) {
ldout(m_image_ctx.cct, 20) << this << " remote async request progress: "
<< request << " @ " << offset
<< "/" << total << dendl;
send_notify(new AsyncProgressPayload(request, offset, total));
return 0;
}
template <typename I>
void ImageWatcher<I>::schedule_async_complete(const AsyncRequestId &request,
int r) {
m_async_op_tracker.start_op();
auto ctx = new LambdaContext([this, request, ret_val=r](int r) {
if (r != -ECANCELED) {
notify_async_complete(request, ret_val);
}
});
m_task_finisher->queue(ctx);
}
template <typename I>
void ImageWatcher<I>::notify_async_complete(const AsyncRequestId &request,
int r) {
ldout(m_image_ctx.cct, 20) << this << " remote async request finished: "
<< request << " = " << r << dendl;
send_notify(new AsyncCompletePayload(request, r),
new LambdaContext(boost::bind(&ImageWatcher<I>::handle_async_complete,
this, request, r, _1)));
}
template <typename I>
void ImageWatcher<I>::handle_async_complete(const AsyncRequestId &request,
int r, int ret_val) {
ldout(m_image_ctx.cct, 20) << this << " " << __func__ << ": "
<< "request=" << request << ", r=" << ret_val
<< dendl;
if (ret_val < 0) {
lderr(m_image_ctx.cct) << this << " failed to notify async complete: "
<< cpp_strerror(ret_val) << dendl;
if (ret_val == -ETIMEDOUT && !is_unregistered()) {
schedule_async_complete(request, r);
m_async_op_tracker.finish_op();
return;
}
}
std::unique_lock async_request_locker{m_async_request_lock};
mark_async_request_complete(request, r);
m_async_op_tracker.finish_op();
}
template <typename I>
void ImageWatcher<I>::notify_flatten(uint64_t request_id,
ProgressContext &prog_ctx,
Context *on_finish) {
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
ceph_assert(m_image_ctx.exclusive_lock &&
!m_image_ctx.exclusive_lock->is_lock_owner());
AsyncRequestId async_request_id(get_client_id(), request_id);
notify_async_request(async_request_id, new FlattenPayload(async_request_id),
prog_ctx, on_finish);
}
template <typename I>
void ImageWatcher<I>::notify_resize(uint64_t request_id, uint64_t size,
bool allow_shrink,
ProgressContext &prog_ctx,
Context *on_finish) {
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
ceph_assert(m_image_ctx.exclusive_lock &&
!m_image_ctx.exclusive_lock->is_lock_owner());
AsyncRequestId async_request_id(get_client_id(), request_id);
notify_async_request(async_request_id,
new ResizePayload(async_request_id, size, allow_shrink),
prog_ctx, on_finish);
}
template <typename I>
void ImageWatcher<I>::notify_snap_create(uint64_t request_id,
const cls::rbd::SnapshotNamespace &snap_namespace,
const std::string &snap_name,
uint64_t flags,
ProgressContext &prog_ctx,
Context *on_finish) {
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
ceph_assert(m_image_ctx.exclusive_lock &&
!m_image_ctx.exclusive_lock->is_lock_owner());
AsyncRequestId async_request_id(get_client_id(), request_id);
notify_async_request(async_request_id,
new SnapCreatePayload(async_request_id, snap_namespace,
snap_name, flags),
prog_ctx, on_finish);
}
template <typename I>
void ImageWatcher<I>::notify_snap_rename(uint64_t request_id,
const snapid_t &src_snap_id,
const std::string &dst_snap_name,
Context *on_finish) {
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
ceph_assert(m_image_ctx.exclusive_lock &&
!m_image_ctx.exclusive_lock->is_lock_owner());
AsyncRequestId async_request_id(get_client_id(), request_id);
notify_async_request(
async_request_id,
new SnapRenamePayload(async_request_id, src_snap_id, dst_snap_name),
m_no_op_prog_ctx, on_finish);
}
template <typename I>
void ImageWatcher<I>::notify_snap_remove(
uint64_t request_id, const cls::rbd::SnapshotNamespace &snap_namespace,
const std::string &snap_name, Context *on_finish) {
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
ceph_assert(m_image_ctx.exclusive_lock &&
!m_image_ctx.exclusive_lock->is_lock_owner());
AsyncRequestId async_request_id(get_client_id(), request_id);
notify_async_request(
async_request_id,
new SnapRemovePayload(async_request_id, snap_namespace, snap_name),
m_no_op_prog_ctx, on_finish);
}
template <typename I>
void ImageWatcher<I>::notify_snap_protect(
uint64_t request_id, const cls::rbd::SnapshotNamespace &snap_namespace,
const std::string &snap_name, Context *on_finish) {
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
ceph_assert(m_image_ctx.exclusive_lock &&
!m_image_ctx.exclusive_lock->is_lock_owner());
AsyncRequestId async_request_id(get_client_id(), request_id);
notify_async_request(
async_request_id,
new SnapProtectPayload(async_request_id, snap_namespace, snap_name),
m_no_op_prog_ctx, on_finish);
}
template <typename I>
void ImageWatcher<I>::notify_snap_unprotect(
uint64_t request_id, const cls::rbd::SnapshotNamespace &snap_namespace,
const std::string &snap_name, Context *on_finish) {
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
ceph_assert(m_image_ctx.exclusive_lock &&
!m_image_ctx.exclusive_lock->is_lock_owner());
AsyncRequestId async_request_id(get_client_id(), request_id);
notify_async_request(
async_request_id,
new SnapUnprotectPayload(async_request_id, snap_namespace, snap_name),
m_no_op_prog_ctx, on_finish);
}
template <typename I>
void ImageWatcher<I>::notify_rebuild_object_map(uint64_t request_id,
ProgressContext &prog_ctx,
Context *on_finish) {
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
ceph_assert(m_image_ctx.exclusive_lock &&
!m_image_ctx.exclusive_lock->is_lock_owner());
AsyncRequestId async_request_id(get_client_id(), request_id);
notify_async_request(async_request_id,
new RebuildObjectMapPayload(async_request_id),
prog_ctx, on_finish);
}
template <typename I>
void ImageWatcher<I>::notify_rename(uint64_t request_id,
const std::string &image_name,
Context *on_finish) {
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
ceph_assert(m_image_ctx.exclusive_lock &&
!m_image_ctx.exclusive_lock->is_lock_owner());
AsyncRequestId async_request_id(get_client_id(), request_id);
notify_async_request(async_request_id,
new RenamePayload(async_request_id, image_name),
m_no_op_prog_ctx, on_finish);
}
template <typename I>
void ImageWatcher<I>::notify_update_features(uint64_t request_id,
uint64_t features, bool enabled,
Context *on_finish) {
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
ceph_assert(m_image_ctx.exclusive_lock &&
!m_image_ctx.exclusive_lock->is_lock_owner());
AsyncRequestId async_request_id(get_client_id(), request_id);
notify_async_request(async_request_id,
new UpdateFeaturesPayload(async_request_id, features, enabled),
m_no_op_prog_ctx, on_finish);
}
template <typename I>
void ImageWatcher<I>::notify_migrate(uint64_t request_id,
ProgressContext &prog_ctx,
Context *on_finish) {
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
ceph_assert(m_image_ctx.exclusive_lock &&
!m_image_ctx.exclusive_lock->is_lock_owner());
AsyncRequestId async_request_id(get_client_id(), request_id);
notify_async_request(async_request_id, new MigratePayload(async_request_id),
prog_ctx, on_finish);
}
template <typename I>
void ImageWatcher<I>::notify_sparsify(uint64_t request_id, size_t sparse_size,
ProgressContext &prog_ctx,
Context *on_finish) {
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
ceph_assert(m_image_ctx.exclusive_lock &&
!m_image_ctx.exclusive_lock->is_lock_owner());
AsyncRequestId async_request_id(get_client_id(), request_id);
notify_async_request(async_request_id,
new SparsifyPayload(async_request_id, sparse_size),
prog_ctx, on_finish);
}
template <typename I>
void ImageWatcher<I>::notify_header_update(Context *on_finish) {
ldout(m_image_ctx.cct, 10) << this << ": " << __func__ << dendl;
// supports legacy (empty buffer) clients
send_notify(new HeaderUpdatePayload(), on_finish);
}
template <typename I>
void ImageWatcher<I>::notify_header_update(librados::IoCtx &io_ctx,
const std::string &oid) {
// supports legacy (empty buffer) clients
bufferlist bl;
encode(NotifyMessage(new HeaderUpdatePayload()), bl);
io_ctx.notify2(oid, bl, watcher::Notifier::NOTIFY_TIMEOUT, nullptr);
}
template <typename I>
void ImageWatcher<I>::notify_quiesce(uint64_t *request_id,
ProgressContext &prog_ctx,
Context *on_finish) {
*request_id = util::reserve_async_request_id();
ldout(m_image_ctx.cct, 10) << this << " " << __func__ << ": request_id="
<< request_id << dendl;
AsyncRequestId async_request_id(get_client_id(), *request_id);
auto attempts = m_image_ctx.config.template get_val<uint64_t>(
"rbd_quiesce_notification_attempts");
notify_quiesce(async_request_id, attempts, prog_ctx, on_finish);
}
template <typename I>
void ImageWatcher<I>::notify_quiesce(const AsyncRequestId &async_request_id,
size_t attempts, ProgressContext &prog_ctx,
Context *on_finish) {
ldout(m_image_ctx.cct, 10) << this << " " << __func__ << ": async_request_id="
<< async_request_id << " attempts=" << attempts
<< dendl;
ceph_assert(attempts > 0);
auto notify_response = new watcher::NotifyResponse();
auto on_notify = new LambdaContext(
[notify_response=std::unique_ptr<watcher::NotifyResponse>(notify_response),
this, async_request_id, &prog_ctx, on_finish, attempts=attempts-1](int r) {
auto total_attempts = m_image_ctx.config.template get_val<uint64_t>(
"rbd_quiesce_notification_attempts");
if (total_attempts < attempts) {
total_attempts = attempts;
}
prog_ctx.update_progress(total_attempts - attempts, total_attempts);
if (r == -ETIMEDOUT) {
ldout(m_image_ctx.cct, 10) << this << " " << __func__ << ": async_request_id="
<< async_request_id << " timed out" << dendl;
if (attempts > 0) {
notify_quiesce(async_request_id, attempts, prog_ctx, on_finish);
return;
}
} else if (r == 0) {
for (auto &[client_id, bl] : notify_response->acks) {
if (bl.length() == 0) {
continue;
}
try {
auto iter = bl.cbegin();
ResponseMessage response_message;
using ceph::decode;
decode(response_message, iter);
if (response_message.result != -EOPNOTSUPP) {
r = response_message.result;
}
} catch (const buffer::error &err) {
r = -EINVAL;
}
if (r < 0) {
break;
}
}
}
if (r < 0) {
lderr(m_image_ctx.cct) << this << " failed to notify quiesce: "
<< cpp_strerror(r) << dendl;
}
on_finish->complete(r);
});
bufferlist bl;
encode(NotifyMessage(new QuiescePayload(async_request_id)), bl);
Watcher::send_notify(bl, notify_response, on_notify);
}
template <typename I>
void ImageWatcher<I>::notify_unquiesce(uint64_t request_id, Context *on_finish) {
ldout(m_image_ctx.cct, 10) << this << " " << __func__ << ": request_id="
<< request_id << dendl;
AsyncRequestId async_request_id(get_client_id(), request_id);
send_notify(new UnquiescePayload(async_request_id), on_finish);
}
template <typename I>
void ImageWatcher<I>::notify_metadata_set(uint64_t request_id,
const std::string &key,
const std::string &value,
Context *on_finish) {
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
ceph_assert(m_image_ctx.exclusive_lock &&
!m_image_ctx.exclusive_lock->is_lock_owner());
AsyncRequestId async_request_id(get_client_id(), request_id);
notify_async_request(
async_request_id,
new MetadataUpdatePayload(async_request_id, key,
std::optional<std::string>{value}),
m_no_op_prog_ctx, on_finish);
}
template <typename I>
void ImageWatcher<I>::notify_metadata_remove(uint64_t request_id,
const std::string &key,
Context *on_finish) {
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
ceph_assert(m_image_ctx.exclusive_lock &&
!m_image_ctx.exclusive_lock->is_lock_owner());
AsyncRequestId async_request_id(get_client_id(), request_id);
notify_async_request(
async_request_id,
new MetadataUpdatePayload(async_request_id, key, std::nullopt),
m_no_op_prog_ctx, on_finish);
}
template <typename I>
void ImageWatcher<I>::schedule_cancel_async_requests() {
auto ctx = new LambdaContext([this](int r) {
if (r != -ECANCELED) {
cancel_async_requests();
}
});
m_task_finisher->queue(TASK_CODE_CANCEL_ASYNC_REQUESTS, ctx);
}
template <typename I>
void ImageWatcher<I>::cancel_async_requests() {
std::unique_lock l{m_async_request_lock};
for (auto iter = m_async_requests.begin(); iter != m_async_requests.end(); ) {
if (iter->second.second == nullptr) {
// Quiesce notify request. Skip.
iter++;
} else {
iter->second.first->complete(-ERESTART);
iter = m_async_requests.erase(iter);
}
}
}
template <typename I>
void ImageWatcher<I>::set_owner_client_id(const ClientId& client_id) {
ceph_assert(ceph_mutex_is_locked(m_owner_client_id_lock));
m_owner_client_id = client_id;
ldout(m_image_ctx.cct, 10) << this << " current lock owner: "
<< m_owner_client_id << dendl;
}
template <typename I>
ClientId ImageWatcher<I>::get_client_id() {
std::shared_lock l{this->m_watch_lock};
return ClientId(m_image_ctx.md_ctx.get_instance_id(), this->m_watch_handle);
}
template <typename I>
void ImageWatcher<I>::notify_acquired_lock() {
ldout(m_image_ctx.cct, 10) << this << " notify acquired lock" << dendl;
ClientId client_id = get_client_id();
{
std::lock_guard owner_client_id_locker{m_owner_client_id_lock};
set_owner_client_id(client_id);
}
send_notify(new AcquiredLockPayload(client_id));
}
template <typename I>
void ImageWatcher<I>::notify_released_lock() {
ldout(m_image_ctx.cct, 10) << this << " notify released lock" << dendl;
{
std::lock_guard owner_client_id_locker{m_owner_client_id_lock};
set_owner_client_id(ClientId());
}
send_notify(new ReleasedLockPayload(get_client_id()));
}
template <typename I>
void ImageWatcher<I>::schedule_request_lock(bool use_timer, int timer_delay) {
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
if (m_image_ctx.exclusive_lock == nullptr) {
// exclusive lock dynamically disabled via image refresh
return;
}
ceph_assert(m_image_ctx.exclusive_lock &&
!m_image_ctx.exclusive_lock->is_lock_owner());
std::shared_lock watch_locker{this->m_watch_lock};
if (this->is_registered(this->m_watch_lock)) {
ldout(m_image_ctx.cct, 15) << this << " requesting exclusive lock" << dendl;
auto ctx = new LambdaContext([this](int r) {
if (r != -ECANCELED) {
notify_request_lock();
}
});
if (use_timer) {
if (timer_delay < 0) {
timer_delay = RETRY_DELAY_SECONDS;
}
m_task_finisher->add_event_after(TASK_CODE_REQUEST_LOCK,
timer_delay, ctx);
} else {
m_task_finisher->queue(TASK_CODE_REQUEST_LOCK, ctx);
}
}
}
template <typename I>
void ImageWatcher<I>::notify_request_lock() {
std::shared_lock owner_locker{m_image_ctx.owner_lock};
std::shared_lock image_locker{m_image_ctx.image_lock};
// ExclusiveLock state machine can be dynamically disabled or
// race with task cancel
if (m_image_ctx.exclusive_lock == nullptr ||
m_image_ctx.exclusive_lock->is_lock_owner()) {
return;
}
ldout(m_image_ctx.cct, 10) << this << " notify request lock" << dendl;
notify_lock_owner(new RequestLockPayload(get_client_id(), false),
create_context_callback<
ImageWatcher, &ImageWatcher<I>::handle_request_lock>(this));
}
template <typename I>
void ImageWatcher<I>::handle_request_lock(int r) {
std::shared_lock owner_locker{m_image_ctx.owner_lock};
std::shared_lock image_locker{m_image_ctx.image_lock};
// ExclusiveLock state machine cannot transition -- but can be
// dynamically disabled
if (m_image_ctx.exclusive_lock == nullptr) {
return;
}
if (r == -ETIMEDOUT) {
ldout(m_image_ctx.cct, 5) << this << " timed out requesting lock: retrying"
<< dendl;
// treat this is a dead client -- so retest acquiring the lock
m_image_ctx.exclusive_lock->handle_peer_notification(0);
} else if (r == -EROFS) {
ldout(m_image_ctx.cct, 5) << this << " peer will not release lock" << dendl;
m_image_ctx.exclusive_lock->handle_peer_notification(r);
} else if (r < 0) {
lderr(m_image_ctx.cct) << this << " error requesting lock: "
<< cpp_strerror(r) << dendl;
schedule_request_lock(true);
} else {
// lock owner acked -- but resend if we don't see them release the lock
int retry_timeout = m_image_ctx.cct->_conf.template get_val<int64_t>(
"client_notify_timeout");
ldout(m_image_ctx.cct, 15) << this << " will retry in " << retry_timeout
<< " seconds" << dendl;
schedule_request_lock(true, retry_timeout);
}
}
template <typename I>
void ImageWatcher<I>::notify_lock_owner(Payload *payload, Context *on_finish) {
ceph_assert(on_finish != nullptr);
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
bufferlist bl;
encode(NotifyMessage(payload), bl);
NotifyLockOwner *notify_lock_owner = NotifyLockOwner::create(
m_image_ctx, this->m_notifier, std::move(bl), on_finish);
notify_lock_owner->send();
}
template <typename I>
bool ImageWatcher<I>::is_new_request(const AsyncRequestId &id) const {
ceph_assert(ceph_mutex_is_locked(m_async_request_lock));
return m_async_pending.count(id) == 0 && m_async_complete.count(id) == 0;
}
template <typename I>
bool ImageWatcher<I>::mark_async_request_complete(const AsyncRequestId &id,
int r) {
ceph_assert(ceph_mutex_is_locked(m_async_request_lock));
bool found = m_async_pending.erase(id);
auto now = ceph_clock_now();
auto it = m_async_complete_expiration.begin();
while (it != m_async_complete_expiration.end() && it->first < now) {
m_async_complete.erase(it->second);
it = m_async_complete_expiration.erase(it);
}
if (!m_async_complete.insert({id, r}).second) {
for (it = m_async_complete_expiration.begin();
it != m_async_complete_expiration.end(); it++) {
if (it->second == id) {
m_async_complete_expiration.erase(it);
break;
}
}
}
auto expiration_time = now;
expiration_time += 600;
m_async_complete_expiration.insert({expiration_time, id});
return found;
}
template <typename I>
Context *ImageWatcher<I>::remove_async_request(const AsyncRequestId &id) {
std::unique_lock async_request_locker{m_async_request_lock};
return remove_async_request(id, m_async_request_lock);
}
template <typename I>
Context *ImageWatcher<I>::remove_async_request(const AsyncRequestId &id,
ceph::shared_mutex &lock) {
ceph_assert(ceph_mutex_is_locked(lock));
ldout(m_image_ctx.cct, 20) << __func__ << ": " << id << dendl;
auto it = m_async_requests.find(id);
if (it != m_async_requests.end()) {
Context *on_complete = it->second.first;
m_async_requests.erase(it);
return on_complete;
}
return nullptr;
}
template <typename I>
void ImageWatcher<I>::schedule_async_request_timed_out(const AsyncRequestId &id) {
ldout(m_image_ctx.cct, 20) << "scheduling async request time out: " << id
<< dendl;
auto ctx = new LambdaContext([this, id](int r) {
if (r != -ECANCELED) {
async_request_timed_out(id);
}
});
Task task(TASK_CODE_ASYNC_REQUEST, id);
m_task_finisher->cancel(task);
m_task_finisher->add_event_after(
task, m_image_ctx.config.template get_val<uint64_t>("rbd_request_timed_out_seconds"),
ctx);
}
template <typename I>
void ImageWatcher<I>::async_request_timed_out(const AsyncRequestId &id) {
Context *on_complete = remove_async_request(id);
if (on_complete != nullptr) {
ldout(m_image_ctx.cct, 5) << "async request timed out: " << id << dendl;
m_image_ctx.op_work_queue->queue(on_complete, -ETIMEDOUT);
}
}
template <typename I>
void ImageWatcher<I>::notify_async_request(
const AsyncRequestId &async_request_id, Payload *payload,
ProgressContext& prog_ctx, Context *on_finish) {
ceph_assert(on_finish != nullptr);
ceph_assert(ceph_mutex_is_locked(m_image_ctx.owner_lock));
ldout(m_image_ctx.cct, 10) << this << " async request: " << async_request_id
<< dendl;
Context *on_notify = new LambdaContext([this, async_request_id](int r) {
if (r < 0) {
// notification failed -- don't expect updates
Context *on_complete = remove_async_request(async_request_id);
if (on_complete != nullptr) {
on_complete->complete(r);
}
}
});
Context *on_complete = new LambdaContext(
[this, async_request_id, on_finish](int r) {
m_task_finisher->cancel(Task(TASK_CODE_ASYNC_REQUEST, async_request_id));
on_finish->complete(r);
});
{
std::unique_lock async_request_locker{m_async_request_lock};
m_async_requests[async_request_id] = AsyncRequest(on_complete, &prog_ctx);
}
schedule_async_request_timed_out(async_request_id);
notify_lock_owner(payload, on_notify);
}
template <typename I>
int ImageWatcher<I>::prepare_async_request(const AsyncRequestId& async_request_id,
bool* new_request, Context** ctx,
ProgressContext** prog_ctx) {
if (async_request_id.client_id == get_client_id()) {
return -ERESTART;
} else {
std::unique_lock l{m_async_request_lock};
if (is_new_request(async_request_id)) {
m_async_pending.insert(async_request_id);
*new_request = true;
*prog_ctx = new RemoteProgressContext(*this, async_request_id);
*ctx = new RemoteContext(*this, async_request_id, *prog_ctx);
} else {
*new_request = false;
auto it = m_async_complete.find(async_request_id);
if (it != m_async_complete.end()) {
int r = it->second;
// reset complete request expiration time
mark_async_request_complete(async_request_id, r);
return r;
}
}
}
return 0;
}
template <typename I>
Context *ImageWatcher<I>::prepare_quiesce_request(
const AsyncRequestId &request, C_NotifyAck *ack_ctx) {
std::unique_lock locker{m_async_request_lock};
auto timeout = 2 * watcher::Notifier::NOTIFY_TIMEOUT / 1000;
if (!is_new_request(request)) {
auto it = m_async_requests.find(request);
if (it != m_async_requests.end()) {
delete it->second.first;
it->second.first = ack_ctx;
} else {
auto it = m_async_complete.find(request);
ceph_assert(it != m_async_complete.end());
m_task_finisher->queue(new C_ResponseMessage(ack_ctx), it->second);
// reset complete request expiration time
mark_async_request_complete(request, it->second);
}
locker.unlock();
m_task_finisher->reschedule_event_after(Task(TASK_CODE_QUIESCE, request),
timeout);
return nullptr;
}
m_async_pending.insert(request);
m_async_requests[request] = AsyncRequest(ack_ctx, nullptr);
m_async_op_tracker.start_op();
return new LambdaContext(
[this, request, timeout](int r) {
auto unquiesce_ctx = new LambdaContext(
[this, request](int r) {
if (r == 0) {
ldout(m_image_ctx.cct, 10) << this << " quiesce request "
<< request << " timed out" << dendl;
}
auto on_finish = new LambdaContext(
[this](int r) {
m_async_op_tracker.finish_op();
});
m_image_ctx.state->notify_unquiesce(on_finish);
});
m_task_finisher->add_event_after(Task(TASK_CODE_QUIESCE, request),
timeout, unquiesce_ctx);
std::unique_lock async_request_locker{m_async_request_lock};
mark_async_request_complete(request, r);
auto ctx = remove_async_request(request, m_async_request_lock);
async_request_locker.unlock();
if (ctx != nullptr) {
ctx = new C_ResponseMessage(static_cast<C_NotifyAck *>(ctx));
ctx->complete(r);
} else {
m_task_finisher->cancel(Task(TASK_CODE_QUIESCE, request));
}
});
}
template <typename I>
void ImageWatcher<I>::prepare_unquiesce_request(const AsyncRequestId &request) {
{
std::unique_lock async_request_locker{m_async_request_lock};
auto it = m_async_complete.find(request);
if (it == m_async_complete.end()) {
ldout(m_image_ctx.cct, 20) << this << " " << request
<< ": not found in complete" << dendl;
return;
}
// reset complete request expiration time
mark_async_request_complete(request, it->second);
}
bool canceled = m_task_finisher->cancel(Task(TASK_CODE_QUIESCE, request));
if (!canceled) {
ldout(m_image_ctx.cct, 20) << this << " " << request
<< ": timer task not found" << dendl;
}
}
template <typename I>
void ImageWatcher<I>::cancel_quiesce_requests() {
std::unique_lock l{m_async_request_lock};
for (auto it = m_async_requests.begin(); it != m_async_requests.end(); ) {
if (it->second.second == nullptr) {
// Quiesce notify request.
mark_async_request_complete(it->first, 0);
delete it->second.first;
it = m_async_requests.erase(it);
} else {
it++;
}
}
}
template <typename I>
bool ImageWatcher<I>::handle_operation_request(
const AsyncRequestId& async_request_id,
exclusive_lock::OperationRequestType request_type, Operation operation,
std::function<void(ProgressContext &prog_ctx, Context*)> execute,
C_NotifyAck *ack_ctx) {
std::shared_lock owner_locker{m_image_ctx.owner_lock};
if (m_image_ctx.exclusive_lock != nullptr) {
int r = 0;
if (m_image_ctx.exclusive_lock->accept_request(request_type, &r)) {
bool new_request;
Context *ctx;
ProgressContext *prog_ctx;
bool complete;
if (async_request_id) {
r = prepare_async_request(async_request_id, &new_request, &ctx,
&prog_ctx);
encode(ResponseMessage(r), ack_ctx->out);
complete = true;
} else {
new_request = true;
ctx = new C_ResponseMessage(ack_ctx);
prog_ctx = &m_no_op_prog_ctx;
complete = false;
}
if (r == 0 && new_request) {
ctx = new LambdaContext(
[this, operation, ctx](int r) {
m_image_ctx.operations->finish_op(operation, r);
ctx->complete(r);
});
ctx = new LambdaContext(
[this, execute, prog_ctx, ctx](int r) {
if (r < 0) {
ctx->complete(r);
return;
}
std::shared_lock l{m_image_ctx.owner_lock};
execute(*prog_ctx, ctx);
});
m_image_ctx.operations->start_op(operation, ctx);
}
return complete;
} else if (r < 0) {
encode(ResponseMessage(r), ack_ctx->out);
}
}
return true;
}
template <typename I>
bool ImageWatcher<I>::handle_payload(const HeaderUpdatePayload &payload,
C_NotifyAck *ack_ctx) {
ldout(m_image_ctx.cct, 10) << this << " image header updated" << dendl;
m_image_ctx.state->handle_update_notification();
m_image_ctx.perfcounter->inc(l_librbd_notify);
if (ack_ctx != nullptr) {
m_image_ctx.state->flush_update_watchers(new C_ResponseMessage(ack_ctx));
return false;
}
return true;
}
template <typename I>
bool ImageWatcher<I>::handle_payload(const AcquiredLockPayload &payload,
C_NotifyAck *ack_ctx) {
ldout(m_image_ctx.cct, 10) << this << " image exclusively locked announcement"
<< dendl;
bool cancel_async_requests = true;
if (payload.client_id.is_valid()) {
std::lock_guard owner_client_id_locker{m_owner_client_id_lock};
if (payload.client_id == m_owner_client_id) {
cancel_async_requests = false;
}
set_owner_client_id(payload.client_id);
}
std::shared_lock owner_locker{m_image_ctx.owner_lock};