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 "messages/MMgrBeacon.h"
15 #include "messages/MMgrMap.h"
16 #include "messages/MMgrDigest.h"
19 #include "PGMonitor.h"
20 #include "include/stringify.h"
21 #include "mgr/MgrContext.h"
22 #include "OSDMonitor.h"
24 #include "MgrMonitor.h"
26 #define dout_subsys ceph_subsys_mon
28 #define dout_prefix _prefix(_dout, mon, map)
29 static ostream
& _prefix(std::ostream
*_dout
, Monitor
*mon
,
30 const MgrMap
& mgrmap
) {
31 return *_dout
<< "mon." << mon
->name
<< "@" << mon
->rank
32 << "(" << mon
->get_state_name()
33 << ").mgr e" << mgrmap
.get_epoch() << " ";
36 void MgrMonitor::create_initial()
40 void MgrMonitor::update_from_paxos(bool *need_bootstrap
)
42 version_t version
= get_last_committed();
43 if (version
!= map
.epoch
) {
44 dout(4) << "loading version " << version
<< dendl
;
47 int err
= get_version(version
, bl
);
50 bufferlist::iterator p
= bl
.begin();
53 dout(4) << "active server: " << map
.active_addr
54 << "(" << map
.active_gid
<< ")" << dendl
;
57 first_seen_inactive
= utime_t();
59 first_seen_inactive
= ceph_clock_now();
65 // feed our pet MgrClient
66 mon
->mgr_client
.ms_dispatch(new MMgrMap(map
));
69 void MgrMonitor::create_pending()
75 void MgrMonitor::encode_pending(MonitorDBStore::TransactionRef t
)
77 dout(10) << __func__
<< " " << pending_map
<< dendl
;
79 pending_map
.encode(bl
, mon
->get_quorum_con_features());
80 put_version(t
, pending_map
.epoch
, bl
);
81 put_last_committed(t
, pending_map
.epoch
);
84 bool MgrMonitor::check_caps(MonOpRequestRef op
, const uuid_d
& fsid
)
87 MonSession
*session
= op
->get_session();
90 if (!session
->is_capable("mgr", MON_CAP_X
)) {
91 dout(1) << __func__
<< " insufficient caps " << session
->caps
<< dendl
;
94 if (fsid
!= mon
->monmap
->fsid
) {
95 dout(1) << __func__
<< " op fsid " << fsid
96 << " != " << mon
->monmap
->fsid
<< dendl
;
102 bool MgrMonitor::preprocess_query(MonOpRequestRef op
)
104 PaxosServiceMessage
*m
= static_cast<PaxosServiceMessage
*>(op
->get_req());
105 switch (m
->get_type()) {
107 return preprocess_beacon(op
);
108 case MSG_MON_COMMAND
:
109 return preprocess_command(op
);
112 derr
<< "Unhandled message type " << m
->get_type() << dendl
;
117 bool MgrMonitor::prepare_update(MonOpRequestRef op
)
119 PaxosServiceMessage
*m
= static_cast<PaxosServiceMessage
*>(op
->get_req());
120 switch (m
->get_type()) {
122 return prepare_beacon(op
);
124 case MSG_MON_COMMAND
:
125 return prepare_command(op
);
129 derr
<< "Unhandled message type " << m
->get_type() << dendl
;
136 class C_Updated
: public Context
{
140 C_Updated(MgrMonitor
*a
, MonOpRequestRef c
) :
142 void finish(int r
) override
{
145 } else if (r
== -ECANCELED
) {
146 mm
->mon
->no_reply(op
);
148 mm
->dispatch(op
); // try again
153 bool MgrMonitor::preprocess_beacon(MonOpRequestRef op
)
155 MMgrBeacon
*m
= static_cast<MMgrBeacon
*>(op
->get_req());
156 dout(4) << "beacon from " << m
->get_gid() << dendl
;
158 if (!check_caps(op
, m
->get_fsid())) {
159 // drop it on the floor
163 // always send this to the leader's prepare_beacon()
167 bool MgrMonitor::prepare_beacon(MonOpRequestRef op
)
169 MMgrBeacon
*m
= static_cast<MMgrBeacon
*>(op
->get_req());
170 dout(4) << "beacon from " << m
->get_gid() << dendl
;
172 // See if we are seeing same name, new GID for the active daemon
173 if (m
->get_name() == pending_map
.active_name
174 && m
->get_gid() != pending_map
.active_gid
)
176 dout(4) << "Active daemon restart (mgr." << m
->get_name() << ")" << dendl
;
180 // See if we are seeing same name, new GID for any standbys
181 for (const auto &i
: pending_map
.standbys
) {
182 const StandbyInfo
&s
= i
.second
;
183 if (s
.name
== m
->get_name() && s
.gid
!= m
->get_gid()) {
184 dout(4) << "Standby daemon restart (mgr." << m
->get_name() << ")" << dendl
;
185 drop_standby(i
.first
);
190 last_beacon
[m
->get_gid()] = ceph_clock_now();
192 // Track whether we modified pending_map
193 bool updated
= false;
195 if (pending_map
.active_gid
== m
->get_gid()) {
196 // A beacon from the currently active daemon
197 if (pending_map
.active_addr
!= m
->get_server_addr()) {
198 dout(4) << "learned address " << m
->get_server_addr()
199 << " (was " << pending_map
.active_addr
<< ")" << dendl
;
200 pending_map
.active_addr
= m
->get_server_addr();
204 if (pending_map
.get_available() != m
->get_available()) {
205 dout(4) << "available " << m
->get_gid() << dendl
;
206 pending_map
.available
= m
->get_available();
209 } else if (pending_map
.active_gid
== 0) {
210 // There is no currently active daemon, select this one.
211 if (pending_map
.standbys
.count(m
->get_gid())) {
212 drop_standby(m
->get_gid());
214 dout(4) << "selecting new active " << m
->get_gid()
215 << " " << m
->get_name()
216 << " (was " << pending_map
.active_gid
<< " "
217 << pending_map
.active_name
<< ")" << dendl
;
218 pending_map
.active_gid
= m
->get_gid();
219 pending_map
.active_name
= m
->get_name();
223 if (pending_map
.standbys
.count(m
->get_gid()) > 0) {
224 dout(10) << "from existing standby " << m
->get_gid() << dendl
;
226 dout(10) << "new standby " << m
->get_gid() << dendl
;
227 pending_map
.standbys
[m
->get_gid()] = {m
->get_gid(), m
->get_name()};
233 dout(4) << "updating map" << dendl
;
234 wait_for_finished_proposal(op
, new C_Updated(this, op
));
236 dout(10) << "no change" << dendl
;
242 void MgrMonitor::check_subs()
244 const std::string type
= "mgrmap";
245 if (mon
->session_map
.subs
.count(type
) == 0)
247 for (auto sub
: *(mon
->session_map
.subs
[type
])) {
252 void MgrMonitor::check_sub(Subscription
*sub
)
254 if (sub
->type
== "mgrmap") {
255 if (sub
->next
<= map
.get_epoch()) {
256 dout(20) << "Sending map to subscriber " << sub
->session
->con
<< dendl
;
257 sub
->session
->con
->send_message(new MMgrMap(map
));
259 mon
->session_map
.remove_sub(sub
);
261 sub
->next
= map
.get_epoch() + 1;
265 assert(sub
->type
== "mgrdigest");
266 if (digest_callback
== nullptr) {
273 * Handle digest subscriptions separately (outside of check_sub) because
274 * they are going to be periodic rather than version-driven.
276 void MgrMonitor::send_digests()
278 digest_callback
= nullptr;
280 const std::string type
= "mgrdigest";
281 if (mon
->session_map
.subs
.count(type
) == 0)
284 for (auto sub
: *(mon
->session_map
.subs
[type
])) {
285 MMgrDigest
*mdigest
= new MMgrDigest
;
288 std::list
<std::string
> health_strs
;
289 mon
->get_health(health_strs
, nullptr, &f
);
290 f
.flush(mdigest
->health_json
);
293 std::ostringstream ss
;
294 mon
->get_mon_status(&f
, ss
);
295 f
.flush(mdigest
->mon_status_json
);
298 sub
->session
->con
->send_message(mdigest
);
301 digest_callback
= new C_MonContext(mon
, [this](int){
304 mon
->timer
.add_event_after(g_conf
->mon_mgr_digest_period
, digest_callback
);
307 void MgrMonitor::on_active()
309 if (mon
->is_leader())
310 mon
->clog
->info() << "mgrmap e" << map
.epoch
<< ": " << map
;
313 void MgrMonitor::get_health(
314 list
<pair
<health_status_t
,string
> >& summary
,
315 list
<pair
<health_status_t
,string
> > *detail
,
316 CephContext
*cct
) const
318 // start mgr warnings as soon as the mons and osds are all upgraded,
319 // but before the require_luminous osdmap flag is set. this way the
320 // user gets some warning before the osd flag is set and mgr is
321 // actually *required*.
322 if (!mon
->monmap
->get_required_features().contains_all(
323 ceph::features::mon::FEATURE_LUMINOUS
) ||
324 !HAVE_FEATURE(mon
->osdmon()->osdmap
.get_up_osd_features(),
329 if (!map
.available
) {
330 auto level
= HEALTH_WARN
;
331 // do not escalate to ERR if they are still upgrading to jewel.
332 if (mon
->osdmon()->osdmap
.test_flag(CEPH_OSDMAP_REQUIRE_LUMINOUS
)) {
333 utime_t now
= ceph_clock_now();
334 if (first_seen_inactive
!= utime_t() &&
335 now
- first_seen_inactive
> g_conf
->mon_mgr_inactive_grace
) {
339 summary
.push_back(make_pair(level
, "no active mgr"));
343 void MgrMonitor::tick()
345 if (!is_active() || !mon
->is_leader())
348 const utime_t now
= ceph_clock_now();
349 utime_t cutoff
= now
;
350 cutoff
-= g_conf
->mon_mgr_beacon_grace
;
352 // Populate any missing beacons (i.e. no beacon since MgrMonitor
353 // instantiation) with the current time, so that they will
354 // eventually look laggy if they fail to give us a beacon.
355 if (pending_map
.active_gid
!= 0
356 && last_beacon
.count(pending_map
.active_gid
) == 0) {
357 last_beacon
[pending_map
.active_gid
] = now
;
359 for (auto s
: pending_map
.standbys
) {
360 if (last_beacon
.count(s
.first
) == 0) {
361 last_beacon
[s
.first
] = now
;
365 // Cull standbys first so that any remaining standbys
366 // will be eligible to take over from the active if we cull him.
367 std::list
<uint64_t> dead_standbys
;
368 for (const auto &i
: pending_map
.standbys
) {
369 auto last_beacon_time
= last_beacon
.at(i
.first
);
370 if (last_beacon_time
< cutoff
) {
371 dead_standbys
.push_back(i
.first
);
375 bool propose
= false;
377 for (auto i
: dead_standbys
) {
378 dout(4) << "Dropping laggy standby " << i
<< dendl
;
383 if (pending_map
.active_gid
!= 0
384 && last_beacon
.at(pending_map
.active_gid
) < cutoff
) {
388 dout(4) << "Dropping active" << pending_map
.active_gid
<< dendl
;
389 if (promote_standby()) {
390 dout(4) << "Promoted standby " << pending_map
.active_gid
<< dendl
;
392 dout(4) << "Active is laggy but have no standbys to replace it" << dendl
;
394 } else if (pending_map
.active_gid
== 0) {
395 if (promote_standby()) {
396 dout(4) << "Promoted standby " << pending_map
.active_gid
<< dendl
;
406 bool MgrMonitor::promote_standby()
408 assert(pending_map
.active_gid
== 0);
409 if (pending_map
.standbys
.size()) {
410 // Promote a replacement (arbitrary choice of standby)
411 auto replacement_gid
= pending_map
.standbys
.begin()->first
;
412 pending_map
.active_gid
= replacement_gid
;
413 pending_map
.active_name
= pending_map
.standbys
.at(replacement_gid
).name
;
414 pending_map
.available
= false;
415 pending_map
.active_addr
= entity_addr_t();
417 drop_standby(replacement_gid
);
424 void MgrMonitor::drop_active()
426 if (last_beacon
.count(pending_map
.active_gid
) > 0) {
427 last_beacon
.erase(pending_map
.active_gid
);
430 pending_map
.active_name
= "";
431 pending_map
.active_gid
= 0;
432 pending_map
.available
= false;
433 pending_map
.active_addr
= entity_addr_t();
436 void MgrMonitor::drop_standby(uint64_t gid
)
438 pending_map
.standbys
.erase(gid
);
439 if (last_beacon
.count(gid
) > 0) {
440 last_beacon
.erase(gid
);
445 bool MgrMonitor::preprocess_command(MonOpRequestRef op
)
451 bool MgrMonitor::prepare_command(MonOpRequestRef op
)
453 MMonCommand
*m
= static_cast<MMonCommand
*>(op
->get_req());
455 std::stringstream ss
;
458 std::map
<std::string
, cmd_vartype
> cmdmap
;
459 if (!cmdmap_from_json(m
->cmd
, &cmdmap
, ss
)) {
460 string rs
= ss
.str();
461 mon
->reply_command(op
, -EINVAL
, rs
, rdata
, get_last_committed());
465 MonSession
*session
= m
->get_session();
467 mon
->reply_command(op
, -EACCES
, "access denied", rdata
, get_last_committed());
472 cmd_getval(g_ceph_context
, cmdmap
, "prefix", prefix
);
476 if (prefix
== "mgr fail") {
478 cmd_getval(g_ceph_context
, cmdmap
, "who", who
);
481 uint64_t gid
= strict_strtol(who
.c_str(), 10, &err
);
482 bool changed
= false;
484 // Does not parse as a gid, treat it as a name
485 if (pending_map
.active_name
== who
) {
490 for (const auto &i
: pending_map
.standbys
) {
491 if (i
.second
.name
== who
) {
500 ss
<< "Daemon not found '" << who
<< "', already failed?";
504 if (pending_map
.active_gid
== gid
) {
507 } else if (pending_map
.standbys
.count(gid
) > 0) {
511 ss
<< "Daemon not found '" << gid
<< "', already failed?";
515 if (changed
&& pending_map
.active_gid
== 0) {
522 dout(4) << __func__
<< " done, r=" << r
<< dendl
;
523 /* Compose response */
528 // success.. delay reply
529 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, r
, rs
,
530 get_last_committed() + 1));
534 mon
->reply_command(op
, r
, rs
, rdata
, get_last_committed());
539 void MgrMonitor::init()
541 if (digest_callback
== nullptr) {
542 send_digests(); // To get it to schedule its own event
546 void MgrMonitor::on_shutdown()
548 if (digest_callback
) {
549 mon
->timer
.cancel_event(digest_callback
);