From 97e5bfa8152aff690457b4ee733929b9108e7dbb Mon Sep 17 00:00:00 2001 From: Vlad Zolotarov Date: Tue, 12 Apr 2016 14:49:08 +0300 Subject: [PATCH] database: add metrics for total writes and reads This patch adds a counter of total writes and reads for each shard. It seems that nothing ensures that all database queries are ready before database object is destroyed. Make _stats lw_shared_ptr in order to ensure that the object is alive when lambda gets to incrementing it. Signed-off-by: Vlad Zolotarov --- database.cc | 28 ++++++++++++++++++++++++++-- database.hh | 7 +++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/database.cc b/database.cc index eef6d16347..bba8b58a14 100644 --- a/database.cc +++ b/database.cc @@ -1201,6 +1201,8 @@ database::database(const db::config& cfg) : _streaming_dirty_memory_region_group(&_dirty_memory_region_group) , _cfg(std::make_unique(cfg)) , _memtable_total_space([this] { + _stats = make_lw_shared(); + auto memtable_total_space = size_t(_cfg->memtable_total_space_in_mb()) << 20; if (!memtable_total_space) { return memory::stats().total_memory() / 2; @@ -1246,6 +1248,20 @@ database::setup_collectd() { , "bytes", "pending_flushes") , scollectd::make_typed(scollectd::data_type::GAUGE, _cf_stats.pending_memtables_flushes_bytes) )); + + _collectd.push_back( + scollectd::add_polled_metric(scollectd::type_instance_id("database" + , scollectd::per_cpu_plugin_instance + , "total_operations", "total_writes") + , scollectd::make_typed(scollectd::data_type::DERIVE, _stats->total_writes) + )); + + _collectd.push_back( + scollectd::add_polled_metric(scollectd::type_instance_id("database" + , scollectd::per_cpu_plugin_instance + , "total_operations", "total_reads") + , scollectd::make_typed(scollectd::data_type::DERIVE, _stats->total_reads) + )); } database::~database() { @@ -1814,13 +1830,19 @@ column_family::as_mutation_source() const { future> database::query(schema_ptr s, const query::read_command& cmd, query::result_request request, const std::vector& ranges) { column_family& cf = find_column_family(cmd.cf_id); - return cf.query(std::move(s), cmd, request, ranges); + return cf.query(std::move(s), cmd, request, ranges).then([this, s = _stats] (auto&& res) { + ++s->total_reads; + return std::move(res); + }); } future database::query_mutations(schema_ptr s, const query::read_command& cmd, const query::partition_range& range) { column_family& cf = find_column_family(cmd.cf_id); - return mutation_query(std::move(s), cf.as_mutation_source(), range, cmd.slice, cmd.row_limit, cmd.timestamp); + return mutation_query(std::move(s), cf.as_mutation_source(), range, cmd.slice, cmd.row_limit, cmd.timestamp).then([this, s = _stats] (auto&& res) { + ++s->total_reads; + return std::move(res); + }); } std::unordered_set database::get_initial_tokens() { @@ -1997,6 +2019,8 @@ future<> database::apply(schema_ptr s, const frozen_mutation& m) { } return _memtables_throttler.throttle().then([this, &m, s = std::move(s)] { return do_apply(std::move(s), m); + }).then([this, s = _stats] { + ++s->total_writes; }); } diff --git a/database.hh b/database.hh index 9aa621fab8..3d6b8b2f7e 100644 --- a/database.hh +++ b/database.hh @@ -781,6 +781,13 @@ public: class database { ::cf_stats _cf_stats; + struct db_stats { + uint64_t total_writes = 0; + uint64_t total_reads = 0; + }; + + lw_shared_ptr _stats; + logalloc::region_group _dirty_memory_region_group; logalloc::region_group _streaming_dirty_memory_region_group; std::unordered_map _keyspaces;