Skip to content

Commit

Permalink
add metric for last voted view (#3935)
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-maron authored Dec 2, 2024
1 parent 78ce7cb commit 86ef57b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crates/hotshot/src/tasks/task_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> CreateTaskState
async fn create_from(handle: &SystemContextHandle<TYPES, I, V>) -> Self {
let consensus = handle.hotshot.consensus();

// Clone the consensus metrics
let consensus_metrics = Arc::clone(&consensus.read().await.metrics);

Self {
public_key: handle.public_key().clone(),
private_key: handle.private_key().clone(),
Expand All @@ -237,6 +240,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> CreateTaskState
storage: Arc::clone(&handle.storage),
upgrade_lock: handle.hotshot.upgrade_lock.clone(),
epoch_height: handle.hotshot.config.epoch_height,
consensus_metrics,
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion crates/task-impls/src/quorum_vote/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use hotshot_task::{
task::TaskState,
};
use hotshot_types::{
consensus::OuterConsensus,
consensus::{ConsensusMetricsValue, OuterConsensus},
data::{Leaf2, QuorumProposal2},
drb::DrbResult,
event::Event,
Expand Down Expand Up @@ -80,6 +80,8 @@ pub struct VoteDependencyHandle<TYPES: NodeType, I: NodeImplementation<TYPES>, V
pub receiver: InactiveReceiver<Arc<HotShotEvent<TYPES>>>,
/// Lock for a decided upgrade
pub upgrade_lock: UpgradeLock<TYPES, V>,
/// The consensus metrics
pub consensus_metrics: Arc<ConsensusMetricsValue>,
/// The node's id
pub id: u64,
/// Number of blocks in an epoch, zero means there are no epochs
Expand Down Expand Up @@ -282,6 +284,9 @@ pub struct QuorumVoteTaskState<TYPES: NodeType, I: NodeImplementation<TYPES>, V:
/// The node's id
pub id: u64,

/// The consensus metrics
pub consensus_metrics: Arc<ConsensusMetricsValue>,

/// Reference to the storage.
pub storage: Arc<RwLock<I::Storage>>,

Expand Down Expand Up @@ -386,6 +391,7 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> QuorumVoteTaskS
upgrade_lock: self.upgrade_lock.clone(),
id: self.id,
epoch_height: self.epoch_height,
consensus_metrics: Arc::clone(&self.consensus_metrics),
},
);
self.vote_dependencies
Expand All @@ -410,6 +416,15 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> QuorumVoteTaskS
}
}

// Update the metric for the last voted view
if let Ok(last_voted_view_usize) = usize::try_from(*new_view) {
self.consensus_metrics
.last_voted_view
.set(last_voted_view_usize);
} else {
tracing::warn!("Failed to convert last voted view to a usize: {}", new_view);
}

self.latest_voted_view = new_view;

return true;
Expand Down
1 change: 1 addition & 0 deletions crates/testing/tests/tests_1/vote_dependency_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ async fn test_vote_dependency_handle() {
public_key: handle.public_key(),
private_key: handle.private_key().clone(),
consensus: OuterConsensus::new(consensus.clone()),
consensus_metrics: Arc::clone(&consensus.read().await.metrics),
instance_state: handle.hotshot.instance_state(),
quorum_membership: (*handle.hotshot.memberships).clone().into(),
storage: Arc::clone(&handle.storage()),
Expand Down
3 changes: 3 additions & 0 deletions crates/types/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ pub struct ConsensusMetricsValue {
pub last_synced_block_height: Box<dyn Gauge>,
/// The number of last decided view
pub last_decided_view: Box<dyn Gauge>,
/// The number of the last voted view
pub last_voted_view: Box<dyn Gauge>,
/// Number of timestamp for the last decided time
pub last_decided_time: Box<dyn Gauge>,
/// The current view
Expand Down Expand Up @@ -365,6 +367,7 @@ impl ConsensusMetricsValue {
last_synced_block_height: metrics
.create_gauge(String::from("last_synced_block_height"), None),
last_decided_view: metrics.create_gauge(String::from("last_decided_view"), None),
last_voted_view: metrics.create_gauge(String::from("last_voted_view"), None),
last_decided_time: metrics.create_gauge(String::from("last_decided_time"), None),
current_view: metrics.create_gauge(String::from("current_view"), None),
number_of_views_since_last_decide: metrics
Expand Down

0 comments on commit 86ef57b

Please sign in to comment.