]>
Commit | Line | Data |
---|---|---|
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) 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; | |
224ce89b | 28 | std::set<std::string> available_modules; |
7c673cae | 29 | |
224ce89b WB |
30 | StandbyInfo(uint64_t gid_, const std::string &name_, |
31 | std::set<std::string>& am) | |
32 | : gid(gid_), name(name_), available_modules(am) | |
7c673cae FG |
33 | {} |
34 | ||
35 | StandbyInfo() | |
36 | : gid(0) | |
37 | {} | |
38 | ||
39 | void encode(bufferlist& bl) const | |
40 | { | |
224ce89b | 41 | ENCODE_START(2, 1, bl); |
7c673cae FG |
42 | ::encode(gid, bl); |
43 | ::encode(name, bl); | |
224ce89b | 44 | ::encode(available_modules, bl); |
7c673cae FG |
45 | ENCODE_FINISH(bl); |
46 | } | |
47 | ||
48 | void decode(bufferlist::iterator& p) | |
49 | { | |
224ce89b | 50 | DECODE_START(2, p); |
7c673cae FG |
51 | ::decode(gid, p); |
52 | ::decode(name, p); | |
224ce89b WB |
53 | if (struct_v >= 2) { |
54 | ::decode(available_modules, p); | |
55 | } | |
7c673cae FG |
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 | ||
224ce89b WB |
77 | std::set<std::string> modules; |
78 | std::set<std::string> available_modules; | |
79 | ||
7c673cae FG |
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 | ||
224ce89b WB |
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 | ||
7c673cae FG |
98 | void encode(bufferlist& bl, uint64_t features) const |
99 | { | |
224ce89b | 100 | ENCODE_START(2, 1, bl); |
7c673cae FG |
101 | ::encode(epoch, bl); |
102 | ::encode(active_addr, bl, features); | |
103 | ::encode(active_gid, bl); | |
104 | ::encode(available, bl); | |
105 | ::encode(active_name, bl); | |
106 | ::encode(standbys, bl); | |
224ce89b WB |
107 | ::encode(modules, bl); |
108 | ::encode(available_modules, bl); | |
7c673cae FG |
109 | ENCODE_FINISH(bl); |
110 | } | |
111 | ||
112 | void decode(bufferlist::iterator& p) | |
113 | { | |
224ce89b | 114 | DECODE_START(2, p); |
7c673cae FG |
115 | ::decode(epoch, p); |
116 | ::decode(active_addr, p); | |
117 | ::decode(active_gid, p); | |
118 | ::decode(available, p); | |
119 | ::decode(active_name, p); | |
120 | ::decode(standbys, p); | |
224ce89b WB |
121 | if (struct_v >= 2) { |
122 | ::decode(modules, p); | |
123 | ::decode(available_modules, p); | |
124 | } | |
7c673cae FG |
125 | DECODE_FINISH(p); |
126 | } | |
127 | ||
128 | void dump(Formatter *f) const { | |
129 | f->dump_int("epoch", epoch); | |
130 | f->dump_int("active_gid", get_active_gid()); | |
131 | f->dump_string("active_name", get_active_name()); | |
132 | f->dump_stream("active_addr") << active_addr; | |
133 | f->dump_bool("available", available); | |
134 | f->open_array_section("standbys"); | |
135 | for (const auto &i : standbys) { | |
136 | f->open_object_section("standby"); | |
137 | f->dump_int("gid", i.second.gid); | |
138 | f->dump_string("name", i.second.name); | |
224ce89b WB |
139 | f->open_array_section("available_modules"); |
140 | for (auto& j : i.second.available_modules) { | |
141 | f->dump_string("module", j); | |
142 | } | |
143 | f->close_section(); | |
7c673cae FG |
144 | f->close_section(); |
145 | } | |
224ce89b WB |
146 | f->close_section(); |
147 | f->open_array_section("modules"); | |
148 | for (auto& i : modules) { | |
149 | f->dump_string("module", i); | |
150 | } | |
151 | f->close_section(); | |
152 | f->open_array_section("available_modules"); | |
153 | for (auto& j : available_modules) { | |
154 | f->dump_string("module", j); | |
155 | } | |
7c673cae FG |
156 | f->close_section(); |
157 | } | |
158 | ||
159 | static void generate_test_instances(list<MgrMap*> &l) { | |
160 | l.push_back(new MgrMap); | |
161 | } | |
162 | ||
163 | void print_summary(Formatter *f, std::ostream *ss) const | |
164 | { | |
165 | // One or the other, not both | |
166 | assert((ss != nullptr) != (f != nullptr)); | |
167 | if (f) { | |
168 | dump(f); | |
169 | } else { | |
170 | if (get_active_gid() != 0) { | |
31f18b77 | 171 | *ss << get_active_name(); |
7c673cae FG |
172 | if (!available) { |
173 | // If the daemon hasn't gone active yet, indicate that. | |
31f18b77 FG |
174 | *ss << "(active, starting)"; |
175 | } else { | |
176 | *ss << "(active)"; | |
7c673cae | 177 | } |
7c673cae | 178 | } else { |
31f18b77 | 179 | *ss << "no daemons active"; |
7c673cae FG |
180 | } |
181 | if (standbys.size()) { | |
31f18b77 | 182 | *ss << ", standbys: "; |
7c673cae FG |
183 | bool first = true; |
184 | for (const auto &i : standbys) { | |
185 | if (!first) { | |
186 | *ss << ", "; | |
187 | } | |
188 | *ss << i.second.name; | |
189 | first = false; | |
190 | } | |
191 | } | |
192 | } | |
193 | } | |
194 | ||
195 | friend ostream& operator<<(ostream& out, const MgrMap& m) { | |
196 | ostringstream ss; | |
197 | m.print_summary(nullptr, &ss); | |
198 | return out << ss.str(); | |
199 | } | |
200 | }; | |
201 | ||
202 | WRITE_CLASS_ENCODER_FEATURES(MgrMap) | |
203 | ||
204 | #endif | |
205 |