]> git.proxmox.com Git - ceph.git/blob - ceph/src/mon/MgrMap.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / mon / MgrMap.h
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) 2016 John Spray <john.spray@redhat.com>
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 #ifndef MGR_MAP_H_
15 #define MGR_MAP_H_
16
17 #include <sstream>
18 #include <set>
19
20 #include "msg/msg_types.h"
21 #include "include/encoding.h"
22 #include "include/utime.h"
23 #include "common/Formatter.h"
24 #include "common/ceph_releases.h"
25 #include "common/version.h"
26 #include "common/options.h"
27 #include "common/Clock.h"
28
29
30 class MgrMap
31 {
32 public:
33 struct ModuleOption {
34 std::string name;
35 uint8_t type = Option::TYPE_STR; // Option::type_t TYPE_*
36 uint8_t level = Option::LEVEL_ADVANCED; // Option::level_t LEVEL_*
37 uint32_t flags = 0; // Option::flag_t FLAG_*
38 std::string default_value;
39 std::string min, max;
40 std::set<std::string> enum_allowed;
41 std::string desc, long_desc;
42 std::set<std::string> tags;
43 std::set<std::string> see_also;
44
45 void encode(ceph::buffer::list& bl) const {
46 ENCODE_START(1, 1, bl);
47 encode(name, bl);
48 encode(type, bl);
49 encode(level, bl);
50 encode(flags, bl);
51 encode(default_value, bl);
52 encode(min, bl);
53 encode(max, bl);
54 encode(enum_allowed, bl);
55 encode(desc, bl);
56 encode(long_desc, bl);
57 encode(tags, bl);
58 encode(see_also, bl);
59 ENCODE_FINISH(bl);
60 }
61 void decode(ceph::buffer::list::const_iterator& p) {
62 DECODE_START(1, p);
63 decode(name, p);
64 decode(type, p);
65 decode(level, p);
66 decode(flags, p);
67 decode(default_value, p);
68 decode(min, p);
69 decode(max, p);
70 decode(enum_allowed, p);
71 decode(desc, p);
72 decode(long_desc, p);
73 decode(tags, p);
74 decode(see_also, p);
75 DECODE_FINISH(p);
76 }
77 void dump(ceph::Formatter *f) const {
78 f->dump_string("name", name);
79 f->dump_string("type", Option::type_to_str(
80 static_cast<Option::type_t>(type)));
81 f->dump_string("level", Option::level_to_str(
82 static_cast<Option::level_t>(level)));
83 f->dump_unsigned("flags", flags);
84 f->dump_string("default_value", default_value);
85 f->dump_string("min", min);
86 f->dump_string("max", max);
87 f->open_array_section("enum_allowed");
88 for (auto& i : enum_allowed) {
89 f->dump_string("value", i);
90 }
91 f->close_section();
92 f->dump_string("desc", desc);
93 f->dump_string("long_desc", long_desc);
94 f->open_array_section("tags");
95 for (auto& i : tags) {
96 f->dump_string("tag", i);
97 }
98 f->close_section();
99 f->open_array_section("see_also");
100 for (auto& i : see_also) {
101 f->dump_string("option", i);
102 }
103 f->close_section();
104 }
105 };
106
107 class ModuleInfo
108 {
109 public:
110 std::string name;
111 bool can_run = true;
112 std::string error_string;
113 std::map<std::string,ModuleOption> module_options;
114
115 // We do not include the module's `failed` field in the beacon,
116 // because it is exposed via health checks.
117 void encode(ceph::buffer::list &bl) const {
118 ENCODE_START(2, 1, bl);
119 encode(name, bl);
120 encode(can_run, bl);
121 encode(error_string, bl);
122 encode(module_options, bl);
123 ENCODE_FINISH(bl);
124 }
125
126 void decode(ceph::buffer::list::const_iterator &bl) {
127 DECODE_START(1, bl);
128 decode(name, bl);
129 decode(can_run, bl);
130 decode(error_string, bl);
131 if (struct_v >= 2) {
132 decode(module_options, bl);
133 }
134 DECODE_FINISH(bl);
135 }
136
137 bool operator==(const ModuleInfo &rhs) const
138 {
139 return (name == rhs.name) && (can_run == rhs.can_run);
140 }
141
142 void dump(ceph::Formatter *f) const {
143 f->open_object_section("module");
144 f->dump_string("name", name);
145 f->dump_bool("can_run", can_run);
146 f->dump_string("error_string", error_string);
147 f->open_object_section("module_options");
148 for (auto& i : module_options) {
149 f->dump_object(i.first.c_str(), i.second);
150 }
151 f->close_section();
152 f->close_section();
153 }
154 };
155
156 class StandbyInfo
157 {
158 public:
159 uint64_t gid = 0;
160 std::string name;
161 std::vector<ModuleInfo> available_modules;
162 uint64_t mgr_features = 0;
163
164 StandbyInfo(uint64_t gid_, const std::string &name_,
165 const std::vector<ModuleInfo>& am,
166 uint64_t feat)
167 : gid(gid_), name(name_), available_modules(am),
168 mgr_features(feat)
169 {}
170
171 StandbyInfo() {}
172
173 void encode(ceph::buffer::list& bl) const
174 {
175 ENCODE_START(4, 1, bl);
176 encode(gid, bl);
177 encode(name, bl);
178 std::set<std::string> old_available_modules;
179 for (const auto &i : available_modules) {
180 old_available_modules.insert(i.name);
181 }
182 encode(old_available_modules, bl); // version 2
183 encode(available_modules, bl); // version 3
184 encode(mgr_features, bl); // v4
185 ENCODE_FINISH(bl);
186 }
187
188 void decode(ceph::buffer::list::const_iterator& p)
189 {
190 DECODE_START(4, p);
191 decode(gid, p);
192 decode(name, p);
193 if (struct_v >= 2) {
194 std::set<std::string> old_available_modules;
195 decode(old_available_modules, p);
196 if (struct_v < 3) {
197 for (const auto &name : old_available_modules) {
198 MgrMap::ModuleInfo info;
199 info.name = name;
200 available_modules.push_back(std::move(info));
201 }
202 }
203 }
204 if (struct_v >= 3) {
205 decode(available_modules, p);
206 }
207 if (struct_v >= 4) {
208 decode(mgr_features, p);
209 }
210 DECODE_FINISH(p);
211 }
212
213 bool have_module(const std::string &module_name) const
214 {
215 auto it = std::find_if(available_modules.begin(),
216 available_modules.end(),
217 [module_name](const ModuleInfo &m) -> bool {
218 return m.name == module_name;
219 });
220
221 return it != available_modules.end();
222 }
223 };
224
225 epoch_t epoch = 0;
226 epoch_t last_failure_osd_epoch = 0;
227
228 /// global_id of the ceph-mgr instance selected as a leader
229 uint64_t active_gid = 0;
230 /// server address reported by the leader once it is active
231 entity_addrvec_t active_addrs;
232 /// whether the nominated leader is active (i.e. has initialized its server)
233 bool available = false;
234 /// the name (foo in mgr.<foo>) of the active daemon
235 std::string active_name;
236 /// when the active mgr became active, or we lost the active mgr
237 utime_t active_change;
238 /// features
239 uint64_t active_mgr_features = 0;
240
241 std::vector<entity_addrvec_t> clients; // for blacklist
242
243 std::map<uint64_t, StandbyInfo> standbys;
244
245 // Modules which are enabled
246 std::set<std::string> modules;
247
248 // Modules which should always be enabled. A manager daemon will enable
249 // modules from the union of this set and the `modules` set above, latest
250 // active version.
251 std::map<uint32_t, std::set<std::string>> always_on_modules;
252
253 // Modules which are reported to exist
254 std::vector<ModuleInfo> available_modules;
255
256 // Map of module name to URI, indicating services exposed by
257 // running modules on the active mgr daemon.
258 std::map<std::string, std::string> services;
259
260 epoch_t get_epoch() const { return epoch; }
261 epoch_t get_last_failure_osd_epoch() const { return last_failure_osd_epoch; }
262 entity_addrvec_t get_active_addrs() const { return active_addrs; }
263 uint64_t get_active_gid() const { return active_gid; }
264 bool get_available() const { return available; }
265 const std::string &get_active_name() const { return active_name; }
266 const utime_t& get_active_change() const { return active_change; }
267
268 bool all_support_module(const std::string& module) {
269 if (!have_module(module)) {
270 return false;
271 }
272 for (auto& p : standbys) {
273 if (!p.second.have_module(module)) {
274 return false;
275 }
276 }
277 return true;
278 }
279
280 bool have_module(const std::string &module_name) const
281 {
282 for (const auto &i : available_modules) {
283 if (i.name == module_name) {
284 return true;
285 }
286 }
287
288 return false;
289 }
290
291 const ModuleInfo *get_module_info(const std::string &module_name) const {
292 for (const auto &i : available_modules) {
293 if (i.name == module_name) {
294 return &i;
295 }
296 }
297 return nullptr;
298 }
299
300 bool can_run_module(const std::string &module_name, std::string *error) const
301 {
302 for (const auto &i : available_modules) {
303 if (i.name == module_name) {
304 *error = i.error_string;
305 return i.can_run;
306 }
307 }
308
309 std::ostringstream oss;
310 oss << "Module '" << module_name << "' does not exist";
311 throw std::logic_error(oss.str());
312 }
313
314 bool module_enabled(const std::string& module_name) const
315 {
316 return modules.find(module_name) != modules.end();
317 }
318
319 bool any_supports_module(const std::string& module) const {
320 if (have_module(module)) {
321 return true;
322 }
323 for (auto& p : standbys) {
324 if (p.second.have_module(module)) {
325 return true;
326 }
327 }
328 return false;
329 }
330
331 bool have_name(const std::string& name) const {
332 if (active_name == name) {
333 return true;
334 }
335 for (auto& p : standbys) {
336 if (p.second.name == name) {
337 return true;
338 }
339 }
340 return false;
341 }
342
343 std::set<std::string> get_all_names() const {
344 std::set<std::string> ls;
345 if (active_name.size()) {
346 ls.insert(active_name);
347 }
348 for (auto& p : standbys) {
349 ls.insert(p.second.name);
350 }
351 return ls;
352 }
353
354 std::set<std::string> get_always_on_modules() const {
355 auto it = always_on_modules.find(ceph::to_integer<uint32_t>(ceph_release()));
356 if (it == always_on_modules.end())
357 return {};
358 return it->second;
359 }
360
361 void encode(ceph::buffer::list& bl, uint64_t features) const
362 {
363 if (!HAVE_FEATURE(features, SERVER_NAUTILUS)) {
364 ENCODE_START(5, 1, bl);
365 encode(epoch, bl);
366 encode(active_addrs.legacy_addr(), bl, features);
367 encode(active_gid, bl);
368 encode(available, bl);
369 encode(active_name, bl);
370 encode(standbys, bl);
371 encode(modules, bl);
372
373 // Pre-version 4 std::string std::list of available modules
374 // (replaced by direct encode of ModuleInfo below)
375 std::set<std::string> old_available_modules;
376 for (const auto &i : available_modules) {
377 old_available_modules.insert(i.name);
378 }
379 encode(old_available_modules, bl);
380
381 encode(services, bl);
382 encode(available_modules, bl);
383 ENCODE_FINISH(bl);
384 return;
385 }
386 ENCODE_START(11, 6, bl);
387 encode(epoch, bl);
388 encode(active_addrs, bl, features);
389 encode(active_gid, bl);
390 encode(available, bl);
391 encode(active_name, bl);
392 encode(standbys, bl);
393 encode(modules, bl);
394 encode(services, bl);
395 encode(available_modules, bl);
396 encode(active_change, bl);
397 encode(always_on_modules, bl);
398 encode(active_mgr_features, bl);
399 encode(last_failure_osd_epoch, bl);
400 encode(clients, bl, features);
401 ENCODE_FINISH(bl);
402 return;
403 }
404
405 void decode(ceph::buffer::list::const_iterator& p)
406 {
407 DECODE_START(11, p);
408 decode(epoch, p);
409 decode(active_addrs, p);
410 decode(active_gid, p);
411 decode(available, p);
412 decode(active_name, p);
413 decode(standbys, p);
414 if (struct_v >= 2) {
415 decode(modules, p);
416
417 if (struct_v < 6) {
418 // Reconstitute ModuleInfos from names
419 std::set<std::string> module_name_list;
420 decode(module_name_list, p);
421 // Only need to unpack this field if we won't have the full
422 // MgrMap::ModuleInfo structures added in v4
423 if (struct_v < 4) {
424 for (const auto &i : module_name_list) {
425 MgrMap::ModuleInfo info;
426 info.name = i;
427 available_modules.push_back(std::move(info));
428 }
429 }
430 }
431 }
432 if (struct_v >= 3) {
433 decode(services, p);
434 }
435 if (struct_v >= 4) {
436 decode(available_modules, p);
437 }
438 if (struct_v >= 7) {
439 decode(active_change, p);
440 } else {
441 active_change = {};
442 }
443 if (struct_v >= 8) {
444 decode(always_on_modules, p);
445 }
446 if (struct_v >= 9) {
447 decode(active_mgr_features, p);
448 }
449 if (struct_v >= 10) {
450 decode(last_failure_osd_epoch, p);
451 }
452 if (struct_v >= 11) {
453 decode(clients, p);
454 }
455 DECODE_FINISH(p);
456 }
457
458 void dump(ceph::Formatter *f) const {
459 f->dump_int("epoch", epoch);
460 f->dump_int("active_gid", get_active_gid());
461 f->dump_string("active_name", get_active_name());
462 f->dump_object("active_addrs", active_addrs);
463 f->dump_stream("active_addr") << active_addrs.get_legacy_str();
464 f->dump_stream("active_change") << active_change;
465 f->dump_unsigned("active_mgr_features", active_mgr_features);
466 f->dump_bool("available", available);
467 f->open_array_section("standbys");
468 for (const auto &i : standbys) {
469 f->open_object_section("standby");
470 f->dump_int("gid", i.second.gid);
471 f->dump_string("name", i.second.name);
472 f->dump_unsigned("mgr_features", i.second.mgr_features);
473 f->open_array_section("available_modules");
474 for (const auto& j : i.second.available_modules) {
475 j.dump(f);
476 }
477 f->close_section();
478 f->close_section();
479 }
480 f->close_section();
481 f->open_array_section("modules");
482 for (auto& i : modules) {
483 f->dump_string("module", i);
484 }
485 f->close_section();
486 f->open_array_section("available_modules");
487 for (const auto& j : available_modules) {
488 j.dump(f);
489 }
490 f->close_section();
491
492 f->open_object_section("services");
493 for (const auto &i : services) {
494 f->dump_string(i.first.c_str(), i.second);
495 }
496 f->close_section();
497
498 f->open_object_section("always_on_modules");
499 for (auto& v : always_on_modules) {
500 f->open_array_section(ceph_release_name(v.first));
501 for (auto& m : v.second) {
502 f->dump_string("module", m);
503 }
504 f->close_section();
505 }
506 f->dump_int("last_failure_osd_epoch", last_failure_osd_epoch);
507 f->open_array_section("active_clients");
508 for (const auto &c : clients) {
509 f->dump_object("client", c);
510 }
511 f->close_section();
512 f->close_section();
513 }
514
515 static void generate_test_instances(std::list<MgrMap*> &l) {
516 l.push_back(new MgrMap);
517 }
518
519 void print_summary(ceph::Formatter *f, std::ostream *ss) const
520 {
521 // One or the other, not both
522 ceph_assert((ss != nullptr) != (f != nullptr));
523 if (f) {
524 f->dump_bool("available", available);
525 f->dump_int("num_standbys", standbys.size());
526 f->open_array_section("modules");
527 for (auto& i : modules) {
528 f->dump_string("module", i);
529 }
530 f->close_section();
531 f->open_object_section("services");
532 for (const auto &i : services) {
533 f->dump_string(i.first.c_str(), i.second);
534 }
535 f->close_section();
536 } else {
537 utime_t now = ceph_clock_now();
538 if (get_active_gid() != 0) {
539 *ss << get_active_name();
540 if (!available) {
541 // If the daemon hasn't gone active yet, indicate that.
542 *ss << "(active, starting";
543 } else {
544 *ss << "(active";
545 }
546 if (active_change) {
547 *ss << ", since " << utimespan_str(now - active_change);
548 }
549 *ss << ")";
550 } else {
551 *ss << "no daemons active";
552 if (active_change) {
553 *ss << " (since " << utimespan_str(now - active_change) << ")";
554 }
555 }
556 if (standbys.size()) {
557 *ss << ", standbys: ";
558 bool first = true;
559 for (const auto &i : standbys) {
560 if (!first) {
561 *ss << ", ";
562 }
563 *ss << i.second.name;
564 first = false;
565 }
566 }
567 }
568 }
569
570 friend std::ostream& operator<<(std::ostream& out, const MgrMap& m) {
571 std::ostringstream ss;
572 m.print_summary(nullptr, &ss);
573 return out << ss.str();
574 }
575
576 friend std::ostream& operator<<(std::ostream& out, const std::vector<ModuleInfo>& mi) {
577 for (const auto &i : mi) {
578 out << i.name << " ";
579 }
580 return out;
581 }
582 };
583
584 WRITE_CLASS_ENCODER_FEATURES(MgrMap)
585 WRITE_CLASS_ENCODER(MgrMap::StandbyInfo)
586 WRITE_CLASS_ENCODER(MgrMap::ModuleInfo);
587 WRITE_CLASS_ENCODER(MgrMap::ModuleOption);
588
589 #endif
590