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

refactor(prune): impl Segment for other prune segments #4899

Merged
merged 35 commits into from
Oct 12, 2023

Conversation

shekhirin
Copy link
Collaborator

@shekhirin shekhirin commented Oct 4, 2023

Continued work started for Receipts segment in #4887, but for other prune segments too. Most of the changes are copy-paste from pruner.rs into dedicated segments/segment.rs files.

Pruner::run was also refactored to make use of a generic interface across segments:

// TODO(alexey): this is cursed, refactor
let segments: [PrunableSegment<DB>; 5] = [
PrunableSegment::new(
segments::Receipts::default(),
PruneModes::prune_target_block_receipts,
),
PrunableSegment::new(
segments::TransactionLookup::default(),
PruneModes::prune_target_block_transaction_lookup,
),
PrunableSegment::new(
segments::SenderRecovery::default(),
PruneModes::prune_target_block_sender_recovery,
),
PrunableSegment::new(
segments::AccountHistory::default(),
PruneModes::prune_target_block_account_history,
),
PrunableSegment::new(
segments::StorageHistory::default(),
PruneModes::prune_target_block_storage_history,
),
];
for PrunableSegment(segment, get_prune_target_block) in segments {
if delete_limit == 0 {
break
}
if let Some((to_block, prune_mode)) =
get_prune_target_block(&self.modes, tip_block_number)?
{
trace!(
target: "pruner",
segment = ?segment.segment(),
%to_block,
?prune_mode,
"Got target block to prune"
);
let segment_start = Instant::now();
let previous_checkpoint = provider.get_prune_checkpoint(segment.segment())?;
let output = segment
.prune(&provider, PruneInput { previous_checkpoint, to_block, delete_limit })?;
if let Some(checkpoint) = output.checkpoint {
segment
.save_checkpoint(&provider, checkpoint.as_prune_checkpoint(prune_mode))?;
}
self.metrics
.get_prune_segment_metrics(segment.segment())
.duration_seconds
.record(segment_start.elapsed());
done = done && output.done;
delete_limit = delete_limit.saturating_sub(output.pruned);
stats.insert(
segment.segment(),
(PruneProgress::from_done(output.done), output.pruned),
);
} else {
trace!(target: "pruner", segment = ?segment.segment(), "No target block to prune");
}
}

There are two main TODOs now:

  1. We don't pass segments externally into the Pruner, but instead they're initialized in-place as an array of Box<dyn Segment> and the corresponding PruneModes::prune_target_block_* function. We need to refactor it by:
    1. Either having a static array, or accepting the collection of segments into Pruner::new.
    2. Getting rid of PruneModes::prune_target_block_* functions and putting this responsibility on Segment.
  2. ReceiptsByLogs segment doesn't implement the Segment trait, because its prune method signature differs. We need to make it work the same way as other segments.

@codecov
Copy link

codecov bot commented Oct 4, 2023

Codecov Report

Merging #4899 (d7fa9a5) into main (65cc314) will increase coverage by 0.06%.
The diff coverage is 87.37%.

Impacted file tree graph

Files Coverage Δ
crates/primitives/src/prune/mod.rs 80.85% <ø> (-5.15%) ⬇️
crates/prune/src/event.rs 0.00% <ø> (ø)
crates/prune/src/segments/headers.rs 99.12% <100.00%> (+0.09%) ⬆️
crates/prune/src/segments/transactions.rs 97.77% <100.00%> (+0.27%) ⬆️
bin/reth/src/node/events.rs 11.36% <0.00%> (ø)
crates/prune/src/segments/mod.rs 83.33% <80.00%> (-1.29%) ⬇️
crates/prune/src/segments/sender_recovery.rs 98.16% <98.16%> (ø)
crates/prune/src/segments/transaction_lookup.rs 98.13% <98.13%> (ø)
crates/prune/src/segments/receipts.rs 95.37% <78.57%> (-2.59%) ⬇️
crates/prune/src/segments/account_history.rs 97.29% <97.29%> (ø)
... and 4 more

... and 4 files with indirect coverage changes

Flag Coverage Δ
integration-tests 15.41% <0.00%> (+<0.01%) ⬆️
unit-tests 62.54% <87.37%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
reth binary 30.62% <0.00%> (ø)
blockchain tree 80.64% <ø> (ø)
pipeline 88.45% <ø> (ø)
storage (db) 74.40% <ø> (ø)
trie 94.48% <ø> (ø)
txpool 48.52% <ø> (ø)
networking 76.07% <ø> (+<0.01%) ⬆️
rpc 57.96% <ø> (-0.03%) ⬇️
consensus 63.01% <ø> (ø)
revm 27.79% <ø> (ø)
payload builder 7.96% <ø> (ø)
primitives 86.35% <ø> (-0.03%) ⬇️

Base automatically changed from alexey/pruner-part-trait to main October 5, 2023 12:07
@shekhirin shekhirin marked this pull request as ready for review October 5, 2023 15:19
@shekhirin shekhirin requested a review from mattsse October 5, 2023 15:19
@shekhirin shekhirin added C-debt Refactor of code section that is hard to understand or maintain A-pruning Related to pruning or full node labels Oct 5, 2023
Comment on lines 102 to 112
#[allow(clippy::type_complexity)]
let segments: [(
Box<dyn Segment<DB>>,
Box<
dyn Fn(
&PruneModes,
BlockNumber,
)
-> Result<Option<(BlockNumber, PruneMode)>, PruneSegmentError>,
>,
); 5] = [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the issue here that we want to combine the segment and the handler into a simple array over which we can loop?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can at least add a helper type for this, with the segment and the handler as field,

or maybe the fn can even be made part of the segment trait?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can at least add a helper type for this, with the segment and the handler as field,

Yep, introduced a helper type. Couldn't make it more succinct because of Fn definition.

or maybe the fn can even be made part of the segment trait?

I think it can be, but it would also require touching PruneModes, and I rather keep this PR only to refactoring the pruner, because it's already large.

@shekhirin shekhirin requested a review from mattsse October 6, 2023 14:13
@shekhirin shekhirin added this pull request to the merge queue Oct 12, 2023
Merged via the queue into main with commit 1831197 Oct 12, 2023
23 checks passed
@shekhirin shekhirin deleted the alexey/pruner-part-trait-impls branch October 12, 2023 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-pruning Related to pruning or full node C-debt Refactor of code section that is hard to understand or maintain
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants