Skip to content

Commit 06430bb

Browse files
authored
fix: Revert #1116 (#1313)
The use of ManuallyDrop seems to cause a leak. Let's fix this correctly later.
1 parent 3b644fa commit 06430bb

File tree

4 files changed

+20
-217
lines changed

4 files changed

+20
-217
lines changed

benchmark/Grafana-dashboard.json

Lines changed: 1 addition & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -845,113 +845,6 @@
845845
],
846846
"title": "Insert Merkle Ops by Type",
847847
"type": "timeseries"
848-
},
849-
{
850-
"datasource": {
851-
"type": "prometheus",
852-
"uid": "${DS_PROMETHEUS}"
853-
},
854-
"fieldConfig": {
855-
"defaults": {
856-
"color": {
857-
"mode": "palette-classic"
858-
},
859-
"custom": {
860-
"axisBorderShow": false,
861-
"axisCenteredZero": false,
862-
"axisColorMode": "text",
863-
"axisLabel": "",
864-
"axisPlacement": "auto",
865-
"barAlignment": 0,
866-
"barWidthFactor": 0.6,
867-
"drawStyle": "line",
868-
"fillOpacity": 0,
869-
"gradientMode": "none",
870-
"hideFrom": {
871-
"legend": false,
872-
"tooltip": false,
873-
"viz": false
874-
},
875-
"insertNulls": false,
876-
"lineInterpolation": "linear",
877-
"lineWidth": 1,
878-
"pointSize": 5,
879-
"scaleDistribution": {
880-
"type": "linear"
881-
},
882-
"showPoints": "auto",
883-
"spanNulls": false,
884-
"stacking": {
885-
"group": "A",
886-
"mode": "none"
887-
},
888-
"thresholdsStyle": {
889-
"mode": "off"
890-
}
891-
},
892-
"mappings": [],
893-
"thresholds": {
894-
"mode": "absolute",
895-
"steps": [
896-
{
897-
"color": "green",
898-
"value": null
899-
},
900-
{
901-
"color": "yellow",
902-
"value": 1000
903-
},
904-
{
905-
"color": "red",
906-
"value": 5000
907-
}
908-
]
909-
},
910-
"unit": "short"
911-
},
912-
"overrides": []
913-
},
914-
"gridPos": {
915-
"h": 8,
916-
"w": 12,
917-
"x": 12,
918-
"y": 31
919-
},
920-
"id": 13,
921-
"options": {
922-
"legend": {
923-
"calcs": [
924-
"min",
925-
"mean",
926-
"max",
927-
"last"
928-
],
929-
"displayMode": "table",
930-
"placement": "bottom",
931-
"showLegend": true
932-
},
933-
"tooltip": {
934-
"hideZeros": false,
935-
"mode": "single",
936-
"sort": "none"
937-
}
938-
},
939-
"pluginVersion": "11.5.1",
940-
"targets": [
941-
{
942-
"editorMode": "code",
943-
"expr": "firewood_nodes_unwritten",
944-
"legendFormat": "Unwritten Nodes",
945-
"range": true,
946-
"refId": "A",
947-
"datasource": {
948-
"type": "prometheus",
949-
"uid": "${DS_PROMETHEUS}"
950-
}
951-
}
952-
],
953-
"title": "Unwritten Nodes (In Memory)",
954-
"type": "timeseries"
955848
}
956849
],
957850
"refresh": "10s",
@@ -970,4 +863,4 @@
970863
"uid": "adxfhfmwx5ypsc",
971864
"version": 12,
972865
"weekStart": ""
973-
}
866+
}

storage/src/nodestore/hash.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ where
118118
pub(super) fn hash_helper(
119119
#[cfg(feature = "ethhash")] &self,
120120
node: Node,
121-
) -> Result<(MaybePersistedNode, HashType, usize), FileIoError> {
121+
) -> Result<(MaybePersistedNode, HashType), FileIoError> {
122122
let mut root_path = Path::new();
123123
#[cfg(not(feature = "ethhash"))]
124124
let res = Self::hash_helper_inner(node, PathGuard::from_path(&mut root_path))?;
@@ -136,10 +136,9 @@ where
136136
mut node: Node,
137137
mut path_prefix: PathGuard<'_>,
138138
#[cfg(feature = "ethhash")] fake_root_extra_nibble: Option<u8>,
139-
) -> Result<(MaybePersistedNode, HashType, usize), FileIoError> {
139+
) -> Result<(MaybePersistedNode, HashType), FileIoError> {
140140
// If this is a branch, find all unhashed children and recursively hash them.
141141
trace!("hashing {node:?} at {path_prefix:?}");
142-
let mut nodes_processed = 1usize; // Count this node
143142
if let Node::Branch(ref mut b) = node {
144143
// special case code for ethereum hashes at the account level
145144
#[cfg(feature = "ethhash")]
@@ -205,7 +204,7 @@ where
205204
let child_node = std::mem::take(child_node);
206205

207206
// Hash this child and update
208-
let (child_node, child_hash, child_count) = {
207+
let (child_node, child_hash) = {
209208
// we extend and truncate path_prefix to reduce memory allocations]
210209
let mut child_path_prefix = PathGuard::new(&mut path_prefix);
211210
child_path_prefix.0.extend(b.partial_path.0.iter().copied());
@@ -218,16 +217,15 @@ where
218217
#[cfg(not(feature = "ethhash"))]
219218
child_path_prefix.0.push(nibble as u8);
220219
#[cfg(feature = "ethhash")]
221-
let (child_node, child_hash, child_count) =
220+
let (child_node, child_hash) =
222221
self.hash_helper_inner(child_node, child_path_prefix, make_fake_root)?;
223222
#[cfg(not(feature = "ethhash"))]
224-
let (child_node, child_hash, child_count) =
223+
let (child_node, child_hash) =
225224
Self::hash_helper_inner(child_node, child_path_prefix)?;
226225

227-
(child_node, child_hash, child_count)
226+
(child_node, child_hash)
228227
};
229228

230-
nodes_processed = nodes_processed.saturating_add(child_count);
231229
*child = Some(Child::MaybePersisted(child_node, child_hash));
232230
trace!("child now {child:?}");
233231
}
@@ -253,7 +251,7 @@ where
253251
#[cfg(not(feature = "ethhash"))]
254252
let hash = hash_node(&node, &path_prefix);
255253

256-
Ok((SharedNode::new(node).into(), hash, nodes_processed))
254+
Ok((SharedNode::new(node).into(), hash))
257255
}
258256

259257
#[cfg(feature = "ethhash")]

storage/src/nodestore/mod.rs

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ pub(crate) mod header;
4444
pub(crate) mod persist;
4545
pub(crate) mod primitives;
4646

47-
use crate::firewood_gauge;
4847
use crate::linear::OffsetReader;
4948
use crate::logger::trace;
5049
use crate::node::branch::ReadSerializable as _;
@@ -53,7 +52,6 @@ use arc_swap::access::DynAccess;
5352
use smallvec::SmallVec;
5453
use std::fmt::Debug;
5554
use std::io::{Error, ErrorKind, Read};
56-
use std::sync::atomic::AtomicUsize;
5755

5856
// Re-export types from alloc module
5957
pub use alloc::NodeAllocator;
@@ -125,7 +123,6 @@ impl<S: ReadableStorage> NodeStore<Committed, S> {
125123
deleted: Box::default(),
126124
root_hash: None,
127125
root: header.root_address().map(Into::into),
128-
unwritten_nodes: AtomicUsize::new(0),
129126
},
130127
storage,
131128
};
@@ -155,7 +152,6 @@ impl<S: ReadableStorage> NodeStore<Committed, S> {
155152
deleted: Box::default(),
156153
root_hash: None,
157154
root: None,
158-
unwritten_nodes: AtomicUsize::new(0),
159155
},
160156
})
161157
}
@@ -362,8 +358,6 @@ pub struct Committed {
362358
deleted: Box<[MaybePersistedNode]>,
363359
root_hash: Option<TrieHash>,
364360
root: Option<MaybePersistedNode>,
365-
/// TODO: No readers of this variable yet - will be used for tracking unwritten nodes in committed revisions
366-
unwritten_nodes: AtomicUsize,
367361
}
368362

369363
impl Clone for Committed {
@@ -372,10 +366,6 @@ impl Clone for Committed {
372366
deleted: self.deleted.clone(),
373367
root_hash: self.root_hash.clone(),
374368
root: self.root.clone(),
375-
unwritten_nodes: AtomicUsize::new(
376-
self.unwritten_nodes
377-
.load(std::sync::atomic::Ordering::Relaxed),
378-
),
379369
}
380370
}
381371
}
@@ -409,8 +399,6 @@ pub struct ImmutableProposal {
409399
root_hash: Option<TrieHash>,
410400
/// The root node, either in memory or on disk
411401
root: Option<MaybePersistedNode>,
412-
/// The number of unwritten nodes in this proposal
413-
unwritten_nodes: usize,
414402
}
415403

416404
impl ImmutableProposal {
@@ -428,21 +416,6 @@ impl ImmutableProposal {
428416
}
429417
}
430418

431-
impl Drop for ImmutableProposal {
432-
fn drop(&mut self) {
433-
// When an immutable proposal is dropped without being committed,
434-
// decrement the gauge to reflect that these nodes will never be written
435-
if self.unwritten_nodes > 0 {
436-
#[allow(clippy::cast_precision_loss)]
437-
firewood_gauge!(
438-
"firewood.nodes.unwritten",
439-
"current number of unwritten nodes"
440-
)
441-
.decrement(self.unwritten_nodes as f64);
442-
}
443-
}
444-
}
445-
446419
/// Contains the state of a revision of a merkle trie.
447420
///
448421
/// The first generic parameter is the type of the revision, which supports reading nodes from parent proposals.
@@ -505,23 +478,14 @@ impl<T: Into<NodeStoreParent>, S: ReadableStorage> From<NodeStore<T, S>>
505478
/// Commit a proposal to a new revision of the trie
506479
impl<S: WritableStorage> From<NodeStore<ImmutableProposal, S>> for NodeStore<Committed, S> {
507480
fn from(val: NodeStore<ImmutableProposal, S>) -> Self {
508-
let NodeStore {
509-
header,
510-
kind,
511-
storage,
512-
} = val;
513-
// Use ManuallyDrop to prevent the Drop impl from running since we're committing
514-
let kind = std::mem::ManuallyDrop::new(kind);
515-
516481
NodeStore {
517-
header,
482+
header: val.header,
518483
kind: Committed {
519-
deleted: kind.deleted.clone(),
520-
root_hash: kind.root_hash.clone(),
521-
root: kind.root.clone(),
522-
unwritten_nodes: AtomicUsize::new(kind.unwritten_nodes),
484+
deleted: val.kind.deleted.clone(),
485+
root_hash: val.kind.root_hash.clone(),
486+
root: val.kind.root.clone(),
523487
},
524-
storage,
488+
storage: val.storage,
525489
}
526490
}
527491
}
@@ -548,7 +512,6 @@ impl<S: WritableStorage> NodeStore<Arc<ImmutableProposal>, S> {
548512
deleted: self.kind.deleted.clone(),
549513
root_hash: self.kind.root_hash.clone(),
550514
root: self.kind.root.clone(),
551-
unwritten_nodes: AtomicUsize::new(self.kind.unwritten_nodes),
552515
},
553516
storage: self.storage.clone(),
554517
}
@@ -574,7 +537,6 @@ impl<S: ReadableStorage> TryFrom<NodeStore<MutableProposal, S>>
574537
parent: Arc::new(ArcSwap::new(Arc::new(kind.parent))),
575538
root_hash: None,
576539
root: None,
577-
unwritten_nodes: 0,
578540
}),
579541
storage,
580542
};
@@ -587,31 +549,19 @@ impl<S: ReadableStorage> TryFrom<NodeStore<MutableProposal, S>>
587549

588550
// Hashes the trie and returns the address of the new root.
589551
#[cfg(feature = "ethhash")]
590-
let (root, root_hash, unwritten_count) = nodestore.hash_helper(root)?;
552+
let (root, root_hash) = nodestore.hash_helper(root)?;
591553
#[cfg(not(feature = "ethhash"))]
592-
let (root, root_hash, unwritten_count) =
593-
NodeStore::<MutableProposal, S>::hash_helper(root)?;
554+
let (root, root_hash) = NodeStore::<MutableProposal, S>::hash_helper(root)?;
594555

595556
let immutable_proposal =
596557
Arc::into_inner(nodestore.kind).expect("no other references to the proposal");
597-
// Use ManuallyDrop to prevent Drop from running since we're replacing the proposal
598-
let immutable_proposal = std::mem::ManuallyDrop::new(immutable_proposal);
599558
nodestore.kind = Arc::new(ImmutableProposal {
600559
deleted: immutable_proposal.deleted.clone(),
601560
parent: immutable_proposal.parent.clone(),
602561
root_hash: Some(root_hash.into_triehash()),
603562
root: Some(root),
604-
unwritten_nodes: unwritten_count,
605563
});
606564

607-
// Track unwritten nodes in metrics
608-
#[allow(clippy::cast_precision_loss)]
609-
firewood_gauge!(
610-
"firewood.nodes.unwritten",
611-
"current number of unwritten nodes"
612-
)
613-
.increment(unwritten_count as f64);
614-
615565
Ok(nodestore)
616566
}
617567
}

0 commit comments

Comments
 (0)