diff --git a/massa-metrics/src/lib.rs b/massa-metrics/src/lib.rs index 90a949f9a2b..542428e019c 100644 --- a/massa-metrics/src/lib.rs +++ b/massa-metrics/src/lib.rs @@ -74,6 +74,10 @@ pub struct MassaMetrics { consensus_state_discarded_index: IntGauge, consensus_state_block_statuses: IntGauge, + // endorsement cache + endorsement_cache_checked_endorsements: IntGauge, + endorsement_cache_known_by_peer: IntGauge, + // blocks_counter: IntGauge, // endorsements_counter: IntGauge, // operations_counter: IntGauge, @@ -196,6 +200,18 @@ impl MassaMetrics { ) .unwrap(); + let endorsement_cache_checked_endorsements = IntGauge::new( + "endorsement_cache_checked_endorsements", + "endorsement cache checked endorsements size", + ) + .unwrap(); + + let endorsement_cache_known_by_peer = IntGauge::new( + "endorsement_cache_known_by_peer", + "endorsement cache know by peer size", + ) + .unwrap(); + if enabled { for i in 0..nb_thread { let gauge = Gauge::new( @@ -239,6 +255,9 @@ impl MassaMetrics { let _ = prometheus::register(Box::new( operation_cache_checked_operations_prefix.clone(), )); + let _ = + prometheus::register(Box::new(endorsement_cache_checked_endorsements.clone())); + let _ = prometheus::register(Box::new(endorsement_cache_known_by_peer.clone())); } } @@ -257,6 +276,8 @@ impl MassaMetrics { consensus_state_incoming_index, consensus_state_discarded_index, consensus_state_block_statuses, + endorsement_cache_checked_endorsements, + endorsement_cache_known_by_peer, // blocks_counter, // endorsements_counter, // operations_counter, @@ -353,6 +374,17 @@ impl MassaMetrics { self.operation_cache_ops_know_by_peer .set(ops_know_by_peer as i64); } + + pub fn set_endorsements_cache_metrics( + &self, + checked_endorsements: usize, + known_by_peer: usize, + ) { + self.endorsement_cache_checked_endorsements + .set(checked_endorsements as i64); + self.endorsement_cache_known_by_peer + .set(known_by_peer as i64); + } } // mod test { // use massa_channel::MassaChannel; diff --git a/massa-protocol-worker/src/connectivity.rs b/massa-protocol-worker/src/connectivity.rs index 008201683bc..21677095364 100644 --- a/massa-protocol-worker/src/connectivity.rs +++ b/massa-protocol-worker/src/connectivity.rs @@ -154,6 +154,7 @@ pub(crate) fn start_connectivity_thread( sender_endorsements_propagation_ext, protocol_channels.endorsement_handler_propagation.1.clone(), peer_management_handler.sender.command_sender.clone(), + massa_metrics.clone(), ); let mut block_handler = BlockHandler::new( network_controller.get_active_connections(), diff --git a/massa-protocol-worker/src/handlers/endorsement_handler/mod.rs b/massa-protocol-worker/src/handlers/endorsement_handler/mod.rs index 8d09bf4480f..e2ae65e6f17 100644 --- a/massa-protocol-worker/src/handlers/endorsement_handler/mod.rs +++ b/massa-protocol-worker/src/handlers/endorsement_handler/mod.rs @@ -1,6 +1,7 @@ use std::thread::JoinHandle; use massa_channel::{receiver::MassaReceiver, sender::MassaSender}; +use massa_metrics::MassaMetrics; use massa_pool_exports::PoolController; use massa_protocol_exports::ProtocolConfig; use massa_storage::Storage; @@ -49,6 +50,7 @@ impl EndorsementHandler { local_sender: MassaSender, local_receiver: MassaReceiver, sender_peer_cmd: MassaSender, + massa_metrics: MassaMetrics, ) -> Self { let endorsement_retrieval_thread = start_retrieval_thread( receiver, @@ -59,6 +61,7 @@ impl EndorsementHandler { pool_controller, config.clone(), storage.clone_without_refs(), + massa_metrics, ); let endorsement_propagation_thread = diff --git a/massa-protocol-worker/src/handlers/endorsement_handler/retrieval.rs b/massa-protocol-worker/src/handlers/endorsement_handler/retrieval.rs index 654946c618d..62cff41dfa9 100644 --- a/massa-protocol-worker/src/handlers/endorsement_handler/retrieval.rs +++ b/massa-protocol-worker/src/handlers/endorsement_handler/retrieval.rs @@ -1,8 +1,9 @@ -use std::thread::JoinHandle; +use std::{thread::JoinHandle, time::Duration}; -use crossbeam::select; +use crossbeam::{channel::tick, select}; use massa_channel::{receiver::MassaReceiver, sender::MassaSender}; use massa_logging::massa_trace; +use massa_metrics::MassaMetrics; use massa_models::{ endorsement::SecureShareEndorsement, prehash::{CapacityAllocator, PreHashMap, PreHashSet}, @@ -41,6 +42,7 @@ pub struct RetrievalThread { config: ProtocolConfig, storage: Storage, peer_cmd_sender: MassaSender, + metrics: MassaMetrics, } impl RetrievalThread { @@ -51,6 +53,8 @@ impl RetrievalThread { max_length_endorsements: self.config.max_endorsements_per_message, endorsement_count: self.config.endorsement_count, }); + let tick_metrics = tick(Duration::from_secs(5)); + loop { select! { recv(self.receiver) -> msg => { @@ -110,6 +114,17 @@ impl RetrievalThread { return; } } + }, + recv(tick_metrics) -> _ => { + // update metrics + let read = self.cache.read(); + let count = read + .endorsements_known_by_peer + .iter() + .map(|(_peer_id, map)| map.len()) + .sum(); + self.metrics + .set_endorsements_cache_metrics(read.checked_endorsements.len(), count); } } } @@ -261,6 +276,7 @@ pub fn start_retrieval_thread( pool_controller: Box, config: ProtocolConfig, storage: Storage, + metrics: MassaMetrics, ) -> JoinHandle<()> { std::thread::Builder::new() .name("protocol-endorsement-handler-retrieval".to_string()) @@ -274,6 +290,7 @@ pub fn start_retrieval_thread( pool_controller, config, storage, + metrics, }; retrieval_thread.run(); })