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) 2016 John Spray <john.spray@redhat.com>
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.
14 #include <boost/tokenizer.hpp>
16 #include "messages/MMgrBeacon.h"
17 #include "messages/MMgrMap.h"
18 #include "messages/MMgrDigest.h"
20 #include "PGStatService.h"
21 #include "include/stringify.h"
22 #include "mgr/MgrContext.h"
23 #include "mgr/mgr_commands.h"
24 #include "OSDMonitor.h"
26 #include "MgrMonitor.h"
28 #define MGR_METADATA_PREFIX "mgr_metadata"
30 #define dout_subsys ceph_subsys_mon
32 #define dout_prefix _prefix(_dout, mon, map)
33 static ostream
& _prefix(std::ostream
*_dout
, Monitor
*mon
,
34 const MgrMap
& mgrmap
) {
35 return *_dout
<< "mon." << mon
->name
<< "@" << mon
->rank
36 << "(" << mon
->get_state_name()
37 << ").mgr e" << mgrmap
.get_epoch() << " ";
40 // Prefix for mon store of active mgr's command descriptions
41 const static std::string command_descs_prefix
= "mgr_command_descs";
44 version_t
MgrMonitor::get_trim_to()
46 int64_t max
= g_conf
->get_val
<int64_t>("mon_max_mgrmap_epochs");
47 if (map
.epoch
> max
) {
48 return map
.epoch
- max
;
53 void MgrMonitor::create_initial()
55 // Take a local copy of initial_modules for tokenizer to iterate over.
56 auto initial_modules
= g_conf
->get_val
<std::string
>("mgr_initial_modules");
57 boost::tokenizer
<> tok(initial_modules
);
59 pending_map
.modules
.insert(m
);
61 pending_command_descs
= mgr_commands
;
62 dout(10) << __func__
<< " initial modules " << pending_map
.modules
63 << ", " << pending_command_descs
.size() << " commands"
67 void MgrMonitor::get_store_prefixes(std::set
<string
>& s
)
69 s
.insert(service_name
);
70 s
.insert(command_descs_prefix
);
71 s
.insert(MGR_METADATA_PREFIX
);
74 void MgrMonitor::update_from_paxos(bool *need_bootstrap
)
76 version_t version
= get_last_committed();
77 if (version
!= map
.epoch
) {
78 dout(4) << "loading version " << version
<< dendl
;
81 int err
= get_version(version
, bl
);
84 bool old_available
= map
.get_available();
85 uint64_t old_gid
= map
.get_active_gid();
87 bufferlist::iterator p
= bl
.begin();
90 dout(4) << "active server: " << map
.active_addr
91 << "(" << map
.active_gid
<< ")" << dendl
;
93 ever_had_active_mgr
= get_value("ever_had_active_mgr");
98 first_seen_inactive
= utime_t();
100 first_seen_inactive
= ceph_clock_now();
106 || command_descs
.empty()
107 || (map
.get_available()
108 && (!old_available
|| old_gid
!= map
.get_active_gid()))) {
109 dout(4) << "mkfs or daemon transitioned to available, loading commands"
111 bufferlist loaded_commands
;
112 int r
= mon
->store
->get(command_descs_prefix
, "", loaded_commands
);
114 derr
<< "Failed to load mgr commands: " << cpp_strerror(r
) << dendl
;
116 auto p
= loaded_commands
.begin();
117 ::decode(command_descs
, p
);
122 // feed our pet MgrClient
123 mon
->mgr_client
.ms_dispatch(new MMgrMap(map
));
126 void MgrMonitor::create_pending()
131 if (map
.get_epoch() == 1 &&
132 command_descs
.empty() &&
133 pending_command_descs
.empty()) {
134 // we've been through the initial map and we haven't populated the
135 // command_descs vector. This likely means we came from kraken, where
136 // we wouldn't populate the vector, nor would we write it to disk, on
142 health_status_t
MgrMonitor::should_warn_about_mgr_down()
144 utime_t now
= ceph_clock_now();
146 // - we've ever had an active mgr, or
147 // - we have osds AND we've exceeded the grace period
148 // which means a new mon cluster and be HEALTH_OK indefinitely as long as
149 // no OSDs are ever created.
150 if (ever_had_active_mgr
||
151 (mon
->osdmon()->osdmap
.get_num_osds() > 0 &&
152 now
> mon
->monmap
->created
+ g_conf
->get_val
<int64_t>("mon_mgr_mkfs_grace"))) {
153 health_status_t level
= HEALTH_WARN
;
154 if (first_seen_inactive
!= utime_t() &&
155 now
- first_seen_inactive
> g_conf
->get_val
<int64_t>("mon_mgr_inactive_grace")) {
163 void MgrMonitor::encode_pending(MonitorDBStore::TransactionRef t
)
165 dout(10) << __func__
<< " " << pending_map
<< dendl
;
167 pending_map
.encode(bl
, mon
->get_quorum_con_features());
168 put_version(t
, pending_map
.epoch
, bl
);
169 put_last_committed(t
, pending_map
.epoch
);
171 for (auto& p
: pending_metadata
) {
172 dout(10) << __func__
<< " set metadata for " << p
.first
<< dendl
;
173 t
->put(MGR_METADATA_PREFIX
, p
.first
, p
.second
);
175 for (auto& name
: pending_metadata_rm
) {
176 dout(10) << __func__
<< " rm metadata for " << name
<< dendl
;
177 t
->erase(MGR_METADATA_PREFIX
, name
);
179 pending_metadata
.clear();
180 pending_metadata_rm
.clear();
182 health_check_map_t next
;
183 if (pending_map
.active_gid
== 0) {
184 auto level
= should_warn_about_mgr_down();
185 if (level
!= HEALTH_OK
) {
186 next
.add("MGR_DOWN", level
, "no active mgr");
188 dout(10) << __func__
<< " no health warning (never active and new cluster)"
192 put_value(t
, "ever_had_active_mgr", 1);
194 encode_health(next
, t
);
196 if (pending_command_descs
.size()) {
197 dout(4) << __func__
<< " encoding " << pending_command_descs
.size()
198 << " command_descs" << dendl
;
199 for (auto& p
: pending_command_descs
) {
200 p
.set_flag(MonCommand::FLAG_MGR
);
203 ::encode(pending_command_descs
, bl
);
204 t
->put(command_descs_prefix
, "", bl
);
205 pending_command_descs
.clear();
209 bool MgrMonitor::check_caps(MonOpRequestRef op
, const uuid_d
& fsid
)
212 MonSession
*session
= op
->get_session();
215 if (!session
->is_capable("mgr", MON_CAP_X
)) {
216 dout(1) << __func__
<< " insufficient caps " << session
->caps
<< dendl
;
219 if (fsid
!= mon
->monmap
->fsid
) {
220 dout(1) << __func__
<< " op fsid " << fsid
221 << " != " << mon
->monmap
->fsid
<< dendl
;
227 bool MgrMonitor::preprocess_query(MonOpRequestRef op
)
229 PaxosServiceMessage
*m
= static_cast<PaxosServiceMessage
*>(op
->get_req());
230 switch (m
->get_type()) {
232 return preprocess_beacon(op
);
233 case MSG_MON_COMMAND
:
234 return preprocess_command(op
);
237 derr
<< "Unhandled message type " << m
->get_type() << dendl
;
242 bool MgrMonitor::prepare_update(MonOpRequestRef op
)
244 PaxosServiceMessage
*m
= static_cast<PaxosServiceMessage
*>(op
->get_req());
245 switch (m
->get_type()) {
247 return prepare_beacon(op
);
249 case MSG_MON_COMMAND
:
250 return prepare_command(op
);
254 derr
<< "Unhandled message type " << m
->get_type() << dendl
;
261 class C_Updated
: public Context
{
265 C_Updated(MgrMonitor
*a
, MonOpRequestRef c
) :
267 void finish(int r
) override
{
270 } else if (r
== -ECANCELED
) {
271 mm
->mon
->no_reply(op
);
273 mm
->dispatch(op
); // try again
278 bool MgrMonitor::preprocess_beacon(MonOpRequestRef op
)
280 MMgrBeacon
*m
= static_cast<MMgrBeacon
*>(op
->get_req());
281 dout(4) << "beacon from " << m
->get_gid() << dendl
;
283 if (!check_caps(op
, m
->get_fsid())) {
284 // drop it on the floor
288 // always send this to the leader's prepare_beacon()
292 bool MgrMonitor::prepare_beacon(MonOpRequestRef op
)
294 MMgrBeacon
*m
= static_cast<MMgrBeacon
*>(op
->get_req());
295 dout(4) << "beacon from " << m
->get_gid() << dendl
;
297 // See if we are seeing same name, new GID for the active daemon
298 if (m
->get_name() == pending_map
.active_name
299 && m
->get_gid() != pending_map
.active_gid
)
301 dout(4) << "Active daemon restart (mgr." << m
->get_name() << ")" << dendl
;
302 mon
->clog
->info() << "Active manager daemon " << m
->get_name()
307 // See if we are seeing same name, new GID for any standbys
308 for (const auto &i
: pending_map
.standbys
) {
309 const StandbyInfo
&s
= i
.second
;
310 if (s
.name
== m
->get_name() && s
.gid
!= m
->get_gid()) {
311 dout(4) << "Standby daemon restart (mgr." << m
->get_name() << ")" << dendl
;
312 mon
->clog
->debug() << "Standby manager daemon " << m
->get_name()
314 drop_standby(i
.first
);
319 last_beacon
[m
->get_gid()] = ceph::coarse_mono_clock::now();
321 // Track whether we modified pending_map
322 bool updated
= false;
324 if (pending_map
.active_gid
== m
->get_gid()) {
325 if (pending_map
.services
!= m
->get_services()) {
326 dout(4) << "updated services from mgr." << m
->get_name()
327 << ": " << m
->get_services() << dendl
;
328 pending_map
.services
= m
->get_services();
332 // A beacon from the currently active daemon
333 if (pending_map
.active_addr
!= m
->get_server_addr()) {
334 dout(4) << "learned address " << m
->get_server_addr()
335 << " (was " << pending_map
.active_addr
<< ")" << dendl
;
336 pending_map
.active_addr
= m
->get_server_addr();
340 if (pending_map
.get_available() != m
->get_available()) {
341 dout(4) << "available " << m
->get_gid() << dendl
;
342 mon
->clog
->info() << "Manager daemon " << pending_map
.active_name
343 << " is now available";
345 // This beacon should include command descriptions
346 pending_command_descs
= m
->get_command_descs();
347 if (pending_command_descs
.empty()) {
348 // This should not happen, but it also isn't fatal: we just
349 // won't successfully update our list of commands.
350 dout(4) << "First available beacon from " << pending_map
.active_name
351 << "(" << m
->get_gid() << ") does not include command descs"
354 dout(4) << "First available beacon from " << pending_map
.active_name
355 << "(" << m
->get_gid() << ") includes "
356 << pending_command_descs
.size() << " command descs" << dendl
;
359 pending_map
.available
= m
->get_available();
362 if (pending_map
.available_modules
!= m
->get_available_modules()) {
363 dout(4) << "available_modules " << m
->get_available_modules()
364 << " (was " << pending_map
.available_modules
<< ")" << dendl
;
365 pending_map
.available_modules
= m
->get_available_modules();
368 } else if (pending_map
.active_gid
== 0) {
369 // There is no currently active daemon, select this one.
370 if (pending_map
.standbys
.count(m
->get_gid())) {
371 drop_standby(m
->get_gid(), false);
373 dout(4) << "selecting new active " << m
->get_gid()
374 << " " << m
->get_name()
375 << " (was " << pending_map
.active_gid
<< " "
376 << pending_map
.active_name
<< ")" << dendl
;
377 pending_map
.active_gid
= m
->get_gid();
378 pending_map
.active_name
= m
->get_name();
379 pending_map
.available_modules
= m
->get_available_modules();
380 ::encode(m
->get_metadata(), pending_metadata
[m
->get_name()]);
381 pending_metadata_rm
.erase(m
->get_name());
383 mon
->clog
->info() << "Activating manager daemon "
384 << pending_map
.active_name
;
388 if (pending_map
.standbys
.count(m
->get_gid()) > 0) {
389 dout(10) << "from existing standby " << m
->get_gid() << dendl
;
390 if (pending_map
.standbys
[m
->get_gid()].available_modules
!=
391 m
->get_available_modules()) {
392 dout(10) << "existing standby " << m
->get_gid() << " available_modules "
393 << m
->get_available_modules() << " (was "
394 << pending_map
.standbys
[m
->get_gid()].available_modules
<< ")"
396 pending_map
.standbys
[m
->get_gid()].available_modules
=
397 m
->get_available_modules();
401 dout(10) << "new standby " << m
->get_gid() << dendl
;
402 mon
->clog
->debug() << "Standby manager daemon " << m
->get_name()
404 pending_map
.standbys
[m
->get_gid()] = {m
->get_gid(), m
->get_name(),
405 m
->get_available_modules()};
406 ::encode(m
->get_metadata(), pending_metadata
[m
->get_name()]);
407 pending_metadata_rm
.erase(m
->get_name());
413 dout(4) << "updating map" << dendl
;
414 wait_for_finished_proposal(op
, new C_Updated(this, op
));
416 dout(10) << "no change" << dendl
;
422 void MgrMonitor::check_subs()
424 const std::string type
= "mgrmap";
425 if (mon
->session_map
.subs
.count(type
) == 0)
427 for (auto sub
: *(mon
->session_map
.subs
[type
])) {
432 void MgrMonitor::check_sub(Subscription
*sub
)
434 if (sub
->type
== "mgrmap") {
435 if (sub
->next
<= map
.get_epoch()) {
436 dout(20) << "Sending map to subscriber " << sub
->session
->con
437 << " " << sub
->session
->con
->get_peer_addr() << dendl
;
438 sub
->session
->con
->send_message(new MMgrMap(map
));
440 mon
->session_map
.remove_sub(sub
);
442 sub
->next
= map
.get_epoch() + 1;
446 assert(sub
->type
== "mgrdigest");
447 if (sub
->next
== 0) {
448 // new registration; cancel previous timer
451 if (digest_event
== nullptr) {
458 * Handle digest subscriptions separately (outside of check_sub) because
459 * they are going to be periodic rather than version-driven.
461 void MgrMonitor::send_digests()
465 const std::string type
= "mgrdigest";
466 if (mon
->session_map
.subs
.count(type
) == 0)
470 // if paxos is currently not active, don't send a digest but reenable timer
473 dout(10) << __func__
<< dendl
;
475 for (auto sub
: *(mon
->session_map
.subs
[type
])) {
476 dout(10) << __func__
<< " sending digest to subscriber " << sub
->session
->con
477 << " " << sub
->session
->con
->get_peer_addr() << dendl
;
478 MMgrDigest
*mdigest
= new MMgrDigest
;
481 mon
->get_health_status(true, &f
, nullptr, nullptr, nullptr);
482 f
.flush(mdigest
->health_json
);
485 std::ostringstream ss
;
486 mon
->get_mon_status(&f
, ss
);
487 f
.flush(mdigest
->mon_status_json
);
490 sub
->session
->con
->send_message(mdigest
);
494 digest_event
= mon
->timer
.add_event_after(
495 g_conf
->get_val
<int64_t>("mon_mgr_digest_period"),
496 new C_MonContext(mon
, [this](int) {
501 void MgrMonitor::cancel_timer()
504 mon
->timer
.cancel_event(digest_event
);
505 digest_event
= nullptr;
509 void MgrMonitor::on_active()
511 if (mon
->is_leader()) {
512 mon
->clog
->debug() << "mgrmap e" << map
.epoch
<< ": " << map
;
516 void MgrMonitor::get_health(
517 list
<pair
<health_status_t
,string
> >& summary
,
518 list
<pair
<health_status_t
,string
> > *detail
,
519 CephContext
*cct
) const
521 // start mgr warnings as soon as the mons and osds are all upgraded,
522 // but before the require_luminous osdmap flag is set. this way the
523 // user gets some warning before the osd flag is set and mgr is
524 // actually *required*.
525 if (!mon
->monmap
->get_required_features().contains_all(
526 ceph::features::mon::FEATURE_LUMINOUS
) ||
527 !HAVE_FEATURE(mon
->osdmon()->osdmap
.get_up_osd_features(),
532 if (map
.active_gid
== 0) {
533 auto level
= HEALTH_WARN
;
534 // do not escalate to ERR if they are still upgrading to jewel.
535 if (mon
->osdmon()->osdmap
.require_osd_release
>= CEPH_RELEASE_LUMINOUS
) {
536 utime_t now
= ceph_clock_now();
537 if (first_seen_inactive
!= utime_t() &&
538 now
- first_seen_inactive
> g_conf
->get_val
<int64_t>("mon_mgr_inactive_grace")) {
542 summary
.push_back(make_pair(level
, "no active mgr"));
546 void MgrMonitor::tick()
548 if (!is_active() || !mon
->is_leader())
551 const auto now
= ceph::coarse_mono_clock::now();
553 const auto mgr_beacon_grace
= std::chrono::seconds(
554 g_conf
->get_val
<int64_t>("mon_mgr_beacon_grace"));
556 // Note that this is the mgr daemon's tick period, not ours (the
557 // beacon is sent with this period).
558 const auto mgr_tick_period
= std::chrono::seconds(
559 g_conf
->get_val
<int64_t>("mgr_tick_period"));
561 if (last_tick
!= ceph::coarse_mono_clock::time_point::min()
562 && (now
- last_tick
> (mgr_beacon_grace
- mgr_tick_period
))) {
563 // This case handles either local slowness (calls being delayed
564 // for whatever reason) or cluster election slowness (a long gap
565 // between calls while an election happened)
566 dout(4) << __func__
<< ": resetting beacon timeouts due to mon delay "
567 "(slow election?) of " << now
- last_tick
<< " seconds" << dendl
;
568 for (auto &i
: last_beacon
) {
575 // Populate any missing beacons (i.e. no beacon since MgrMonitor
576 // instantiation) with the current time, so that they will
577 // eventually look laggy if they fail to give us a beacon.
578 if (pending_map
.active_gid
!= 0
579 && last_beacon
.count(pending_map
.active_gid
) == 0) {
580 last_beacon
[pending_map
.active_gid
] = now
;
582 for (auto s
: pending_map
.standbys
) {
583 if (last_beacon
.count(s
.first
) == 0) {
584 last_beacon
[s
.first
] = now
;
588 // Cull standbys first so that any remaining standbys
589 // will be eligible to take over from the active if we cull him.
590 std::list
<uint64_t> dead_standbys
;
591 const auto cutoff
= now
- mgr_beacon_grace
;
592 for (const auto &i
: pending_map
.standbys
) {
593 auto last_beacon_time
= last_beacon
.at(i
.first
);
594 if (last_beacon_time
< cutoff
) {
595 dead_standbys
.push_back(i
.first
);
599 bool propose
= false;
601 for (auto i
: dead_standbys
) {
602 dout(4) << "Dropping laggy standby " << i
<< dendl
;
607 if (pending_map
.active_gid
!= 0
608 && last_beacon
.at(pending_map
.active_gid
) < cutoff
) {
609 const std::string old_active_name
= pending_map
.active_name
;
612 dout(4) << "Dropping active" << pending_map
.active_gid
<< dendl
;
613 if (promote_standby()) {
614 dout(4) << "Promoted standby " << pending_map
.active_gid
<< dendl
;
615 mon
->clog
->info() << "Manager daemon " << old_active_name
616 << " is unresponsive, replacing it with standby"
617 << " daemon " << pending_map
.active_name
;
619 dout(4) << "Active is laggy but have no standbys to replace it" << dendl
;
620 mon
->clog
->warn() << "Manager daemon " << old_active_name
621 << " is unresponsive. No standby daemons available.";
623 } else if (pending_map
.active_gid
== 0) {
624 if (promote_standby()) {
625 dout(4) << "Promoted standby " << pending_map
.active_gid
<< dendl
;
626 mon
->clog
->info() << "Activating manager daemon "
627 << pending_map
.active_name
;
632 if (!pending_map
.available
&&
633 !ever_had_active_mgr
&&
634 should_warn_about_mgr_down() != HEALTH_OK
) {
635 dout(10) << " exceeded mon_mgr_mkfs_grace "
636 << g_conf
->get_val
<int64_t>("mon_mgr_mkfs_grace")
637 << " seconds" << dendl
;
646 void MgrMonitor::on_restart()
648 // Clear out the leader-specific state.
650 last_tick
= ceph::coarse_mono_clock::now();
654 bool MgrMonitor::promote_standby()
656 assert(pending_map
.active_gid
== 0);
657 if (pending_map
.standbys
.size()) {
658 // Promote a replacement (arbitrary choice of standby)
659 auto replacement_gid
= pending_map
.standbys
.begin()->first
;
660 pending_map
.active_gid
= replacement_gid
;
661 pending_map
.active_name
= pending_map
.standbys
.at(replacement_gid
).name
;
662 pending_map
.available
= false;
663 pending_map
.active_addr
= entity_addr_t();
665 drop_standby(replacement_gid
, false);
673 void MgrMonitor::drop_active()
675 if (last_beacon
.count(pending_map
.active_gid
) > 0) {
676 last_beacon
.erase(pending_map
.active_gid
);
679 pending_metadata_rm
.insert(pending_map
.active_name
);
680 pending_metadata
.erase(pending_map
.active_name
);
681 pending_map
.active_name
= "";
682 pending_map
.active_gid
= 0;
683 pending_map
.available
= false;
684 pending_map
.active_addr
= entity_addr_t();
685 pending_map
.services
.clear();
687 // So that when new active mgr subscribes to mgrdigest, it will
688 // get an immediate response instead of waiting for next timer
692 void MgrMonitor::drop_standby(uint64_t gid
, bool drop_meta
)
695 pending_metadata_rm
.insert(pending_map
.standbys
[gid
].name
);
696 pending_metadata
.erase(pending_map
.standbys
[gid
].name
);
698 pending_map
.standbys
.erase(gid
);
699 if (last_beacon
.count(gid
) > 0) {
700 last_beacon
.erase(gid
);
704 bool MgrMonitor::preprocess_command(MonOpRequestRef op
)
706 MMonCommand
*m
= static_cast<MMonCommand
*>(op
->get_req());
707 std::stringstream ss
;
710 std::map
<std::string
, cmd_vartype
> cmdmap
;
711 if (!cmdmap_from_json(m
->cmd
, &cmdmap
, ss
)) {
712 string rs
= ss
.str();
713 mon
->reply_command(op
, -EINVAL
, rs
, rdata
, get_last_committed());
717 MonSession
*session
= m
->get_session();
719 mon
->reply_command(op
, -EACCES
, "access denied", rdata
,
720 get_last_committed());
725 cmd_getval(g_ceph_context
, cmdmap
, "format", format
, string("json-pretty"));
726 boost::scoped_ptr
<Formatter
> f(Formatter::create(format
));
729 cmd_getval(g_ceph_context
, cmdmap
, "prefix", prefix
);
732 if (prefix
== "mgr dump") {
734 cmd_getval(g_ceph_context
, cmdmap
, "epoch", epoch
, (int64_t)map
.get_epoch());
735 if (epoch
== (int64_t)map
.get_epoch()) {
736 f
->dump_object("mgrmap", map
);
739 int err
= get_version(epoch
, bl
);
740 if (err
== -ENOENT
) {
742 ss
<< "there is no map for epoch " << epoch
;
748 f
->dump_object("mgrmap", m
);
751 } else if (prefix
== "mgr module ls") {
752 f
->open_object_section("modules");
754 f
->open_array_section("enabled_modules");
755 for (auto& p
: map
.modules
) {
756 f
->dump_string("module", p
);
759 f
->open_array_section("disabled_modules");
760 for (auto& p
: map
.available_modules
) {
761 if (map
.modules
.count(p
) == 0) {
762 f
->dump_string("module", p
);
769 } else if (prefix
== "mgr services") {
770 f
->open_object_section("services");
771 for (const auto &i
: map
.services
) {
772 f
->dump_string(i
.first
.c_str(), i
.second
);
776 } else if (prefix
== "mgr metadata") {
778 cmd_getval(g_ceph_context
, cmdmap
, "id", name
);
779 if (name
.size() > 0 && !map
.have_name(name
)) {
780 ss
<< "mgr." << name
<< " does not exist";
785 cmd_getval(g_ceph_context
, cmdmap
, "format", format
);
786 boost::scoped_ptr
<Formatter
> f(Formatter::create(format
, "json-pretty", "json-pretty"));
788 f
->open_object_section("mgr_metadata");
789 f
->dump_string("id", name
);
790 r
= dump_metadata(name
, f
.get(), &ss
);
796 f
->open_array_section("mgr_metadata");
797 for (auto& i
: map
.get_all_names()) {
798 f
->open_object_section("mgr");
799 f
->dump_string("id", i
);
800 r
= dump_metadata(i
, f
.get(), NULL
);
801 if (r
== -EINVAL
|| r
== -ENOENT
) {
802 // Drop error, continue to get other daemons' metadata
803 dout(4) << "No metadata for mgr." << i
<< dendl
;
814 } else if (prefix
== "mgr versions") {
816 f
.reset(Formatter::create("json-pretty"));
817 count_metadata("ceph_version", f
.get());
820 } else if (prefix
== "mgr count-metadata") {
822 f
.reset(Formatter::create("json-pretty"));
824 cmd_getval(g_ceph_context
, cmdmap
, "property", field
);
825 count_metadata(field
, f
.get());
835 mon
->reply_command(op
, r
, rs
, rdata
, get_last_committed());
839 bool MgrMonitor::prepare_command(MonOpRequestRef op
)
841 MMonCommand
*m
= static_cast<MMonCommand
*>(op
->get_req());
843 std::stringstream ss
;
846 std::map
<std::string
, cmd_vartype
> cmdmap
;
847 if (!cmdmap_from_json(m
->cmd
, &cmdmap
, ss
)) {
848 string rs
= ss
.str();
849 mon
->reply_command(op
, -EINVAL
, rs
, rdata
, get_last_committed());
853 MonSession
*session
= m
->get_session();
855 mon
->reply_command(op
, -EACCES
, "access denied", rdata
, get_last_committed());
860 cmd_getval(g_ceph_context
, cmdmap
, "format", format
, string("plain"));
861 boost::scoped_ptr
<Formatter
> f(Formatter::create(format
));
864 cmd_getval(g_ceph_context
, cmdmap
, "prefix", prefix
);
868 if (prefix
== "mgr fail") {
870 cmd_getval(g_ceph_context
, cmdmap
, "who", who
);
873 uint64_t gid
= strict_strtol(who
.c_str(), 10, &err
);
874 bool changed
= false;
876 // Does not parse as a gid, treat it as a name
877 if (pending_map
.active_name
== who
) {
882 for (const auto &i
: pending_map
.standbys
) {
883 if (i
.second
.name
== who
) {
892 ss
<< "Daemon not found '" << who
<< "', already failed?";
896 if (pending_map
.active_gid
== gid
) {
899 } else if (pending_map
.standbys
.count(gid
) > 0) {
903 ss
<< "Daemon not found '" << gid
<< "', already failed?";
907 if (changed
&& pending_map
.active_gid
== 0) {
910 } else if (prefix
== "mgr module enable") {
912 cmd_getval(g_ceph_context
, cmdmap
, "module", module
);
913 if (module
.empty()) {
918 cmd_getval(g_ceph_context
, cmdmap
, "force", force
);
919 if (!pending_map
.all_support_module(module
) &&
920 force
!= "--force") {
921 ss
<< "all mgr daemons do not support module '" << module
<< "', pass "
922 << "--force to force enablement";
926 pending_map
.modules
.insert(module
);
927 } else if (prefix
== "mgr module disable") {
929 cmd_getval(g_ceph_context
, cmdmap
, "module", module
);
930 if (module
.empty()) {
934 pending_map
.modules
.erase(module
);
936 ss
<< "Command '" << prefix
<< "' not implemented!";
941 dout(4) << __func__
<< " done, r=" << r
<< dendl
;
942 /* Compose response */
947 // success.. delay reply
948 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, r
, rs
,
949 get_last_committed() + 1));
953 mon
->reply_command(op
, r
, rs
, rdata
, get_last_committed());
958 void MgrMonitor::init()
960 if (digest_event
== nullptr) {
961 send_digests(); // To get it to schedule its own event
965 void MgrMonitor::on_shutdown()
970 int MgrMonitor::load_metadata(const string
& name
, std::map
<string
, string
>& m
,
974 int r
= mon
->store
->get(MGR_METADATA_PREFIX
, name
, bl
);
978 bufferlist::iterator p
= bl
.begin();
981 catch (buffer::error
& e
) {
983 *err
<< "mgr." << name
<< " metadata is corrupt";
989 void MgrMonitor::count_metadata(const string
& field
, std::map
<string
,int> *out
)
991 std::set
<string
> ls
= map
.get_all_names();
992 for (auto& name
: ls
) {
993 std::map
<string
,string
> meta
;
994 load_metadata(name
, meta
, nullptr);
995 auto p
= meta
.find(field
);
996 if (p
== meta
.end()) {
1004 void MgrMonitor::count_metadata(const string
& field
, Formatter
*f
)
1006 std::map
<string
,int> by_val
;
1007 count_metadata(field
, &by_val
);
1008 f
->open_object_section(field
.c_str());
1009 for (auto& p
: by_val
) {
1010 f
->dump_int(p
.first
.c_str(), p
.second
);
1015 int MgrMonitor::dump_metadata(const string
& name
, Formatter
*f
, ostream
*err
)
1017 std::map
<string
,string
> m
;
1018 if (int r
= load_metadata(name
, m
, err
))
1021 f
->dump_string(p
.first
.c_str(), p
.second
);
1026 const std::vector
<MonCommand
> &MgrMonitor::get_command_descs() const
1028 if (command_descs
.empty()) {
1029 // must have just upgraded; fallback to static commands
1030 return mgr_commands
;
1032 return command_descs
;