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

Commit a3902fb

Browse files
drahnrordian
andauthored
add dispute metrics, some chores (#3842)
* rename: MsgFilter -> MessageInterceptor * feat: add dispute metrics * fixup * test fixins * fix metrics * dummysubsystem export and trait fn fix * chore: fmt * undo unwanted changes * foo * pfmt * fixup * fixup * revert * some more * Update node/malus/Cargo.toml Co-authored-by: Andronik Ordian <[email protected]> * Update node/core/dispute-coordinator/src/metrics.rs Co-authored-by: Andronik Ordian <[email protected]> * Update node/core/dispute-coordinator/src/metrics.rs Co-authored-by: Andronik Ordian <[email protected]> * Update node/core/dispute-coordinator/src/metrics.rs Co-authored-by: Andronik Ordian <[email protected]> * add license header * fix lockfile * new with opts * fmt * Update node/core/dispute-coordinator/src/metrics.rs * feature gate Co-authored-by: Andronik Ordian <[email protected]>
1 parent 9dc2d42 commit a3902fb

File tree

14 files changed

+339
-54
lines changed

14 files changed

+339
-54
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/core/dispute-coordinator/src/dummy.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ use kvdb::KeyValueDB;
3030
use parity_scale_codec::{Decode, Encode, Error as CodecError};
3131
use sc_keystore::LocalKeystore;
3232

33+
use crate::metrics::Metrics;
34+
3335
const LOG_TARGET: &str = "parachain::dispute-coordinator";
3436

3537
/// Timestamp based on the 1 Jan 1970 UNIX base, which is persistent across node restarts and OS reboots.
@@ -52,7 +54,7 @@ pub struct DisputeCoordinatorSubsystem {}
5254

5355
impl DisputeCoordinatorSubsystem {
5456
/// Create a new instance of the subsystem.
55-
pub fn new(_: Arc<dyn KeyValueDB>, _: Config, _: Arc<LocalKeystore>) -> Self {
57+
pub fn new(_: Arc<dyn KeyValueDB>, _: Config, _: Arc<LocalKeystore>, _: Metrics) -> Self {
5658
DisputeCoordinatorSubsystem {}
5759
}
5860
}

node/core/dispute-coordinator/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
//! another node, this will trigger the dispute participation subsystem to recover and validate the block and call
2626
//! back to this subsystem.
2727
28+
mod metrics;
29+
2830
#[cfg(feature = "disputes")]
2931
mod real;
3032
#[cfg(feature = "disputes")]
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2020 Parity Technologies (UK) Ltd.
2+
// This file is part of Polkadot.
3+
4+
// Polkadot is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Polkadot is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
16+
17+
use polkadot_node_subsystem_util::metrics::{self, prometheus};
18+
19+
#[derive(Clone)]
20+
struct MetricsInner {
21+
/// Number of opened disputes.
22+
open: prometheus::Counter<prometheus::U64>,
23+
/// Votes of all disputes.
24+
votes: prometheus::CounterVec<prometheus::U64>,
25+
/// Conclusion across all disputes.
26+
concluded: prometheus::CounterVec<prometheus::U64>,
27+
}
28+
29+
/// Candidate validation metrics.
30+
#[derive(Default, Clone)]
31+
pub struct Metrics(Option<MetricsInner>);
32+
33+
#[cfg(feature = "disputes")]
34+
impl Metrics {
35+
pub(crate) fn on_open(&self) {
36+
if let Some(metrics) = &self.0 {
37+
metrics.open.inc();
38+
}
39+
}
40+
41+
pub(crate) fn on_valid_vote(&self) {
42+
if let Some(metrics) = &self.0 {
43+
metrics.votes.with_label_values(&["valid"]).inc();
44+
}
45+
}
46+
47+
pub(crate) fn on_invalid_vote(&self) {
48+
if let Some(metrics) = &self.0 {
49+
metrics.votes.with_label_values(&["invalid"]).inc();
50+
}
51+
}
52+
53+
pub(crate) fn on_concluded_valid(&self) {
54+
if let Some(metrics) = &self.0 {
55+
metrics.concluded.with_label_values(&["valid"]).inc();
56+
}
57+
}
58+
59+
pub(crate) fn on_concluded_invalid(&self) {
60+
if let Some(metrics) = &self.0 {
61+
metrics.concluded.with_label_values(&["invalid"]).inc();
62+
}
63+
}
64+
}
65+
66+
impl metrics::Metrics for Metrics {
67+
fn try_register(registry: &prometheus::Registry) -> Result<Self, prometheus::PrometheusError> {
68+
let metrics = MetricsInner {
69+
open: prometheus::register(
70+
prometheus::Counter::with_opts(prometheus::Opts::new(
71+
"parachain_candidate_disputes_total",
72+
"Total number of raised disputes.",
73+
))?,
74+
registry,
75+
)?,
76+
concluded: prometheus::register(
77+
prometheus::CounterVec::new(
78+
prometheus::Opts::new(
79+
"parachain_candidate_dispute_concluded",
80+
"Concluded dispute votes, sorted by candidate is `valid` and `invalid`.",
81+
),
82+
&["validity"],
83+
)?,
84+
registry,
85+
)?,
86+
votes: prometheus::register(
87+
prometheus::CounterVec::new(
88+
prometheus::Opts::new(
89+
"parachain_candidate_dispute_votes",
90+
"Accumulated dispute votes, sorted by candidate is `valid` and `invalid`.",
91+
),
92+
&["validity"],
93+
)?,
94+
registry,
95+
)?,
96+
};
97+
Ok(Metrics(Some(metrics)))
98+
}
99+
}

node/core/dispute-coordinator/src/real/mod.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use kvdb::KeyValueDB;
5656
use parity_scale_codec::{Decode, Encode, Error as CodecError};
5757
use sc_keystore::LocalKeystore;
5858

59+
use crate::metrics::Metrics;
5960
use backend::{Backend, OverlayedBackend};
6061
use db::v1::{DbBackend, RecentDisputes};
6162

@@ -116,12 +117,18 @@ pub struct DisputeCoordinatorSubsystem {
116117
config: Config,
117118
store: Arc<dyn KeyValueDB>,
118119
keystore: Arc<LocalKeystore>,
120+
metrics: Metrics,
119121
}
120122

121123
impl DisputeCoordinatorSubsystem {
122124
/// Create a new instance of the subsystem.
123-
pub fn new(store: Arc<dyn KeyValueDB>, config: Config, keystore: Arc<LocalKeystore>) -> Self {
124-
DisputeCoordinatorSubsystem { store, config, keystore }
125+
pub fn new(
126+
store: Arc<dyn KeyValueDB>,
127+
config: Config,
128+
keystore: Arc<LocalKeystore>,
129+
metrics: Metrics,
130+
) -> Self {
131+
DisputeCoordinatorSubsystem { store, config, keystore, metrics }
125132
}
126133
}
127134

@@ -329,6 +336,7 @@ where
329336
rolling_session_window: RollingSessionWindow::new(DISPUTE_WINDOW),
330337
recovery_state: Participation::Pending,
331338
};
339+
let metrics = &subsystem.metrics;
332340

333341
loop {
334342
let mut overlay_db = OverlayedBackend::new(backend);
@@ -348,7 +356,8 @@ where
348356
},
349357
FromOverseer::Signal(OverseerSignal::BlockFinalized(_, _)) => {},
350358
FromOverseer::Communication { msg } =>
351-
handle_incoming(ctx, &mut overlay_db, &mut state, msg, clock.now()).await?,
359+
handle_incoming(ctx, &mut overlay_db, &mut state, msg, clock.now(), &metrics)
360+
.await?,
352361
}
353362

354363
if !overlay_db.is_empty() {
@@ -518,6 +527,7 @@ async fn handle_incoming(
518527
state: &mut State,
519528
message: DisputeCoordinatorMessage,
520529
now: Timestamp,
530+
metrics: &Metrics,
521531
) -> Result<(), Error> {
522532
match message {
523533
DisputeCoordinatorMessage::ImportStatements {
@@ -537,6 +547,7 @@ async fn handle_incoming(
537547
statements,
538548
now,
539549
pending_confirmation,
550+
metrics,
540551
)
541552
.await?;
542553
},
@@ -578,6 +589,7 @@ async fn handle_incoming(
578589
session,
579590
valid,
580591
now,
592+
metrics,
581593
)
582594
.await?;
583595
},
@@ -635,6 +647,7 @@ async fn handle_import_statements(
635647
statements: Vec<(SignedDisputeStatement, ValidatorIndex)>,
636648
now: Timestamp,
637649
pending_confirmation: oneshot::Sender<ImportStatementsResult>,
650+
metrics: &Metrics,
638651
) -> Result<(), Error> {
639652
if state.highest_session.map_or(true, |h| session + DISPUTE_WINDOW < h) {
640653
// It is not valid to participate in an ancient dispute (spam?).
@@ -694,6 +707,7 @@ async fn handle_import_statements(
694707

695708
match statement.statement().clone() {
696709
DisputeStatement::Valid(valid_kind) => {
710+
metrics.on_valid_vote();
697711
insert_into_statement_vec(
698712
&mut votes.valid,
699713
valid_kind,
@@ -702,6 +716,7 @@ async fn handle_import_statements(
702716
);
703717
},
704718
DisputeStatement::Invalid(invalid_kind) => {
719+
metrics.on_invalid_vote();
705720
insert_into_statement_vec(
706721
&mut votes.invalid,
707722
invalid_kind,
@@ -784,6 +799,14 @@ async fn handle_import_statements(
784799
);
785800
return Ok(())
786801
}
802+
metrics.on_open();
803+
804+
if concluded_valid {
805+
metrics.on_concluded_valid();
806+
}
807+
if concluded_invalid {
808+
metrics.on_concluded_invalid();
809+
}
787810
}
788811

789812
// Only write when updated and vote is available.
@@ -824,6 +847,7 @@ async fn issue_local_statement(
824847
session: SessionIndex,
825848
valid: bool,
826849
now: Timestamp,
850+
metrics: &Metrics,
827851
) -> Result<(), Error> {
828852
// Load session info.
829853
let info = match state.rolling_session_window.session_info(session) {
@@ -857,7 +881,6 @@ async fn issue_local_statement(
857881

858882
let voted_indices: HashSet<_> = voted_indices.into_iter().collect();
859883
let controlled_indices = find_controlled_validator_indices(&state.keystore, &validators[..]);
860-
861884
for index in controlled_indices {
862885
if voted_indices.contains(&index) {
863886
continue
@@ -914,6 +937,7 @@ async fn issue_local_statement(
914937
statements,
915938
now,
916939
pending_confirmation,
940+
metrics,
917941
)
918942
.await?;
919943
match rx.await {

node/core/dispute-coordinator/src/real/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ impl TestState {
286286
self.db.clone(),
287287
self.config.clone(),
288288
self.subsystem_keystore.clone(),
289+
Metrics::default(),
289290
);
290291
let backend = DbBackend::new(self.db.clone(), self.config.column_config());
291292
let subsystem_task = run(subsystem, ctx, backend, Box::new(self.clock.clone()));

node/malus/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ color-eyre = { version = "0.5.11", default-features = false }
2727
assert_matches = "1.5"
2828
structopt = "0.3.23"
2929
async-trait = "0.1.51"
30+
31+
[dev-dependencies]
32+
polkadot-node-subsystem-test-helpers = { path = "../subsystem-test-helpers" }
33+
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
34+
futures = { version = "0.3.17", features = ["thread-pool"] }

0 commit comments

Comments
 (0)