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

Commit

Permalink
Node-side metrics for asynchronous backing (#6549)
Browse files Browse the repository at this point in the history
* Add metrics for `prune_view_candidate_storage`

* Add metrics for `request_unblocked_collations`

* Fix docstring

* Couple fixes from review comments
  • Loading branch information
mrcnski committed Jan 30, 2023
1 parent 11556a6 commit 1c59180
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 22 deletions.
41 changes: 23 additions & 18 deletions node/core/prospective-parachains/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Parity Technologies (UK) Ltd.
// Copyright 2022-2023 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -61,6 +61,9 @@ mod fragment_tree;
#[cfg(test)]
mod tests;

mod metrics;
use self::metrics::Metrics;

const LOG_TARGET: &str = "parachain::prospective-parachains";

struct RelayBlockViewData {
Expand All @@ -82,12 +85,14 @@ impl View {

/// The prospective parachains subsystem.
#[derive(Default)]
pub struct ProspectiveParachainsSubsystem;
pub struct ProspectiveParachainsSubsystem {
metrics: Metrics,
}

impl ProspectiveParachainsSubsystem {
/// Create a new instance of the `ProspectiveParachainsSubsystem`.
pub fn new() -> Self {
Self
pub fn new(metrics: Metrics) -> Self {
Self { metrics }
}
}

Expand All @@ -98,7 +103,7 @@ where
{
fn start(self, ctx: Context) -> SpawnedSubsystem {
SpawnedSubsystem {
future: run(ctx)
future: run(ctx, self.metrics)
.map_err(|e| SubsystemError::with_origin("prospective-parachains", e))
.boxed(),
name: "prospective-parachains-subsystem",
Expand All @@ -107,23 +112,27 @@ where
}

#[overseer::contextbounds(ProspectiveParachains, prefix = self::overseer)]
async fn run<Context>(mut ctx: Context) -> FatalResult<()> {
async fn run<Context>(mut ctx: Context, metrics: Metrics) -> FatalResult<()> {
let mut view = View::new();
loop {
crate::error::log_error(
run_iteration(&mut ctx, &mut view).await,
run_iteration(&mut ctx, &mut view, &metrics).await,
"Encountered issue during run iteration",
)?;
}
}

#[overseer::contextbounds(ProspectiveParachains, prefix = self::overseer)]
async fn run_iteration<Context>(ctx: &mut Context, view: &mut View) -> Result<()> {
async fn run_iteration<Context>(
ctx: &mut Context,
view: &mut View,
metrics: &Metrics,
) -> Result<()> {
loop {
match ctx.recv().await.map_err(FatalError::SubsystemReceive)? {
FromOrchestra::Signal(OverseerSignal::Conclude) => return Ok(()),
FromOrchestra::Signal(OverseerSignal::ActiveLeaves(update)) => {
handle_active_leaves_update(&mut *ctx, view, update).await?;
handle_active_leaves_update(&mut *ctx, view, update, metrics).await?;
},
FromOrchestra::Signal(OverseerSignal::BlockFinalized(..)) => {},
FromOrchestra::Communication { msg } => match msg {
Expand Down Expand Up @@ -155,6 +164,7 @@ async fn handle_active_leaves_update<Context>(
ctx: &mut Context,
view: &mut View,
update: ActiveLeavesUpdate,
metrics: &Metrics,
) -> JfyiErrorResult<()> {
// 1. clean up inactive leaves
// 2. determine all scheduled para at new block
Expand Down Expand Up @@ -245,13 +255,15 @@ async fn handle_active_leaves_update<Context>(

if !update.deactivated.is_empty() {
// This has potential to be a hotspot.
prune_view_candidate_storage(view);
prune_view_candidate_storage(view, metrics);
}

Ok(())
}

fn prune_view_candidate_storage(view: &mut View) {
fn prune_view_candidate_storage(view: &mut View, metrics: &Metrics) {
metrics.time_prune_view_candidate_storage();

let active_leaves = &view.active_leaves;
view.candidate_storage.retain(|para_id, storage| {
let mut coverage = HashSet::new();
Expand Down Expand Up @@ -721,10 +733,3 @@ async fn fetch_block_info<Context>(
storage_root: header.state_root,
}))
}

#[derive(Clone)]
struct MetricsInner;

/// Prospective parachain metrics.
#[derive(Default, Clone)]
pub struct Metrics(Option<MetricsInner>);
52 changes: 52 additions & 0 deletions node/core/prospective-parachains/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2023 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot 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.

// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.

use polkadot_node_subsystem_util::metrics::{self, prometheus};

#[derive(Clone)]
pub(crate) struct MetricsInner {
pub(crate) prune_view_candidate_storage: prometheus::Histogram,
}

/// Candidate backing metrics.
#[derive(Default, Clone)]
pub struct Metrics(pub(crate) Option<MetricsInner>);

impl Metrics {
/// Provide a timer for handling `prune_view_candidate_storage` which observes on drop.
pub fn time_prune_view_candidate_storage(
&self,
) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
self.0
.as_ref()
.map(|metrics| metrics.prune_view_candidate_storage.start_timer())
}
}

impl metrics::Metrics for Metrics {
fn try_register(registry: &prometheus::Registry) -> Result<Self, prometheus::PrometheusError> {
let metrics = MetricsInner {
prune_view_candidate_storage: prometheus::register(
prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
"polkadot_parachain_prospective_parachains_prune_view_candidate_storage",
"Time spent within `prospective_parachains::prune_view_candidate_storage`",
))?,
registry,
)?,
};
Ok(Metrics(Some(metrics)))
}
}
21 changes: 20 additions & 1 deletion node/network/collator-protocol/src/validator_side/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// Copyright 2017-2023 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -56,6 +56,15 @@ impl Metrics {
) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
self.0.as_ref().map(|metrics| metrics.collation_request_duration.start_timer())
}

/// Provide a timer for `request_unblocked_collations` which observes on drop.
pub fn time_request_unblocked_collations(
&self,
) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
self.0
.as_ref()
.map(|metrics| metrics.request_unblocked_collations.start_timer())
}
}

#[derive(Clone)]
Expand All @@ -65,6 +74,7 @@ struct MetricsInner {
handle_collation_request_result: prometheus::Histogram,
collator_peer_count: prometheus::Gauge<prometheus::U64>,
collation_request_duration: prometheus::Histogram,
request_unblocked_collations: prometheus::Histogram,
}

impl metrics::Metrics for Metrics {
Expand Down Expand Up @@ -116,6 +126,15 @@ impl metrics::Metrics for Metrics {
)?,
registry,
)?,
request_unblocked_collations: prometheus::register(
prometheus::Histogram::with_opts(
prometheus::HistogramOpts::new(
"polkadot_parachain_collator_protocol_validator_request_unblocked_collations",
"Time spent within `collator_protocol_validator::request_unblocked_collations`",
)
)?,
registry,
)?,
};

Ok(Metrics(Some(metrics)))
Expand Down
4 changes: 3 additions & 1 deletion node/network/collator-protocol/src/validator_side/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// Copyright 2020-2023 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -979,6 +979,8 @@ where
Sender: CollatorProtocolSenderTrait,
I: IntoIterator<Item = ((ParaId, Hash), Vec<BlockedAdvertisement>)>,
{
let _timer = state.metrics.time_request_unblocked_collations();

for (key, mut value) in blocked {
let (para_id, para_head) = key;
let blocked = std::mem::take(&mut value);
Expand Down
4 changes: 2 additions & 2 deletions node/service/src/overseer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// Copyright 2017-2023 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -322,7 +322,7 @@ where
Metrics::register(registry)?,
))
.chain_selection(ChainSelectionSubsystem::new(chain_selection_config, parachains_db))
.prospective_parachains(ProspectiveParachainsSubsystem::new())
.prospective_parachains(ProspectiveParachainsSubsystem::new(Metrics::register(registry)?))
.leaves(Vec::from_iter(
leaves
.into_iter()
Expand Down

0 comments on commit 1c59180

Please sign in to comment.