Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/alexey/pruner-account-history' i…
Browse files Browse the repository at this point in the history
…nto alexey/pruner-storage-history
  • Loading branch information
shekhirin committed Aug 2, 2023
2 parents 14added + 467ae73 commit e5fe91b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 15 deletions.
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: 0 additions & 1 deletion bin/reth/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,6 @@ impl<Ext: RethCliExt> Command<Ext> {
db.clone(),
self.chain.clone(),
prune_config.block_interval,
tree_config.max_reorg_depth(),
prune_config.parts,
BatchSizes::default(),
)
Expand Down
1 change: 0 additions & 1 deletion crates/consensus/beacon/src/engine/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,6 @@ where
db.clone(),
self.base_config.chain_spec.clone(),
5,
0,
PruneModes::default(),
BatchSizes::default(),
);
Expand Down
1 change: 1 addition & 0 deletions crates/prune/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ reth-primitives.workspace = true
reth-db.workspace = true
reth-provider.workspace = true
reth-interfaces.workspace = true
reth-metrics.workspace = true

# misc
tracing.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions crates/prune/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod error;
mod metrics;
mod pruner;

pub use error::PrunerError;
use metrics::Metrics;
pub use pruner::{BatchSizes, Pruner, PrunerResult, PrunerWithResult};
36 changes: 36 additions & 0 deletions crates/prune/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use reth_metrics::{metrics, metrics::Histogram, Metrics};
use reth_primitives::PrunePart;
use std::collections::HashMap;

#[derive(Debug, Default)]
pub(crate) struct Metrics {
pub(crate) pruner: PrunerMetrics,
prune_parts: HashMap<PrunePart, PrunerPartMetrics>,
}

impl Metrics {
/// Returns existing or initializes a new instance of [PrunerPartMetrics] for the provided
/// [PrunePart].
pub(crate) fn get_prune_part_metrics(
&mut self,
prune_part: PrunePart,
) -> &mut PrunerPartMetrics {
self.prune_parts.entry(prune_part).or_insert_with(|| {
PrunerPartMetrics::new_with_labels(&[("part", prune_part.to_string())])
})
}
}

#[derive(Metrics)]
#[metrics(scope = "pruner")]
pub(crate) struct PrunerMetrics {
/// Pruning duration
pub(crate) duration_seconds: Histogram,
}

#[derive(Metrics)]
#[metrics(scope = "pruner.parts")]
pub(crate) struct PrunerPartMetrics {
/// Pruning duration for this part
pub(crate) duration_seconds: Histogram,
}
38 changes: 25 additions & 13 deletions crates/prune/src/pruner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Support for pruning.

use crate::PrunerError;
use crate::{Metrics, PrunerError};
use rayon::prelude::*;
use reth_db::{
abstraction::cursor::{DbCursorRO, DbCursorRW},
Expand All @@ -17,7 +17,7 @@ use reth_provider::{
BlockReader, DatabaseProviderRW, ProviderFactory, PruneCheckpointReader, PruneCheckpointWriter,
TransactionsProvider,
};
use std::{ops::RangeInclusive, sync::Arc};
use std::{ops::RangeInclusive, sync::Arc, time::Instant};
use tracing::{debug, instrument, trace};

/// Result of [Pruner::run] execution
Expand Down Expand Up @@ -48,14 +48,11 @@ impl Default for BatchSizes {

/// Pruning routine. Main pruning logic happens in [Pruner::run].
pub struct Pruner<DB> {
metrics: Metrics,
provider_factory: ProviderFactory<DB>,
/// Minimum pruning interval measured in blocks. All prune parts are checked and, if needed,
/// pruned, when the chain advances by the specified number of blocks.
min_block_interval: u64,
/// Maximum prune depth. Used to determine the pruning target for parts that are needed during
/// the reorg, e.g. changesets.
#[allow(dead_code)]
max_prune_depth: u64,
/// Last pruned block number. Used in conjunction with `min_block_interval` to determine
/// when the pruning needs to be initiated.
last_pruned_block_number: Option<BlockNumber>,
Expand All @@ -69,14 +66,13 @@ impl<DB: Database> Pruner<DB> {
db: DB,
chain_spec: Arc<ChainSpec>,
min_block_interval: u64,
max_prune_depth: u64,
modes: PruneModes,
batch_sizes: BatchSizes,
) -> Self {
Self {
metrics: Metrics::default(),
provider_factory: ProviderFactory::new(db, chain_spec),
min_block_interval,
max_prune_depth,
last_pruned_block_number: None,
modes,
batch_sizes,
Expand All @@ -85,24 +81,41 @@ impl<DB: Database> Pruner<DB> {

/// Run the pruner
pub fn run(&mut self, tip_block_number: BlockNumber) -> PrunerResult {
let start = Instant::now();

let provider = self.provider_factory.provider_rw()?;

if let Some((to_block, prune_mode)) =
self.modes.prune_target_block_receipts(tip_block_number)?
{
let part_start = Instant::now();
self.prune_receipts(&provider, to_block, prune_mode)?;
self.metrics
.get_prune_part_metrics(PrunePart::Receipts)
.duration_seconds
.record(part_start.elapsed())
}

if let Some((to_block, prune_mode)) =
self.modes.prune_target_block_transaction_lookup(tip_block_number)?
{
let part_start = Instant::now();
self.prune_transaction_lookup(&provider, to_block, prune_mode)?;
self.metrics
.get_prune_part_metrics(PrunePart::TransactionLookup)
.duration_seconds
.record(part_start.elapsed())
}

if let Some((to_block, prune_mode)) =
self.modes.prune_target_block_sender_recovery(tip_block_number)?
{
let part_start = Instant::now();
self.prune_transaction_senders(&provider, to_block, prune_mode)?;
self.metrics
.get_prune_part_metrics(PrunePart::SenderRecovery)
.duration_seconds
.record(part_start.elapsed())
}

if let Some((to_block, prune_mode)) =
Expand All @@ -118,8 +131,10 @@ impl<DB: Database> Pruner<DB> {
}

provider.commit()?;

self.last_pruned_block_number = Some(tip_block_number);

self.metrics.pruner.duration_seconds.record(start.elapsed());

Ok(())
}

Expand Down Expand Up @@ -577,7 +592,7 @@ mod tests {
fn is_pruning_needed() {
let db = create_test_rw_db();
let pruner =
Pruner::new(db, MAINNET.clone(), 5, 0, PruneModes::default(), BatchSizes::default());
Pruner::new(db, MAINNET.clone(), 5, PruneModes::default(), BatchSizes::default());

// No last pruned block number was set before
let first_block_number = 1;
Expand Down Expand Up @@ -624,7 +639,6 @@ mod tests {
tx.inner_raw(),
MAINNET.clone(),
5,
0,
PruneModes { receipts: Some(prune_mode), ..Default::default() },
BatchSizes {
// Less than total amount of blocks to prune to test the batching logic
Expand Down Expand Up @@ -685,7 +699,6 @@ mod tests {
tx.inner_raw(),
MAINNET.clone(),
5,
0,
PruneModes { transaction_lookup: Some(prune_mode), ..Default::default() },
BatchSizes {
// Less than total amount of blocks to prune to test the batching logic
Expand Down Expand Up @@ -752,7 +765,6 @@ mod tests {
tx.inner_raw(),
MAINNET.clone(),
5,
0,
PruneModes { sender_recovery: Some(prune_mode), ..Default::default() },
BatchSizes {
// Less than total amount of blocks to prune to test the batching logic
Expand Down

0 comments on commit e5fe91b

Please sign in to comment.