Skip to content

Commit

Permalink
feat(state-keeper): Add metric for l2 block seal reason (#2229)
Browse files Browse the repository at this point in the history
## What ❔

Add metric for l2 block seal reason

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.
- [ ] Spellcheck has been run via `zk spellcheck`.
  • Loading branch information
perekopskiy committed Jun 13, 2024
1 parent 6cc5455 commit f967e6d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
13 changes: 11 additions & 2 deletions core/node/state_keeper/src/io/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
L1BatchParams, L2BlockParams, PendingBatchData, StateKeeperIO,
},
mempool_actor::l2_tx_filter,
metrics::KEEPER_METRICS,
metrics::{L2BlockSealReason, AGGREGATION_METRICS, KEEPER_METRICS},
seal_criteria::{
IoSealCriteria, L2BlockMaxPayloadSizeSealer, TimeoutSealer, UnexecutableReason,
},
Expand Down Expand Up @@ -65,10 +65,19 @@ impl IoSealCriteria for MempoolIO {

fn should_seal_l2_block(&mut self, manager: &UpdatesManager) -> bool {
if self.timeout_sealer.should_seal_l2_block(manager) {
AGGREGATION_METRICS.l2_block_reason_inc(&L2BlockSealReason::Timeout);
return true;
}
self.l2_block_max_payload_size_sealer

if self
.l2_block_max_payload_size_sealer
.should_seal_l2_block(manager)
{
AGGREGATION_METRICS.l2_block_reason_inc(&L2BlockSealReason::PayloadSize);
return true;
}

false
}
}

Expand Down
4 changes: 3 additions & 1 deletion core/node/state_keeper/src/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ impl ZkSyncStateKeeper {
&mut self,
updates: &UpdatesManager,
) -> Result<L2BlockParams, Error> {
let latency = KEEPER_METRICS.wait_for_l2_block_params.start();
let cursor = updates.io_cursor();
while !self.is_canceled() {
if let Some(params) = self
Expand All @@ -341,6 +342,7 @@ impl ZkSyncStateKeeper {
.await
.context("error waiting for new L2 block params")?
{
latency.observe();
return Ok(params);
}
}
Expand Down Expand Up @@ -719,7 +721,7 @@ impl ZkSyncStateKeeper {
} else {
SealResolution::ExcludeAndSeal
};
AGGREGATION_METRICS.inc(criterion, &resolution);
AGGREGATION_METRICS.l1_batch_reason_inc(criterion, &resolution);
resolution
}
TxExecutionResult::RejectedByVm { reason } => {
Expand Down
19 changes: 17 additions & 2 deletions core/node/state_keeper/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ pub struct StateKeeperMetrics {
/// The time it takes for one iteration of the main loop in `process_l1_batch`.
#[metrics(buckets = Buckets::LATENCIES)]
pub process_l1_batch_loop_iteration: Histogram<Duration>,
/// The time it takes to wait for new L2 block parameters
#[metrics(buckets = Buckets::LATENCIES)]
pub wait_for_l2_block_params: Histogram<Duration>,
}

fn vm_revert_reason_as_metric_label(reason: &VmRevertReason) -> &'static str {
Expand Down Expand Up @@ -203,6 +206,13 @@ impl From<&SealResolution> for SealResolutionLabel {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EncodeLabelSet, EncodeLabelValue)]
#[metrics(label = "reason", rename_all = "snake_case")]
pub(super) enum L2BlockSealReason {
Timeout,
PayloadSize,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EncodeLabelSet)]
struct TxAggregationLabels {
criterion: &'static str,
Expand All @@ -213,24 +223,29 @@ struct TxAggregationLabels {
#[metrics(prefix = "server_tx_aggregation")]
pub(super) struct TxAggregationMetrics {
reason: Family<TxAggregationLabels, Counter>,
l2_block_reason: Family<L2BlockSealReason, Counter>,
}

impl TxAggregationMetrics {
pub fn inc(&self, criterion: &'static str, resolution: &SealResolution) {
pub fn l1_batch_reason_inc(&self, criterion: &'static str, resolution: &SealResolution) {
let labels = TxAggregationLabels {
criterion,
seal_resolution: Some(resolution.into()),
};
self.reason[&labels].inc();
}

pub fn inc_criterion(&self, criterion: &'static str) {
pub fn l1_batch_reason_inc_criterion(&self, criterion: &'static str) {
let labels = TxAggregationLabels {
criterion,
seal_resolution: None,
};
self.reason[&labels].inc();
}

pub fn l2_block_reason_inc(&self, reason: &L2BlockSealReason) {
self.l2_block_reason[reason].inc();
}
}

#[vise::register]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ impl ConditionalSealer for SequencerSealer {
"L1 batch #{l1_batch_number} processed by `{name}` with resolution {seal_resolution:?}",
name = sealer.prom_criterion_name()
);
AGGREGATION_METRICS.inc(sealer.prom_criterion_name(), &seal_resolution);
AGGREGATION_METRICS
.l1_batch_reason_inc(sealer.prom_criterion_name(), &seal_resolution);
}
SealResolution::NoSeal => { /* Don't do anything */ }
}
Expand Down
2 changes: 1 addition & 1 deletion core/node/state_keeper/src/seal_criteria/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl IoSealCriteria for TimeoutSealer {
millis_since(manager.batch_timestamp()) > block_commit_deadline_ms;

if should_seal_timeout {
AGGREGATION_METRICS.inc_criterion(RULE_NAME);
AGGREGATION_METRICS.l1_batch_reason_inc_criterion(RULE_NAME);
tracing::debug!(
"Decided to seal L1 batch using rule `{RULE_NAME}`; batch timestamp: {}, \
commit deadline: {block_commit_deadline_ms}ms",
Expand Down

0 comments on commit f967e6d

Please sign in to comment.