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) 2004-2006 Sage Weil <sage@newdream.net>
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.
17 #include <boost/utility.hpp>
19 #include "MDSMonitor.h"
20 #include "FSCommands.h"
22 #include "MonitorDBStore.h"
23 #include "OSDMonitor.h"
25 #include "common/strtol.h"
26 #include "common/perf_counters.h"
27 #include "common/config.h"
28 #include "common/cmdparse.h"
29 #include "messages/MMDSMap.h"
30 #include "messages/MFSMap.h"
31 #include "messages/MFSMapUser.h"
32 #include "messages/MMDSLoadTargets.h"
33 #include "messages/MMonCommand.h"
34 #include "messages/MGenericMessage.h"
36 #include "include/ceph_assert.h"
37 #include "include/str_list.h"
38 #include "include/stringify.h"
39 #include "mds/mdstypes.h"
42 #define dout_subsys ceph_subsys_mon
44 #define dout_prefix _prefix(_dout, mon, get_fsmap())
45 using namespace TOPNSPC::common
;
47 static ostream
& _prefix(std::ostream
*_dout
, Monitor
*mon
, const FSMap
& fsmap
) {
48 return *_dout
<< "mon." << mon
->name
<< "@" << mon
->rank
49 << "(" << mon
->get_state_name()
50 << ").mds e" << fsmap
.get_epoch() << " ";
53 static const string
MDS_METADATA_PREFIX("mds_metadata");
54 static const string
MDS_HEALTH_PREFIX("mds_health");
58 * Specialized implementation of cmd_getval to allow us to parse
59 * out strongly-typedef'd types
61 namespace TOPNSPC::common
{
62 template<> bool cmd_getval(const cmdmap_t
& cmdmap
,
63 const std::string
& k
, mds_gid_t
&val
)
65 return cmd_getval(cmdmap
, k
, (int64_t&)val
);
68 template<> bool cmd_getval(const cmdmap_t
& cmdmap
,
69 const std::string
& k
, mds_rank_t
&val
)
71 return cmd_getval(cmdmap
, k
, (int64_t&)val
);
74 template<> bool cmd_getval(const cmdmap_t
& cmdmap
,
75 const std::string
& k
, MDSMap::DaemonState
&val
)
77 return cmd_getval(cmdmap
, k
, (int64_t&)val
);
83 void MDSMonitor::print_map(const FSMap
& m
)
85 dout(dblV
) << "print_map\n";
91 void MDSMonitor::create_initial()
93 dout(10) << "create_initial" << dendl
;
96 void MDSMonitor::get_store_prefixes(std::set
<string
>& s
) const
98 s
.insert(service_name
);
99 s
.insert(MDS_METADATA_PREFIX
);
100 s
.insert(MDS_HEALTH_PREFIX
);
103 void MDSMonitor::update_from_paxos(bool *need_bootstrap
)
105 version_t version
= get_last_committed();
106 if (version
== get_fsmap().epoch
)
109 dout(10) << __func__
<< " version " << version
110 << ", my e " << get_fsmap().epoch
<< dendl
;
111 ceph_assert(version
> get_fsmap().epoch
);
118 int err
= get_version(version
, fsmap_bl
);
119 ceph_assert(err
== 0);
121 ceph_assert(fsmap_bl
.length() > 0);
122 dout(10) << __func__
<< " got " << version
<< dendl
;
123 PaxosFSMap::decode(fsmap_bl
);
126 dout(0) << "new map" << dendl
;
127 print_map
<0>(get_fsmap());
128 if (!g_conf()->mon_mds_skip_sanity
) {
129 get_fsmap().sanity();
135 void MDSMonitor::init()
137 (void)load_metadata(pending_metadata
);
140 void MDSMonitor::create_pending()
142 auto &fsmap
= PaxosFSMap::create_pending();
144 if (mon
->osdmon()->is_readable()) {
145 const auto &osdmap
= mon
->osdmon()->osdmap
;
146 fsmap
.sanitize([&osdmap
](int64_t pool
){return osdmap
.have_pg_pool(pool
);});
149 dout(10) << "create_pending e" << fsmap
.epoch
<< dendl
;
152 void MDSMonitor::encode_pending(MonitorDBStore::TransactionRef t
)
154 auto &pending
= get_pending_fsmap_writeable();
155 auto &epoch
= pending
.epoch
;
157 dout(10) << "encode_pending e" << epoch
<< dendl
;
159 // print map iff 'debug mon = 30' or higher
160 print_map
<30>(pending
);
161 if (!g_conf()->mon_mds_skip_sanity
) {
165 // Set 'modified' on maps modified this epoch
166 for (auto &p
: pending
.filesystems
) {
167 if (p
.second
->mds_map
.epoch
== epoch
) {
168 p
.second
->mds_map
.modified
= ceph_clock_now();
173 ceph_assert(get_last_committed() + 1 == pending
.epoch
);
174 bufferlist pending_bl
;
175 pending
.encode(pending_bl
, mon
->get_quorum_con_features());
177 /* put everything in the transaction */
178 put_version(t
, pending
.epoch
, pending_bl
);
179 put_last_committed(t
, pending
.epoch
);
181 // Encode MDSHealth data
182 for (std::map
<uint64_t, MDSHealth
>::iterator i
= pending_daemon_health
.begin();
183 i
!= pending_daemon_health
.end(); ++i
) {
185 i
->second
.encode(bl
);
186 t
->put(MDS_HEALTH_PREFIX
, stringify(i
->first
), bl
);
189 for (std::set
<uint64_t>::iterator i
= pending_daemon_health_rm
.begin();
190 i
!= pending_daemon_health_rm
.end(); ++i
) {
191 t
->erase(MDS_HEALTH_PREFIX
, stringify(*i
));
193 pending_daemon_health_rm
.clear();
194 remove_from_metadata(pending
, t
);
197 health_check_map_t new_checks
;
198 const auto &info_map
= pending
.get_mds_info();
199 for (const auto &i
: info_map
) {
200 const auto &gid
= i
.first
;
201 const auto &info
= i
.second
;
202 if (pending_daemon_health_rm
.count(gid
)) {
206 auto p
= pending_daemon_health
.find(gid
);
207 if (p
!= pending_daemon_health
.end()) {
211 mon
->store
->get(MDS_HEALTH_PREFIX
, stringify(gid
), bl
);
213 derr
<< "Missing health data for MDS " << gid
<< dendl
;
216 auto bl_i
= bl
.cbegin();
219 for (const auto &metric
: health
.metrics
) {
220 const auto rank
= info
.rank
;
221 health_check_t
*check
= &new_checks
.get_or_add(
222 mds_metric_name(metric
.type
),
224 mds_metric_summary(metric
.type
),
227 ss
<< "mds" << info
.name
<< "(mds." << rank
<< "): " << metric
.message
;
229 for (auto &p
: metric
.metadata
) {
235 ss
<< p
.first
<< ": " << p
.second
;
238 check
->detail
.push_back(ss
.str());
241 pending
.get_health_checks(&new_checks
);
242 for (auto& p
: new_checks
.checks
) {
243 p
.second
.summary
= std::regex_replace(
246 stringify(p
.second
.detail
.size()));
247 p
.second
.summary
= std::regex_replace(
249 std::regex("%plurals%"),
250 p
.second
.detail
.size() > 1 ? "s" : "");
251 p
.second
.summary
= std::regex_replace(
253 std::regex("%isorare%"),
254 p
.second
.detail
.size() > 1 ? "are" : "is");
255 p
.second
.summary
= std::regex_replace(
257 std::regex("%hasorhave%"),
258 p
.second
.detail
.size() > 1 ? "have" : "has");
260 encode_health(new_checks
, t
);
263 version_t
MDSMonitor::get_trim_to() const
266 if (g_conf()->mon_mds_force_trim_to
> 0 &&
267 g_conf()->mon_mds_force_trim_to
< (int)get_last_committed()) {
268 floor
= g_conf()->mon_mds_force_trim_to
;
269 dout(10) << __func__
<< " explicit mon_mds_force_trim_to = "
273 unsigned max
= g_conf()->mon_max_mdsmap_epochs
;
274 version_t last
= get_last_committed();
276 if (last
- get_first_committed() > max
&& floor
< last
- max
)
281 bool MDSMonitor::preprocess_query(MonOpRequestRef op
)
283 op
->mark_mdsmon_event(__func__
);
284 auto m
= op
->get_req
<PaxosServiceMessage
>();
285 dout(10) << "preprocess_query " << *m
<< " from " << m
->get_orig_source()
286 << " " << m
->get_orig_source_addrs() << dendl
;
288 switch (m
->get_type()) {
291 return preprocess_beacon(op
);
293 case MSG_MON_COMMAND
:
295 return preprocess_command(op
);
296 } catch (const bad_cmd_get
& e
) {
298 mon
->reply_command(op
, -EINVAL
, e
.what(), bl
, get_last_committed());
302 case MSG_MDS_OFFLOAD_TARGETS
:
303 return preprocess_offload_targets(op
);
311 void MDSMonitor::_note_beacon(MMDSBeacon
*m
)
313 mds_gid_t gid
= mds_gid_t(m
->get_global_id());
314 version_t seq
= m
->get_seq();
316 dout(5) << "_note_beacon " << *m
<< " noting time" << dendl
;
317 auto &beacon
= last_beacon
[gid
];
318 beacon
.stamp
= mono_clock::now();
322 bool MDSMonitor::preprocess_beacon(MonOpRequestRef op
)
324 op
->mark_mdsmon_event(__func__
);
325 auto m
= op
->get_req
<MMDSBeacon
>();
326 MDSMap::DaemonState state
= m
->get_state();
327 mds_gid_t gid
= m
->get_global_id();
328 version_t seq
= m
->get_seq();
329 MDSMap::mds_info_t info
;
330 epoch_t effective_epoch
= 0;
332 const auto &fsmap
= get_fsmap();
334 // check privileges, ignore if fails
335 MonSession
*session
= op
->get_session();
338 if (!session
->is_capable("mds", MON_CAP_X
)) {
339 dout(0) << "preprocess_beacon got MMDSBeacon from entity with insufficient privileges "
340 << session
->caps
<< dendl
;
344 if (m
->get_fsid() != mon
->monmap
->fsid
) {
345 dout(0) << "preprocess_beacon on fsid " << m
->get_fsid() << " != " << mon
->monmap
->fsid
<< dendl
;
349 dout(5) << "preprocess_beacon " << *m
350 << " from " << m
->get_orig_source()
351 << " " << m
->get_orig_source_addrs()
352 << " " << m
->get_compat()
355 // make sure the address has a port
356 if (m
->get_orig_source_addr().get_port() == 0) {
357 dout(1) << " ignoring boot message without a port" << dendl
;
362 if (!m
->get_compat().writeable(fsmap
.compat
)) {
363 dout(1) << " mds " << m
->get_orig_source()
364 << " " << m
->get_orig_source_addrs()
365 << " can't write to fsmap " << fsmap
.compat
<< dendl
;
373 // booted, but not in map?
374 if (!fsmap
.gid_exists(gid
)) {
375 if (state
!= MDSMap::STATE_BOOT
) {
376 dout(7) << "mds_beacon " << *m
<< " is not in fsmap (state "
377 << ceph_mds_state_name(state
) << ")" << dendl
;
379 /* We can't send an MDSMap this MDS was a part of because we no longer
380 * know which FS it was part of. Nor does this matter. Sending an empty
381 * MDSMap is sufficient for getting the MDS to respawn.
384 null_map
.epoch
= fsmap
.epoch
;
385 null_map
.compat
= fsmap
.compat
;
386 auto m
= make_message
<MMDSMap
>(mon
->monmap
->fsid
, null_map
);
387 mon
->send_reply(op
, m
.detach());
390 return false; // not booted yet.
393 dout(10) << __func__
<< ": GID exists in map: " << gid
<< dendl
;
394 info
= fsmap
.get_info_gid(gid
);
397 if (info
.state_seq
> seq
) {
398 dout(7) << "mds_beacon " << *m
<< " has old seq, ignoring" << dendl
;
402 // Work out the latest epoch that this daemon should have seen
404 fs_cluster_id_t fscid
= fsmap
.mds_roles
.at(gid
);
405 if (fscid
== FS_CLUSTER_ID_NONE
) {
406 effective_epoch
= fsmap
.standby_epochs
.at(gid
);
408 effective_epoch
= fsmap
.get_filesystem(fscid
)->mds_map
.epoch
;
410 if (effective_epoch
!= m
->get_last_epoch_seen()) {
411 dout(10) << "mds_beacon " << *m
412 << " ignoring requested state, because mds hasn't seen latest map" << dendl
;
419 return false; // no longer laggy, need to update map.
421 if (state
== MDSMap::STATE_BOOT
) {
422 // ignore, already booted.
426 // did the join_fscid change
427 if (m
->get_fs().size()) {
428 fs_cluster_id_t fscid
= FS_CLUSTER_ID_NONE
;
429 auto f
= fsmap
.get_filesystem(m
->get_fs());
433 if (info
.join_fscid
!= fscid
) {
434 dout(10) << __func__
<< " standby mds_join_fs changed to " << fscid
435 << " (" << m
->get_fs() << ")" << dendl
;
440 if (info
.join_fscid
!= FS_CLUSTER_ID_NONE
) {
441 dout(10) << __func__
<< " standby mds_join_fs was cleared" << dendl
;
447 // is there a state change here?
448 if (info
.state
!= state
) {
449 // legal state change?
450 if ((info
.state
== MDSMap::STATE_STANDBY
||
451 info
.state
== MDSMap::STATE_STANDBY_REPLAY
) && state
> 0) {
452 dout(10) << "mds_beacon mds can't activate itself (" << ceph_mds_state_name(info
.state
)
453 << " -> " << ceph_mds_state_name(state
) << ")" << dendl
;
457 if ((state
== MDSMap::STATE_STANDBY
|| state
== MDSMap::STATE_STANDBY_REPLAY
)
458 && info
.rank
!= MDS_RANK_NONE
)
460 dout(4) << "mds_beacon MDS can't go back into standby after taking rank: "
461 "held rank " << info
.rank
<< " while requesting state "
462 << ceph_mds_state_name(state
) << dendl
;
470 // Comparing known daemon health with m->get_health()
471 // and return false (i.e. require proposal) if they
472 // do not match, to update our stored
473 if (!(pending_daemon_health
[gid
] == m
->get_health())) {
474 dout(10) << __func__
<< " health metrics for gid " << gid
<< " were updated" << dendl
;
480 // note time and reply
481 ceph_assert(effective_epoch
> 0);
484 auto beacon
= make_message
<MMDSBeacon
>(mon
->monmap
->fsid
,
485 m
->get_global_id(), m
->get_name(), effective_epoch
,
486 state
, seq
, CEPH_FEATURES_SUPPORTED_DEFAULT
);
487 mon
->send_reply(op
, beacon
.detach());
492 // I won't reply this beacon, drop it.
497 bool MDSMonitor::preprocess_offload_targets(MonOpRequestRef op
)
499 op
->mark_mdsmon_event(__func__
);
500 auto m
= op
->get_req
<MMDSLoadTargets
>();
501 dout(10) << "preprocess_offload_targets " << *m
<< " from " << m
->get_orig_source() << dendl
;
503 const auto &fsmap
= get_fsmap();
505 // check privileges, ignore message if fails
506 MonSession
*session
= op
->get_session();
509 if (!session
->is_capable("mds", MON_CAP_X
)) {
510 dout(0) << "preprocess_offload_targets got MMDSLoadTargets from entity with insufficient caps "
511 << session
->caps
<< dendl
;
515 if (fsmap
.gid_exists(m
->global_id
) &&
516 m
->targets
== fsmap
.get_info_gid(m
->global_id
).export_targets
)
527 bool MDSMonitor::prepare_update(MonOpRequestRef op
)
529 op
->mark_mdsmon_event(__func__
);
530 auto m
= op
->get_req
<PaxosServiceMessage
>();
531 dout(7) << "prepare_update " << *m
<< dendl
;
533 switch (m
->get_type()) {
536 return prepare_beacon(op
);
538 case MSG_MON_COMMAND
:
540 return prepare_command(op
);
541 } catch (const bad_cmd_get
& e
) {
543 mon
->reply_command(op
, -EINVAL
, e
.what(), bl
, get_last_committed());
547 case MSG_MDS_OFFLOAD_TARGETS
:
548 return prepare_offload_targets(op
);
557 bool MDSMonitor::prepare_beacon(MonOpRequestRef op
)
559 op
->mark_mdsmon_event(__func__
);
560 auto m
= op
->get_req
<MMDSBeacon
>();
561 // -- this is an update --
562 dout(12) << "prepare_beacon " << *m
<< " from " << m
->get_orig_source()
563 << " " << m
->get_orig_source_addrs() << dendl
;
564 entity_addrvec_t addrs
= m
->get_orig_source_addrs();
565 mds_gid_t gid
= m
->get_global_id();
566 MDSMap::DaemonState state
= m
->get_state();
567 version_t seq
= m
->get_seq();
569 auto &pending
= get_pending_fsmap_writeable();
571 dout(15) << __func__
<< " got health from gid " << gid
<< " with " << m
->get_health().metrics
.size() << " metrics." << dendl
;
573 // Calculate deltas of health metrics created and removed
574 // Do this by type rather than MDSHealthMetric equality, because messages can
575 // change a lot when they include e.g. a number of items.
576 const auto &old_health
= pending_daemon_health
[gid
].metrics
;
577 const auto &new_health
= m
->get_health().metrics
;
579 std::set
<mds_metric_t
> old_types
;
580 for (const auto &i
: old_health
) {
581 old_types
.insert(i
.type
);
584 std::set
<mds_metric_t
> new_types
;
585 for (const auto &i
: new_health
) {
586 new_types
.insert(i
.type
);
589 for (const auto &new_metric
: new_health
) {
590 if (old_types
.count(new_metric
.type
) == 0) {
591 dout(10) << "MDS health message (" << m
->get_orig_source()
592 << "): " << new_metric
.sev
<< " " << new_metric
.message
<< dendl
;
596 // Log the disappearance of health messages at INFO
597 for (const auto &old_metric
: old_health
) {
598 if (new_types
.count(old_metric
.type
) == 0) {
599 mon
->clog
->info() << "MDS health message cleared ("
600 << m
->get_orig_source() << "): " << old_metric
.message
;
605 pending_daemon_health
[gid
] = m
->get_health();
608 if (state
== MDSMap::STATE_BOOT
) {
609 // zap previous instance of this name?
610 if (g_conf()->mds_enforce_unique_name
) {
611 bool failed_mds
= false;
612 while (mds_gid_t existing
= pending
.find_mds_gid_by_name(m
->get_name())) {
613 if (!mon
->osdmon()->is_writeable()) {
614 mon
->osdmon()->wait_for_writeable(op
, new C_RetryMessage(this, op
));
617 const MDSMap::mds_info_t
&existing_info
=
618 pending
.get_info_gid(existing
);
619 mon
->clog
->info() << existing_info
.human_name() << " restarted";
620 fail_mds_gid(pending
, existing
);
624 ceph_assert(mon
->osdmon()->is_writeable());
625 request_proposal(mon
->osdmon());
629 // Add this daemon to the map
630 if (pending
.mds_roles
.count(gid
) == 0) {
631 MDSMap::mds_info_t new_info
;
632 new_info
.global_id
= gid
;
633 new_info
.name
= m
->get_name();
634 new_info
.addrs
= addrs
;
635 new_info
.mds_features
= m
->get_mds_features();
636 new_info
.state
= MDSMap::STATE_STANDBY
;
637 new_info
.state_seq
= seq
;
638 pending
.insert(new_info
);
639 if (m
->get_fs().size()) {
640 fs_cluster_id_t fscid
= FS_CLUSTER_ID_NONE
;
641 auto f
= pending
.get_filesystem(m
->get_fs());
645 new_info
.join_fscid
= fscid
;
649 // initialize the beacon timer
650 auto &beacon
= last_beacon
[gid
];
651 beacon
.stamp
= mono_clock::now();
655 if (!pending
.compat
.writeable(m
->get_compat())) {
656 dout(10) << " fsmap " << pending
.compat
657 << " can't write to new mds' " << m
->get_compat()
658 << ", updating fsmap and killing old mds's"
660 pending
.update_compat(m
->get_compat());
663 update_metadata(m
->get_global_id(), m
->get_sys_info());
667 if (!pending
.gid_exists(gid
)) {
668 /* gid has been removed from pending, send null map */
669 dout(5) << "mds_beacon " << *m
<< " is not in fsmap (state "
670 << ceph_mds_state_name(state
) << ")" << dendl
;
672 /* We can't send an MDSMap this MDS was a part of because we no longer
673 * know which FS it was part of. Nor does this matter. Sending an empty
674 * MDSMap is sufficient for getting the MDS to respawn.
676 wait_for_finished_proposal(op
, new LambdaContext([op
, this](int r
){
678 const auto& fsmap
= get_fsmap();
680 null_map
.epoch
= fsmap
.epoch
;
681 null_map
.compat
= fsmap
.compat
;
682 auto m
= make_message
<MMDSMap
>(mon
->monmap
->fsid
, null_map
);
683 mon
->send_reply(op
, m
.detach());
685 dispatch(op
); // try again
691 const auto& info
= pending
.get_info_gid(gid
);
692 if (info
.state
== MDSMap::STATE_STOPPING
&&
693 state
!= MDSMap::STATE_STOPPING
&&
694 state
!= MDSMap::STATE_STOPPED
) {
695 // we can't transition to any other states from STOPPING
696 dout(0) << "got beacon for MDS in STATE_STOPPING, ignoring requested state change"
703 dout(1) << "prepare_beacon clearing laggy flag on " << addrs
<< dendl
;
704 pending
.modify_daemon(info
.global_id
, [](auto& info
)
711 dout(5) << "prepare_beacon mds." << info
.rank
712 << " " << ceph_mds_state_name(info
.state
)
713 << " -> " << ceph_mds_state_name(state
)
716 fs_cluster_id_t fscid
= FS_CLUSTER_ID_NONE
;
717 if (m
->get_fs().size()) {
718 auto f
= pending
.get_filesystem(m
->get_fs());
723 pending
.modify_daemon(gid
, [fscid
](auto& info
) {
724 info
.join_fscid
= fscid
;
727 if (state
== MDSMap::STATE_STOPPED
) {
728 const auto fscid
= pending
.mds_roles
.at(gid
);
729 const auto &fs
= pending
.get_filesystem(fscid
);
731 mon
->clog
->info() << info
.human_name() << " finished "
732 << "stopping rank " << info
.rank
<< " in filesystem "
733 << fs
->mds_map
.fs_name
<< " (now has "
734 << fs
->mds_map
.get_num_in_mds() - 1 << " ranks)";
736 auto erased
= pending
.stop(gid
);
737 erased
.push_back(gid
);
739 for (const auto& erased_gid
: erased
) {
740 last_beacon
.erase(erased_gid
);
741 if (pending_daemon_health
.count(erased_gid
)) {
742 pending_daemon_health
.erase(erased_gid
);
743 pending_daemon_health_rm
.insert(erased_gid
);
748 } else if (state
== MDSMap::STATE_DAMAGED
) {
749 if (!mon
->osdmon()->is_writeable()) {
750 dout(1) << __func__
<< ": DAMAGED from rank " << info
.rank
751 << " waiting for osdmon writeable to blacklist it" << dendl
;
752 mon
->osdmon()->wait_for_writeable(op
, new C_RetryMessage(this, op
));
756 // Record this MDS rank as damaged, so that other daemons
757 // won't try to run it.
758 dout(0) << __func__
<< ": marking rank "
759 << info
.rank
<< " damaged" << dendl
;
761 utime_t until
= ceph_clock_now();
762 until
+= g_conf().get_val
<double>("mon_mds_blacklist_interval");
763 const auto blacklist_epoch
= mon
->osdmon()->blacklist(info
.addrs
, until
);
764 request_proposal(mon
->osdmon());
765 pending
.damaged(gid
, blacklist_epoch
);
766 last_beacon
.erase(gid
);
768 // Respond to MDS, so that it knows it can continue to shut down
769 auto beacon
= make_message
<MMDSBeacon
>(
770 mon
->monmap
->fsid
, m
->get_global_id(),
771 m
->get_name(), pending
.get_epoch(), state
, seq
,
772 CEPH_FEATURES_SUPPORTED_DEFAULT
);
773 mon
->send_reply(op
, beacon
.detach());
774 } else if (state
== MDSMap::STATE_DNE
) {
775 if (!mon
->osdmon()->is_writeable()) {
776 dout(1) << __func__
<< ": DNE from rank " << info
.rank
777 << " waiting for osdmon writeable to blacklist it" << dendl
;
778 mon
->osdmon()->wait_for_writeable(op
, new C_RetryMessage(this, op
));
782 fail_mds_gid(pending
, gid
);
783 ceph_assert(mon
->osdmon()->is_writeable());
784 request_proposal(mon
->osdmon());
786 // Respond to MDS, so that it knows it can continue to shut down
787 auto beacon
= make_message
<MMDSBeacon
>(mon
->monmap
->fsid
,
788 m
->get_global_id(), m
->get_name(), pending
.get_epoch(), state
, seq
,
789 CEPH_FEATURES_SUPPORTED_DEFAULT
);
790 mon
->send_reply(op
, beacon
.detach());
791 } else if (info
.state
== MDSMap::STATE_STANDBY
&& state
!= info
.state
) {
792 // Standby daemons should never modify their own
793 // state. Reject any attempts to do so.
794 derr
<< "standby " << gid
<< " attempted to change state to "
795 << ceph_mds_state_name(state
) << ", rejecting" << dendl
;
797 } else if (info
.state
!= MDSMap::STATE_STANDBY
&& state
!= info
.state
&&
798 !MDSMap::state_transition_valid(info
.state
, state
)) {
799 // Validate state transitions for daemons that hold a rank
800 derr
<< "daemon " << gid
<< " (rank " << info
.rank
<< ") "
801 << "reported invalid state transition "
802 << ceph_mds_state_name(info
.state
) << " -> "
803 << ceph_mds_state_name(state
) << dendl
;
806 if (info
.state
!= MDSMap::STATE_ACTIVE
&& state
== MDSMap::STATE_ACTIVE
) {
807 const auto &fscid
= pending
.mds_roles
.at(gid
);
808 const auto &fs
= pending
.get_filesystem(fscid
);
809 mon
->clog
->info() << info
.human_name() << " is now active in "
810 << "filesystem " << fs
->mds_map
.fs_name
<< " as rank "
814 // Made it through special cases and validations, record the
815 // daemon's reported state to the FSMap.
816 pending
.modify_daemon(gid
, [state
, seq
](auto& info
) {
818 info
.state_seq
= seq
;
823 dout(5) << "prepare_beacon pending map now:" << dendl
;
826 wait_for_finished_proposal(op
, new LambdaContext([op
, this](int r
){
828 _updated(op
); // success
829 else if (r
== -ECANCELED
) {
832 dispatch(op
); // try again
839 bool MDSMonitor::prepare_offload_targets(MonOpRequestRef op
)
841 auto &pending
= get_pending_fsmap_writeable();
843 op
->mark_mdsmon_event(__func__
);
844 auto m
= op
->get_req
<MMDSLoadTargets
>();
845 mds_gid_t gid
= m
->global_id
;
846 if (pending
.gid_has_rank(gid
)) {
847 dout(10) << "prepare_offload_targets " << gid
<< " " << m
->targets
<< dendl
;
848 pending
.update_export_targets(gid
, m
->targets
);
850 dout(10) << "prepare_offload_targets " << gid
<< " not in map" << dendl
;
856 bool MDSMonitor::should_propose(double& delay
)
858 // delegate to PaxosService to assess whether we should propose
859 return PaxosService::should_propose(delay
);
862 void MDSMonitor::_updated(MonOpRequestRef op
)
864 const auto &fsmap
= get_fsmap();
865 op
->mark_mdsmon_event(__func__
);
866 auto m
= op
->get_req
<MMDSBeacon
>();
867 dout(10) << "_updated " << m
->get_orig_source() << " " << *m
<< dendl
;
868 mon
->clog
->debug() << m
->get_orig_source() << " "
869 << m
->get_orig_source_addrs() << " "
870 << ceph_mds_state_name(m
->get_state());
872 if (m
->get_state() == MDSMap::STATE_STOPPED
) {
873 // send the map manually (they're out of the map, so they won't get it automatic)
875 null_map
.epoch
= fsmap
.epoch
;
876 null_map
.compat
= fsmap
.compat
;
877 auto m
= make_message
<MMDSMap
>(mon
->monmap
->fsid
, null_map
);
878 mon
->send_reply(op
, m
.detach());
880 auto beacon
= make_message
<MMDSBeacon
>(mon
->monmap
->fsid
,
881 m
->get_global_id(), m
->get_name(), fsmap
.get_epoch(),
882 m
->get_state(), m
->get_seq(), CEPH_FEATURES_SUPPORTED_DEFAULT
);
883 mon
->send_reply(op
, beacon
.detach());
887 void MDSMonitor::on_active()
892 mon
->clog
->debug() << "fsmap " << get_fsmap();
896 void MDSMonitor::dump_info(Formatter
*f
)
898 f
->open_object_section("fsmap");
902 f
->dump_unsigned("mdsmap_first_committed", get_first_committed());
903 f
->dump_unsigned("mdsmap_last_committed", get_last_committed());
906 bool MDSMonitor::preprocess_command(MonOpRequestRef op
)
908 op
->mark_mdsmon_event(__func__
);
909 auto m
= op
->get_req
<MMonCommand
>();
914 const auto &fsmap
= get_fsmap();
917 if (!cmdmap_from_json(m
->cmd
, &cmdmap
, ss
)) {
918 // ss has reason for failure
919 string rs
= ss
.str();
920 mon
->reply_command(op
, -EINVAL
, rs
, rdata
, get_last_committed());
925 cmd_getval(cmdmap
, "prefix", prefix
);
927 cmd_getval(cmdmap
, "format", format
, string("plain"));
928 std::unique_ptr
<Formatter
> f(Formatter::create(format
));
930 MonSession
*session
= op
->get_session();
932 mon
->reply_command(op
, -EACCES
, "access denied", rdata
, get_last_committed());
936 if (prefix
== "mds stat") {
938 f
->open_object_section("mds_stat");
946 } else if (prefix
== "mds ok-to-stop") {
948 if (!cmd_getval(cmdmap
, "ids", ids
)) {
950 ss
<< "must specify mds id";
953 if (fsmap
.is_any_degraded()) {
954 ss
<< "one or more filesystems is currently degraded";
958 set
<mds_gid_t
> stopping
;
959 for (auto& id
: ids
) {
961 mds_gid_t gid
= gid_from_arg(fsmap
, id
, ess
);
962 if (gid
== MDS_GID_NONE
) {
963 // the mds doesn't exist, but no file systems are unhappy, so losing it
964 // can't have any effect.
967 stopping
.insert(gid
);
969 set
<mds_gid_t
> active
;
970 set
<mds_gid_t
> standby
;
971 for (auto gid
: stopping
) {
972 if (fsmap
.gid_has_rank(gid
)) {
973 // ignore standby-replay daemons (at this level)
974 if (!fsmap
.is_standby_replay(gid
)) {
975 auto standby
= fsmap
.get_standby_replay(gid
);
976 if (standby
== MDS_GID_NONE
||
977 stopping
.count(standby
)) {
978 // no standby-replay, or we're also stopping the standby-replay
984 // net loss of a standby
988 if (fsmap
.get_num_standby() - standby
.size() < active
.size()) {
990 ss
<< "insufficent standby MDS daemons to stop active gids "
992 << " and/or standby gids " << stringify(standby
);;
996 ss
<< "should be safe to stop " << ids
;
997 } else if (prefix
== "fs dump") {
1001 const FSMap
*fsmapp
= &fsmap
;
1003 if (cmd_getval(cmdmap
, "epoch", epocharg
)) {
1006 int err
= get_version(epoch
, b
);
1007 if (err
== -ENOENT
) {
1011 ceph_assert(err
== 0);
1012 ceph_assert(b
.length());
1020 f
->open_object_section("fsmap");
1021 fsmapp
->dump(f
.get());
1031 ss
<< "dumped fsmap epoch " << fsmapp
->get_epoch();
1032 } else if (prefix
== "mds metadata") {
1034 f
.reset(Formatter::create("json-pretty"));
1037 bool all
= !cmd_getval(cmdmap
, "who", who
);
1038 dout(1) << "all = " << all
<< dendl
;
1041 // Dump all MDSs' metadata
1042 const auto all_info
= fsmap
.get_mds_info();
1044 f
->open_array_section("mds_metadata");
1045 for(const auto &i
: all_info
) {
1046 const auto &info
= i
.second
;
1048 f
->open_object_section("mds");
1049 f
->dump_string("name", info
.name
);
1050 std::ostringstream get_err
;
1051 r
= dump_metadata(fsmap
, info
.name
, f
.get(), get_err
);
1052 if (r
== -EINVAL
|| r
== -ENOENT
) {
1053 // Drop error, list what metadata we do have
1054 dout(1) << get_err
.str() << dendl
;
1056 } else if (r
!= 0) {
1057 derr
<< "Unexpected error reading metadata: " << cpp_strerror(r
)
1059 ss
<< get_err
.str();
1067 // Dump a single daemon's metadata
1068 f
->open_object_section("mds_metadata");
1069 r
= dump_metadata(fsmap
, who
, f
.get(), ss
);
1073 } else if (prefix
== "mds versions") {
1075 f
.reset(Formatter::create("json-pretty"));
1076 count_metadata("ceph_version", f
.get());
1079 } else if (prefix
== "mds count-metadata") {
1081 f
.reset(Formatter::create("json-pretty"));
1083 cmd_getval(cmdmap
, "property", field
);
1084 count_metadata(field
, f
.get());
1087 } else if (prefix
== "mds compat show") {
1089 f
->open_object_section("mds_compat");
1090 fsmap
.compat
.dump(f
.get());
1097 } else if (prefix
== "fs get") {
1099 cmd_getval(cmdmap
, "fs_name", fs_name
);
1100 const auto &fs
= fsmap
.get_filesystem(fs_name
);
1101 if (fs
== nullptr) {
1102 ss
<< "filesystem '" << fs_name
<< "' not found";
1106 f
->open_object_section("filesystem");
1116 } else if (prefix
== "fs ls") {
1118 f
->open_array_section("filesystems");
1119 for (const auto &p
: fsmap
.filesystems
) {
1120 const auto &fs
= p
.second
;
1121 f
->open_object_section("filesystem");
1123 const MDSMap
&mds_map
= fs
->mds_map
;
1124 f
->dump_string("name", mds_map
.fs_name
);
1125 /* Output both the names and IDs of pools, for use by
1126 * humans and machines respectively */
1127 f
->dump_string("metadata_pool", mon
->osdmon()->osdmap
.get_pool_name(
1128 mds_map
.metadata_pool
));
1129 f
->dump_int("metadata_pool_id", mds_map
.metadata_pool
);
1130 f
->open_array_section("data_pool_ids");
1131 for (const auto &id
: mds_map
.data_pools
) {
1132 f
->dump_int("data_pool_id", id
);
1136 f
->open_array_section("data_pools");
1137 for (const auto &id
: mds_map
.data_pools
) {
1138 const auto &name
= mon
->osdmon()->osdmap
.get_pool_name(id
);
1139 f
->dump_string("data_pool", name
);
1148 for (const auto &p
: fsmap
.filesystems
) {
1149 const auto &fs
= p
.second
;
1150 const MDSMap
&mds_map
= fs
->mds_map
;
1151 const string
&md_pool_name
= mon
->osdmon()->osdmap
.get_pool_name(
1152 mds_map
.metadata_pool
);
1154 ds
<< "name: " << mds_map
.fs_name
<< ", metadata pool: "
1155 << md_pool_name
<< ", data pools: [";
1156 for (const auto &id
: mds_map
.data_pools
) {
1157 const string
&pool_name
= mon
->osdmon()->osdmap
.get_pool_name(id
);
1158 ds
<< pool_name
<< " ";
1160 ds
<< "]" << std::endl
;
1163 if (fsmap
.filesystems
.empty()) {
1164 ds
<< "No filesystems enabled" << std::endl
;
1175 mon
->reply_command(op
, r
, rs
, rdata
, get_last_committed());
1181 bool MDSMonitor::fail_mds_gid(FSMap
&fsmap
, mds_gid_t gid
)
1183 const auto& info
= fsmap
.get_info_gid(gid
);
1184 dout(1) << "fail_mds_gid " << gid
<< " mds." << info
.name
<< " role " << info
.rank
<< dendl
;
1186 ceph_assert(mon
->osdmon()->is_writeable());
1188 epoch_t blacklist_epoch
= 0;
1189 if (info
.rank
>= 0 && info
.state
!= MDSMap::STATE_STANDBY_REPLAY
) {
1190 utime_t until
= ceph_clock_now();
1191 until
+= g_conf().get_val
<double>("mon_mds_blacklist_interval");
1192 blacklist_epoch
= mon
->osdmon()->blacklist(info
.addrs
, until
);
1195 fsmap
.erase(gid
, blacklist_epoch
);
1196 last_beacon
.erase(gid
);
1197 if (pending_daemon_health
.count(gid
)) {
1198 pending_daemon_health
.erase(gid
);
1199 pending_daemon_health_rm
.insert(gid
);
1202 return blacklist_epoch
!= 0;
1205 mds_gid_t
MDSMonitor::gid_from_arg(const FSMap
&fsmap
, const std::string
&arg
, std::ostream
&ss
)
1207 // Try parsing as a role
1209 std::ostringstream ignore_err
; // Don't spam 'ss' with parse_role errors
1210 int r
= fsmap
.parse_role(arg
, &role
, ignore_err
);
1212 // See if a GID is assigned to this role
1213 const auto &fs
= fsmap
.get_filesystem(role
.fscid
);
1214 ceph_assert(fs
!= nullptr); // parse_role ensures it exists
1215 if (fs
->mds_map
.is_up(role
.rank
)) {
1216 dout(10) << __func__
<< ": validated rank/GID " << role
1217 << " as a rank" << dendl
;
1218 return fs
->mds_map
.get_mds_info(role
.rank
).global_id
;
1222 // Try parsing as a gid
1224 unsigned long long maybe_gid
= strict_strtoll(arg
.c_str(), 10, &err
);
1226 // Not a role or a GID, try as a daemon name
1227 const MDSMap::mds_info_t
*mds_info
= fsmap
.find_by_name(arg
);
1229 ss
<< "MDS named '" << arg
1230 << "' does not exist, or is not up";
1231 return MDS_GID_NONE
;
1233 dout(10) << __func__
<< ": resolved MDS name '" << arg
1234 << "' to GID " << mds_info
->global_id
<< dendl
;
1235 return mds_info
->global_id
;
1237 // Not a role, but parses as a an integer, might be a GID
1238 dout(10) << __func__
<< ": treating MDS reference '" << arg
1239 << "' as an integer " << maybe_gid
<< dendl
;
1241 if (fsmap
.gid_exists(mds_gid_t(maybe_gid
))) {
1242 return mds_gid_t(maybe_gid
);
1246 dout(1) << __func__
<< ": rank/GID " << arg
1247 << " not a existent rank or GID" << dendl
;
1248 return MDS_GID_NONE
;
1251 int MDSMonitor::fail_mds(FSMap
&fsmap
, std::ostream
&ss
,
1252 const std::string
&arg
, MDSMap::mds_info_t
*failed_info
)
1254 ceph_assert(failed_info
!= nullptr);
1256 mds_gid_t gid
= gid_from_arg(fsmap
, arg
, ss
);
1257 if (gid
== MDS_GID_NONE
) {
1260 if (!mon
->osdmon()->is_writeable()) {
1264 // Take a copy of the info before removing the MDS from the map,
1265 // so that the caller knows which mds (if any) they ended up removing.
1266 *failed_info
= fsmap
.get_info_gid(gid
);
1268 fail_mds_gid(fsmap
, gid
);
1269 ss
<< "failed mds gid " << gid
;
1270 ceph_assert(mon
->osdmon()->is_writeable());
1271 request_proposal(mon
->osdmon());
1275 bool MDSMonitor::prepare_command(MonOpRequestRef op
)
1277 op
->mark_mdsmon_event(__func__
);
1278 auto m
= op
->get_req
<MMonCommand
>();
1284 if (!cmdmap_from_json(m
->cmd
, &cmdmap
, ss
)) {
1285 string rs
= ss
.str();
1286 mon
->reply_command(op
, -EINVAL
, rs
, rdata
, get_last_committed());
1291 cmd_getval(cmdmap
, "prefix", prefix
);
1293 /* Refuse access if message not associated with a valid session */
1294 MonSession
*session
= op
->get_session();
1296 mon
->reply_command(op
, -EACCES
, "access denied", rdata
, get_last_committed());
1300 auto &pending
= get_pending_fsmap_writeable();
1302 bool batched_propose
= false;
1303 for (const auto &h
: handlers
) {
1304 if (h
->can_handle(prefix
)) {
1305 batched_propose
= h
->batched_propose();
1306 if (batched_propose
) {
1309 r
= h
->handle(mon
, pending
, op
, cmdmap
, ss
);
1310 if (batched_propose
) {
1315 // message has been enqueued for retry; return.
1316 dout(4) << __func__
<< " enqueue for retry by prepare_command" << dendl
;
1320 // On successful updates, print the updated map
1323 // Successful or not, we're done: respond.
1329 r
= filesystem_command(pending
, op
, prefix
, cmdmap
, ss
);
1332 } else if (r
== -EAGAIN
) {
1333 // Do not reply, the message has been enqueued for retry
1334 dout(4) << __func__
<< " enqueue for retry by filesystem_command" << dendl
;
1336 } else if (r
!= -ENOSYS
) {
1340 if (r
== -ENOSYS
&& ss
.str().empty()) {
1341 ss
<< "unrecognized command";
1345 dout(4) << __func__
<< " done, r=" << r
<< dendl
;
1346 /* Compose response */
1351 // success.. delay reply
1352 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, r
, rs
,
1353 get_last_committed() + 1));
1354 if (batched_propose
) {
1355 force_immediate_propose();
1359 // reply immediately
1360 mon
->reply_command(op
, r
, rs
, rdata
, get_last_committed());
1365 int MDSMonitor::filesystem_command(
1368 std::string
const &prefix
,
1369 const cmdmap_t
& cmdmap
,
1370 std::stringstream
&ss
)
1372 dout(4) << __func__
<< " prefix='" << prefix
<< "'" << dendl
;
1373 op
->mark_mdsmon_event(__func__
);
1376 cmd_getval(cmdmap
, "role", whostr
);
1378 if (prefix
== "mds set_state") {
1380 if (!cmd_getval(cmdmap
, "gid", gid
)) {
1381 ss
<< "error parsing 'gid' value '"
1382 << cmd_vartype_stringify(cmdmap
.at("gid")) << "'";
1385 MDSMap::DaemonState state
;
1386 if (!cmd_getval(cmdmap
, "state", state
)) {
1387 ss
<< "error parsing 'state' string value '"
1388 << cmd_vartype_stringify(cmdmap
.at("state")) << "'";
1391 if (fsmap
.gid_exists(gid
)) {
1392 fsmap
.modify_daemon(gid
, [state
](auto& info
) {
1395 ss
<< "set mds gid " << gid
<< " to state " << state
<< " "
1396 << ceph_mds_state_name(state
);
1399 } else if (prefix
== "mds fail") {
1401 cmd_getval(cmdmap
, "role_or_gid", who
);
1403 MDSMap::mds_info_t failed_info
;
1404 r
= fail_mds(fsmap
, ss
, who
, &failed_info
);
1405 if (r
< 0 && r
== -EAGAIN
) {
1406 mon
->osdmon()->wait_for_writeable(op
, new C_RetryMessage(this, op
));
1407 return -EAGAIN
; // don't propose yet; wait for message to be retried
1408 } else if (r
== 0) {
1409 // Only log if we really did something (not when was already gone)
1410 if (failed_info
.global_id
!= MDS_GID_NONE
) {
1411 mon
->clog
->info() << failed_info
.human_name() << " marked failed by "
1412 << op
->get_session()->entity_name
;
1415 } else if (prefix
== "mds rm") {
1417 if (!cmd_getval(cmdmap
, "gid", gid
)) {
1418 ss
<< "error parsing 'gid' value '"
1419 << cmd_vartype_stringify(cmdmap
.at("gid")) << "'";
1422 if (!fsmap
.gid_exists(gid
)) {
1423 ss
<< "mds gid " << gid
<< " does not exist";
1426 const auto &info
= fsmap
.get_info_gid(gid
);
1427 MDSMap::DaemonState state
= info
.state
;
1429 ss
<< "cannot remove active mds." << info
.name
1430 << " rank " << info
.rank
;
1433 fsmap
.erase(gid
, {});
1434 ss
<< "removed mds gid " << gid
;
1438 } else if (prefix
== "mds rmfailed") {
1439 bool confirm
= false;
1440 cmd_getval(cmdmap
, "yes_i_really_mean_it", confirm
);
1442 ss
<< "WARNING: this can make your filesystem inaccessible! "
1443 "Add --yes-i-really-mean-it if you are sure you wish to continue.";
1447 std::string role_str
;
1448 cmd_getval(cmdmap
, "role", role_str
);
1450 int r
= fsmap
.parse_role(role_str
, &role
, ss
);
1452 ss
<< "invalid role '" << role_str
<< "'";
1456 fsmap
.modify_filesystem(
1458 [role
](std::shared_ptr
<Filesystem
> fs
)
1460 fs
->mds_map
.failed
.erase(role
.rank
);
1463 ss
<< "removed failed mds." << role
;
1465 } else if (prefix
== "mds compat rm_compat") {
1467 if (!cmd_getval(cmdmap
, "feature", f
)) {
1468 ss
<< "error parsing feature value '"
1469 << cmd_vartype_stringify(cmdmap
.at("feature")) << "'";
1472 if (fsmap
.compat
.compat
.contains(f
)) {
1473 ss
<< "removing compat feature " << f
;
1474 CompatSet modified
= fsmap
.compat
;
1475 modified
.compat
.remove(f
);
1476 fsmap
.update_compat(modified
);
1478 ss
<< "compat feature " << f
<< " not present in " << fsmap
.compat
;
1481 } else if (prefix
== "mds compat rm_incompat") {
1483 if (!cmd_getval(cmdmap
, "feature", f
)) {
1484 ss
<< "error parsing feature value '"
1485 << cmd_vartype_stringify(cmdmap
.at("feature")) << "'";
1488 if (fsmap
.compat
.incompat
.contains(f
)) {
1489 ss
<< "removing incompat feature " << f
;
1490 CompatSet modified
= fsmap
.compat
;
1491 modified
.incompat
.remove(f
);
1492 fsmap
.update_compat(modified
);
1494 ss
<< "incompat feature " << f
<< " not present in " << fsmap
.compat
;
1497 } else if (prefix
== "mds repaired") {
1498 std::string role_str
;
1499 cmd_getval(cmdmap
, "role", role_str
);
1501 r
= fsmap
.parse_role(role_str
, &role
, ss
);
1506 bool modified
= fsmap
.undamaged(role
.fscid
, role
.rank
);
1508 ss
<< "repaired: restoring rank " << role
;
1510 ss
<< "nothing to do: rank is not damaged";
1514 } else if (prefix
== "mds freeze") {
1516 cmd_getval(cmdmap
, "role_or_gid", who
);
1517 mds_gid_t gid
= gid_from_arg(fsmap
, who
, ss
);
1518 if (gid
== MDS_GID_NONE
) {
1522 bool freeze
= false;
1525 cmd_getval(cmdmap
, "val", str
);
1526 if ((r
= parse_bool(str
, &freeze
, ss
)) != 0) {
1531 auto f
= [freeze
,gid
,&ss
](auto& info
) {
1533 ss
<< "freezing mds." << gid
;
1536 ss
<< "unfreezing mds." << gid
;
1540 fsmap
.modify_daemon(gid
, f
);
1549 void MDSMonitor::check_subs()
1551 // Subscriptions may be to "mdsmap" (MDS and legacy clients),
1552 // "mdsmap.<namespace>", or to "fsmap" for the full state of all
1553 // filesystems. Build a list of all the types we service
1554 // subscriptions for.
1556 std::vector
<std::string
> types
= {
1562 for (const auto &p
: get_fsmap().filesystems
) {
1563 const auto &fscid
= p
.first
;
1564 CachedStackStringStream cos
;
1565 *cos
<< "mdsmap." << fscid
;
1566 types
.push_back(std::string(cos
->strv()));
1569 for (const auto &type
: types
) {
1570 auto& subs
= mon
->session_map
.subs
;
1571 auto subs_it
= subs
.find(type
);
1572 if (subs_it
== subs
.end())
1574 auto sub_it
= subs_it
->second
->begin();
1575 while (!sub_it
.end()) {
1577 ++sub_it
; // N.B. check_sub may remove sub!
1584 void MDSMonitor::check_sub(Subscription
*sub
)
1586 dout(20) << __func__
<< ": " << sub
->type
<< dendl
;
1588 const auto &fsmap
= get_fsmap();
1590 if (sub
->type
== "fsmap") {
1591 if (sub
->next
<= fsmap
.get_epoch()) {
1592 sub
->session
->con
->send_message(new MFSMap(mon
->monmap
->fsid
, fsmap
));
1594 mon
->session_map
.remove_sub(sub
);
1596 sub
->next
= fsmap
.get_epoch() + 1;
1599 } else if (sub
->type
== "fsmap.user") {
1600 if (sub
->next
<= fsmap
.get_epoch()) {
1602 fsmap_u
.epoch
= fsmap
.get_epoch();
1603 fsmap_u
.legacy_client_fscid
= fsmap
.legacy_client_fscid
;
1604 for (const auto &p
: fsmap
.filesystems
) {
1605 FSMapUser::fs_info_t
& fs_info
= fsmap_u
.filesystems
[p
.second
->fscid
];
1606 fs_info
.cid
= p
.second
->fscid
;
1607 fs_info
.name
= p
.second
->mds_map
.fs_name
;
1609 sub
->session
->con
->send_message(new MFSMapUser(mon
->monmap
->fsid
, fsmap_u
));
1611 mon
->session_map
.remove_sub(sub
);
1613 sub
->next
= fsmap
.get_epoch() + 1;
1616 } else if (sub
->type
.compare(0, 6, "mdsmap") == 0) {
1617 if (sub
->next
> fsmap
.get_epoch()) {
1621 const bool is_mds
= sub
->session
->name
.is_mds();
1622 mds_gid_t mds_gid
= MDS_GID_NONE
;
1623 fs_cluster_id_t fscid
= FS_CLUSTER_ID_NONE
;
1625 // What (if any) namespace are you assigned to?
1626 auto mds_info
= fsmap
.get_mds_info();
1627 for (const auto &p
: mds_info
) {
1628 if (p
.second
.addrs
== sub
->session
->addrs
) {
1630 fscid
= fsmap
.mds_roles
.at(mds_gid
);
1634 // You're a client. Did you request a particular
1636 if (sub
->type
.compare(0, 7, "mdsmap.") == 0) {
1637 auto namespace_id_str
= sub
->type
.substr(std::string("mdsmap.").size());
1638 dout(10) << __func__
<< ": namespace_id " << namespace_id_str
<< dendl
;
1640 fscid
= strict_strtoll(namespace_id_str
.c_str(), 10, &err
);
1642 // Client asked for a non-existent namespace, send them nothing
1643 dout(1) << "Invalid client subscription '" << sub
->type
1647 if (fsmap
.filesystems
.count(fscid
) == 0) {
1648 // Client asked for a non-existent namespace, send them nothing
1649 // TODO: something more graceful for when a client has a filesystem
1650 // mounted, and the fileysstem is deleted. Add a "shut down you fool"
1652 dout(1) << "Client subscribed to non-existent namespace '" <<
1653 fscid
<< "'" << dendl
;
1657 // Unqualified request for "mdsmap": give it the one marked
1658 // for use by legacy clients.
1659 if (fsmap
.legacy_client_fscid
!= FS_CLUSTER_ID_NONE
) {
1660 fscid
= fsmap
.legacy_client_fscid
;
1662 dout(1) << "Client subscribed for legacy filesystem but "
1663 "none is configured" << dendl
;
1668 dout(10) << __func__
<< ": is_mds=" << is_mds
<< ", fscid= " << fscid
<< dendl
;
1670 // Work out the effective latest epoch
1671 const MDSMap
*mds_map
= nullptr;
1673 null_map
.compat
= fsmap
.compat
;
1674 if (fscid
== FS_CLUSTER_ID_NONE
) {
1675 // For a client, we should have already dropped out
1676 ceph_assert(is_mds
);
1678 auto it
= fsmap
.standby_daemons
.find(mds_gid
);
1679 if (it
!= fsmap
.standby_daemons
.end()) {
1680 // For an MDS, we need to feed it an MDSMap with its own state in
1681 null_map
.mds_info
[mds_gid
] = it
->second
;
1682 null_map
.epoch
= fsmap
.standby_epochs
.at(mds_gid
);
1684 null_map
.epoch
= fsmap
.epoch
;
1686 mds_map
= &null_map
;
1688 // Check the effective epoch
1689 mds_map
= &fsmap
.get_filesystem(fscid
)->mds_map
;
1692 ceph_assert(mds_map
!= nullptr);
1693 dout(10) << __func__
<< " selected MDS map epoch " <<
1694 mds_map
->epoch
<< " for namespace " << fscid
<< " for subscriber "
1695 << sub
->session
->name
<< " who wants epoch " << sub
->next
<< dendl
;
1697 if (sub
->next
> mds_map
->epoch
) {
1700 auto msg
= make_message
<MMDSMap
>(mon
->monmap
->fsid
, *mds_map
);
1702 sub
->session
->con
->send_message(msg
.detach());
1704 mon
->session_map
.remove_sub(sub
);
1706 sub
->next
= mds_map
->get_epoch() + 1;
1712 void MDSMonitor::update_metadata(mds_gid_t gid
,
1713 const map
<string
, string
>& metadata
)
1715 if (metadata
.empty()) {
1718 pending_metadata
[gid
] = metadata
;
1720 MonitorDBStore::TransactionRef t
= paxos
->get_pending_transaction();
1722 encode(pending_metadata
, bl
);
1723 t
->put(MDS_METADATA_PREFIX
, "last_metadata", bl
);
1724 paxos
->trigger_propose();
1727 void MDSMonitor::remove_from_metadata(const FSMap
&fsmap
, MonitorDBStore::TransactionRef t
)
1729 bool update
= false;
1730 for (auto it
= pending_metadata
.begin(); it
!= pending_metadata
.end(); ) {
1731 if (!fsmap
.gid_exists(it
->first
)) {
1732 it
= pending_metadata
.erase(it
);
1741 encode(pending_metadata
, bl
);
1742 t
->put(MDS_METADATA_PREFIX
, "last_metadata", bl
);
1745 int MDSMonitor::load_metadata(map
<mds_gid_t
, Metadata
>& m
)
1748 int r
= mon
->store
->get(MDS_METADATA_PREFIX
, "last_metadata", bl
);
1750 dout(5) << "Unable to load 'last_metadata'" << dendl
;
1754 auto it
= bl
.cbegin();
1755 ceph::decode(m
, it
);
1759 void MDSMonitor::count_metadata(const std::string
&field
, map
<string
,int> *out
)
1761 map
<mds_gid_t
,Metadata
> meta
;
1762 load_metadata(meta
);
1763 for (auto& p
: meta
) {
1764 auto q
= p
.second
.find(field
);
1765 if (q
== p
.second
.end()) {
1766 (*out
)["unknown"]++;
1768 (*out
)[q
->second
]++;
1773 void MDSMonitor::count_metadata(const std::string
&field
, Formatter
*f
)
1775 map
<string
,int> by_val
;
1776 count_metadata(field
, &by_val
);
1777 f
->open_object_section(field
.c_str());
1778 for (auto& p
: by_val
) {
1779 f
->dump_int(p
.first
.c_str(), p
.second
);
1784 int MDSMonitor::dump_metadata(const FSMap
& fsmap
, const std::string
&who
,
1785 Formatter
*f
, ostream
& err
)
1789 mds_gid_t gid
= gid_from_arg(fsmap
, who
, err
);
1790 if (gid
== MDS_GID_NONE
) {
1794 map
<mds_gid_t
, Metadata
> metadata
;
1795 if (int r
= load_metadata(metadata
)) {
1796 err
<< "Unable to load 'last_metadata'";
1800 if (!metadata
.count(gid
)) {
1803 const Metadata
& m
= metadata
[gid
];
1804 for (Metadata::const_iterator p
= m
.begin(); p
!= m
.end(); ++p
) {
1805 f
->dump_string(p
->first
.c_str(), p
->second
);
1810 int MDSMonitor::print_nodes(Formatter
*f
)
1814 const auto &fsmap
= get_fsmap();
1816 map
<mds_gid_t
, Metadata
> metadata
;
1817 if (int r
= load_metadata(metadata
)) {
1821 map
<string
, list
<string
> > mdses
; // hostname => mds
1822 for (const auto &p
: metadata
) {
1823 const mds_gid_t
& gid
= p
.first
;
1824 const Metadata
& m
= p
.second
;
1825 Metadata::const_iterator hostname
= m
.find("hostname");
1826 if (hostname
== m
.end()) {
1827 // not likely though
1830 if (!fsmap
.gid_exists(gid
)) {
1831 dout(5) << __func__
<< ": GID " << gid
<< " not existent" << dendl
;
1834 const MDSMap::mds_info_t
& mds_info
= fsmap
.get_info_gid(gid
);
1835 mdses
[hostname
->second
].push_back(mds_info
.name
);
1838 dump_services(f
, mdses
, "mds");
1843 * If a cluster is undersized (with respect to max_mds), then
1844 * attempt to find daemons to grow it. If the cluster is oversized
1845 * (with respect to max_mds) then shrink it by stopping its highest rank.
1847 bool MDSMonitor::maybe_resize_cluster(FSMap
&fsmap
, fs_cluster_id_t fscid
)
1849 auto ¤t_mds_map
= get_fsmap().get_filesystem(fscid
)->mds_map
;
1850 auto&& fs
= fsmap
.get_filesystem(fscid
);
1851 auto &mds_map
= fs
->mds_map
;
1853 int in
= mds_map
.get_num_in_mds();
1854 int max
= mds_map
.get_max_mds();
1856 dout(20) << __func__
<< " in " << in
<< " max " << max
<< dendl
;
1858 /* Check that both the current epoch mds_map is resizeable as well as the
1859 * current batch of changes in pending. This is important if an MDS is
1860 * becoming active in the next epoch.
1862 if (!current_mds_map
.is_resizeable() ||
1863 !mds_map
.is_resizeable()) {
1864 dout(5) << __func__
<< " mds_map is not currently resizeable" << dendl
;
1868 if (in
< max
&& !mds_map
.test_flag(CEPH_MDSMAP_NOT_JOINABLE
)) {
1869 mds_rank_t mds
= mds_rank_t(0);
1870 while (mds_map
.is_in(mds
)) {
1873 auto info
= fsmap
.find_replacement_for({fscid
, mds
});
1878 dout(1) << "assigned standby " << info
->addrs
1879 << " as mds." << mds
<< dendl
;
1880 mon
->clog
->info() << info
->human_name() << " assigned to "
1881 "filesystem " << mds_map
.fs_name
<< " as rank "
1882 << mds
<< " (now has " << mds_map
.get_num_in_mds() + 1
1884 fsmap
.promote(info
->global_id
, *fs
, mds
);
1886 } else if (in
> max
) {
1887 mds_rank_t target
= in
- 1;
1888 const auto &info
= mds_map
.get_info(target
);
1889 if (mds_map
.is_active(target
)) {
1890 dout(1) << "stopping " << target
<< dendl
;
1891 mon
->clog
->info() << "stopping " << info
.human_name();
1892 auto f
= [](auto& info
) {
1893 info
.state
= MDSMap::STATE_STOPPING
;
1895 fsmap
.modify_daemon(info
.global_id
, f
);
1898 dout(20) << "skipping stop of " << target
<< dendl
;
1908 * Fail a daemon and replace it with a suitable standby.
1910 bool MDSMonitor::drop_mds(FSMap
&fsmap
, mds_gid_t gid
, const mds_info_t
* rep_info
, bool *osd_propose
)
1912 ceph_assert(osd_propose
!= nullptr);
1914 const auto fscid
= fsmap
.mds_roles
.at(gid
);
1915 const auto& info
= fsmap
.get_info_gid(gid
);
1916 const auto rank
= info
.rank
;
1917 const auto state
= info
.state
;
1919 if (info
.is_frozen()) {
1921 } else if (state
== MDSMap::STATE_STANDBY_REPLAY
||
1922 state
== MDSMap::STATE_STANDBY
) {
1923 dout(1) << " failing and removing standby " << gid
<< " " << info
.addrs
1925 << "." << info
.inc
<< " " << ceph_mds_state_name(state
)
1927 *osd_propose
|= fail_mds_gid(fsmap
, gid
);
1929 } else if (rank
>= 0 && rep_info
) {
1930 auto fs
= fsmap
.filesystems
.at(fscid
);
1931 if (fs
->mds_map
.test_flag(CEPH_MDSMAP_NOT_JOINABLE
)) {
1935 // and is there a non-laggy standby that can take over for us?
1936 dout(1) << " replacing " << gid
<< " " << info
.addrs
1937 << " mds." << rank
<< "." << info
.inc
1938 << " " << ceph_mds_state_name(state
)
1939 << " with " << rep_info
->global_id
<< "/" << rep_info
->name
<< " " << rep_info
->addrs
1942 mon
->clog
->warn() << "Replacing " << info
.human_name()
1943 << " as rank " << rank
1944 << " with standby " << rep_info
->human_name();
1946 // Remove the old one
1947 *osd_propose
|= fail_mds_gid(fsmap
, gid
);
1949 // Promote the replacement
1950 fsmap
.promote(rep_info
->global_id
, *fs
, rank
);
1957 bool MDSMonitor::check_health(FSMap
& fsmap
, bool* propose_osdmap
)
1959 bool do_propose
= false;
1960 const auto now
= mono_clock::now();
1961 const bool osdmap_writeable
= mon
->osdmon()->is_writeable();
1962 const auto mds_beacon_grace
= g_conf().get_val
<double>("mds_beacon_grace");
1963 const auto mds_beacon_interval
= g_conf().get_val
<double>("mds_beacon_interval");
1965 if (mono_clock::is_zero(last_tick
)) {
1970 auto since_last
= std::chrono::duration
<double>(now
-last_tick
);
1972 if (since_last
.count() > (mds_beacon_grace
-mds_beacon_interval
)) {
1973 // This case handles either local slowness (calls being delayed
1974 // for whatever reason) or cluster election slowness (a long gap
1975 // between calls while an election happened)
1976 dout(1) << __func__
<< ": resetting beacon timeouts due to mon delay "
1977 "(slow election?) of " << since_last
.count() << " seconds" << dendl
;
1978 for (auto& p
: last_beacon
) {
1979 p
.second
.stamp
= now
;
1984 // make sure last_beacon is fully populated
1985 for (auto& p
: fsmap
.mds_roles
) {
1986 auto& gid
= p
.first
;
1987 last_beacon
.emplace(std::piecewise_construct
,
1988 std::forward_as_tuple(gid
),
1989 std::forward_as_tuple(now
, 0));
1992 // We will only take decisive action (replacing/removing a daemon)
1993 // if we have some indication that some other daemon(s) are successfully
1994 // getting beacons through recently.
1995 mono_time latest_beacon
= mono_clock::zero();
1996 for (const auto& p
: last_beacon
) {
1997 latest_beacon
= std::max(p
.second
.stamp
, latest_beacon
);
1999 auto since
= chrono::duration
<double>(now
-latest_beacon
);
2000 const bool may_replace
= since
.count() <
2001 std::max(g_conf()->mds_beacon_interval
, g_conf()->mds_beacon_grace
* 0.5);
2003 // check beacon timestamps
2004 std::vector
<mds_gid_t
> to_remove
;
2005 for (auto it
= last_beacon
.begin(); it
!= last_beacon
.end(); ) {
2006 auto& [gid
, beacon_info
] = *it
;
2007 auto since_last
= chrono::duration
<double>(now
-beacon_info
.stamp
);
2009 if (!fsmap
.gid_exists(gid
)) {
2010 // gid no longer exists, remove from tracked beacons
2011 it
= last_beacon
.erase(it
);
2015 if (since_last
.count() >= g_conf()->mds_beacon_grace
) {
2016 auto& info
= fsmap
.get_info_gid(gid
);
2017 dout(1) << "no beacon from mds." << info
.rank
<< "." << info
.inc
2018 << " (gid: " << gid
<< " addr: " << info
.addrs
2019 << " state: " << ceph_mds_state_name(info
.state
) << ")"
2020 << " since " << since_last
.count() << dendl
;
2021 // If the OSDMap is writeable, we can blacklist things, so we can
2022 // try failing any laggy MDS daemons. Consider each one for failure.
2023 if (!info
.laggy()) {
2024 dout(1) << " marking " << gid
<< " " << info
.addrs
2025 << " mds." << info
.rank
<< "." << info
.inc
2026 << " " << ceph_mds_state_name(info
.state
)
2027 << " laggy" << dendl
;
2028 fsmap
.modify_daemon(info
.global_id
, [](auto& info
) {
2029 info
.laggy_since
= ceph_clock_now();
2033 if (osdmap_writeable
&& may_replace
) {
2034 to_remove
.push_back(gid
); // drop_mds may invalidate iterator
2041 for (const auto& gid
: to_remove
) {
2042 auto& info
= fsmap
.get_info_gid(gid
);
2043 const mds_info_t
* rep_info
= nullptr;
2044 if (info
.rank
>= 0) {
2045 auto fscid
= fsmap
.gid_fscid(gid
);
2046 rep_info
= fsmap
.find_replacement_for({fscid
, info
.rank
});
2048 bool dropped
= drop_mds(fsmap
, gid
, rep_info
, propose_osdmap
);
2050 mon
->clog
->info() << "MDS " << info
.human_name()
2051 << " is removed because it is dead or otherwise unavailable.";
2056 if (osdmap_writeable
) {
2057 for (auto& [fscid
, fs
] : fsmap
.filesystems
) {
2058 if (!fs
->mds_map
.test_flag(CEPH_MDSMAP_NOT_JOINABLE
) &&
2059 fs
->mds_map
.is_resizeable()) {
2060 // Check if a rank or standby-replay should be replaced with a stronger
2061 // affinity standby. This looks at ranks and standby-replay:
2062 for (const auto& [gid
, info
] : fs
->mds_map
.get_mds_info()) {
2063 const auto join_fscid
= info
.join_fscid
;
2064 if (join_fscid
== fscid
)
2066 const auto rank
= info
.rank
;
2067 const auto state
= info
.state
;
2068 const mds_info_t
* rep_info
= nullptr;
2069 if (state
== MDSMap::STATE_STANDBY_REPLAY
) {
2070 rep_info
= fsmap
.get_available_standby(fscid
);
2071 } else if (state
== MDSMap::STATE_ACTIVE
) {
2072 rep_info
= fsmap
.find_replacement_for({fscid
, rank
});
2074 /* N.B. !is_degraded() */
2075 ceph_abort_msg("invalid state in MDSMap");
2080 bool better_affinity
= false;
2081 if (join_fscid
== FS_CLUSTER_ID_NONE
) {
2082 better_affinity
= (rep_info
->join_fscid
== fscid
);
2084 better_affinity
= (rep_info
->join_fscid
== fscid
) ||
2085 (rep_info
->join_fscid
== FS_CLUSTER_ID_NONE
);
2087 if (better_affinity
) {
2088 if (state
== MDSMap::STATE_STANDBY_REPLAY
) {
2089 mon
->clog
->info() << "Dropping low affinity standby-replay "
2090 << info
.human_name()
2091 << " in favor of higher affinity standby.";
2092 *propose_osdmap
|= fail_mds_gid(fsmap
, gid
);
2093 /* Now let maybe_promote_standby do the promotion. */
2095 mon
->clog
->info() << "Dropping low affinity active "
2096 << info
.human_name()
2097 << " in favor of higher affinity standby.";
2098 do_propose
|= drop_mds(fsmap
, gid
, rep_info
, propose_osdmap
);
2100 break; /* don't replace more than one per tick per fs */
2109 bool MDSMonitor::maybe_promote_standby(FSMap
&fsmap
, Filesystem
& fs
)
2111 if (fs
.mds_map
.test_flag(CEPH_MDSMAP_NOT_JOINABLE
)) {
2115 bool do_propose
= false;
2117 // have a standby take over?
2118 set
<mds_rank_t
> failed
;
2119 fs
.mds_map
.get_failed_mds_set(failed
);
2120 for (const auto& rank
: failed
) {
2121 auto info
= fsmap
.find_replacement_for({fs
.fscid
, rank
});
2123 dout(1) << " taking over failed mds." << rank
<< " with " << info
->global_id
2124 << "/" << info
->name
<< " " << info
->addrs
<< dendl
;
2125 mon
->clog
->info() << "Standby " << info
->human_name()
2126 << " assigned to filesystem " << fs
.mds_map
.fs_name
2127 << " as rank " << rank
;
2129 fsmap
.promote(info
->global_id
, fs
, rank
);
2134 if (!fs
.mds_map
.is_degraded() && fs
.mds_map
.allows_standby_replay()) {
2135 // There were no failures to replace, so try using any available standbys
2136 // as standby-replay daemons. Don't do this when the cluster is degraded
2137 // as a standby-replay daemon may try to read a journal being migrated.
2139 auto info
= fsmap
.get_available_standby(fs
.fscid
);
2141 dout(20) << "standby available mds." << info
->global_id
<< dendl
;
2142 bool changed
= false;
2143 for (const auto& rank
: fs
.mds_map
.in
) {
2144 dout(20) << "examining " << rank
<< dendl
;
2145 if (fs
.mds_map
.is_followable(rank
)) {
2146 dout(1) << " setting mds." << info
->global_id
2147 << " to follow mds rank " << rank
<< dendl
;
2148 fsmap
.assign_standby_replay(info
->global_id
, fs
.fscid
, rank
);
2154 if (!changed
) break;
2161 void MDSMonitor::tick()
2163 if (!is_active() || !is_leader()) return;
2165 auto &pending
= get_pending_fsmap_writeable();
2167 bool do_propose
= false;
2168 bool propose_osdmap
= false;
2170 do_propose
|= pending
.check_health();
2172 /* Check health and affinity of ranks */
2173 do_propose
|= check_health(pending
, &propose_osdmap
);
2175 /* Resize the cluster according to max_mds. */
2176 for (auto& p
: pending
.filesystems
) {
2177 do_propose
|= maybe_resize_cluster(pending
, p
.second
->fscid
);
2180 /* Replace any failed ranks. */
2181 for (auto& p
: pending
.filesystems
) {
2182 do_propose
|= maybe_promote_standby(pending
, *p
.second
);
2185 if (propose_osdmap
) {
2186 request_proposal(mon
->osdmon());
2193 last_tick
= mono_clock::now();
2196 MDSMonitor::MDSMonitor(Monitor
*mn
, Paxos
*p
, string service_name
)
2197 : PaxosService(mn
, p
, service_name
)
2199 handlers
= FileSystemCommandHandler::load(p
);
2202 void MDSMonitor::on_restart()
2204 // Clear out the leader-specific state.
2205 last_tick
= mono_clock::now();
2206 last_beacon
.clear();