From 88fefea3401ec969051ab82112e656c415f3179f Mon Sep 17 00:00:00 2001 From: Dan Wang Date: Tue, 18 Apr 2023 14:56:56 +0800 Subject: [PATCH 1/7] feat(new_metrics): migrate server-level metrics for replica_stub (part 1) --- src/replica/replica.cpp | 4 +- src/replica/replica.h | 2 +- src/replica/replica_stub.cpp | 66 ++++++++++++++++------------ src/replica/replica_stub.h | 11 +++-- src/replica/replication_app_base.cpp | 2 +- src/utils/metrics.h | 3 +- 6 files changed, 51 insertions(+), 37 deletions(-) diff --git a/src/replica/replica.cpp b/src/replica/replica.cpp index 80bbed9d00..1168cb1bea 100644 --- a/src/replica/replica.cpp +++ b/src/replica/replica.cpp @@ -230,9 +230,9 @@ void replica::update_last_checkpoint_generate_time() // Statistics // // // -void replica::update_commit_qps(int count) +void replica::update_commit_requests(int count) { - _stub->_counter_replicas_commit_qps->add((uint64_t)count); + METRIC_INCREMENT_BY(*_stub, committed_requests, count); } void replica::init_state() diff --git a/src/replica/replica.h b/src/replica/replica.h index bdead98e95..9f003e8140 100644 --- a/src/replica/replica.h +++ b/src/replica/replica.h @@ -284,7 +284,7 @@ class replica : public serverlet, public ref_counter, public replica_ba // // Statistics // - void update_commit_qps(int count); + void update_commit_requests(int count); // routine for get extra envs from replica const std::map &get_replica_extra_envs() const { return _extra_envs; } diff --git a/src/replica/replica_stub.cpp b/src/replica/replica_stub.cpp index 715bc718fb..9312def05f 100644 --- a/src/replica/replica_stub.cpp +++ b/src/replica/replica_stub.cpp @@ -95,6 +95,26 @@ #include "remote_cmd/remote_command.h" #include "utils/fail_point.h" +METRIC_DEFINE_gauge_int64(server, + total_replicas, + dsn::metric_unit::kReplicas, + "The total number of replicas"); + +METRIC_DEFINE_gauge_int64(server, + opening_replicas, + dsn::metric_unit::kReplicas, + "The number of opening replicas"); + +METRIC_DEFINE_gauge_int64(server, + closing_replicas, + dsn::metric_unit::kReplicas, + "The number of closing replicas"); + +METRIC_DEFINE_counter(server, + committed_requests, + dsn::metric_unit::kRequests, + "The number of committed requests"); + namespace dsn { namespace replication { @@ -197,7 +217,11 @@ replica_stub::replica_stub(replica_state_subscriber subscriber /*= nullptr*/, _learn_app_concurrent_count(0), _bulk_load_downloading_count(0), _manual_emergency_checkpointing_count(0), - _is_running(false) + _is_running(false), + METRIC_VAR_INIT_server(total_replicas), + METRIC_VAR_INIT_server(opening_replicas), + METRIC_VAR_INIT_server(closing_replicas), + METRIC_VAR_INIT_server(committed_requests), { #ifdef DSN_ENABLE_GPERF _is_releasing_memory = false; @@ -215,20 +239,6 @@ replica_stub::~replica_stub(void) { close(); } void replica_stub::install_perf_counters() { - _counter_replicas_count.init_app_counter( - "eon.replica_stub", "replica(Count)", COUNTER_TYPE_NUMBER, "# in replica_stub._replicas"); - _counter_replicas_opening_count.init_app_counter("eon.replica_stub", - "opening.replica(Count)", - COUNTER_TYPE_NUMBER, - "# in replica_stub._opening_replicas"); - _counter_replicas_closing_count.init_app_counter("eon.replica_stub", - "closing.replica(Count)", - COUNTER_TYPE_NUMBER, - "# in replica_stub._closing_replicas"); - _counter_replicas_commit_qps.init_app_counter("eon.replica_stub", - "replicas.commit.qps", - COUNTER_TYPE_RATE, - "server-level commit throughput"); _counter_replicas_learning_count.init_app_counter("eon.replica_stub", "replicas.learning.count", COUNTER_TYPE_NUMBER, @@ -797,7 +807,7 @@ void replica_stub::initialize(const replication_options &opts, bool clear /* = f // attach rps _replicas = std::move(rps); - _counter_replicas_count->add((uint64_t)_replicas.size()); + METRIC_VAR_INCREMENT_BY(total_replicas, _replicas.size()); for (const auto &kv : _replicas) { _fs_manager.add_replica(kv.first, kv.second->dir()); } @@ -2032,10 +2042,10 @@ task_ptr replica_stub::begin_open_replica( if (rep->status() == partition_status::PS_INACTIVE && tsk->cancel(false)) { // reopen it _closing_replicas.erase(it); - _counter_replicas_closing_count->decrement(); + METRIC_VAR_DECREMENT(closing_replicas); _replicas.emplace(id, rep); - _counter_replicas_count->increment(); + METRIC_VAR_INCREMENT(total_replicas); _closed_replicas.erase(id); @@ -2061,7 +2071,7 @@ task_ptr replica_stub::begin_open_replica( std::bind(&replica_stub::open_replica, this, app, id, group_check, configuration_update)); _opening_replicas[id] = task; - _counter_replicas_opening_count->increment(); + METRIC_VAR_INCREMENT(opening_replicas); _closed_replicas.erase(id); _replicas_lock.unlock_write(); @@ -2169,7 +2179,7 @@ void replica_stub::open_replica( 0, "replica {} is not in _opening_replicas", id.to_string()); - _counter_replicas_opening_count->decrement(); + METRIC_VAR_DECREMENT(opening_replicas); return; } @@ -2179,13 +2189,13 @@ void replica_stub::open_replica( 0, "replica {} is not in _opening_replicas", id.to_string()); - _counter_replicas_opening_count->decrement(); + METRIC_VAR_DECREMENT(opening_replicas); CHECK(_replicas.find(id) == _replicas.end(), "replica {} is already in _replicas", id.to_string()); _replicas.insert(replicas::value_type(rep->get_gpid(), rep)); - _counter_replicas_count->increment(); + METRIC_VAR_INCREMENT(total_replicas); _closed_replicas.erase(id); } @@ -2342,7 +2352,7 @@ task_ptr replica_stub::begin_close_replica(replica_ptr r) return nullptr; } - _counter_replicas_count->decrement(); + METRIC_VAR_DECREMENT(total_replicas); int delay_ms = 0; if (r->status() == partition_status::PS_INACTIVE) { @@ -2361,7 +2371,7 @@ task_ptr replica_stub::begin_close_replica(replica_ptr r) 0, std::chrono::milliseconds(delay_ms)); _closing_replicas[id] = std::make_tuple(task, r, std::move(a_info), std::move(r_info)); - _counter_replicas_closing_count->increment(); + METRIC_VAR_INCREMENT(closing_replicas); return task; } @@ -2381,7 +2391,7 @@ void replica_stub::close_replica(replica_ptr r) _closed_replicas.emplace( id, std::make_pair(std::get<2>(find->second), std::get<3>(find->second))); _closing_replicas.erase(find); - _counter_replicas_closing_count->decrement(); + METRIC_VAR_DECREMENT(closing_replicas); } if (r->is_data_corrupted()) { @@ -2840,7 +2850,7 @@ void replica_stub::close() task->cancel(true); - _counter_replicas_opening_count->decrement(); + METRIC_VAR_DECREMENT(opening_replicas); _replicas_lock.lock_write(); _opening_replicas.erase(_opening_replicas.begin()); } @@ -2848,7 +2858,7 @@ void replica_stub::close() while (!_replicas.empty()) { _replicas.begin()->second->close(); - _counter_replicas_count->decrement(); + METRIC_VAR_DECREMENT(total_replicas); _replicas.erase(_replicas.begin()); } } @@ -3007,7 +3017,7 @@ replica_ptr replica_stub::create_child_replica_if_not_found(gpid child_pid, if (rep != nullptr) { auto pr = _replicas.insert(replicas::value_type(child_pid, rep)); CHECK(pr.second, "child replica {} has been existed", rep->name()); - _counter_replicas_count->increment(); + METRIC_VAR_INCREMENT(total_replicas); _closed_replicas.erase(child_pid); } return rep; diff --git a/src/replica/replica_stub.h b/src/replica/replica_stub.h index edcbbca716..b8561e1ca3 100644 --- a/src/replica/replica_stub.h +++ b/src/replica/replica_stub.h @@ -69,6 +69,7 @@ #include "utils/autoref_ptr.h" #include "utils/error_code.h" #include "utils/flags.h" +#include "utils/metrics.h" #include "utils/zlocks.h" namespace dsn { @@ -365,6 +366,8 @@ class replica_stub : public serverlet, public ref_counter // Wait all replicas in closing state to be finished. void wait_closing_replicas_finished(); + METRIC_DEFINE_INCREMENT_BY(committed_requests) + private: friend class ::dsn::replication::test::test_checker; friend class ::dsn::replication::replica; @@ -467,10 +470,10 @@ class replica_stub : public serverlet, public ref_counter #endif // performance counters - perf_counter_wrapper _counter_replicas_count; - perf_counter_wrapper _counter_replicas_opening_count; - perf_counter_wrapper _counter_replicas_closing_count; - perf_counter_wrapper _counter_replicas_commit_qps; + METRIC_VAR_DECLARE_gauge_int64(total_replicas); + METRIC_VAR_DECLARE_gauge_int64(opening_replicas); + METRIC_VAR_DECLARE_gauge_int64(closing_replicas); + METRIC_VAR_DECLARE_counter(committed_requests); perf_counter_wrapper _counter_replicas_learning_count; perf_counter_wrapper _counter_replicas_learning_max_duration_time_ms; diff --git a/src/replica/replication_app_base.cpp b/src/replica/replication_app_base.cpp index 8b20d6df8c..2d535b0657 100644 --- a/src/replica/replication_app_base.cpp +++ b/src/replica/replication_app_base.cpp @@ -485,7 +485,7 @@ error_code replication_app_base::apply_mutation(const mutation *mu) "mutation {} committed on {}, batched_count = {}", mu->name(), str, batched_count); } - _replica->update_commit_qps(batched_count); + _replica->update_commit_requests(batched_count); return ERR_OK; } diff --git a/src/utils/metrics.h b/src/utils/metrics.h index 8b9d396a9c..23ce4d9bda 100644 --- a/src/utils/metrics.h +++ b/src/utils/metrics.h @@ -646,6 +646,8 @@ enum class metric_unit : size_t kCapacityUnits, kPercent, kPartitions, + kReplicas, + kServers, kRequests, kSeeks, kPointLookups, @@ -660,7 +662,6 @@ enum class metric_unit : size_t kOperations, kTasks, kDisconnections, - kServers, kInvalidUnit, }; From e8835aca79a96179112d9122cb72daf36b440e15 Mon Sep 17 00:00:00 2001 From: Dan Wang Date: Tue, 18 Apr 2023 15:00:56 +0800 Subject: [PATCH 2/7] feat(new_metrics): migrate server-level metrics for replica_stub (part 1) --- src/replica/replica_stub.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/replica/replica_stub.cpp b/src/replica/replica_stub.cpp index 9312def05f..710e1956a0 100644 --- a/src/replica/replica_stub.cpp +++ b/src/replica/replica_stub.cpp @@ -111,9 +111,9 @@ METRIC_DEFINE_gauge_int64(server, "The number of closing replicas"); METRIC_DEFINE_counter(server, - committed_requests, - dsn::metric_unit::kRequests, - "The number of committed requests"); + committed_requests, + dsn::metric_unit::kRequests, + "The number of committed requests"); namespace dsn { namespace replication { From e24f11d320ed66b26a06d1cdccdc30430849d9c4 Mon Sep 17 00:00:00 2001 From: Dan Wang Date: Tue, 18 Apr 2023 16:10:33 +0800 Subject: [PATCH 3/7] feat(new_metrics): migrate server-level metrics for replica_stub (part 1) --- src/replica/replica_stub.cpp | 2 +- src/utils/metrics.h | 19 ++++++++++--------- src/utils/test/metrics_test.cpp | 14 +++++++------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/replica/replica_stub.cpp b/src/replica/replica_stub.cpp index 710e1956a0..04dee0c432 100644 --- a/src/replica/replica_stub.cpp +++ b/src/replica/replica_stub.cpp @@ -221,7 +221,7 @@ replica_stub::replica_stub(replica_state_subscriber subscriber /*= nullptr*/, METRIC_VAR_INIT_server(total_replicas), METRIC_VAR_INIT_server(opening_replicas), METRIC_VAR_INIT_server(closing_replicas), - METRIC_VAR_INIT_server(committed_requests), + METRIC_VAR_INIT_server(committed_requests) { #ifdef DSN_ENABLE_GPERF _is_releasing_memory = false; diff --git a/src/utils/metrics.h b/src/utils/metrics.h index 23ce4d9bda..64358e6c1e 100644 --- a/src/utils/metrics.h +++ b/src/utils/metrics.h @@ -153,7 +153,8 @@ class error_code; // // Since a type tends to be a class template where there might be commas, use variadic arguments // instead of a single fixed argument to represent a type. -#define METRIC_VAR_DECLARE(name, ...) __VA_ARGS__ _##name +#define METRIC_VAR_NAME(name) _metric_##name +#define METRIC_VAR_DECLARE(name, ...) __VA_ARGS__ METRIC_VAR_NAME(name) #define METRIC_VAR_DECLARE_gauge_int64(name) METRIC_VAR_DECLARE(name, dsn::gauge_ptr) #define METRIC_VAR_DECLARE_counter(name) \ METRIC_VAR_DECLARE(name, dsn::counter_ptr) @@ -162,7 +163,7 @@ class error_code; // Initialize a metric variable in user class. #define METRIC_VAR_INIT(name, entity, ...) \ - _##name(METRIC_##name.instantiate(entity##_metric_entity(), ##__VA_ARGS__)) + METRIC_VAR_NAME(name)(METRIC_##name.instantiate(entity##_metric_entity(), ##__VA_ARGS__)) #define METRIC_VAR_INIT_replica(name, ...) METRIC_VAR_INIT(name, replica, ##__VA_ARGS__) #define METRIC_VAR_INIT_server(name, ...) METRIC_VAR_INIT(name, server, ##__VA_ARGS__) #define METRIC_VAR_INIT_disk(name, ...) METRIC_VAR_INIT(name, disk, ##__VA_ARGS__) @@ -175,15 +176,15 @@ class error_code; do { \ const auto v = (x); \ if (v != 0) { \ - _##name->increment_by(v); \ + METRIC_VAR_NAME(name)->increment_by(v); \ } \ } while (0) // Perform increment() operations on gauges and counters. -#define METRIC_VAR_INCREMENT(name) _##name->increment() +#define METRIC_VAR_INCREMENT(name) METRIC_VAR_NAME(name)->increment() // Perform decrement() operations on gauges. -#define METRIC_VAR_DECREMENT(name) _##name->decrement() +#define METRIC_VAR_DECREMENT(name) METRIC_VAR_NAME(name)->decrement() // Perform set() operations on gauges and percentiles. // @@ -191,20 +192,20 @@ class error_code; // * set(val): set a single value for a metric, such as gauge, percentile; // * set(n, val): set multiple repeated values (the number of duplicates is n) for a metric, // such as percentile. -#define METRIC_VAR_SET(name, ...) _##name->set(__VA_ARGS__) +#define METRIC_VAR_SET(name, ...) METRIC_VAR_NAME(name)->set(__VA_ARGS__) // Read the current measurement of gauges and counters. -#define METRIC_VAR_VALUE(name) _##name->value() +#define METRIC_VAR_VALUE(name) METRIC_VAR_NAME(name)->value() // Convenient macro that is used to compute latency automatically, which is dedicated to percentile. #define METRIC_VAR_AUTO_LATENCY(name, ...) \ - dsn::auto_latency __##name##_auto_latency(_##name, ##__VA_ARGS__) + dsn::auto_latency __##name##_auto_latency(METRIC_VAR_NAME(name), ##__VA_ARGS__) #define METRIC_VAR_AUTO_LATENCY_DURATION_NS(name) __##name##_auto_latency.duration_ns() // Convenient macro that is used to increment/decrement gauge automatically in current scope. #define METRIC_VAR_AUTO_COUNT(name, ...) \ - dsn::auto_count __##name##_auto_count(_##name, ##__VA_ARGS__) + dsn::auto_count __##name##_auto_count(METRIC_VAR_NAME(name), ##__VA_ARGS__) #define METRIC_DEFINE_INCREMENT_BY(name) \ void increment_##name##_by(int64_t x) { METRIC_VAR_INCREMENT_BY(name, x); } diff --git a/src/utils/test/metrics_test.cpp b/src/utils/test/metrics_test.cpp index 5197ec4b6a..24b5d43e3d 100644 --- a/src/utils/test/metrics_test.cpp +++ b/src/utils/test/metrics_test.cpp @@ -3083,12 +3083,12 @@ class MetricVarTest : public testing::Test void SetUp() override { - _test_replica_gauge_int64->set(0); - _test_replica_counter->reset(); - _test_replica_percentile_int64_ns->reset_tail_for_test(); - _test_replica_percentile_int64_us->reset_tail_for_test(); - _test_replica_percentile_int64_ms->reset_tail_for_test(); - _test_replica_percentile_int64_s->reset_tail_for_test(); + METRIC_VAR_SET(test_replica_gauge_int64, 0); + METRIC_VAR_NAME(test_replica_counter)->reset(); + METRIC_VAR_NAME(test_replica_percentile_int64_ns)->reset_tail_for_test(); + METRIC_VAR_NAME(test_replica_percentile_int64_us)->reset_tail_for_test(); + METRIC_VAR_NAME(test_replica_percentile_int64_ms)->reset_tail_for_test(); + METRIC_VAR_NAME(test_replica_percentile_int64_s)->reset_tail_for_test(); } const metric_entity_ptr &my_replica_metric_entity() const { return _my_replica_metric_entity; } @@ -3120,7 +3120,7 @@ MetricVarTest::MetricVarTest() { } -#define METRIC_VAR_SAMPLES(name) _##name->samples_for_test() +#define METRIC_VAR_SAMPLES(name) METRIC_VAR_NAME(name)->samples_for_test() void MetricVarTest::test_set_percentile(const std::vector &expected_samples) { From f06ab0a4a0bd9c93812dc3be7dad07d147545713 Mon Sep 17 00:00:00 2001 From: Dan Wang Date: Tue, 18 Apr 2023 19:38:29 +0800 Subject: [PATCH 4/7] feat(new_metrics): migrate server-level metrics for replica_stub (part 1) --- src/meta/table_metrics.h | 10 +++++----- src/replica/replica.cpp | 6 +----- src/replica/replica.h | 2 +- src/replica/replication_app_base.cpp | 2 +- src/utils/metrics.h | 23 +++++++++++++++++------ 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/meta/table_metrics.h b/src/meta/table_metrics.h index 7dc5e83a31..cec35e64b8 100644 --- a/src/meta/table_metrics.h +++ b/src/meta/table_metrics.h @@ -93,7 +93,7 @@ class table_metrics METRIC_DEFINE_SET(healthy_partitions, int64_t) #define __METRIC_DEFINE_INCREMENT_BY(name) \ - void increment_##name##_by(int32_t partition_id, int64_t x) \ + void METRIC_FUNC_NAME_INCREMENT_BY(name)(int32_t partition_id, int64_t x) \ { \ CHECK_LT(partition_id, _partition_metrics.size()); \ METRIC_INCREMENT_BY(*(_partition_metrics[partition_id]), name, x); \ @@ -106,7 +106,7 @@ class table_metrics #undef __METRIC_DEFINE_INCREMENT_BY #define __METRIC_DEFINE_INCREMENT(name) \ - void increment_##name(int32_t partition_id) \ + void METRIC_FUNC_NAME_INCREMENT(name)(int32_t partition_id) \ { \ CHECK_LT(partition_id, _partition_metrics.size()); \ METRIC_INCREMENT(*(_partition_metrics[partition_id]), name); \ @@ -120,7 +120,7 @@ class table_metrics #undef __METRIC_DEFINE_INCREMENT #define __METRIC_DEFINE_SET(name, value_type) \ - void set_##name(int32_t partition_id, value_type value) \ + void METRIC_FUNC_NAME_SET(name)(int32_t partition_id, value_type value) \ { \ CHECK_LT(partition_id, _partition_metrics.size()); \ METRIC_SET(*(_partition_metrics[partition_id]), name, value); \ @@ -167,7 +167,7 @@ class greedy_balance_stats using partition_map = std::unordered_map; #define __METRIC_DEFINE_INCREMENT(name) \ - void increment_##name(const gpid &id, bool balance_checker) \ + void METRIC_FUNC_NAME_INCREMENT(name)(const gpid &id, bool balance_checker) \ { \ auto &partition = _partition_map[id]; \ ++(partition.greedy_recent_balance_operations); \ @@ -210,7 +210,7 @@ class table_metric_entities void clear_entities(); #define __METRIC_DEFINE_INCREMENT(name) \ - void increment_##name(const gpid &id) \ + void METRIC_FUNC_NAME_INCREMENT(name)(const gpid &id) \ { \ utils::auto_read_lock l(_lock); \ \ diff --git a/src/replica/replica.cpp b/src/replica/replica.cpp index 1168cb1bea..383774a5f5 100644 --- a/src/replica/replica.cpp +++ b/src/replica/replica.cpp @@ -229,11 +229,7 @@ void replica::update_last_checkpoint_generate_time() // // // Statistics // // // - -void replica::update_commit_requests(int count) -{ - METRIC_INCREMENT_BY(*_stub, committed_requests, count); -} +METRIC_IMPL_INCREMENT_BY(*_stub, committed_requests, replica::) void replica::init_state() { diff --git a/src/replica/replica.h b/src/replica/replica.h index 9f003e8140..e26d816905 100644 --- a/src/replica/replica.h +++ b/src/replica/replica.h @@ -284,7 +284,7 @@ class replica : public serverlet, public ref_counter, public replica_ba // // Statistics // - void update_commit_requests(int count); + METRIC_DECLARE_INCREMENT_BY(committed_requests, ); // routine for get extra envs from replica const std::map &get_replica_extra_envs() const { return _extra_envs; } diff --git a/src/replica/replication_app_base.cpp b/src/replica/replication_app_base.cpp index 2d535b0657..05a86de355 100644 --- a/src/replica/replication_app_base.cpp +++ b/src/replica/replication_app_base.cpp @@ -485,7 +485,7 @@ error_code replication_app_base::apply_mutation(const mutation *mu) "mutation {} committed on {}, batched_count = {}", mu->name(), str, batched_count); } - _replica->update_commit_requests(batched_count); + METRIC_INCREMENT_BY(*_replica, committed_requests, batched_count); return ERR_OK; } diff --git a/src/utils/metrics.h b/src/utils/metrics.h index 64358e6c1e..9d026d077f 100644 --- a/src/utils/metrics.h +++ b/src/utils/metrics.h @@ -207,23 +207,34 @@ class error_code; #define METRIC_VAR_AUTO_COUNT(name, ...) \ dsn::auto_count __##name##_auto_count(METRIC_VAR_NAME(name), ##__VA_ARGS__) +#define METRIC_FUNC_NAME_INCREMENT_BY(name) increment_##name##_by + #define METRIC_DEFINE_INCREMENT_BY(name) \ - void increment_##name##_by(int64_t x) { METRIC_VAR_INCREMENT_BY(name, x); } + void METRIC_FUNC_NAME_INCREMENT_BY(name)(int64_t x) { METRIC_VAR_INCREMENT_BY(name, x); } // To be adaptive to self-defined `increment_by` methods, arguments are declared as variadic. -#define METRIC_INCREMENT_BY(obj, name, ...) (obj).increment_##name##_by(__VA_ARGS__) +#define METRIC_INCREMENT_BY(obj, name, ...) (obj).METRIC_FUNC_NAME_INCREMENT_BY(name)(__VA_ARGS__) + +#define METRIC_DECLARE_INCREMENT_BY(name, prefix) \ + void prefix METRIC_FUNC_NAME_INCREMENT_BY(name)(int64_t x) +#define METRIC_IMPL_INCREMENT_BY(obj, name, prefix) \ + METRIC_DECLARE_INCREMENT_BY(name, prefix) { METRIC_INCREMENT_BY(obj, name, x); } + +#define METRIC_FUNC_NAME_INCREMENT(name) increment_##name #define METRIC_DEFINE_INCREMENT(name) \ - void increment_##name() { METRIC_VAR_INCREMENT(name); } + void METRIC_FUNC_NAME_INCREMENT(name)() { METRIC_VAR_INCREMENT(name); } // To be adaptive to self-defined `increment` methods, arguments are declared as variadic. -#define METRIC_INCREMENT(obj, name, ...) (obj).increment_##name(__VA_ARGS__) +#define METRIC_INCREMENT(obj, name, ...) (obj).METRIC_FUNC_NAME_INCREMENT(name)(__VA_ARGS__) + +#define METRIC_FUNC_NAME_SET(name) set_##name #define METRIC_DEFINE_SET(name, value_type) \ - void set_##name(value_type value) { METRIC_VAR_SET(name, value); } + void METRIC_FUNC_NAME_SET(name)(value_type value) { METRIC_VAR_SET(name, value); } // To be adaptive to self-defined `set` methods, arguments are declared as variadic. -#define METRIC_SET(obj, name, ...) (obj).set_##name(__VA_ARGS__) +#define METRIC_SET(obj, name, ...) (obj).METRIC_FUNC_NAME_SET(name)(__VA_ARGS__) namespace dsn { class metric; // IWYU pragma: keep From c8856da4734ed53cbb1bb6dc29d85d25e093482c Mon Sep 17 00:00:00 2001 From: Dan Wang Date: Tue, 18 Apr 2023 19:42:34 +0800 Subject: [PATCH 5/7] feat(new_metrics): migrate server-level metrics for replica_stub (part 1) --- src/replica/replica.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/replica/replica.cpp b/src/replica/replica.cpp index 383774a5f5..d1e3443947 100644 --- a/src/replica/replica.cpp +++ b/src/replica/replica.cpp @@ -47,7 +47,6 @@ #include "mutation.h" #include "mutation_log.h" #include "perf_counter/perf_counter.h" -#include "perf_counter/perf_counter_wrapper.h" #include "perf_counter/perf_counters.h" #include "replica/prepare_list.h" #include "replica/replica_context.h" From a25f7b28d647fccaa74710f8751d3b3dcb0ce652 Mon Sep 17 00:00:00 2001 From: Dan Wang Date: Wed, 19 Apr 2023 00:54:04 +0800 Subject: [PATCH 6/7] feat(new_metrics): migrate server-level metrics for replica_stub (part 1) --- src/replica/replication_app_base.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/replica/replication_app_base.cpp b/src/replica/replication_app_base.cpp index 05a86de355..8d6a7dba77 100644 --- a/src/replica/replication_app_base.cpp +++ b/src/replica/replication_app_base.cpp @@ -61,6 +61,7 @@ #include "utils/filesystem.h" #include "utils/fmt_logging.h" #include "utils/latency_tracer.h" +#include "utils/metrics.h" #include "utils/ports.h" #include "utils/string_view.h" #include "utils/threadpool_code.h" From 60a2a41084ead6afc737630b604b36c25d762e3f Mon Sep 17 00:00:00 2001 From: Dan Wang Date: Wed, 19 Apr 2023 16:57:36 +0800 Subject: [PATCH 7/7] feat(new_metrics): migrate server-level metrics for replica_stub (part 1) --- src/replica/replica.cpp | 5 ----- src/replica/replica.h | 5 ----- src/replica/replica_stub.cpp | 8 +------- src/replica/replica_stub.h | 3 --- src/replica/replication_app_base.cpp | 11 ++++++++--- src/replica/replication_app_base.h | 4 ++++ src/utils/metrics.h | 5 ----- 7 files changed, 13 insertions(+), 28 deletions(-) diff --git a/src/replica/replica.cpp b/src/replica/replica.cpp index d1e3443947..aceb4770ae 100644 --- a/src/replica/replica.cpp +++ b/src/replica/replica.cpp @@ -225,11 +225,6 @@ void replica::update_last_checkpoint_generate_time() _last_checkpoint_generate_time_ms + rand::next_u64(max_interval_ms / 2, max_interval_ms); } -// // -// Statistics // -// // -METRIC_IMPL_INCREMENT_BY(*_stub, committed_requests, replica::) - void replica::init_state() { _inactive_is_transient = false; diff --git a/src/replica/replica.h b/src/replica/replica.h index e26d816905..ae00a7d195 100644 --- a/src/replica/replica.h +++ b/src/replica/replica.h @@ -281,11 +281,6 @@ class replica : public serverlet, public ref_counter, public replica_ba replica_follower *get_replica_follower() const { return _replica_follower.get(); }; - // - // Statistics - // - METRIC_DECLARE_INCREMENT_BY(committed_requests, ); - // routine for get extra envs from replica const std::map &get_replica_extra_envs() const { return _extra_envs; } diff --git a/src/replica/replica_stub.cpp b/src/replica/replica_stub.cpp index 04dee0c432..c9d1c8ad5b 100644 --- a/src/replica/replica_stub.cpp +++ b/src/replica/replica_stub.cpp @@ -110,11 +110,6 @@ METRIC_DEFINE_gauge_int64(server, dsn::metric_unit::kReplicas, "The number of closing replicas"); -METRIC_DEFINE_counter(server, - committed_requests, - dsn::metric_unit::kRequests, - "The number of committed requests"); - namespace dsn { namespace replication { @@ -220,8 +215,7 @@ replica_stub::replica_stub(replica_state_subscriber subscriber /*= nullptr*/, _is_running(false), METRIC_VAR_INIT_server(total_replicas), METRIC_VAR_INIT_server(opening_replicas), - METRIC_VAR_INIT_server(closing_replicas), - METRIC_VAR_INIT_server(committed_requests) + METRIC_VAR_INIT_server(closing_replicas) { #ifdef DSN_ENABLE_GPERF _is_releasing_memory = false; diff --git a/src/replica/replica_stub.h b/src/replica/replica_stub.h index b8561e1ca3..bae225ef36 100644 --- a/src/replica/replica_stub.h +++ b/src/replica/replica_stub.h @@ -366,8 +366,6 @@ class replica_stub : public serverlet, public ref_counter // Wait all replicas in closing state to be finished. void wait_closing_replicas_finished(); - METRIC_DEFINE_INCREMENT_BY(committed_requests) - private: friend class ::dsn::replication::test::test_checker; friend class ::dsn::replication::replica; @@ -473,7 +471,6 @@ class replica_stub : public serverlet, public ref_counter METRIC_VAR_DECLARE_gauge_int64(total_replicas); METRIC_VAR_DECLARE_gauge_int64(opening_replicas); METRIC_VAR_DECLARE_gauge_int64(closing_replicas); - METRIC_VAR_DECLARE_counter(committed_requests); perf_counter_wrapper _counter_replicas_learning_count; perf_counter_wrapper _counter_replicas_learning_max_duration_time_ms; diff --git a/src/replica/replication_app_base.cpp b/src/replica/replication_app_base.cpp index 8d6a7dba77..2cc6b4bb9e 100644 --- a/src/replica/replication_app_base.cpp +++ b/src/replica/replication_app_base.cpp @@ -61,12 +61,16 @@ #include "utils/filesystem.h" #include "utils/fmt_logging.h" #include "utils/latency_tracer.h" -#include "utils/metrics.h" #include "utils/ports.h" #include "utils/string_view.h" #include "utils/threadpool_code.h" #include "utils/utils.h" +METRIC_DEFINE_counter(replica, + committed_requests, + dsn::metric_unit::kRequests, + "The number of committed requests"); + namespace dsn { class disk_file; @@ -247,7 +251,8 @@ replication_app_base *replication_app_base::new_storage_instance(const std::stri return utils::factory_store::create(name.c_str(), PROVIDER_TYPE_MAIN, r); } -replication_app_base::replication_app_base(replica *replica) : replica_base(replica) +replication_app_base::replication_app_base(replica *replica) + : replica_base(replica), METRIC_VAR_INIT_replica(committed_requests) { _dir_data = utils::filesystem::path_combine(replica->dir(), "data"); _dir_learn = utils::filesystem::path_combine(replica->dir(), "learn"); @@ -486,7 +491,7 @@ error_code replication_app_base::apply_mutation(const mutation *mu) "mutation {} committed on {}, batched_count = {}", mu->name(), str, batched_count); } - METRIC_INCREMENT_BY(*_replica, committed_requests, batched_count); + METRIC_VAR_INCREMENT_BY(committed_requests, batched_count); return ERR_OK; } diff --git a/src/replica/replication_app_base.h b/src/replica/replication_app_base.h index 66ac6fb9b2..cfd07b235d 100644 --- a/src/replica/replication_app_base.h +++ b/src/replica/replication_app_base.h @@ -39,6 +39,7 @@ #include "replica/replica_base.h" #include "replica_admin_types.h" #include "utils/error_code.h" +#include "utils/metrics.h" #include "utils/ports.h" namespace dsn { @@ -313,6 +314,9 @@ class replication_app_base : public replica_base replica_init_info _info; explicit replication_app_base(replication::replica *replica); + +private: + METRIC_VAR_DECLARE_counter(committed_requests); }; } // namespace replication diff --git a/src/utils/metrics.h b/src/utils/metrics.h index 9d026d077f..f9ab9c2dc1 100644 --- a/src/utils/metrics.h +++ b/src/utils/metrics.h @@ -215,11 +215,6 @@ class error_code; // To be adaptive to self-defined `increment_by` methods, arguments are declared as variadic. #define METRIC_INCREMENT_BY(obj, name, ...) (obj).METRIC_FUNC_NAME_INCREMENT_BY(name)(__VA_ARGS__) -#define METRIC_DECLARE_INCREMENT_BY(name, prefix) \ - void prefix METRIC_FUNC_NAME_INCREMENT_BY(name)(int64_t x) -#define METRIC_IMPL_INCREMENT_BY(obj, name, prefix) \ - METRIC_DECLARE_INCREMENT_BY(name, prefix) { METRIC_INCREMENT_BY(obj, name, x); } - #define METRIC_FUNC_NAME_INCREMENT(name) increment_##name #define METRIC_DEFINE_INCREMENT(name) \