]> git.proxmox.com Git - ceph.git/blob - ceph/src/mon/MgrMap.h
5a7d33397e6410d2c1e65b4e05ae18470d8eb426
[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
19 #include "msg/msg_types.h"
20 #include "common/Formatter.h"
21 #include "include/encoding.h"
22
23 class StandbyInfo
24 {
25 public:
26 uint64_t gid;
27 std::string name;
28
29 StandbyInfo(uint64_t gid_, const std::string &name_)
30 : gid(gid_), name(name_)
31 {}
32
33 StandbyInfo()
34 : gid(0)
35 {}
36
37 void encode(bufferlist& bl) const
38 {
39 ENCODE_START(1, 1, bl);
40 ::encode(gid, bl);
41 ::encode(name, bl);
42 ENCODE_FINISH(bl);
43 }
44
45 void decode(bufferlist::iterator& p)
46 {
47 DECODE_START(1, p);
48 ::decode(gid, p);
49 ::decode(name, p);
50 DECODE_FINISH(p);
51 }
52 };
53 WRITE_CLASS_ENCODER(StandbyInfo)
54
55 class MgrMap
56 {
57 public:
58 epoch_t epoch = 0;
59
60 /// global_id of the ceph-mgr instance selected as a leader
61 uint64_t active_gid = 0;
62 /// server address reported by the leader once it is active
63 entity_addr_t active_addr;
64 /// whether the nominated leader is active (i.e. has initialized its server)
65 bool available = false;
66 /// the name (foo in mgr.<foo>) of the active daemon
67 std::string active_name;
68
69 std::map<uint64_t, StandbyInfo> standbys;
70
71 epoch_t get_epoch() const { return epoch; }
72 entity_addr_t get_active_addr() const { return active_addr; }
73 uint64_t get_active_gid() const { return active_gid; }
74 bool get_available() const { return available; }
75 const std::string &get_active_name() const { return active_name; }
76
77 void encode(bufferlist& bl, uint64_t features) const
78 {
79 ENCODE_START(1, 1, bl);
80 ::encode(epoch, bl);
81 ::encode(active_addr, bl, features);
82 ::encode(active_gid, bl);
83 ::encode(available, bl);
84 ::encode(active_name, bl);
85 ::encode(standbys, bl);
86 ENCODE_FINISH(bl);
87 }
88
89 void decode(bufferlist::iterator& p)
90 {
91 DECODE_START(1, p);
92 ::decode(epoch, p);
93 ::decode(active_addr, p);
94 ::decode(active_gid, p);
95 ::decode(available, p);
96 ::decode(active_name, p);
97 ::decode(standbys, p);
98 DECODE_FINISH(p);
99 }
100
101 void dump(Formatter *f) const {
102 f->dump_int("epoch", epoch);
103 f->dump_int("active_gid", get_active_gid());
104 f->dump_string("active_name", get_active_name());
105 f->dump_stream("active_addr") << active_addr;
106 f->dump_bool("available", available);
107 f->open_array_section("standbys");
108 for (const auto &i : standbys) {
109 f->open_object_section("standby");
110 f->dump_int("gid", i.second.gid);
111 f->dump_string("name", i.second.name);
112 f->close_section();
113 }
114 f->close_section();
115 }
116
117 static void generate_test_instances(list<MgrMap*> &l) {
118 l.push_back(new MgrMap);
119 }
120
121 void print_summary(Formatter *f, std::ostream *ss) const
122 {
123 // One or the other, not both
124 assert((ss != nullptr) != (f != nullptr));
125 if (f) {
126 dump(f);
127 } else {
128 if (get_active_gid() != 0) {
129 *ss << get_active_name();
130 if (!available) {
131 // If the daemon hasn't gone active yet, indicate that.
132 *ss << "(active, starting)";
133 } else {
134 *ss << "(active)";
135 }
136 } else {
137 *ss << "no daemons active";
138 }
139 if (standbys.size()) {
140 *ss << ", standbys: ";
141 bool first = true;
142 for (const auto &i : standbys) {
143 if (!first) {
144 *ss << ", ";
145 }
146 *ss << i.second.name;
147 first = false;
148 }
149 }
150 }
151 }
152
153 friend ostream& operator<<(ostream& out, const MgrMap& m) {
154 ostringstream ss;
155 m.print_summary(nullptr, &ss);
156 return out << ss.str();
157 }
158 };
159
160 WRITE_CLASS_ENCODER_FEATURES(MgrMap)
161
162 #endif
163