Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(code): Feature-gate metrics in core-consensus crate #762

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions code/crates/core-consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ readme = "../../../README.md"
all-features = true

[features]
std = []
std = ["malachitebft-metrics"]
debug = ["std", "malachitebft-core-driver/debug"]

[dependencies]
malachitebft-core-types.workspace = true
malachitebft-core-driver.workspace = true
malachitebft-metrics.workspace = true
malachitebft-metrics = { workspace = true, optional = true }
malachitebft-peer.workspace = true

async-recursion = { workspace = true }
Expand Down
4 changes: 3 additions & 1 deletion code/crates/core-consensus/src/handle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::prelude::*;

#[cfg(not(feature = "std"))]
use crate::types::Metrics;
mod decide;
mod driver;
mod proposal;
Expand All @@ -23,6 +24,7 @@ use timeout::on_timeout_elapsed;
use vote::on_vote;
use vote_set::{on_vote_set_request, on_vote_set_response};

#[allow(unused_variables)]
#[allow(private_interfaces)]
pub async fn handle<Ctx>(
co: Co<Ctx>,
Expand Down
4 changes: 4 additions & 0 deletions code/crates/core-consensus/src/handle/decide.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;

#[allow(unused_variables)]
pub async fn decide<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
Expand All @@ -21,6 +24,7 @@ where
state.remove_full_proposals(height);

// Update metrics
#[cfg(feature = "std")]
{
// We are only interested in consensus time for round 0, ie. in the happy path.
if consensus_round == Round::new(0) {
Expand Down
12 changes: 8 additions & 4 deletions code/crates/core-consensus/src/handle/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use crate::handle::signature::sign_proposal;
use crate::handle::signature::sign_vote;
use crate::handle::vote::on_vote;
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;
use crate::types::SignedConsensusMsg;
use crate::util::pretty::PrettyVal;
use malachitebft_core_driver::Input as DriverInput;
use malachitebft_core_driver::Output as DriverOutput;

#[async_recursion]
pub async fn apply_driver_input<Ctx>(
co: &Co<Ctx>,
Expand All @@ -20,6 +21,7 @@ where
{
match &input {
DriverInput::NewRound(height, round, proposer) => {
#[cfg(feature = "std")]
metrics.round.set(round.as_i64());

info!(%height, %round, %proposer, "Starting new round");
Expand Down Expand Up @@ -103,8 +105,11 @@ where
);
}
}
metrics.step_end(prev_step);
metrics.step_start(new_step);
#[cfg(feature = "std")]
{
metrics.step_end(prev_step);
metrics.step_start(new_step);
}
}

if prev_step != new_step {
Expand Down Expand Up @@ -177,7 +182,6 @@ where
match output {
DriverOutput::NewRound(height, round) => {
let proposer = state.get_proposer(height, round);

apply_driver_input(
co,
state,
Expand Down
3 changes: 2 additions & 1 deletion code/crates/core-consensus/src/handle/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use crate::handle::signature::verify_signature;
use crate::handle::validator_set::get_validator_set;
use crate::input::Input;
use crate::types::ConsensusMsg;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;
use crate::util::pretty::PrettyProposal;
use crate::ProposedValue;
use crate::{prelude::*, SignedConsensusMsg};

pub async fn on_proposal<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
Expand Down
4 changes: 3 additions & 1 deletion code/crates/core-consensus/src/handle/propose.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;

use crate::handle::driver::apply_driver_input;
use crate::types::{ProposedValue, ValueToPropose};
Expand Down Expand Up @@ -37,7 +39,7 @@ where

return Ok(());
}

#[cfg(feature = "std")]
metrics.consensus_start();

state.store_value(&ProposedValue {
Expand Down
2 changes: 2 additions & 0 deletions code/crates/core-consensus/src/handle/proposed_value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;

use crate::handle::driver::apply_driver_input;
use crate::types::ProposedValue;
Expand Down
15 changes: 9 additions & 6 deletions code/crates/core-consensus/src/handle/start_height.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;

use crate::handle::driver::apply_driver_input;
use crate::handle::handle_input;
Expand All @@ -15,7 +17,7 @@ where
{
perform!(co, Effect::CancelAllTimeouts(Default::default()));
perform!(co, Effect::ResetTimeouts(Default::default()));

#[cfg(feature = "std")]
metrics.step_end(state.driver.step());

state.driver.move_to_height(height, validator_set);
Expand All @@ -37,11 +39,12 @@ where
{
let round = Round::new(0);
info!(%height, "Starting new height");

metrics.block_start();
metrics.height.set(height.as_u64() as i64);
metrics.round.set(round.as_i64());

#[cfg(feature = "std")]
{
metrics.block_start();
metrics.height.set(height.as_u64() as i64);
metrics.round.set(round.as_i64());
}
let proposer = state.get_proposer(height, round);

apply_driver_input(
Expand Down
4 changes: 4 additions & 0 deletions code/crates/core-consensus/src/handle/step_timeout.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;

#[allow(unused_variables)]
pub async fn on_step_limit_timeout<Ctx>(
co: &Co<Ctx>,
state: &mut State<Ctx>,
Expand All @@ -17,6 +20,7 @@ where
co,
Effect::GetVoteSet(state.driver.height(), round, Default::default())
);
#[cfg(feature = "std")]
metrics.step_timeouts.inc();

if state.driver.step_is_prevote() {
Expand Down
3 changes: 2 additions & 1 deletion code/crates/core-consensus/src/handle/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::handle::driver::apply_driver_input;
use crate::handle::signature::verify_certificate;
use crate::handle::validator_set::get_validator_set;
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;

pub async fn on_commit_certificate<Ctx>(
co: &Co<Ctx>,
Expand Down Expand Up @@ -33,7 +35,6 @@ where
return Err(Error::InvalidCertificate(certificate, e));
}

// Go to Commit step via L49
apply_driver_input(
co,
state,
Expand Down
2 changes: 2 additions & 0 deletions code/crates/core-consensus/src/handle/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::handle::decide::decide;
use crate::handle::driver::apply_driver_input;
use crate::handle::step_timeout::on_step_limit_timeout;
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;

pub async fn on_timeout_elapsed<Ctx>(
co: &Co<Ctx>,
Expand Down
2 changes: 2 additions & 0 deletions code/crates/core-consensus/src/handle/vote.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(not(feature = "std"))]
use crate::types::Metrics;
use crate::{prelude::*, SignedConsensusMsg};

use crate::handle::driver::apply_driver_input;
Expand Down
2 changes: 2 additions & 0 deletions code/crates/core-consensus/src/handle/vote_set.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::handle::vote::on_vote;
use crate::input::RequestId;
use crate::prelude::*;
#[cfg(not(feature = "std"))]
use crate::types::Metrics;

pub async fn on_vote_set_request<Ctx>(
co: &Co<Ctx>,
Expand Down
1 change: 0 additions & 1 deletion code/crates/core-consensus/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
macro_rules! process {
(input: $input:expr, state: $state:expr, metrics: $metrics:expr, with: $effect:ident => $handle:expr) => {{
let mut gen = $crate::gen::Gen::new(|co| $crate::handle(co, $state, $metrics, $input));

let mut co_result = gen.resume_with($crate::Resume::Start);

loop {
Expand Down
1 change: 1 addition & 0 deletions code/crates/core-consensus/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub use tracing::{debug, info, warn};

pub use malachitebft_core_driver::Input as DriverInput;
pub use malachitebft_core_types::*;
#[cfg(feature = "std")]
pub use malachitebft_metrics::Metrics;

pub use crate::effect::{Effect, Resume};
Expand Down
4 changes: 3 additions & 1 deletion code/crates/core-consensus/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use malachitebft_core_types::{

pub use malachitebft_peer::PeerId;
pub use multiaddr::Multiaddr;

// Dummy metrics structure
#[cfg(not(feature = "std"))]
pub type Metrics = ();
/// A signed consensus message, ie. a signed vote or a signed proposal.
#[derive_where(Clone, Debug, PartialEq, Eq)]
pub enum SignedConsensusMsg<Ctx: Context> {
Expand Down
Loading