1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
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.
19 #include <seastar/core/future.hh>
22 #include "common/config_fwd.h"
24 #include "include/err.h"
25 #include "include/types.h"
27 #include "mon/mon_types.h"
28 #include "msg/Message.h"
32 namespace ceph::common
{
45 * i.e., 'foo' in 'mon.foo'
49 * monitor's public address(es)
51 * public facing address(es), used to communicate with all clients
52 * and with other monitors.
54 entity_addrvec_t public_addrs
;
56 * the priority of the mon, the lower value the more preferred
61 mon_info_t(const string
& n
, const entity_addr_t
& p_addr
, uint16_t p
)
62 : name(n
), public_addrs(p_addr
), priority(p
)
66 mon_info_t(const string
& n
, const entity_addrvec_t
& p_addrs
, uint16_t p
)
67 : name(n
), public_addrs(p_addrs
), priority(p
)
69 mon_info_t(const string
&n
, const entity_addrvec_t
& p_addrs
)
70 : name(n
), public_addrs(p_addrs
)
76 void encode(bufferlist
& bl
, uint64_t features
) const;
77 void decode(bufferlist::const_iterator
& p
);
78 void print(ostream
& out
) const;
80 WRITE_CLASS_ENCODER_FEATURES(mon_info_t
)
82 inline ostream
& operator<<(ostream
& out
, const mon_info_t
& mon
) {
89 epoch_t epoch
; // what epoch/version of the monmap
94 map
<string
, mon_info_t
> mon_info
;
95 map
<entity_addr_t
, string
> addr_mons
;
100 * Persistent Features are all those features that once set on a
101 * monmap cannot, and should not, be removed. These will define the
102 * non-negotiable features that a given monitor must support to
103 * properly operate in a given quorum.
105 * Should be reserved for features that we really want to make sure
106 * are sticky, and are important enough to tolerate not being able
107 * to downgrade a monitor.
109 mon_feature_t persistent_features
;
111 * Optional Features are all those features that can be enabled or
112 * disabled following a given criteria -- e.g., user-mandated via the
113 * cli --, and act much like indicators of what the cluster currently
116 * They are by no means "optional" in the sense that monitors can
117 * ignore them. Just that they are not persistent.
119 mon_feature_t optional_features
;
122 * Returns the set of features required by this monmap.
124 * The features required by this monmap is the union of all the
125 * currently set persistent features and the currently set optional
128 * @returns the set of features required by this monmap
130 mon_feature_t
get_required_features() const {
131 return (persistent_features
| optional_features
);
135 uint8_t min_mon_release
= 0;
137 void _add_ambiguous_addr(const string
& name
,
140 bool for_mkfs
=false);
143 void calc_legacy_ranks();
144 void calc_addr_mons() {
145 // populate addr_mons
147 for (auto& p
: mon_info
) {
148 for (auto& a
: p
.second
.public_addrs
.v
) {
149 addr_mons
[a
] = p
.first
;
158 uuid_d
& get_fsid() { return fsid
; }
160 unsigned size() const {
161 return mon_info
.size();
164 unsigned min_quorum_size(unsigned total_mons
=0) const {
165 if (total_mons
== 0) {
168 return total_mons
/ 2 + 1;
171 epoch_t
get_epoch() const { return epoch
; }
172 void set_epoch(epoch_t e
) { epoch
= e
; }
175 * Obtain list of public facing addresses
177 * @param ls list to populate with the monitors' addresses
179 void list_addrs(list
<entity_addr_t
>& ls
) const {
180 for (auto& i
: mon_info
) {
181 for (auto& j
: i
.second
.public_addrs
.v
) {
188 * Add new monitor to the monmap
190 * @param m monitor info of the new monitor
192 void add(const mon_info_t
& m
) {
193 ceph_assert(mon_info
.count(m
.name
) == 0);
194 for (auto& a
: m
.public_addrs
.v
) {
195 ceph_assert(addr_mons
.count(a
) == 0);
197 mon_info
[m
.name
] = m
;
198 if (get_required_features().contains_all(
199 ceph::features::mon::FEATURE_NAUTILUS
)) {
200 ranks
.push_back(m
.name
);
201 ceph_assert(ranks
.size() == mon_info
.size());
209 * Add new monitor to the monmap
211 * @param name Monitor name (i.e., 'foo' in 'mon.foo')
212 * @param addr Monitor's public address
214 void add(const string
&name
, const entity_addrvec_t
&addrv
,
216 add(mon_info_t(name
, addrv
, priority
));
220 * Remove monitor from the monmap
222 * @param name Monitor name (i.e., 'foo' in 'mon.foo')
224 void remove(const string
&name
) {
225 ceph_assert(mon_info
.count(name
));
226 mon_info
.erase(name
);
227 ceph_assert(mon_info
.count(name
) == 0);
228 if (get_required_features().contains_all(
229 ceph::features::mon::FEATURE_NAUTILUS
)) {
230 ranks
.erase(std::find(ranks
.begin(), ranks
.end(), name
));
231 ceph_assert(ranks
.size() == mon_info
.size());
239 * Rename monitor from @p oldname to @p newname
241 * @param oldname monitor's current name (i.e., 'foo' in 'mon.foo')
242 * @param newname monitor's new name (i.e., 'bar' in 'mon.bar')
244 void rename(string oldname
, string newname
) {
245 ceph_assert(contains(oldname
));
246 ceph_assert(!contains(newname
));
247 mon_info
[newname
] = mon_info
[oldname
];
248 mon_info
.erase(oldname
);
249 mon_info
[newname
].name
= newname
;
250 if (get_required_features().contains_all(
251 ceph::features::mon::FEATURE_NAUTILUS
)) {
252 *std::find(ranks
.begin(), ranks
.end(), oldname
) = newname
;
253 ceph_assert(ranks
.size() == mon_info
.size());
260 int set_rank(const string
& name
, int rank
) {
261 int oldrank
= get_rank(name
);
265 if (rank
< 0 || rank
>= (int)ranks
.size()) {
268 if (oldrank
!= rank
) {
269 ranks
.erase(ranks
.begin() + oldrank
);
270 ranks
.insert(ranks
.begin() + rank
, name
);
275 bool contains(const string
& name
) const {
276 return mon_info
.count(name
);
280 * Check if monmap contains a monitor with address @p a
282 * @note checks for all addresses a monitor may have, public or otherwise.
284 * @param a monitor address
285 * @returns true if monmap contains a monitor with address @p;
288 bool contains(const entity_addr_t
&a
, string
*name
=nullptr) const {
289 for (auto& i
: mon_info
) {
290 for (auto& j
: i
.second
.public_addrs
.v
) {
301 bool contains(const entity_addrvec_t
&av
, string
*name
=nullptr) const {
302 for (auto& i
: mon_info
) {
303 for (auto& j
: i
.second
.public_addrs
.v
) {
304 for (auto& k
: av
.v
) {
317 string
get_name(unsigned n
) const {
318 ceph_assert(n
< ranks
.size());
321 string
get_name(const entity_addr_t
& a
) const {
322 map
<entity_addr_t
,string
>::const_iterator p
= addr_mons
.find(a
);
323 if (p
== addr_mons
.end())
328 string
get_name(const entity_addrvec_t
& av
) const {
329 for (auto& i
: av
.v
) {
330 map
<entity_addr_t
,string
>::const_iterator p
= addr_mons
.find(i
);
331 if (p
!= addr_mons
.end())
337 int get_rank(const string
& n
) const {
338 if (auto found
= std::find(ranks
.begin(), ranks
.end(), n
);
339 found
!= ranks
.end()) {
340 return std::distance(ranks
.begin(), found
);
345 int get_rank(const entity_addr_t
& a
) const {
346 string n
= get_name(a
);
352 int get_rank(const entity_addrvec_t
& av
) const {
353 string n
= get_name(av
);
359 bool get_addr_name(const entity_addr_t
& a
, string
& name
) {
360 if (addr_mons
.count(a
) == 0)
366 const entity_addrvec_t
& get_addrs(const string
& n
) const {
367 ceph_assert(mon_info
.count(n
));
368 map
<string
,mon_info_t
>::const_iterator p
= mon_info
.find(n
);
369 return p
->second
.public_addrs
;
371 const entity_addrvec_t
& get_addrs(unsigned m
) const {
372 ceph_assert(m
< ranks
.size());
373 return get_addrs(ranks
[m
]);
375 void set_addrvec(const string
& n
, const entity_addrvec_t
& a
) {
376 ceph_assert(mon_info
.count(n
));
377 mon_info
[n
].public_addrs
= a
;
381 void encode(bufferlist
& blist
, uint64_t con_features
) const;
382 void decode(bufferlist
& blist
) {
383 auto p
= std::cbegin(blist
);
386 void decode(bufferlist::const_iterator
& p
);
388 void generate_fsid() {
389 fsid
.generate_random();
392 // read from/write to a file
393 int write(const char *fn
);
394 int read(const char *fn
);
397 * build an initial bootstrap monmap from conf
399 * Build an initial bootstrap monmap from the config. This will
400 * try, in this order:
402 * 1 monmap -- an explicitly provided monmap
403 * 2 mon_host -- list of monitors
404 * 3 config [mon.*] sections, and 'mon addr' fields in those sections
406 * @param cct context (and associated config)
407 * @param errout ostream to send error messages too
410 seastar::future
<> build_initial(const ceph::common::ConfigProxy
& conf
, bool for_mkfs
);
412 int build_initial(CephContext
*cct
, bool for_mkfs
, ostream
& errout
);
415 * filter monmap given a set of initial members.
417 * Remove mons that aren't in the initial_members list. Add missing
418 * mons and give them dummy IPs (blank IPv4, with a non-zero
419 * nonce). If the name matches my_name, then my_addr will be used in
420 * place of a dummy addr.
422 * @param initial_members list of initial member names
423 * @param my_name name of self, can be blank
424 * @param my_addr my addr
425 * @param removed optional pointer to set to insert removed mon addrs to
427 void set_initial_members(CephContext
*cct
,
428 list
<std::string
>& initial_members
,
430 const entity_addrvec_t
& my_addrs
,
431 set
<entity_addrvec_t
> *removed
);
433 void print(ostream
& out
) const;
434 void print_summary(ostream
& out
) const;
435 void dump(ceph::Formatter
*f
) const;
437 static void generate_test_instances(list
<MonMap
*>& o
);
440 * build a monmap from a list of ips
442 * Give mons dummy names.
444 * @param hosts list of ips, space or comma separated
445 * @param prefix prefix to prepend to generated mon names
446 * @return 0 for success, -errno on error
448 int init_with_ips(const std::string
& ips
,
450 const std::string
&prefix
);
452 * build a monmap from a list of hostnames
454 * Give mons dummy names.
456 * @param hosts list of ips, space or comma separated
457 * @param prefix prefix to prepend to generated mon names
458 * @return 0 for success, -errno on error
460 int init_with_hosts(const std::string
& hostlist
,
462 const std::string
& prefix
);
463 int init_with_config_file(const ConfigProxy
& conf
, std::ostream
& errout
);
465 seastar::future
<> read_monmap(const std::string
& monmap
);
466 /// try to build monmap with different settings, like
467 /// mon_host, mon* sections, and mon_dns_srv_name
468 seastar::future
<> build_monmap(const ceph::common::ConfigProxy
& conf
, bool for_mkfs
);
469 /// initialize monmap by resolving given service name
470 seastar::future
<> init_with_dns_srv(bool for_mkfs
, const std::string
& name
);
472 /// read from encoded monmap file
473 int init_with_monmap(const std::string
& monmap
, std::ostream
& errout
);
474 int init_with_dns_srv(CephContext
* cct
, std::string srv_name
, bool for_mkfs
,
475 std::ostream
& errout
);
478 WRITE_CLASS_ENCODER_FEATURES(MonMap
)
480 inline ostream
& operator<<(ostream
&out
, const MonMap
&m
) {
481 m
.print_summary(out
);