]> git.proxmox.com Git - ceph.git/blame - ceph/src/mds/FSMap.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / mds / FSMap.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
15
16#include "FSMap.h"
17
18#include <sstream>
19using std::stringstream;
20
21
22void Filesystem::dump(Formatter *f) const
23{
24 f->open_object_section("mdsmap");
25 mds_map.dump(f);
26 f->close_section();
27 f->dump_int("id", fscid);
28}
29
30void FSMap::dump(Formatter *f) const
31{
32 f->dump_int("epoch", epoch);
33
34 f->open_object_section("compat");
35 compat.dump(f);
36 f->close_section();
37
38 f->open_object_section("feature_flags");
39 f->dump_bool("enable_multiple", enable_multiple);
40 f->dump_bool("ever_enabled_multiple", ever_enabled_multiple);
41 f->close_section();
42
43 f->open_array_section("standbys");
44 for (const auto &i : standby_daemons) {
45 f->open_object_section("info");
46 i.second.dump(f);
47 f->dump_int("epoch", standby_epochs.at(i.first));
48 f->close_section();
49 }
50 f->close_section();
51
52 f->open_array_section("filesystems");
53 for (const auto &fs : filesystems) {
54 f->open_object_section("filesystem");
55 fs.second->dump(f);
56 f->close_section();
57 }
58 f->close_section();
59}
60
61void FSMap::generate_test_instances(list<FSMap*>& ls)
62{
63 FSMap *m = new FSMap();
64
65 std::list<MDSMap*> mds_map_instances;
66 MDSMap::generate_test_instances(mds_map_instances);
67
68 int k = 20;
69 for (auto i : mds_map_instances) {
70 auto fs = std::make_shared<Filesystem>();
71 fs->fscid = k++;
72 fs->mds_map = *i;
73 delete i;
74 m->filesystems[fs->fscid] = fs;
75 }
76 mds_map_instances.clear();
77
78 ls.push_back(m);
79}
80
81void FSMap::print(ostream& out) const
82{
83 out << "e" << epoch << std::endl;
84 out << "enable_multiple, ever_enabled_multiple: " << enable_multiple << ","
85 << ever_enabled_multiple << std::endl;
86 out << "compat: " << compat << std::endl;
87 out << "legacy client fscid: " << legacy_client_fscid << std::endl;
88 out << " " << std::endl;
89
90 if (filesystems.empty()) {
91 out << "No filesystems configured" << std::endl;
92 return;
93 }
94
95 for (const auto &fs : filesystems) {
96 fs.second->print(out);
97 out << " " << std::endl << " " << std::endl; // Space out a bit
98 }
99
100 if (!standby_daemons.empty()) {
101 out << "Standby daemons:" << std::endl << " " << std::endl;
102 }
103
104 for (const auto &p : standby_daemons) {
105 p.second.print_summary(out);
106 out << std::endl;
107 }
108}
109
110
111
112void FSMap::print_summary(Formatter *f, ostream *out) const
113{
114 map<mds_role_t,string> by_rank;
115 map<string,int> by_state;
116
117 if (f) {
118 f->dump_unsigned("epoch", get_epoch());
119 for (auto i : filesystems) {
120 auto fs = i.second;
121 f->dump_unsigned("id", fs->fscid);
122 f->dump_unsigned("up", fs->mds_map.up.size());
123 f->dump_unsigned("in", fs->mds_map.in.size());
124 f->dump_unsigned("max", fs->mds_map.max_mds);
125 }
126 } else {
127 *out << "e" << get_epoch() << ":";
128 if (filesystems.size() == 1) {
129 auto fs = filesystems.begin()->second;
130 *out << " " << fs->mds_map.up.size() << "/" << fs->mds_map.in.size() << "/"
131 << fs->mds_map.max_mds << " up";
132 } else {
133 for (auto i : filesystems) {
134 auto fs = i.second;
135 *out << " " << fs->mds_map.fs_name << "-" << fs->mds_map.up.size() << "/"
136 << fs->mds_map.in.size() << "/" << fs->mds_map.max_mds << " up";
137 }
138 }
139 }
140
141 if (f) {
142 f->open_array_section("by_rank");
143 }
144
145 const auto all_info = get_mds_info();
146 for (const auto &p : all_info) {
147 const auto &info = p.second;
148 string s = ceph_mds_state_name(info.state);
149 if (info.laggy()) {
150 s += "(laggy or crashed)";
151 }
152
153 const fs_cluster_id_t fscid = mds_roles.at(info.global_id);
154
155 if (info.rank != MDS_RANK_NONE &&
156 info.state != MDSMap::STATE_STANDBY_REPLAY) {
157 if (f) {
158 f->open_object_section("mds");
159 f->dump_unsigned("filesystem_id", fscid);
160 f->dump_unsigned("rank", info.rank);
161 f->dump_string("name", info.name);
162 f->dump_string("status", s);
163 f->close_section();
164 } else {
165 by_rank[mds_role_t(fscid, info.rank)] = info.name + "=" + s;
166 }
167 } else {
168 by_state[s]++;
169 }
170 }
171
172 if (f) {
173 f->close_section();
174 } else {
175 if (!by_rank.empty()) {
176 if (filesystems.size() > 1) {
177 // Disambiguate filesystems
178 std::map<std::string, std::string> pretty;
179 for (auto i : by_rank) {
180 const auto &fs_name = filesystems.at(i.first.fscid)->mds_map.fs_name;
181 std::ostringstream o;
182 o << "[" << fs_name << ":" << i.first.rank << "]";
183 pretty[o.str()] = i.second;
184 }
185 *out << " " << pretty;
186 } else {
187 // Omit FSCID in output when only one filesystem exists
188 std::map<mds_rank_t, std::string> shortened;
189 for (auto i : by_rank) {
190 shortened[i.first.rank] = i.second;
191 }
192 *out << " " << shortened;
193 }
194 }
195 }
196
197 for (map<string,int>::reverse_iterator p = by_state.rbegin(); p != by_state.rend(); ++p) {
198 if (f) {
199 f->dump_unsigned(p->first.c_str(), p->second);
200 } else {
201 *out << ", " << p->second << " " << p->first;
202 }
203 }
204
205 size_t failed = 0;
206 size_t damaged = 0;
207 for (auto i : filesystems) {
208 auto fs = i.second;
209 failed += fs->mds_map.failed.size();
210 damaged += fs->mds_map.damaged.size();
211 }
212
213 if (failed > 0) {
214 if (f) {
215 f->dump_unsigned("failed", failed);
216 } else {
217 *out << ", " << failed << " failed";
218 }
219 }
220
221 if (damaged > 0) {
222 if (f) {
223 f->dump_unsigned("damaged", damaged);
224 } else {
225 *out << ", " << damaged << " damaged";
226 }
227 }
228 //if (stopped.size())
229 //out << ", " << stopped.size() << " stopped";
230}
231
232
233void FSMap::create_filesystem(const std::string &name,
234 int64_t metadata_pool, int64_t data_pool,
235 uint64_t features)
236{
237 auto fs = std::make_shared<Filesystem>();
238 fs->mds_map.fs_name = name;
239 fs->mds_map.max_mds = 1;
240 fs->mds_map.data_pools.insert(data_pool);
241 fs->mds_map.metadata_pool = metadata_pool;
242 fs->mds_map.cas_pool = -1;
243 fs->mds_map.max_file_size = g_conf->mds_max_file_size;
244 fs->mds_map.compat = compat;
245 fs->mds_map.created = ceph_clock_now();
246 fs->mds_map.modified = ceph_clock_now();
247 fs->mds_map.session_timeout = g_conf->mds_session_timeout;
248 fs->mds_map.session_autoclose = g_conf->mds_session_autoclose;
249 fs->mds_map.enabled = true;
250 if (features & CEPH_FEATURE_SERVER_JEWEL) {
251 fs->fscid = next_filesystem_id++;
252 // ANONYMOUS is only for upgrades from legacy mdsmaps, we should
253 // have initialized next_filesystem_id such that it's never used here.
254 assert(fs->fscid != FS_CLUSTER_ID_ANONYMOUS);
255 } else {
256 // Use anon fscid because this will get thrown away when encoding
257 // as legacy MDSMap for legacy mons.
258 assert(filesystems.empty());
259 fs->fscid = FS_CLUSTER_ID_ANONYMOUS;
260 }
261 filesystems[fs->fscid] = fs;
262
263 // Created first filesystem? Set it as the one
264 // for legacy clients to use
265 if (filesystems.size() == 1) {
266 legacy_client_fscid = fs->fscid;
267 }
268}
269
270void FSMap::reset_filesystem(fs_cluster_id_t fscid)
271{
272 auto fs = get_filesystem(fscid);
273 auto new_fs = std::make_shared<Filesystem>();
274
275 // Populate rank 0 as existing (so don't go into CREATING)
276 // but failed (so that next available MDS is assigned the rank)
277 new_fs->mds_map.in.insert(mds_rank_t(0));
278 new_fs->mds_map.failed.insert(mds_rank_t(0));
279
280 // Carry forward what makes sense
281 new_fs->fscid = fs->fscid;
282 new_fs->mds_map.inline_data_enabled = fs->mds_map.inline_data_enabled;
283 new_fs->mds_map.max_mds = 1;
284 new_fs->mds_map.data_pools = fs->mds_map.data_pools;
285 new_fs->mds_map.metadata_pool = fs->mds_map.metadata_pool;
286 new_fs->mds_map.cas_pool = fs->mds_map.cas_pool;
287 new_fs->mds_map.fs_name = fs->mds_map.fs_name;
288 new_fs->mds_map.max_file_size = g_conf->mds_max_file_size;
289 new_fs->mds_map.compat = compat;
290 new_fs->mds_map.created = ceph_clock_now();
291 new_fs->mds_map.modified = ceph_clock_now();
292 new_fs->mds_map.session_timeout = g_conf->mds_session_timeout;
293 new_fs->mds_map.session_autoclose = g_conf->mds_session_autoclose;
294 new_fs->mds_map.standby_count_wanted = fs->mds_map.standby_count_wanted;
295 new_fs->mds_map.enabled = true;
296
297 // Persist the new FSMap
298 filesystems[new_fs->fscid] = new_fs;
299}
300
301void FSMap::get_health(list<pair<health_status_t,string> >& summary,
302 list<pair<health_status_t,string> > *detail) const
303{
304 mds_rank_t standby_count_wanted = 0;
305 for (const auto &i : filesystems) {
306 const auto &fs = i.second;
307
308 // TODO: move get_health up into here so that we can qualify
309 // all the messages with what filesystem they're talking about
310 fs->mds_map.get_health(summary, detail);
311
312 standby_count_wanted = std::max(standby_count_wanted, fs->mds_map.get_standby_count_wanted((mds_rank_t)standby_daemons.size()));
313 }
314
315 if (standby_count_wanted) {
316 std::ostringstream oss;
317 oss << "insufficient standby daemons available: have " << standby_daemons.size() << "; want " << standby_count_wanted << " more";
318 summary.push_back(make_pair(HEALTH_WARN, oss.str()));
319 }
320}
321
322bool FSMap::check_health(void)
323{
324 bool changed = false;
325 for (auto &i : filesystems) {
326 changed |= i.second->mds_map.check_health((mds_rank_t)standby_daemons.size());
327 }
328 return changed;
329}
330
331void FSMap::encode(bufferlist& bl, uint64_t features) const
332{
333 if (features & CEPH_FEATURE_SERVER_JEWEL) {
334 ENCODE_START(7, 6, bl);
335 ::encode(epoch, bl);
336 ::encode(next_filesystem_id, bl);
337 ::encode(legacy_client_fscid, bl);
338 ::encode(compat, bl);
339 ::encode(enable_multiple, bl);
340 std::vector<Filesystem> fs_list;
341 for (auto i : filesystems) {
342 fs_list.push_back(*(i.second));
343 }
344 ::encode(fs_list, bl, features);
345 ::encode(mds_roles, bl);
346 ::encode(standby_daemons, bl, features);
347 ::encode(standby_epochs, bl);
348 ::encode(ever_enabled_multiple, bl);
349 ENCODE_FINISH(bl);
350 } else {
351 if (filesystems.empty()) {
352 MDSMap disabled_map;
353 disabled_map.epoch = epoch;
354 disabled_map.encode(bl, features);
355 } else {
356 // MDSMonitor should never have created multiple filesystems
357 // until the quorum features indicated Jewel
358 assert(filesystems.size() == 1);
359 auto fs = filesystems.begin()->second;
360
361 // Take the MDSMap for the enabled filesystem, and populated its
362 // mds_info with the standbys to get a pre-jewel-style mon MDSMap.
363 MDSMap full_mdsmap = fs->mds_map;
364 full_mdsmap.epoch = epoch;
365 for (const auto &p : standby_daemons) {
366 full_mdsmap.mds_info[p.first] = p.second;
367 }
368
369 // Old MDSMaps don't set rank on standby replay daemons
370 for (auto &i : full_mdsmap.mds_info) {
371 auto &info = i.second;
372 if (info.state == MDSMap::STATE_STANDBY_REPLAY) {
373 info.rank = MDS_RANK_NONE;
374 }
375 }
376
377 full_mdsmap.encode(bl, features);
378 }
379 }
380}
381
382void FSMap::decode(bufferlist::iterator& p)
383{
384 // Because the mon used to store an MDSMap where we now
385 // store an FSMap, FSMap knows how to decode the legacy
386 // MDSMap format (it never needs to encode it though).
387 MDSMap legacy_mds_map;
388
389 // The highest MDSMap encoding version before we changed the
390 // MDSMonitor to store an FSMap instead of an MDSMap was
391 // 5, so anything older than 6 is decoded as an MDSMap,
392 // and anything newer is decoded as an FSMap.
393 DECODE_START_LEGACY_COMPAT_LEN_16(7, 4, 4, p);
394 if (struct_v < 6) {
395 // Decoding an MDSMap (upgrade)
396 ::decode(epoch, p);
397 ::decode(legacy_mds_map.flags, p);
398 ::decode(legacy_mds_map.last_failure, p);
399 ::decode(legacy_mds_map.root, p);
400 ::decode(legacy_mds_map.session_timeout, p);
401 ::decode(legacy_mds_map.session_autoclose, p);
402 ::decode(legacy_mds_map.max_file_size, p);
403 ::decode(legacy_mds_map.max_mds, p);
404 ::decode(legacy_mds_map.mds_info, p);
405 if (struct_v < 3) {
406 __u32 n;
407 ::decode(n, p);
408 while (n--) {
409 __u32 m;
410 ::decode(m, p);
411 legacy_mds_map.data_pools.insert(m);
412 }
413 __s32 s;
414 ::decode(s, p);
415 legacy_mds_map.cas_pool = s;
416 } else {
417 ::decode(legacy_mds_map.data_pools, p);
418 ::decode(legacy_mds_map.cas_pool, p);
419 }
420
421 // kclient ignores everything from here
422 __u16 ev = 1;
423 if (struct_v >= 2)
424 ::decode(ev, p);
425 if (ev >= 3)
426 ::decode(legacy_mds_map.compat, p);
427 else
428 legacy_mds_map.compat = get_mdsmap_compat_set_base();
429 if (ev < 5) {
430 __u32 n;
431 ::decode(n, p);
432 legacy_mds_map.metadata_pool = n;
433 } else {
434 ::decode(legacy_mds_map.metadata_pool, p);
435 }
436 ::decode(legacy_mds_map.created, p);
437 ::decode(legacy_mds_map.modified, p);
438 ::decode(legacy_mds_map.tableserver, p);
439 ::decode(legacy_mds_map.in, p);
440 std::map<mds_rank_t,int32_t> inc; // Legacy field, parse and drop
441 ::decode(inc, p);
442 ::decode(legacy_mds_map.up, p);
443 ::decode(legacy_mds_map.failed, p);
444 ::decode(legacy_mds_map.stopped, p);
445 if (ev >= 4)
446 ::decode(legacy_mds_map.last_failure_osd_epoch, p);
447 if (ev >= 6) {
448 if (ev < 10) {
449 // previously this was a bool about snaps, not a flag map
450 bool flag;
451 ::decode(flag, p);
452 legacy_mds_map.ever_allowed_features = flag ?
453 CEPH_MDSMAP_ALLOW_SNAPS : 0;
454 ::decode(flag, p);
455 legacy_mds_map.explicitly_allowed_features = flag ?
456 CEPH_MDSMAP_ALLOW_SNAPS : 0;
457 if (legacy_mds_map.max_mds > 1) {
458 legacy_mds_map.set_multimds_allowed();
459 }
460 } else {
461 ::decode(legacy_mds_map.ever_allowed_features, p);
462 ::decode(legacy_mds_map.explicitly_allowed_features, p);
463 }
464 } else {
465 legacy_mds_map.ever_allowed_features = CEPH_MDSMAP_ALLOW_CLASSICS;
466 legacy_mds_map.explicitly_allowed_features = 0;
467 if (legacy_mds_map.max_mds > 1) {
468 legacy_mds_map.set_multimds_allowed();
469 }
470 }
471 if (ev >= 7)
472 ::decode(legacy_mds_map.inline_data_enabled, p);
473
474 if (ev >= 8) {
475 assert(struct_v >= 5);
476 ::decode(legacy_mds_map.enabled, p);
477 ::decode(legacy_mds_map.fs_name, p);
478 } else {
479 legacy_mds_map.fs_name = "default";
480 if (epoch > 1) {
481 // If an MDS has ever been started, epoch will be greater than 1,
482 // assume filesystem is enabled.
483 legacy_mds_map.enabled = true;
484 } else {
485 // Upgrading from a cluster that never used an MDS, switch off
486 // filesystem until it's explicitly enabled.
487 legacy_mds_map.enabled = false;
488 }
489 }
490
491 if (ev >= 9) {
492 ::decode(legacy_mds_map.damaged, p);
493 }
494
495 // We're upgrading, populate filesystems from the legacy fields
496 filesystems.clear();
497 standby_daemons.clear();
498 standby_epochs.clear();
499 mds_roles.clear();
500 compat = legacy_mds_map.compat;
501 enable_multiple = false;
502
503 // Synthesise a Filesystem from legacy_mds_map, if enabled
504 if (legacy_mds_map.enabled) {
505 // Construct a Filesystem from the legacy MDSMap
506 auto migrate_fs = std::make_shared<Filesystem>();
507 migrate_fs->fscid = FS_CLUSTER_ID_ANONYMOUS;
508 migrate_fs->mds_map = legacy_mds_map;
509 migrate_fs->mds_map.epoch = epoch;
510 filesystems[migrate_fs->fscid] = migrate_fs;
511
512 // List of GIDs that had invalid states
513 std::set<mds_gid_t> drop_gids;
514
515 // Construct mds_roles, standby_daemons, and remove
516 // standbys from the MDSMap in the Filesystem.
517 for (auto &p : migrate_fs->mds_map.mds_info) {
518 if (p.second.state == MDSMap::STATE_STANDBY_REPLAY) {
519 // In legacy MDSMap, standby replay daemons don't have
520 // rank set, but since FSMap they do.
521 p.second.rank = p.second.standby_for_rank;
522 }
523 if (p.second.rank == MDS_RANK_NONE) {
524 if (p.second.state != MDSMap::STATE_STANDBY) {
525 // Old MDSMaps can have down:dne here, which
526 // is invalid in an FSMap (#17837)
527 drop_gids.insert(p.first);
528 } else {
529 insert(p.second); // into standby_daemons
530 }
531 } else {
532 mds_roles[p.first] = migrate_fs->fscid;
533 }
534 }
535 for (const auto &p : standby_daemons) {
536 // Erase from this Filesystem's MDSMap, because it has
537 // been copied into FSMap::Standby_daemons above
538 migrate_fs->mds_map.mds_info.erase(p.first);
539 }
540 for (const auto &gid : drop_gids) {
541 // Throw away all info for this MDS because it was identified
542 // as having invalid state above.
543 migrate_fs->mds_map.mds_info.erase(gid);
544 }
545
546 legacy_client_fscid = migrate_fs->fscid;
547 } else {
548 legacy_client_fscid = FS_CLUSTER_ID_NONE;
549 }
550 } else {
551 ::decode(epoch, p);
552 ::decode(next_filesystem_id, p);
553 ::decode(legacy_client_fscid, p);
554 ::decode(compat, p);
555 ::decode(enable_multiple, p);
556 std::vector<Filesystem> fs_list;
557 ::decode(fs_list, p);
558 filesystems.clear();
559 for (std::vector<Filesystem>::const_iterator fs = fs_list.begin(); fs != fs_list.end(); ++fs) {
560 filesystems[fs->fscid] = std::make_shared<Filesystem>(*fs);
561 }
562
563 ::decode(mds_roles, p);
564 ::decode(standby_daemons, p);
565 ::decode(standby_epochs, p);
566 if (struct_v >= 7) {
567 ::decode(ever_enabled_multiple, p);
568 }
569 }
570
571 DECODE_FINISH(p);
572}
573
574
575void Filesystem::encode(bufferlist& bl, uint64_t features) const
576{
577 ENCODE_START(1, 1, bl);
578 ::encode(fscid, bl);
579 bufferlist mdsmap_bl;
580 mds_map.encode(mdsmap_bl, features);
581 ::encode(mdsmap_bl, bl);
582 ENCODE_FINISH(bl);
583}
584
585void Filesystem::decode(bufferlist::iterator& p)
586{
587 DECODE_START(1, p);
588 ::decode(fscid, p);
589 bufferlist mdsmap_bl;
590 ::decode(mdsmap_bl, p);
591 bufferlist::iterator mdsmap_bl_iter = mdsmap_bl.begin();
592 mds_map.decode(mdsmap_bl_iter);
593 DECODE_FINISH(p);
594}
595
596int FSMap::parse_filesystem(
597 std::string const &ns_str,
598 std::shared_ptr<const Filesystem> *result
599 ) const
600{
601 std::string ns_err;
602 fs_cluster_id_t fscid = strict_strtol(ns_str.c_str(), 10, &ns_err);
603 if (!ns_err.empty() || filesystems.count(fscid) == 0) {
604 for (auto &fs : filesystems) {
605 if (fs.second->mds_map.fs_name == ns_str) {
606 *result = std::const_pointer_cast<const Filesystem>(fs.second);
607 return 0;
608 }
609 }
610 return -ENOENT;
611 } else {
612 *result = get_filesystem(fscid);
613 return 0;
614 }
615}
616
617void Filesystem::print(std::ostream &out) const
618{
619 out << "Filesystem '" << mds_map.fs_name
620 << "' (" << fscid << ")" << std::endl;
621 mds_map.print(out);
622}
623
624mds_gid_t FSMap::find_standby_for(mds_role_t role, const std::string& name) const
625{
626 mds_gid_t result = MDS_GID_NONE;
627
628 // First see if we have a STANDBY_REPLAY
629 auto fs = get_filesystem(role.fscid);
630 for (const auto &i : fs->mds_map.mds_info) {
631 const auto &info = i.second;
632 if (info.rank == role.rank && info.state == MDSMap::STATE_STANDBY_REPLAY) {
633 return info.global_id;
634 }
635 }
636
637 // See if there are any STANDBY daemons available
638 for (const auto &i : standby_daemons) {
639 const auto &gid = i.first;
640 const auto &info = i.second;
641 assert(info.state == MDSMap::STATE_STANDBY);
642 assert(info.rank == MDS_RANK_NONE);
643
644 if (info.laggy()) {
645 continue;
646 }
647
648 // The mds_info_t may or may not tell us exactly which filesystem
649 // the standby_for_rank refers to: lookup via legacy_client_fscid
650 mds_role_t target_role = {
651 info.standby_for_fscid == FS_CLUSTER_ID_NONE ?
652 legacy_client_fscid : info.standby_for_fscid,
653 info.standby_for_rank};
654
655 if ((target_role.rank == role.rank && target_role.fscid == role.fscid)
656 || (name.length() && info.standby_for_name == name)) {
657 // It's a named standby for *me*, use it.
658 return gid;
659 } else if (
660 info.standby_for_rank < 0 && info.standby_for_name.length() == 0 &&
661 (info.standby_for_fscid == FS_CLUSTER_ID_NONE ||
662 info.standby_for_fscid == role.fscid)) {
663 // It's not a named standby for anyone, use it if we don't find
664 // a named standby for me later, unless it targets another FSCID.
665 result = gid;
666 }
667 }
668
669 return result;
670}
671
672mds_gid_t FSMap::find_unused(fs_cluster_id_t fscid,
673 bool force_standby_active) const {
674 for (const auto &i : standby_daemons) {
675 const auto &gid = i.first;
676 const auto &info = i.second;
677 assert(info.state == MDSMap::STATE_STANDBY);
678
679 if (info.laggy() || info.rank >= 0)
680 continue;
681
682 if (info.standby_for_fscid != FS_CLUSTER_ID_NONE &&
683 info.standby_for_fscid != fscid)
684 continue;
685
686 // To be considered 'unused' a daemon must either not
687 // be selected for standby-replay or the force_standby_active
688 // setting must be enabled to use replay daemons anyway.
689 if (!info.standby_replay || force_standby_active) {
690 return gid;
691 }
692 }
693 return MDS_GID_NONE;
694}
695
696mds_gid_t FSMap::find_replacement_for(mds_role_t role, const std::string& name,
697 bool force_standby_active) const {
698 const mds_gid_t standby = find_standby_for(role, name);
699 if (standby)
700 return standby;
701 else
702 return find_unused(role.fscid, force_standby_active);
703}
704
705void FSMap::sanity() const
706{
707 if (legacy_client_fscid != FS_CLUSTER_ID_NONE) {
708 assert(filesystems.count(legacy_client_fscid) == 1);
709 }
710
711 for (const auto &i : filesystems) {
712 auto fs = i.second;
713 assert(fs->mds_map.compat.compare(compat) == 0);
714 assert(fs->fscid == i.first);
715 for (const auto &j : fs->mds_map.mds_info) {
716 assert(j.second.rank != MDS_RANK_NONE);
717 assert(mds_roles.count(j.first) == 1);
718 assert(standby_daemons.count(j.first) == 0);
719 assert(standby_epochs.count(j.first) == 0);
720 assert(mds_roles.at(j.first) == i.first);
721 if (j.second.state != MDSMap::STATE_STANDBY_REPLAY) {
722 assert(fs->mds_map.up.at(j.second.rank) == j.first);
723 assert(fs->mds_map.failed.count(j.second.rank) == 0);
724 assert(fs->mds_map.damaged.count(j.second.rank) == 0);
725 }
726 }
727
728 for (const auto &j : fs->mds_map.up) {
729 mds_rank_t rank = j.first;
730 assert(fs->mds_map.in.count(rank) == 1);
731 mds_gid_t gid = j.second;
732 assert(fs->mds_map.mds_info.count(gid) == 1);
733 }
734 }
735
736 for (const auto &i : standby_daemons) {
737 assert(i.second.state == MDSMap::STATE_STANDBY);
738 assert(i.second.rank == MDS_RANK_NONE);
739 assert(i.second.global_id == i.first);
740 assert(standby_epochs.count(i.first) == 1);
741 assert(mds_roles.count(i.first) == 1);
742 assert(mds_roles.at(i.first) == FS_CLUSTER_ID_NONE);
743 }
744
745 for (const auto &i : standby_epochs) {
746 assert(standby_daemons.count(i.first) == 1);
747 }
748
749 for (const auto &i : mds_roles) {
750 if (i.second == FS_CLUSTER_ID_NONE) {
751 assert(standby_daemons.count(i.first) == 1);
752 } else {
753 assert(filesystems.count(i.second) == 1);
754 assert(filesystems.at(i.second)->mds_map.mds_info.count(i.first) == 1);
755 }
756 }
757}
758
759void FSMap::promote(
760 mds_gid_t standby_gid,
761 const std::shared_ptr<Filesystem> &filesystem,
762 mds_rank_t assigned_rank)
763{
764 assert(gid_exists(standby_gid));
765 bool is_standby_replay = mds_roles.at(standby_gid) != FS_CLUSTER_ID_NONE;
766 if (!is_standby_replay) {
767 assert(standby_daemons.count(standby_gid));
768 assert(standby_daemons.at(standby_gid).state == MDSMap::STATE_STANDBY);
769 }
770
771 MDSMap &mds_map = filesystem->mds_map;
772
773 // Insert daemon state to Filesystem
774 if (!is_standby_replay) {
775 mds_map.mds_info[standby_gid] = standby_daemons.at(standby_gid);
776 } else {
777 assert(mds_map.mds_info.count(standby_gid));
778 assert(mds_map.mds_info.at(standby_gid).state == MDSMap::STATE_STANDBY_REPLAY);
779 assert(mds_map.mds_info.at(standby_gid).rank == assigned_rank);
780 }
781 MDSMap::mds_info_t &info = mds_map.mds_info[standby_gid];
782
783 if (mds_map.stopped.erase(assigned_rank)) {
784 // The cluster is being expanded with a stopped rank
785 info.state = MDSMap::STATE_STARTING;
786 } else if (!mds_map.is_in(assigned_rank)) {
787 // The cluster is being expanded with a new rank
788 info.state = MDSMap::STATE_CREATING;
789 } else {
790 // An existing rank is being assigned to a replacement
791 info.state = MDSMap::STATE_REPLAY;
792 mds_map.failed.erase(assigned_rank);
793 }
794 info.rank = assigned_rank;
795 info.inc = epoch;
796 mds_roles[standby_gid] = filesystem->fscid;
797
798 // Update the rank state in Filesystem
799 mds_map.in.insert(assigned_rank);
800 mds_map.up[assigned_rank] = standby_gid;
801
802 // Remove from the list of standbys
803 if (!is_standby_replay) {
804 standby_daemons.erase(standby_gid);
805 standby_epochs.erase(standby_gid);
806 }
807
808 // Indicate that Filesystem has been modified
809 mds_map.epoch = epoch;
810}
811
812void FSMap::assign_standby_replay(
813 const mds_gid_t standby_gid,
814 const fs_cluster_id_t leader_ns,
815 const mds_rank_t leader_rank)
816{
817 assert(mds_roles.at(standby_gid) == FS_CLUSTER_ID_NONE);
818 assert(gid_exists(standby_gid));
819 assert(!gid_has_rank(standby_gid));
820 assert(standby_daemons.count(standby_gid));
821
822 // Insert to the filesystem
823 auto fs = filesystems.at(leader_ns);
824 fs->mds_map.mds_info[standby_gid] = standby_daemons.at(standby_gid);
825 fs->mds_map.mds_info[standby_gid].rank = leader_rank;
826 fs->mds_map.mds_info[standby_gid].state = MDSMap::STATE_STANDBY_REPLAY;
827 mds_roles[standby_gid] = leader_ns;
828
829 // Remove from the list of standbys
830 standby_daemons.erase(standby_gid);
831 standby_epochs.erase(standby_gid);
832
833 // Indicate that Filesystem has been modified
834 fs->mds_map.epoch = epoch;
835}
836
837void FSMap::erase(mds_gid_t who, epoch_t blacklist_epoch)
838{
839 if (mds_roles.at(who) == FS_CLUSTER_ID_NONE) {
840 standby_daemons.erase(who);
841 standby_epochs.erase(who);
842 } else {
843 auto &fs = filesystems.at(mds_roles.at(who));
844 const auto &info = fs->mds_map.mds_info.at(who);
845 if (info.state != MDSMap::STATE_STANDBY_REPLAY) {
846 if (info.state == MDSMap::STATE_CREATING) {
847 // If this gid didn't make it past CREATING, then forget
848 // the rank ever existed so that next time it's handed out
849 // to a gid it'll go back into CREATING.
850 fs->mds_map.in.erase(info.rank);
851 } else {
852 // Put this rank into the failed list so that the next available
853 // STANDBY will pick it up.
854 fs->mds_map.failed.insert(info.rank);
855 }
856 assert(fs->mds_map.up.at(info.rank) == info.global_id);
857 fs->mds_map.up.erase(info.rank);
858 }
859 fs->mds_map.mds_info.erase(who);
860 fs->mds_map.last_failure_osd_epoch = blacklist_epoch;
861 fs->mds_map.epoch = epoch;
862 }
863
864 mds_roles.erase(who);
865}
866
867void FSMap::damaged(mds_gid_t who, epoch_t blacklist_epoch)
868{
869 assert(mds_roles.at(who) != FS_CLUSTER_ID_NONE);
870 auto fs = filesystems.at(mds_roles.at(who));
871 mds_rank_t rank = fs->mds_map.mds_info[who].rank;
872
873 erase(who, blacklist_epoch);
874 fs->mds_map.failed.erase(rank);
875 fs->mds_map.damaged.insert(rank);
876
877 assert(fs->mds_map.epoch == epoch);
878}
879
880/**
881 * Update to indicate that the rank `rank` is to be removed
882 * from the damaged list of the filesystem `fscid`
883 */
884bool FSMap::undamaged(const fs_cluster_id_t fscid, const mds_rank_t rank)
885{
886 auto fs = filesystems.at(fscid);
887
888 if (fs->mds_map.damaged.erase(rank)) {
889 fs->mds_map.failed.insert(rank);
890 fs->mds_map.epoch = epoch;
891 return true;
892 } else {
893 return false;
894 }
895}
896
897void FSMap::insert(const MDSMap::mds_info_t &new_info)
898{
899 assert(new_info.state == MDSMap::STATE_STANDBY);
900 assert(new_info.rank == MDS_RANK_NONE);
901 mds_roles[new_info.global_id] = FS_CLUSTER_ID_NONE;
902 standby_daemons[new_info.global_id] = new_info;
903 standby_epochs[new_info.global_id] = epoch;
904}
905
906std::list<mds_gid_t> FSMap::stop(mds_gid_t who)
907{
908 assert(mds_roles.at(who) != FS_CLUSTER_ID_NONE);
909 auto fs = filesystems.at(mds_roles.at(who));
910 const auto &info = fs->mds_map.mds_info.at(who);
911 fs->mds_map.up.erase(info.rank);
912 fs->mds_map.in.erase(info.rank);
913 fs->mds_map.stopped.insert(info.rank);
914
915 // Also drop any standby replays that were following this rank
916 std::list<mds_gid_t> standbys;
917 for (const auto &i : fs->mds_map.mds_info) {
918 const auto &other_gid = i.first;
919 const auto &other_info = i.second;
920 if (other_info.rank == info.rank
921 && other_info.state == MDSMap::STATE_STANDBY_REPLAY) {
922 standbys.push_back(other_gid);
923 erase(other_gid, 0);
924 }
925 }
926
927 fs->mds_map.mds_info.erase(who);
928 mds_roles.erase(who);
929
930 fs->mds_map.epoch = epoch;
931
932 return standbys;
933}
934
935
936/**
937 * Given one of the following forms:
938 * <fs name>:<rank>
939 * <fs id>:<rank>
940 * <rank>
941 *
942 * Parse into a mds_role_t. The rank-only form is only valid
943 * if legacy_client_ns is set.
944 */
945int FSMap::parse_role(
946 const std::string &role_str,
947 mds_role_t *role,
948 std::ostream &ss) const
949{
950 size_t colon_pos = role_str.find(":");
951 size_t rank_pos;
952 std::shared_ptr<const Filesystem> fs;
953 if (colon_pos == std::string::npos) {
954 if (legacy_client_fscid == FS_CLUSTER_ID_NONE) {
955 ss << "No filesystem selected";
956 return -ENOENT;
957 }
958 fs = get_filesystem(legacy_client_fscid);
959 rank_pos = 0;
960 } else {
961 if (parse_filesystem(role_str.substr(0, colon_pos), &fs) < 0) {
962 ss << "Invalid filesystem";
963 return -ENOENT;
964 }
965 rank_pos = colon_pos+1;
966 }
967
968 mds_rank_t rank;
969 std::string err;
970 std::string rank_str = role_str.substr(rank_pos);
971 long rank_i = strict_strtol(rank_str.c_str(), 10, &err);
972 if (rank_i < 0 || !err.empty()) {
973 ss << "Invalid rank '" << rank_str << "'";
974 return -EINVAL;
975 } else {
976 rank = rank_i;
977 }
978
979 if (fs->mds_map.in.count(rank) == 0) {
980 ss << "Rank '" << rank << "' not found";
981 return -ENOENT;
982 }
983
984 *role = {fs->fscid, rank};
985
986 return 0;
987}