]> git.proxmox.com Git - ceph.git/blame - ceph/src/mon/MDSMonitor.cc
bump version to 16.2.6-pve2
[ceph.git] / ceph / src / mon / MDSMonitor.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
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.
12 *
13 */
14
11fdf7f2 15#include <regex>
7c673cae
FG
16#include <sstream>
17#include <boost/utility.hpp>
18
19#include "MDSMonitor.h"
20#include "FSCommands.h"
21#include "Monitor.h"
22#include "MonitorDBStore.h"
23#include "OSDMonitor.h"
7c673cae
FG
24
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"
35
11fdf7f2 36#include "include/ceph_assert.h"
7c673cae
FG
37#include "include/str_list.h"
38#include "include/stringify.h"
39#include "mds/mdstypes.h"
40#include "Session.h"
41
f67539c2
TL
42using namespace TOPNSPC::common;
43
44using std::dec;
45using std::hex;
46using std::list;
47using std::map;
48using std::make_pair;
49using std::ostream;
50using std::ostringstream;
51using std::pair;
52using std::set;
53using std::string;
54using std::string_view;
55using std::stringstream;
56using std::to_string;
57using std::vector;
58
59using ceph::bufferlist;
60using ceph::decode;
61using ceph::encode;
62using ceph::ErasureCodeInterfaceRef;
63using ceph::ErasureCodeProfile;
64using ceph::Formatter;
65using ceph::JSONFormatter;
66using ceph::make_message;
67using ceph::mono_clock;
68using ceph::mono_time;
69
7c673cae
FG
70#define dout_subsys ceph_subsys_mon
71#undef dout_prefix
28e407b8 72#define dout_prefix _prefix(_dout, mon, get_fsmap())
f67539c2
TL
73static ostream& _prefix(std::ostream *_dout, Monitor &mon, const FSMap& fsmap) {
74 return *_dout << "mon." << mon.name << "@" << mon.rank
75 << "(" << mon.get_state_name()
7c673cae
FG
76 << ").mds e" << fsmap.get_epoch() << " ";
77}
78
3efd9988
FG
79static const string MDS_METADATA_PREFIX("mds_metadata");
80static const string MDS_HEALTH_PREFIX("mds_health");
81
82
7c673cae
FG
83/*
84 * Specialized implementation of cmd_getval to allow us to parse
85 * out strongly-typedef'd types
86 */
9f95a23c
TL
87namespace TOPNSPC::common {
88template<> bool cmd_getval(const cmdmap_t& cmdmap,
31f18b77 89 const std::string& k, mds_gid_t &val)
7c673cae 90{
9f95a23c 91 return cmd_getval(cmdmap, k, (int64_t&)val);
7c673cae
FG
92}
93
9f95a23c 94template<> bool cmd_getval(const cmdmap_t& cmdmap,
31f18b77 95 const std::string& k, mds_rank_t &val)
7c673cae 96{
9f95a23c 97 return cmd_getval(cmdmap, k, (int64_t&)val);
7c673cae
FG
98}
99
9f95a23c 100template<> bool cmd_getval(const cmdmap_t& cmdmap,
31f18b77 101 const std::string& k, MDSMap::DaemonState &val)
7c673cae 102{
9f95a23c
TL
103 return cmd_getval(cmdmap, k, (int64_t&)val);
104}
7c673cae 105}
7c673cae
FG
106// my methods
107
11fdf7f2
TL
108template <int dblV>
109void MDSMonitor::print_map(const FSMap& m)
7c673cae 110{
11fdf7f2 111 dout(dblV) << "print_map\n";
7c673cae
FG
112 m.print(*_dout);
113 *_dout << dendl;
114}
115
116// service methods
117void MDSMonitor::create_initial()
118{
119 dout(10) << "create_initial" << dendl;
120}
121
11fdf7f2 122void MDSMonitor::get_store_prefixes(std::set<string>& s) const
3efd9988
FG
123{
124 s.insert(service_name);
125 s.insert(MDS_METADATA_PREFIX);
126 s.insert(MDS_HEALTH_PREFIX);
127}
7c673cae
FG
128
129void MDSMonitor::update_from_paxos(bool *need_bootstrap)
130{
131 version_t version = get_last_committed();
28e407b8 132 if (version == get_fsmap().epoch)
7c673cae
FG
133 return;
134
135 dout(10) << __func__ << " version " << version
28e407b8 136 << ", my e " << get_fsmap().epoch << dendl;
11fdf7f2 137 ceph_assert(version > get_fsmap().epoch);
7c673cae 138
224ce89b
WB
139 load_health();
140
7c673cae
FG
141 // read and decode
142 bufferlist fsmap_bl;
143 fsmap_bl.clear();
144 int err = get_version(version, fsmap_bl);
11fdf7f2 145 ceph_assert(err == 0);
7c673cae 146
11fdf7f2 147 ceph_assert(fsmap_bl.length() > 0);
7c673cae 148 dout(10) << __func__ << " got " << version << dendl;
522d829b
TL
149 try {
150 PaxosFSMap::decode(fsmap_bl);
151 } catch (const ceph::buffer::malformed_input& e) {
152 derr << "unable to decode FSMap: " << e.what() << dendl;
153 throw;
154 }
7c673cae
FG
155
156 // new map
91327a77 157 dout(0) << "new map" << dendl;
11fdf7f2
TL
158 print_map<0>(get_fsmap());
159 if (!g_conf()->mon_mds_skip_sanity) {
28e407b8 160 get_fsmap().sanity();
7c673cae
FG
161 }
162
163 check_subs();
7c673cae
FG
164}
165
166void MDSMonitor::init()
167{
168 (void)load_metadata(pending_metadata);
169}
170
171void MDSMonitor::create_pending()
172{
28e407b8 173 auto &fsmap = PaxosFSMap::create_pending();
7c673cae 174
f67539c2
TL
175 if (mon.osdmon()->is_readable()) {
176 const auto &osdmap = mon.osdmon()->osdmap;
28e407b8 177 fsmap.sanitize([&osdmap](int64_t pool){return osdmap.have_pg_pool(pool);});
3efd9988
FG
178 }
179
28e407b8 180 dout(10) << "create_pending e" << fsmap.epoch << dendl;
7c673cae
FG
181}
182
183void MDSMonitor::encode_pending(MonitorDBStore::TransactionRef t)
184{
28e407b8
AA
185 auto &pending = get_pending_fsmap_writeable();
186 auto &epoch = pending.epoch;
7c673cae 187
28e407b8 188 dout(10) << "encode_pending e" << epoch << dendl;
7c673cae
FG
189
190 // print map iff 'debug mon = 30' or higher
11fdf7f2
TL
191 print_map<30>(pending);
192 if (!g_conf()->mon_mds_skip_sanity) {
28e407b8 193 pending.sanity();
7c673cae
FG
194 }
195
196 // Set 'modified' on maps modified this epoch
28e407b8
AA
197 for (auto &p : pending.filesystems) {
198 if (p.second->mds_map.epoch == epoch) {
199 p.second->mds_map.modified = ceph_clock_now();
7c673cae
FG
200 }
201 }
202
203 // apply to paxos
11fdf7f2 204 ceph_assert(get_last_committed() + 1 == pending.epoch);
28e407b8 205 bufferlist pending_bl;
f67539c2 206 pending.encode(pending_bl, mon.get_quorum_con_features());
7c673cae
FG
207
208 /* put everything in the transaction */
28e407b8
AA
209 put_version(t, pending.epoch, pending_bl);
210 put_last_committed(t, pending.epoch);
7c673cae
FG
211
212 // Encode MDSHealth data
213 for (std::map<uint64_t, MDSHealth>::iterator i = pending_daemon_health.begin();
214 i != pending_daemon_health.end(); ++i) {
215 bufferlist bl;
216 i->second.encode(bl);
217 t->put(MDS_HEALTH_PREFIX, stringify(i->first), bl);
218 }
219
220 for (std::set<uint64_t>::iterator i = pending_daemon_health_rm.begin();
221 i != pending_daemon_health_rm.end(); ++i) {
222 t->erase(MDS_HEALTH_PREFIX, stringify(*i));
223 }
224 pending_daemon_health_rm.clear();
1adf2230 225 remove_from_metadata(pending, t);
224ce89b
WB
226
227 // health
228 health_check_map_t new_checks;
28e407b8 229 const auto &info_map = pending.get_mds_info();
224ce89b
WB
230 for (const auto &i : info_map) {
231 const auto &gid = i.first;
232 const auto &info = i.second;
233 if (pending_daemon_health_rm.count(gid)) {
234 continue;
235 }
236 MDSHealth health;
237 auto p = pending_daemon_health.find(gid);
238 if (p != pending_daemon_health.end()) {
239 health = p->second;
240 } else {
241 bufferlist bl;
f67539c2 242 mon.store->get(MDS_HEALTH_PREFIX, stringify(gid), bl);
224ce89b
WB
243 if (!bl.length()) {
244 derr << "Missing health data for MDS " << gid << dendl;
245 continue;
246 }
11fdf7f2 247 auto bl_i = bl.cbegin();
224ce89b
WB
248 health.decode(bl_i);
249 }
250 for (const auto &metric : health.metrics) {
9f95a23c 251 const auto rank = info.rank;
224ce89b
WB
252 health_check_t *check = &new_checks.get_or_add(
253 mds_metric_name(metric.type),
254 metric.sev,
9f95a23c
TL
255 mds_metric_summary(metric.type),
256 1);
224ce89b 257 ostringstream ss;
f91f0fd5 258 ss << "mds." << info.name << "(mds." << rank << "): " << metric.message;
28e407b8
AA
259 bool first = true;
260 for (auto &p : metric.metadata) {
261 if (first) {
262 ss << " ";
263 } else {
224ce89b 264 ss << ", ";
28e407b8
AA
265 }
266 ss << p.first << ": " << p.second;
267 first = false;
224ce89b
WB
268 }
269 check->detail.push_back(ss.str());
270 }
271 }
28e407b8 272 pending.get_health_checks(&new_checks);
224ce89b 273 for (auto& p : new_checks.checks) {
11fdf7f2 274 p.second.summary = std::regex_replace(
224ce89b 275 p.second.summary,
11fdf7f2 276 std::regex("%num%"),
224ce89b 277 stringify(p.second.detail.size()));
11fdf7f2 278 p.second.summary = std::regex_replace(
224ce89b 279 p.second.summary,
11fdf7f2 280 std::regex("%plurals%"),
224ce89b 281 p.second.detail.size() > 1 ? "s" : "");
11fdf7f2 282 p.second.summary = std::regex_replace(
224ce89b 283 p.second.summary,
11fdf7f2 284 std::regex("%isorare%"),
224ce89b 285 p.second.detail.size() > 1 ? "are" : "is");
11fdf7f2 286 p.second.summary = std::regex_replace(
181888fb 287 p.second.summary,
11fdf7f2 288 std::regex("%hasorhave%"),
181888fb 289 p.second.detail.size() > 1 ? "have" : "has");
224ce89b
WB
290 }
291 encode_health(new_checks, t);
7c673cae
FG
292}
293
11fdf7f2 294version_t MDSMonitor::get_trim_to() const
7c673cae
FG
295{
296 version_t floor = 0;
11fdf7f2 297 if (g_conf()->mon_mds_force_trim_to > 0 &&
522d829b 298 g_conf()->mon_mds_force_trim_to <= (int)get_last_committed()) {
11fdf7f2 299 floor = g_conf()->mon_mds_force_trim_to;
7c673cae
FG
300 dout(10) << __func__ << " explicit mon_mds_force_trim_to = "
301 << floor << dendl;
302 }
303
11fdf7f2 304 unsigned max = g_conf()->mon_max_mdsmap_epochs;
7c673cae
FG
305 version_t last = get_last_committed();
306
522d829b
TL
307 if (last - get_first_committed() > max && floor < last - max) {
308 floor = last-max;
309 }
310
311 dout(20) << __func__ << " = " << floor << dendl;
7c673cae
FG
312 return floor;
313}
314
7c673cae
FG
315bool MDSMonitor::preprocess_query(MonOpRequestRef op)
316{
317 op->mark_mdsmon_event(__func__);
9f95a23c 318 auto m = op->get_req<PaxosServiceMessage>();
11fdf7f2
TL
319 dout(10) << "preprocess_query " << *m << " from " << m->get_orig_source()
320 << " " << m->get_orig_source_addrs() << dendl;
7c673cae
FG
321
322 switch (m->get_type()) {
323
324 case MSG_MDS_BEACON:
325 return preprocess_beacon(op);
326
327 case MSG_MON_COMMAND:
f64942e4
AA
328 try {
329 return preprocess_command(op);
11fdf7f2 330 } catch (const bad_cmd_get& e) {
f64942e4 331 bufferlist bl;
f67539c2 332 mon.reply_command(op, -EINVAL, e.what(), bl, get_last_committed());
f64942e4
AA
333 return true;
334 }
7c673cae
FG
335
336 case MSG_MDS_OFFLOAD_TARGETS:
337 return preprocess_offload_targets(op);
338
339 default:
340 ceph_abort();
341 return true;
342 }
343}
344
345void MDSMonitor::_note_beacon(MMDSBeacon *m)
346{
347 mds_gid_t gid = mds_gid_t(m->get_global_id());
348 version_t seq = m->get_seq();
349
91327a77 350 dout(5) << "_note_beacon " << *m << " noting time" << dendl;
1adf2230
AA
351 auto &beacon = last_beacon[gid];
352 beacon.stamp = mono_clock::now();
353 beacon.seq = seq;
7c673cae
FG
354}
355
356bool MDSMonitor::preprocess_beacon(MonOpRequestRef op)
357{
358 op->mark_mdsmon_event(__func__);
9f95a23c 359 auto m = op->get_req<MMDSBeacon>();
7c673cae
FG
360 MDSMap::DaemonState state = m->get_state();
361 mds_gid_t gid = m->get_global_id();
362 version_t seq = m->get_seq();
363 MDSMap::mds_info_t info;
364 epoch_t effective_epoch = 0;
365
1adf2230 366 const auto &fsmap = get_fsmap();
28e407b8 367
7c673cae 368 // check privileges, ignore if fails
11fdf7f2
TL
369 MonSession *session = op->get_session();
370 if (!session)
371 goto ignore;
7c673cae
FG
372 if (!session->is_capable("mds", MON_CAP_X)) {
373 dout(0) << "preprocess_beacon got MMDSBeacon from entity with insufficient privileges "
374 << session->caps << dendl;
375 goto ignore;
376 }
377
f67539c2
TL
378 if (m->get_fsid() != mon.monmap->fsid) {
379 dout(0) << "preprocess_beacon on fsid " << m->get_fsid() << " != " << mon.monmap->fsid << dendl;
7c673cae
FG
380 goto ignore;
381 }
382
91327a77 383 dout(5) << "preprocess_beacon " << *m
11fdf7f2
TL
384 << " from " << m->get_orig_source()
385 << " " << m->get_orig_source_addrs()
7c673cae
FG
386 << " " << m->get_compat()
387 << dendl;
388
389 // make sure the address has a port
390 if (m->get_orig_source_addr().get_port() == 0) {
391 dout(1) << " ignoring boot message without a port" << dendl;
392 goto ignore;
393 }
394
7c673cae 395 // fw to leader?
28e407b8 396 if (!is_leader())
7c673cae
FG
397 return false;
398
399 // booted, but not in map?
28e407b8 400 if (!fsmap.gid_exists(gid)) {
7c673cae
FG
401 if (state != MDSMap::STATE_BOOT) {
402 dout(7) << "mds_beacon " << *m << " is not in fsmap (state "
403 << ceph_mds_state_name(state) << ")" << dendl;
404
1adf2230
AA
405 /* We can't send an MDSMap this MDS was a part of because we no longer
406 * know which FS it was part of. Nor does this matter. Sending an empty
407 * MDSMap is sufficient for getting the MDS to respawn.
408 */
522d829b 409 auto m = make_message<MMDSMap>(mon.monmap->fsid, MDSMap::create_null_mdsmap());
f67539c2 410 mon.send_reply(op, m.detach());
7c673cae
FG
411 return true;
412 } else {
413 return false; // not booted yet.
414 }
415 }
416 dout(10) << __func__ << ": GID exists in map: " << gid << dendl;
28e407b8 417 info = fsmap.get_info_gid(gid);
7c673cae 418
f91f0fd5
TL
419 if (state == MDSMap::STATE_DNE) {
420 return false;
421 }
422
7c673cae
FG
423 // old seq?
424 if (info.state_seq > seq) {
425 dout(7) << "mds_beacon " << *m << " has old seq, ignoring" << dendl;
426 goto ignore;
427 }
428
429 // Work out the latest epoch that this daemon should have seen
430 {
28e407b8 431 fs_cluster_id_t fscid = fsmap.mds_roles.at(gid);
7c673cae 432 if (fscid == FS_CLUSTER_ID_NONE) {
28e407b8 433 effective_epoch = fsmap.standby_epochs.at(gid);
7c673cae 434 } else {
28e407b8 435 effective_epoch = fsmap.get_filesystem(fscid)->mds_map.epoch;
7c673cae
FG
436 }
437 if (effective_epoch != m->get_last_epoch_seen()) {
438 dout(10) << "mds_beacon " << *m
439 << " ignoring requested state, because mds hasn't seen latest map" << dendl;
440 goto reply;
441 }
442 }
443
444 if (info.laggy()) {
445 _note_beacon(m);
446 return false; // no longer laggy, need to update map.
447 }
448 if (state == MDSMap::STATE_BOOT) {
449 // ignore, already booted.
450 goto ignore;
451 }
9f95a23c
TL
452
453 // did the join_fscid change
454 if (m->get_fs().size()) {
455 fs_cluster_id_t fscid = FS_CLUSTER_ID_NONE;
456 auto f = fsmap.get_filesystem(m->get_fs());
457 if (f) {
458 fscid = f->fscid;
459 }
460 if (info.join_fscid != fscid) {
461 dout(10) << __func__ << " standby mds_join_fs changed to " << fscid
462 << " (" << m->get_fs() << ")" << dendl;
463 _note_beacon(m);
464 return false;
465 }
466 } else {
467 if (info.join_fscid != FS_CLUSTER_ID_NONE) {
468 dout(10) << __func__ << " standby mds_join_fs was cleared" << dendl;
469 _note_beacon(m);
470 return false;
471 }
472 }
473
7c673cae
FG
474 // is there a state change here?
475 if (info.state != state) {
476 // legal state change?
477 if ((info.state == MDSMap::STATE_STANDBY ||
478 info.state == MDSMap::STATE_STANDBY_REPLAY) && state > 0) {
479 dout(10) << "mds_beacon mds can't activate itself (" << ceph_mds_state_name(info.state)
480 << " -> " << ceph_mds_state_name(state) << ")" << dendl;
481 goto reply;
482 }
483
484 if ((state == MDSMap::STATE_STANDBY || state == MDSMap::STATE_STANDBY_REPLAY)
485 && info.rank != MDS_RANK_NONE)
486 {
487 dout(4) << "mds_beacon MDS can't go back into standby after taking rank: "
488 "held rank " << info.rank << " while requesting state "
489 << ceph_mds_state_name(state) << dendl;
490 goto reply;
491 }
492
493 _note_beacon(m);
494 return false;
495 }
496
497 // Comparing known daemon health with m->get_health()
498 // and return false (i.e. require proposal) if they
499 // do not match, to update our stored
500 if (!(pending_daemon_health[gid] == m->get_health())) {
91327a77 501 dout(10) << __func__ << " health metrics for gid " << gid << " were updated" << dendl;
7c673cae
FG
502 _note_beacon(m);
503 return false;
504 }
505
506 reply:
507 // note time and reply
11fdf7f2 508 ceph_assert(effective_epoch > 0);
7c673cae 509 _note_beacon(m);
11fdf7f2 510 {
f67539c2 511 auto beacon = make_message<MMDSBeacon>(mon.monmap->fsid,
11fdf7f2
TL
512 m->get_global_id(), m->get_name(), effective_epoch,
513 state, seq, CEPH_FEATURES_SUPPORTED_DEFAULT);
f67539c2 514 mon.send_reply(op, beacon.detach());
11fdf7f2 515 }
7c673cae
FG
516 return true;
517
518 ignore:
519 // I won't reply this beacon, drop it.
f67539c2 520 mon.no_reply(op);
7c673cae
FG
521 return true;
522}
523
524bool MDSMonitor::preprocess_offload_targets(MonOpRequestRef op)
525{
526 op->mark_mdsmon_event(__func__);
9f95a23c 527 auto m = op->get_req<MMDSLoadTargets>();
7c673cae 528 dout(10) << "preprocess_offload_targets " << *m << " from " << m->get_orig_source() << dendl;
28e407b8 529
1adf2230 530 const auto &fsmap = get_fsmap();
7c673cae
FG
531
532 // check privileges, ignore message if fails
11fdf7f2 533 MonSession *session = op->get_session();
7c673cae 534 if (!session)
1adf2230 535 goto ignore;
7c673cae
FG
536 if (!session->is_capable("mds", MON_CAP_X)) {
537 dout(0) << "preprocess_offload_targets got MMDSLoadTargets from entity with insufficient caps "
538 << session->caps << dendl;
1adf2230 539 goto ignore;
7c673cae
FG
540 }
541
542 if (fsmap.gid_exists(m->global_id) &&
543 m->targets == fsmap.get_info_gid(m->global_id).export_targets)
1adf2230 544 goto ignore;
7c673cae
FG
545
546 return false;
547
1adf2230 548 ignore:
f67539c2 549 mon.no_reply(op);
7c673cae
FG
550 return true;
551}
552
553
554bool MDSMonitor::prepare_update(MonOpRequestRef op)
555{
556 op->mark_mdsmon_event(__func__);
9f95a23c 557 auto m = op->get_req<PaxosServiceMessage>();
7c673cae
FG
558 dout(7) << "prepare_update " << *m << dendl;
559
560 switch (m->get_type()) {
561
562 case MSG_MDS_BEACON:
563 return prepare_beacon(op);
564
565 case MSG_MON_COMMAND:
f64942e4
AA
566 try {
567 return prepare_command(op);
11fdf7f2 568 } catch (const bad_cmd_get& e) {
f64942e4 569 bufferlist bl;
f67539c2 570 mon.reply_command(op, -EINVAL, e.what(), bl, get_last_committed());
f64942e4
AA
571 return true;
572 }
7c673cae
FG
573
574 case MSG_MDS_OFFLOAD_TARGETS:
575 return prepare_offload_targets(op);
576
577 default:
578 ceph_abort();
579 }
580
581 return true;
582}
583
584bool MDSMonitor::prepare_beacon(MonOpRequestRef op)
585{
586 op->mark_mdsmon_event(__func__);
9f95a23c 587 auto m = op->get_req<MMDSBeacon>();
7c673cae 588 // -- this is an update --
11fdf7f2
TL
589 dout(12) << "prepare_beacon " << *m << " from " << m->get_orig_source()
590 << " " << m->get_orig_source_addrs() << dendl;
591 entity_addrvec_t addrs = m->get_orig_source_addrs();
7c673cae
FG
592 mds_gid_t gid = m->get_global_id();
593 MDSMap::DaemonState state = m->get_state();
594 version_t seq = m->get_seq();
595
28e407b8
AA
596 auto &pending = get_pending_fsmap_writeable();
597
91327a77 598 dout(15) << __func__ << " got health from gid " << gid << " with " << m->get_health().metrics.size() << " metrics." << dendl;
7c673cae
FG
599
600 // Calculate deltas of health metrics created and removed
601 // Do this by type rather than MDSHealthMetric equality, because messages can
602 // change a lot when they include e.g. a number of items.
603 const auto &old_health = pending_daemon_health[gid].metrics;
604 const auto &new_health = m->get_health().metrics;
605
606 std::set<mds_metric_t> old_types;
607 for (const auto &i : old_health) {
608 old_types.insert(i.type);
609 }
610
611 std::set<mds_metric_t> new_types;
612 for (const auto &i : new_health) {
613 new_types.insert(i.type);
614 }
615
616 for (const auto &new_metric: new_health) {
617 if (old_types.count(new_metric.type) == 0) {
11fdf7f2 618 dout(10) << "MDS health message (" << m->get_orig_source()
28e407b8 619 << "): " << new_metric.sev << " " << new_metric.message << dendl;
7c673cae
FG
620 }
621 }
622
623 // Log the disappearance of health messages at INFO
624 for (const auto &old_metric : old_health) {
625 if (new_types.count(old_metric.type) == 0) {
f67539c2 626 mon.clog->info() << "MDS health message cleared ("
11fdf7f2 627 << m->get_orig_source() << "): " << old_metric.message;
7c673cae
FG
628 }
629 }
630
631 // Store health
632 pending_daemon_health[gid] = m->get_health();
633
522d829b 634 const auto& cs = m->get_compat();
7c673cae
FG
635 if (state == MDSMap::STATE_BOOT) {
636 // zap previous instance of this name?
11fdf7f2 637 if (g_conf()->mds_enforce_unique_name) {
7c673cae 638 bool failed_mds = false;
28e407b8 639 while (mds_gid_t existing = pending.find_mds_gid_by_name(m->get_name())) {
f67539c2
TL
640 if (!mon.osdmon()->is_writeable()) {
641 mon.osdmon()->wait_for_writeable(op, new C_RetryMessage(this, op));
7c673cae
FG
642 return false;
643 }
522d829b 644 const auto& existing_info = pending.get_info_gid(existing);
f67539c2 645 mon.clog->info() << existing_info.human_name() << " restarted";
1adf2230 646 fail_mds_gid(pending, existing);
7c673cae
FG
647 failed_mds = true;
648 }
649 if (failed_mds) {
f67539c2
TL
650 ceph_assert(mon.osdmon()->is_writeable());
651 request_proposal(mon.osdmon());
7c673cae
FG
652 }
653 }
654
655 // Add this daemon to the map
28e407b8 656 if (pending.mds_roles.count(gid) == 0) {
7c673cae
FG
657 MDSMap::mds_info_t new_info;
658 new_info.global_id = gid;
659 new_info.name = m->get_name();
11fdf7f2 660 new_info.addrs = addrs;
7c673cae
FG
661 new_info.mds_features = m->get_mds_features();
662 new_info.state = MDSMap::STATE_STANDBY;
663 new_info.state_seq = seq;
522d829b 664 new_info.compat = cs;
9f95a23c
TL
665 if (m->get_fs().size()) {
666 fs_cluster_id_t fscid = FS_CLUSTER_ID_NONE;
667 auto f = pending.get_filesystem(m->get_fs());
668 if (f) {
669 fscid = f->fscid;
670 }
671 new_info.join_fscid = fscid;
672 }
522d829b 673 pending.insert(new_info);
7c673cae
FG
674 }
675
7c673cae 676 // initialize the beacon timer
1adf2230
AA
677 auto &beacon = last_beacon[gid];
678 beacon.stamp = mono_clock::now();
679 beacon.seq = seq;
7c673cae 680
7c673cae
FG
681 update_metadata(m->get_global_id(), m->get_sys_info());
682 } else {
683 // state update
91327a77
AA
684
685 if (!pending.gid_exists(gid)) {
686 /* gid has been removed from pending, send null map */
687 dout(5) << "mds_beacon " << *m << " is not in fsmap (state "
688 << ceph_mds_state_name(state) << ")" << dendl;
689
690 /* We can't send an MDSMap this MDS was a part of because we no longer
691 * know which FS it was part of. Nor does this matter. Sending an empty
692 * MDSMap is sufficient for getting the MDS to respawn.
693 */
9f95a23c 694 wait_for_finished_proposal(op, new LambdaContext([op, this](int r){
91327a77 695 if (r >= 0) {
522d829b 696 auto m = make_message<MMDSMap>(mon.monmap->fsid, MDSMap::create_null_mdsmap());
f67539c2 697 mon.send_reply(op, m.detach());
91327a77
AA
698 } else {
699 dispatch(op); // try again
700 }
701 }));
702 return true;
703 }
704
11fdf7f2 705 const auto& info = pending.get_info_gid(gid);
522d829b
TL
706
707 // did the reported compat change? That's illegal!
708 if (cs.compare(info.compat) != 0) {
709 if (!mon.osdmon()->is_writeable()) {
710 mon.osdmon()->wait_for_writeable(op, new C_RetryMessage(this, op));
711 return false;
712 }
713 mon.clog->warn() << info.human_name() << " compat changed unexpectedly";
714 fail_mds_gid(pending, gid);
715 request_proposal(mon.osdmon());
716 return true;
717 }
718
f64942e4
AA
719 if (info.state == MDSMap::STATE_STOPPING &&
720 state != MDSMap::STATE_STOPPING &&
721 state != MDSMap::STATE_STOPPED) {
7c673cae
FG
722 // we can't transition to any other states from STOPPING
723 dout(0) << "got beacon for MDS in STATE_STOPPING, ignoring requested state change"
724 << dendl;
725 _note_beacon(m);
726 return true;
727 }
728
729 if (info.laggy()) {
11fdf7f2
TL
730 dout(1) << "prepare_beacon clearing laggy flag on " << addrs << dendl;
731 pending.modify_daemon(info.global_id, [](auto& info)
7c673cae 732 {
11fdf7f2 733 info.clear_laggy();
7c673cae
FG
734 }
735 );
736 }
9f95a23c 737
91327a77 738 dout(5) << "prepare_beacon mds." << info.rank
7c673cae
FG
739 << " " << ceph_mds_state_name(info.state)
740 << " -> " << ceph_mds_state_name(state)
7c673cae 741 << dendl;
9f95a23c
TL
742
743 fs_cluster_id_t fscid = FS_CLUSTER_ID_NONE;
744 if (m->get_fs().size()) {
745 auto f = pending.get_filesystem(m->get_fs());
746 if (f) {
747 fscid = f->fscid;
748 }
749 }
750 pending.modify_daemon(gid, [fscid](auto& info) {
751 info.join_fscid = fscid;
752 });
753
7c673cae 754 if (state == MDSMap::STATE_STOPPED) {
28e407b8
AA
755 const auto fscid = pending.mds_roles.at(gid);
756 const auto &fs = pending.get_filesystem(fscid);
181888fb 757
f67539c2 758 mon.clog->info() << info.human_name() << " finished "
11fdf7f2 759 << "stopping rank " << info.rank << " in filesystem "
d2e6a577 760 << fs->mds_map.fs_name << " (now has "
181888fb 761 << fs->mds_map.get_num_in_mds() - 1 << " ranks)";
d2e6a577 762
28e407b8 763 auto erased = pending.stop(gid);
7c673cae
FG
764 erased.push_back(gid);
765
9f95a23c 766 for (const auto& erased_gid : erased) {
7c673cae
FG
767 last_beacon.erase(erased_gid);
768 if (pending_daemon_health.count(erased_gid)) {
769 pending_daemon_health.erase(erased_gid);
770 pending_daemon_health_rm.insert(erased_gid);
771 }
772 }
d2e6a577
FG
773
774
7c673cae 775 } else if (state == MDSMap::STATE_DAMAGED) {
f67539c2 776 if (!mon.osdmon()->is_writeable()) {
91327a77 777 dout(1) << __func__ << ": DAMAGED from rank " << info.rank
f67539c2
TL
778 << " waiting for osdmon writeable to blocklist it" << dendl;
779 mon.osdmon()->wait_for_writeable(op, new C_RetryMessage(this, op));
7c673cae
FG
780 return false;
781 }
782
783 // Record this MDS rank as damaged, so that other daemons
784 // won't try to run it.
91327a77 785 dout(0) << __func__ << ": marking rank "
7c673cae
FG
786 << info.rank << " damaged" << dendl;
787
788 utime_t until = ceph_clock_now();
f67539c2
TL
789 until += g_conf().get_val<double>("mon_mds_blocklist_interval");
790 const auto blocklist_epoch = mon.osdmon()->blocklist(info.addrs, until);
791 request_proposal(mon.osdmon());
792 pending.damaged(gid, blocklist_epoch);
7c673cae
FG
793 last_beacon.erase(gid);
794
795 // Respond to MDS, so that it knows it can continue to shut down
9f95a23c 796 auto beacon = make_message<MMDSBeacon>(
f67539c2 797 mon.monmap->fsid, m->get_global_id(),
28e407b8 798 m->get_name(), pending.get_epoch(), state, seq,
11fdf7f2 799 CEPH_FEATURES_SUPPORTED_DEFAULT);
f67539c2 800 mon.send_reply(op, beacon.detach());
7c673cae 801 } else if (state == MDSMap::STATE_DNE) {
f67539c2 802 if (!mon.osdmon()->is_writeable()) {
91327a77 803 dout(1) << __func__ << ": DNE from rank " << info.rank
f67539c2
TL
804 << " waiting for osdmon writeable to blocklist it" << dendl;
805 mon.osdmon()->wait_for_writeable(op, new C_RetryMessage(this, op));
7c673cae
FG
806 return false;
807 }
808
1adf2230 809 fail_mds_gid(pending, gid);
f67539c2
TL
810 ceph_assert(mon.osdmon()->is_writeable());
811 request_proposal(mon.osdmon());
7c673cae
FG
812
813 // Respond to MDS, so that it knows it can continue to shut down
f67539c2 814 auto beacon = make_message<MMDSBeacon>(mon.monmap->fsid,
11fdf7f2
TL
815 m->get_global_id(), m->get_name(), pending.get_epoch(), state, seq,
816 CEPH_FEATURES_SUPPORTED_DEFAULT);
f67539c2 817 mon.send_reply(op, beacon.detach());
7c673cae
FG
818 } else if (info.state == MDSMap::STATE_STANDBY && state != info.state) {
819 // Standby daemons should never modify their own
820 // state. Reject any attempts to do so.
821 derr << "standby " << gid << " attempted to change state to "
822 << ceph_mds_state_name(state) << ", rejecting" << dendl;
823 return true;
824 } else if (info.state != MDSMap::STATE_STANDBY && state != info.state &&
825 !MDSMap::state_transition_valid(info.state, state)) {
826 // Validate state transitions for daemons that hold a rank
827 derr << "daemon " << gid << " (rank " << info.rank << ") "
828 << "reported invalid state transition "
829 << ceph_mds_state_name(info.state) << " -> "
830 << ceph_mds_state_name(state) << dendl;
831 return true;
832 } else {
b32b8144 833 if (info.state != MDSMap::STATE_ACTIVE && state == MDSMap::STATE_ACTIVE) {
28e407b8
AA
834 const auto &fscid = pending.mds_roles.at(gid);
835 const auto &fs = pending.get_filesystem(fscid);
f67539c2 836 mon.clog->info() << info.human_name() << " is now active in "
d2e6a577
FG
837 << "filesystem " << fs->mds_map.fs_name << " as rank "
838 << info.rank;
839 }
b32b8144
FG
840
841 // Made it through special cases and validations, record the
842 // daemon's reported state to the FSMap.
11fdf7f2
TL
843 pending.modify_daemon(gid, [state, seq](auto& info) {
844 info.state = state;
845 info.state_seq = seq;
b32b8144 846 });
7c673cae
FG
847 }
848 }
849
91327a77 850 dout(5) << "prepare_beacon pending map now:" << dendl;
28e407b8 851 print_map(pending);
7c673cae 852
9f95a23c 853 wait_for_finished_proposal(op, new LambdaContext([op, this](int r){
7c673cae
FG
854 if (r >= 0)
855 _updated(op); // success
856 else if (r == -ECANCELED) {
f67539c2 857 mon.no_reply(op);
7c673cae
FG
858 } else {
859 dispatch(op); // try again
860 }
861 }));
862
863 return true;
864}
865
866bool MDSMonitor::prepare_offload_targets(MonOpRequestRef op)
867{
28e407b8
AA
868 auto &pending = get_pending_fsmap_writeable();
869
7c673cae 870 op->mark_mdsmon_event(__func__);
9f95a23c 871 auto m = op->get_req<MMDSLoadTargets>();
7c673cae 872 mds_gid_t gid = m->global_id;
28e407b8 873 if (pending.gid_has_rank(gid)) {
7c673cae 874 dout(10) << "prepare_offload_targets " << gid << " " << m->targets << dendl;
28e407b8 875 pending.update_export_targets(gid, m->targets);
7c673cae
FG
876 } else {
877 dout(10) << "prepare_offload_targets " << gid << " not in map" << dendl;
878 }
f67539c2 879 mon.no_reply(op);
7c673cae
FG
880 return true;
881}
882
883bool MDSMonitor::should_propose(double& delay)
884{
885 // delegate to PaxosService to assess whether we should propose
886 return PaxosService::should_propose(delay);
887}
888
889void MDSMonitor::_updated(MonOpRequestRef op)
890{
28e407b8 891 const auto &fsmap = get_fsmap();
7c673cae 892 op->mark_mdsmon_event(__func__);
9f95a23c 893 auto m = op->get_req<MMDSBeacon>();
7c673cae 894 dout(10) << "_updated " << m->get_orig_source() << " " << *m << dendl;
f67539c2 895 mon.clog->debug() << m->get_orig_source() << " "
11fdf7f2
TL
896 << m->get_orig_source_addrs() << " "
897 << ceph_mds_state_name(m->get_state());
7c673cae
FG
898
899 if (m->get_state() == MDSMap::STATE_STOPPED) {
900 // send the map manually (they're out of the map, so they won't get it automatic)
522d829b 901 auto m = make_message<MMDSMap>(mon.monmap->fsid, MDSMap::create_null_mdsmap());
f67539c2 902 mon.send_reply(op, m.detach());
7c673cae 903 } else {
f67539c2 904 auto beacon = make_message<MMDSBeacon>(mon.monmap->fsid,
11fdf7f2
TL
905 m->get_global_id(), m->get_name(), fsmap.get_epoch(),
906 m->get_state(), m->get_seq(), CEPH_FEATURES_SUPPORTED_DEFAULT);
f67539c2 907 mon.send_reply(op, beacon.detach());
7c673cae
FG
908 }
909}
910
911void MDSMonitor::on_active()
912{
913 tick();
7c673cae 914
28e407b8 915 if (is_leader()) {
f67539c2 916 mon.clog->debug() << "fsmap " << get_fsmap();
224ce89b 917 }
7c673cae
FG
918}
919
7c673cae
FG
920void MDSMonitor::dump_info(Formatter *f)
921{
922 f->open_object_section("fsmap");
28e407b8 923 get_fsmap().dump(f);
7c673cae
FG
924 f->close_section();
925
926 f->dump_unsigned("mdsmap_first_committed", get_first_committed());
927 f->dump_unsigned("mdsmap_last_committed", get_last_committed());
928}
929
930bool MDSMonitor::preprocess_command(MonOpRequestRef op)
931{
932 op->mark_mdsmon_event(__func__);
9f95a23c 933 auto m = op->get_req<MMonCommand>();
7c673cae
FG
934 int r = -1;
935 bufferlist rdata;
936 stringstream ss, ds;
937
11fdf7f2 938 cmdmap_t cmdmap;
7c673cae
FG
939 if (!cmdmap_from_json(m->cmd, &cmdmap, ss)) {
940 // ss has reason for failure
941 string rs = ss.str();
f67539c2 942 mon.reply_command(op, -EINVAL, rs, rdata, get_last_committed());
7c673cae
FG
943 return true;
944 }
945
946 string prefix;
9f95a23c 947 cmd_getval(cmdmap, "prefix", prefix);
7c673cae 948 string format;
9f95a23c 949 cmd_getval(cmdmap, "format", format, string("plain"));
1adf2230 950 std::unique_ptr<Formatter> f(Formatter::create(format));
7c673cae 951
11fdf7f2 952 MonSession *session = op->get_session();
7c673cae 953 if (!session) {
f67539c2 954 mon.reply_command(op, -EACCES, "access denied", rdata, get_last_committed());
7c673cae
FG
955 return true;
956 }
957
f67539c2
TL
958 // to use const qualifier filter fsmap beforehand
959 FSMap _fsmap_copy = get_fsmap();
960 _fsmap_copy.filter(session->get_allowed_fs_names());
961 const auto& fsmap = _fsmap_copy;
962
7c673cae
FG
963 if (prefix == "mds stat") {
964 if (f) {
965 f->open_object_section("mds_stat");
966 dump_info(f.get());
967 f->close_section();
968 f->flush(ds);
969 } else {
970 ds << fsmap;
971 }
972 r = 0;
11fdf7f2
TL
973 } else if (prefix == "mds ok-to-stop") {
974 vector<string> ids;
9f95a23c 975 if (!cmd_getval(cmdmap, "ids", ids)) {
11fdf7f2
TL
976 r = -EINVAL;
977 ss << "must specify mds id";
978 goto out;
979 }
980 if (fsmap.is_any_degraded()) {
981 ss << "one or more filesystems is currently degraded";
982 r = -EBUSY;
983 goto out;
984 }
985 set<mds_gid_t> stopping;
986 for (auto& id : ids) {
987 ostringstream ess;
988 mds_gid_t gid = gid_from_arg(fsmap, id, ess);
989 if (gid == MDS_GID_NONE) {
990 // the mds doesn't exist, but no file systems are unhappy, so losing it
991 // can't have any effect.
992 continue;
993 }
994 stopping.insert(gid);
995 }
996 set<mds_gid_t> active;
997 set<mds_gid_t> standby;
998 for (auto gid : stopping) {
999 if (fsmap.gid_has_rank(gid)) {
1000 // ignore standby-replay daemons (at this level)
1001 if (!fsmap.is_standby_replay(gid)) {
1002 auto standby = fsmap.get_standby_replay(gid);
1003 if (standby == MDS_GID_NONE ||
1004 stopping.count(standby)) {
1005 // no standby-replay, or we're also stopping the standby-replay
1006 // for this mds
1007 active.insert(gid);
1008 }
1009 }
7c673cae 1010 } else {
11fdf7f2
TL
1011 // net loss of a standby
1012 standby.insert(gid);
7c673cae
FG
1013 }
1014 }
11fdf7f2
TL
1015 if (fsmap.get_num_standby() - standby.size() < active.size()) {
1016 r = -EBUSY;
1017 ss << "insufficent standby MDS daemons to stop active gids "
1018 << stringify(active)
1019 << " and/or standby gids " << stringify(standby);;
1020 goto out;
28e407b8 1021 }
11fdf7f2
TL
1022 r = 0;
1023 ss << "should be safe to stop " << ids;
7c673cae
FG
1024 } else if (prefix == "fs dump") {
1025 int64_t epocharg;
1026 epoch_t epoch;
1027
1adf2230 1028 const FSMap *fsmapp = &fsmap;
28e407b8 1029 FSMap dummy;
9f95a23c 1030 if (cmd_getval(cmdmap, "epoch", epocharg)) {
7c673cae
FG
1031 epoch = epocharg;
1032 bufferlist b;
1033 int err = get_version(epoch, b);
1034 if (err == -ENOENT) {
7c673cae 1035 r = -ENOENT;
28e407b8 1036 goto out;
7c673cae 1037 } else {
11fdf7f2
TL
1038 ceph_assert(err == 0);
1039 ceph_assert(b.length());
28e407b8
AA
1040 dummy.decode(b);
1041 fsmapp = &dummy;
7c673cae
FG
1042 }
1043 }
c07f9fc5 1044
28e407b8
AA
1045 stringstream ds;
1046 if (f != NULL) {
1047 f->open_object_section("fsmap");
1048 fsmapp->dump(f.get());
1049 f->close_section();
1050 f->flush(ds);
1051 r = 0;
1052 } else {
1053 fsmapp->print(ds);
1054 r = 0;
7c673cae 1055 }
28e407b8
AA
1056
1057 rdata.append(ds);
1058 ss << "dumped fsmap epoch " << fsmapp->get_epoch();
7c673cae
FG
1059 } else if (prefix == "mds metadata") {
1060 if (!f)
1061 f.reset(Formatter::create("json-pretty"));
1062
1063 string who;
9f95a23c 1064 bool all = !cmd_getval(cmdmap, "who", who);
7c673cae
FG
1065 dout(1) << "all = " << all << dendl;
1066 if (all) {
1067 r = 0;
1068 // Dump all MDSs' metadata
1069 const auto all_info = fsmap.get_mds_info();
1070
1071 f->open_array_section("mds_metadata");
1072 for(const auto &i : all_info) {
1073 const auto &info = i.second;
1074
1075 f->open_object_section("mds");
1076 f->dump_string("name", info.name);
1077 std::ostringstream get_err;
1adf2230 1078 r = dump_metadata(fsmap, info.name, f.get(), get_err);
7c673cae
FG
1079 if (r == -EINVAL || r == -ENOENT) {
1080 // Drop error, list what metadata we do have
1081 dout(1) << get_err.str() << dendl;
1082 r = 0;
1083 } else if (r != 0) {
1084 derr << "Unexpected error reading metadata: " << cpp_strerror(r)
1085 << dendl;
1086 ss << get_err.str();
c07f9fc5 1087 f->close_section();
7c673cae
FG
1088 break;
1089 }
1090 f->close_section();
1091 }
1092 f->close_section();
1093 } else {
1094 // Dump a single daemon's metadata
1095 f->open_object_section("mds_metadata");
1adf2230 1096 r = dump_metadata(fsmap, who, f.get(), ss);
7c673cae
FG
1097 f->close_section();
1098 }
1099 f->flush(ds);
31f18b77
FG
1100 } else if (prefix == "mds versions") {
1101 if (!f)
1102 f.reset(Formatter::create("json-pretty"));
1103 count_metadata("ceph_version", f.get());
1104 f->flush(ds);
1105 r = 0;
1106 } else if (prefix == "mds count-metadata") {
1107 if (!f)
1108 f.reset(Formatter::create("json-pretty"));
1109 string field;
9f95a23c 1110 cmd_getval(cmdmap, "property", field);
31f18b77
FG
1111 count_metadata(field, f.get());
1112 f->flush(ds);
1113 r = 0;
522d829b
TL
1114 } else if (prefix == "fs compat show") {
1115 string fs_name;
1116 cmd_getval(cmdmap, "fs_name", fs_name);
1117 const auto &fs = fsmap.get_filesystem(fs_name);
1118 if (fs == nullptr) {
1119 ss << "filesystem '" << fs_name << "' not found";
1120 r = -ENOENT;
1121 goto out;
1122 }
1123
1124 if (f) {
1125 f->open_object_section("mds_compat");
1126 fs->mds_map.compat.dump(f.get());
1127 f->close_section();
1128 f->flush(ds);
1129 } else {
1130 ds << fs->mds_map.compat;
1131 }
1132 r = 0;
7c673cae
FG
1133 } else if (prefix == "mds compat show") {
1134 if (f) {
1135 f->open_object_section("mds_compat");
522d829b 1136 fsmap.default_compat.dump(f.get());
7c673cae
FG
1137 f->close_section();
1138 f->flush(ds);
1139 } else {
522d829b 1140 ds << fsmap.default_compat;
7c673cae
FG
1141 }
1142 r = 0;
1143 } else if (prefix == "fs get") {
1144 string fs_name;
9f95a23c 1145 cmd_getval(cmdmap, "fs_name", fs_name);
28e407b8 1146 const auto &fs = fsmap.get_filesystem(fs_name);
7c673cae
FG
1147 if (fs == nullptr) {
1148 ss << "filesystem '" << fs_name << "' not found";
1149 r = -ENOENT;
1150 } else {
1151 if (f != nullptr) {
1152 f->open_object_section("filesystem");
1153 fs->dump(f.get());
1154 f->close_section();
1155 f->flush(ds);
1156 r = 0;
1157 } else {
1158 fs->print(ds);
1159 r = 0;
1160 }
1161 }
1162 } else if (prefix == "fs ls") {
1163 if (f) {
1164 f->open_array_section("filesystems");
1adf2230
AA
1165 for (const auto &p : fsmap.filesystems) {
1166 const auto &fs = p.second;
1167 f->open_object_section("filesystem");
1168 {
1169 const MDSMap &mds_map = fs->mds_map;
1170 f->dump_string("name", mds_map.fs_name);
1171 /* Output both the names and IDs of pools, for use by
1172 * humans and machines respectively */
f67539c2 1173 f->dump_string("metadata_pool", mon.osdmon()->osdmap.get_pool_name(
1adf2230
AA
1174 mds_map.metadata_pool));
1175 f->dump_int("metadata_pool_id", mds_map.metadata_pool);
1176 f->open_array_section("data_pool_ids");
1177 for (const auto &id : mds_map.data_pools) {
1178 f->dump_int("data_pool_id", id);
1179 }
1180 f->close_section();
7c673cae 1181
1adf2230
AA
1182 f->open_array_section("data_pools");
1183 for (const auto &id : mds_map.data_pools) {
f67539c2 1184 const auto &name = mon.osdmon()->osdmap.get_pool_name(id);
1adf2230 1185 f->dump_string("data_pool", name);
7c673cae
FG
1186 }
1187 f->close_section();
1188 }
1adf2230 1189 f->close_section();
7c673cae
FG
1190 }
1191 f->close_section();
1192 f->flush(ds);
1193 } else {
28e407b8
AA
1194 for (const auto &p : fsmap.filesystems) {
1195 const auto &fs = p.second;
7c673cae 1196 const MDSMap &mds_map = fs->mds_map;
f67539c2 1197 const string &md_pool_name = mon.osdmon()->osdmap.get_pool_name(
7c673cae
FG
1198 mds_map.metadata_pool);
1199
1200 ds << "name: " << mds_map.fs_name << ", metadata pool: "
1201 << md_pool_name << ", data pools: [";
1adf2230 1202 for (const auto &id : mds_map.data_pools) {
f67539c2 1203 const string &pool_name = mon.osdmon()->osdmap.get_pool_name(id);
7c673cae
FG
1204 ds << pool_name << " ";
1205 }
1206 ds << "]" << std::endl;
1207 }
1208
1209 if (fsmap.filesystems.empty()) {
1210 ds << "No filesystems enabled" << std::endl;
1211 }
1212 }
1213 r = 0;
f67539c2
TL
1214 } else if (prefix == "fs feature ls") {
1215 if (f) {
1216 f->open_array_section("cephfs_features");
1217 for (size_t i = 0; i <= CEPHFS_FEATURE_MAX; ++i) {
1218 f->open_object_section("feature");
1219 f->dump_int("index", i);
1220 f->dump_string("name", cephfs_feature_name(i));
1221 f->close_section();
1222 }
1223 f->close_section();
1224 f->flush(ds);
1225 } else {
1226 for (size_t i = 0; i <= CEPHFS_FEATURE_MAX; ++i) {
1227 ds << i << " " << cephfs_feature_name(i) << std::endl;
1228 }
1229 }
1230 r = 0;
7c673cae
FG
1231 }
1232
28e407b8 1233out:
7c673cae
FG
1234 if (r != -1) {
1235 rdata.append(ds);
1236 string rs;
1237 getline(ss, rs);
f67539c2 1238 mon.reply_command(op, r, rs, rdata, get_last_committed());
7c673cae
FG
1239 return true;
1240 } else
1241 return false;
1242}
1243
1adf2230 1244bool MDSMonitor::fail_mds_gid(FSMap &fsmap, mds_gid_t gid)
7c673cae 1245{
9f95a23c 1246 const auto& info = fsmap.get_info_gid(gid);
91327a77 1247 dout(1) << "fail_mds_gid " << gid << " mds." << info.name << " role " << info.rank << dendl;
7c673cae 1248
f67539c2 1249 ceph_assert(mon.osdmon()->is_writeable());
a8e16298 1250
f67539c2 1251 epoch_t blocklist_epoch = 0;
7c673cae
FG
1252 if (info.rank >= 0 && info.state != MDSMap::STATE_STANDBY_REPLAY) {
1253 utime_t until = ceph_clock_now();
f67539c2
TL
1254 until += g_conf().get_val<double>("mon_mds_blocklist_interval");
1255 blocklist_epoch = mon.osdmon()->blocklist(info.addrs, until);
7c673cae
FG
1256 }
1257
f67539c2 1258 fsmap.erase(gid, blocklist_epoch);
7c673cae
FG
1259 last_beacon.erase(gid);
1260 if (pending_daemon_health.count(gid)) {
1261 pending_daemon_health.erase(gid);
1262 pending_daemon_health_rm.insert(gid);
1263 }
1264
f67539c2 1265 return blocklist_epoch != 0;
7c673cae
FG
1266}
1267
1adf2230 1268mds_gid_t MDSMonitor::gid_from_arg(const FSMap &fsmap, const std::string &arg, std::ostream &ss)
7c673cae
FG
1269{
1270 // Try parsing as a role
1271 mds_role_t role;
1272 std::ostringstream ignore_err; // Don't spam 'ss' with parse_role errors
1adf2230 1273 int r = fsmap.parse_role(arg, &role, ignore_err);
7c673cae
FG
1274 if (r == 0) {
1275 // See if a GID is assigned to this role
28e407b8 1276 const auto &fs = fsmap.get_filesystem(role.fscid);
11fdf7f2 1277 ceph_assert(fs != nullptr); // parse_role ensures it exists
7c673cae
FG
1278 if (fs->mds_map.is_up(role.rank)) {
1279 dout(10) << __func__ << ": validated rank/GID " << role
1280 << " as a rank" << dendl;
1281 return fs->mds_map.get_mds_info(role.rank).global_id;
1282 }
1283 }
1284
1285 // Try parsing as a gid
1286 std::string err;
1287 unsigned long long maybe_gid = strict_strtoll(arg.c_str(), 10, &err);
1288 if (!err.empty()) {
1289 // Not a role or a GID, try as a daemon name
28e407b8 1290 const MDSMap::mds_info_t *mds_info = fsmap.find_by_name(arg);
7c673cae
FG
1291 if (!mds_info) {
1292 ss << "MDS named '" << arg
1293 << "' does not exist, or is not up";
1294 return MDS_GID_NONE;
1295 }
1296 dout(10) << __func__ << ": resolved MDS name '" << arg
1297 << "' to GID " << mds_info->global_id << dendl;
1298 return mds_info->global_id;
1299 } else {
1300 // Not a role, but parses as a an integer, might be a GID
1301 dout(10) << __func__ << ": treating MDS reference '" << arg
1302 << "' as an integer " << maybe_gid << dendl;
31f18b77 1303
28e407b8 1304 if (fsmap.gid_exists(mds_gid_t(maybe_gid))) {
31f18b77 1305 return mds_gid_t(maybe_gid);
7c673cae
FG
1306 }
1307 }
1308
1309 dout(1) << __func__ << ": rank/GID " << arg
1310 << " not a existent rank or GID" << dendl;
1311 return MDS_GID_NONE;
1312}
1313
1adf2230
AA
1314int MDSMonitor::fail_mds(FSMap &fsmap, std::ostream &ss,
1315 const std::string &arg, MDSMap::mds_info_t *failed_info)
7c673cae 1316{
11fdf7f2 1317 ceph_assert(failed_info != nullptr);
d2e6a577 1318
1adf2230 1319 mds_gid_t gid = gid_from_arg(fsmap, arg, ss);
7c673cae
FG
1320 if (gid == MDS_GID_NONE) {
1321 return 0;
1322 }
f67539c2 1323 if (!mon.osdmon()->is_writeable()) {
7c673cae
FG
1324 return -EAGAIN;
1325 }
d2e6a577
FG
1326
1327 // Take a copy of the info before removing the MDS from the map,
1328 // so that the caller knows which mds (if any) they ended up removing.
1adf2230 1329 *failed_info = fsmap.get_info_gid(gid);
d2e6a577 1330
1adf2230 1331 fail_mds_gid(fsmap, gid);
7c673cae 1332 ss << "failed mds gid " << gid;
f67539c2
TL
1333 ceph_assert(mon.osdmon()->is_writeable());
1334 request_proposal(mon.osdmon());
7c673cae
FG
1335 return 0;
1336}
1337
1338bool MDSMonitor::prepare_command(MonOpRequestRef op)
1339{
1340 op->mark_mdsmon_event(__func__);
9f95a23c 1341 auto m = op->get_req<MMonCommand>();
7c673cae
FG
1342 int r = -EINVAL;
1343 stringstream ss;
1344 bufferlist rdata;
1345
11fdf7f2 1346 cmdmap_t cmdmap;
7c673cae
FG
1347 if (!cmdmap_from_json(m->cmd, &cmdmap, ss)) {
1348 string rs = ss.str();
f67539c2 1349 mon.reply_command(op, -EINVAL, rs, rdata, get_last_committed());
7c673cae
FG
1350 return true;
1351 }
1352
1353 string prefix;
9f95a23c 1354 cmd_getval(cmdmap, "prefix", prefix);
7c673cae
FG
1355
1356 /* Refuse access if message not associated with a valid session */
11fdf7f2 1357 MonSession *session = op->get_session();
7c673cae 1358 if (!session) {
f67539c2 1359 mon.reply_command(op, -EACCES, "access denied", rdata, get_last_committed());
7c673cae
FG
1360 return true;
1361 }
1362
28e407b8
AA
1363 auto &pending = get_pending_fsmap_writeable();
1364
c07f9fc5 1365 bool batched_propose = false;
28e407b8 1366 for (const auto &h : handlers) {
f67539c2
TL
1367 r = h->can_handle(prefix, op, pending, cmdmap, ss);
1368 if (r == 1) {
1369 ; // pass, since we got the right handler.
1370 } else if (r == 0) {
1371 continue;
1372 } else {
1373 goto out;
1374 }
c07f9fc5 1375
f67539c2
TL
1376 batched_propose = h->batched_propose();
1377 if (batched_propose) {
1378 paxos.plug();
1379 }
1380 r = h->handle(&mon, pending, op, cmdmap, ss);
1381 if (batched_propose) {
1382 paxos.unplug();
1383 }
1384
1385 if (r == -EAGAIN) {
1386 // message has been enqueued for retry; return.
1387 dout(4) << __func__ << " enqueue for retry by prepare_command" << dendl;
1388 return false;
1389 } else {
1390 if (r == 0) {
1391 // On successful updates, print the updated map
1392 print_map(pending);
7c673cae 1393 }
f67539c2
TL
1394 // Successful or not, we're done: respond.
1395 goto out;
7c673cae
FG
1396 }
1397 }
1398
1adf2230 1399 r = filesystem_command(pending, op, prefix, cmdmap, ss);
7c673cae
FG
1400 if (r >= 0) {
1401 goto out;
1402 } else if (r == -EAGAIN) {
1403 // Do not reply, the message has been enqueued for retry
1404 dout(4) << __func__ << " enqueue for retry by filesystem_command" << dendl;
1405 return false;
1406 } else if (r != -ENOSYS) {
1407 goto out;
1408 }
1409
7c673cae
FG
1410 if (r == -ENOSYS && ss.str().empty()) {
1411 ss << "unrecognized command";
1412 }
1413
1414out:
1415 dout(4) << __func__ << " done, r=" << r << dendl;
1416 /* Compose response */
1417 string rs;
1418 getline(ss, rs);
1419
1420 if (r >= 0) {
1421 // success.. delay reply
1422 wait_for_finished_proposal(op, new Monitor::C_Command(mon, op, r, rs,
1423 get_last_committed() + 1));
c07f9fc5
FG
1424 if (batched_propose) {
1425 force_immediate_propose();
1426 }
7c673cae
FG
1427 return true;
1428 } else {
1429 // reply immediately
f67539c2 1430 mon.reply_command(op, r, rs, rdata, get_last_committed());
7c673cae
FG
1431 return false;
1432 }
1433}
1434
7c673cae 1435int MDSMonitor::filesystem_command(
1adf2230 1436 FSMap &fsmap,
7c673cae
FG
1437 MonOpRequestRef op,
1438 std::string const &prefix,
11fdf7f2 1439 const cmdmap_t& cmdmap,
7c673cae
FG
1440 std::stringstream &ss)
1441{
1442 dout(4) << __func__ << " prefix='" << prefix << "'" << dendl;
1443 op->mark_mdsmon_event(__func__);
1444 int r = 0;
1445 string whostr;
9f95a23c 1446 cmd_getval(cmdmap, "role", whostr);
7c673cae 1447
11fdf7f2 1448 if (prefix == "mds set_state") {
7c673cae 1449 mds_gid_t gid;
9f95a23c 1450 if (!cmd_getval(cmdmap, "gid", gid)) {
7c673cae 1451 ss << "error parsing 'gid' value '"
11fdf7f2 1452 << cmd_vartype_stringify(cmdmap.at("gid")) << "'";
7c673cae
FG
1453 return -EINVAL;
1454 }
1455 MDSMap::DaemonState state;
9f95a23c 1456 if (!cmd_getval(cmdmap, "state", state)) {
7c673cae 1457 ss << "error parsing 'state' string value '"
11fdf7f2 1458 << cmd_vartype_stringify(cmdmap.at("state")) << "'";
7c673cae
FG
1459 return -EINVAL;
1460 }
f67539c2 1461 if (fsmap.gid_exists(gid, op->get_session()->get_allowed_fs_names())) {
11fdf7f2
TL
1462 fsmap.modify_daemon(gid, [state](auto& info) {
1463 info.state = state;
7c673cae
FG
1464 });
1465 ss << "set mds gid " << gid << " to state " << state << " "
1466 << ceph_mds_state_name(state);
1467 return 0;
1468 }
1469 } else if (prefix == "mds fail") {
1470 string who;
9f95a23c 1471 cmd_getval(cmdmap, "role_or_gid", who);
d2e6a577
FG
1472
1473 MDSMap::mds_info_t failed_info;
f67539c2
TL
1474 mds_gid_t gid = gid_from_arg(fsmap, who, ss);
1475 if (gid == MDS_GID_NONE) {
1476 ss << "MDS named '" << who << "' does not exist, is not up or you "
1477 << "lack the permission to see.";
1478 return 0;
1479 }
1480 if(!fsmap.gid_exists(gid, op->get_session()->get_allowed_fs_names())) {
1481 ss << "MDS named '" << who << "' does not exist, is not up or you "
1482 << "lack the permission to see.";
1483 return -EINVAL;
1484 }
1485 string_view fs_name = fsmap.fs_name_from_gid(gid);
1486 if (!op->get_session()->fs_name_capable(fs_name, MON_CAP_W)) {
1487 ss << "Permission denied.";
1488 return -EPERM;
1489 }
1490
1adf2230 1491 r = fail_mds(fsmap, ss, who, &failed_info);
7c673cae 1492 if (r < 0 && r == -EAGAIN) {
f67539c2 1493 mon.osdmon()->wait_for_writeable(op, new C_RetryMessage(this, op));
7c673cae 1494 return -EAGAIN; // don't propose yet; wait for message to be retried
d2e6a577
FG
1495 } else if (r == 0) {
1496 // Only log if we really did something (not when was already gone)
1497 if (failed_info.global_id != MDS_GID_NONE) {
f67539c2 1498 mon.clog->info() << failed_info.human_name() << " marked failed by "
d2e6a577
FG
1499 << op->get_session()->entity_name;
1500 }
7c673cae
FG
1501 }
1502 } else if (prefix == "mds rm") {
1503 mds_gid_t gid;
9f95a23c 1504 if (!cmd_getval(cmdmap, "gid", gid)) {
7c673cae 1505 ss << "error parsing 'gid' value '"
11fdf7f2 1506 << cmd_vartype_stringify(cmdmap.at("gid")) << "'";
7c673cae
FG
1507 return -EINVAL;
1508 }
f67539c2 1509 if (!fsmap.gid_exists(gid, op->get_session()->get_allowed_fs_names())) {
11fdf7f2 1510 ss << "mds gid " << gid << " does not exist";
f67539c2
TL
1511 return 0;
1512 }
1513 string_view fs_name = fsmap.fs_name_from_gid(gid);
1514 if (!op->get_session()->fs_name_capable(fs_name, MON_CAP_W)) {
1515 ss << "Permission denied.";
1516 return -EPERM;
1517 }
1518 const auto &info = fsmap.get_info_gid(gid);
1519 MDSMap::DaemonState state = info.state;
1520 if (state > 0) {
1521 ss << "cannot remove active mds." << info.name
1522 << " rank " << info.rank;
1523 return -EBUSY;
7c673cae 1524 } else {
f67539c2
TL
1525 fsmap.erase(gid, {});
1526 ss << "removed mds gid " << gid;
1527 return 0;
7c673cae
FG
1528 }
1529 } else if (prefix == "mds rmfailed") {
11fdf7f2 1530 bool confirm = false;
9f95a23c 1531 cmd_getval(cmdmap, "yes_i_really_mean_it", confirm);
11fdf7f2 1532 if (!confirm) {
7c673cae
FG
1533 ss << "WARNING: this can make your filesystem inaccessible! "
1534 "Add --yes-i-really-mean-it if you are sure you wish to continue.";
1535 return -EPERM;
1536 }
1537
1538 std::string role_str;
9f95a23c 1539 cmd_getval(cmdmap, "role", role_str);
7c673cae 1540 mds_role_t role;
f67539c2
TL
1541 const auto fs_names = op->get_session()->get_allowed_fs_names();
1542 int r = fsmap.parse_role(role_str, &role, ss, fs_names);
7c673cae
FG
1543 if (r < 0) {
1544 ss << "invalid role '" << role_str << "'";
1545 return -EINVAL;
1546 }
f67539c2
TL
1547 string_view fs_name = fsmap.get_filesystem(role.fscid)->mds_map.get_fs_name();
1548 if (!op->get_session()->fs_name_capable(fs_name, MON_CAP_W)) {
1549 ss << "Permission denied.";
1550 return -EPERM;
1551 }
7c673cae 1552
1adf2230 1553 fsmap.modify_filesystem(
7c673cae
FG
1554 role.fscid,
1555 [role](std::shared_ptr<Filesystem> fs)
1556 {
1557 fs->mds_map.failed.erase(role.rank);
1558 });
1559
1560 ss << "removed failed mds." << role;
1561 return 0;
522d829b 1562 /* TODO: convert to fs commands to update defaults */
7c673cae
FG
1563 } else if (prefix == "mds compat rm_compat") {
1564 int64_t f;
9f95a23c 1565 if (!cmd_getval(cmdmap, "feature", f)) {
7c673cae 1566 ss << "error parsing feature value '"
11fdf7f2 1567 << cmd_vartype_stringify(cmdmap.at("feature")) << "'";
7c673cae
FG
1568 return -EINVAL;
1569 }
522d829b 1570 if (fsmap.default_compat.compat.contains(f)) {
7c673cae 1571 ss << "removing compat feature " << f;
522d829b 1572 fsmap.default_compat.compat.remove(f);
7c673cae 1573 } else {
522d829b 1574 ss << "compat feature " << f << " not present in " << fsmap.default_compat;
7c673cae
FG
1575 }
1576 r = 0;
1577 } else if (prefix == "mds compat rm_incompat") {
1578 int64_t f;
9f95a23c 1579 if (!cmd_getval(cmdmap, "feature", f)) {
7c673cae 1580 ss << "error parsing feature value '"
11fdf7f2 1581 << cmd_vartype_stringify(cmdmap.at("feature")) << "'";
7c673cae
FG
1582 return -EINVAL;
1583 }
522d829b 1584 if (fsmap.default_compat.incompat.contains(f)) {
7c673cae 1585 ss << "removing incompat feature " << f;
522d829b 1586 fsmap.default_compat.incompat.remove(f);
7c673cae 1587 } else {
522d829b 1588 ss << "incompat feature " << f << " not present in " << fsmap.default_compat;
7c673cae
FG
1589 }
1590 r = 0;
1591 } else if (prefix == "mds repaired") {
1592 std::string role_str;
9f95a23c 1593 cmd_getval(cmdmap, "role", role_str);
7c673cae 1594 mds_role_t role;
f67539c2
TL
1595 const auto fs_names = op->get_session()->get_allowed_fs_names();
1596 r = fsmap.parse_role(role_str, &role, ss, fs_names);
7c673cae
FG
1597 if (r < 0) {
1598 return r;
1599 }
f67539c2
TL
1600 string_view fs_name = fsmap.get_filesystem(role.fscid)->mds_map.get_fs_name();
1601 if (!op->get_session()->fs_name_capable(fs_name, MON_CAP_W)) {
1602 ss << "Permission denied.";
1603 return -EPERM;
1604 }
7c673cae 1605
1adf2230 1606 bool modified = fsmap.undamaged(role.fscid, role.rank);
7c673cae 1607 if (modified) {
494da23a 1608 ss << "repaired: restoring rank " << role;
7c673cae 1609 } else {
494da23a 1610 ss << "nothing to do: rank is not damaged";
7c673cae
FG
1611 }
1612
1613 r = 0;
11fdf7f2
TL
1614 } else if (prefix == "mds freeze") {
1615 std::string who;
9f95a23c 1616 cmd_getval(cmdmap, "role_or_gid", who);
11fdf7f2
TL
1617 mds_gid_t gid = gid_from_arg(fsmap, who, ss);
1618 if (gid == MDS_GID_NONE) {
7c673cae
FG
1619 return -EINVAL;
1620 }
1621
f67539c2
TL
1622 string_view fs_name = fsmap.fs_name_from_gid(gid);
1623 if (!op->get_session()->fs_name_capable(fs_name, MON_CAP_W)) {
1624 ss << "Permission denied.";
1625 return -EPERM;
1626 }
1627
11fdf7f2 1628 bool freeze = false;
7c673cae 1629 {
11fdf7f2 1630 std::string str;
9f95a23c 1631 cmd_getval(cmdmap, "val", str);
11fdf7f2
TL
1632 if ((r = parse_bool(str, &freeze, ss)) != 0) {
1633 return r;
1634 }
1635 }
7c673cae 1636
11fdf7f2
TL
1637 auto f = [freeze,gid,&ss](auto& info) {
1638 if (freeze) {
1639 ss << "freezing mds." << gid;
1640 info.freeze();
1641 } else {
1642 ss << "unfreezing mds." << gid;
1643 info.unfreeze();
1644 }
1645 };
1646 fsmap.modify_daemon(gid, f);
7c673cae
FG
1647 r = 0;
1648 } else {
1649 return -ENOSYS;
1650 }
1651
1652 return r;
1653}
1654
7c673cae
FG
1655void MDSMonitor::check_subs()
1656{
7c673cae
FG
1657 // Subscriptions may be to "mdsmap" (MDS and legacy clients),
1658 // "mdsmap.<namespace>", or to "fsmap" for the full state of all
1659 // filesystems. Build a list of all the types we service
1660 // subscriptions for.
9f95a23c
TL
1661
1662 std::vector<std::string> types = {
1663 "fsmap",
1664 "fsmap.user",
1665 "mdsmap",
1666 };
1667
28e407b8
AA
1668 for (const auto &p : get_fsmap().filesystems) {
1669 const auto &fscid = p.first;
9f95a23c
TL
1670 CachedStackStringStream cos;
1671 *cos << "mdsmap." << fscid;
1672 types.push_back(std::string(cos->strv()));
7c673cae
FG
1673 }
1674
1675 for (const auto &type : types) {
f67539c2 1676 auto& subs = mon.session_map.subs;
9f95a23c
TL
1677 auto subs_it = subs.find(type);
1678 if (subs_it == subs.end())
7c673cae 1679 continue;
9f95a23c
TL
1680 auto sub_it = subs_it->second->begin();
1681 while (!sub_it.end()) {
1682 auto sub = *sub_it;
1683 ++sub_it; // N.B. check_sub may remove sub!
7c673cae
FG
1684 check_sub(sub);
1685 }
1686 }
1687}
1688
1689
1690void MDSMonitor::check_sub(Subscription *sub)
1691{
1692 dout(20) << __func__ << ": " << sub->type << dendl;
1693
f67539c2
TL
1694 // to use const qualifier filter fsmap beforehand
1695 FSMap _fsmap_copy = get_fsmap();
1696 _fsmap_copy.filter(sub->session->get_allowed_fs_names());
1697 const auto& fsmap = _fsmap_copy;
1698 if (sub->next > fsmap.get_epoch()) {
1699 return;
1700 }
28e407b8 1701
7c673cae 1702 if (sub->type == "fsmap") {
f67539c2
TL
1703 sub->session->con->send_message(new MFSMap(mon.monmap->fsid, fsmap));
1704 if (sub->onetime) {
1705 mon.session_map.remove_sub(sub);
1706 } else {
1707 sub->next = fsmap.get_epoch() + 1;
7c673cae
FG
1708 }
1709 } else if (sub->type == "fsmap.user") {
f67539c2
TL
1710 FSMapUser fsmap_u;
1711 fsmap_u.epoch = fsmap.get_epoch();
1712 fsmap_u.legacy_client_fscid = fsmap.legacy_client_fscid;
1713 for (const auto &p : fsmap.filesystems) {
1714 FSMapUser::fs_info_t& fs_info = fsmap_u.filesystems[p.second->fscid];
1715 fs_info.cid = p.second->fscid;
1716 fs_info.name = p.second->mds_map.fs_name;
1717 }
1718 sub->session->con->send_message(new MFSMapUser(mon.monmap->fsid, fsmap_u));
1719 if (sub->onetime) {
1720 mon.session_map.remove_sub(sub);
1721 } else {
1722 sub->next = fsmap.get_epoch() + 1;
7c673cae
FG
1723 }
1724 } else if (sub->type.compare(0, 6, "mdsmap") == 0) {
11fdf7f2 1725 const bool is_mds = sub->session->name.is_mds();
7c673cae
FG
1726 mds_gid_t mds_gid = MDS_GID_NONE;
1727 fs_cluster_id_t fscid = FS_CLUSTER_ID_NONE;
1728 if (is_mds) {
1729 // What (if any) namespace are you assigned to?
1730 auto mds_info = fsmap.get_mds_info();
1adf2230 1731 for (const auto &p : mds_info) {
11fdf7f2 1732 if (p.second.addrs == sub->session->addrs) {
1adf2230 1733 mds_gid = p.first;
7c673cae
FG
1734 fscid = fsmap.mds_roles.at(mds_gid);
1735 }
1736 }
1737 } else {
1738 // You're a client. Did you request a particular
1739 // namespace?
11fdf7f2 1740 if (sub->type.compare(0, 7, "mdsmap.") == 0) {
7c673cae
FG
1741 auto namespace_id_str = sub->type.substr(std::string("mdsmap.").size());
1742 dout(10) << __func__ << ": namespace_id " << namespace_id_str << dendl;
1743 std::string err;
1744 fscid = strict_strtoll(namespace_id_str.c_str(), 10, &err);
1745 if (!err.empty()) {
1746 // Client asked for a non-existent namespace, send them nothing
1747 dout(1) << "Invalid client subscription '" << sub->type
1748 << "'" << dendl;
1749 return;
1750 }
7c673cae
FG
1751 } else {
1752 // Unqualified request for "mdsmap": give it the one marked
1753 // for use by legacy clients.
1754 if (fsmap.legacy_client_fscid != FS_CLUSTER_ID_NONE) {
1755 fscid = fsmap.legacy_client_fscid;
1756 } else {
1757 dout(1) << "Client subscribed for legacy filesystem but "
1758 "none is configured" << dendl;
1759 return;
1760 }
1761 }
b3b6e05e
TL
1762 if (!fsmap.filesystem_exists(fscid)) {
1763 // Client asked for a non-existent namespace, send them nothing
1764 // TODO: something more graceful for when a client has a filesystem
1765 // mounted, and the fileysstem is deleted. Add a "shut down you fool"
1766 // flag to MMDSMap?
1767 dout(1) << "Client subscribed to non-existent namespace '" <<
1768 fscid << "'" << dendl;
1769 return;
1770 }
7c673cae
FG
1771 }
1772 dout(10) << __func__ << ": is_mds=" << is_mds << ", fscid= " << fscid << dendl;
1773
1774 // Work out the effective latest epoch
28e407b8 1775 const MDSMap *mds_map = nullptr;
522d829b 1776 MDSMap null_map = MDSMap::create_null_mdsmap();
7c673cae
FG
1777 if (fscid == FS_CLUSTER_ID_NONE) {
1778 // For a client, we should have already dropped out
11fdf7f2 1779 ceph_assert(is_mds);
7c673cae 1780
28e407b8
AA
1781 auto it = fsmap.standby_daemons.find(mds_gid);
1782 if (it != fsmap.standby_daemons.end()) {
7c673cae 1783 // For an MDS, we need to feed it an MDSMap with its own state in
28e407b8
AA
1784 null_map.mds_info[mds_gid] = it->second;
1785 null_map.epoch = fsmap.standby_epochs.at(mds_gid);
7c673cae
FG
1786 } else {
1787 null_map.epoch = fsmap.epoch;
1788 }
1789 mds_map = &null_map;
1790 } else {
1791 // Check the effective epoch
28e407b8 1792 mds_map = &fsmap.get_filesystem(fscid)->mds_map;
7c673cae
FG
1793 }
1794
11fdf7f2 1795 ceph_assert(mds_map != nullptr);
7c673cae
FG
1796 dout(10) << __func__ << " selected MDS map epoch " <<
1797 mds_map->epoch << " for namespace " << fscid << " for subscriber "
11fdf7f2 1798 << sub->session->name << " who wants epoch " << sub->next << dendl;
7c673cae
FG
1799
1800 if (sub->next > mds_map->epoch) {
1801 return;
1802 }
f67539c2
TL
1803 auto msg = make_message<MMDSMap>(mon.monmap->fsid, *mds_map,
1804 mds_map->fs_name);
7c673cae 1805
11fdf7f2 1806 sub->session->con->send_message(msg.detach());
7c673cae 1807 if (sub->onetime) {
f67539c2 1808 mon.session_map.remove_sub(sub);
7c673cae
FG
1809 } else {
1810 sub->next = mds_map->get_epoch() + 1;
1811 }
1812 }
1813}
1814
1815
1816void MDSMonitor::update_metadata(mds_gid_t gid,
1817 const map<string, string>& metadata)
1818{
1819 if (metadata.empty()) {
1820 return;
1821 }
1822 pending_metadata[gid] = metadata;
1823
f67539c2 1824 MonitorDBStore::TransactionRef t = paxos.get_pending_transaction();
7c673cae 1825 bufferlist bl;
11fdf7f2 1826 encode(pending_metadata, bl);
7c673cae 1827 t->put(MDS_METADATA_PREFIX, "last_metadata", bl);
f67539c2 1828 paxos.trigger_propose();
7c673cae
FG
1829}
1830
1adf2230 1831void MDSMonitor::remove_from_metadata(const FSMap &fsmap, MonitorDBStore::TransactionRef t)
7c673cae
FG
1832{
1833 bool update = false;
1adf2230
AA
1834 for (auto it = pending_metadata.begin(); it != pending_metadata.end(); ) {
1835 if (!fsmap.gid_exists(it->first)) {
1836 it = pending_metadata.erase(it);
7c673cae
FG
1837 update = true;
1838 } else {
1adf2230 1839 ++it;
7c673cae
FG
1840 }
1841 }
1842 if (!update)
1843 return;
1844 bufferlist bl;
11fdf7f2 1845 encode(pending_metadata, bl);
7c673cae
FG
1846 t->put(MDS_METADATA_PREFIX, "last_metadata", bl);
1847}
1848
1849int MDSMonitor::load_metadata(map<mds_gid_t, Metadata>& m)
1850{
1851 bufferlist bl;
f67539c2 1852 int r = mon.store->get(MDS_METADATA_PREFIX, "last_metadata", bl);
7c673cae 1853 if (r) {
11fdf7f2 1854 dout(5) << "Unable to load 'last_metadata'" << dendl;
7c673cae
FG
1855 return r;
1856 }
1857
11fdf7f2
TL
1858 auto it = bl.cbegin();
1859 ceph::decode(m, it);
7c673cae
FG
1860 return 0;
1861}
1862
1adf2230 1863void MDSMonitor::count_metadata(const std::string &field, map<string,int> *out)
31f18b77 1864{
31f18b77
FG
1865 map<mds_gid_t,Metadata> meta;
1866 load_metadata(meta);
1867 for (auto& p : meta) {
1868 auto q = p.second.find(field);
1869 if (q == p.second.end()) {
c07f9fc5 1870 (*out)["unknown"]++;
31f18b77 1871 } else {
c07f9fc5 1872 (*out)[q->second]++;
31f18b77
FG
1873 }
1874 }
c07f9fc5
FG
1875}
1876
1adf2230 1877void MDSMonitor::count_metadata(const std::string &field, Formatter *f)
c07f9fc5
FG
1878{
1879 map<string,int> by_val;
1880 count_metadata(field, &by_val);
31f18b77
FG
1881 f->open_object_section(field.c_str());
1882 for (auto& p : by_val) {
1883 f->dump_int(p.first.c_str(), p.second);
1884 }
1885 f->close_section();
1886}
1887
f67539c2
TL
1888void MDSMonitor::get_versions(std::map<string, list<string> > &versions)
1889{
1890 map<mds_gid_t,Metadata> meta;
1891 load_metadata(meta);
1892 const auto &fsmap = get_fsmap();
1893 std::map<mds_gid_t, mds_info_t> map = fsmap.get_mds_info();
1894 dout(10) << __func__ << " mds meta=" << meta << dendl;
1895 for (auto& p : meta) {
1896 auto q = p.second.find("ceph_version_short");
1897 if (q == p.second.end()) continue;
1898 versions[q->second].push_back(string("mds.") + map[p.first].name);
1899 }
1900}
1901
1adf2230
AA
1902int MDSMonitor::dump_metadata(const FSMap& fsmap, const std::string &who,
1903 Formatter *f, ostream& err)
7c673cae 1904{
11fdf7f2 1905 ceph_assert(f);
7c673cae 1906
1adf2230 1907 mds_gid_t gid = gid_from_arg(fsmap, who, err);
7c673cae
FG
1908 if (gid == MDS_GID_NONE) {
1909 return -EINVAL;
1910 }
1911
1912 map<mds_gid_t, Metadata> metadata;
1913 if (int r = load_metadata(metadata)) {
1914 err << "Unable to load 'last_metadata'";
1915 return r;
1916 }
1917
1918 if (!metadata.count(gid)) {
1919 return -ENOENT;
1920 }
1921 const Metadata& m = metadata[gid];
1922 for (Metadata::const_iterator p = m.begin(); p != m.end(); ++p) {
1923 f->dump_string(p->first.c_str(), p->second);
1924 }
1925 return 0;
1926}
1927
1928int MDSMonitor::print_nodes(Formatter *f)
1929{
11fdf7f2 1930 ceph_assert(f);
7c673cae 1931
1adf2230
AA
1932 const auto &fsmap = get_fsmap();
1933
7c673cae
FG
1934 map<mds_gid_t, Metadata> metadata;
1935 if (int r = load_metadata(metadata)) {
1936 return r;
1937 }
1938
11fdf7f2 1939 map<string, list<string> > mdses; // hostname => mds
1adf2230
AA
1940 for (const auto &p : metadata) {
1941 const mds_gid_t& gid = p.first;
1942 const Metadata& m = p.second;
7c673cae
FG
1943 Metadata::const_iterator hostname = m.find("hostname");
1944 if (hostname == m.end()) {
1945 // not likely though
1946 continue;
1947 }
1adf2230 1948 if (!fsmap.gid_exists(gid)) {
7c673cae
FG
1949 dout(5) << __func__ << ": GID " << gid << " not existent" << dendl;
1950 continue;
1951 }
1adf2230 1952 const MDSMap::mds_info_t& mds_info = fsmap.get_info_gid(gid);
11fdf7f2 1953 mdses[hostname->second].push_back(mds_info.name);
7c673cae
FG
1954 }
1955
1956 dump_services(f, mdses, "mds");
1957 return 0;
1958}
1959
1960/**
1961 * If a cluster is undersized (with respect to max_mds), then
11fdf7f2
TL
1962 * attempt to find daemons to grow it. If the cluster is oversized
1963 * (with respect to max_mds) then shrink it by stopping its highest rank.
7c673cae 1964 */
11fdf7f2 1965bool MDSMonitor::maybe_resize_cluster(FSMap &fsmap, fs_cluster_id_t fscid)
7c673cae 1966{
11fdf7f2
TL
1967 auto &current_mds_map = get_fsmap().get_filesystem(fscid)->mds_map;
1968 auto&& fs = fsmap.get_filesystem(fscid);
1adf2230 1969 auto &mds_map = fs->mds_map;
7c673cae 1970
1adf2230
AA
1971 int in = mds_map.get_num_in_mds();
1972 int max = mds_map.get_max_mds();
1973
1974 dout(20) << __func__ << " in " << in << " max " << max << dendl;
1975
11fdf7f2
TL
1976 /* Check that both the current epoch mds_map is resizeable as well as the
1977 * current batch of changes in pending. This is important if an MDS is
1978 * becoming active in the next epoch.
1979 */
1980 if (!current_mds_map.is_resizeable() ||
1981 !mds_map.is_resizeable()) {
1982 dout(5) << __func__ << " mds_map is not currently resizeable" << dendl;
1983 return false;
1984 }
1985
1986 if (in < max && !mds_map.test_flag(CEPH_MDSMAP_NOT_JOINABLE)) {
7c673cae 1987 mds_rank_t mds = mds_rank_t(0);
1adf2230 1988 while (mds_map.is_in(mds)) {
7c673cae
FG
1989 mds++;
1990 }
9f95a23c
TL
1991 auto info = fsmap.find_replacement_for({fscid, mds});
1992 if (!info) {
1adf2230 1993 return false;
7c673cae
FG
1994 }
1995
9f95a23c 1996 dout(1) << "assigned standby " << info->addrs
7c673cae 1997 << " as mds." << mds << dendl;
f67539c2 1998 mon.clog->info() << info->human_name() << " assigned to "
1adf2230
AA
1999 "filesystem " << mds_map.fs_name << " as rank "
2000 << mds << " (now has " << mds_map.get_num_in_mds() + 1
d2e6a577 2001 << " ranks)";
9f95a23c 2002 fsmap.promote(info->global_id, *fs, mds);
1adf2230 2003 return true;
11fdf7f2
TL
2004 } else if (in > max) {
2005 mds_rank_t target = in - 1;
2006 const auto &info = mds_map.get_info(target);
2007 if (mds_map.is_active(target)) {
2008 dout(1) << "stopping " << target << dendl;
f67539c2 2009 mon.clog->info() << "stopping " << info.human_name();
11fdf7f2
TL
2010 auto f = [](auto& info) {
2011 info.state = MDSMap::STATE_STOPPING;
2012 };
2013 fsmap.modify_daemon(info.global_id, f);
2014 return true;
2015 } else {
2016 dout(20) << "skipping stop of " << target << dendl;
2017 return false;
2018 }
7c673cae
FG
2019 }
2020
1adf2230 2021 return false;
7c673cae
FG
2022}
2023
2024
2025/**
9f95a23c 2026 * Fail a daemon and replace it with a suitable standby.
7c673cae 2027 */
9f95a23c 2028bool MDSMonitor::drop_mds(FSMap &fsmap, mds_gid_t gid, const mds_info_t* rep_info, bool *osd_propose)
7c673cae 2029{
11fdf7f2 2030 ceph_assert(osd_propose != nullptr);
7c673cae 2031
1adf2230 2032 const auto fscid = fsmap.mds_roles.at(gid);
9f95a23c
TL
2033 const auto& info = fsmap.get_info_gid(gid);
2034 const auto rank = info.rank;
2035 const auto state = info.state;
2036
2037 if (info.is_frozen()) {
2038 return false;
2039 } else if (state == MDSMap::STATE_STANDBY_REPLAY ||
2040 state == MDSMap::STATE_STANDBY) {
2041 dout(1) << " failing and removing standby " << gid << " " << info.addrs
2042 << " mds." << rank
2043 << "." << info.inc << " " << ceph_mds_state_name(state)
2044 << dendl;
2045 *osd_propose |= fail_mds_gid(fsmap, gid);
2046 return true;
2047 } else if (rank >= 0 && rep_info) {
2048 auto fs = fsmap.filesystems.at(fscid);
2049 if (fs->mds_map.test_flag(CEPH_MDSMAP_NOT_JOINABLE)) {
2050 return false;
2051 }
2052 // are we in?
2053 // and is there a non-laggy standby that can take over for us?
2054 dout(1) << " replacing " << gid << " " << info.addrs
2055 << " mds." << rank << "." << info.inc
2056 << " " << ceph_mds_state_name(state)
2057 << " with " << rep_info->global_id << "/" << rep_info->name << " " << rep_info->addrs
2058 << dendl;
2059
f67539c2 2060 mon.clog->warn() << "Replacing " << info.human_name()
9f95a23c
TL
2061 << " as rank " << rank
2062 << " with standby " << rep_info->human_name();
2063
2064 // Remove the old one
2065 *osd_propose |= fail_mds_gid(fsmap, gid);
2066
2067 // Promote the replacement
2068 fsmap.promote(rep_info->global_id, *fs, rank);
2069
2070 return true;
2071 }
2072 return false;
2073}
2074
2075bool MDSMonitor::check_health(FSMap& fsmap, bool* propose_osdmap)
2076{
2077 bool do_propose = false;
2078 const auto now = mono_clock::now();
f67539c2 2079 const bool osdmap_writeable = mon.osdmon()->is_writeable();
9f95a23c
TL
2080 const auto mds_beacon_grace = g_conf().get_val<double>("mds_beacon_grace");
2081 const auto mds_beacon_interval = g_conf().get_val<double>("mds_beacon_interval");
2082
2083 if (mono_clock::is_zero(last_tick)) {
2084 last_tick = now;
2085 }
2086
2087 {
2088 auto since_last = std::chrono::duration<double>(now-last_tick);
2089
2090 if (since_last.count() > (mds_beacon_grace-mds_beacon_interval)) {
2091 // This case handles either local slowness (calls being delayed
2092 // for whatever reason) or cluster election slowness (a long gap
2093 // between calls while an election happened)
2094 dout(1) << __func__ << ": resetting beacon timeouts due to mon delay "
2095 "(slow election?) of " << since_last.count() << " seconds" << dendl;
2096 for (auto& p : last_beacon) {
2097 p.second.stamp = now;
2098 }
2099 }
2100 }
2101
2102 // make sure last_beacon is fully populated
2103 for (auto& p : fsmap.mds_roles) {
2104 auto& gid = p.first;
2105 last_beacon.emplace(std::piecewise_construct,
2106 std::forward_as_tuple(gid),
2107 std::forward_as_tuple(now, 0));
2108 }
7c673cae 2109
31f18b77 2110 // We will only take decisive action (replacing/removing a daemon)
9f95a23c 2111 // if we have some indication that some other daemon(s) are successfully
31f18b77 2112 // getting beacons through recently.
1adf2230 2113 mono_time latest_beacon = mono_clock::zero();
9f95a23c 2114 for (const auto& p : last_beacon) {
1adf2230 2115 latest_beacon = std::max(p.second.stamp, latest_beacon);
31f18b77 2116 }
f67539c2 2117 auto since = std::chrono::duration<double>(now-latest_beacon);
1adf2230 2118 const bool may_replace = since.count() <
11fdf7f2 2119 std::max(g_conf()->mds_beacon_interval, g_conf()->mds_beacon_grace * 0.5);
31f18b77 2120
9f95a23c
TL
2121 // check beacon timestamps
2122 std::vector<mds_gid_t> to_remove;
2123 for (auto it = last_beacon.begin(); it != last_beacon.end(); ) {
2124 auto& [gid, beacon_info] = *it;
f67539c2 2125 auto since_last = std::chrono::duration<double>(now-beacon_info.stamp);
9f95a23c
TL
2126
2127 if (!fsmap.gid_exists(gid)) {
2128 // gid no longer exists, remove from tracked beacons
2129 it = last_beacon.erase(it);
2130 continue;
2131 }
7c673cae 2132
9f95a23c
TL
2133 if (since_last.count() >= g_conf()->mds_beacon_grace) {
2134 auto& info = fsmap.get_info_gid(gid);
2135 dout(1) << "no beacon from mds." << info.rank << "." << info.inc
2136 << " (gid: " << gid << " addr: " << info.addrs
2137 << " state: " << ceph_mds_state_name(info.state) << ")"
2138 << " since " << since_last.count() << dendl;
f67539c2 2139 // If the OSDMap is writeable, we can blocklist things, so we can
9f95a23c
TL
2140 // try failing any laggy MDS daemons. Consider each one for failure.
2141 if (!info.laggy()) {
2142 dout(1) << " marking " << gid << " " << info.addrs
2143 << " mds." << info.rank << "." << info.inc
2144 << " " << ceph_mds_state_name(info.state)
2145 << " laggy" << dendl;
2146 fsmap.modify_daemon(info.global_id, [](auto& info) {
2147 info.laggy_since = ceph_clock_now();
2148 });
2149 do_propose = true;
2150 }
2151 if (osdmap_writeable && may_replace) {
2152 to_remove.push_back(gid); // drop_mds may invalidate iterator
2153 }
2154 }
31f18b77 2155
9f95a23c
TL
2156 ++it;
2157 }
7c673cae 2158
9f95a23c 2159 for (const auto& gid : to_remove) {
f6b5b4d7 2160 auto info = fsmap.get_info_gid(gid);
9f95a23c
TL
2161 const mds_info_t* rep_info = nullptr;
2162 if (info.rank >= 0) {
f67539c2 2163 auto fscid = fsmap.fscid_from_gid(gid);
9f95a23c
TL
2164 rep_info = fsmap.find_replacement_for({fscid, info.rank});
2165 }
2166 bool dropped = drop_mds(fsmap, gid, rep_info, propose_osdmap);
2167 if (dropped) {
f67539c2 2168 mon.clog->info() << "MDS " << info.human_name()
9f95a23c
TL
2169 << " is removed because it is dead or otherwise unavailable.";
2170 do_propose = true;
2171 }
2172 }
7c673cae 2173
9f95a23c
TL
2174 if (osdmap_writeable) {
2175 for (auto& [fscid, fs] : fsmap.filesystems) {
2176 if (!fs->mds_map.test_flag(CEPH_MDSMAP_NOT_JOINABLE) &&
2177 fs->mds_map.is_resizeable()) {
2178 // Check if a rank or standby-replay should be replaced with a stronger
2179 // affinity standby. This looks at ranks and standby-replay:
2180 for (const auto& [gid, info] : fs->mds_map.get_mds_info()) {
2181 const auto join_fscid = info.join_fscid;
2182 if (join_fscid == fscid)
2183 continue;
2184 const auto rank = info.rank;
2185 const auto state = info.state;
2186 const mds_info_t* rep_info = nullptr;
2187 if (state == MDSMap::STATE_STANDBY_REPLAY) {
522d829b 2188 rep_info = fsmap.get_available_standby(*fs);
9f95a23c
TL
2189 } else if (state == MDSMap::STATE_ACTIVE) {
2190 rep_info = fsmap.find_replacement_for({fscid, rank});
2191 } else {
2192 /* N.B. !is_degraded() */
2193 ceph_abort_msg("invalid state in MDSMap");
2194 }
2195 if (!rep_info) {
2196 break;
2197 }
2198 bool better_affinity = false;
2199 if (join_fscid == FS_CLUSTER_ID_NONE) {
2200 better_affinity = (rep_info->join_fscid == fscid);
2201 } else {
2202 better_affinity = (rep_info->join_fscid == fscid) ||
2203 (rep_info->join_fscid == FS_CLUSTER_ID_NONE);
2204 }
2205 if (better_affinity) {
2206 if (state == MDSMap::STATE_STANDBY_REPLAY) {
f67539c2 2207 mon.clog->info() << "Dropping low affinity standby-replay "
9f95a23c
TL
2208 << info.human_name()
2209 << " in favor of higher affinity standby.";
2210 *propose_osdmap |= fail_mds_gid(fsmap, gid);
2211 /* Now let maybe_promote_standby do the promotion. */
2212 } else {
f67539c2 2213 mon.clog->info() << "Dropping low affinity active "
9f95a23c
TL
2214 << info.human_name()
2215 << " in favor of higher affinity standby.";
2216 do_propose |= drop_mds(fsmap, gid, rep_info, propose_osdmap);
2217 }
2218 break; /* don't replace more than one per tick per fs */
2219 }
2220 }
2221 }
2222 }
7c673cae 2223 }
9f95a23c 2224 return do_propose;
7c673cae
FG
2225}
2226
11fdf7f2 2227bool MDSMonitor::maybe_promote_standby(FSMap &fsmap, Filesystem& fs)
7c673cae 2228{
11fdf7f2
TL
2229 if (fs.mds_map.test_flag(CEPH_MDSMAP_NOT_JOINABLE)) {
2230 return false;
2231 }
7c673cae
FG
2232
2233 bool do_propose = false;
2234
2235 // have a standby take over?
2236 set<mds_rank_t> failed;
11fdf7f2
TL
2237 fs.mds_map.get_failed_mds_set(failed);
2238 for (const auto& rank : failed) {
9f95a23c
TL
2239 auto info = fsmap.find_replacement_for({fs.fscid, rank});
2240 if (info) {
2241 dout(1) << " taking over failed mds." << rank << " with " << info->global_id
2242 << "/" << info->name << " " << info->addrs << dendl;
f67539c2 2243 mon.clog->info() << "Standby " << info->human_name()
11fdf7f2
TL
2244 << " assigned to filesystem " << fs.mds_map.fs_name
2245 << " as rank " << rank;
2246
9f95a23c 2247 fsmap.promote(info->global_id, fs, rank);
11fdf7f2 2248 do_propose = true;
7c673cae 2249 }
11fdf7f2
TL
2250 }
2251
f67539c2 2252 if (fs.mds_map.is_resizeable() && fs.mds_map.allows_standby_replay()) {
7c673cae 2253 // There were no failures to replace, so try using any available standbys
a8e16298
TL
2254 // as standby-replay daemons. Don't do this when the cluster is degraded
2255 // as a standby-replay daemon may try to read a journal being migrated.
11fdf7f2 2256 for (;;) {
522d829b 2257 auto info = fsmap.get_available_standby(fs);
9f95a23c
TL
2258 if (!info) break;
2259 dout(20) << "standby available mds." << info->global_id << dendl;
11fdf7f2
TL
2260 bool changed = false;
2261 for (const auto& rank : fs.mds_map.in) {
9f95a23c 2262 dout(20) << "examining " << rank << dendl;
11fdf7f2 2263 if (fs.mds_map.is_followable(rank)) {
9f95a23c 2264 dout(1) << " setting mds." << info->global_id
11fdf7f2 2265 << " to follow mds rank " << rank << dendl;
9f95a23c 2266 fsmap.assign_standby_replay(info->global_id, fs.fscid, rank);
11fdf7f2
TL
2267 do_propose = true;
2268 changed = true;
2269 break;
7c673cae 2270 }
7c673cae 2271 }
11fdf7f2 2272 if (!changed) break;
7c673cae
FG
2273 }
2274 }
2275
2276 return do_propose;
2277}
2278
2279void MDSMonitor::tick()
2280{
1adf2230 2281 if (!is_active() || !is_leader()) return;
28e407b8
AA
2282
2283 auto &pending = get_pending_fsmap_writeable();
7c673cae 2284
28e407b8 2285 bool do_propose = false;
9f95a23c 2286 bool propose_osdmap = false;
7c673cae 2287
522d829b
TL
2288 if (check_fsmap_struct_version) {
2289 /* Allow time for trimming otherwise PaxosService::is_writeable will always
2290 * be false.
2291 */
2292
2293 auto now = clock::now();
2294 auto elapsed = now - last_fsmap_struct_flush;
2295 if (elapsed > std::chrono::seconds(30)) {
2296 FSMap fsmap;
2297 bufferlist bl;
2298 auto v = get_first_committed();
2299 int err = get_version(v, bl);
2300 if (err) {
2301 derr << "could not get version " << v << dendl;
2302 ceph_abort();
2303 }
2304 fsmap.decode(bl);
2305 if (fsmap.is_struct_old()) {
2306 dout(5) << "fsmap struct is too old; proposing to flush out old versions" << dendl;
2307 do_propose = true;
2308 last_fsmap_struct_flush = now;
2309 } else {
2310 dout(20) << "struct is recent" << dendl;
2311 check_fsmap_struct_version = false;
2312 }
2313 }
2314 }
2315
28e407b8 2316 do_propose |= pending.check_health();
7c673cae 2317
9f95a23c
TL
2318 /* Check health and affinity of ranks */
2319 do_propose |= check_health(pending, &propose_osdmap);
7c673cae 2320
9f95a23c
TL
2321 /* Resize the cluster according to max_mds. */
2322 for (auto& p : pending.filesystems) {
2323 do_propose |= maybe_resize_cluster(pending, p.second->fscid);
7c673cae
FG
2324 }
2325
9f95a23c
TL
2326 /* Replace any failed ranks. */
2327 for (auto& p : pending.filesystems) {
2328 do_propose |= maybe_promote_standby(pending, *p.second);
7c673cae
FG
2329 }
2330
c07f9fc5 2331 if (propose_osdmap) {
f67539c2 2332 request_proposal(mon.osdmon());
c07f9fc5 2333 }
7c673cae 2334
7c673cae
FG
2335 if (do_propose) {
2336 propose_pending();
2337 }
9f95a23c
TL
2338
2339 last_tick = mono_clock::now();
7c673cae
FG
2340}
2341
f67539c2 2342MDSMonitor::MDSMonitor(Monitor &mn, Paxos &p, string service_name)
7c673cae
FG
2343 : PaxosService(mn, p, service_name)
2344{
f67539c2 2345 handlers = FileSystemCommandHandler::load(&p);
7c673cae
FG
2346}
2347
2348void MDSMonitor::on_restart()
2349{
2350 // Clear out the leader-specific state.
1adf2230 2351 last_tick = mono_clock::now();
7c673cae
FG
2352 last_beacon.clear();
2353}
2354