From: xie xingguo Date: Sat, 16 Mar 2019 09:48:19 +0000 (+0800) Subject: mon/MonClient: implement weight-based mon selection X-Git-Tag: v15.0.0~143^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=8ca47bfc5fc2f1a3ef1166823708d990243caeb3;p=ceph.git mon/MonClient: implement weight-based mon selection - first choose mon by priority, lowest first - if there are still multiple targets, then choose them by their corresponding weight. If no customized or all zero weights specified, all targets will be selected at random. Signed-off-by: xie xingguo --- diff --git a/src/mon/MonClient.cc b/src/mon/MonClient.cc index bf4e1240759..f514e7370f0 100644 --- a/src/mon/MonClient.cc +++ b/src/mon/MonClient.cc @@ -13,6 +13,7 @@ */ #include +#include "common/weighted_shuffle.h" #include "include/scope_guard.h" #include "include/stringify.h" @@ -687,21 +688,24 @@ MonConnection& MonClient::_add_conn(unsigned rank, uint64_t global_id) void MonClient::_add_conns(uint64_t global_id) { - uint16_t min_priority = std::numeric_limits::max(); + map> rank_by_priority; for (const auto& m : monmap.mon_info) { - if (m.second.priority < min_priority) { - min_priority = m.second.priority; - } + rank_by_priority[m.second.priority].push_back(monmap.get_rank(m.first)); } vector ranks; - for (const auto& m : monmap.mon_info) { - if (m.second.priority == min_priority) { - ranks.push_back(monmap.get_rank(m.first)); + ceph_assert(!rank_by_priority.empty()); + ranks = rank_by_priority.begin()->second; + if (ranks.size() > 1) { + vector weights; + for (auto i : ranks) { + auto rank_name = monmap.get_name(i); + weights.push_back(monmap.get_weight(rank_name)); } + std::random_device rd; + std::mt19937 gen(rd()); + weighted_shuffle(ranks, weights, gen); } - std::random_device rd; - std::mt19937 rng(rd()); - std::shuffle(ranks.begin(), ranks.end(), rng); + ldout(cct, 10) << __func__ << " ranks=" << ranks << dendl; unsigned n = cct->_conf->mon_client_hunt_parallel; if (n == 0 || n > ranks.size()) { n = ranks.size();