1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2015 Red Hat
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include <string_view>
17 #include "common/debug.h"
18 #include "common/errno.h"
20 #include "messages/MClientRequestForward.h"
21 #include "messages/MMDSLoadTargets.h"
22 #include "messages/MMDSTableRequest.h"
24 #include "MDSDaemon.h"
26 #include "SnapClient.h"
27 #include "SnapServer.h"
28 #include "MDBalancer.h"
32 #include "mon/MonClient.h"
33 #include "common/HeartbeatMap.h"
34 #include "ScrubStack.h"
39 #define dout_context g_ceph_context
40 #define dout_subsys ceph_subsys_mds
42 #define dout_prefix *_dout << "mds." << whoami << '.' << incarnation << ' '
44 class C_Flush_Journal
: public MDSInternalContext
{
46 C_Flush_Journal(MDCache
*mdcache
, MDLog
*mdlog
, MDSRank
*mds
,
47 std::ostream
*ss
, Context
*on_finish
)
48 : MDSInternalContext(mds
),
49 mdcache(mdcache
), mdlog(mdlog
), ss(ss
), on_finish(on_finish
),
50 whoami(mds
->whoami
), incarnation(mds
->incarnation
) {
54 assert(mds
->mds_lock
.is_locked());
56 dout(20) << __func__
<< dendl
;
58 if (mdcache
->is_readonly()) {
59 dout(5) << __func__
<< ": read-only FS" << dendl
;
64 if (!mds
->is_active()) {
65 dout(5) << __func__
<< ": MDS not active, no-op" << dendl
;
76 dout(20) << __func__
<< dendl
;
78 // I need to seal off the current segment, and then mark all
79 // previous segments for expiry
80 mdlog
->start_new_segment();
82 Context
*ctx
= new FunctionContext([this](int r
) {
83 handle_flush_mdlog(r
);
86 // Flush initially so that all the segments older than our new one
87 // will be elegible for expiry
89 mdlog
->wait_for_safe(new MDSInternalContextWrapper(mds
, ctx
));
92 void handle_flush_mdlog(int r
) {
93 dout(20) << __func__
<< ": r=" << r
<< dendl
;
96 *ss
<< "Error " << r
<< " (" << cpp_strerror(r
) << ") while flushing journal";
105 dout(20) << __func__
<< dendl
;
107 Context
*ctx
= new FunctionContext([this](int r
) {
108 handle_clear_mdlog(r
);
111 // Because we may not be the last wait_for_safe context on MDLog,
112 // and subsequent contexts might wake up in the middle of our
113 // later trim_all and interfere with expiry (by e.g. marking
114 // dirs/dentries dirty on previous log segments), we run a second
115 // wait_for_safe here. See #10368
116 mdlog
->wait_for_safe(new MDSInternalContextWrapper(mds
, ctx
));
119 void handle_clear_mdlog(int r
) {
120 dout(20) << __func__
<< ": r=" << r
<< dendl
;
123 *ss
<< "Error " << r
<< " (" << cpp_strerror(r
) << ") while flushing journal";
132 // Put all the old log segments into expiring or expired state
133 dout(5) << __func__
<< ": beginning segment expiry" << dendl
;
135 int ret
= mdlog
->trim_all();
137 *ss
<< "Error " << ret
<< " (" << cpp_strerror(ret
) << ") while trimming log";
145 void expire_segments() {
146 dout(20) << __func__
<< dendl
;
148 // Attach contexts to wait for all expiring segments to expire
149 MDSGatherBuilder
*expiry_gather
= new MDSGatherBuilder(g_ceph_context
);
151 const auto &expiring_segments
= mdlog
->get_expiring_segments();
152 for (auto p
: expiring_segments
) {
153 p
->wait_for_expiry(expiry_gather
->new_sub());
155 dout(5) << __func__
<< ": waiting for " << expiry_gather
->num_subs_created()
156 << " segments to expire" << dendl
;
158 if (!expiry_gather
->has_subs()) {
160 delete expiry_gather
;
164 Context
*ctx
= new FunctionContext([this](int r
) {
165 handle_expire_segments(r
);
167 expiry_gather
->set_finisher(new MDSInternalContextWrapper(mds
, ctx
));
168 expiry_gather
->activate();
171 void handle_expire_segments(int r
) {
172 dout(20) << __func__
<< ": r=" << r
<< dendl
;
174 ceph_assert(r
== 0); // MDLog is not allowed to raise errors via
179 void trim_segments() {
180 dout(20) << __func__
<< dendl
;
182 Context
*ctx
= new C_OnFinisher(new FunctionContext([this](int _
) {
183 std::lock_guard
locker(mds
->mds_lock
);
184 trim_expired_segments();
189 void trim_expired_segments() {
190 dout(5) << __func__
<< ": expiry complete, expire_pos/trim_pos is now "
191 << std::hex
<< mdlog
->get_journaler()->get_expire_pos() << "/"
192 << mdlog
->get_journaler()->get_trimmed_pos() << dendl
;
194 // Now everyone I'm interested in is expired
195 mdlog
->trim_expired_segments();
197 dout(5) << __func__
<< ": trim complete, expire_pos/trim_pos is now "
198 << std::hex
<< mdlog
->get_journaler()->get_expire_pos() << "/"
199 << mdlog
->get_journaler()->get_trimmed_pos() << dendl
;
201 write_journal_head();
204 void write_journal_head() {
205 dout(20) << __func__
<< dendl
;
207 Context
*ctx
= new FunctionContext([this](int r
) {
208 std::lock_guard
locker(mds
->mds_lock
);
209 handle_write_head(r
);
211 // Flush the journal header so that readers will start from after
212 // the flushed region
213 mdlog
->get_journaler()->write_head(ctx
);
216 void handle_write_head(int r
) {
218 *ss
<< "Error " << r
<< " (" << cpp_strerror(r
) << ") while writing header";
220 dout(5) << __func__
<< ": write_head complete, all done!" << dendl
;
226 void finish(int r
) override
{
227 dout(20) << __func__
<< ": r=" << r
<< dendl
;
228 on_finish
->complete(r
);
241 class C_Drop_Cache
: public MDSInternalContext
{
243 C_Drop_Cache(Server
*server
, MDCache
*mdcache
, MDLog
*mdlog
,
244 MDSRank
*mds
, uint64_t recall_timeout
,
245 Formatter
*f
, Context
*on_finish
)
246 : MDSInternalContext(mds
),
247 server(server
), mdcache(mdcache
), mdlog(mdlog
),
248 recall_timeout(recall_timeout
), recall_start(mono_clock::now()),
249 f(f
), on_finish(on_finish
),
250 whoami(mds
->whoami
), incarnation(mds
->incarnation
) {
254 // not really a hard requirement here, but lets ensure this in
255 // case we change the logic here.
256 assert(mds
->mds_lock
.is_locked());
258 dout(20) << __func__
<< dendl
;
259 f
->open_object_section("result");
260 recall_client_state();
264 // context which completes itself (with -ETIMEDOUT) after a specified
265 // timeout or when explicitly completed, whichever comes first. Note
266 // that the context does not detroy itself after completion -- it
267 // needs to be explicitly freed.
268 class C_ContextTimeout
: public MDSInternalContext
{
270 C_ContextTimeout(MDSRank
*mds
, uint64_t timeout
, Context
*on_finish
)
271 : MDSInternalContext(mds
),
273 lock("mds::context::timeout", false, true),
274 on_finish(on_finish
) {
276 ~C_ContextTimeout() {
277 ceph_assert(timer_task
== nullptr);
285 timer_task
= new FunctionContext([this](int _
) {
286 timer_task
= nullptr;
287 complete(-ETIMEDOUT
);
289 mds
->timer
.add_event_after(timeout
, timer_task
);
292 void finish(int r
) override
{
293 Context
*ctx
= nullptr;
295 std::lock_guard
locker(lock
);
296 std::swap(on_finish
, ctx
);
298 if (ctx
!= nullptr) {
302 void complete(int r
) override
{
303 if (timer_task
!= nullptr) {
304 mds
->timer
.cancel_event(timer_task
);
312 Context
*on_finish
= nullptr;
313 Context
*timer_task
= nullptr;
317 auto [throttled
, count
] = mdcache
->trim(UINT64_MAX
);
319 << (throttled
? " (throttled)" : "")
320 << " trimmed " << count
<< " caps" << dendl
;
321 dentries_trimmed
+= count
;
322 return std::make_pair(throttled
, count
);
325 void recall_client_state() {
326 dout(20) << __func__
<< dendl
;
327 auto now
= mono_clock::now();
328 auto duration
= std::chrono::duration
<double>(now
-recall_start
).count();
330 MDSGatherBuilder
*gather
= new MDSGatherBuilder(g_ceph_context
);
331 auto [throttled
, count
] = server
->recall_client_state(gather
, Server::RecallFlags::STEADY
);
333 << (throttled
? " (throttled)" : "")
334 << " recalled " << count
<< " caps" << dendl
;
336 caps_recalled
+= count
;
337 if ((throttled
|| count
> 0) && (recall_timeout
== 0 || duration
< recall_timeout
)) {
338 C_ContextTimeout
*ctx
= new C_ContextTimeout(
339 mds
, 1, new FunctionContext([this](int r
) {
340 recall_client_state();
343 gather
->set_finisher(new MDSInternalContextWrapper(mds
, ctx
));
345 mdlog
->flush(); /* use down-time to incrementally flush log */
346 do_trim(); /* use down-time to incrementally trim cache */
348 if (!gather
->has_subs()) {
350 return handle_recall_client_state(0);
351 } else if (recall_timeout
> 0 && duration
> recall_timeout
) {
352 gather
->set_finisher(new C_MDSInternalNoop
);
354 return handle_recall_client_state(-ETIMEDOUT
);
356 uint64_t remaining
= (recall_timeout
== 0 ? 0 : recall_timeout
-duration
);
357 C_ContextTimeout
*ctx
= new C_ContextTimeout(
358 mds
, remaining
, new FunctionContext([this](int r
) {
359 handle_recall_client_state(r
);
363 gather
->set_finisher(new MDSInternalContextWrapper(mds
, ctx
));
369 void handle_recall_client_state(int r
) {
370 dout(20) << __func__
<< ": r=" << r
<< dendl
;
372 // client recall section
373 f
->open_object_section("client_recall");
374 f
->dump_int("return_code", r
);
375 f
->dump_string("message", cpp_strerror(r
));
376 f
->dump_int("recalled", caps_recalled
);
379 // we can still continue after recall timeout
383 void flush_journal() {
384 dout(20) << __func__
<< dendl
;
386 Context
*ctx
= new FunctionContext([this](int r
) {
387 handle_flush_journal(r
);
390 C_Flush_Journal
*flush_journal
= new C_Flush_Journal(mdcache
, mdlog
, mds
, &ss
, ctx
);
391 flush_journal
->send();
394 void handle_flush_journal(int r
) {
395 dout(20) << __func__
<< ": r=" << r
<< dendl
;
398 cmd_err(f
, ss
.str());
403 // journal flush section
404 f
->open_object_section("flush_journal");
405 f
->dump_int("return_code", r
);
406 f
->dump_string("message", ss
.str());
413 dout(20) << __func__
<< dendl
;
415 auto [throttled
, count
] = do_trim();
416 if (throttled
&& count
> 0) {
417 auto timer
= new FunctionContext([this](int _
) {
420 mds
->timer
.add_event_after(1.0, timer
);
426 void cache_status() {
427 dout(20) << __func__
<< dendl
;
429 f
->open_object_section("trim_cache");
430 f
->dump_int("trimmed", dentries_trimmed
);
433 // cache status section
434 mdcache
->cache_status(f
);
439 void finish(int r
) override
{
440 dout(20) << __func__
<< ": r=" << r
<< dendl
;
442 auto d
= std::chrono::duration
<double>(mono_clock::now()-recall_start
);
443 f
->dump_float("duration", d
.count());
446 on_finish
->complete(r
);
452 uint64_t recall_timeout
;
453 mono_time recall_start
;
458 std::stringstream ss
;
459 uint64_t caps_recalled
= 0;
460 uint64_t dentries_trimmed
= 0;
466 void cmd_err(Formatter
*f
, std::string_view err
) {
468 f
->open_object_section("result");
469 f
->dump_string("error", err
);
477 LogChannelRef
&clog_
,
480 std::unique_ptr
<MDSMap
>& mdsmap_
,
483 Context
*respawn_hook_
,
484 Context
*suicide_hook_
)
486 whoami(whoami_
), incarnation(0),
487 mds_lock(mds_lock_
), cct(msgr
->cct
), clog(clog_
), timer(timer_
),
489 objecter(new Objecter(g_ceph_context
, msgr
, monc_
, nullptr, 0, 0)),
490 server(NULL
), mdcache(NULL
), locker(NULL
), mdlog(NULL
),
491 balancer(NULL
), scrubstack(NULL
),
492 damage_table(whoami_
),
493 inotable(NULL
), snapserver(NULL
), snapclient(NULL
),
494 sessionmap(this), logger(NULL
), mlogger(NULL
),
495 op_tracker(g_ceph_context
, g_conf()->mds_enable_op_tracker
,
496 g_conf()->osd_num_op_tracker_shard
),
497 last_state(MDSMap::STATE_BOOT
),
498 state(MDSMap::STATE_BOOT
),
499 cluster_degraded(false), stopping(false),
500 purge_queue(g_ceph_context
, whoami_
,
501 mdsmap_
->get_metadata_pool(), objecter
,
504 // Purge Queue operates inside mds_lock when we're calling into
505 // it, and outside when in background, so must handle both cases.
506 if (mds_lock
.is_locked_by_me()) {
507 handle_write_error(r
);
509 std::lock_guard
l(mds_lock
);
510 handle_write_error(r
);
515 progress_thread(this), dispatch_depth(0),
516 hb(NULL
), last_tid(0), osd_epoch_barrier(0), beacon(beacon_
),
517 mds_slow_req_count(0),
518 last_client_mdsmap_bcast(0),
519 messenger(msgr
), monc(monc_
),
520 respawn_hook(respawn_hook_
),
521 suicide_hook(suicide_hook_
),
522 standby_replaying(false),
523 starttime(mono_clock::now())
525 hb
= g_ceph_context
->get_heartbeat_map()->add_worker("MDSRank", pthread_self());
527 purge_queue
.update_op_limit(*mdsmap
);
529 objecter
->unset_honor_osdmap_full();
531 finisher
= new Finisher(cct
);
533 mdcache
= new MDCache(this, purge_queue
);
534 mdlog
= new MDLog(this);
535 balancer
= new MDBalancer(this, messenger
, monc
);
537 scrubstack
= new ScrubStack(mdcache
, finisher
);
539 inotable
= new InoTable(this);
540 snapserver
= new SnapServer(this, monc
);
541 snapclient
= new SnapClient(this);
543 server
= new Server(this);
544 locker
= new Locker(this, mdcache
);
546 op_tracker
.set_complaint_and_threshold(cct
->_conf
->mds_op_complaint_time
,
547 cct
->_conf
->mds_op_log_threshold
);
548 op_tracker
.set_history_size_and_duration(cct
->_conf
->mds_op_history_size
,
549 cct
->_conf
->mds_op_history_duration
);
555 g_ceph_context
->get_heartbeat_map()->remove_worker(hb
);
558 if (scrubstack
) { delete scrubstack
; scrubstack
= NULL
; }
559 if (mdcache
) { delete mdcache
; mdcache
= NULL
; }
560 if (mdlog
) { delete mdlog
; mdlog
= NULL
; }
561 if (balancer
) { delete balancer
; balancer
= NULL
; }
562 if (inotable
) { delete inotable
; inotable
= NULL
; }
563 if (snapserver
) { delete snapserver
; snapserver
= NULL
; }
564 if (snapclient
) { delete snapclient
; snapclient
= NULL
; }
566 if (server
) { delete server
; server
= 0; }
567 if (locker
) { delete locker
; locker
= 0; }
570 g_ceph_context
->get_perfcounters_collection()->remove(logger
);
575 g_ceph_context
->get_perfcounters_collection()->remove(mlogger
);
593 void MDSRankDispatcher::init()
596 messenger
->add_dispatcher_head(objecter
);
603 // Expose the OSDMap (already populated during MDS::init) to anyone
604 // who is interested in it.
607 progress_thread
.create("mds_rank_progr");
614 void MDSRank::update_targets()
616 // get MonMap's idea of my export_targets
617 const set
<mds_rank_t
>& map_targets
= mdsmap
->get_mds_info(get_nodeid()).export_targets
;
619 dout(20) << "updating export targets, currently " << map_targets
.size() << " ranks are targets" << dendl
;
622 set
<mds_rank_t
> new_map_targets
;
624 auto it
= export_targets
.begin();
625 while (it
!= export_targets
.end()) {
626 mds_rank_t rank
= it
->first
;
627 auto &counter
= it
->second
;
628 dout(20) << "export target mds." << rank
<< " is " << counter
<< dendl
;
630 double val
= counter
.get();
632 dout(15) << "export target mds." << rank
<< " is no longer an export target" << dendl
;
633 export_targets
.erase(it
++);
637 if (!map_targets
.count(rank
)) {
638 dout(15) << "export target mds." << rank
<< " not in map's export_targets" << dendl
;
641 new_map_targets
.insert(rank
);
644 if (new_map_targets
.size() < map_targets
.size()) {
645 dout(15) << "export target map holds stale targets, sending update" << dendl
;
650 dout(15) << "updating export_targets, now " << new_map_targets
.size() << " ranks are targets" << dendl
;
651 auto m
= MMDSLoadTargets::create(mds_gid_t(monc
->get_global_id()), new_map_targets
);
652 monc
->send_mon_message(m
.detach());
656 void MDSRank::hit_export_target(mds_rank_t rank
, double amount
)
658 double rate
= g_conf()->mds_bal_target_decay
;
660 amount
= 100.0/g_conf()->mds_bal_target_decay
; /* a good default for "i am trying to keep this export_target active" */
662 auto em
= export_targets
.emplace(std::piecewise_construct
, std::forward_as_tuple(rank
), std::forward_as_tuple(DecayRate(rate
)));
663 auto &counter
= em
.first
->second
;
666 dout(15) << "hit export target (new) is " << counter
<< dendl
;
668 dout(15) << "hit export target is " << counter
<< dendl
;
672 class C_MDS_MonCommand
: public MDSInternalContext
{
676 C_MDS_MonCommand(MDSRank
*m
, std::string_view c
)
677 : MDSInternalContext(m
), cmd(c
) {}
678 void finish(int r
) override
{
679 mds
->_mon_command_finish(r
, cmd
, outs
);
683 void MDSRank::_mon_command_finish(int r
, std::string_view cmd
, std::string_view outs
)
686 dout(0) << __func__
<< ": mon command " << cmd
<< " failed with errno " << r
687 << " (" << outs
<< ")" << dendl
;
689 dout(1) << __func__
<< ": mon command " << cmd
<< " succeed" << dendl
;
693 void MDSRank::set_mdsmap_multimds_snaps_allowed()
695 static bool already_sent
= false;
700 ss
<< "{\"prefix\":\"fs set\", \"fs_name\":\"" << mdsmap
->get_fs_name() << "\", ";
701 ss
<< "\"var\":\"allow_multimds_snaps\", \"val\":\"true\", ";
702 ss
<< "\"confirm\":\"--yes-i-am-really-a-mds\"}";
703 std::vector
<std::string
> cmd
= {ss
.str()};
705 dout(0) << __func__
<< ": sending mon command: " << cmd
[0] << dendl
;
707 C_MDS_MonCommand
*fin
= new C_MDS_MonCommand(this, cmd
[0]);
708 monc
->start_mon_command(cmd
, {}, nullptr, &fin
->outs
, new C_IO_Wrapper(this, fin
));
713 void MDSRank::mark_base_recursively_scrubbed(inodeno_t ino
)
715 if (mdsmap
->get_tableserver() == whoami
)
716 snapserver
->mark_base_recursively_scrubbed(ino
);
719 void MDSRankDispatcher::tick()
723 if (beacon
.is_laggy()) {
724 dout(1) << "skipping upkeep work because connection to Monitors appears laggy" << dendl
;
728 check_ops_in_flight();
730 // Wake up thread in case we use to be laggy and have waiting_for_nolaggy
731 // messages to progress.
732 progress_thread
.signal();
734 // make sure mds log flushes, trims periodically
737 // update average session uptime
738 sessionmap
.update_average_session_age();
740 if (is_active() || is_stopping()) {
741 mdlog
->trim(); // NOT during recovery!
745 if (is_clientreplay() || is_active() || is_stopping()) {
746 mdcache
->trim_client_leases();
748 mdcache
->check_memory_usage();
750 server
->recall_client_state(nullptr, Server::RecallFlags::ENFORCE_MAX
);
752 server
->find_idle_sessions();
753 server
->evict_cap_revoke_non_responders();
759 logger
->set(l_mds_subtrees
, mdcache
->num_subtrees());
764 server
->reconnect_tick();
768 mdcache
->find_stale_fragment_freeze();
769 mdcache
->migrator
->find_stale_export_freeze();
771 if (mdsmap
->get_tableserver() == whoami
) {
772 snapserver
->check_osd_map(false);
773 // Filesystem was created by pre-mimic mds. Allow multi-active mds after
774 // all old snapshots are deleted.
775 if (!mdsmap
->allows_multimds_snaps() &&
776 snapserver
->can_allow_multimds_snaps()) {
777 set_mdsmap_multimds_snaps_allowed();
782 if (is_active() || is_stopping()) {
789 if (mdcache
->shutdown_pass()) {
790 uint64_t pq_progress
= 0 ;
791 uint64_t pq_total
= 0;
792 size_t pq_in_flight
= 0;
793 if (!purge_queue
.drain(&pq_progress
, &pq_total
, &pq_in_flight
)) {
794 dout(7) << "shutdown_pass=true, but still waiting for purge queue"
796 // This takes unbounded time, so we must indicate progress
797 // to the administrator: we do it in a slightly imperfect way
798 // by sending periodic (tick frequency) clog messages while
800 clog
->info() << "MDS rank " << whoami
<< " waiting for purge queue ("
801 << std::dec
<< pq_progress
<< "/" << pq_total
<< " " << pq_in_flight
802 << " files purging" << ")";
804 dout(7) << "shutdown_pass=true, finished w/ shutdown, moving to "
805 "down:stopped" << dendl
;
810 dout(7) << "shutdown_pass=false" << dendl
;
814 // Expose ourselves to Beacon to update health indicators
815 beacon
.notify_health(this);
818 void MDSRankDispatcher::shutdown()
820 // It should never be possible for shutdown to get called twice, because
821 // anyone picking up mds_lock checks if stopping is true and drops
823 ceph_assert(stopping
== false);
826 dout(1) << __func__
<< ": shutting down rank " << whoami
<< dendl
;
830 // MDLog has to shut down before the finisher, because some of its
831 // threads block on IOs that require finisher to complete.
837 purge_queue
.shutdown();
840 finisher
->stop(); // no flushing
843 if (objecter
->initialized
)
844 objecter
->shutdown();
848 op_tracker
.on_shutdown();
850 progress_thread
.shutdown();
852 // release mds_lock for finisher/messenger threads (e.g.
853 // MDSDaemon::ms_handle_reset called from Messenger).
856 // shut down messenger
857 messenger
->shutdown();
861 // Workaround unclean shutdown: HeartbeatMap will assert if
862 // worker is not removed (as we do in ~MDS), but ~MDS is not
863 // always called after suicide.
865 g_ceph_context
->get_heartbeat_map()->remove_worker(hb
);
871 * Helper for simple callbacks that call a void fn with no args.
873 class C_MDS_VoidFn
: public MDSInternalContext
875 typedef void (MDSRank::*fn_ptr
)();
879 C_MDS_VoidFn(MDSRank
*mds_
, fn_ptr fn_
)
880 : MDSInternalContext(mds_
), fn(fn_
)
886 void finish(int r
) override
892 int64_t MDSRank::get_metadata_pool()
894 return mdsmap
->get_metadata_pool();
897 MDSTableClient
*MDSRank::get_table_client(int t
)
900 case TABLE_ANCHOR
: return NULL
;
901 case TABLE_SNAP
: return snapclient
;
902 default: ceph_abort();
906 MDSTableServer
*MDSRank::get_table_server(int t
)
909 case TABLE_ANCHOR
: return NULL
;
910 case TABLE_SNAP
: return snapserver
;
911 default: ceph_abort();
915 void MDSRank::suicide()
918 suicide_hook
->complete(0);
923 void MDSRank::respawn()
926 respawn_hook
->complete(0);
931 void MDSRank::damaged()
933 ceph_assert(whoami
!= MDS_RANK_NONE
);
934 ceph_assert(mds_lock
.is_locked_by_me());
936 beacon
.set_want_state(*mdsmap
, MDSMap::STATE_DAMAGED
);
937 monc
->flush_log(); // Flush any clog error from before we were called
938 beacon
.notify_health(this); // Include latest status in our swan song
939 beacon
.send_and_wait(g_conf()->mds_mon_shutdown_timeout
);
941 // It's okay if we timed out and the mon didn't get our beacon, because
942 // another daemon (or ourselves after respawn) will eventually take the
943 // rank and report DAMAGED again when it hits same problem we did.
945 respawn(); // Respawn into standby in case mon has other work for us
948 void MDSRank::damaged_unlocked()
950 std::lock_guard
l(mds_lock
);
954 void MDSRank::handle_write_error(int err
)
956 if (err
== -EBLACKLISTED
) {
957 derr
<< "we have been blacklisted (fenced), respawning..." << dendl
;
962 if (g_conf()->mds_action_on_write_error
>= 2) {
963 derr
<< "unhandled write error " << cpp_strerror(err
) << ", suicide..." << dendl
;
965 } else if (g_conf()->mds_action_on_write_error
== 1) {
966 derr
<< "unhandled write error " << cpp_strerror(err
) << ", force readonly..." << dendl
;
967 mdcache
->force_readonly();
970 derr
<< "unhandled write error " << cpp_strerror(err
) << ", ignore..." << dendl
;
974 void *MDSRank::ProgressThread::entry()
976 std::lock_guard
l(mds
->mds_lock
);
978 while (!mds
->stopping
&&
979 mds
->finished_queue
.empty() &&
980 (mds
->waiting_for_nolaggy
.empty() || mds
->beacon
.is_laggy())) {
981 cond
.Wait(mds
->mds_lock
);
988 mds
->_advance_queues();
995 void MDSRank::ProgressThread::shutdown()
997 ceph_assert(mds
->mds_lock
.is_locked_by_me());
998 ceph_assert(mds
->stopping
);
1001 // Stopping is set, we will fall out of our main loop naturally
1003 // Kick the thread to notice mds->stopping, and join it
1005 mds
->mds_lock
.Unlock();
1008 mds
->mds_lock
.Lock();
1012 bool MDSRankDispatcher::ms_dispatch(const Message::const_ref
&m
)
1014 if (m
->get_source().is_client()) {
1015 Session
*session
= static_cast<Session
*>(m
->get_connection()->get_priv().get());
1017 session
->last_seen
= Session::clock::now();
1020 inc_dispatch_depth();
1021 bool ret
= _dispatch(m
, true);
1022 dec_dispatch_depth();
1026 bool MDSRank::_dispatch(const Message::const_ref
&m
, bool new_msg
)
1028 if (is_stale_message(m
)) {
1032 if (beacon
.is_laggy()) {
1033 dout(5) << " laggy, deferring " << *m
<< dendl
;
1034 waiting_for_nolaggy
.push_back(m
);
1035 } else if (new_msg
&& !waiting_for_nolaggy
.empty()) {
1036 dout(5) << " there are deferred messages, deferring " << *m
<< dendl
;
1037 waiting_for_nolaggy
.push_back(m
);
1039 if (!handle_deferrable_message(m
)) {
1040 dout(0) << "unrecognized message " << *m
<< dendl
;
1047 if (dispatch_depth
> 1)
1050 // finish any triggered contexts
1053 if (beacon
.is_laggy()) {
1054 // We've gone laggy during dispatch, don't do any
1055 // more housekeeping
1059 // hack: thrash exports
1060 static utime_t start
;
1061 utime_t now
= ceph_clock_now();
1062 if (start
== utime_t())
1064 /*double el = now - start;
1067 for (int i
=0; i
<g_conf()->mds_thrash_exports
; i
++) {
1069 if (!is_active()) break;
1070 mdsmap
->get_mds_set(s
, MDSMap::STATE_ACTIVE
);
1071 if (s
.size() < 2 || CInode::count() < 10)
1072 break; // need peers for this to work.
1073 if (mdcache
->migrator
->get_num_exporting() > g_conf()->mds_thrash_exports
* 5 ||
1074 mdcache
->migrator
->get_export_queue_size() > g_conf()->mds_thrash_exports
* 10)
1077 dout(7) << "mds thrashing exports pass " << (i
+1) << "/" << g_conf()->mds_thrash_exports
<< dendl
;
1079 // pick a random dir inode
1080 CInode
*in
= mdcache
->hack_pick_random_inode();
1083 in
->get_dirfrags(ls
);
1084 if (!ls
.empty()) { // must be an open dir.
1085 list
<CDir
*>::iterator p
= ls
.begin();
1086 int n
= rand() % ls
.size();
1090 if (!dir
->get_parent_dir()) continue; // must be linked.
1091 if (!dir
->is_auth()) continue; // must be auth.
1095 int k
= rand() % s
.size();
1096 set
<mds_rank_t
>::iterator p
= s
.begin();
1099 } while (dest
== whoami
);
1100 mdcache
->migrator
->export_dir_nicely(dir
,dest
);
1103 // hack: thrash fragments
1104 for (int i
=0; i
<g_conf()->mds_thrash_fragments
; i
++) {
1105 if (!is_active()) break;
1106 if (mdcache
->get_num_fragmenting_dirs() > 5 * g_conf()->mds_thrash_fragments
) break;
1107 dout(7) << "mds thrashing fragments pass " << (i
+1) << "/" << g_conf()->mds_thrash_fragments
<< dendl
;
1109 // pick a random dir inode
1110 CInode
*in
= mdcache
->hack_pick_random_inode();
1113 in
->get_dirfrags(ls
);
1114 if (ls
.empty()) continue; // must be an open dir.
1115 CDir
*dir
= ls
.front();
1116 if (!dir
->get_parent_dir()) continue; // must be linked.
1117 if (!dir
->is_auth()) continue; // must be auth.
1118 frag_t fg
= dir
->get_frag();
1119 if ((fg
== frag_t() || (rand() % (1 << fg
.bits()) == 0))) {
1120 mdcache
->split_dir(dir
, 1);
1122 balancer
->queue_merge(dir
);
1126 // hack: force hash root?
1129 mdcache->get_root() &&
1130 mdcache->get_root()->dir &&
1131 !(mdcache->get_root()->dir->is_hashed() ||
1132 mdcache->get_root()->dir->is_hashing())) {
1133 dout(0) << "hashing root" << dendl;
1134 mdcache->migrator->hash_dir(mdcache->get_root()->dir);
1142 void MDSRank::update_mlogger()
1145 mlogger
->set(l_mdm_ino
, CInode::count());
1146 mlogger
->set(l_mdm_dir
, CDir::count());
1147 mlogger
->set(l_mdm_dn
, CDentry::count());
1148 mlogger
->set(l_mdm_cap
, Capability::count());
1149 mlogger
->set(l_mdm_inoa
, CInode::increments());
1150 mlogger
->set(l_mdm_inos
, CInode::decrements());
1151 mlogger
->set(l_mdm_dira
, CDir::increments());
1152 mlogger
->set(l_mdm_dirs
, CDir::decrements());
1153 mlogger
->set(l_mdm_dna
, CDentry::increments());
1154 mlogger
->set(l_mdm_dns
, CDentry::decrements());
1155 mlogger
->set(l_mdm_capa
, Capability::increments());
1156 mlogger
->set(l_mdm_caps
, Capability::decrements());
1161 * lower priority messages we defer if we seem laggy
1163 bool MDSRank::handle_deferrable_message(const Message::const_ref
&m
)
1165 int port
= m
->get_type() & 0xff00;
1168 case MDS_PORT_CACHE
:
1169 ALLOW_MESSAGES_FROM(CEPH_ENTITY_TYPE_MDS
);
1170 mdcache
->dispatch(m
);
1173 case MDS_PORT_MIGRATOR
:
1174 ALLOW_MESSAGES_FROM(CEPH_ENTITY_TYPE_MDS
);
1175 mdcache
->migrator
->dispatch(m
);
1179 switch (m
->get_type()) {
1181 case CEPH_MSG_CLIENT_SESSION
:
1182 case CEPH_MSG_CLIENT_RECONNECT
:
1183 case CEPH_MSG_CLIENT_RECLAIM
:
1184 ALLOW_MESSAGES_FROM(CEPH_ENTITY_TYPE_CLIENT
);
1186 case CEPH_MSG_CLIENT_REQUEST
:
1187 server
->dispatch(m
);
1189 case MSG_MDS_SLAVE_REQUEST
:
1190 ALLOW_MESSAGES_FROM(CEPH_ENTITY_TYPE_MDS
);
1191 server
->dispatch(m
);
1194 case MSG_MDS_HEARTBEAT
:
1195 ALLOW_MESSAGES_FROM(CEPH_ENTITY_TYPE_MDS
);
1196 balancer
->proc_message(m
);
1199 case MSG_MDS_TABLE_REQUEST
:
1200 ALLOW_MESSAGES_FROM(CEPH_ENTITY_TYPE_MDS
);
1202 const MMDSTableRequest::const_ref
&req
= MMDSTableRequest::msgref_cast(m
);
1204 MDSTableClient
*client
= get_table_client(req
->table
);
1205 client
->handle_request(req
);
1207 MDSTableServer
*server
= get_table_server(req
->table
);
1208 server
->handle_request(req
);
1214 case MSG_MDS_INODEFILECAPS
:
1215 ALLOW_MESSAGES_FROM(CEPH_ENTITY_TYPE_MDS
);
1216 locker
->dispatch(m
);
1219 case CEPH_MSG_CLIENT_CAPS
:
1220 case CEPH_MSG_CLIENT_CAPRELEASE
:
1221 case CEPH_MSG_CLIENT_LEASE
:
1222 ALLOW_MESSAGES_FROM(CEPH_ENTITY_TYPE_CLIENT
);
1223 locker
->dispatch(m
);
1235 * Advance finished_queue and waiting_for_nolaggy.
1237 * Usually drain both queues, but may not drain waiting_for_nolaggy
1238 * if beacon is currently laggy.
1240 void MDSRank::_advance_queues()
1242 ceph_assert(mds_lock
.is_locked_by_me());
1244 if (!finished_queue
.empty()) {
1245 dout(7) << "mds has " << finished_queue
.size() << " queued contexts" << dendl
;
1246 while (!finished_queue
.empty()) {
1247 auto fin
= finished_queue
.front();
1248 finished_queue
.pop_front();
1250 dout(10) << " finish " << fin
<< dendl
;
1257 while (!waiting_for_nolaggy
.empty()) {
1258 // stop if we're laggy now!
1259 if (beacon
.is_laggy())
1262 Message::const_ref old
= waiting_for_nolaggy
.front();
1263 waiting_for_nolaggy
.pop_front();
1265 if (!is_stale_message(old
)) {
1266 dout(7) << " processing laggy deferred " << *old
<< dendl
;
1267 if (!handle_deferrable_message(old
)) {
1268 dout(0) << "unrecognized message " << *old
<< dendl
;
1277 * Call this when you take mds_lock, or periodically if you're going to
1278 * hold the lock for a long time (e.g. iterating over clients/inodes)
1280 void MDSRank::heartbeat_reset()
1282 // Any thread might jump into mds_lock and call us immediately
1283 // after a call to suicide() completes, in which case MDSRank::hb
1284 // has been freed and we are a no-op.
1286 ceph_assert(stopping
);
1290 // NB not enabling suicide grace, because the mon takes care of killing us
1291 // (by blacklisting us) when we fail to send beacons, and it's simpler to
1292 // only have one way of dying.
1293 auto grace
= g_conf().get_val
<double>("mds_heartbeat_grace");
1294 g_ceph_context
->get_heartbeat_map()->reset_timeout(hb
, grace
, 0);
1297 bool MDSRank::is_stale_message(const Message::const_ref
&m
) const
1300 if (m
->get_source().is_mds()) {
1301 mds_rank_t from
= mds_rank_t(m
->get_source().num());
1303 if (mdsmap
->is_down(from
)) {
1306 // FIXME: this is a convoluted check. we should be maintaining a nice
1307 // clean map of current ConnectionRefs for current mdses!!!
1308 auto c
= messenger
->connect_to(CEPH_ENTITY_TYPE_MDS
,
1309 mdsmap
->get_addrs(from
));
1310 if (c
!= m
->get_connection()) {
1312 dout(5) << " mds." << from
<< " should be " << c
<< " "
1313 << c
->get_peer_addrs() << " but this message is "
1314 << m
->get_connection() << " " << m
->get_source_addrs()
1320 if (m
->get_type() == CEPH_MSG_MDS_MAP
) {
1321 dout(5) << "got " << *m
<< " from old/bad/imposter mds " << m
->get_source()
1322 << ", but it's an mdsmap, looking at it" << dendl
;
1323 } else if (m
->get_type() == MSG_MDS_CACHEEXPIRE
&&
1324 mdsmap
->get_addrs(from
) == m
->get_source_addrs()) {
1325 dout(5) << "got " << *m
<< " from down mds " << m
->get_source()
1326 << ", but it's a cache_expire, looking at it" << dendl
;
1328 dout(5) << "got " << *m
<< " from down/old/bad/imposter mds " << m
->get_source()
1329 << ", dropping" << dendl
;
1337 Session
*MDSRank::get_session(const Message::const_ref
&m
)
1340 auto session
= static_cast<Session
*>(m
->get_connection()->get_priv().get());
1342 dout(20) << "get_session have " << session
<< " " << session
->info
.inst
1343 << " state " << session
->get_state_name() << dendl
;
1344 // Check if we've imported an open session since (new sessions start closed)
1345 if (session
->is_closed()) {
1346 Session
*imported_session
= sessionmap
.get_session(session
->info
.inst
.name
);
1347 if (imported_session
&& imported_session
!= session
) {
1348 dout(10) << __func__
<< " replacing connection bootstrap session "
1349 << session
<< " with imported session " << imported_session
1351 imported_session
->info
.auth_name
= session
->info
.auth_name
;
1352 //assert(session->info.auth_name == imported_session->info.auth_name);
1353 ceph_assert(session
->info
.inst
== imported_session
->info
.inst
);
1354 imported_session
->set_connection(session
->get_connection().get());
1355 // send out any queued messages
1356 while (!session
->preopen_out_queue
.empty()) {
1357 imported_session
->get_connection()->send_message2(std::move(session
->preopen_out_queue
.front()));
1358 session
->preopen_out_queue
.pop_front();
1360 imported_session
->auth_caps
= session
->auth_caps
;
1361 imported_session
->last_seen
= session
->last_seen
;
1362 ceph_assert(session
->get_nref() == 1);
1363 imported_session
->get_connection()->set_priv(imported_session
->get());
1364 session
= imported_session
;
1368 dout(20) << "get_session dne for " << m
->get_source_inst() << dendl
;
1373 void MDSRank::send_message(const Message::ref
& m
, const ConnectionRef
& c
)
1376 c
->send_message2(m
);
1380 void MDSRank::send_message_mds(const Message::ref
& m
, mds_rank_t mds
)
1382 if (!mdsmap
->is_up(mds
)) {
1383 dout(10) << "send_message_mds mds." << mds
<< " not up, dropping " << *m
<< dendl
;
1387 // send mdsmap first?
1388 if (mds
!= whoami
&& peer_mdsmap_epoch
[mds
] < mdsmap
->get_epoch()) {
1389 auto _m
= MMDSMap::create(monc
->get_fsid(), *mdsmap
);
1390 messenger
->send_to_mds(_m
.detach(), mdsmap
->get_addrs(mds
));
1391 peer_mdsmap_epoch
[mds
] = mdsmap
->get_epoch();
1395 messenger
->send_to_mds(Message::ref(m
).detach(), mdsmap
->get_addrs(mds
));
1398 void MDSRank::forward_message_mds(const MClientRequest::const_ref
& m
, mds_rank_t mds
)
1400 ceph_assert(mds
!= whoami
);
1403 * don't actually forward if non-idempotent!
1404 * client has to do it. although the MDS will ignore duplicate requests,
1405 * the affected metadata may migrate, in which case the new authority
1406 * won't have the metareq_id in the completed request map.
1408 // NEW: always make the client resend!
1409 bool client_must_resend
= true; //!creq->can_forward();
1411 // tell the client where it should go
1412 auto session
= get_session(m
);
1413 auto f
= MClientRequestForward::create(m
->get_tid(), mds
, m
->get_num_fwd()+1, client_must_resend
);
1414 send_message_client(f
, session
);
1417 void MDSRank::send_message_client_counted(const Message::ref
& m
, client_t client
)
1419 Session
*session
= sessionmap
.get_session(entity_name_t::CLIENT(client
.v
));
1421 send_message_client_counted(m
, session
);
1423 dout(10) << "send_message_client_counted no session for client." << client
<< " " << *m
<< dendl
;
1427 void MDSRank::send_message_client_counted(const Message::ref
& m
, const ConnectionRef
& connection
)
1430 auto session
= static_cast<Session
*>(connection
->get_priv().get());
1432 send_message_client_counted(m
, session
);
1434 dout(10) << "send_message_client_counted has no session for " << m
->get_source_inst() << dendl
;
1435 // another Connection took over the Session
1439 void MDSRank::send_message_client_counted(const Message::ref
& m
, Session
* session
)
1441 version_t seq
= session
->inc_push_seq();
1442 dout(10) << "send_message_client_counted " << session
->info
.inst
.name
<< " seq "
1443 << seq
<< " " << *m
<< dendl
;
1444 if (session
->get_connection()) {
1445 session
->get_connection()->send_message2(m
);
1447 session
->preopen_out_queue
.push_back(m
);
1451 void MDSRank::send_message_client(const Message::ref
& m
, Session
* session
)
1453 dout(10) << "send_message_client " << session
->info
.inst
<< " " << *m
<< dendl
;
1454 if (session
->get_connection()) {
1455 session
->get_connection()->send_message2(m
);
1457 session
->preopen_out_queue
.push_back(m
);
1462 * This is used whenever a RADOS operation has been cancelled
1463 * or a RADOS client has been blacklisted, to cause the MDS and
1464 * any clients to wait for this OSD epoch before using any new caps.
1466 * See doc/cephfs/eviction
1468 void MDSRank::set_osd_epoch_barrier(epoch_t e
)
1470 dout(4) << __func__
<< ": epoch=" << e
<< dendl
;
1471 osd_epoch_barrier
= e
;
1474 void MDSRank::retry_dispatch(const Message::const_ref
&m
)
1476 inc_dispatch_depth();
1477 _dispatch(m
, false);
1478 dec_dispatch_depth();
1481 double MDSRank::get_dispatch_queue_max_age(utime_t now
) const
1483 return messenger
->get_dispatch_queue_max_age(now
);
1486 bool MDSRank::is_daemon_stopping() const
1491 void MDSRank::request_state(MDSMap::DaemonState s
)
1493 dout(3) << "request_state " << ceph_mds_state_name(s
) << dendl
;
1494 beacon
.set_want_state(*mdsmap
, s
);
1499 class C_MDS_BootStart
: public MDSInternalContext
{
1500 MDSRank::BootStep nextstep
;
1502 C_MDS_BootStart(MDSRank
*m
, MDSRank::BootStep n
)
1503 : MDSInternalContext(m
), nextstep(n
) {}
1504 void finish(int r
) override
{
1505 mds
->boot_start(nextstep
, r
);
1510 void MDSRank::boot_start(BootStep step
, int r
)
1512 // Handle errors from previous step
1514 if (is_standby_replay() && (r
== -EAGAIN
)) {
1515 dout(0) << "boot_start encountered an error EAGAIN"
1516 << ", respawning since we fell behind journal" << dendl
;
1518 } else if (r
== -EINVAL
|| r
== -ENOENT
) {
1519 // Invalid or absent data, indicates damaged on-disk structures
1520 clog
->error() << "Error loading MDS rank " << whoami
<< ": "
1523 ceph_assert(r
== 0); // Unreachable, damaged() calls respawn()
1524 } else if (r
== -EROFS
) {
1525 dout(0) << "boot error forcing transition to read-only; MDS will try to continue" << dendl
;
1527 // Completely unexpected error, give up and die
1528 dout(0) << "boot_start encountered an error, failing" << dendl
;
1534 ceph_assert(is_starting() || is_any_replay());
1537 case MDS_BOOT_INITIAL
:
1539 mdcache
->init_layouts();
1541 MDSGatherBuilder
gather(g_ceph_context
,
1542 new C_MDS_BootStart(this, MDS_BOOT_OPEN_ROOT
));
1543 dout(2) << "Booting: " << step
<< ": opening inotable" << dendl
;
1544 inotable
->set_rank(whoami
);
1545 inotable
->load(gather
.new_sub());
1547 dout(2) << "Booting: " << step
<< ": opening sessionmap" << dendl
;
1548 sessionmap
.set_rank(whoami
);
1549 sessionmap
.load(gather
.new_sub());
1551 dout(2) << "Booting: " << step
<< ": opening mds log" << dendl
;
1552 mdlog
->open(gather
.new_sub());
1554 if (is_starting()) {
1555 dout(2) << "Booting: " << step
<< ": opening purge queue" << dendl
;
1556 purge_queue
.open(new C_IO_Wrapper(this, gather
.new_sub()));
1557 } else if (!standby_replaying
) {
1558 dout(2) << "Booting: " << step
<< ": opening purge queue (async)" << dendl
;
1559 purge_queue
.open(NULL
);
1560 dout(2) << "Booting: " << step
<< ": loading open file table (async)" << dendl
;
1561 mdcache
->open_file_table
.load(nullptr);
1564 if (mdsmap
->get_tableserver() == whoami
) {
1565 dout(2) << "Booting: " << step
<< ": opening snap table" << dendl
;
1566 snapserver
->set_rank(whoami
);
1567 snapserver
->load(gather
.new_sub());
1573 case MDS_BOOT_OPEN_ROOT
:
1575 dout(2) << "Booting: " << step
<< ": loading/discovering base inodes" << dendl
;
1577 MDSGatherBuilder
gather(g_ceph_context
,
1578 new C_MDS_BootStart(this, MDS_BOOT_PREPARE_LOG
));
1580 if (is_starting()) {
1581 // load mydir frag for the first log segment (creating subtree map)
1582 mdcache
->open_mydir_frag(gather
.new_sub());
1584 mdcache
->open_mydir_inode(gather
.new_sub());
1587 mdcache
->create_global_snaprealm();
1589 if (whoami
== mdsmap
->get_root()) { // load root inode off disk if we are auth
1590 mdcache
->open_root_inode(gather
.new_sub());
1591 } else if (is_any_replay()) {
1592 // replay. make up fake root inode to start with
1593 mdcache
->create_root_inode();
1598 case MDS_BOOT_PREPARE_LOG
:
1599 if (is_any_replay()) {
1600 dout(2) << "Booting: " << step
<< ": replaying mds log" << dendl
;
1601 MDSGatherBuilder
gather(g_ceph_context
,
1602 new C_MDS_BootStart(this, MDS_BOOT_REPLAY_DONE
));
1604 if (!standby_replaying
) {
1605 dout(2) << "Booting: " << step
<< ": waiting for purge queue recovered" << dendl
;
1606 purge_queue
.wait_for_recovery(new C_IO_Wrapper(this, gather
.new_sub()));
1609 mdlog
->replay(gather
.new_sub());
1612 dout(2) << "Booting: " << step
<< ": positioning at end of old mds log" << dendl
;
1617 case MDS_BOOT_REPLAY_DONE
:
1618 ceph_assert(is_any_replay());
1620 // Sessiontable and inotable should be in sync after replay, validate
1621 // that they are consistent.
1622 validate_sessions();
1629 void MDSRank::validate_sessions()
1631 ceph_assert(mds_lock
.is_locked_by_me());
1634 // Identify any sessions which have state inconsistent with other,
1635 // after they have been loaded from rados during startup.
1636 // Mitigate bugs like: http://tracker.ceph.com/issues/16842
1637 for (const auto &i
: sessionmap
.get_sessions()) {
1638 Session
*session
= i
.second
;
1639 interval_set
<inodeno_t
> badones
;
1640 if (inotable
->intersects_free(session
->info
.prealloc_inos
, &badones
)) {
1641 clog
->error() << "client " << *session
1642 << "loaded with preallocated inodes that are inconsistent with inotable";
1653 void MDSRank::starting_done()
1655 dout(3) << "starting_done" << dendl
;
1656 ceph_assert(is_starting());
1657 request_state(MDSMap::STATE_ACTIVE
);
1659 mdlog
->start_new_segment();
1661 // sync snaptable cache
1662 snapclient
->sync(new C_MDSInternalNoop
);
1666 void MDSRank::calc_recovery_set()
1668 // initialize gather sets
1670 mdsmap
->get_recovery_mds_set(rs
);
1672 mdcache
->set_recovery_set(rs
);
1674 dout(1) << " recovery set is " << rs
<< dendl
;
1678 void MDSRank::replay_start()
1680 dout(1) << "replay_start" << dendl
;
1682 if (is_standby_replay())
1683 standby_replaying
= true;
1685 calc_recovery_set();
1687 // Check if we need to wait for a newer OSD map before starting
1688 Context
*fin
= new C_IO_Wrapper(this, new C_MDS_BootStart(this, MDS_BOOT_INITIAL
));
1689 bool const ready
= objecter
->wait_for_map(
1690 mdsmap
->get_last_failure_osd_epoch(),
1697 dout(1) << " waiting for osdmap " << mdsmap
->get_last_failure_osd_epoch()
1698 << " (which blacklists prior instance)" << dendl
;
1703 class MDSRank::C_MDS_StandbyReplayRestartFinish
: public MDSIOContext
{
1704 uint64_t old_read_pos
;
1706 C_MDS_StandbyReplayRestartFinish(MDSRank
*mds_
, uint64_t old_read_pos_
) :
1707 MDSIOContext(mds_
), old_read_pos(old_read_pos_
) {}
1708 void finish(int r
) override
{
1709 mds
->_standby_replay_restart_finish(r
, old_read_pos
);
1711 void print(ostream
& out
) const override
{
1712 out
<< "standby_replay_restart";
1716 void MDSRank::_standby_replay_restart_finish(int r
, uint64_t old_read_pos
)
1718 if (old_read_pos
< mdlog
->get_journaler()->get_trimmed_pos()) {
1719 dout(0) << "standby MDS fell behind active MDS journal's expire_pos, restarting" << dendl
;
1720 respawn(); /* we're too far back, and this is easier than
1721 trying to reset everything in the cache, etc */
1723 mdlog
->standby_trim_segments();
1724 boot_start(MDS_BOOT_PREPARE_LOG
, r
);
1728 class MDSRank::C_MDS_StandbyReplayRestart
: public MDSInternalContext
{
1730 explicit C_MDS_StandbyReplayRestart(MDSRank
*m
) : MDSInternalContext(m
) {}
1731 void finish(int r
) override
{
1733 mds
->standby_replay_restart();
1737 void MDSRank::standby_replay_restart()
1739 if (standby_replaying
) {
1740 /* Go around for another pass of replaying in standby */
1741 dout(5) << "Restarting replay as standby-replay" << dendl
;
1742 mdlog
->get_journaler()->reread_head_and_probe(
1743 new C_MDS_StandbyReplayRestartFinish(
1745 mdlog
->get_journaler()->get_read_pos()));
1747 /* We are transitioning out of standby: wait for OSD map update
1748 before making final pass */
1749 dout(1) << "standby_replay_restart (final takeover pass)" << dendl
;
1750 Context
*fin
= new C_IO_Wrapper(this, new C_MDS_StandbyReplayRestart(this));
1751 bool ready
= objecter
->wait_for_map(mdsmap
->get_last_failure_osd_epoch(), fin
);
1754 mdlog
->get_journaler()->reread_head_and_probe(
1755 new C_MDS_StandbyReplayRestartFinish(
1757 mdlog
->get_journaler()->get_read_pos()));
1759 dout(1) << " opening purge_queue (async)" << dendl
;
1760 purge_queue
.open(NULL
);
1761 dout(1) << " opening open_file_table (async)" << dendl
;
1762 mdcache
->open_file_table
.load(nullptr);
1764 dout(1) << " waiting for osdmap " << mdsmap
->get_last_failure_osd_epoch()
1765 << " (which blacklists prior instance)" << dendl
;
1770 void MDSRank::replay_done()
1772 if (!standby_replaying
) {
1773 dout(1) << "Finished replaying journal" << dendl
;
1775 dout(5) << "Finished replaying journal as standby-replay" << dendl
;
1778 if (is_standby_replay()) {
1779 // The replay was done in standby state, and we are still in that state
1780 ceph_assert(standby_replaying
);
1781 dout(10) << "setting replay timer" << dendl
;
1782 timer
.add_event_after(g_conf()->mds_replay_interval
,
1783 new C_MDS_StandbyReplayRestart(this));
1785 } else if (standby_replaying
) {
1786 // The replay was done in standby state, we have now _left_ that state
1787 dout(10) << " last replay pass was as a standby; making final pass" << dendl
;
1788 standby_replaying
= false;
1789 standby_replay_restart();
1792 // Replay is complete, journal read should be up to date
1793 ceph_assert(mdlog
->get_journaler()->get_read_pos() == mdlog
->get_journaler()->get_write_pos());
1794 ceph_assert(!is_standby_replay());
1796 // Reformat and come back here
1797 if (mdlog
->get_journaler()->get_stream_format() < g_conf()->mds_journal_format
) {
1798 dout(4) << "reformatting journal on standby-replay->replay transition" << dendl
;
1799 mdlog
->reopen(new C_MDS_BootStart(this, MDS_BOOT_REPLAY_DONE
));
1804 dout(1) << "making mds journal writeable" << dendl
;
1805 mdlog
->get_journaler()->set_writeable();
1806 mdlog
->get_journaler()->trim_tail();
1808 if (mdsmap
->get_tableserver() == whoami
&&
1809 snapserver
->upgrade_format()) {
1810 dout(1) << "upgrading snaptable format" << dendl
;
1811 snapserver
->save(new C_MDSInternalNoop
);
1814 if (g_conf()->mds_wipe_sessions
) {
1815 dout(1) << "wiping out client sessions" << dendl
;
1817 sessionmap
.save(new C_MDSInternalNoop
);
1819 if (g_conf()->mds_wipe_ino_prealloc
) {
1820 dout(1) << "wiping out ino prealloc from sessions" << dendl
;
1821 sessionmap
.wipe_ino_prealloc();
1822 sessionmap
.save(new C_MDSInternalNoop
);
1824 if (g_conf()->mds_skip_ino
) {
1825 inodeno_t i
= g_conf()->mds_skip_ino
;
1826 dout(1) << "skipping " << i
<< " inodes" << dendl
;
1827 inotable
->skip_inos(i
);
1828 inotable
->save(new C_MDSInternalNoop
);
1831 if (mdsmap
->get_num_in_mds() == 1 &&
1832 mdsmap
->get_num_failed_mds() == 0) { // just me!
1833 dout(2) << "i am alone, moving to state reconnect" << dendl
;
1834 request_state(MDSMap::STATE_RECONNECT
);
1835 // sync snaptable cache
1836 snapclient
->sync(new C_MDSInternalNoop
);
1838 dout(2) << "i am not alone, moving to state resolve" << dendl
;
1839 request_state(MDSMap::STATE_RESOLVE
);
1843 void MDSRank::reopen_log()
1845 dout(1) << "reopen_log" << dendl
;
1846 mdcache
->rollback_uncommitted_fragments();
1849 void MDSRank::resolve_start()
1851 dout(1) << "resolve_start" << dendl
;
1855 mdcache
->resolve_start(new C_MDS_VoidFn(this, &MDSRank::resolve_done
));
1856 finish_contexts(g_ceph_context
, waiting_for_resolve
);
1859 void MDSRank::resolve_done()
1861 dout(1) << "resolve_done" << dendl
;
1862 request_state(MDSMap::STATE_RECONNECT
);
1863 // sync snaptable cache
1864 snapclient
->sync(new C_MDSInternalNoop
);
1867 void MDSRank::reconnect_start()
1869 dout(1) << "reconnect_start" << dendl
;
1871 if (last_state
== MDSMap::STATE_REPLAY
) {
1875 // Drop any blacklisted clients from the SessionMap before going
1876 // into reconnect, so that we don't wait for them.
1877 objecter
->enable_blacklist_events();
1878 std::set
<entity_addr_t
> blacklist
;
1880 objecter
->with_osdmap([&blacklist
, &epoch
](const OSDMap
& o
) {
1881 o
.get_blacklist(&blacklist
);
1882 epoch
= o
.get_epoch();
1884 auto killed
= server
->apply_blacklist(blacklist
);
1885 dout(4) << "reconnect_start: killed " << killed
<< " blacklisted sessions ("
1886 << blacklist
.size() << " blacklist entries, "
1887 << sessionmap
.get_sessions().size() << ")" << dendl
;
1889 set_osd_epoch_barrier(epoch
);
1892 server
->reconnect_clients(new C_MDS_VoidFn(this, &MDSRank::reconnect_done
));
1893 finish_contexts(g_ceph_context
, waiting_for_reconnect
);
1895 void MDSRank::reconnect_done()
1897 dout(1) << "reconnect_done" << dendl
;
1898 request_state(MDSMap::STATE_REJOIN
); // move to rejoin state
1901 void MDSRank::rejoin_joint_start()
1903 dout(1) << "rejoin_joint_start" << dendl
;
1904 mdcache
->rejoin_send_rejoins();
1906 void MDSRank::rejoin_start()
1908 dout(1) << "rejoin_start" << dendl
;
1909 mdcache
->rejoin_start(new C_MDS_VoidFn(this, &MDSRank::rejoin_done
));
1910 finish_contexts(g_ceph_context
, waiting_for_rejoin
);
1912 void MDSRank::rejoin_done()
1914 dout(1) << "rejoin_done" << dendl
;
1915 mdcache
->show_subtrees();
1916 mdcache
->show_cache();
1918 // funny case: is our cache empty? no subtrees?
1919 if (!mdcache
->is_subtrees()) {
1921 // The root should always have a subtree!
1922 clog
->error() << "No subtrees found for root MDS rank!";
1924 ceph_assert(mdcache
->is_subtrees());
1926 dout(1) << " empty cache, no subtrees, leaving cluster" << dendl
;
1927 request_state(MDSMap::STATE_STOPPED
);
1932 if (replay_queue
.empty() && !server
->get_num_pending_reclaim()) {
1933 request_state(MDSMap::STATE_ACTIVE
);
1935 replaying_requests_done
= replay_queue
.empty();
1936 request_state(MDSMap::STATE_CLIENTREPLAY
);
1940 void MDSRank::clientreplay_start()
1942 dout(1) << "clientreplay_start" << dendl
;
1943 finish_contexts(g_ceph_context
, waiting_for_replay
); // kick waiters
1944 mdcache
->start_files_to_recover();
1948 bool MDSRank::queue_one_replay()
1950 if (!replay_queue
.empty()) {
1951 queue_waiter(replay_queue
.front());
1952 replay_queue
.pop_front();
1955 if (!replaying_requests_done
) {
1956 replaying_requests_done
= true;
1959 maybe_clientreplay_done();
1963 void MDSRank::maybe_clientreplay_done()
1965 if (is_clientreplay() && get_want_state() == MDSMap::STATE_CLIENTREPLAY
) {
1967 // don't go to active if there are session waiting for being reclaimed
1968 if (replaying_requests_done
&& !server
->get_num_pending_reclaim()) {
1969 mdlog
->wait_for_safe(new C_MDS_VoidFn(this, &MDSRank::clientreplay_done
));
1973 dout(1) << " still have " << replay_queue
.size() + (int)!replaying_requests_done
1974 << " requests need to be replayed, " << server
->get_num_pending_reclaim()
1975 << " sessions need to be reclaimed" << dendl
;
1979 void MDSRank::clientreplay_done()
1981 dout(1) << "clientreplay_done" << dendl
;
1982 request_state(MDSMap::STATE_ACTIVE
);
1985 void MDSRank::active_start()
1987 dout(1) << "active_start" << dendl
;
1989 if (last_state
== MDSMap::STATE_CREATING
||
1990 last_state
== MDSMap::STATE_STARTING
) {
1991 mdcache
->open_root();
1994 mdcache
->clean_open_file_lists();
1995 mdcache
->export_remaining_imported_caps();
1996 finish_contexts(g_ceph_context
, waiting_for_replay
); // kick waiters
1997 mdcache
->start_files_to_recover();
1999 mdcache
->reissue_all_caps();
2001 finish_contexts(g_ceph_context
, waiting_for_active
); // kick waiters
2004 void MDSRank::recovery_done(int oldstate
)
2006 dout(1) << "recovery_done -- successful recovery!" << dendl
;
2007 ceph_assert(is_clientreplay() || is_active());
2009 if (oldstate
== MDSMap::STATE_CREATING
)
2012 mdcache
->start_recovered_truncates();
2013 mdcache
->do_file_recover();
2015 // tell connected clients
2016 //bcast_mds_map(); // not anymore, they get this from the monitor
2018 mdcache
->populate_mydir();
2021 void MDSRank::creating_done()
2023 dout(1)<< "creating_done" << dendl
;
2024 request_state(MDSMap::STATE_ACTIVE
);
2025 // sync snaptable cache
2026 snapclient
->sync(new C_MDSInternalNoop
);
2029 void MDSRank::boot_create()
2031 dout(3) << "boot_create" << dendl
;
2033 MDSGatherBuilder
fin(g_ceph_context
, new C_MDS_VoidFn(this, &MDSRank::creating_done
));
2035 mdcache
->init_layouts();
2037 inotable
->set_rank(whoami
);
2038 sessionmap
.set_rank(whoami
);
2040 // start with a fresh journal
2041 dout(10) << "boot_create creating fresh journal" << dendl
;
2042 mdlog
->create(fin
.new_sub());
2044 // open new journal segment, but do not journal subtree map (yet)
2045 mdlog
->prepare_new_segment();
2047 if (whoami
== mdsmap
->get_root()) {
2048 dout(3) << "boot_create creating fresh hierarchy" << dendl
;
2049 mdcache
->create_empty_hierarchy(fin
.get());
2052 dout(3) << "boot_create creating mydir hierarchy" << dendl
;
2053 mdcache
->create_mydir_hierarchy(fin
.get());
2055 dout(3) << "boot_create creating global snaprealm" << dendl
;
2056 mdcache
->create_global_snaprealm();
2058 // fixme: fake out inotable (reset, pretend loaded)
2059 dout(10) << "boot_create creating fresh inotable table" << dendl
;
2061 inotable
->save(fin
.new_sub());
2063 // write empty sessionmap
2064 sessionmap
.save(fin
.new_sub());
2066 // Create empty purge queue
2067 purge_queue
.create(new C_IO_Wrapper(this, fin
.new_sub()));
2069 // initialize tables
2070 if (mdsmap
->get_tableserver() == whoami
) {
2071 dout(10) << "boot_create creating fresh snaptable" << dendl
;
2072 snapserver
->set_rank(whoami
);
2073 snapserver
->reset();
2074 snapserver
->save(fin
.new_sub());
2077 ceph_assert(g_conf()->mds_kill_create_at
!= 1);
2079 // ok now journal it
2080 mdlog
->journal_segment_subtree_map(fin
.new_sub());
2083 // Usually we do this during reconnect, but creation skips that.
2084 objecter
->enable_blacklist_events();
2089 void MDSRank::stopping_start()
2091 dout(2) << "Stopping..." << dendl
;
2093 if (mdsmap
->get_num_in_mds() == 1 && !sessionmap
.empty()) {
2094 std::vector
<Session
*> victims
;
2095 const auto& sessions
= sessionmap
.get_sessions();
2096 for (const auto& p
: sessions
) {
2097 if (!p
.first
.is_client()) {
2101 Session
*s
= p
.second
;
2102 victims
.push_back(s
);
2105 dout(20) << __func__
<< " matched " << victims
.size() << " sessions" << dendl
;
2106 ceph_assert(!victims
.empty());
2108 C_GatherBuilder
gather(g_ceph_context
, new C_MDSInternalNoop
);
2109 for (const auto &s
: victims
) {
2110 std::stringstream ss
;
2111 evict_client(s
->get_client().v
, false,
2112 g_conf()->mds_session_blacklist_on_evict
, ss
, gather
.new_sub());
2117 mdcache
->shutdown_start();
2120 void MDSRank::stopping_done()
2122 dout(2) << "Finished stopping..." << dendl
;
2124 // tell monitor we shut down cleanly.
2125 request_state(MDSMap::STATE_STOPPED
);
2128 void MDSRankDispatcher::handle_mds_map(
2129 const MMDSMap::const_ref
&m
,
2130 const MDSMap
&oldmap
)
2132 // I am only to be passed MDSMaps in which I hold a rank
2133 ceph_assert(whoami
!= MDS_RANK_NONE
);
2135 MDSMap::DaemonState oldstate
= state
;
2136 mds_gid_t mds_gid
= mds_gid_t(monc
->get_global_id());
2137 state
= mdsmap
->get_state_gid(mds_gid
);
2138 if (state
!= oldstate
) {
2139 last_state
= oldstate
;
2140 incarnation
= mdsmap
->get_inc_gid(mds_gid
);
2143 version_t epoch
= m
->get_epoch();
2145 // note source's map version
2146 if (m
->get_source().is_mds() &&
2147 peer_mdsmap_epoch
[mds_rank_t(m
->get_source().num())] < epoch
) {
2148 dout(15) << " peer " << m
->get_source()
2149 << " has mdsmap epoch >= " << epoch
2151 peer_mdsmap_epoch
[mds_rank_t(m
->get_source().num())] = epoch
;
2154 // Validate state transitions while I hold a rank
2155 if (!MDSMap::state_transition_valid(oldstate
, state
)) {
2156 derr
<< "Invalid state transition " << ceph_mds_state_name(oldstate
)
2157 << "->" << ceph_mds_state_name(state
) << dendl
;
2161 if (oldstate
!= state
) {
2162 // update messenger.
2163 if (state
== MDSMap::STATE_STANDBY_REPLAY
) {
2164 dout(1) << "handle_mds_map i am now mds." << mds_gid
<< "." << incarnation
2165 << " replaying mds." << whoami
<< "." << incarnation
<< dendl
;
2166 messenger
->set_myname(entity_name_t::MDS(mds_gid
));
2168 dout(1) << "handle_mds_map i am now mds." << whoami
<< "." << incarnation
<< dendl
;
2169 messenger
->set_myname(entity_name_t::MDS(whoami
));
2173 // tell objecter my incarnation
2174 if (objecter
->get_client_incarnation() != incarnation
)
2175 objecter
->set_client_incarnation(incarnation
);
2177 if (oldmap
.get_min_compat_client() != mdsmap
->get_min_compat_client())
2178 server
->update_required_client_features();
2181 if (g_conf()->mds_dump_cache_on_map
)
2182 mdcache
->dump_cache();
2184 cluster_degraded
= mdsmap
->is_degraded();
2186 // mdsmap and oldmap can be discontinuous. failover might happen in the missing mdsmap.
2187 // the 'restart' set tracks ranks that have restarted since the old mdsmap
2188 set
<mds_rank_t
> restart
;
2189 // replaying mds does not communicate with other ranks
2190 if (state
>= MDSMap::STATE_RESOLVE
) {
2191 // did someone fail?
2193 set
<mds_rank_t
> olddown
, down
;
2194 oldmap
.get_down_mds_set(&olddown
);
2195 mdsmap
->get_down_mds_set(&down
);
2196 for (const auto& r
: down
) {
2197 if (oldmap
.have_inst(r
) && olddown
.count(r
) == 0) {
2198 messenger
->mark_down_addrs(oldmap
.get_addrs(r
));
2199 handle_mds_failure(r
);
2203 // did someone fail?
2204 // did their addr/inst change?
2206 mdsmap
->get_up_mds_set(up
);
2207 for (const auto& r
: up
) {
2208 auto& info
= mdsmap
->get_info(r
);
2209 if (oldmap
.have_inst(r
)) {
2210 auto& oldinfo
= oldmap
.get_info(r
);
2211 if (info
.inc
!= oldinfo
.inc
) {
2212 messenger
->mark_down_addrs(oldinfo
.get_addrs());
2213 if (info
.state
== MDSMap::STATE_REPLAY
||
2214 info
.state
== MDSMap::STATE_RESOLVE
) {
2216 handle_mds_failure(r
);
2218 ceph_assert(info
.state
== MDSMap::STATE_STARTING
||
2219 info
.state
== MDSMap::STATE_ACTIVE
);
2220 // -> stopped (missing) -> starting -> active
2222 mdcache
->migrator
->handle_mds_failure_or_stop(r
);
2223 if (mdsmap
->get_tableserver() == whoami
)
2224 snapserver
->handle_mds_failure_or_stop(r
);
2228 if (info
.state
== MDSMap::STATE_REPLAY
||
2229 info
.state
== MDSMap::STATE_RESOLVE
) {
2230 // -> starting/creating (missing) -> active (missing) -> replay -> resolve
2232 handle_mds_failure(r
);
2234 ceph_assert(info
.state
== MDSMap::STATE_CREATING
||
2235 info
.state
== MDSMap::STATE_STARTING
||
2236 info
.state
== MDSMap::STATE_ACTIVE
);
2243 if (oldstate
!= state
) {
2244 dout(1) << "handle_mds_map state change "
2245 << ceph_mds_state_name(oldstate
) << " --> "
2246 << ceph_mds_state_name(state
) << dendl
;
2247 beacon
.set_want_state(*mdsmap
, state
);
2249 if (oldstate
== MDSMap::STATE_STANDBY_REPLAY
) {
2250 dout(10) << "Monitor activated us! Deactivating replay loop" << dendl
;
2251 assert (state
== MDSMap::STATE_REPLAY
);
2253 // did i just recover?
2254 if ((is_active() || is_clientreplay()) &&
2255 (oldstate
== MDSMap::STATE_CREATING
||
2256 oldstate
== MDSMap::STATE_REJOIN
||
2257 oldstate
== MDSMap::STATE_RECONNECT
))
2258 recovery_done(oldstate
);
2262 } else if (is_any_replay()) {
2264 } else if (is_resolve()) {
2266 } else if (is_reconnect()) {
2268 } else if (is_rejoin()) {
2270 } else if (is_clientreplay()) {
2271 clientreplay_start();
2272 } else if (is_creating()) {
2274 } else if (is_starting()) {
2276 } else if (is_stopping()) {
2277 ceph_assert(oldstate
== MDSMap::STATE_ACTIVE
);
2284 // is someone else newly resolving?
2285 if (state
>= MDSMap::STATE_RESOLVE
) {
2286 // recover snaptable
2287 if (mdsmap
->get_tableserver() == whoami
) {
2288 if (oldstate
< MDSMap::STATE_RESOLVE
) {
2290 mdsmap
->get_mds_set_lower_bound(s
, MDSMap::STATE_RESOLVE
);
2291 snapserver
->finish_recovery(s
);
2293 set
<mds_rank_t
> old_set
, new_set
;
2294 oldmap
.get_mds_set_lower_bound(old_set
, MDSMap::STATE_RESOLVE
);
2295 mdsmap
->get_mds_set_lower_bound(new_set
, MDSMap::STATE_RESOLVE
);
2296 for (const auto& r
: new_set
) {
2299 if (!old_set
.count(r
) || restart
.count(r
)) { // newly so?
2300 snapserver
->handle_mds_recovery(r
);
2306 if ((!oldmap
.is_resolving() || !restart
.empty()) && mdsmap
->is_resolving()) {
2307 set
<mds_rank_t
> resolve
;
2308 mdsmap
->get_mds_set(resolve
, MDSMap::STATE_RESOLVE
);
2309 dout(10) << " resolve set is " << resolve
<< dendl
;
2310 calc_recovery_set();
2311 mdcache
->send_resolves();
2316 // is everybody finally rejoining?
2317 if (state
>= MDSMap::STATE_REJOIN
) {
2319 if (!oldmap
.is_rejoining() && mdsmap
->is_rejoining())
2320 rejoin_joint_start();
2323 if (g_conf()->mds_dump_cache_after_rejoin
&&
2324 oldmap
.is_rejoining() && !mdsmap
->is_rejoining())
2325 mdcache
->dump_cache(); // for DEBUG only
2327 if (oldstate
>= MDSMap::STATE_REJOIN
||
2328 oldstate
== MDSMap::STATE_STARTING
) {
2329 // ACTIVE|CLIENTREPLAY|REJOIN => we can discover from them.
2330 set
<mds_rank_t
> olddis
, dis
;
2331 oldmap
.get_mds_set_lower_bound(olddis
, MDSMap::STATE_REJOIN
);
2332 mdsmap
->get_mds_set_lower_bound(dis
, MDSMap::STATE_REJOIN
);
2333 for (const auto& r
: dis
) {
2336 if (!olddis
.count(r
) || restart
.count(r
)) { // newly so?
2337 mdcache
->kick_discovers(r
);
2338 mdcache
->kick_open_ino_peers(r
);
2344 if (oldmap
.is_degraded() && !cluster_degraded
&& state
>= MDSMap::STATE_ACTIVE
) {
2345 dout(1) << "cluster recovered." << dendl
;
2346 auto it
= waiting_for_active_peer
.find(MDS_RANK_NONE
);
2347 if (it
!= waiting_for_active_peer
.end()) {
2348 queue_waiters(it
->second
);
2349 waiting_for_active_peer
.erase(it
);
2353 // did someone go active?
2354 if (state
>= MDSMap::STATE_CLIENTREPLAY
&&
2355 oldstate
>= MDSMap::STATE_CLIENTREPLAY
) {
2356 set
<mds_rank_t
> oldactive
, active
;
2357 oldmap
.get_mds_set_lower_bound(oldactive
, MDSMap::STATE_CLIENTREPLAY
);
2358 mdsmap
->get_mds_set_lower_bound(active
, MDSMap::STATE_CLIENTREPLAY
);
2359 for (const auto& r
: active
) {
2362 if (!oldactive
.count(r
) || restart
.count(r
)) // newly so?
2363 handle_mds_recovery(r
);
2367 if (is_clientreplay() || is_active() || is_stopping()) {
2369 set
<mds_rank_t
> oldstopped
, stopped
;
2370 oldmap
.get_stopped_mds_set(oldstopped
);
2371 mdsmap
->get_stopped_mds_set(stopped
);
2372 for (const auto& r
: stopped
)
2373 if (oldstopped
.count(r
) == 0) { // newly so?
2374 mdcache
->migrator
->handle_mds_failure_or_stop(r
);
2375 if (mdsmap
->get_tableserver() == whoami
)
2376 snapserver
->handle_mds_failure_or_stop(r
);
2381 map
<epoch_t
,MDSContext::vec
>::iterator p
= waiting_for_mdsmap
.begin();
2382 while (p
!= waiting_for_mdsmap
.end() && p
->first
<= mdsmap
->get_epoch()) {
2385 waiting_for_mdsmap
.erase(p
++);
2391 // Before going active, set OSD epoch barrier to latest (so that
2392 // we don't risk handing out caps to clients with old OSD maps that
2393 // might not include barriers from the previous incarnation of this MDS)
2394 set_osd_epoch_barrier(objecter
->with_osdmap(
2395 std::mem_fn(&OSDMap::get_epoch
)));
2397 /* Now check if we should hint to the OSD that a read may follow */
2398 if (mdsmap
->has_standby_replay(whoami
))
2399 mdlog
->set_write_iohint(0);
2401 mdlog
->set_write_iohint(CEPH_OSD_OP_FLAG_FADVISE_DONTNEED
);
2404 if (oldmap
.get_max_mds() != mdsmap
->get_max_mds()) {
2405 purge_queue
.update_op_limit(*mdsmap
);
2409 void MDSRank::handle_mds_recovery(mds_rank_t who
)
2411 dout(5) << "handle_mds_recovery mds." << who
<< dendl
;
2413 mdcache
->handle_mds_recovery(who
);
2415 queue_waiters(waiting_for_active_peer
[who
]);
2416 waiting_for_active_peer
.erase(who
);
2419 void MDSRank::handle_mds_failure(mds_rank_t who
)
2421 if (who
== whoami
) {
2422 dout(5) << "handle_mds_failure for myself; not doing anything" << dendl
;
2425 dout(5) << "handle_mds_failure mds." << who
<< dendl
;
2427 mdcache
->handle_mds_failure(who
);
2429 if (mdsmap
->get_tableserver() == whoami
)
2430 snapserver
->handle_mds_failure_or_stop(who
);
2432 snapclient
->handle_mds_failure(who
);
2435 bool MDSRankDispatcher::handle_asok_command(std::string_view command
,
2436 const cmdmap_t
& cmdmap
,
2440 if (command
== "dump_ops_in_flight" ||
2442 if (!op_tracker
.dump_ops_in_flight(f
)) {
2443 ss
<< "op_tracker tracking is not enabled now, so no ops are tracked currently, even those get stuck. \
2444 please enable \"mds_enable_op_tracker\", and the tracker will start to track new ops received afterwards.";
2446 } else if (command
== "dump_blocked_ops") {
2447 if (!op_tracker
.dump_ops_in_flight(f
, true)) {
2448 ss
<< "op_tracker tracking is not enabled now, so no ops are tracked currently, even those get stuck. \
2449 Please enable \"mds_enable_op_tracker\", and the tracker will start to track new ops received afterwards.";
2451 } else if (command
== "dump_historic_ops") {
2452 if (!op_tracker
.dump_historic_ops(f
)) {
2453 ss
<< "op_tracker tracking is not enabled now, so no ops are tracked currently, even those get stuck. \
2454 please enable \"mds_enable_op_tracker\", and the tracker will start to track new ops received afterwards.";
2456 } else if (command
== "dump_historic_ops_by_duration") {
2457 if (!op_tracker
.dump_historic_ops(f
, true)) {
2458 ss
<< "op_tracker tracking is not enabled now, so no ops are tracked currently, even those get stuck. \
2459 please enable \"mds_enable_op_tracker\", and the tracker will start to track new ops received afterwards.";
2461 } else if (command
== "osdmap barrier") {
2462 int64_t target_epoch
= 0;
2463 bool got_val
= cmd_getval(g_ceph_context
, cmdmap
, "target_epoch", target_epoch
);
2466 ss
<< "no target epoch given";
2471 set_osd_epoch_barrier(target_epoch
);
2475 bool already_got
= objecter
->wait_for_map(target_epoch
, &cond
);
2477 dout(4) << __func__
<< ": waiting for OSD epoch " << target_epoch
<< dendl
;
2480 } else if (command
== "session ls") {
2481 std::lock_guard
l(mds_lock
);
2485 dump_sessions(SessionFilter(), f
);
2486 } else if (command
== "session evict") {
2487 std::string client_id
;
2488 const bool got_arg
= cmd_getval(g_ceph_context
, cmdmap
, "client_id", client_id
);
2490 ss
<< "Invalid client_id specified";
2495 std::stringstream dss
;
2496 bool evicted
= evict_client(strtol(client_id
.c_str(), 0, 10), true,
2497 g_conf()->mds_session_blacklist_on_evict
, dss
);
2499 dout(15) << dss
.str() << dendl
;
2503 } else if (command
== "scrub_path") {
2505 vector
<string
> scrubop_vec
;
2506 cmd_getval(g_ceph_context
, cmdmap
, "scrubops", scrubop_vec
);
2507 cmd_getval(g_ceph_context
, cmdmap
, "path", path
);
2510 command_scrub_start(f
, path
, "", scrubop_vec
, &cond
);
2512 } else if (command
== "tag path") {
2514 cmd_getval(g_ceph_context
, cmdmap
, "path", path
);
2516 cmd_getval(g_ceph_context
, cmdmap
, "tag", tag
);
2517 command_tag_path(f
, path
, tag
);
2518 } else if (command
== "flush_path") {
2520 cmd_getval(g_ceph_context
, cmdmap
, "path", path
);
2521 command_flush_path(f
, path
);
2522 } else if (command
== "flush journal") {
2523 command_flush_journal(f
);
2524 } else if (command
== "get subtrees") {
2525 command_get_subtrees(f
);
2526 } else if (command
== "export dir") {
2528 if(!cmd_getval(g_ceph_context
, cmdmap
, "path", path
)) {
2529 ss
<< "malformed path";
2533 if(!cmd_getval(g_ceph_context
, cmdmap
, "rank", rank
)) {
2534 ss
<< "malformed rank";
2537 command_export_dir(f
, path
, (mds_rank_t
)rank
);
2538 } else if (command
== "dump cache") {
2539 std::lock_guard
l(mds_lock
);
2542 if(!cmd_getval(g_ceph_context
, cmdmap
, "path", path
)) {
2543 r
= mdcache
->dump_cache(f
);
2545 r
= mdcache
->dump_cache(path
);
2549 ss
<< "Failed to dump cache: " << cpp_strerror(r
);
2552 } else if (command
== "cache status") {
2553 std::lock_guard
l(mds_lock
);
2554 mdcache
->cache_status(f
);
2555 } else if (command
== "dump tree") {
2556 command_dump_tree(cmdmap
, ss
, f
);
2557 } else if (command
== "dump loads") {
2558 std::lock_guard
l(mds_lock
);
2559 int r
= balancer
->dump_loads(f
);
2561 ss
<< "Failed to dump loads: " << cpp_strerror(r
);
2564 } else if (command
== "dump snaps") {
2565 std::lock_guard
l(mds_lock
);
2567 cmd_getval(g_ceph_context
, cmdmap
, "server", server
);
2568 if (server
== "--server") {
2569 if (mdsmap
->get_tableserver() == whoami
) {
2570 snapserver
->dump(f
);
2572 ss
<< "Not snapserver";
2575 int r
= snapclient
->dump_cache(f
);
2577 ss
<< "Failed to dump snapclient: " << cpp_strerror(r
);
2581 } else if (command
== "force_readonly") {
2582 std::lock_guard
l(mds_lock
);
2583 mdcache
->force_readonly();
2584 } else if (command
== "dirfrag split") {
2585 command_dirfrag_split(cmdmap
, ss
);
2586 } else if (command
== "dirfrag merge") {
2587 command_dirfrag_merge(cmdmap
, ss
);
2588 } else if (command
== "dirfrag ls") {
2589 command_dirfrag_ls(cmdmap
, ss
, f
);
2590 } else if (command
== "openfiles ls") {
2591 command_openfiles_ls(f
);
2592 } else if (command
== "dump inode") {
2593 command_dump_inode(f
, cmdmap
, ss
);
2601 class C_MDS_Send_Command_Reply
: public MDSInternalContext
{
2603 MCommand::const_ref m
;
2605 C_MDS_Send_Command_Reply(MDSRank
*_mds
, const MCommand::const_ref
&_m
) :
2606 MDSInternalContext(_mds
), m(_m
) {}
2608 void send(int r
, std::string_view ss
) {
2609 std::stringstream ds
;
2613 void send(int r
, std::string_view ss
, std::stringstream
&ds
) {
2616 MDSDaemon::send_command_reply(m
, mds
, r
, bl
, ss
);
2619 void finish(int r
) override
{
2624 class C_ExecAndReply
: public C_MDS_Send_Command_Reply
{
2626 C_ExecAndReply(MDSRank
*mds
, const MCommand::const_ref
&m
)
2627 : C_MDS_Send_Command_Reply(mds
, m
), f(true) {
2630 void finish(int r
) override
{
2631 std::stringstream ds
;
2632 std::stringstream ss
;
2639 send(r
, ss
.str(), ds
);
2642 virtual void exec() = 0;
2648 class C_CacheDropExecAndReply
: public C_ExecAndReply
{
2650 C_CacheDropExecAndReply(MDSRank
*mds
, const MCommand::const_ref
&m
,
2652 : C_ExecAndReply(mds
, m
), timeout(timeout
) {
2655 void exec() override
{
2656 mds
->command_cache_drop(timeout
, &f
, this);
2663 class C_ScrubExecAndReply
: public C_ExecAndReply
{
2665 C_ScrubExecAndReply(MDSRank
*mds
, const MCommand::const_ref
&m
,
2666 const std::string
&path
, const std::string
&tag
,
2667 const std::vector
<std::string
> &scrubop
)
2668 : C_ExecAndReply(mds
, m
), path(path
), tag(tag
), scrubop(scrubop
) {
2671 void exec() override
{
2672 mds
->command_scrub_start(&f
, path
, tag
, scrubop
, this);
2678 std::vector
<std::string
> scrubop
;
2681 class C_ScrubControlExecAndReply
: public C_ExecAndReply
{
2683 C_ScrubControlExecAndReply(MDSRank
*mds
, const MCommand::const_ref
&m
,
2684 const std::string
&command
)
2685 : C_ExecAndReply(mds
, m
), command(command
) {
2688 void exec() override
{
2689 if (command
== "abort") {
2690 mds
->command_scrub_abort(&f
, this);
2691 } else if (command
== "pause") {
2692 mds
->command_scrub_pause(&f
, this);
2698 void finish(int r
) override
{
2699 f
.open_object_section("result");
2700 f
.dump_int("return_code", r
);
2702 C_ExecAndReply::finish(r
);
2706 std::string command
;
2710 * This function drops the mds_lock, so don't do anything with
2711 * MDSRank after calling it (we could have gone into shutdown): just
2712 * send your result back to the calling client and finish.
2714 void MDSRankDispatcher::evict_clients(const SessionFilter
&filter
, const MCommand::const_ref
&m
)
2716 C_MDS_Send_Command_Reply
*reply
= new C_MDS_Send_Command_Reply(this, m
);
2718 if (is_any_replay()) {
2719 reply
->send(-EAGAIN
, "MDS is replaying log");
2724 std::vector
<Session
*> victims
;
2725 const auto& sessions
= sessionmap
.get_sessions();
2726 for (const auto& p
: sessions
) {
2727 if (!p
.first
.is_client()) {
2731 Session
*s
= p
.second
;
2733 if (filter
.match(*s
, std::bind(&Server::waiting_for_reconnect
, server
, std::placeholders::_1
))) {
2734 victims
.push_back(s
);
2738 dout(20) << __func__
<< " matched " << victims
.size() << " sessions" << dendl
;
2740 if (victims
.empty()) {
2746 C_GatherBuilder
gather(g_ceph_context
, reply
);
2747 for (const auto s
: victims
) {
2748 std::stringstream ss
;
2749 evict_client(s
->get_client().v
, false,
2750 g_conf()->mds_session_blacklist_on_evict
, ss
, gather
.new_sub());
2755 void MDSRankDispatcher::dump_sessions(const SessionFilter
&filter
, Formatter
*f
) const
2757 // Dump sessions, decorated with recovery/replay status
2758 f
->open_array_section("sessions");
2759 const ceph::unordered_map
<entity_name_t
, Session
*> session_map
= sessionmap
.get_sessions();
2760 for (auto& p
: session_map
) {
2761 if (!p
.first
.is_client()) {
2765 Session
*s
= p
.second
;
2767 if (!filter
.match(*s
, std::bind(&Server::waiting_for_reconnect
, server
, std::placeholders::_1
))) {
2771 f
->open_object_section("session");
2772 f
->dump_int("id", p
.first
.num());
2774 f
->dump_int("num_leases", s
->leases
.size());
2775 f
->dump_int("num_caps", s
->caps
.size());
2777 f
->dump_string("state", s
->get_state_name());
2778 if (s
->is_open() || s
->is_stale()) {
2779 f
->dump_unsigned("request_load_avg", s
->get_load_avg());
2781 f
->dump_float("uptime", s
->get_session_uptime());
2782 f
->dump_int("replay_requests", is_clientreplay() ? s
->get_request_count() : 0);
2783 f
->dump_unsigned("completed_requests", s
->get_num_completed_requests());
2784 f
->dump_bool("reconnecting", server
->waiting_for_reconnect(p
.first
.num()));
2785 f
->dump_stream("inst") << s
->info
.inst
;
2786 f
->open_object_section("client_metadata");
2787 s
->info
.client_metadata
.dump(f
);
2788 f
->close_section(); // client_metadata
2789 f
->close_section(); //session
2791 f
->close_section(); //sessions
2794 void MDSRank::command_scrub_start(Formatter
*f
,
2795 std::string_view path
, std::string_view tag
,
2796 const vector
<string
>& scrubop_vec
, Context
*on_finish
)
2799 bool recursive
= false;
2800 bool repair
= false;
2801 for (auto &op
: scrubop_vec
) {
2804 else if (op
== "recursive")
2806 else if (op
== "repair")
2810 std::lock_guard
l(mds_lock
);
2811 mdcache
->enqueue_scrub(path
, tag
, force
, recursive
, repair
, f
, on_finish
);
2812 // scrub_dentry() finishers will dump the data for us; we're done!
2815 void MDSRank::command_tag_path(Formatter
*f
,
2816 std::string_view path
, std::string_view tag
)
2820 std::lock_guard
l(mds_lock
);
2821 mdcache
->enqueue_scrub(path
, tag
, true, true, false, f
, &scond
);
2826 void MDSRank::command_scrub_abort(Formatter
*f
, Context
*on_finish
) {
2827 std::lock_guard
l(mds_lock
);
2828 scrubstack
->scrub_abort(on_finish
);
2831 void MDSRank::command_scrub_pause(Formatter
*f
, Context
*on_finish
) {
2832 std::lock_guard
l(mds_lock
);
2833 scrubstack
->scrub_pause(on_finish
);
2836 void MDSRank::command_scrub_resume(Formatter
*f
) {
2837 int r
= scrubstack
->scrub_resume();
2839 f
->open_object_section("result");
2840 f
->dump_int("return_code", r
);
2844 void MDSRank::command_scrub_status(Formatter
*f
) {
2845 scrubstack
->scrub_status(f
);
2848 void MDSRank::command_flush_path(Formatter
*f
, std::string_view path
)
2852 std::lock_guard
l(mds_lock
);
2853 mdcache
->flush_dentry(path
, &scond
);
2855 int r
= scond
.wait();
2856 f
->open_object_section("results");
2857 f
->dump_int("return_code", r
);
2858 f
->close_section(); // results
2861 // synchronous wrapper around "journal flush" asynchronous context
2863 void MDSRank::command_flush_journal(Formatter
*f
) {
2864 ceph_assert(f
!= NULL
);
2867 std::stringstream ss
;
2869 std::lock_guard
locker(mds_lock
);
2870 C_Flush_Journal
*flush_journal
= new C_Flush_Journal(mdcache
, mdlog
, this, &ss
, &cond
);
2871 flush_journal
->send();
2873 int r
= cond
.wait();
2875 f
->open_object_section("result");
2876 f
->dump_string("message", ss
.str());
2877 f
->dump_int("return_code", r
);
2881 void MDSRank::command_get_subtrees(Formatter
*f
)
2883 ceph_assert(f
!= NULL
);
2884 std::lock_guard
l(mds_lock
);
2886 std::vector
<CDir
*> subtrees
;
2887 mdcache
->get_subtrees(subtrees
);
2889 f
->open_array_section("subtrees");
2890 for (const auto& dir
: subtrees
) {
2891 f
->open_object_section("subtree");
2893 f
->dump_bool("is_auth", dir
->is_auth());
2894 f
->dump_int("auth_first", dir
->get_dir_auth().first
);
2895 f
->dump_int("auth_second", dir
->get_dir_auth().second
);
2896 f
->dump_int("export_pin", dir
->inode
->get_export_pin());
2897 f
->open_object_section("dir");
2907 void MDSRank::command_export_dir(Formatter
*f
,
2908 std::string_view path
,
2911 int r
= _command_export_dir(path
, target
);
2912 f
->open_object_section("results");
2913 f
->dump_int("return_code", r
);
2914 f
->close_section(); // results
2917 int MDSRank::_command_export_dir(
2918 std::string_view path
,
2921 std::lock_guard
l(mds_lock
);
2924 if (target
== whoami
|| !mdsmap
->is_up(target
) || !mdsmap
->is_in(target
)) {
2925 derr
<< "bad MDS target " << target
<< dendl
;
2929 CInode
*in
= mdcache
->cache_traverse(fp
);
2931 derr
<< "Bath path '" << path
<< "'" << dendl
;
2934 CDir
*dir
= in
->get_dirfrag(frag_t());
2935 if (!dir
|| !(dir
->is_auth())) {
2936 derr
<< "bad export_dir path dirfrag frag_t() or dir not auth" << dendl
;
2940 mdcache
->migrator
->export_dir(dir
, target
);
2944 void MDSRank::command_dump_tree(const cmdmap_t
&cmdmap
, std::ostream
&ss
, Formatter
*f
)
2948 cmd_getval(g_ceph_context
, cmdmap
, "root", root
);
2949 if (!cmd_getval(g_ceph_context
, cmdmap
, "depth", depth
))
2951 std::lock_guard
l(mds_lock
);
2952 CInode
*in
= mdcache
->cache_traverse(filepath(root
.c_str()));
2954 ss
<< "root inode is not in cache";
2957 f
->open_array_section("inodes");
2958 mdcache
->dump_tree(in
, 0, depth
, f
);
2962 CDir
*MDSRank::_command_dirfrag_get(
2963 const cmdmap_t
&cmdmap
,
2967 bool got
= cmd_getval(g_ceph_context
, cmdmap
, "path", path
);
2969 ss
<< "missing path argument";
2973 std::string frag_str
;
2974 if (!cmd_getval(g_ceph_context
, cmdmap
, "frag", frag_str
)) {
2975 ss
<< "missing frag argument";
2979 CInode
*in
= mdcache
->cache_traverse(filepath(path
.c_str()));
2981 // TODO really we should load something in if it's not in cache,
2982 // but the infrastructure is harder, and we might still be unable
2983 // to act on it if someone else is auth.
2984 ss
<< "directory '" << path
<< "' inode not in cache";
2990 if (!fg
.parse(frag_str
.c_str())) {
2991 ss
<< "frag " << frag_str
<< " failed to parse";
2995 CDir
*dir
= in
->get_dirfrag(fg
);
2997 ss
<< "frag " << in
->ino() << "/" << fg
<< " not in cache ("
2998 "use `dirfrag ls` to see if it should exist)";
3002 if (!dir
->is_auth()) {
3003 ss
<< "frag " << dir
->dirfrag() << " not auth (auth = "
3004 << dir
->authority() << ")";
3011 bool MDSRank::command_dirfrag_split(
3015 std::lock_guard
l(mds_lock
);
3017 if (!cmd_getval(g_ceph_context
, cmdmap
, "bits", by
)) {
3018 ss
<< "missing bits argument";
3023 ss
<< "must split by >0 bits";
3027 CDir
*dir
= _command_dirfrag_get(cmdmap
, ss
);
3032 mdcache
->split_dir(dir
, by
);
3037 bool MDSRank::command_dirfrag_merge(
3041 std::lock_guard
l(mds_lock
);
3043 bool got
= cmd_getval(g_ceph_context
, cmdmap
, "path", path
);
3045 ss
<< "missing path argument";
3049 std::string frag_str
;
3050 if (!cmd_getval(g_ceph_context
, cmdmap
, "frag", frag_str
)) {
3051 ss
<< "missing frag argument";
3055 CInode
*in
= mdcache
->cache_traverse(filepath(path
.c_str()));
3057 ss
<< "directory '" << path
<< "' inode not in cache";
3062 if (!fg
.parse(frag_str
.c_str())) {
3063 ss
<< "frag " << frag_str
<< " failed to parse";
3067 mdcache
->merge_dir(in
, fg
);
3072 bool MDSRank::command_dirfrag_ls(
3077 std::lock_guard
l(mds_lock
);
3079 bool got
= cmd_getval(g_ceph_context
, cmdmap
, "path", path
);
3081 ss
<< "missing path argument";
3085 CInode
*in
= mdcache
->cache_traverse(filepath(path
.c_str()));
3087 ss
<< "directory inode not in cache";
3091 f
->open_array_section("frags");
3093 // NB using get_leaves_under instead of get_dirfrags to give
3094 // you the list of what dirfrags may exist, not which are in cache
3095 in
->dirfragtree
.get_leaves_under(frag_t(), leaves
);
3096 for (const auto& leaf
: leaves
) {
3097 f
->open_object_section("frag");
3098 f
->dump_int("value", leaf
.value());
3099 f
->dump_int("bits", leaf
.bits());
3100 CachedStackStringStream css
;
3101 *css
<< std::hex
<< leaf
.value() << "/" << std::dec
<< leaf
.bits();
3102 f
->dump_string("str", css
->strv());
3110 void MDSRank::command_openfiles_ls(Formatter
*f
)
3112 std::lock_guard
l(mds_lock
);
3113 mdcache
->dump_openfiles(f
);
3116 void MDSRank::command_dump_inode(Formatter
*f
, const cmdmap_t
&cmdmap
, std::ostream
&ss
)
3118 std::lock_guard
l(mds_lock
);
3120 bool got
= cmd_getval(g_ceph_context
, cmdmap
, "number", number
);
3122 ss
<< "missing inode number";
3126 bool success
= mdcache
->dump_inode(f
, number
);
3128 ss
<< "dump inode failed, wrong inode number or the inode is not cached";
3132 void MDSRank::dump_status(Formatter
*f
) const
3134 if (state
== MDSMap::STATE_REPLAY
||
3135 state
== MDSMap::STATE_STANDBY_REPLAY
) {
3136 mdlog
->dump_replay_status(f
);
3137 } else if (state
== MDSMap::STATE_RESOLVE
) {
3138 mdcache
->dump_resolve_status(f
);
3139 } else if (state
== MDSMap::STATE_RECONNECT
) {
3140 server
->dump_reconnect_status(f
);
3141 } else if (state
== MDSMap::STATE_REJOIN
) {
3142 mdcache
->dump_rejoin_status(f
);
3143 } else if (state
== MDSMap::STATE_CLIENTREPLAY
) {
3144 dump_clientreplay_status(f
);
3146 f
->dump_float("rank_uptime", get_uptime().count());
3149 void MDSRank::dump_clientreplay_status(Formatter
*f
) const
3151 f
->open_object_section("clientreplay_status");
3152 f
->dump_unsigned("clientreplay_queue", replay_queue
.size());
3153 f
->dump_unsigned("active_replay", mdcache
->get_num_client_requests());
3157 void MDSRankDispatcher::update_log_config()
3159 map
<string
,string
> log_to_monitors
;
3160 map
<string
,string
> log_to_syslog
;
3161 map
<string
,string
> log_channel
;
3162 map
<string
,string
> log_prio
;
3163 map
<string
,string
> log_to_graylog
;
3164 map
<string
,string
> log_to_graylog_host
;
3165 map
<string
,string
> log_to_graylog_port
;
3169 if (parse_log_client_options(g_ceph_context
, log_to_monitors
, log_to_syslog
,
3170 log_channel
, log_prio
, log_to_graylog
,
3171 log_to_graylog_host
, log_to_graylog_port
,
3173 clog
->update_config(log_to_monitors
, log_to_syslog
,
3174 log_channel
, log_prio
, log_to_graylog
,
3175 log_to_graylog_host
, log_to_graylog_port
,
3177 dout(10) << __func__
<< " log_to_monitors " << log_to_monitors
<< dendl
;
3180 void MDSRank::create_logger()
3182 dout(10) << "create_logger" << dendl
;
3184 PerfCountersBuilder
mds_plb(g_ceph_context
, "mds", l_mds_first
, l_mds_last
);
3186 // super useful (high prio) perf stats
3187 mds_plb
.add_u64_counter(l_mds_request
, "request", "Requests", "req",
3188 PerfCountersBuilder::PRIO_CRITICAL
);
3189 mds_plb
.add_time_avg(l_mds_reply_latency
, "reply_latency", "Reply latency", "rlat",
3190 PerfCountersBuilder::PRIO_CRITICAL
);
3191 mds_plb
.add_u64(l_mds_inodes
, "inodes", "Inodes", "inos",
3192 PerfCountersBuilder::PRIO_CRITICAL
);
3193 mds_plb
.add_u64_counter(l_mds_forward
, "forward", "Forwarding request", "fwd",
3194 PerfCountersBuilder::PRIO_INTERESTING
);
3195 mds_plb
.add_u64(l_mds_caps
, "caps", "Capabilities", "caps",
3196 PerfCountersBuilder::PRIO_INTERESTING
);
3197 mds_plb
.add_u64_counter(l_mds_exported_inodes
, "exported_inodes", "Exported inodes",
3198 "exi", PerfCountersBuilder::PRIO_INTERESTING
);
3199 mds_plb
.add_u64_counter(l_mds_imported_inodes
, "imported_inodes", "Imported inodes",
3200 "imi", PerfCountersBuilder::PRIO_INTERESTING
);
3202 // useful dir/inode/subtree stats
3203 mds_plb
.set_prio_default(PerfCountersBuilder::PRIO_USEFUL
);
3204 mds_plb
.add_u64_counter(l_mds_dir_fetch
, "dir_fetch", "Directory fetch");
3205 mds_plb
.add_u64_counter(l_mds_dir_commit
, "dir_commit", "Directory commit");
3206 mds_plb
.add_u64_counter(l_mds_dir_split
, "dir_split", "Directory split");
3207 mds_plb
.add_u64_counter(l_mds_dir_merge
, "dir_merge", "Directory merge");
3208 mds_plb
.add_u64(l_mds_inode_max
, "inode_max", "Max inodes, cache size");
3209 mds_plb
.add_u64(l_mds_inodes_pinned
, "inodes_pinned", "Inodes pinned");
3210 mds_plb
.add_u64(l_mds_inodes_expired
, "inodes_expired", "Inodes expired");
3211 mds_plb
.add_u64(l_mds_inodes_with_caps
, "inodes_with_caps",
3212 "Inodes with capabilities");
3213 mds_plb
.add_u64(l_mds_subtrees
, "subtrees", "Subtrees");
3214 mds_plb
.add_u64(l_mds_load_cent
, "load_cent", "Load per cent");
3215 mds_plb
.add_u64_counter(l_mds_openino_dir_fetch
, "openino_dir_fetch",
3216 "OpenIno incomplete directory fetchings");
3219 mds_plb
.set_prio_default(PerfCountersBuilder::PRIO_DEBUGONLY
);
3220 mds_plb
.add_u64_counter(l_mds_reply
, "reply", "Replies");
3221 mds_plb
.add_u64(l_mds_inodes_top
, "inodes_top", "Inodes on top");
3222 mds_plb
.add_u64(l_mds_inodes_bottom
, "inodes_bottom", "Inodes on bottom");
3224 l_mds_inodes_pin_tail
, "inodes_pin_tail", "Inodes on pin tail");
3225 mds_plb
.add_u64_counter(l_mds_traverse
, "traverse", "Traverses");
3226 mds_plb
.add_u64_counter(l_mds_traverse_hit
, "traverse_hit", "Traverse hits");
3227 mds_plb
.add_u64_counter(l_mds_traverse_forward
, "traverse_forward",
3228 "Traverse forwards");
3229 mds_plb
.add_u64_counter(l_mds_traverse_discover
, "traverse_discover",
3230 "Traverse directory discovers");
3231 mds_plb
.add_u64_counter(l_mds_traverse_dir_fetch
, "traverse_dir_fetch",
3232 "Traverse incomplete directory content fetchings");
3233 mds_plb
.add_u64_counter(l_mds_traverse_remote_ino
, "traverse_remote_ino",
3234 "Traverse remote dentries");
3235 mds_plb
.add_u64_counter(l_mds_traverse_lock
, "traverse_lock",
3237 mds_plb
.add_u64(l_mds_dispatch_queue_len
, "q", "Dispatch queue length");
3238 mds_plb
.add_u64_counter(l_mds_exported
, "exported", "Exports");
3239 mds_plb
.add_u64_counter(l_mds_imported
, "imported", "Imports");
3240 mds_plb
.add_u64_counter(l_mds_openino_backtrace_fetch
, "openino_backtrace_fetch",
3241 "OpenIno backtrace fetchings");
3242 mds_plb
.add_u64_counter(l_mds_openino_peer_discover
, "openino_peer_discover",
3243 "OpenIno peer inode discovers");
3245 logger
= mds_plb
.create_perf_counters();
3246 g_ceph_context
->get_perfcounters_collection()->add(logger
);
3250 PerfCountersBuilder
mdm_plb(g_ceph_context
, "mds_mem", l_mdm_first
, l_mdm_last
);
3251 mdm_plb
.add_u64(l_mdm_ino
, "ino", "Inodes", "ino",
3252 PerfCountersBuilder::PRIO_INTERESTING
);
3253 mdm_plb
.add_u64(l_mdm_dn
, "dn", "Dentries", "dn",
3254 PerfCountersBuilder::PRIO_INTERESTING
);
3256 mdm_plb
.set_prio_default(PerfCountersBuilder::PRIO_USEFUL
);
3257 mdm_plb
.add_u64_counter(l_mdm_inoa
, "ino+", "Inodes opened");
3258 mdm_plb
.add_u64_counter(l_mdm_inos
, "ino-", "Inodes closed");
3259 mdm_plb
.add_u64(l_mdm_dir
, "dir", "Directories");
3260 mdm_plb
.add_u64_counter(l_mdm_dira
, "dir+", "Directories opened");
3261 mdm_plb
.add_u64_counter(l_mdm_dirs
, "dir-", "Directories closed");
3262 mdm_plb
.add_u64_counter(l_mdm_dna
, "dn+", "Dentries opened");
3263 mdm_plb
.add_u64_counter(l_mdm_dns
, "dn-", "Dentries closed");
3264 mdm_plb
.add_u64(l_mdm_cap
, "cap", "Capabilities");
3265 mdm_plb
.add_u64_counter(l_mdm_capa
, "cap+", "Capabilities added");
3266 mdm_plb
.add_u64_counter(l_mdm_caps
, "cap-", "Capabilities removed");
3267 mdm_plb
.add_u64(l_mdm_heap
, "heap", "Heap size");
3269 mdm_plb
.set_prio_default(PerfCountersBuilder::PRIO_DEBUGONLY
);
3270 mdm_plb
.add_u64(l_mdm_rss
, "rss", "RSS");
3272 mlogger
= mdm_plb
.create_perf_counters();
3273 g_ceph_context
->get_perfcounters_collection()->add(mlogger
);
3276 mdlog
->create_logger();
3277 server
->create_logger();
3278 purge_queue
.create_logger();
3279 sessionmap
.register_perfcounters();
3280 mdcache
->register_perfcounters();
3283 void MDSRank::check_ops_in_flight()
3286 vector
<string
> warnings
;
3288 if (op_tracker
.check_ops_in_flight(&summary
, warnings
, &slow
)) {
3289 clog
->warn() << summary
;
3290 for (const auto& warning
: warnings
) {
3291 clog
->warn() << warning
;
3295 // set mds slow request count
3296 mds_slow_req_count
= slow
;
3300 void MDSRankDispatcher::handle_osd_map()
3303 mdsmap
->get_tableserver() == whoami
) {
3304 snapserver
->check_osd_map(true);
3307 server
->handle_osd_map();
3309 purge_queue
.update_op_limit(*mdsmap
);
3311 std::set
<entity_addr_t
> newly_blacklisted
;
3312 objecter
->consume_blacklist_events(&newly_blacklisted
);
3313 auto epoch
= objecter
->with_osdmap([](const OSDMap
&o
){return o
.get_epoch();});
3314 dout(4) << "handle_osd_map epoch " << epoch
<< ", "
3315 << newly_blacklisted
.size() << " new blacklist entries" << dendl
;
3316 auto victims
= server
->apply_blacklist(newly_blacklisted
);
3318 set_osd_epoch_barrier(epoch
);
3322 // By default the objecter only requests OSDMap updates on use,
3323 // we would like to always receive the latest maps in order to
3324 // apply policy based on the FULL flag.
3325 objecter
->maybe_request_map();
3328 bool MDSRank::evict_client(int64_t session_id
,
3329 bool wait
, bool blacklist
, std::ostream
& err_ss
,
3332 ceph_assert(mds_lock
.is_locked_by_me());
3334 // Mutually exclusive args
3335 ceph_assert(!(wait
&& on_killed
!= nullptr));
3337 if (is_any_replay()) {
3338 err_ss
<< "MDS is replaying log";
3342 Session
*session
= sessionmap
.get_session(
3343 entity_name_t(CEPH_ENTITY_TYPE_CLIENT
, session_id
));
3345 err_ss
<< "session " << session_id
<< " not in sessionmap!";
3349 auto& addr
= session
->info
.inst
.addr
;
3351 CachedStackStringStream css
;
3352 *css
<< "Evicting " << (blacklist
? "(and blacklisting) " : "")
3353 << "client session " << session_id
<< " (" << addr
<< ")";
3354 dout(1) << css
->strv() << dendl
;
3355 clog
->info() << css
->strv();
3358 dout(4) << "Preparing blacklist command... (wait=" << wait
<< ")" << dendl
;
3360 ss
<< "{\"prefix\":\"osd blacklist\", \"blacklistop\":\"add\",";
3361 ss
<< "\"addr\":\"";
3364 std::string tmp
= ss
.str();
3365 std::vector
<std::string
> cmd
= {tmp
};
3367 auto kill_client_session
= [this, session_id
, wait
, on_killed
](){
3368 ceph_assert(mds_lock
.is_locked_by_me());
3369 Session
*session
= sessionmap
.get_session(
3370 entity_name_t(CEPH_ENTITY_TYPE_CLIENT
, session_id
));
3372 if (on_killed
|| !wait
) {
3373 server
->kill_session(session
, on_killed
);
3375 C_SaferCond on_safe
;
3376 server
->kill_session(session
, &on_safe
);
3383 dout(1) << "session " << session_id
<< " was removed while we waited "
3384 "for blacklist" << dendl
;
3386 // Even though it wasn't us that removed it, kick our completion
3387 // as the session has been removed.
3389 on_killed
->complete(0);
3394 auto apply_blacklist
= [this, cmd
](std::function
<void ()> fn
){
3395 ceph_assert(mds_lock
.is_locked_by_me());
3397 Context
*on_blacklist_done
= new FunctionContext([this, fn
](int r
) {
3398 objecter
->wait_for_latest_osdmap(
3400 new FunctionContext([this, fn
](int r
) {
3401 std::lock_guard
l(mds_lock
);
3402 auto epoch
= objecter
->with_osdmap([](const OSDMap
&o
){
3403 return o
.get_epoch();
3406 set_osd_epoch_barrier(epoch
);
3413 dout(4) << "Sending mon blacklist command: " << cmd
[0] << dendl
;
3414 monc
->start_mon_command(cmd
, {}, nullptr, nullptr, on_blacklist_done
);
3419 C_SaferCond inline_ctx
;
3420 apply_blacklist([&inline_ctx
](){inline_ctx
.complete(0);});
3426 // We dropped mds_lock, so check that session still exists
3427 session
= sessionmap
.get_session(entity_name_t(CEPH_ENTITY_TYPE_CLIENT
,
3430 dout(1) << "session " << session_id
<< " was removed while we waited "
3431 "for blacklist" << dendl
;
3434 kill_client_session();
3437 apply_blacklist(kill_client_session
);
3439 kill_client_session();
3446 void MDSRank::bcast_mds_map()
3448 dout(7) << "bcast_mds_map " << mdsmap
->get_epoch() << dendl
;
3450 // share the map with mounted clients
3451 set
<Session
*> clients
;
3452 sessionmap
.get_client_session_set(clients
);
3453 for (const auto &session
: clients
) {
3454 auto m
= MMDSMap::create(monc
->get_fsid(), *mdsmap
);
3455 session
->get_connection()->send_message2(std::move(m
));
3457 last_client_mdsmap_bcast
= mdsmap
->get_epoch();
3460 Context
*MDSRank::create_async_exec_context(C_ExecAndReply
*ctx
) {
3461 return new C_OnFinisher(new FunctionContext([ctx
](int _
) {
3466 MDSRankDispatcher::MDSRankDispatcher(
3469 LogChannelRef
&clog_
,
3472 std::unique_ptr
<MDSMap
> &mdsmap_
,
3475 Context
*respawn_hook_
,
3476 Context
*suicide_hook_
)
3477 : MDSRank(whoami_
, mds_lock_
, clog_
, timer_
, beacon_
, mdsmap_
,
3478 msgr
, monc_
, respawn_hook_
, suicide_hook_
)
3481 bool MDSRankDispatcher::handle_command(
3482 const cmdmap_t
&cmdmap
,
3483 const MCommand::const_ref
&m
,
3485 std::stringstream
*ds
,
3486 std::stringstream
*ss
,
3487 Context
**run_later
,
3490 ceph_assert(r
!= nullptr);
3491 ceph_assert(ds
!= nullptr);
3492 ceph_assert(ss
!= nullptr);
3497 cmd_getval(g_ceph_context
, cmdmap
, "prefix", prefix
);
3499 if (prefix
== "session ls" || prefix
== "client ls") {
3500 std::vector
<std::string
> filter_args
;
3501 cmd_getval(g_ceph_context
, cmdmap
, "filters", filter_args
);
3503 SessionFilter filter
;
3504 *r
= filter
.parse(filter_args
, ss
);
3509 JSONFormatter
f(true);
3510 dump_sessions(filter
, &f
);
3513 } else if (prefix
== "session evict" || prefix
== "client evict") {
3514 std::vector
<std::string
> filter_args
;
3515 cmd_getval(g_ceph_context
, cmdmap
, "filters", filter_args
);
3517 SessionFilter filter
;
3518 *r
= filter
.parse(filter_args
, ss
);
3523 evict_clients(filter
, m
);
3525 *need_reply
= false;
3527 } else if (prefix
== "damage ls") {
3528 JSONFormatter
f(true);
3529 damage_table
.dump(&f
);
3532 } else if (prefix
== "damage rm") {
3533 damage_entry_id_t id
= 0;
3534 bool got
= cmd_getval(g_ceph_context
, cmdmap
, "damage_id", (int64_t&)id
);
3540 damage_table
.erase(id
);
3542 } else if (prefix
== "cache drop") {
3544 if (!cmd_getval(g_ceph_context
, cmdmap
, "timeout", timeout
)) {
3548 *need_reply
= false;
3549 *run_later
= create_async_exec_context(new C_CacheDropExecAndReply
3550 (this, m
, (uint64_t)timeout
));
3552 } else if (prefix
== "scrub start") {
3555 vector
<string
> scrubop_vec
;
3556 cmd_getval(g_ceph_context
, cmdmap
, "scrubops", scrubop_vec
);
3557 cmd_getval(g_ceph_context
, cmdmap
, "path", path
);
3558 cmd_getval(g_ceph_context
, cmdmap
, "tag", tag
);
3560 *need_reply
= false;
3561 *run_later
= create_async_exec_context(new C_ScrubExecAndReply
3562 (this, m
, path
, tag
, scrubop_vec
));
3564 } else if (prefix
== "scrub abort") {
3565 *need_reply
= false;
3566 *run_later
= create_async_exec_context(new C_ScrubControlExecAndReply
3567 (this, m
, "abort"));
3569 } else if (prefix
== "scrub pause") {
3570 *need_reply
= false;
3571 *run_later
= create_async_exec_context(new C_ScrubControlExecAndReply
3572 (this, m
, "pause"));
3574 } else if (prefix
== "scrub resume") {
3575 JSONFormatter
f(true);
3576 command_scrub_resume(&f
);
3579 } else if (prefix
== "scrub status") {
3580 JSONFormatter
f(true);
3581 command_scrub_status(&f
);
3589 void MDSRank::command_cache_drop(uint64_t timeout
, Formatter
*f
, Context
*on_finish
) {
3590 dout(20) << __func__
<< dendl
;
3592 std::lock_guard
locker(mds_lock
);
3593 C_Drop_Cache
*request
= new C_Drop_Cache(server
, mdcache
, mdlog
, this,
3594 timeout
, f
, on_finish
);
3598 epoch_t
MDSRank::get_osd_epoch() const
3600 return objecter
->with_osdmap(std::mem_fn(&OSDMap::get_epoch
));