/
MDSRank.cc
2957 lines (2544 loc) · 85 KB
/
MDSRank.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) 2015 Red Hat
*
* 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 <boost/utility/string_view.hpp>
#include "common/debug.h"
#include "common/errno.h"
#include "messages/MClientRequestForward.h"
#include "messages/MMDSLoadTargets.h"
#include "messages/MMDSMap.h"
#include "messages/MMDSTableRequest.h"
#include "messages/MCommand.h"
#include "messages/MCommandReply.h"
#include "MDSDaemon.h"
#include "MDSMap.h"
#include "SnapClient.h"
#include "SnapServer.h"
#include "MDBalancer.h"
#include "Locker.h"
#include "Server.h"
#include "InoTable.h"
#include "mon/MonClient.h"
#include "common/HeartbeatMap.h"
#include "ScrubStack.h"
#include "MDSRank.h"
#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_mds
#undef dout_prefix
#define dout_prefix *_dout << "mds." << whoami << '.' << incarnation << ' '
MDSRank::MDSRank(
mds_rank_t whoami_,
Mutex &mds_lock_,
LogChannelRef &clog_,
SafeTimer &timer_,
Beacon &beacon_,
MDSMap *& mdsmap_,
Messenger *msgr,
MonClient *monc_,
Context *respawn_hook_,
Context *suicide_hook_)
:
whoami(whoami_), incarnation(0),
mds_lock(mds_lock_), cct(msgr->cct), clog(clog_), timer(timer_),
mdsmap(mdsmap_),
objecter(new Objecter(g_ceph_context, msgr, monc_, nullptr, 0, 0)),
server(NULL), mdcache(NULL), locker(NULL), mdlog(NULL),
balancer(NULL), scrubstack(NULL),
damage_table(whoami_),
inotable(NULL), snapserver(NULL), snapclient(NULL),
sessionmap(this), logger(NULL), mlogger(NULL),
op_tracker(g_ceph_context, g_conf->mds_enable_op_tracker,
g_conf->osd_num_op_tracker_shard),
last_state(MDSMap::STATE_BOOT),
state(MDSMap::STATE_BOOT),
cluster_degraded(false), stopping(false),
purge_queue(g_ceph_context, whoami_,
mdsmap_->get_metadata_pool(), objecter,
new FunctionContext(
[this](int r){
// Purge Queue operates inside mds_lock when we're calling into
// it, and outside when in background, so must handle both cases.
if (mds_lock.is_locked_by_me()) {
damaged();
} else {
damaged_unlocked();
}
}
)
),
progress_thread(this), dispatch_depth(0),
hb(NULL), last_tid(0), osd_epoch_barrier(0), beacon(beacon_),
mds_slow_req_count(0),
last_client_mdsmap_bcast(0),
messenger(msgr), monc(monc_),
respawn_hook(respawn_hook_),
suicide_hook(suicide_hook_),
standby_replaying(false),
starttime(mono_clock::now())
{
hb = g_ceph_context->get_heartbeat_map()->add_worker("MDSRank", pthread_self());
purge_queue.update_op_limit(*mdsmap);
objecter->unset_honor_osdmap_full();
finisher = new Finisher(cct);
mdcache = new MDCache(this, purge_queue);
mdlog = new MDLog(this);
balancer = new MDBalancer(this, messenger, monc);
scrubstack = new ScrubStack(mdcache, finisher);
inotable = new InoTable(this);
snapserver = new SnapServer(this, monc);
snapclient = new SnapClient(this);
server = new Server(this);
locker = new Locker(this, mdcache);
op_tracker.set_complaint_and_threshold(cct->_conf->mds_op_complaint_time,
cct->_conf->mds_op_log_threshold);
op_tracker.set_history_size_and_duration(cct->_conf->mds_op_history_size,
cct->_conf->mds_op_history_duration);
}
MDSRank::~MDSRank()
{
if (hb) {
g_ceph_context->get_heartbeat_map()->remove_worker(hb);
}
if (scrubstack) { delete scrubstack; scrubstack = NULL; }
if (mdcache) { delete mdcache; mdcache = NULL; }
if (mdlog) { delete mdlog; mdlog = NULL; }
if (balancer) { delete balancer; balancer = NULL; }
if (inotable) { delete inotable; inotable = NULL; }
if (snapserver) { delete snapserver; snapserver = NULL; }
if (snapclient) { delete snapclient; snapclient = NULL; }
if (mdsmap) { delete mdsmap; mdsmap = 0; }
if (server) { delete server; server = 0; }
if (locker) { delete locker; locker = 0; }
if (logger) {
g_ceph_context->get_perfcounters_collection()->remove(logger);
delete logger;
logger = 0;
}
if (mlogger) {
g_ceph_context->get_perfcounters_collection()->remove(mlogger);
delete mlogger;
mlogger = 0;
}
delete finisher;
finisher = NULL;
delete suicide_hook;
suicide_hook = NULL;
delete respawn_hook;
respawn_hook = NULL;
delete objecter;
objecter = nullptr;
}
void MDSRankDispatcher::init()
{
objecter->init();
messenger->add_dispatcher_head(objecter);
objecter->start();
update_log_config();
create_logger();
// Expose the OSDMap (already populated during MDS::init) to anyone
// who is interested in it.
handle_osd_map();
progress_thread.create("mds_rank_progr");
purge_queue.init();
finisher->start();
}
void MDSRank::update_targets(utime_t now)
{
// get MonMap's idea of my export_targets
const set<mds_rank_t>& map_targets = mdsmap->get_mds_info(get_nodeid()).export_targets;
dout(20) << "updating export targets, currently " << map_targets.size() << " ranks are targets" << dendl;
bool send = false;
set<mds_rank_t> new_map_targets;
auto it = export_targets.begin();
while (it != export_targets.end()) {
mds_rank_t rank = it->first;
double val = it->second.get(now);
dout(20) << "export target mds." << rank << " value is " << val << " @ " << now << dendl;
if (val <= 0.01) {
dout(15) << "export target mds." << rank << " is no longer an export target" << dendl;
export_targets.erase(it++);
send = true;
continue;
}
if (!map_targets.count(rank)) {
dout(15) << "export target mds." << rank << " not in map's export_targets" << dendl;
send = true;
}
new_map_targets.insert(rank);
it++;
}
if (new_map_targets.size() < map_targets.size()) {
dout(15) << "export target map holds stale targets, sending update" << dendl;
send = true;
}
if (send) {
dout(15) << "updating export_targets, now " << new_map_targets.size() << " ranks are targets" << dendl;
MMDSLoadTargets* m = new MMDSLoadTargets(mds_gid_t(monc->get_global_id()), new_map_targets);
monc->send_mon_message(m);
}
}
void MDSRank::hit_export_target(utime_t now, mds_rank_t rank, double amount)
{
double rate = g_conf->mds_bal_target_decay;
if (amount < 0.0) {
amount = 100.0/g_conf->mds_bal_target_decay; /* a good default for "i am trying to keep this export_target active" */
}
auto em = export_targets.emplace(std::piecewise_construct, std::forward_as_tuple(rank), std::forward_as_tuple(now, DecayRate(rate)));
if (em.second) {
dout(15) << "hit export target (new) " << amount << " @ " << now << dendl;
} else {
dout(15) << "hit export target " << amount << " @ " << now << dendl;
}
em.first->second.hit(now, amount);
}
void MDSRankDispatcher::tick()
{
heartbeat_reset();
if (beacon.is_laggy()) {
dout(5) << "tick bailing out since we seem laggy" << dendl;
return;
}
check_ops_in_flight();
// Wake up thread in case we use to be laggy and have waiting_for_nolaggy
// messages to progress.
progress_thread.signal();
// make sure mds log flushes, trims periodically
mdlog->flush();
if (is_active() || is_stopping()) {
mdcache->trim();
mdcache->trim_client_leases();
mdcache->check_memory_usage();
mdlog->trim(); // NOT during recovery!
}
// log
if (logger) {
logger->set(l_mds_subtrees, mdcache->num_subtrees());
mdcache->log_stat();
}
// ...
if (is_clientreplay() || is_active() || is_stopping()) {
server->find_idle_sessions();
locker->tick();
}
if (is_reconnect())
server->reconnect_tick();
if (is_active()) {
balancer->tick();
mdcache->find_stale_fragment_freeze();
mdcache->migrator->find_stale_export_freeze();
if (snapserver)
snapserver->check_osd_map(false);
}
if (is_active() || is_stopping()) {
update_targets(ceph_clock_now());
}
// shut down?
if (is_stopping()) {
mdlog->trim();
if (mdcache->shutdown_pass()) {
uint64_t pq_progress = 0 ;
uint64_t pq_total = 0;
size_t pq_in_flight = 0;
if (!purge_queue.drain(&pq_progress, &pq_total, &pq_in_flight)) {
dout(7) << "shutdown_pass=true, but still waiting for purge queue"
<< dendl;
// This takes unbounded time, so we must indicate progress
// to the administrator: we do it in a slightly imperfect way
// by sending periodic (tick frequency) clog messages while
// in this state.
clog->info() << "MDS rank " << whoami << " waiting for purge queue ("
<< std::dec << pq_progress << "/" << pq_total << " " << pq_in_flight
<< " files purging" << ")";
} else {
dout(7) << "shutdown_pass=true, finished w/ shutdown, moving to "
"down:stopped" << dendl;
stopping_done();
}
}
else {
dout(7) << "shutdown_pass=false" << dendl;
}
}
// Expose ourselves to Beacon to update health indicators
beacon.notify_health(this);
}
void MDSRankDispatcher::shutdown()
{
// It should never be possible for shutdown to get called twice, because
// anyone picking up mds_lock checks if stopping is true and drops
// out if it is.
assert(stopping == false);
stopping = true;
dout(1) << __func__ << ": shutting down rank " << whoami << dendl;
timer.shutdown();
// MDLog has to shut down before the finisher, because some of its
// threads block on IOs that require finisher to complete.
mdlog->shutdown();
// shut down cache
mdcache->shutdown();
purge_queue.shutdown();
mds_lock.Unlock();
finisher->stop(); // no flushing
mds_lock.Lock();
if (objecter->initialized)
objecter->shutdown();
monc->shutdown();
op_tracker.on_shutdown();
progress_thread.shutdown();
// release mds_lock for finisher/messenger threads (e.g.
// MDSDaemon::ms_handle_reset called from Messenger).
mds_lock.Unlock();
// shut down messenger
messenger->shutdown();
mds_lock.Lock();
// Workaround unclean shutdown: HeartbeatMap will assert if
// worker is not removed (as we do in ~MDS), but ~MDS is not
// always called after suicide.
if (hb) {
g_ceph_context->get_heartbeat_map()->remove_worker(hb);
hb = NULL;
}
}
/**
* Helper for simple callbacks that call a void fn with no args.
*/
class C_MDS_VoidFn : public MDSInternalContext
{
typedef void (MDSRank::*fn_ptr)();
protected:
fn_ptr fn;
public:
C_MDS_VoidFn(MDSRank *mds_, fn_ptr fn_)
: MDSInternalContext(mds_), fn(fn_)
{
assert(mds_);
assert(fn_);
}
void finish(int r) override
{
(mds->*fn)();
}
};
int64_t MDSRank::get_metadata_pool()
{
return mdsmap->get_metadata_pool();
}
MDSTableClient *MDSRank::get_table_client(int t)
{
switch (t) {
case TABLE_ANCHOR: return NULL;
case TABLE_SNAP: return snapclient;
default: ceph_abort();
}
}
MDSTableServer *MDSRank::get_table_server(int t)
{
switch (t) {
case TABLE_ANCHOR: return NULL;
case TABLE_SNAP: return snapserver;
default: ceph_abort();
}
}
void MDSRank::suicide()
{
if (suicide_hook) {
suicide_hook->complete(0);
suicide_hook = NULL;
}
}
void MDSRank::respawn()
{
if (respawn_hook) {
respawn_hook->complete(0);
respawn_hook = NULL;
}
}
void MDSRank::damaged()
{
assert(whoami != MDS_RANK_NONE);
assert(mds_lock.is_locked_by_me());
beacon.set_want_state(mdsmap, MDSMap::STATE_DAMAGED);
monc->flush_log(); // Flush any clog error from before we were called
beacon.notify_health(this); // Include latest status in our swan song
beacon.send_and_wait(g_conf->mds_mon_shutdown_timeout);
// It's okay if we timed out and the mon didn't get our beacon, because
// another daemon (or ourselves after respawn) will eventually take the
// rank and report DAMAGED again when it hits same problem we did.
respawn(); // Respawn into standby in case mon has other work for us
}
void MDSRank::damaged_unlocked()
{
Mutex::Locker l(mds_lock);
damaged();
}
void MDSRank::handle_write_error(int err)
{
if (err == -EBLACKLISTED) {
derr << "we have been blacklisted (fenced), respawning..." << dendl;
respawn();
return;
}
if (g_conf->mds_action_on_write_error >= 2) {
derr << "unhandled write error " << cpp_strerror(err) << ", suicide..." << dendl;
respawn();
} else if (g_conf->mds_action_on_write_error == 1) {
derr << "unhandled write error " << cpp_strerror(err) << ", force readonly..." << dendl;
mdcache->force_readonly();
} else {
// ignore;
derr << "unhandled write error " << cpp_strerror(err) << ", ignore..." << dendl;
}
}
void *MDSRank::ProgressThread::entry()
{
Mutex::Locker l(mds->mds_lock);
while (true) {
while (!mds->stopping &&
mds->finished_queue.empty() &&
(mds->waiting_for_nolaggy.empty() || mds->beacon.is_laggy())) {
cond.Wait(mds->mds_lock);
}
if (mds->stopping) {
break;
}
mds->_advance_queues();
}
return NULL;
}
void MDSRank::ProgressThread::shutdown()
{
assert(mds->mds_lock.is_locked_by_me());
assert(mds->stopping);
if (am_self()) {
// Stopping is set, we will fall out of our main loop naturally
} else {
// Kick the thread to notice mds->stopping, and join it
cond.Signal();
mds->mds_lock.Unlock();
if (is_started())
join();
mds->mds_lock.Lock();
}
}
bool MDSRankDispatcher::ms_dispatch(Message *m)
{
bool ret;
inc_dispatch_depth();
ret = _dispatch(m, true);
dec_dispatch_depth();
return ret;
}
/* If this function returns true, it recognizes the message and has taken the
* reference. If it returns false, it has done neither. */
bool MDSRank::_dispatch(Message *m, bool new_msg)
{
if (is_stale_message(m)) {
m->put();
return true;
}
if (beacon.is_laggy()) {
dout(10) << " laggy, deferring " << *m << dendl;
waiting_for_nolaggy.push_back(m);
} else if (new_msg && !waiting_for_nolaggy.empty()) {
dout(10) << " there are deferred messages, deferring " << *m << dendl;
waiting_for_nolaggy.push_back(m);
} else {
if (!handle_deferrable_message(m)) {
dout(0) << "unrecognized message " << *m << dendl;
return false;
}
heartbeat_reset();
}
if (dispatch_depth > 1)
return true;
// finish any triggered contexts
_advance_queues();
if (beacon.is_laggy()) {
// We've gone laggy during dispatch, don't do any
// more housekeeping
return true;
}
// done with all client replayed requests?
if (is_clientreplay() &&
mdcache->is_open() &&
replay_queue.empty() &&
beacon.get_want_state() == MDSMap::STATE_CLIENTREPLAY) {
int num_requests = mdcache->get_num_client_requests();
dout(10) << " still have " << num_requests << " active replay requests" << dendl;
if (num_requests == 0)
clientreplay_done();
}
// hack: thrash exports
static utime_t start;
utime_t now = ceph_clock_now();
if (start == utime_t())
start = now;
/*double el = now - start;
if (el > 30.0 &&
el < 60.0)*/
for (int i=0; i<g_conf->mds_thrash_exports; i++) {
set<mds_rank_t> s;
if (!is_active()) break;
mdsmap->get_mds_set(s, MDSMap::STATE_ACTIVE);
if (s.size() < 2 || CInode::count() < 10)
break; // need peers for this to work.
if (mdcache->migrator->get_num_exporting() > g_conf->mds_thrash_exports * 5 ||
mdcache->migrator->get_export_queue_size() > g_conf->mds_thrash_exports * 10)
break;
dout(7) << "mds thrashing exports pass " << (i+1) << "/" << g_conf->mds_thrash_exports << dendl;
// pick a random dir inode
CInode *in = mdcache->hack_pick_random_inode();
list<CDir*> ls;
in->get_dirfrags(ls);
if (!ls.empty()) { // must be an open dir.
list<CDir*>::iterator p = ls.begin();
int n = rand() % ls.size();
while (n--)
++p;
CDir *dir = *p;
if (!dir->get_parent_dir()) continue; // must be linked.
if (!dir->is_auth()) continue; // must be auth.
mds_rank_t dest;
do {
int k = rand() % s.size();
set<mds_rank_t>::iterator p = s.begin();
while (k--) ++p;
dest = *p;
} while (dest == whoami);
mdcache->migrator->export_dir_nicely(dir,dest);
}
}
// hack: thrash fragments
for (int i=0; i<g_conf->mds_thrash_fragments; i++) {
if (!is_active()) break;
if (mdcache->get_num_fragmenting_dirs() > 5 * g_conf->mds_thrash_fragments) break;
dout(7) << "mds thrashing fragments pass " << (i+1) << "/" << g_conf->mds_thrash_fragments << dendl;
// pick a random dir inode
CInode *in = mdcache->hack_pick_random_inode();
list<CDir*> ls;
in->get_dirfrags(ls);
if (ls.empty()) continue; // must be an open dir.
CDir *dir = ls.front();
if (!dir->get_parent_dir()) continue; // must be linked.
if (!dir->is_auth()) continue; // must be auth.
frag_t fg = dir->get_frag();
if (mdsmap->allows_dirfrags()) {
if ((fg == frag_t() || (rand() % (1 << fg.bits()) == 0))) {
mdcache->split_dir(dir, 1);
} else {
balancer->queue_merge(dir);
}
}
}
// hack: force hash root?
/*
if (false &&
mdcache->get_root() &&
mdcache->get_root()->dir &&
!(mdcache->get_root()->dir->is_hashed() ||
mdcache->get_root()->dir->is_hashing())) {
dout(0) << "hashing root" << dendl;
mdcache->migrator->hash_dir(mdcache->get_root()->dir);
}
*/
update_mlogger();
return true;
}
void MDSRank::update_mlogger()
{
if (mlogger) {
mlogger->set(l_mdm_ino, CInode::count());
mlogger->set(l_mdm_dir, CDir::count());
mlogger->set(l_mdm_dn, CDentry::count());
mlogger->set(l_mdm_cap, Capability::count());
mlogger->set(l_mdm_inoa, CInode::increments());
mlogger->set(l_mdm_inos, CInode::decrements());
mlogger->set(l_mdm_dira, CDir::increments());
mlogger->set(l_mdm_dirs, CDir::decrements());
mlogger->set(l_mdm_dna, CDentry::increments());
mlogger->set(l_mdm_dns, CDentry::decrements());
mlogger->set(l_mdm_capa, Capability::increments());
mlogger->set(l_mdm_caps, Capability::decrements());
mlogger->set(l_mdm_buf, buffer::get_total_alloc());
}
}
/*
* lower priority messages we defer if we seem laggy
*/
bool MDSRank::handle_deferrable_message(Message *m)
{
int port = m->get_type() & 0xff00;
switch (port) {
case MDS_PORT_CACHE:
ALLOW_MESSAGES_FROM(CEPH_ENTITY_TYPE_MDS);
mdcache->dispatch(m);
break;
case MDS_PORT_MIGRATOR:
ALLOW_MESSAGES_FROM(CEPH_ENTITY_TYPE_MDS);
mdcache->migrator->dispatch(m);
break;
default:
switch (m->get_type()) {
// SERVER
case CEPH_MSG_CLIENT_SESSION:
case CEPH_MSG_CLIENT_RECONNECT:
ALLOW_MESSAGES_FROM(CEPH_ENTITY_TYPE_CLIENT);
// fall-thru
case CEPH_MSG_CLIENT_REQUEST:
server->dispatch(m);
break;
case MSG_MDS_SLAVE_REQUEST:
ALLOW_MESSAGES_FROM(CEPH_ENTITY_TYPE_MDS);
server->dispatch(m);
break;
case MSG_MDS_HEARTBEAT:
ALLOW_MESSAGES_FROM(CEPH_ENTITY_TYPE_MDS);
balancer->proc_message(m);
break;
case MSG_MDS_TABLE_REQUEST:
ALLOW_MESSAGES_FROM(CEPH_ENTITY_TYPE_MDS);
{
MMDSTableRequest *req = static_cast<MMDSTableRequest*>(m);
if (req->op < 0) {
MDSTableClient *client = get_table_client(req->table);
client->handle_request(req);
} else {
MDSTableServer *server = get_table_server(req->table);
server->handle_request(req);
}
}
break;
case MSG_MDS_LOCK:
case MSG_MDS_INODEFILECAPS:
ALLOW_MESSAGES_FROM(CEPH_ENTITY_TYPE_MDS);
locker->dispatch(m);
break;
case CEPH_MSG_CLIENT_CAPS:
case CEPH_MSG_CLIENT_CAPRELEASE:
case CEPH_MSG_CLIENT_LEASE:
ALLOW_MESSAGES_FROM(CEPH_ENTITY_TYPE_CLIENT);
locker->dispatch(m);
break;
default:
return false;
}
}
return true;
}
/**
* Advance finished_queue and waiting_for_nolaggy.
*
* Usually drain both queues, but may not drain waiting_for_nolaggy
* if beacon is currently laggy.
*/
void MDSRank::_advance_queues()
{
assert(mds_lock.is_locked_by_me());
while (!finished_queue.empty()) {
dout(7) << "mds has " << finished_queue.size() << " queued contexts" << dendl;
dout(10) << finished_queue << dendl;
list<MDSInternalContextBase*> ls;
ls.swap(finished_queue);
while (!ls.empty()) {
dout(10) << " finish " << ls.front() << dendl;
ls.front()->complete(0);
ls.pop_front();
heartbeat_reset();
}
}
while (!waiting_for_nolaggy.empty()) {
// stop if we're laggy now!
if (beacon.is_laggy())
break;
Message *old = waiting_for_nolaggy.front();
waiting_for_nolaggy.pop_front();
if (is_stale_message(old)) {
old->put();
} else {
dout(7) << " processing laggy deferred " << *old << dendl;
if (!handle_deferrable_message(old)) {
dout(0) << "unrecognized message " << *old << dendl;
old->put();
}
}
heartbeat_reset();
}
}
/**
* Call this when you take mds_lock, or periodically if you're going to
* hold the lock for a long time (e.g. iterating over clients/inodes)
*/
void MDSRank::heartbeat_reset()
{
// Any thread might jump into mds_lock and call us immediately
// after a call to suicide() completes, in which case MDSRank::hb
// has been freed and we are a no-op.
if (!hb) {
assert(stopping);
return;
}
// NB not enabling suicide grace, because the mon takes care of killing us
// (by blacklisting us) when we fail to send beacons, and it's simpler to
// only have one way of dying.
g_ceph_context->get_heartbeat_map()->reset_timeout(hb, g_conf->mds_beacon_grace, 0);
}
bool MDSRank::is_stale_message(Message *m) const
{
// from bad mds?
if (m->get_source().is_mds()) {
mds_rank_t from = mds_rank_t(m->get_source().num());
if (!mdsmap->have_inst(from) ||
mdsmap->get_inst(from) != m->get_source_inst() ||
mdsmap->is_down(from)) {
// bogus mds?
if (m->get_type() == CEPH_MSG_MDS_MAP) {
dout(5) << "got " << *m << " from old/bad/imposter mds " << m->get_source()
<< ", but it's an mdsmap, looking at it" << dendl;
} else if (m->get_type() == MSG_MDS_CACHEEXPIRE &&
mdsmap->get_inst(from) == m->get_source_inst()) {
dout(5) << "got " << *m << " from down mds " << m->get_source()
<< ", but it's a cache_expire, looking at it" << dendl;
} else {
dout(5) << "got " << *m << " from down/old/bad/imposter mds " << m->get_source()
<< ", dropping" << dendl;
return true;
}
}
}
return false;
}
Session *MDSRank::get_session(Message *m)
{
Session *session = static_cast<Session *>(m->get_connection()->get_priv());
if (session) {
session->put(); // do not carry ref
dout(20) << "get_session have " << session << " " << session->info.inst
<< " state " << session->get_state_name() << dendl;
// Check if we've imported an open session since (new sessions start closed)
if (session->is_closed()) {
Session *imported_session = sessionmap.get_session(session->info.inst.name);
if (imported_session && imported_session != session) {
dout(10) << __func__ << " replacing connection bootstrap session " << session << " with imported session " << imported_session << dendl;
imported_session->info.auth_name = session->info.auth_name;
//assert(session->info.auth_name == imported_session->info.auth_name);
assert(session->info.inst == imported_session->info.inst);
imported_session->connection = session->connection;
// send out any queued messages
while (!session->preopen_out_queue.empty()) {
imported_session->connection->send_message(session->preopen_out_queue.front());
session->preopen_out_queue.pop_front();
}
imported_session->auth_caps = session->auth_caps;
assert(session->get_nref() == 1);
imported_session->connection->set_priv(imported_session->get());
session = imported_session;
}
}
} else {
dout(20) << "get_session dne for " << m->get_source_inst() << dendl;
}
return session;
}
void MDSRank::send_message(Message *m, Connection *c)
{
assert(c);
c->send_message(m);
}
void MDSRank::send_message_mds(Message *m, mds_rank_t mds)
{
if (!mdsmap->is_up(mds)) {
dout(10) << "send_message_mds mds." << mds << " not up, dropping " << *m << dendl;
m->put();
return;
}
// send mdsmap first?
if (mds != whoami && peer_mdsmap_epoch[mds] < mdsmap->get_epoch()) {
messenger->send_message(new MMDSMap(monc->get_fsid(), mdsmap),
mdsmap->get_inst(mds));
peer_mdsmap_epoch[mds] = mdsmap->get_epoch();
}
// send message
messenger->send_message(m, mdsmap->get_inst(mds));
}
void MDSRank::forward_message_mds(Message *m, mds_rank_t mds)
{
assert(mds != whoami);
// client request?
if (m->get_type() == CEPH_MSG_CLIENT_REQUEST &&
(static_cast<MClientRequest*>(m))->get_source().is_client()) {
MClientRequest *creq = static_cast<MClientRequest*>(m);
creq->inc_num_fwd(); // inc forward counter
/*
* don't actually forward if non-idempotent!
* client has to do it. although the MDS will ignore duplicate requests,
* the affected metadata may migrate, in which case the new authority
* won't have the metareq_id in the completed request map.
*/
// NEW: always make the client resend!
bool client_must_resend = true; //!creq->can_forward();
// tell the client where it should go
messenger->send_message(new MClientRequestForward(creq->get_tid(), mds, creq->get_num_fwd(),
client_must_resend),
creq->get_source_inst());
if (client_must_resend) {
m->put();
return;
}
}
// these are the only types of messages we should be 'forwarding'; they
// explicitly encode their source mds, which gets clobbered when we resend
// them here.
assert(m->get_type() == MSG_MDS_DIRUPDATE ||
m->get_type() == MSG_MDS_EXPORTDIRDISCOVER);
// send mdsmap first?
if (peer_mdsmap_epoch[mds] < mdsmap->get_epoch()) {
messenger->send_message(new MMDSMap(monc->get_fsid(), mdsmap),
mdsmap->get_inst(mds));
peer_mdsmap_epoch[mds] = mdsmap->get_epoch();
}
messenger->send_message(m, mdsmap->get_inst(mds));
}
void MDSRank::send_message_client_counted(Message *m, client_t client)
{
Session *session = sessionmap.get_session(entity_name_t::CLIENT(client.v));
if (session) {
send_message_client_counted(m, session);
} else {
dout(10) << "send_message_client_counted no session for client." << client << " " << *m << dendl;
}
}
void MDSRank::send_message_client_counted(Message *m, Connection *connection)
{
Session *session = static_cast<Session *>(connection->get_priv());
if (session) {
session->put(); // do not carry ref
send_message_client_counted(m, session);
} else {
dout(10) << "send_message_client_counted has no session for " << m->get_source_inst() << dendl;
// another Connection took over the Session
}
}
void MDSRank::send_message_client_counted(Message *m, Session *session)
{
version_t seq = session->inc_push_seq();
dout(10) << "send_message_client_counted " << session->info.inst.name << " seq "
<< seq << " " << *m << dendl;
if (session->connection) {
session->connection->send_message(m);
} else {
session->preopen_out_queue.push_back(m);
}
}
void MDSRank::send_message_client(Message *m, Session *session)
{
dout(10) << "send_message_client " << session->info.inst << " " << *m << dendl;
if (session->connection) {
session->connection->send_message(m);
} else {
session->preopen_out_queue.push_back(m);
}
}
/**
* This is used whenever a RADOS operation has been cancelled
* or a RADOS client has been blacklisted, to cause the MDS and