Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Add block construction metrics to validation #1097

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ macro_rules! new_full {
validation_service_handle,
slot_duration,
backend,
service.prometheus_registry().as_ref(),
);

let select_chain = service.select_chain().ok_or(ServiceError::SelectChainRequired)?;
Expand Down
1 change: 1 addition & 0 deletions validation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ bitvec = { version = "0.17.4", default-features = false, features = ["alloc"] }
runtime_babe = { package = "pallet-babe", git = "https://github.com/paritytech/substrate", branch = "master" }
babe-primitives = { package = "sp-consensus-babe", git = "https://github.com/paritytech/substrate", branch = "master" }
keystore = { package = "sc-keystore", git = "https://github.com/paritytech/substrate", branch = "master" }
prometheus-endpoint = { package = "substrate-prometheus-endpoint", git = "https://github.com/paritytech/substrate", branch = "master" }

[dev-dependencies]
sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "master" }
32 changes: 24 additions & 8 deletions validation/src/block_production.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ use inherents::InherentData;
use sp_timestamp::TimestampInherentData;
use log::{info, debug, warn, trace};
use sp_api::{ApiExt, ProvideRuntimeApi};
use prometheus_endpoint::Registry as PrometheusRegistry;

use crate::validation_service::ServiceHandle;
use crate::dynamic_inclusion::DynamicInclusion;
use crate::Error;
use crate::{
Error,
dynamic_inclusion::DynamicInclusion,
metrics::MetricsLink as PrometheusMetrics,
validation_service::ServiceHandle,
};

// block size limit.
pub(crate) const MAX_TRANSACTIONS_SIZE: usize = 4 * 1024 * 1024;
Expand All @@ -57,6 +61,7 @@ pub struct ProposerFactory<Client, TxPool, Backend> {
service_handle: ServiceHandle,
babe_slot_duration: u64,
backend: Arc<Backend>,
metrics: PrometheusMetrics,
}

impl<Client, TxPool, Backend> ProposerFactory<Client, TxPool, Backend> {
Expand All @@ -67,13 +72,15 @@ impl<Client, TxPool, Backend> ProposerFactory<Client, TxPool, Backend> {
service_handle: ServiceHandle,
babe_slot_duration: u64,
backend: Arc<Backend>,
prometheus: Option<&PrometheusRegistry>,
) -> Self {
ProposerFactory {
client,
transaction_pool,
service_handle: service_handle,
babe_slot_duration,
backend,
metrics: PrometheusMetrics::new(prometheus),
}
}
}
Expand Down Expand Up @@ -109,6 +116,7 @@ where
let transaction_pool = self.transaction_pool.clone();
let backend = self.backend.clone();
let slot_duration = self.babe_slot_duration.clone();
let metrics = self.metrics.clone();

let maybe_proposer = self.service_handle
.clone()
Expand All @@ -120,6 +128,7 @@ where
transaction_pool,
slot_duration,
backend,
metrics,
})));

Box::pin(maybe_proposer)
Expand All @@ -134,6 +143,7 @@ pub struct Proposer<Client, TxPool, Backend> {
transaction_pool: Arc<TxPool>,
slot_duration: u64,
backend: Arc<Backend>,
metrics: PrometheusMetrics,
}

impl<Client, TxPool, Backend> consensus::Proposer<Block> for Proposer<Client, TxPool, Backend> where
Expand Down Expand Up @@ -175,6 +185,7 @@ impl<Client, TxPool, Backend> consensus::Proposer<Block> for Proposer<Client, Tx
let transaction_pool = self.transaction_pool.clone();
let table = self.tracker.table().clone();
let backend = self.backend.clone();
let metrics = self.metrics.clone();

async move {
let enough_candidates = dynamic_inclusion.acceptable_in(
Expand Down Expand Up @@ -214,11 +225,16 @@ impl<Client, TxPool, Backend> consensus::Proposer<Block> for Proposer<Client, Tx

Delay::new(enough_candidates).await;

tokio::task::spawn_blocking(move || {
let proposed_candidates = table.proposed_set();
data.propose_with(proposed_candidates)
})
.await?
let result = tokio::task::spawn_blocking(
move || {
let proposed_candidates = table.proposed_set();
data.propose_with(proposed_candidates)
}
).await?;

metrics.report(move |metrics| metrics.block_constructed.observe(now.elapsed().as_secs_f64()));

result
}.boxed()
}
}
Expand Down
1 change: 1 addition & 0 deletions validation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub use parachain::wasm_executor::run_worker as run_validation_worker;
mod dynamic_inclusion;
mod error;
mod shared_table;
mod metrics;

pub mod block_production;
pub mod collation;
Expand Down
62 changes: 62 additions & 0 deletions validation/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Various validation metrics.

use std::sync::Arc;

use prometheus_endpoint::{register, PrometheusError, Registry, Histogram, HistogramOpts};

/// Optional shareable link to basic authorship metrics.
#[derive(Clone, Default)]
pub struct MetricsLink(Arc<Option<Metrics>>);

impl MetricsLink {
pub fn new(registry: Option<&Registry>) -> Self {
Self(Arc::new(
registry.and_then(|registry|
Metrics::register(registry)
.map_err(|err| { log::warn!("Failed to register prometheus metrics: {}", err); })
.ok()
)
))
}

pub fn report(&self, do_this: impl FnOnce(&Metrics)) {
if let Some(metrics) = self.0.as_ref() {
do_this(metrics);
}
}
}

/// Authorship metrics.
pub struct Metrics {
pub block_constructed: Histogram,
}

impl Metrics {
pub fn register(registry: &Registry) -> Result<Self, PrometheusError> {
Ok(Self {
block_constructed: register(
Histogram::with_opts(HistogramOpts::new(
"polkadot_proposer_block_constructed",
"Histogram of time taken to construct new block",
))?,
registry,
)?,
})
}
}