-
Notifications
You must be signed in to change notification settings - Fork 24
feat(deferred-persist): Part 1: unpersisted gauge #1116
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c32cd9d
feat(deferred-persist): Part 1: unpersisted gauge
rkuris bad662f
Lint
rkuris b514848
Update dashboard
rkuris 57cdd41
lints
demosdemon 72d7d08
Merge branch 'main' into rkuris/track-unwritten-nodes
rkuris 1ea7a63
Remove duplicate expect clauses
rkuris d6d8201
Another attempt to merge this without warning
rkuris ba3becc
Merge remote-tracking branch 'origin/main' into rkuris/track-unwritte…
demosdemon 5e54330
describe the gauges
demosdemon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,13 +43,15 @@ pub(crate) mod hash; | |
| pub(crate) mod header; | ||
| pub(crate) mod persist; | ||
|
|
||
| use crate::firewood_gauge; | ||
| use crate::logger::trace; | ||
| use crate::node::branch::ReadSerializable as _; | ||
| use arc_swap::ArcSwap; | ||
| use arc_swap::access::DynAccess; | ||
| use smallvec::SmallVec; | ||
| use std::fmt::Debug; | ||
| use std::io::{Error, ErrorKind}; | ||
| use std::sync::atomic::AtomicUsize; | ||
|
|
||
| // Re-export types from alloc module | ||
| pub use alloc::{AreaIndex, LinearAddress, NodeAllocator}; | ||
|
|
@@ -121,6 +123,7 @@ impl<S: ReadableStorage> NodeStore<Committed, S> { | |
| deleted: Box::default(), | ||
| root_hash: None, | ||
| root: header.root_address().map(Into::into), | ||
| unwritten_nodes: AtomicUsize::new(0), | ||
| }, | ||
| storage, | ||
| }; | ||
|
|
@@ -150,6 +153,7 @@ impl<S: ReadableStorage> NodeStore<Committed, S> { | |
| deleted: Box::default(), | ||
| root_hash: None, | ||
| root: None, | ||
| unwritten_nodes: AtomicUsize::new(0), | ||
| }, | ||
| }) | ||
| } | ||
|
|
@@ -350,11 +354,27 @@ pub trait RootReader { | |
| } | ||
|
|
||
| /// A committed revision of a merkle trie. | ||
| #[derive(Clone, Debug)] | ||
| #[derive(Debug)] | ||
| pub struct Committed { | ||
| deleted: Box<[MaybePersistedNode]>, | ||
| root_hash: Option<TrieHash>, | ||
| root: Option<MaybePersistedNode>, | ||
| /// TODO: No readers of this variable yet - will be used for tracking unwritten nodes in committed revisions | ||
| unwritten_nodes: AtomicUsize, | ||
| } | ||
|
|
||
| impl Clone for Committed { | ||
| fn clone(&self) -> Self { | ||
| Self { | ||
| deleted: self.deleted.clone(), | ||
| root_hash: self.root_hash.clone(), | ||
| root: self.root.clone(), | ||
| unwritten_nodes: AtomicUsize::new( | ||
| self.unwritten_nodes | ||
| .load(std::sync::atomic::Ordering::Relaxed), | ||
| ), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Debug)] | ||
|
|
@@ -386,6 +406,8 @@ pub struct ImmutableProposal { | |
| root_hash: Option<TrieHash>, | ||
| /// The root node, either in memory or on disk | ||
| root: Option<MaybePersistedNode>, | ||
| /// The number of unwritten nodes in this proposal | ||
| unwritten_nodes: usize, | ||
| } | ||
|
|
||
| impl ImmutableProposal { | ||
|
|
@@ -403,6 +425,21 @@ impl ImmutableProposal { | |
| } | ||
| } | ||
|
|
||
| impl Drop for ImmutableProposal { | ||
| fn drop(&mut self) { | ||
| // When an immutable proposal is dropped without being committed, | ||
| // decrement the gauge to reflect that these nodes will never be written | ||
| if self.unwritten_nodes > 0 { | ||
| #[allow(clippy::cast_precision_loss)] | ||
| firewood_gauge!( | ||
| "firewood.nodes.unwritten", | ||
| "current number of unwritten nodes" | ||
| ) | ||
| .decrement(self.unwritten_nodes as f64); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Contains the state of a revision of a merkle trie. | ||
| /// | ||
| /// The first generic parameter is the type of the revision, which supports reading nodes from parent proposals. | ||
|
|
@@ -461,14 +498,23 @@ impl<T: Into<NodeStoreParent>, S: ReadableStorage> From<NodeStore<T, S>> | |
| /// Commit a proposal to a new revision of the trie | ||
| impl<S: WritableStorage> From<NodeStore<ImmutableProposal, S>> for NodeStore<Committed, S> { | ||
| fn from(val: NodeStore<ImmutableProposal, S>) -> Self { | ||
| let NodeStore { | ||
| header, | ||
| kind, | ||
| storage, | ||
| } = val; | ||
| // Use ManuallyDrop to prevent the Drop impl from running since we're committing | ||
| let kind = std::mem::ManuallyDrop::new(kind); | ||
|
|
||
| NodeStore { | ||
| header: val.header, | ||
| header, | ||
| kind: Committed { | ||
| deleted: val.kind.deleted, | ||
| root_hash: val.kind.root_hash, | ||
| root: val.kind.root, | ||
| deleted: kind.deleted.clone(), | ||
| root_hash: kind.root_hash.clone(), | ||
| root: kind.root.clone(), | ||
| unwritten_nodes: AtomicUsize::new(kind.unwritten_nodes), | ||
| }, | ||
| storage: val.storage, | ||
| storage, | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -495,6 +541,7 @@ impl<S: WritableStorage> NodeStore<Arc<ImmutableProposal>, S> { | |
| deleted: self.kind.deleted.clone(), | ||
| root_hash: self.kind.root_hash.clone(), | ||
| root: self.kind.root.clone(), | ||
| unwritten_nodes: AtomicUsize::new(self.kind.unwritten_nodes), | ||
| }, | ||
| storage: self.storage.clone(), | ||
| } | ||
|
|
@@ -520,6 +567,7 @@ impl<S: ReadableStorage> TryFrom<NodeStore<MutableProposal, S>> | |
| parent: Arc::new(ArcSwap::new(Arc::new(kind.parent))), | ||
| root_hash: None, | ||
| root: None, | ||
| unwritten_nodes: 0, | ||
| }), | ||
| storage, | ||
| }; | ||
|
|
@@ -532,20 +580,32 @@ impl<S: ReadableStorage> TryFrom<NodeStore<MutableProposal, S>> | |
|
|
||
| // Hashes the trie and returns the address of the new root. | ||
| #[cfg(feature = "ethhash")] | ||
| let (root, root_hash) = nodestore.hash_helper(root, &mut Path::new(), None)?; | ||
| let (root, root_hash, unwritten_count) = | ||
| nodestore.hash_helper(root, &mut Path::new(), None)?; | ||
| #[cfg(not(feature = "ethhash"))] | ||
| let (root, root_hash) = | ||
| let (root, root_hash, unwritten_count) = | ||
| NodeStore::<MutableProposal, S>::hash_helper(root, &mut Path::new())?; | ||
|
|
||
| let immutable_proposal = | ||
| Arc::into_inner(nodestore.kind).expect("no other references to the proposal"); | ||
| // Use ManuallyDrop to prevent Drop from running since we're replacing the proposal | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you expand the comment to explain the implications of not using |
||
| let immutable_proposal = std::mem::ManuallyDrop::new(immutable_proposal); | ||
| nodestore.kind = Arc::new(ImmutableProposal { | ||
| deleted: immutable_proposal.deleted, | ||
| parent: immutable_proposal.parent, | ||
| deleted: immutable_proposal.deleted.clone(), | ||
| parent: immutable_proposal.parent.clone(), | ||
| root_hash: Some(root_hash.into_triehash()), | ||
| root: Some(root), | ||
| unwritten_nodes: unwritten_count, | ||
| }); | ||
|
|
||
| // Track unwritten nodes in metrics | ||
| #[allow(clippy::cast_precision_loss)] | ||
| firewood_gauge!( | ||
| "firewood.nodes.unwritten", | ||
| "current number of unwritten nodes" | ||
| ) | ||
| .increment(unwritten_count as f64); | ||
|
|
||
| Ok(nodestore) | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirming that this is intentional the the cloned committed is using a new atomic integer and isn't re-using the integer from the previous cloned. The diverging committed nodes will have different unwritten_nodes counts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, it's not correct. I'll remove this code as it's dead anyway; we never clone Committed.