diff --git a/service/cache_hitrate_calculator.hh b/service/cache_hitrate_calculator.hh index 340f91ecb2..f6f13161d7 100644 --- a/service/cache_hitrate_calculator.hh +++ b/service/cache_hitrate_calculator.hh @@ -28,11 +28,22 @@ namespace service { class cache_hitrate_calculator : public seastar::async_sharded_service { + struct stat { + float h = 0; + float m = 0; + stat& operator+=(stat& o) { + h += o.h; + m += o.m; + return *this; + } + }; + seastar::sharded& _db; seastar::sharded& _me; timer _timer; bool _stopped = false; float _diff = 0; + std::unordered_map _rates; future<> _done = make_ready_future(); future recalculate_hitrates(); diff --git a/service/misc_services.cc b/service/misc_services.cc index f0483d6afe..d44426e4ad 100644 --- a/service/misc_services.cc +++ b/service/misc_services.cc @@ -112,16 +112,6 @@ void cache_hitrate_calculator::run_on(size_t master, lowres_clock::duration d) { } future cache_hitrate_calculator::recalculate_hitrates() { - struct stat { - float h = 0; - float m = 0; - stat& operator+=(stat& o) { - h += o.h; - m += o.m; - return *this; - } - }; - auto non_system_filter = [&] (const std::pair>& cf) { return _db.local().find_keyspace(cf.second->schema()->ks_name()).get_replication_strategy().get_type() != locator::replication_strategy_type::local; }; @@ -143,12 +133,13 @@ future cache_hitrate_calculator::recalculate_hitrates() return _db.map_reduce0(cf_to_cache_hit_stats, std::unordered_map(), sum_stats_per_cf).then([this, non_system_filter] (std::unordered_map rates) mutable { _diff = 0; + _rates = std::move(rates); // set calculated rates on all shards - return _db.invoke_on_all([this, rates = std::move(rates), cpuid = engine().cpu_id(), non_system_filter] (database& db) { + return _db.invoke_on_all([this, cpuid = engine().cpu_id(), non_system_filter] (database& db) { sstring gstate; for (auto& cf : db.get_column_families() | boost::adaptors::filtered(non_system_filter)) { - auto it = rates.find(cf.first); - if (it == rates.end()) { // a table may be added before map/reduce compltes and this code runs + auto it = _rates.find(cf.first); + if (it == _rates.end()) { // a table may be added before map/reduce completes and this code runs continue; } stat s = it->second; @@ -171,6 +162,7 @@ future cache_hitrate_calculator::recalculate_hitrates() return make_ready_future<>(); }); }).then([this] { + _rates.clear(); // if max difference during this round is big schedule next recalculate earlier if (_diff < 0.01) { return std::chrono::milliseconds(2000);