-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMDLog.cc
1657 lines (1408 loc) · 51.7 KB
/
MDLog.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 "MDSRank.h"
#include "MDLog.h"
#include "MDCache.h"
#include "LogEvent.h"
#include "MDSContext.h"
#include "osdc/Journaler.h"
#include "mds/JournalPointer.h"
#include "common/entity_name.h"
#include "common/perf_counters.h"
#include "common/Cond.h"
#include "common/ceph_time.h"
#include "events/ESubtreeMap.h"
#include "events/ESegment.h"
#include "events/ELid.h"
#include "common/config.h"
#include "common/errno.h"
#include "include/ceph_assert.h"
#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_mds
#undef dout_prefix
#define dout_prefix *_dout << "mds." << mds->get_nodeid() << ".log "
using namespace std;
MDLog::MDLog(MDSRank* m)
:
mds(m),
replay_thread(this),
recovery_thread(this),
submit_thread(this),
log_trim_counter(DecayCounter(g_conf().get_val<double>("mds_log_trim_decay_rate")))
{
debug_subtrees = g_conf().get_val<bool>("mds_debug_subtrees");
event_large_threshold = g_conf().get_val<uint64_t>("mds_log_event_large_threshold");
events_per_segment = g_conf().get_val<uint64_t>("mds_log_events_per_segment");
pause = g_conf().get_val<bool>("mds_log_pause");
max_segments = g_conf().get_val<uint64_t>("mds_log_max_segments");
max_events = g_conf().get_val<int64_t>("mds_log_max_events");
skip_corrupt_events = g_conf().get_val<bool>("mds_log_skip_corrupt_events");
skip_unbounded_events = g_conf().get_val<bool>("mds_log_skip_unbounded_events");
log_warn_factor = g_conf().get_val<double>("mds_log_warn_factor");
minor_segments_per_major_segment = g_conf().get_val<uint64_t>("mds_log_minor_segments_per_major_segment");
upkeep_thread = std::thread(&MDLog::log_trim_upkeep, this);
}
MDLog::~MDLog()
{
if (journaler) { delete journaler; journaler = 0; }
if (logger) {
g_ceph_context->get_perfcounters_collection()->remove(logger);
delete logger;
logger = 0;
}
}
void MDLog::create_logger()
{
PerfCountersBuilder plb(g_ceph_context, "mds_log", l_mdl_first, l_mdl_last);
plb.add_u64_counter(l_mdl_evadd, "evadd", "Events submitted", "subm",
PerfCountersBuilder::PRIO_INTERESTING);
plb.add_u64(l_mdl_ev, "ev", "Events", "evts",
PerfCountersBuilder::PRIO_INTERESTING);
plb.add_u64(l_mdl_seg, "seg", "Segments", "segs",
PerfCountersBuilder::PRIO_INTERESTING);
plb.set_prio_default(PerfCountersBuilder::PRIO_USEFUL);
plb.add_u64(l_mdl_evlrg, "evlrg", "Large events");
plb.add_u64(l_mdl_evexg, "evexg", "Expiring events");
plb.add_u64(l_mdl_evexd, "evexd", "Current expired events");
plb.add_u64(l_mdl_segexg, "segexg", "Expiring segments");
plb.add_u64(l_mdl_segexd, "segexd", "Current expired segments");
plb.add_u64(l_mdl_segmjr, "segmjr", "Major Segments");
plb.add_u64_counter(l_mdl_replayed, "replayed", "Events replayed",
"repl", PerfCountersBuilder::PRIO_INTERESTING);
plb.add_time_avg(l_mdl_jlat, "jlat", "Journaler flush latency");
plb.add_u64_counter(l_mdl_evex, "evex", "Total expired events");
plb.add_u64_counter(l_mdl_evtrm, "evtrm", "Trimmed events");
plb.add_u64_counter(l_mdl_segadd, "segadd", "Segments added");
plb.add_u64_counter(l_mdl_segex, "segex", "Total expired segments");
plb.add_u64_counter(l_mdl_segtrm, "segtrm", "Trimmed segments");
plb.set_prio_default(PerfCountersBuilder::PRIO_DEBUGONLY);
plb.add_u64(l_mdl_expos, "expos", "Journaler xpire position");
plb.add_u64(l_mdl_wrpos, "wrpos", "Journaler write position");
plb.add_u64(l_mdl_rdpos, "rdpos", "Journaler read position");
// logger
logger = plb.create_perf_counters();
g_ceph_context->get_perfcounters_collection()->add(logger);
}
void MDLog::set_write_iohint(unsigned iohint_flags)
{
journaler->set_write_iohint(iohint_flags);
}
class C_MDL_WriteError : public MDSIOContextBase {
protected:
MDLog *mdlog;
MDSRank *get_mds() override {return mdlog->mds;}
void finish(int r) override {
MDSRank *mds = get_mds();
// assume journal is reliable, so don't choose action based on
// g_conf()->mds_action_on_write_error.
if (r == -CEPHFS_EBLOCKLISTED) {
derr << "we have been blocklisted (fenced), respawning..." << dendl;
mds->respawn();
} else {
derr << "unhandled error " << cpp_strerror(r) << ", shutting down..." << dendl;
// Although it's possible that this could be something transient,
// it's severe and scary, so disable this rank until an administrator
// intervenes.
mds->clog->error() << "Unhandled journal write error on MDS rank " <<
mds->get_nodeid() << ": " << cpp_strerror(r) << ", shutting down.";
mds->damaged();
ceph_abort(); // damaged should never return
}
}
public:
explicit C_MDL_WriteError(MDLog *m) :
MDSIOContextBase(false), mdlog(m) {}
void print(ostream& out) const override {
out << "mdlog_write_error";
}
};
class C_MDL_WriteHead : public MDSIOContextBase {
public:
explicit C_MDL_WriteHead(MDLog* m)
: MDSIOContextBase(true)
, mdlog(m)
{}
void print(ostream& out) const override {
out << "mdlog_write_head";
}
protected:
void finish(int r) override {
mdlog->finish_head_waiters();
}
MDSRank *get_mds() override {return mdlog->mds;}
MDLog *mdlog;
};
void MDLog::finish_head_waiters()
{
ceph_assert(ceph_mutex_is_locked_by_me(mds->mds_lock));
auto&& last_committed = journaler->get_last_committed();
auto& expire_pos = last_committed.expire_pos;
dout(20) << __func__ << " expire_pos=" << std::hex << expire_pos << dendl;
{
auto last = waiting_for_expire.upper_bound(expire_pos);
for (auto it = waiting_for_expire.begin(); it != last; it++) {
finish_contexts(g_ceph_context, it->second);
}
waiting_for_expire.erase(waiting_for_expire.begin(), last);
}
}
void MDLog::write_head(MDSContext *c)
{
ceph_assert(ceph_mutex_is_locked_by_me(mds->mds_lock));
auto&& last_written = journaler->get_last_written();
auto expire_pos = journaler->get_expire_pos();
dout(10) << __func__ << " last_written=" << last_written << " current expire_pos=" << std::hex << expire_pos << dendl;
if (last_written.expire_pos < expire_pos) {
if (c != NULL) {
dout(25) << __func__ << " queueing waiter " << c << dendl;
waiting_for_expire[expire_pos].push_back(c);
}
auto* fin = new C_MDL_WriteHead(this);
journaler->write_head(fin);
} else {
if (c) {
c->complete(0);
}
}
}
uint64_t MDLog::get_read_pos() const
{
return journaler->get_read_pos();
}
uint64_t MDLog::get_write_pos() const
{
return journaler->get_write_pos();
}
uint64_t MDLog::get_safe_pos() const
{
return journaler->get_write_safe_pos();
}
void MDLog::create(MDSContext *c)
{
ceph_assert(ceph_mutex_is_locked_by_me(mds->mds_lock));
dout(5) << "create empty log" << dendl;
C_GatherBuilder gather(g_ceph_context);
// This requires an OnFinisher wrapper because Journaler will call back the completion for write_head inside its own lock
// XXX but should maybe that be handled inside Journaler?
gather.set_finisher(new C_IO_Wrapper(mds, c));
// The inode of the default Journaler we will create
ino = MDS_INO_LOG_OFFSET + mds->get_nodeid();
// Instantiate Journaler and start async write to RADOS
ceph_assert(journaler == NULL);
journaler = new Journaler("mdlog", ino, mds->get_metadata_pool(),
CEPH_FS_ONDISK_MAGIC, mds->objecter, logger,
l_mdl_jlat, mds->finisher);
ceph_assert(journaler->is_readonly());
journaler->set_write_error_handler(new C_MDL_WriteError(this));
journaler->set_writeable();
journaler->create(&mds->mdcache->default_log_layout, g_conf()->mds_journal_format);
journaler->write_head(gather.new_sub());
// Async write JournalPointer to RADOS
JournalPointer jp(mds->get_nodeid(), mds->get_metadata_pool());
jp.front = ino;
jp.back = 0;
jp.save(mds->objecter, gather.new_sub());
gather.activate();
logger->set(l_mdl_expos, journaler->get_expire_pos());
logger->set(l_mdl_wrpos, journaler->get_write_pos());
submit_thread.create("mds-log-submit");
}
void MDLog::open(MDSContext *c)
{
dout(5) << "open discovering log bounds" << dendl;
ceph_assert(!recovery_thread.is_started());
recovery_thread.set_completion(c);
recovery_thread.create("mds-log-recvr");
submit_thread.create("mds-log-submit");
// either append() or replay() will follow.
}
/**
* Final part of reopen() procedure, after recovery_thread
* has done its thing we call append()
*/
class C_ReopenComplete : public MDSInternalContext {
MDLog *mdlog;
MDSContext *on_complete;
public:
C_ReopenComplete(MDLog *mdlog_, MDSContext *on_complete_) : MDSInternalContext(mdlog_->mds), mdlog(mdlog_), on_complete(on_complete_) {}
void finish(int r) override {
mdlog->append();
on_complete->complete(r);
}
};
/**
* Given that open() has been called in the past, go through the journal
* recovery procedure again, potentially reformatting the journal if it
* was in an old format.
*/
void MDLog::reopen(MDSContext *c)
{
dout(5) << "reopen" << dendl;
// Because we will call append() at the completion of this, check that we have already
// read the whole journal.
ceph_assert(journaler != NULL);
ceph_assert(journaler->get_read_pos() == journaler->get_write_pos());
delete journaler;
journaler = NULL;
// recovery_thread was started at some point in the past. Although
// it has called it's completion if we made it back here, it might
// still not have been cleaned up: join it.
recovery_thread.join();
recovery_thread.set_completion(new C_ReopenComplete(this, c));
recovery_thread.create("mds-log-reopen");
}
void MDLog::append()
{
dout(5) << "append positioning at end and marking writeable" << dendl;
journaler->set_read_pos(journaler->get_write_pos());
journaler->set_expire_pos(journaler->get_write_pos());
journaler->set_writeable();
logger->set(l_mdl_expos, journaler->get_write_pos());
}
// -------------------------------------------------
LogSegment* MDLog::_start_new_segment(SegmentBoundary* sb)
{
ceph_assert(ceph_mutex_is_locked_by_me(mds->mds_lock));
auto ls = new LogSegment(event_seq);
segments[event_seq] = ls;
logger->inc(l_mdl_segadd);
logger->set(l_mdl_seg, segments.size());
sb->set_seq(event_seq);
dout(20) << __func__ << ": starting new segment " << *ls << dendl;
// Adjust to next stray dir
if (!mds->is_stopping()) {
mds->mdcache->advance_stray();
}
return ls;
}
void MDLog::_submit_entry(LogEvent *le, MDSLogContextBase* c)
{
dout(20) << __func__ << " " << *le << dendl;
ceph_assert(ceph_mutex_is_locked_by_me(mds->mds_lock));
ceph_assert(ceph_mutex_is_locked_by_me(submit_mutex));
ceph_assert(!mds->is_any_replay());
ceph_assert(!mds_is_shutting_down);
event_seq++;
if (auto sb = dynamic_cast<SegmentBoundary*>(le); sb) {
auto ls = _start_new_segment(sb);
if (sb->is_major_segment_boundary()) {
major_segments.insert(ls->seq);
logger->set(l_mdl_segmjr, major_segments.size());
minor_segments_since_last_major_segment = 0;
} else {
++minor_segments_since_last_major_segment;
}
}
EMetaBlob *metablob = le->get_metablob();
if (metablob) {
for (auto& in : metablob->get_touched_inodes()) {
in->last_journaled = event_seq;
}
}
// let the event register itself in the segment
ceph_assert(!segments.empty());
LogSegment *ls = segments.rbegin()->second;
ls->num_events++;
le->_segment = ls;
le->update_segment();
le->set_stamp(ceph_clock_now());
mdsmap_up_features = mds->mdsmap->get_up_features();
pending_events[ls->seq].push_back(PendingEvent(le, c));
num_events++;
if (logger) {
logger->inc(l_mdl_evadd);
logger->set(l_mdl_ev, num_events);
}
unflushed++;
}
void MDLog::_segment_upkeep()
{
ceph_assert(ceph_mutex_is_locked_by_me(mds->mds_lock));
ceph_assert(ceph_mutex_is_locked_by_me(submit_mutex));
uint64_t period = journaler->get_layout_period();
auto ls = get_current_segment();
// start a new segment?
if (minor_segments_since_last_major_segment > minor_segments_per_major_segment) {
dout(10) << __func__ << ": starting new major segment, current " << *ls << dendl;
auto sle = mds->mdcache->create_subtree_map();
_submit_entry(sle, NULL);
} else if (ls->end/period != ls->offset/period || ls->num_events >= events_per_segment) {
dout(10) << __func__ << ": starting new segment, current " << *ls << dendl;
auto sb = new ESegment();
_submit_entry(sb, nullptr);
} else if (debug_subtrees && ls->num_events > 1) {
// debug: journal this every time to catch subtree replay bugs.
// use a different event id so it doesn't get interpreted as a
// LogSegment boundary on replay.
dout(10) << __func__ << ": creating test subtree map" << dendl;
auto sle = mds->mdcache->create_subtree_map();
sle->set_type(EVENT_SUBTREEMAP_TEST);
_submit_entry(sle, NULL);
}
}
/**
* Invoked on the flush after each entry submitted
*/
class C_MDL_Flushed : public MDSLogContextBase {
protected:
MDLog *mdlog;
MDSRank *get_mds() override {return mdlog->mds;}
Context *wrapped;
void finish(int r) override {
if (wrapped)
wrapped->complete(r);
}
public:
C_MDL_Flushed(MDLog *m, Context *w)
: mdlog(m), wrapped(w) {}
C_MDL_Flushed(MDLog *m, uint64_t wp) : mdlog(m), wrapped(NULL) {
set_write_pos(wp);
}
};
void MDLog::_submit_thread()
{
dout(10) << "_submit_thread start" << dendl;
std::unique_lock locker{submit_mutex};
while (!mds->is_daemon_stopping()) {
if (pause) {
submit_cond.wait(locker);
continue;
}
map<uint64_t,list<PendingEvent> >::iterator it = pending_events.begin();
if (it == pending_events.end()) {
submit_cond.wait(locker);
continue;
}
if (it->second.empty()) {
pending_events.erase(it);
continue;
}
int64_t features = mdsmap_up_features;
PendingEvent data = it->second.front();
it->second.pop_front();
locker.unlock();
if (data.le) {
LogEvent *le = data.le;
LogSegment *ls = le->_segment;
// encode it, with event type
bufferlist bl;
le->encode_with_header(bl, features);
uint64_t write_pos = journaler->get_write_pos();
le->set_start_off(write_pos);
if (dynamic_cast<SegmentBoundary*>(le)) {
ls->offset = write_pos;
}
if (bl.length() >= event_large_threshold.load()) {
dout(5) << "large event detected!" << dendl;
logger->inc(l_mdl_evlrg);
}
dout(5) << "_submit_thread " << write_pos << "~" << bl.length()
<< " : " << *le << dendl;
// journal it.
const uint64_t new_write_pos = journaler->append_entry(bl); // bl is destroyed.
ls->end = new_write_pos;
MDSLogContextBase *fin;
if (data.fin) {
fin = dynamic_cast<MDSLogContextBase*>(data.fin);
ceph_assert(fin);
fin->set_write_pos(new_write_pos);
} else {
fin = new C_MDL_Flushed(this, new_write_pos);
}
journaler->wait_for_flush(fin);
if (data.flush)
journaler->flush();
if (logger)
logger->set(l_mdl_wrpos, ls->end);
delete le;
} else {
if (data.fin) {
Context* fin = dynamic_cast<Context*>(data.fin);
ceph_assert(fin);
C_MDL_Flushed *fin2 = new C_MDL_Flushed(this, fin);
fin2->set_write_pos(journaler->get_write_pos());
journaler->wait_for_flush(fin2);
}
if (data.flush)
journaler->flush();
}
locker.lock();
if (data.flush)
unflushed = 0;
else if (data.le)
unflushed++;
}
}
void MDLog::wait_for_safe(Context* c)
{
submit_mutex.lock();
bool no_pending = true;
if (!pending_events.empty()) {
pending_events.rbegin()->second.push_back(PendingEvent(NULL, c));
no_pending = false;
submit_cond.notify_all();
}
submit_mutex.unlock();
if (no_pending && c)
journaler->wait_for_flush(new C_IO_Wrapper(mds, c));
}
void MDLog::flush()
{
submit_mutex.lock();
bool do_flush = unflushed > 0;
unflushed = 0;
if (!pending_events.empty()) {
pending_events.rbegin()->second.push_back(PendingEvent(NULL, NULL, true));
do_flush = false;
submit_cond.notify_all();
}
submit_mutex.unlock();
if (do_flush)
journaler->flush();
}
void MDLog::kick_submitter()
{
std::lock_guard l(submit_mutex);
submit_cond.notify_all();
}
void MDLog::cap()
{
dout(5) << "mark mds is shutting down" << dendl;
mds_is_shutting_down = true;
}
void MDLog::shutdown()
{
ceph_assert(ceph_mutex_is_locked_by_me(mds->mds_lock));
dout(5) << "shutdown" << dendl;
if (submit_thread.is_started()) {
ceph_assert(mds->is_daemon_stopping());
if (submit_thread.am_self()) {
// Called suicide from the thread: trust it to do no work after
// returning from suicide, and subsequently respect mds->is_daemon_stopping()
// and fall out of its loop.
} else {
mds->mds_lock.unlock();
// Because MDS::stopping is true, it's safe to drop mds_lock: nobody else
// picking it up will do anything with it.
submit_mutex.lock();
submit_cond.notify_all();
submit_mutex.unlock();
mds->mds_lock.lock();
submit_thread.join();
}
}
upkeep_log_trim_shutdown = true;
cond.notify_one();
mds->mds_lock.unlock();
upkeep_thread.join();
mds->mds_lock.lock();
// Replay thread can be stuck inside e.g. Journaler::wait_for_readable,
// so we need to shutdown the journaler first.
if (journaler) {
journaler->shutdown();
}
if (replay_thread.is_started() && !replay_thread.am_self()) {
mds->mds_lock.unlock();
replay_thread.join();
mds->mds_lock.lock();
}
if (recovery_thread.is_started() && !recovery_thread.am_self()) {
mds->mds_lock.unlock();
recovery_thread.join();
mds->mds_lock.lock();
}
}
void MDLog::try_to_commit_open_file_table(uint64_t last_seq)
{
ceph_assert(ceph_mutex_is_locked_by_me(submit_mutex));
if (mds_is_shutting_down) // shutting down the MDS
return;
if (mds->mdcache->open_file_table.is_any_committing())
return;
// when there have dirty items, maybe there has no any new log event
if (mds->mdcache->open_file_table.is_any_dirty() ||
last_seq > mds->mdcache->open_file_table.get_committed_log_seq()) {
submit_mutex.unlock();
mds->mdcache->open_file_table.commit(nullptr, last_seq, CEPH_MSG_PRIO_HIGH);
submit_mutex.lock();
}
}
bool MDLog::is_trim_slow() const {
return (segments.size() > (size_t)(max_segments * log_warn_factor));
}
void MDLog::log_trim_upkeep(void) {
ceph_pthread_setname("mds-log-trim");
dout(10) << dendl;
std::unique_lock mds_lock(mds->mds_lock);
while (!upkeep_log_trim_shutdown.load()) {
if (mds->is_active() || mds->is_stopping()) {
trim();
}
cond.wait_for(mds_lock, g_conf().get_val<std::chrono::milliseconds>("mds_log_trim_upkeep_interval"));
}
dout(10) << __func__ << ": finished" << dendl;
}
void MDLog::trim()
{
int max_ev = max_events;
if (mds->mdcache->is_readonly()) {
dout(10) << "trim, ignoring read-only FS" << dendl;
return;
}
// Clamp max_ev to not be smaller than events per segment
if (max_ev > 0 && (uint64_t)max_ev <= events_per_segment) {
max_ev = events_per_segment + 1;
}
std::unique_lock locker{submit_mutex};
// trim!
dout(10) << "trim "
<< segments.size() << " / " << max_segments << " segments, "
<< num_events << " / " << max_ev << " events"
<< ", " << expiring_segments.size() << " (" << expiring_events << ") expiring"
<< ", " << expired_segments.size() << " (" << expired_events << ") expired"
<< dendl;
if (segments.empty()) {
return;
}
int op_prio = CEPH_MSG_PRIO_LOW +
(CEPH_MSG_PRIO_HIGH - CEPH_MSG_PRIO_LOW) *
expiring_segments.size() / max_segments;
if (op_prio > CEPH_MSG_PRIO_HIGH)
op_prio = CEPH_MSG_PRIO_HIGH;
unsigned new_expiring_segments = 0;
if (pre_segments_size > 0) {
ceph_assert(segments.size() >= pre_segments_size);
}
map<uint64_t,LogSegment*>::iterator p = segments.begin();
auto trim_start = ceph::coarse_mono_clock::now();
std::optional<ceph::coarse_mono_time> trim_end;
auto log_trim_counter_start = log_trim_counter.get();
auto log_trim_threshold = g_conf().get_val<Option::size_t>("mds_log_trim_threshold");
while (p != segments.end()) {
// throttle - break out of trimmming if we've hit the threshold
if (log_trim_counter_start + new_expiring_segments >= log_trim_threshold) {
auto time_spent = std::chrono::duration<double>::zero();
if (trim_end) {
time_spent = std::chrono::duration<double>(*trim_end - trim_start);
}
dout(10) << __func__ << ": breaking out of trim loop - trimmed "
<< new_expiring_segments << " segment(s) in " << time_spent.count()
<< "s" << dendl;
break;
}
unsigned num_remaining_segments = (segments.size() - expired_segments.size() - expiring_segments.size());
dout(10) << __func__ << ": new_expiring_segments=" << new_expiring_segments
<< ", num_remaining_segments=" << num_remaining_segments
<< ", max_segments=" << max_segments << dendl;
if ((num_remaining_segments <= max_segments) &&
(max_ev < 0 || (num_events - expiring_events - expired_events) <= (uint64_t)max_ev)) {
dout(10) << __func__ << ": breaking out of trim loop - segments/events fell below ceiling"
<< " max_segments/max_ev" << dendl;
break;
}
// look at first segment
LogSegment *ls = p->second;
ceph_assert(ls);
++p;
if (pending_events.count(ls->seq) ||
ls->end > safe_pos) {
dout(5) << "trim " << *ls << " is not fully flushed yet: safe "
<< journaler->get_write_safe_pos() << " < end " << ls->end << dendl;
break;
}
if (expiring_segments.count(ls)) {
dout(5) << "trim already expiring " << *ls << dendl;
} else if (expired_segments.count(ls)) {
dout(5) << "trim already expired " << *ls << dendl;
} else {
ceph_assert(expiring_segments.count(ls) == 0);
new_expiring_segments++;
expiring_segments.insert(ls);
expiring_events += ls->num_events;
locker.unlock();
uint64_t last_seq = ls->seq;
try_expire(ls, op_prio);
log_trim_counter.hit();
trim_end = ceph::coarse_mono_clock::now();
locker.lock();
p = segments.lower_bound(last_seq + 1);
}
}
ceph_assert(locker.owns_lock());
try_to_commit_open_file_table(get_last_segment_seq());
_trim_expired_segments(locker);
}
class C_MaybeExpiredSegment : public MDSInternalContext {
MDLog *mdlog;
LogSegment *ls;
int op_prio;
public:
C_MaybeExpiredSegment(MDLog *mdl, LogSegment *s, int p) :
MDSInternalContext(mdl->mds), mdlog(mdl), ls(s), op_prio(p) {}
void finish(int res) override {
dout(10) << __func__ << ": ls=" << *ls << ", r=" << res << dendl;
if (res < 0)
mdlog->mds->handle_write_error(res);
mdlog->_maybe_expired(ls, op_prio);
}
};
/**
* Like MDLog::trim, but instead of trimming to max_segments, trim all but the latest
* segment.
*/
int MDLog::trim_to(SegmentBoundary::seq_t seq)
{
std::unique_lock locker(submit_mutex);
dout(10) << __func__ << ": "
<< seq
<< " " << segments.size()
<< "/" << expiring_segments.size()
<< "/" << expired_segments.size() << dendl;
uint64_t last_seq = seq;
if (last_seq == 0 || !segments.empty()) {
last_seq = get_last_segment_seq();
try_to_commit_open_file_table(last_seq);
}
map<uint64_t,LogSegment*>::iterator p = segments.begin();
while (p != segments.end() &&
p->first < last_seq &&
p->second->end < safe_pos) { // next segment should have been started
LogSegment *ls = p->second;
++p;
// Caller should have flushed journaler before calling this
if (pending_events.count(ls->seq)) {
dout(5) << __func__ << ": " << *ls << " has pending events" << dendl;
locker.unlock();
return -CEPHFS_EAGAIN;
}
if (expiring_segments.count(ls)) {
dout(5) << "trim already expiring " << *ls << dendl;
} else if (expired_segments.count(ls)) {
dout(5) << "trim already expired " << *ls << dendl;
} else {
ceph_assert(expiring_segments.count(ls) == 0);
expiring_segments.insert(ls);
expiring_events += ls->num_events;
locker.unlock();
uint64_t next_seq = ls->seq + 1;
try_expire(ls, CEPH_MSG_PRIO_DEFAULT);
locker.lock();
p = segments.lower_bound(next_seq);
}
}
_trim_expired_segments(locker);
return 0;
}
void MDLog::try_expire(LogSegment *ls, int op_prio)
{
ceph_assert(ceph_mutex_is_locked(mds->mds_lock));
MDSGatherBuilder gather_bld(g_ceph_context);
ls->try_to_expire(mds, gather_bld, op_prio);
if (gather_bld.has_subs()) {
dout(5) << "try_expire expiring " << *ls << dendl;
gather_bld.set_finisher(new C_MaybeExpiredSegment(this, ls, op_prio));
gather_bld.activate();
} else {
dout(10) << "try_expire expired " << *ls << dendl;
submit_mutex.lock();
ceph_assert(expiring_segments.count(ls));
expiring_segments.erase(ls);
expiring_events -= ls->num_events;
_expired(ls);
submit_mutex.unlock();
}
logger->set(l_mdl_segexg, expiring_segments.size());
logger->set(l_mdl_evexg, expiring_events);
}
void MDLog::_maybe_expired(LogSegment *ls, int op_prio)
{
if (mds->mdcache->is_readonly()) {
dout(10) << "_maybe_expired, ignoring read-only FS" << dendl;
return;
}
dout(10) << "_maybe_expired " << *ls << dendl;
try_expire(ls, op_prio);
}
void MDLog::_trim_expired_segments(auto& locker, MDSContext* ctx)
{
ceph_assert(ceph_mutex_is_locked_by_me(submit_mutex));
ceph_assert(locker.owns_lock());
// trim expired segments?
uint64_t end = 0;
for (auto it = segments.begin(); it != segments.end(); ++it) {
auto& [seq, ls] = *it;
dout(20) << __func__ << ": examining " << *ls << dendl;
if (auto msit = major_segments.find(seq); msit != major_segments.end() && end > 0) {
dout(10) << __func__ << ": expiring up to this major segment seq=" << seq << dendl;
uint64_t expire_pos = 0;
for (auto& [seq2, ls2] : segments) {
if (seq <= seq2) {
break;
}
dout(20) << __func__ << ": expiring " << *ls2 << dendl;
expired_events -= ls2->num_events;
expired_segments.erase(ls2);
if (pre_segments_size > 0)
pre_segments_size--;
num_events -= ls2->num_events;
logger->inc(l_mdl_evtrm, ls2->num_events);
logger->inc(l_mdl_segtrm);
expire_pos = ls2->end;
delete ls2;
}
segments.erase(segments.begin(), it);
logger->set(l_mdl_seg, segments.size());
major_segments.erase(major_segments.begin(), msit);
logger->set(l_mdl_segmjr, major_segments.size());
auto jexpire_pos = journaler->get_expire_pos();
if (jexpire_pos < expire_pos) {
journaler->set_expire_pos(expire_pos);
logger->set(l_mdl_expos, expire_pos);
} else {
logger->set(l_mdl_expos, jexpire_pos);
}
}
if (!expired_segments.count(ls)) {
dout(10) << __func__ << " waiting for expiry " << *ls << dendl;
break;
}
end = seq;
dout(10) << __func__ << ": maybe expiring " << *ls << dendl;
}
locker.unlock();
write_head(ctx);
}
void MDLog::_expired(LogSegment *ls)
{
ceph_assert(ceph_mutex_is_locked_by_me(submit_mutex));
dout(5) << "_expired " << *ls << dendl;
if (!mds_is_shutting_down && ls == peek_current_segment()) {
dout(5) << "_expired not expiring current segment, and !mds_is_shutting_down" << dendl;
} else {
// expired.
expired_segments.insert(ls);
expired_events += ls->num_events;
// Trigger all waiters
finish_contexts(g_ceph_context, ls->expiry_waiters);
logger->inc(l_mdl_evex, ls->num_events);
logger->inc(l_mdl_segex);
}
logger->set(l_mdl_ev, num_events);
logger->set(l_mdl_evexd, expired_events);
logger->set(l_mdl_segexd, expired_segments.size());
}
void MDLog::replay(MDSContext *c)
{
ceph_assert(journaler->is_active());
ceph_assert(journaler->is_readonly());
// empty?
if (journaler->get_read_pos() == journaler->get_write_pos()) {
dout(10) << "replay - journal empty, done." << dendl;
mds->mdcache->trim();
if (mds->is_standby_replay())
mds->update_mlogger();
if (c) {
c->complete(0);
}
return;