From 41e643ddb99b3e7afd40b248d45e71ef9358c246 Mon Sep 17 00:00:00 2001 From: Tomasz Grabiec Date: Tue, 16 Jul 2024 20:44:42 +0200 Subject: [PATCH] tablets: load_balancer: Extract nodes_by_load_cmp Will be reused in a different place. --- service/tablet_allocator.cc | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/service/tablet_allocator.cc b/service/tablet_allocator.cc index cb418823fa..b5c03a1e4d 100644 --- a/service/tablet_allocator.cc +++ b/service/tablet_allocator.cc @@ -311,6 +311,15 @@ class load_balancer { // Data structure used for making load-balancing decisions over a set of nodes. using node_load_map = std::unordered_map; + // Less-comparator which orders nodes by load. + struct nodes_by_load_cmp { + node_load_map& nodes; + + bool operator()(host_id a, host_id b) const { + return nodes[a].avg_load < nodes[b].avg_load; + } + }; + // We have split and merge thresholds, which work respectively as (target) upper and lower // bound for average size of tablets. // @@ -884,9 +893,7 @@ public: std::vector nodes_by_load_dst; nodes_by_load_dst.reserve(nodes.size()); - auto nodes_cmp = [&] (const host_id& a, const host_id& b) { - return nodes[a].avg_load < nodes[b].avg_load; - }; + auto nodes_cmp = nodes_by_load_cmp(nodes); auto nodes_dst_cmp = [&] (const host_id& a, const host_id& b) { return nodes_cmp(b, a); };