-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjecter.cc
5510 lines (4780 loc) · 149 KB
/
Objecter.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
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2006 Sage Weil <[email protected]>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include <algorithm>
#include <cerrno>
#include "Objecter.h"
#include "osd/OSDMap.h"
#include "osd/error_code.h"
#include "Filer.h"
#include "mon/MonClient.h"
#include "mon/error_code.h"
#include "msg/Messenger.h"
#include "msg/Message.h"
#include "messages/MPing.h"
#include "messages/MOSDOp.h"
#include "messages/MOSDOpReply.h"
#include "messages/MOSDBackoff.h"
#include "messages/MOSDMap.h"
#include "messages/MPoolOp.h"
#include "messages/MPoolOpReply.h"
#include "messages/MGetPoolStats.h"
#include "messages/MGetPoolStatsReply.h"
#include "messages/MStatfs.h"
#include "messages/MStatfsReply.h"
#include "messages/MMonCommand.h"
#include "messages/MCommand.h"
#include "messages/MCommandReply.h"
#include "messages/MWatchNotify.h"
#include "common/Cond.h"
#include "common/config.h"
#include "common/perf_counters.h"
#include "common/scrub_types.h"
#include "include/str_list.h"
#include "common/errno.h"
#include "common/EventTrace.h"
#include "common/async/waiter.h"
#include "error_code.h"
using std::list;
using std::make_pair;
using std::map;
using std::ostream;
using std::ostringstream;
using std::pair;
using std::set;
using std::string;
using std::stringstream;
using std::vector;
using ceph::decode;
using ceph::encode;
using ceph::Formatter;
using std::defer_lock;
using std::scoped_lock;
using std::shared_lock;
using std::unique_lock;
using ceph::real_time;
using ceph::real_clock;
using ceph::mono_clock;
using ceph::mono_time;
using ceph::timespan;
using ceph::shunique_lock;
using ceph::acquire_shared;
using ceph::acquire_unique;
namespace bc = boost::container;
namespace bs = boost::system;
namespace ca = ceph::async;
namespace cb = ceph::buffer;
namespace asio = boost::asio;
#define dout_subsys ceph_subsys_objecter
#undef dout_prefix
#define dout_prefix *_dout << messenger->get_myname() << ".objecter "
enum {
l_osdc_first = 123200,
l_osdc_op_active,
l_osdc_op_laggy,
l_osdc_op_send,
l_osdc_op_send_bytes,
l_osdc_op_resend,
l_osdc_op_reply,
l_osdc_op_latency,
l_osdc_op_inflight,
l_osdc_oplen_avg,
l_osdc_op,
l_osdc_op_r,
l_osdc_op_w,
l_osdc_op_rmw,
l_osdc_op_pg,
l_osdc_osdop_stat,
l_osdc_osdop_create,
l_osdc_osdop_read,
l_osdc_osdop_write,
l_osdc_osdop_writefull,
l_osdc_osdop_writesame,
l_osdc_osdop_append,
l_osdc_osdop_zero,
l_osdc_osdop_truncate,
l_osdc_osdop_delete,
l_osdc_osdop_mapext,
l_osdc_osdop_sparse_read,
l_osdc_osdop_clonerange,
l_osdc_osdop_getxattr,
l_osdc_osdop_setxattr,
l_osdc_osdop_cmpxattr,
l_osdc_osdop_rmxattr,
l_osdc_osdop_resetxattrs,
l_osdc_osdop_call,
l_osdc_osdop_watch,
l_osdc_osdop_notify,
l_osdc_osdop_src_cmpxattr,
l_osdc_osdop_pgls,
l_osdc_osdop_pgls_filter,
l_osdc_osdop_other,
l_osdc_linger_active,
l_osdc_linger_send,
l_osdc_linger_resend,
l_osdc_linger_ping,
l_osdc_poolop_active,
l_osdc_poolop_send,
l_osdc_poolop_resend,
l_osdc_poolstat_active,
l_osdc_poolstat_send,
l_osdc_poolstat_resend,
l_osdc_statfs_active,
l_osdc_statfs_send,
l_osdc_statfs_resend,
l_osdc_command_active,
l_osdc_command_send,
l_osdc_command_resend,
l_osdc_map_epoch,
l_osdc_map_full,
l_osdc_map_inc,
l_osdc_osd_sessions,
l_osdc_osd_session_open,
l_osdc_osd_session_close,
l_osdc_osd_laggy,
l_osdc_osdop_omap_wr,
l_osdc_osdop_omap_rd,
l_osdc_osdop_omap_del,
l_osdc_last,
};
namespace {
inline bs::error_code osdcode(int r) {
return (r < 0) ? bs::error_code(-r, osd_category()) : bs::error_code();
}
}
// config obs ----------------------------
class Objecter::RequestStateHook : public AdminSocketHook {
Objecter *m_objecter;
public:
explicit RequestStateHook(Objecter *objecter);
int call(std::string_view command, const cmdmap_t& cmdmap,
const bufferlist&,
Formatter *f,
std::ostream& ss,
cb::list& out) override;
};
std::unique_lock<std::mutex> Objecter::OSDSession::get_lock(object_t& oid)
{
if (oid.name.empty())
return {};
static constexpr uint32_t HASH_PRIME = 1021;
uint32_t h = ceph_str_hash_linux(oid.name.c_str(), oid.name.size())
% HASH_PRIME;
return {completion_locks[h % num_locks], std::defer_lock};
}
const char** Objecter::get_tracked_conf_keys() const
{
static const char *config_keys[] = {
"crush_location",
"rados_mon_op_timeout",
"rados_osd_op_timeout",
NULL
};
return config_keys;
}
void Objecter::handle_conf_change(const ConfigProxy& conf,
const std::set <std::string> &changed)
{
if (changed.count("crush_location")) {
update_crush_location();
}
if (changed.count("rados_mon_op_timeout")) {
mon_timeout = conf.get_val<std::chrono::seconds>("rados_mon_op_timeout");
}
if (changed.count("rados_osd_op_timeout")) {
osd_timeout = conf.get_val<std::chrono::seconds>("rados_osd_op_timeout");
}
}
void Objecter::update_crush_location()
{
unique_lock wl(rwlock);
crush_location = cct->crush_location.get_location();
}
// messages ------------------------------
/*
* initialize only internal data structures, don't initiate cluster interaction
*/
void Objecter::init()
{
ceph_assert(!initialized);
if (!logger) {
PerfCountersBuilder pcb(cct, "objecter", l_osdc_first, l_osdc_last);
pcb.add_u64(l_osdc_op_active, "op_active", "Operations active", "actv",
PerfCountersBuilder::PRIO_CRITICAL);
pcb.add_u64(l_osdc_op_laggy, "op_laggy", "Laggy operations");
pcb.add_u64_counter(l_osdc_op_send, "op_send", "Sent operations");
pcb.add_u64_counter(l_osdc_op_send_bytes, "op_send_bytes", "Sent data", NULL, 0, unit_t(UNIT_BYTES));
pcb.add_u64_counter(l_osdc_op_resend, "op_resend", "Resent operations");
pcb.add_u64_counter(l_osdc_op_reply, "op_reply", "Operation reply");
pcb.add_time_avg(l_osdc_op_latency, "op_latency", "Operation latency");
pcb.add_u64(l_osdc_op_inflight, "op_inflight", "Operations in flight");
pcb.add_u64_avg(l_osdc_oplen_avg, "oplen_avg", "Average length of operation vector");
pcb.add_u64_counter(l_osdc_op, "op", "Operations");
pcb.add_u64_counter(l_osdc_op_r, "op_r", "Read operations", "rd",
PerfCountersBuilder::PRIO_CRITICAL);
pcb.add_u64_counter(l_osdc_op_w, "op_w", "Write operations", "wr",
PerfCountersBuilder::PRIO_CRITICAL);
pcb.add_u64_counter(l_osdc_op_rmw, "op_rmw", "Read-modify-write operations",
"rdwr", PerfCountersBuilder::PRIO_INTERESTING);
pcb.add_u64_counter(l_osdc_op_pg, "op_pg", "PG operation");
pcb.add_u64_counter(l_osdc_osdop_stat, "osdop_stat", "Stat operations");
pcb.add_u64_counter(l_osdc_osdop_create, "osdop_create",
"Create object operations");
pcb.add_u64_counter(l_osdc_osdop_read, "osdop_read", "Read operations");
pcb.add_u64_counter(l_osdc_osdop_write, "osdop_write", "Write operations");
pcb.add_u64_counter(l_osdc_osdop_writefull, "osdop_writefull",
"Write full object operations");
pcb.add_u64_counter(l_osdc_osdop_writesame, "osdop_writesame",
"Write same operations");
pcb.add_u64_counter(l_osdc_osdop_append, "osdop_append",
"Append operation");
pcb.add_u64_counter(l_osdc_osdop_zero, "osdop_zero",
"Set object to zero operations");
pcb.add_u64_counter(l_osdc_osdop_truncate, "osdop_truncate",
"Truncate object operations");
pcb.add_u64_counter(l_osdc_osdop_delete, "osdop_delete",
"Delete object operations");
pcb.add_u64_counter(l_osdc_osdop_mapext, "osdop_mapext",
"Map extent operations");
pcb.add_u64_counter(l_osdc_osdop_sparse_read, "osdop_sparse_read",
"Sparse read operations");
pcb.add_u64_counter(l_osdc_osdop_clonerange, "osdop_clonerange",
"Clone range operations");
pcb.add_u64_counter(l_osdc_osdop_getxattr, "osdop_getxattr",
"Get xattr operations");
pcb.add_u64_counter(l_osdc_osdop_setxattr, "osdop_setxattr",
"Set xattr operations");
pcb.add_u64_counter(l_osdc_osdop_cmpxattr, "osdop_cmpxattr",
"Xattr comparison operations");
pcb.add_u64_counter(l_osdc_osdop_rmxattr, "osdop_rmxattr",
"Remove xattr operations");
pcb.add_u64_counter(l_osdc_osdop_resetxattrs, "osdop_resetxattrs",
"Reset xattr operations");
pcb.add_u64_counter(l_osdc_osdop_call, "osdop_call",
"Call (execute) operations");
pcb.add_u64_counter(l_osdc_osdop_watch, "osdop_watch",
"Watch by object operations");
pcb.add_u64_counter(l_osdc_osdop_notify, "osdop_notify",
"Notify about object operations");
pcb.add_u64_counter(l_osdc_osdop_src_cmpxattr, "osdop_src_cmpxattr",
"Extended attribute comparison in multi operations");
pcb.add_u64_counter(l_osdc_osdop_pgls, "osdop_pgls");
pcb.add_u64_counter(l_osdc_osdop_pgls_filter, "osdop_pgls_filter");
pcb.add_u64_counter(l_osdc_osdop_other, "osdop_other", "Other operations");
pcb.add_u64(l_osdc_linger_active, "linger_active",
"Active lingering operations");
pcb.add_u64_counter(l_osdc_linger_send, "linger_send",
"Sent lingering operations");
pcb.add_u64_counter(l_osdc_linger_resend, "linger_resend",
"Resent lingering operations");
pcb.add_u64_counter(l_osdc_linger_ping, "linger_ping",
"Sent pings to lingering operations");
pcb.add_u64(l_osdc_poolop_active, "poolop_active",
"Active pool operations");
pcb.add_u64_counter(l_osdc_poolop_send, "poolop_send",
"Sent pool operations");
pcb.add_u64_counter(l_osdc_poolop_resend, "poolop_resend",
"Resent pool operations");
pcb.add_u64(l_osdc_poolstat_active, "poolstat_active",
"Active get pool stat operations");
pcb.add_u64_counter(l_osdc_poolstat_send, "poolstat_send",
"Pool stat operations sent");
pcb.add_u64_counter(l_osdc_poolstat_resend, "poolstat_resend",
"Resent pool stats");
pcb.add_u64(l_osdc_statfs_active, "statfs_active", "Statfs operations");
pcb.add_u64_counter(l_osdc_statfs_send, "statfs_send", "Sent FS stats");
pcb.add_u64_counter(l_osdc_statfs_resend, "statfs_resend",
"Resent FS stats");
pcb.add_u64(l_osdc_command_active, "command_active", "Active commands");
pcb.add_u64_counter(l_osdc_command_send, "command_send",
"Sent commands");
pcb.add_u64_counter(l_osdc_command_resend, "command_resend",
"Resent commands");
pcb.add_u64(l_osdc_map_epoch, "map_epoch", "OSD map epoch");
pcb.add_u64_counter(l_osdc_map_full, "map_full",
"Full OSD maps received");
pcb.add_u64_counter(l_osdc_map_inc, "map_inc",
"Incremental OSD maps received");
pcb.add_u64(l_osdc_osd_sessions, "osd_sessions",
"Open sessions"); // open sessions
pcb.add_u64_counter(l_osdc_osd_session_open, "osd_session_open",
"Sessions opened");
pcb.add_u64_counter(l_osdc_osd_session_close, "osd_session_close",
"Sessions closed");
pcb.add_u64(l_osdc_osd_laggy, "osd_laggy", "Laggy OSD sessions");
pcb.add_u64_counter(l_osdc_osdop_omap_wr, "omap_wr",
"OSD OMAP write operations");
pcb.add_u64_counter(l_osdc_osdop_omap_rd, "omap_rd",
"OSD OMAP read operations");
pcb.add_u64_counter(l_osdc_osdop_omap_del, "omap_del",
"OSD OMAP delete operations");
logger = pcb.create_perf_counters();
cct->get_perfcounters_collection()->add(logger);
}
m_request_state_hook = new RequestStateHook(this);
auto admin_socket = cct->get_admin_socket();
int ret = admin_socket->register_command("objecter_requests",
m_request_state_hook,
"show in-progress osd requests");
/* Don't warn on EEXIST, happens if multiple ceph clients
* are instantiated from one process */
if (ret < 0 && ret != -EEXIST) {
lderr(cct) << "error registering admin socket command: "
<< cpp_strerror(ret) << dendl;
}
update_crush_location();
cct->_conf.add_observer(this);
initialized = true;
}
/*
* ok, cluster interaction can happen
*/
void Objecter::start(const OSDMap* o)
{
shared_lock rl(rwlock);
start_tick();
if (o) {
osdmap->deepish_copy_from(*o);
prune_pg_mapping(osdmap->get_pools());
} else if (osdmap->get_epoch() == 0) {
_maybe_request_map();
}
}
void Objecter::shutdown()
{
ceph_assert(initialized);
unique_lock wl(rwlock);
initialized = false;
wl.unlock();
cct->_conf.remove_observer(this);
wl.lock();
while (!osd_sessions.empty()) {
auto p = osd_sessions.begin();
close_session(p->second);
}
while(!check_latest_map_lingers.empty()) {
auto i = check_latest_map_lingers.begin();
i->second->put();
check_latest_map_lingers.erase(i->first);
}
while(!check_latest_map_ops.empty()) {
auto i = check_latest_map_ops.begin();
i->second->put();
check_latest_map_ops.erase(i->first);
}
while(!check_latest_map_commands.empty()) {
auto i = check_latest_map_commands.begin();
i->second->put();
check_latest_map_commands.erase(i->first);
}
while(!poolstat_ops.empty()) {
auto i = poolstat_ops.begin();
delete i->second;
poolstat_ops.erase(i->first);
}
while(!statfs_ops.empty()) {
auto i = statfs_ops.begin();
delete i->second;
statfs_ops.erase(i->first);
}
while(!pool_ops.empty()) {
auto i = pool_ops.begin();
delete i->second;
pool_ops.erase(i->first);
}
ldout(cct, 20) << __func__ << " clearing up homeless session..." << dendl;
while(!homeless_session->linger_ops.empty()) {
auto i = homeless_session->linger_ops.begin();
ldout(cct, 10) << " linger_op " << i->first << dendl;
LingerOp *lop = i->second;
{
std::unique_lock swl(homeless_session->lock);
_session_linger_op_remove(homeless_session, lop);
}
linger_ops.erase(lop->linger_id);
linger_ops_set.erase(lop);
lop->put();
}
while(!homeless_session->ops.empty()) {
auto i = homeless_session->ops.begin();
ldout(cct, 10) << " op " << i->first << dendl;
auto op = i->second;
{
std::unique_lock swl(homeless_session->lock);
_session_op_remove(homeless_session, op);
}
op->put();
}
while(!homeless_session->command_ops.empty()) {
auto i = homeless_session->command_ops.begin();
ldout(cct, 10) << " command_op " << i->first << dendl;
auto cop = i->second;
{
std::unique_lock swl(homeless_session->lock);
_session_command_op_remove(homeless_session, cop);
}
cop->put();
}
if (tick_event) {
if (timer.cancel_event(tick_event)) {
ldout(cct, 10) << " successfully canceled tick" << dendl;
}
tick_event = 0;
}
if (logger) {
cct->get_perfcounters_collection()->remove(logger);
delete logger;
logger = NULL;
}
// Let go of Objecter write lock so timer thread can shutdown
wl.unlock();
// Outside of lock to avoid cycle WRT calls to RequestStateHook
// This is safe because we guarantee no concurrent calls to
// shutdown() with the ::initialized check at start.
if (m_request_state_hook) {
auto admin_socket = cct->get_admin_socket();
admin_socket->unregister_commands(m_request_state_hook);
delete m_request_state_hook;
m_request_state_hook = NULL;
}
}
void Objecter::_send_linger(LingerOp *info,
ceph::shunique_lock<ceph::shared_mutex>& sul)
{
ceph_assert(sul.owns_lock() && sul.mutex() == &rwlock);
fu2::unique_function<Op::OpSig> oncommit;
osdc_opvec opv;
std::shared_lock watchl(info->watch_lock);
cb::list *poutbl = nullptr;
if (info->registered && info->is_watch) {
ldout(cct, 15) << "send_linger " << info->linger_id << " reconnect"
<< dendl;
opv.push_back(OSDOp());
opv.back().op.op = CEPH_OSD_OP_WATCH;
opv.back().op.watch.cookie = info->get_cookie();
opv.back().op.watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
opv.back().op.watch.gen = ++info->register_gen;
oncommit = CB_Linger_Reconnect(this, info);
} else {
ldout(cct, 15) << "send_linger " << info->linger_id << " register"
<< dendl;
opv = info->ops;
// TODO Augment ca::Completion with an equivalent of
// target so we can handle these cases better.
auto c = std::make_unique<CB_Linger_Commit>(this, info);
if (!info->is_watch) {
info->notify_id = 0;
poutbl = &c->outbl;
}
oncommit = [c = std::move(c)](bs::error_code ec) mutable {
std::move(*c)(ec);
};
}
watchl.unlock();
auto o = new Op(info->target.base_oid, info->target.base_oloc,
std::move(opv), info->target.flags | CEPH_OSD_FLAG_READ,
std::move(oncommit), info->pobjver);
o->outbl = poutbl;
o->snapid = info->snap;
o->snapc = info->snapc;
o->mtime = info->mtime;
o->target = info->target;
o->tid = ++last_tid;
// do not resend this; we will send a new op to reregister
o->should_resend = false;
o->ctx_budgeted = true;
if (info->register_tid) {
// repeat send. cancel old registration op, if any.
std::unique_lock sl(info->session->lock);
if (info->session->ops.count(info->register_tid)) {
auto o = info->session->ops[info->register_tid];
_op_cancel_map_check(o);
_cancel_linger_op(o);
}
sl.unlock();
}
_op_submit_with_budget(o, sul, &info->register_tid, &info->ctx_budget);
logger->inc(l_osdc_linger_send);
}
void Objecter::_linger_commit(LingerOp *info, bs::error_code ec,
cb::list& outbl)
{
std::unique_lock wl(info->watch_lock);
ldout(cct, 10) << "_linger_commit " << info->linger_id << dendl;
if (info->on_reg_commit) {
asio::defer(service.get_executor(),
asio::append(std::move(info->on_reg_commit),
ec, cb::list{}));
}
if (ec && info->on_notify_finish) {
asio::defer(service.get_executor(),
asio::append(std::move(info->on_notify_finish),
ec, cb::list{}));
}
// only tell the user the first time we do this
info->registered = true;
info->pobjver = NULL;
if (!info->is_watch) {
// make note of the notify_id
auto p = outbl.cbegin();
try {
decode(info->notify_id, p);
ldout(cct, 10) << "_linger_commit notify_id=" << info->notify_id
<< dendl;
}
catch (cb::error& e) {
}
}
}
class CB_DoWatchError {
Objecter *objecter;
boost::intrusive_ptr<Objecter::LingerOp> info;
bs::error_code ec;
public:
CB_DoWatchError(Objecter *o, Objecter::LingerOp *i,
bs::error_code ec)
: objecter(o), info(i), ec(ec) {
info->_queued_async();
}
void operator()() {
std::unique_lock wl(objecter->rwlock);
bool canceled = info->canceled;
wl.unlock();
if (!canceled) {
info->handle(ec, 0, info->get_cookie(), 0, {});
}
info->finished_async();
}
};
bs::error_code Objecter::_normalize_watch_error(bs::error_code ec)
{
// translate ENOENT -> ENOTCONN so that a delete->disconnection
// notification and a failure to reconnect because we raced with
// the delete appear the same to the user.
if (ec == bs::errc::no_such_file_or_directory)
ec = bs::error_code(ENOTCONN, osd_category());
return ec;
}
void Objecter::_linger_reconnect(LingerOp *info, bs::error_code ec)
{
ldout(cct, 10) << __func__ << " " << info->linger_id << " = " << ec
<< " (last_error " << info->last_error << ")" << dendl;
std::unique_lock wl(info->watch_lock);
if (ec) {
ec = _normalize_watch_error(ec);
if (!info->last_error) {
if (info->handle) {
asio::defer(finish_strand, CB_DoWatchError(this, info, ec));
}
}
}
info->last_error = ec;
}
void Objecter::_send_linger_ping(LingerOp *info)
{
// rwlock is locked unique
// info->session->lock is locked
if (cct->_conf->objecter_inject_no_watch_ping) {
ldout(cct, 10) << __func__ << " " << info->linger_id << " SKIPPING"
<< dendl;
return;
}
if (osdmap->test_flag(CEPH_OSDMAP_PAUSERD)) {
ldout(cct, 10) << __func__ << " PAUSERD" << dendl;
return;
}
ceph::coarse_mono_time now = ceph::coarse_mono_clock::now();
ldout(cct, 10) << __func__ << " " << info->linger_id << " now " << now
<< dendl;
osdc_opvec opv(1);
opv[0].op.op = CEPH_OSD_OP_WATCH;
opv[0].op.watch.cookie = info->get_cookie();
opv[0].op.watch.op = CEPH_OSD_WATCH_OP_PING;
opv[0].op.watch.gen = info->register_gen;
Op *o = new Op(info->target.base_oid, info->target.base_oloc,
std::move(opv), info->target.flags | CEPH_OSD_FLAG_READ,
fu2::unique_function<Op::OpSig>{CB_Linger_Ping(this, info, now)},
nullptr, nullptr);
o->target = info->target;
o->should_resend = false;
_send_op_account(o);
o->tid = ++last_tid;
_session_op_assign(info->session, o);
_send_op(o);
info->ping_tid = o->tid;
logger->inc(l_osdc_linger_ping);
}
void Objecter::_linger_ping(LingerOp *info, bs::error_code ec, ceph::coarse_mono_time sent,
uint32_t register_gen)
{
std::unique_lock l(info->watch_lock);
ldout(cct, 10) << __func__ << " " << info->linger_id
<< " sent " << sent << " gen " << register_gen << " = " << ec
<< " (last_error " << info->last_error
<< " register_gen " << info->register_gen << ")" << dendl;
if (info->register_gen == register_gen) {
if (!ec) {
info->watch_valid_thru = sent;
} else if (ec && !info->last_error) {
ec = _normalize_watch_error(ec);
info->last_error = ec;
if (info->handle) {
asio::defer(finish_strand, CB_DoWatchError(this, info, ec));
}
}
} else {
ldout(cct, 20) << " ignoring old gen" << dendl;
}
}
tl::expected<ceph::timespan,
bs::error_code> Objecter::linger_check(LingerOp *info)
{
std::shared_lock l(info->watch_lock);
ceph::coarse_mono_time stamp = info->watch_valid_thru;
if (!info->watch_pending_async.empty())
stamp = std::min(info->watch_valid_thru, info->watch_pending_async.front());
auto age = ceph::coarse_mono_clock::now() - stamp;
ldout(cct, 10) << __func__ << " " << info->linger_id
<< " err " << info->last_error
<< " age " << age << dendl;
if (info->last_error)
return tl::unexpected(info->last_error);
// return a safe upper bound (we are truncating to ms)
return age;
}
void Objecter::linger_cancel(LingerOp *info)
{
unique_lock wl(rwlock);
_linger_cancel(info);
info->put();
}
void Objecter::_linger_cancel(LingerOp *info)
{
// rwlock is locked unique
ldout(cct, 20) << __func__ << " linger_id=" << info->linger_id << dendl;
if (!info->canceled) {
OSDSession *s = info->session;
std::unique_lock sl(s->lock);
_session_linger_op_remove(s, info);
sl.unlock();
linger_ops.erase(info->linger_id);
linger_ops_set.erase(info);
ceph_assert(linger_ops.size() == linger_ops_set.size());
info->canceled = true;
info->put();
logger->dec(l_osdc_linger_active);
}
}
Objecter::LingerOp *Objecter::linger_register(const object_t& oid,
const object_locator_t& oloc,
int flags)
{
unique_lock l(rwlock);
// Acquire linger ID
auto info = new LingerOp(this, ++max_linger_id);
info->target.base_oid = oid;
info->target.base_oloc = oloc;
if (info->target.base_oloc.key == oid)
info->target.base_oloc.key.clear();
info->target.flags = flags;
info->watch_valid_thru = ceph::coarse_mono_clock::now();
ldout(cct, 10) << __func__ << " info " << info
<< " linger_id " << info->linger_id
<< " cookie " << info->get_cookie()
<< dendl;
linger_ops[info->linger_id] = info;
linger_ops_set.insert(info);
ceph_assert(linger_ops.size() == linger_ops_set.size());
info->get(); // for the caller
return info;
}
ceph_tid_t Objecter::linger_watch(LingerOp *info,
ObjectOperation& op,
const SnapContext& snapc,
real_time mtime,
cb::list& inbl,
decltype(info->on_reg_commit)&& oncommit,
version_t *objver)
{
info->is_watch = true;
info->snapc = snapc;
info->mtime = mtime;
info->target.flags |= CEPH_OSD_FLAG_WRITE;
info->ops = op.ops;
info->inbl = inbl;
info->pobjver = objver;
info->on_reg_commit = std::move(oncommit);
info->ctx_budget = take_linger_budget(info);
shunique_lock sul(rwlock, ceph::acquire_unique);
_linger_submit(info, sul);
logger->inc(l_osdc_linger_active);
op.clear();
return info->linger_id;
}
ceph_tid_t Objecter::linger_notify(LingerOp *info,
ObjectOperation& op,
snapid_t snap, cb::list& inbl,
decltype(LingerOp::on_reg_commit)&& onfinish,
version_t *objver)
{
info->snap = snap;
info->target.flags |= CEPH_OSD_FLAG_READ;
info->ops = op.ops;
info->inbl = inbl;
info->pobjver = objver;
info->on_reg_commit = std::move(onfinish);
info->ctx_budget = take_linger_budget(info);
shunique_lock sul(rwlock, ceph::acquire_unique);
_linger_submit(info, sul);
logger->inc(l_osdc_linger_active);
op.clear();
return info->linger_id;
}
void Objecter::_linger_submit(LingerOp *info,
ceph::shunique_lock<ceph::shared_mutex>& sul)
{
ceph_assert(sul.owns_lock() && sul.mutex() == &rwlock);
ceph_assert(info->linger_id);
ceph_assert(info->ctx_budget != -1); // caller needs to have taken budget already!
// Populate Op::target
OSDSession *s = NULL;
int r = _calc_target(&info->target, nullptr);
switch (r) {
case RECALC_OP_TARGET_POOL_EIO:
_check_linger_pool_eio(info);
return;
}
// Create LingerOp<->OSDSession relation
r = _get_session(info->target.osd, &s, sul);
ceph_assert(r == 0);
unique_lock sl(s->lock);
_session_linger_op_assign(s, info);
sl.unlock();
put_session(s);
_send_linger(info, sul);
}
struct CB_DoWatchNotify {
Objecter *objecter;
boost::intrusive_ptr<Objecter::LingerOp> info;
boost::intrusive_ptr<MWatchNotify> msg;
CB_DoWatchNotify(Objecter *o, Objecter::LingerOp *i, MWatchNotify *m)
: objecter(o), info(i), msg(m) {
info->_queued_async();
}
void operator()() {
objecter->_do_watch_notify(std::move(info), std::move(msg));
}
};
void Objecter::handle_watch_notify(MWatchNotify *m)
{
shared_lock l(rwlock);
if (!initialized) {
return;
}
LingerOp *info = reinterpret_cast<LingerOp*>(m->cookie);
if (linger_ops_set.count(info) == 0) {
ldout(cct, 7) << __func__ << " cookie " << m->cookie << " dne" << dendl;
return;
}
std::unique_lock wl(info->watch_lock);
if (m->opcode == CEPH_WATCH_EVENT_DISCONNECT) {
if (!info->last_error) {
info->last_error = bs::error_code(ENOTCONN, osd_category());
if (info->handle) {
asio::defer(finish_strand, CB_DoWatchError(this, info,
info->last_error));
}
}
} else if (!info->is_watch) {
// we have CEPH_WATCH_EVENT_NOTIFY_COMPLETE; we can do this inline
// since we know the only user (librados) is safe to call in
// fast-dispatch context
if (info->notify_id &&
info->notify_id != m->notify_id) {
ldout(cct, 10) << __func__ << " reply notify " << m->notify_id
<< " != " << info->notify_id << ", ignoring" << dendl;
} else if (info->on_notify_finish) {
asio::defer(service.get_executor(),
asio::append(std::move(info->on_notify_finish),
osdcode(m->return_code),
std::move(m->get_data())));
// if we race with reconnect we might get a second notify; only
// notify the caller once!
info->on_notify_finish = nullptr;
}
} else {
asio::defer(finish_strand, CB_DoWatchNotify(this, info, m));
}
}
void Objecter::_do_watch_notify(boost::intrusive_ptr<LingerOp> info,
boost::intrusive_ptr<MWatchNotify> m)
{
ldout(cct, 10) << __func__ << " " << *m << dendl;
shared_lock l(rwlock);
ceph_assert(initialized);
if (info->canceled) {
l.unlock();
goto out;
}
// notify completion?
ceph_assert(info->is_watch);
ceph_assert(info->handle);
ceph_assert(m->opcode != CEPH_WATCH_EVENT_DISCONNECT);
l.unlock();
switch (m->opcode) {
case CEPH_WATCH_EVENT_NOTIFY:
info->handle({}, m->notify_id, m->cookie, m->notifier_gid, std::move(m->bl));
break;
}
out:
info->finished_async();
}
bool Objecter::ms_dispatch(Message *m)
{
ldout(cct, 10) << __func__ << " " << cct << " " << *m << dendl;
switch (m->get_type()) {
// these we exlusively handle
case CEPH_MSG_OSD_OPREPLY:
handle_osd_op_reply(static_cast<MOSDOpReply*>(m));
return true;
case CEPH_MSG_OSD_BACKOFF:
handle_osd_backoff(static_cast<MOSDBackoff*>(m));
return true;
case CEPH_MSG_WATCH_NOTIFY:
handle_watch_notify(static_cast<MWatchNotify*>(m));
m->put();
return true;