]> git.proxmox.com Git - ceph.git/blame - ceph/src/mon/MonMap.h
update sources to 12.2.8
[ceph.git] / ceph / src / mon / MonMap.h
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#ifndef CEPH_MONMAP_H
16#define CEPH_MONMAP_H
17
18#include "include/err.h"
19
20#include "msg/Message.h"
21#include "include/types.h"
22#include "mon/mon_types.h"
23
24namespace ceph {
25 class Formatter;
26}
27
28struct mon_info_t {
29 /**
30 * monitor name
31 *
32 * i.e., 'foo' in 'mon.foo'
33 */
34 string name;
35 /**
36 * monitor's public address
37 *
38 * public facing address, traditionally used to communicate with all clients
39 * and other monitors.
40 */
41 entity_addr_t public_addr;
224ce89b
WB
42 /**
43 * the priority of the mon, the lower value the more preferred
44 */
45 uint16_t priority{0};
7c673cae 46
224ce89b
WB
47 mon_info_t(const string& n, const entity_addr_t& p_addr, uint16_t p)
48 : name(n), public_addr(p_addr), priority(p)
49 {}
50 mon_info_t(const string &n, const entity_addr_t& p_addr)
7c673cae
FG
51 : name(n), public_addr(p_addr)
52 { }
53
54 mon_info_t() { }
55
56
57 void encode(bufferlist& bl, uint64_t features) const;
58 void decode(bufferlist::iterator& p);
59 void print(ostream& out) const;
60};
61WRITE_CLASS_ENCODER_FEATURES(mon_info_t)
62
63inline ostream& operator<<(ostream& out, const mon_info_t& mon) {
64 mon.print(out);
65 return out;
66}
67
68class MonMap {
69 public:
70 epoch_t epoch; // what epoch/version of the monmap
71 uuid_d fsid;
72 utime_t last_changed;
73 utime_t created;
74
75 map<string, mon_info_t> mon_info;
76 map<entity_addr_t, string> addr_mons;
77
78 vector<string> ranks;
79
80 /**
81 * Persistent Features are all those features that once set on a
82 * monmap cannot, and should not, be removed. These will define the
83 * non-negotiable features that a given monitor must support to
84 * properly operate in a given quorum.
85 *
86 * Should be reserved for features that we really want to make sure
87 * are sticky, and are important enough to tolerate not being able
88 * to downgrade a monitor.
89 */
90 mon_feature_t persistent_features;
91 /**
92 * Optional Features are all those features that can be enabled or
93 * disabled following a given criteria -- e.g., user-mandated via the
94 * cli --, and act much like indicators of what the cluster currently
95 * supports.
96 *
97 * They are by no means "optional" in the sense that monitors can
98 * ignore them. Just that they are not persistent.
99 */
100 mon_feature_t optional_features;
101
102 /**
103 * Returns the set of features required by this monmap.
104 *
105 * The features required by this monmap is the union of all the
106 * currently set persistent features and the currently set optional
107 * features.
108 *
109 * @returns the set of features required by this monmap
110 */
111 mon_feature_t get_required_features() const {
112 return (persistent_features | optional_features);
113 }
114
115public:
116 void sanitize_mons(map<string,entity_addr_t>& o);
117 void calc_ranks();
118
119 MonMap()
120 : epoch(0) {
7c673cae
FG
121 }
122
123 uuid_d& get_fsid() { return fsid; }
124
125 unsigned size() const {
126 return mon_info.size();
127 }
128
129 epoch_t get_epoch() const { return epoch; }
130 void set_epoch(epoch_t e) { epoch = e; }
131
132 /**
133 * Obtain list of public facing addresses
134 *
135 * @param ls list to populate with the monitors' addresses
136 */
137 void list_addrs(list<entity_addr_t>& ls) const {
138 for (map<string,mon_info_t>::const_iterator p = mon_info.begin();
139 p != mon_info.end();
140 ++p) {
141 ls.push_back(p->second.public_addr);
142 }
143 }
144
224ce89b
WB
145 /**
146 * Add new monitor to the monmap
147 *
148 * @param m monitor info of the new monitor
149 */
150 void add(mon_info_t &&m) {
151 assert(mon_info.count(m.name) == 0);
152 assert(addr_mons.count(m.public_addr) == 0);
153 mon_info[m.name] = std::move(m);
154 calc_ranks();
155 }
156
7c673cae
FG
157 /**
158 * Add new monitor to the monmap
159 *
160 * @param name Monitor name (i.e., 'foo' in 'mon.foo')
161 * @param addr Monitor's public address
162 */
163 void add(const string &name, const entity_addr_t &addr) {
224ce89b 164 add(mon_info_t(name, addr));
7c673cae 165 }
224ce89b 166
7c673cae
FG
167 /**
168 * Remove monitor from the monmap
169 *
170 * @param name Monitor name (i.e., 'foo' in 'mon.foo')
171 */
172 void remove(const string &name) {
173 assert(mon_info.count(name));
174 mon_info.erase(name);
175 assert(mon_info.count(name) == 0);
176 calc_ranks();
177 }
178
179 /**
180 * Rename monitor from @p oldname to @p newname
181 *
182 * @param oldname monitor's current name (i.e., 'foo' in 'mon.foo')
183 * @param newname monitor's new name (i.e., 'bar' in 'mon.bar')
184 */
185 void rename(string oldname, string newname) {
186 assert(contains(oldname));
187 assert(!contains(newname));
188 mon_info[newname] = mon_info[oldname];
189 mon_info.erase(oldname);
190 mon_info[newname].name = newname;
191 calc_ranks();
192 }
193
194 bool contains(const string& name) const {
195 return mon_info.count(name);
196 }
197
198 /**
199 * Check if monmap contains a monitor with address @p a
200 *
201 * @note checks for all addresses a monitor may have, public or otherwise.
202 *
203 * @param a monitor address
204 * @returns true if monmap contains a monitor with address @p;
205 * false otherwise.
206 */
207 bool contains(const entity_addr_t &a) const {
208 for (map<string,mon_info_t>::const_iterator p = mon_info.begin();
209 p != mon_info.end();
210 ++p) {
211 if (p->second.public_addr == a)
212 return true;
213 }
214 return false;
215 }
216
217 string get_name(unsigned n) const {
218 assert(n < ranks.size());
219 return ranks[n];
220 }
221 string get_name(const entity_addr_t& a) const {
222 map<entity_addr_t,string>::const_iterator p = addr_mons.find(a);
223 if (p == addr_mons.end())
224 return string();
225 else
226 return p->second;
227 }
228
229 int get_rank(const string& n) {
230 for (unsigned i = 0; i < ranks.size(); i++)
231 if (ranks[i] == n)
232 return i;
233 return -1;
234 }
235 int get_rank(const entity_addr_t& a) {
236 string n = get_name(a);
237 if (n.empty())
238 return -1;
239
240 for (unsigned i = 0; i < ranks.size(); i++)
241 if (ranks[i] == n)
242 return i;
243 return -1;
244 }
245 bool get_addr_name(const entity_addr_t& a, string& name) {
246 if (addr_mons.count(a) == 0)
247 return false;
248 name = addr_mons[a];
249 return true;
250 }
251
252 const entity_addr_t& get_addr(const string& n) const {
253 assert(mon_info.count(n));
254 map<string,mon_info_t>::const_iterator p = mon_info.find(n);
255 return p->second.public_addr;
256 }
257 const entity_addr_t& get_addr(unsigned m) const {
258 assert(m < ranks.size());
259 return get_addr(ranks[m]);
260 }
261 void set_addr(const string& n, const entity_addr_t& a) {
262 assert(mon_info.count(n));
263 mon_info[n].public_addr = a;
264 calc_ranks();
265 }
266 entity_inst_t get_inst(const string& n) {
267 assert(mon_info.count(n));
268 int m = get_rank(n);
269 assert(m >= 0); // vector can't take negative indicies
270 return get_inst(m);
271 }
272 entity_inst_t get_inst(unsigned m) const {
273 assert(m < ranks.size());
274 entity_inst_t i;
275 i.addr = get_addr(m);
276 i.name = entity_name_t::MON(m);
277 return i;
278 }
279
280 void encode(bufferlist& blist, uint64_t con_features) const;
281 void decode(bufferlist& blist) {
282 bufferlist::iterator p = blist.begin();
283 decode(p);
284 }
285 void decode(bufferlist::iterator &p);
286
287 void generate_fsid() {
288 fsid.generate_random();
289 }
290
291 // read from/write to a file
292 int write(const char *fn);
293 int read(const char *fn);
294
295 /**
296 * build an initial bootstrap monmap from conf
297 *
298 * Build an initial bootstrap monmap from the config. This will
299 * try, in this order:
300 *
301 * 1 monmap -- an explicitly provided monmap
302 * 2 mon_host -- list of monitors
303 * 3 config [mon.*] sections, and 'mon addr' fields in those sections
304 *
305 * @param cct context (and associated config)
306 * @param errout ostream to send error messages too
307 */
308 int build_initial(CephContext *cct, ostream& errout);
309
310 /**
311 * build a monmap from a list of hosts or ips
312 *
313 * Resolve dns as needed. Give mons dummy names.
314 *
315 * @param hosts list of hosts, space or comma separated
316 * @param prefix prefix to prepend to generated mon names
317 * @return 0 for success, -errno on error
318 */
319 int build_from_host_list(std::string hosts, std::string prefix);
320
321 /**
322 * filter monmap given a set of initial members.
323 *
324 * Remove mons that aren't in the initial_members list. Add missing
325 * mons and give them dummy IPs (blank IPv4, with a non-zero
326 * nonce). If the name matches my_name, then my_addr will be used in
327 * place of a dummy addr.
328 *
329 * @param initial_members list of initial member names
330 * @param my_name name of self, can be blank
331 * @param my_addr my addr
332 * @param removed optional pointer to set to insert removed mon addrs to
333 */
334 void set_initial_members(CephContext *cct,
335 list<std::string>& initial_members,
336 string my_name, const entity_addr_t& my_addr,
337 set<entity_addr_t> *removed);
338
339 void print(ostream& out) const;
340 void print_summary(ostream& out) const;
341 void dump(ceph::Formatter *f) const;
342
343 static void generate_test_instances(list<MonMap*>& o);
344};
345WRITE_CLASS_ENCODER_FEATURES(MonMap)
346
347inline ostream& operator<<(ostream &out, const MonMap &m) {
348 m.print_summary(out);
349 return out;
350}
351
352#endif