This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix state-db pinning #12927
Merged
Merged
Fix state-db pinning #12927
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -38,7 +38,7 @@ pub struct NonCanonicalOverlay<BlockHash: Hash, Key: Hash> { | |||||||||||||
// would be deleted but kept around because block is pinned, ref counted. | ||||||||||||||
pinned: HashMap<BlockHash, u32>, | ||||||||||||||
pinned_insertions: HashMap<BlockHash, (Vec<Key>, u32)>, | ||||||||||||||
last_canon_pinned: Option<BlockHash>, | ||||||||||||||
pinned_canonincalized: Vec<BlockHash>, | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[cfg_attr(test, derive(PartialEq, Debug))] | ||||||||||||||
|
@@ -226,7 +226,7 @@ impl<BlockHash: Hash, Key: Hash> NonCanonicalOverlay<BlockHash, Key> { | |||||||||||||
pinned: Default::default(), | ||||||||||||||
pinned_insertions: Default::default(), | ||||||||||||||
values, | ||||||||||||||
last_canon_pinned: None, | ||||||||||||||
pinned_canonincalized: Default::default(), | ||||||||||||||
}) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
@@ -350,6 +350,16 @@ impl<BlockHash: Hash, Key: Hash> NonCanonicalOverlay<BlockHash, Key> { | |||||||||||||
self.last_canonicalized.as_ref().map(|&(_, n)| n) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
pub fn sync(&mut self) { | ||||||||||||||
arkpar marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
let mut pinned = std::mem::take(&mut self.pinned_canonincalized); | ||||||||||||||
for hash in pinned.iter() { | ||||||||||||||
self.unpin(hash) | ||||||||||||||
} | ||||||||||||||
pinned.clear(); | ||||||||||||||
Comment on lines
+356
to
+360
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.
Suggested change
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. This won't work because 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. Good point, but the call to |
||||||||||||||
// Reuse the same memory buffer | ||||||||||||||
self.pinned_canonincalized = pinned; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Select a top-level root and canonicalized it. Discards all sibling subtrees and the root. | ||||||||||||||
/// Add a set of changes of the canonicalized block to `CommitSet` | ||||||||||||||
/// Return the block number of the canonicalized block | ||||||||||||||
|
@@ -371,13 +381,9 @@ impl<BlockHash: Hash, Key: Hash> NonCanonicalOverlay<BlockHash, Key> { | |||||||||||||
|
||||||||||||||
// No failures are possible beyond this point. | ||||||||||||||
|
||||||||||||||
// Unpin previously canonicalized block | ||||||||||||||
if let Some(prev_hash) = self.last_canon_pinned.take() { | ||||||||||||||
self.unpin(&prev_hash); | ||||||||||||||
} | ||||||||||||||
// Force pin canonicalized block so that it is no discarded immediately | ||||||||||||||
self.pin(hash); | ||||||||||||||
self.last_canon_pinned = Some(hash.clone()); | ||||||||||||||
self.pinned_canonincalized.push(hash.clone()); | ||||||||||||||
|
||||||||||||||
let mut discarded_journals = Vec::new(); | ||||||||||||||
let mut discarded_blocks = Vec::new(); | ||||||||||||||
|
@@ -720,16 +726,17 @@ mod tests { | |||||||||||||
let mut commit = CommitSet::default(); | ||||||||||||||
overlay.canonicalize(&h1, &mut commit).unwrap(); | ||||||||||||||
db.commit(&commit); | ||||||||||||||
assert!(contains(&overlay, 5)); | ||||||||||||||
overlay.sync(); | ||||||||||||||
assert!(!contains(&overlay, 5)); | ||||||||||||||
assert!(contains(&overlay, 7)); | ||||||||||||||
assert_eq!(overlay.levels.len(), 1); | ||||||||||||||
assert_eq!(overlay.parents.len(), 2); | ||||||||||||||
assert_eq!(overlay.parents.len(), 1); | ||||||||||||||
let mut commit = CommitSet::default(); | ||||||||||||||
overlay.canonicalize(&h2, &mut commit).unwrap(); | ||||||||||||||
assert!(!contains(&overlay, 5)); | ||||||||||||||
db.commit(&commit); | ||||||||||||||
overlay.sync(); | ||||||||||||||
assert_eq!(overlay.levels.len(), 0); | ||||||||||||||
assert_eq!(overlay.parents.len(), 1); | ||||||||||||||
assert_eq!(overlay.parents.len(), 0); | ||||||||||||||
assert!(db.data_eq(&make_db(&[1, 4, 6, 7, 8]))); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
@@ -746,8 +753,7 @@ mod tests { | |||||||||||||
let mut commit = CommitSet::default(); | ||||||||||||||
overlay.canonicalize(&h_1, &mut commit).unwrap(); | ||||||||||||||
db.commit(&commit); | ||||||||||||||
// explicitly unpin last block | ||||||||||||||
overlay.unpin(&h_1); | ||||||||||||||
overlay.sync(); | ||||||||||||||
assert!(!contains(&overlay, 1)); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
@@ -834,9 +840,8 @@ mod tests { | |||||||||||||
// canonicalize 1. 2 and all its children should be discarded | ||||||||||||||
let mut commit = CommitSet::default(); | ||||||||||||||
overlay.canonicalize(&h_1, &mut commit).unwrap(); | ||||||||||||||
// explicitly unpin last block | ||||||||||||||
overlay.unpin(&h_1); | ||||||||||||||
db.commit(&commit); | ||||||||||||||
overlay.sync(); | ||||||||||||||
assert_eq!(overlay.levels.len(), 2); | ||||||||||||||
assert_eq!(overlay.parents.len(), 6); | ||||||||||||||
assert!(!contains(&overlay, 1)); | ||||||||||||||
|
@@ -856,8 +861,8 @@ mod tests { | |||||||||||||
// canonicalize 1_2. 1_1 and all its children should be discarded | ||||||||||||||
let mut commit = CommitSet::default(); | ||||||||||||||
overlay.canonicalize(&h_1_2, &mut commit).unwrap(); | ||||||||||||||
overlay.unpin(&h_1_2); | ||||||||||||||
db.commit(&commit); | ||||||||||||||
overlay.sync(); | ||||||||||||||
assert_eq!(overlay.levels.len(), 1); | ||||||||||||||
assert_eq!(overlay.parents.len(), 3); | ||||||||||||||
assert!(!contains(&overlay, 11)); | ||||||||||||||
|
@@ -873,8 +878,8 @@ mod tests { | |||||||||||||
// canonicalize 1_2_2 | ||||||||||||||
let mut commit = CommitSet::default(); | ||||||||||||||
overlay.canonicalize(&h_1_2_2, &mut commit).unwrap(); | ||||||||||||||
overlay.unpin(&h_1_2_2); | ||||||||||||||
db.commit(&commit); | ||||||||||||||
overlay.sync(); | ||||||||||||||
assert_eq!(overlay.levels.len(), 0); | ||||||||||||||
assert_eq!(overlay.parents.len(), 0); | ||||||||||||||
assert!(db.data_eq(&make_db(&[1, 12, 122]))); | ||||||||||||||
|
@@ -958,6 +963,28 @@ mod tests { | |||||||||||||
assert!(!contains(&overlay, 1)); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[test] | ||||||||||||||
fn pins_canonicalized() { | ||||||||||||||
let mut db = make_db(&[]); | ||||||||||||||
|
||||||||||||||
let (h_1, c_1) = (H256::random(), make_changeset(&[1], &[])); | ||||||||||||||
let (h_2, c_2) = (H256::random(), make_changeset(&[2], &[])); | ||||||||||||||
|
||||||||||||||
let mut overlay = NonCanonicalOverlay::<H256, H256>::new(&db).unwrap(); | ||||||||||||||
db.commit(&overlay.insert(&h_1, 1, &H256::default(), c_1).unwrap()); | ||||||||||||||
db.commit(&overlay.insert(&h_2, 2, &h_1, c_2).unwrap()); | ||||||||||||||
|
||||||||||||||
let mut commit = CommitSet::default(); | ||||||||||||||
overlay.canonicalize(&h_1, &mut commit).unwrap(); | ||||||||||||||
overlay.canonicalize(&h_2, &mut commit).unwrap(); | ||||||||||||||
assert!(contains(&overlay, 1)); | ||||||||||||||
assert!(contains(&overlay, 2)); | ||||||||||||||
db.commit(&commit); | ||||||||||||||
overlay.sync(); | ||||||||||||||
assert!(!contains(&overlay, 1)); | ||||||||||||||
assert!(!contains(&overlay, 2)); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[test] | ||||||||||||||
fn pin_keeps_parent() { | ||||||||||||||
let mut db = make_db(&[]); | ||||||||||||||
|
@@ -1019,8 +1046,8 @@ mod tests { | |||||||||||||
|
||||||||||||||
let mut commit = CommitSet::default(); | ||||||||||||||
overlay.canonicalize(&h21, &mut commit).unwrap(); // h11 should stay in the DB | ||||||||||||||
overlay.unpin(&h21); | ||||||||||||||
db.commit(&commit); | ||||||||||||||
overlay.sync(); | ||||||||||||||
assert!(!contains(&overlay, 21)); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
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.
Maybe rename
sync
method withcommit_cannonicals
or something more related tocommit_operation
(commit_synch
maybe).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.
I went for a more general name. It basically means that the in-memory state is synchronized with the on-disk state.