From 2ae7fa498b74fcff8e515978efa15b25fc378256 Mon Sep 17 00:00:00 2001 From: Boris Erakhtin Date: Thu, 31 Oct 2024 11:49:59 +0700 Subject: [PATCH] Candidate metrics added, but it does not build --- core/consensus/babe/impl/babe.cpp | 73 ++++++++++++++++++++++++-- core/consensus/babe/impl/babe.hpp | 15 +++++- test/core/consensus/babe/babe_test.cpp | 10 ++-- 3 files changed, 90 insertions(+), 8 deletions(-) diff --git a/core/consensus/babe/impl/babe.cpp b/core/consensus/babe/impl/babe.cpp index 93f5413532..ce05f8a4f1 100644 --- a/core/consensus/babe/impl/babe.cpp +++ b/core/consensus/babe/impl/babe.cpp @@ -58,6 +58,23 @@ namespace { constexpr const char *kIsRelayChainValidator = "kagome_node_is_active_validator"; + constexpr const char *kBackedCandidatesInBlock = + "kagome_node_backed_candidates_in_block"; + + constexpr const char *kIncludeCandidatesInBlock = + "kagome_node_include_candidates_in_block"; + + constexpr const char *kNoBackedCandidatesInBlock = + "kagome_node_no_backed_candidates_in_block"; + + constexpr const char *kNoIncludeCandidatesInBlock = + "kagome_node_no_include_candidates_in_block"; + + constexpr const char *kNoBackedNoIncludeCandidatesInBlock = + "kagome_node_no_backed_no_include_candidates_in_block"; + + constexpr const char *kTotalBlocks = "kagome_node_total_blocks"; + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) kagome::metrics::HistogramTimer metric_block_proposal_time{ "kagome_proposer_block_constructed", @@ -149,6 +166,32 @@ namespace kagome::consensus::babe { metric_is_relaychain_validator_ = metrics_registry_->registerGaugeMetric(kIsRelayChainValidator); metric_is_relaychain_validator_->set(false); + + metrics_registry_->registerGaugeFamily( + kBackedCandidatesInBlock, "Number of backed candidates in the block"); + metric_backed_candidates_in_block_ = + metrics_registry_->registerGaugeMetric(kBackedCandidatesInBlock); + metric_backed_candidates_in_block_->set(0); + + metrics_registry_->registerGaugeFamily( + kIncludeCandidatesInBlock, + "Number of candidates included in the block"); + metric_include_candidates_in_block_ = + metrics_registry_->registerGaugeMetric(kIncludeCandidatesInBlock); + metric_include_candidates_in_block_->set(0); + + metric_no_backed_candidates_in_block_ = + metrics_registry_->registerCounterMetric(kNoBackedCandidatesInBlock); + + metric_no_include_candidates_in_block_ = + metrics_registry_->registerCounterMetric(kNoIncludeCandidatesInBlock); + + metric_no_backed_no_include_candidates_in_block_ = + metrics_registry_->registerCounterMetric( + kNoBackedNoIncludeCandidatesInBlock); + + metric_total_blocks_ = + metrics_registry_->registerCounterMetric(kTotalBlocks); } bool Babe::isGenesisConsensus() const { @@ -495,6 +538,11 @@ namespace kagome::consensus::babe { return BlockProductionError::CAN_NOT_PREPARE_BLOCK; } + CandidatesMetricInfo candidates{ + .backed = parachain_inherent_data.backed_candidates.size(), + .included = parachain_inherent_data.bitfields.size(), + }; + auto proposal_start = std::chrono::steady_clock::now(); auto pre_digest_res = makePreDigest(); @@ -512,7 +560,8 @@ namespace kagome::consensus::babe { proposal_start, pre_digest{std::move(pre_digest)}, slot = slot_, - parent = parent_]() mutable { + parent = parent_, + candidates]() mutable { auto self = wp.lock(); if (not self) { return; @@ -538,12 +587,14 @@ namespace kagome::consensus::babe { now, proposal_start, changes_tracker{std::move(changes_tracker)}, - unsealed_block{std::move(unsealed_block)}]() mutable { + unsealed_block{std::move(unsealed_block)}, + candidates]() mutable { auto res = self->processSlotLeadershipProposed(now, proposal_start, std::move(changes_tracker), - std::move(unsealed_block)); + std::move(unsealed_block), + candidates); if (res.has_error()) { SL_ERROR(self->log_, "Cannot propose a block: {}", res.error()); return; @@ -561,7 +612,8 @@ namespace kagome::consensus::babe { clock::SteadyClock::TimePoint proposal_start, std::shared_ptr &&changes_tracker, - primitives::Block &&block) { + primitives::Block &&block, + std::optional candidates_metrics) { auto duration_ms = metric_block_proposal_time.observe(proposal_start).count(); SL_DEBUG(log_, "Block has been built in {} ms", duration_ms); @@ -648,6 +700,19 @@ namespace kagome::consensus::babe { } } + if (candidates_metrics) { + if (candidates_metrics->backed == 0 and candidates_metrics->included == 0) { + metric_no_backed_no_include_candidates_in_block_->inc(); + } else if (candidates_metrics->backed == 0) { + metric_no_backed_candidates_in_block_->inc(); + } else if (candidates_metrics->included == 0) { + metric_no_include_candidates_in_block_->inc(); + } + metric_backed_candidates_in_block_->set(candidates_metrics->backed); + metric_include_candidates_in_block_->set(candidates_metrics->included); + } + metric_total_blocks_->inc(); + return outcome::success(); } diff --git a/core/consensus/babe/impl/babe.hpp b/core/consensus/babe/impl/babe.hpp index 9d49cb04c2..d424bccaa3 100644 --- a/core/consensus/babe/impl/babe.hpp +++ b/core/consensus/babe/impl/babe.hpp @@ -98,6 +98,11 @@ namespace kagome::consensus::babe { std::shared_ptr keypair; }; + struct CandidatesMetricInfo { + size_t backed; + size_t included; + }; + Babe( application::AppStateManager &app_state_manager, const application::AppConfiguration &app_config, @@ -167,7 +172,9 @@ namespace kagome::consensus::babe { clock::SteadyClock::TimePoint proposal_start, std::shared_ptr &&changes_tracker, - primitives::Block &&block); + primitives::Block &&block, + std::optional candidates_metrics = + std::nullopt) = 0; private: log::Logger log_; @@ -211,6 +218,12 @@ namespace kagome::consensus::babe { // Metrics metrics::RegistryPtr metrics_registry_ = metrics::createRegistry(); metrics::Gauge *metric_is_relaychain_validator_; + metrics::Gauge *metric_backed_candidates_in_block_; + metrics::Gauge *metric_include_candidates_in_block_; + metrics::Counter *metric_no_backed_candidates_in_block_; + metrics::Counter *metric_no_include_candidates_in_block_; + metrics::Counter *metric_no_backed_no_include_candidates_in_block_; + metrics::Counter *metric_total_blocks_; telemetry::Telemetry telemetry_; // telemetry }; diff --git a/test/core/consensus/babe/babe_test.cpp b/test/core/consensus/babe/babe_test.cpp index ba8b39baa2..f6c9860806 100644 --- a/test/core/consensus/babe/babe_test.cpp +++ b/test/core/consensus/babe/babe_test.cpp @@ -162,9 +162,13 @@ class BabeWrapper : public Babe { kagome::clock::SteadyClock::TimePoint proposal_start, std::shared_ptr &&changes_tracker, - kagome::primitives::Block &&block) override { - auto res = Babe::processSlotLeadershipProposed( - now, proposal_start, std::move(changes_tracker), std::move(block)); + kagome::primitives::Block &&block, + std::optional candidates_metrics) override { + auto res = Babe::processSlotLeadershipProposed(now, + proposal_start, + std::move(changes_tracker), + std::move(block), + candidates_metrics); if (on_proposed) { on_proposed(); }