From: xie xingguo Date: Fri, 15 Mar 2019 06:12:38 +0000 (+0800) Subject: mon/MonMap: add weight to mon_info_t X-Git-Tag: v15.0.0~143^2~6 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=bf7490bf7c0e11e7f7630ddc4f0f831a42ba4c12;p=ceph.git mon/MonMap: add weight to mon_info_t Signed-off-by: xie xingguo --- diff --git a/src/mon/MonMap.cc b/src/mon/MonMap.cc index 2a51b7d7421a..33b7082f26f9 100644 --- a/src/mon/MonMap.cc +++ b/src/mon/MonMap.cc @@ -29,7 +29,7 @@ using ceph::Formatter; void mon_info_t::encode(bufferlist& bl, uint64_t features) const { - uint8_t v = 3; + uint8_t v = 4; if (!HAVE_FEATURE(features, SERVER_NAUTILUS)) { v = 2; } @@ -41,17 +41,21 @@ void mon_info_t::encode(bufferlist& bl, uint64_t features) const encode(public_addrs, bl, features); } encode(priority, bl); + encode(weight, bl); ENCODE_FINISH(bl); } void mon_info_t::decode(bufferlist::const_iterator& p) { - DECODE_START(3, p); + DECODE_START(4, p); decode(name, p); decode(public_addrs, p); if (struct_v >= 2) { decode(priority, p); } + if (struct_v >= 4) { + decode(weight, p); + } DECODE_FINISH(p); } @@ -59,7 +63,8 @@ void mon_info_t::print(ostream& out) const { out << "mon." << name << " addrs " << public_addrs - << " priority " << priority; + << " priority " << priority + << " weight " << weight; } namespace { @@ -257,7 +262,7 @@ void MonMap::generate_test_instances(list& o) entity_addrvec_t local_pub_addr; local_pub_addr.parse(local_pub_addr_s, &end_p); - m->add(mon_info_t("filled_pub_addr", entity_addrvec_t(local_pub_addr), 1)); + m->add(mon_info_t("filled_pub_addr", entity_addrvec_t(local_pub_addr), 1, 1)); m->add("empty_addr_zero", entity_addrvec_t()); } @@ -356,9 +361,10 @@ void MonMap::dump(Formatter *f) const // when that happens we need to try them both (unless we can // reasonably infer from the port number which it is). void MonMap::_add_ambiguous_addr(const string& name, - entity_addr_t addr, - int priority, - bool for_mkfs) + entity_addr_t addr, + int priority, + int weight, + bool for_mkfs) { if (addr.get_type() != entity_addr_t::TYPE_ANY) { // a v1: or v2: prefix was specified @@ -373,11 +379,11 @@ void MonMap::_add_ambiguous_addr(const string& name, return; } if (!contains(addr)) { - add(name, entity_addrvec_t(addr), priority); + add(name, entity_addrvec_t(addr), priority, weight); } } else { if (!contains(addr)) { - add(name, entity_addrvec_t(addr), priority); + add(name, entity_addrvec_t(addr), priority, weight); } } } else { @@ -387,16 +393,16 @@ void MonMap::_add_ambiguous_addr(const string& name, addr.set_type(entity_addr_t::TYPE_LEGACY); if (!contains(addr)) { if (!for_mkfs) { - add(name + "-legacy", entity_addrvec_t(addr), priority); + add(name + "-legacy", entity_addrvec_t(addr), priority, weight); } else { - add(name, entity_addrvec_t(addr), priority); + add(name, entity_addrvec_t(addr), priority, weight); } } } else if (addr.get_port() == CEPH_MON_PORT_IANA) { // iana port implies msgr2 addr addr.set_type(entity_addr_t::TYPE_MSGR2); if (!contains(addr)) { - add(name, entity_addrvec_t(addr), priority); + add(name, entity_addrvec_t(addr), priority, weight); } } else if (addr.get_port() == 0) { // no port; include both msgr2 and legacy ports @@ -404,12 +410,12 @@ void MonMap::_add_ambiguous_addr(const string& name, addr.set_type(entity_addr_t::TYPE_MSGR2); addr.set_port(CEPH_MON_PORT_IANA); if (!contains(addr)) { - add(name, entity_addrvec_t(addr), priority); + add(name, entity_addrvec_t(addr), priority, weight); } addr.set_type(entity_addr_t::TYPE_LEGACY); addr.set_port(CEPH_MON_PORT_LEGACY); if (!contains(addr)) { - add(name + "-legacy", entity_addrvec_t(addr), priority); + add(name + "-legacy", entity_addrvec_t(addr), priority, weight); } } else { entity_addrvec_t av; @@ -420,19 +426,19 @@ void MonMap::_add_ambiguous_addr(const string& name, addr.set_port(CEPH_MON_PORT_LEGACY); av.v.push_back(addr); if (!contains(av)) { - add(name, av, priority); + add(name, av, priority, weight); } } } else { addr.set_type(entity_addr_t::TYPE_MSGR2); if (!contains(addr)) { - add(name, entity_addrvec_t(addr), priority); + add(name, entity_addrvec_t(addr), priority, weight); } if (!for_mkfs) { // try legacy on same port too addr.set_type(entity_addr_t::TYPE_LEGACY); if (!contains(addr)) { - add(name + "-legacy", entity_addrvec_t(addr), priority); + add(name + "-legacy", entity_addrvec_t(addr), priority, weight); } } } @@ -459,7 +465,7 @@ int MonMap::init_with_ips(const std::string& ips, name = prefix; name += n; if (addrs[i].v.size() == 1) { - _add_ambiguous_addr(name, addrs[i].front(), 0, for_mkfs); + _add_ambiguous_addr(name, addrs[i].front(), 0, DEFAULT_WEIGHT, for_mkfs); } else { // they specified an addrvec, so let's assume they also specified // the addr *type* and *port*. (we could possibly improve this?) @@ -494,7 +500,7 @@ int MonMap::init_with_hosts(const std::string& hostlist, string name = prefix; name += n; if (addrs[i].v.size() == 1) { - _add_ambiguous_addr(name, addrs[i].front(), 0); + _add_ambiguous_addr(name, addrs[i].front(), 0, DEFAULT_WEIGHT, false); } else { add(name, addrs[i], 0); } @@ -610,13 +616,24 @@ int MonMap::init_with_config_file(const ConfigProxy& conf, continue; } } + uint16_t weight = 0; + if (!conf.get_val_from_conf_file(sections, "mon weight", val, false)) { + try { + weight = std::stoul(val); + } catch (std::logic_error&) { + errout << "unable to parse weight for mon." << mon_name + << ": weight='" << val << "'" + << std::endl; + continue; + } + } - // the make sure this mon isn't already in the map + // make sure this mon isn't already in the map if (contains(addr)) remove(get_name(addr)); if (contains(mon_name)) remove(mon_name); - _add_ambiguous_addr(mon_name, addr, priority); + _add_ambiguous_addr(mon_name, addr, priority, weight, false); } return 0; } @@ -670,7 +687,11 @@ future<> MonMap::init_with_dns_srv(bool for_mkfs, const std::string& name) addr.in6_addr().sin6_addr = a; break; } - _add_ambiguous_addr(record.target, addr, record.priority); + _add_ambiguous_addr(record.target, + addr, + record.priority, + record.weight, + false); }); }); }).handle_exception_type([](const std::system_error& e) { @@ -773,8 +794,11 @@ int MonMap::init_with_dns_srv(CephContext* cct, } else { for (auto& record : records) { record.second.addr.set_type(entity_addr_t::TYPE_ANY); - _add_ambiguous_addr(record.first, record.second.addr, - record.second.priority); + _add_ambiguous_addr(record.first, + record.second.addr, + record.second.priority, + record.second.weight, + false); } return 0; } diff --git a/src/mon/MonMap.h b/src/mon/MonMap.h index dff996f2a1df..1afbac375c66 100644 --- a/src/mon/MonMap.h +++ b/src/mon/MonMap.h @@ -38,6 +38,8 @@ namespace ceph { class Formatter; } +constexpr uint16_t DEFAULT_WEIGHT = 10; + struct mon_info_t { /** * monitor name @@ -56,6 +58,7 @@ struct mon_info_t { * the priority of the mon, the lower value the more preferred */ uint16_t priority{0}; + uint16_t weight{DEFAULT_WEIGHT}; // mon_info_t(const string& n, const entity_addr_t& p_addr, uint16_t p) @@ -63,8 +66,9 @@ struct mon_info_t { {} // - mon_info_t(const string& n, const entity_addrvec_t& p_addrs, uint16_t p) - : name(n), public_addrs(p_addrs), priority(p) + mon_info_t(const string& n, const entity_addrvec_t& p_addrs, + uint16_t p, uint16_t w) + : name(n), public_addrs(p_addrs), priority(p), weight(w) {} mon_info_t(const string &n, const entity_addrvec_t& p_addrs) : name(n), public_addrs(p_addrs) @@ -135,9 +139,10 @@ class MonMap { uint8_t min_mon_release = 0; void _add_ambiguous_addr(const string& name, - entity_addr_t addr, - int priority, - bool for_mkfs=false); + entity_addr_t addr, + int priority, + int weight, + bool for_mkfs); public: void calc_legacy_ranks(); @@ -205,8 +210,8 @@ public: * @param addr Monitor's public address */ void add(const string &name, const entity_addrvec_t &addrv, - int priority=0) { - add(mon_info_t(name, addrv, priority)); + int priority=0, int weight=DEFAULT_WEIGHT) { + add(mon_info_t(name, addrv, priority, weight)); } /**