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: Better progress reporting for stage checkpoints #2982

Merged
merged 13 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix(stages): include block range in headers checkpoint (#2950)
  • Loading branch information
shekhirin committed Jun 2, 2023
commit 2659cbaf539c61e24b27cda3537aa1ab0f7e6951
67 changes: 49 additions & 18 deletions crates/primitives/src/stage/checkpoints.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
trie::{hash_builder::HashBuilderState, StoredSubNode},
Address, BlockNumber, TxNumber, H256,
Address, BlockNumber, H256,
};
use bytes::{Buf, BufMut};
use reth_codecs::{derive_arbitrary, main_codec, Compact};
Expand Down Expand Up @@ -136,6 +136,16 @@ pub struct ExecutionCheckpoint {
pub progress: EntitiesCheckpoint,
}

/// Saves the progress of Headers stage.
#[main_codec]
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct HeadersCheckpoint {
/// Block range which this checkpoint is valid for.
pub block_range: CheckpointBlockRange,
/// Progress measured in gas.
pub progress: EntitiesCheckpoint,
}

/// Saves the progress of abstract stage iterating over or downloading entities.
#[main_codec]
#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)]
Expand Down Expand Up @@ -221,6 +231,14 @@ impl StageCheckpoint {
}
}

/// Returns the headers stage checkpoint, if any.
pub fn headers_stage_checkpoint(&self) -> Option<HeadersCheckpoint> {
match self.stage_checkpoint {
Some(StageUnitCheckpoint::Headers(checkpoint)) => Some(checkpoint),
_ => None,
}
}

/// Sets the block number.
pub fn with_block_number(mut self, block_number: BlockNumber) -> Self {
self.block_number = block_number;
Expand Down Expand Up @@ -256,6 +274,12 @@ impl StageCheckpoint {
self.stage_checkpoint = Some(StageUnitCheckpoint::Execution(checkpoint));
self
}

/// Sets the stage checkpoint to headers.
pub fn with_headers_stage_checkpoint(mut self, checkpoint: HeadersCheckpoint) -> Self {
self.stage_checkpoint = Some(StageUnitCheckpoint::Headers(checkpoint));
self
}
}

impl Display for StageCheckpoint {
Expand All @@ -269,9 +293,10 @@ impl Display for StageCheckpoint {
progress: entities, ..
}) |
StageUnitCheckpoint::Entities(entities) |
StageUnitCheckpoint::Execution(ExecutionCheckpoint { progress: entities, .. }),
StageUnitCheckpoint::Execution(ExecutionCheckpoint { progress: entities, .. }) |
StageUnitCheckpoint::Headers(HeadersCheckpoint { progress: entities, .. }),
) => entities.fmt(f),
Some(StageUnitCheckpoint::Transaction(_)) | None => write!(f, "{}", self.block_number),
None => write!(f, "{}", self.block_number),
}
}
}
Expand All @@ -282,8 +307,6 @@ impl Display for StageCheckpoint {
#[derive_arbitrary(compact)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum StageUnitCheckpoint {
/// Saves the progress of transaction-indexed stages.
Transaction(TxNumber),
/// Saves the progress of AccountHashing stage.
Account(AccountHashingCheckpoint),
/// Saves the progress of StorageHashing stage.
Expand All @@ -292,6 +315,8 @@ pub enum StageUnitCheckpoint {
Entities(EntitiesCheckpoint),
/// Saves the progress of Execution stage.
Execution(ExecutionCheckpoint),
/// Saves the progress of Headers stage.
Headers(HeadersCheckpoint),
}

impl Compact for StageUnitCheckpoint {
Expand All @@ -300,23 +325,23 @@ impl Compact for StageUnitCheckpoint {
B: BufMut + AsMut<[u8]>,
{
match self {
StageUnitCheckpoint::Transaction(data) => {
StageUnitCheckpoint::Account(data) => {
buf.put_u8(0);
1 + data.to_compact(buf)
}
StageUnitCheckpoint::Account(data) => {
StageUnitCheckpoint::Storage(data) => {
buf.put_u8(1);
1 + data.to_compact(buf)
}
StageUnitCheckpoint::Storage(data) => {
StageUnitCheckpoint::Entities(data) => {
buf.put_u8(2);
1 + data.to_compact(buf)
}
StageUnitCheckpoint::Entities(data) => {
StageUnitCheckpoint::Execution(data) => {
buf.put_u8(3);
1 + data.to_compact(buf)
}
StageUnitCheckpoint::Execution(data) => {
StageUnitCheckpoint::Headers(data) => {
buf.put_u8(4);
1 + data.to_compact(buf)
}
Expand All @@ -329,25 +354,25 @@ impl Compact for StageUnitCheckpoint {
{
match buf[0] {
0 => {
let (data, buf) = TxNumber::from_compact(&buf[1..], buf.len() - 1);
(Self::Transaction(data), buf)
}
1 => {
let (data, buf) = AccountHashingCheckpoint::from_compact(&buf[1..], buf.len() - 1);
(Self::Account(data), buf)
}
2 => {
1 => {
let (data, buf) = StorageHashingCheckpoint::from_compact(&buf[1..], buf.len() - 1);
(Self::Storage(data), buf)
}
3 => {
2 => {
let (data, buf) = EntitiesCheckpoint::from_compact(&buf[1..], buf.len() - 1);
(Self::Entities(data), buf)
}
4 => {
3 => {
let (data, buf) = ExecutionCheckpoint::from_compact(&buf[1..], buf.len() - 1);
(Self::Execution(data), buf)
}
4 => {
let (data, buf) = HeadersCheckpoint::from_compact(&buf[1..], buf.len() - 1);
(Self::Headers(data), buf)
}
_ => unreachable!("Junk data in database: unknown StageUnitCheckpoint variant"),
}
}
Expand Down Expand Up @@ -383,7 +408,6 @@ mod tests {
fn stage_unit_checkpoint_roundtrip() {
let mut rng = rand::thread_rng();
let checkpoints = vec![
StageUnitCheckpoint::Transaction(rng.gen()),
StageUnitCheckpoint::Account(AccountHashingCheckpoint {
address: Some(Address::from_low_u64_be(rng.gen())),
block_range: CheckpointBlockRange { from: rng.gen(), to: rng.gen() },
Expand Down Expand Up @@ -412,6 +436,13 @@ mod tests {
total: Some(u32::MAX as u64 + rng.gen::<u64>()),
},
}),
StageUnitCheckpoint::Headers(HeadersCheckpoint {
block_range: CheckpointBlockRange { from: rng.gen(), to: rng.gen() },
progress: EntitiesCheckpoint {
processed: rng.gen::<u32>() as u64,
total: Some(u32::MAX as u64 + rng.gen::<u64>()),
},
}),
];

for checkpoint in checkpoints {
Expand Down
3 changes: 2 additions & 1 deletion crates/primitives/src/stage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ pub use id::StageId;
mod checkpoints;
pub use checkpoints::{
AccountHashingCheckpoint, CheckpointBlockRange, EntitiesCheckpoint, ExecutionCheckpoint,
MerkleCheckpoint, StageCheckpoint, StageUnitCheckpoint, StorageHashingCheckpoint,
HeadersCheckpoint, MerkleCheckpoint, StageCheckpoint, StageUnitCheckpoint,
StorageHashingCheckpoint,
};
23 changes: 11 additions & 12 deletions crates/stages/src/pipeline/sync_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use reth_metrics::{
};
use reth_primitives::{
stage::{
AccountHashingCheckpoint, EntitiesCheckpoint, ExecutionCheckpoint, StageCheckpoint,
StageId, StageUnitCheckpoint, StorageHashingCheckpoint,
AccountHashingCheckpoint, EntitiesCheckpoint, ExecutionCheckpoint, HeadersCheckpoint,
StageCheckpoint, StageId, StageUnitCheckpoint, StorageHashingCheckpoint,
},
BlockNumber,
};
Expand Down Expand Up @@ -43,25 +43,24 @@ impl Metrics {

let (processed, total) = match checkpoint.stage_checkpoint {
Some(
StageUnitCheckpoint::Entities(progress @ EntitiesCheckpoint { .. }) |
StageUnitCheckpoint::Execution(ExecutionCheckpoint { progress, .. }) |
// Only report metrics for hashing stages if `total` is known, otherwise it means
// we're unwinding and operating on changesets, rather than accounts or storage slots.
// we're unwinding and operating on changesets, rather than accounts or storage
// slots.
StageUnitCheckpoint::Account(AccountHashingCheckpoint {
progress: progress @ EntitiesCheckpoint { total: Some(_), .. },
..
}) |
StageUnitCheckpoint::Storage(StorageHashingCheckpoint {
progress: progress @ EntitiesCheckpoint { total: Some(_), .. },
..
}),
}) |
StageUnitCheckpoint::Entities(progress @ EntitiesCheckpoint { .. }) |
StageUnitCheckpoint::Execution(ExecutionCheckpoint { progress, .. }) |
StageUnitCheckpoint::Headers(HeadersCheckpoint { progress, .. }),
) => (progress.processed, progress.total),
Some(
StageUnitCheckpoint::Transaction(_) |
StageUnitCheckpoint::Account(_) |
StageUnitCheckpoint::Storage(_),
) |
None => (checkpoint.block_number, max_block_number),
Some(StageUnitCheckpoint::Account(_) | StageUnitCheckpoint::Storage(_)) | None => {
(checkpoint.block_number, max_block_number)
}
};

stage_metrics.entities_processed.set(processed as f64);
Expand Down
Loading