topology: Add local-dc detection shugar

It's often needed to check if an endpoint sits in the same DC as the
current node. It can be done by

    topo.get_datacenter() == topo.get_datacenter(endpoint)

but in some cases a RAII filter function can be helpful.

Also there's a db::count_local_endpoints() that is surprisingly in use,
so add it to topology as well. Next patches will make use of both.

Signed-off-by: Pavel Emelyanov <xemul@scylladb.com>
This commit is contained in:
Pavel Emelyanov
2022-07-30 13:28:16 +03:00
parent cf47db2bdb
commit ee0828b506
2 changed files with 14 additions and 0 deletions

View File

@@ -1348,6 +1348,12 @@ sstring topology::get_datacenter(inet_address ep) const {
return i_endpoint_snitch::get_local_snitch_ptr()->get_datacenter(ep);
}
std::function<bool(inet_address)> topology::get_local_dc_filter() const noexcept {
return [ this, local_dc = get_datacenter() ] (inet_address ep) {
return get_datacenter(ep) == local_dc;
};
}
/////////////////// class topology end /////////////////////////////////////////
void shared_token_metadata::set(mutable_token_metadata_ptr tmptr) noexcept {

View File

@@ -103,6 +103,14 @@ public:
sstring get_datacenter() const;
sstring get_datacenter(inet_address ep) const;
std::function<bool(inet_address)> get_local_dc_filter() const noexcept;
template <std::ranges::range Range>
inline size_t count_local_endpoints(const Range& endpoints) const {
auto filter = get_local_dc_filter();
return std::count_if(endpoints.begin(), endpoints.end(), filter);
}
private:
/** multi-map: DC -> endpoints in that DC */
std::unordered_map<sstring,