]> git.proxmox.com Git - ceph.git/blob - ceph/src/mon/MgrMap.h
update sources to v12.1.2
[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 std::set<std::string> available_modules;
29
30 StandbyInfo(uint64_t gid_, const std::string &name_,
31 std::set<std::string>& am)
32 : gid(gid_), name(name_), available_modules(am)
33 {}
34
35 StandbyInfo()
36 : gid(0)
37 {}
38
39 void encode(bufferlist& bl) const
40 {
41 ENCODE_START(2, 1, bl);
42 ::encode(gid, bl);
43 ::encode(name, bl);
44 ::encode(available_modules, bl);
45 ENCODE_FINISH(bl);
46 }
47
48 void decode(bufferlist::iterator& p)
49 {
50 DECODE_START(2, p);
51 ::decode(gid, p);
52 ::decode(name, p);
53 if (struct_v >= 2) {
54 ::decode(available_modules, p);
55 }
56 DECODE_FINISH(p);
57 }
58 };
59 WRITE_CLASS_ENCODER(StandbyInfo)
60
61 class MgrMap
62 {
63 public:
64 epoch_t epoch = 0;
65
66 /// global_id of the ceph-mgr instance selected as a leader
67 uint64_t active_gid = 0;
68 /// server address reported by the leader once it is active
69 entity_addr_t active_addr;
70 /// whether the nominated leader is active (i.e. has initialized its server)
71 bool available = false;
72 /// the name (foo in mgr.<foo>) of the active daemon
73 std::string active_name;
74
75 std::map<uint64_t, StandbyInfo> standbys;
76
77 std::set<std::string> modules;
78 std::set<std::string> available_modules;
79
80 epoch_t get_epoch() const { return epoch; }
81 entity_addr_t get_active_addr() const { return active_addr; }
82 uint64_t get_active_gid() const { return active_gid; }
83 bool get_available() const { return available; }
84 const std::string &get_active_name() const { return active_name; }
85
86 bool all_support_module(const std::string& module) {
87 if (!available_modules.count(module)) {
88 return false;
89 }
90 for (auto& p : standbys) {
91 if (!p.second.available_modules.count(module)) {
92 return false;
93 }
94 }
95 return true;
96 }
97
98 bool have_name(const string& name) const {
99 if (active_name == name) {
100 return true;
101 }
102 for (auto& p : standbys) {
103 if (p.second.name == name) {
104 return true;
105 }
106 }
107 return false;
108 }
109
110 std::set<std::string> get_all_names() const {
111 std::set<std::string> ls;
112 if (active_name.size()) {
113 ls.insert(active_name);
114 }
115 for (auto& p : standbys) {
116 ls.insert(p.second.name);
117 }
118 return ls;
119 }
120
121 void encode(bufferlist& bl, uint64_t features) const
122 {
123 ENCODE_START(2, 1, bl);
124 ::encode(epoch, bl);
125 ::encode(active_addr, bl, features);
126 ::encode(active_gid, bl);
127 ::encode(available, bl);
128 ::encode(active_name, bl);
129 ::encode(standbys, bl);
130 ::encode(modules, bl);
131 ::encode(available_modules, bl);
132 ENCODE_FINISH(bl);
133 }
134
135 void decode(bufferlist::iterator& p)
136 {
137 DECODE_START(2, p);
138 ::decode(epoch, p);
139 ::decode(active_addr, p);
140 ::decode(active_gid, p);
141 ::decode(available, p);
142 ::decode(active_name, p);
143 ::decode(standbys, p);
144 if (struct_v >= 2) {
145 ::decode(modules, p);
146 ::decode(available_modules, p);
147 }
148 DECODE_FINISH(p);
149 }
150
151 void dump(Formatter *f) const {
152 f->dump_int("epoch", epoch);
153 f->dump_int("active_gid", get_active_gid());
154 f->dump_string("active_name", get_active_name());
155 f->dump_stream("active_addr") << active_addr;
156 f->dump_bool("available", available);
157 f->open_array_section("standbys");
158 for (const auto &i : standbys) {
159 f->open_object_section("standby");
160 f->dump_int("gid", i.second.gid);
161 f->dump_string("name", i.second.name);
162 f->open_array_section("available_modules");
163 for (auto& j : i.second.available_modules) {
164 f->dump_string("module", j);
165 }
166 f->close_section();
167 f->close_section();
168 }
169 f->close_section();
170 f->open_array_section("modules");
171 for (auto& i : modules) {
172 f->dump_string("module", i);
173 }
174 f->close_section();
175 f->open_array_section("available_modules");
176 for (auto& j : available_modules) {
177 f->dump_string("module", j);
178 }
179 f->close_section();
180 }
181
182 static void generate_test_instances(list<MgrMap*> &l) {
183 l.push_back(new MgrMap);
184 }
185
186 void print_summary(Formatter *f, std::ostream *ss) const
187 {
188 // One or the other, not both
189 assert((ss != nullptr) != (f != nullptr));
190 if (f) {
191 dump(f);
192 } else {
193 if (get_active_gid() != 0) {
194 *ss << get_active_name();
195 if (!available) {
196 // If the daemon hasn't gone active yet, indicate that.
197 *ss << "(active, starting)";
198 } else {
199 *ss << "(active)";
200 }
201 } else {
202 *ss << "no daemons active";
203 }
204 if (standbys.size()) {
205 *ss << ", standbys: ";
206 bool first = true;
207 for (const auto &i : standbys) {
208 if (!first) {
209 *ss << ", ";
210 }
211 *ss << i.second.name;
212 first = false;
213 }
214 }
215 }
216 }
217
218 friend ostream& operator<<(ostream& out, const MgrMap& m) {
219 ostringstream ss;
220 m.print_summary(nullptr, &ss);
221 return out << ss.str();
222 }
223 };
224
225 WRITE_CLASS_ENCODER_FEATURES(MgrMap)
226
227 #endif
228