-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Remove needless last_root for better reclaims #8148
Merged
Merged
Changes from all 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,6 @@ pub struct AccountsIndex<T> { | |
pub account_maps: HashMap<Pubkey, RwLock<SlotList<T>>>, | ||
|
||
pub roots: HashSet<Slot>, | ||
|
||
// This value that needs to be stored to recover the index from AppendVec | ||
pub last_root: Slot, | ||
} | ||
|
||
impl<T: Clone> AccountsIndex<T> { | ||
|
@@ -148,10 +145,6 @@ impl<T: Clone> AccountsIndex<T> { | |
entry.write().unwrap().push((slot, account_info)); | ||
} | ||
|
||
pub fn is_purged(&self, slot: Slot) -> bool { | ||
slot < self.last_root | ||
} | ||
|
||
pub fn can_purge(max_root: Slot, slot: Slot) -> bool { | ||
slot < max_root | ||
} | ||
|
@@ -161,11 +154,6 @@ impl<T: Clone> AccountsIndex<T> { | |
} | ||
|
||
pub fn add_root(&mut self, slot: Slot) { | ||
assert!( | ||
(self.last_root == 0 && slot == 0) || (slot >= self.last_root), | ||
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. I think removing this assertion is ok to remove |
||
"new roots must be increasing" | ||
); | ||
self.last_root = slot; | ||
self.roots.insert(slot); | ||
} | ||
/// Remove the slot when the storage for the slot is freed | ||
|
@@ -270,29 +258,6 @@ mod tests { | |
assert_eq!(list[idx], (0, true)); | ||
} | ||
|
||
#[test] | ||
fn test_is_purged() { | ||
let mut index = AccountsIndex::<bool>::default(); | ||
assert!(!index.is_purged(0)); | ||
index.add_root(1); | ||
assert!(index.is_purged(0)); | ||
} | ||
|
||
#[test] | ||
fn test_max_last_root() { | ||
let mut index = AccountsIndex::<bool>::default(); | ||
index.add_root(1); | ||
assert_eq!(index.last_root, 1); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn test_max_last_root_old() { | ||
let mut index = AccountsIndex::<bool>::default(); | ||
index.add_root(1); | ||
index.add_root(0); | ||
} | ||
|
||
#[test] | ||
fn test_cleanup_first() { | ||
let mut index = AccountsIndex::<bool>::default(); | ||
|
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.
I just realized all inputs of this function is ensured to be older or equal to
last_root
.Notice the
or equal to
part, this assertion must be*slot <= last_root
forpurge_zero_lamport_accounts
. But, we don't need this to begin with, as said above.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.
sorry, why are they guaranteed to be older than last_root?
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 can explain this by reading the code from here upwards the call graph.
Firstly, this
cleanup_dead_slots(dead_slots, last_root)
is only called fromhandle_reclaims(reclaims)
.dead_slots
are derived fromreclaims
by callingremove_dead_accounts()
insidehandle_reclaims()
, which does just simple 1-to-1 storage -> slot mapping. Thus, we just need to explain in terms ofreclaims
further on.handle_reclaims()
are called only frompurge_zero_lamport_accounts()
(1) andstore_with_hashes()
(2). These are the only 2 code paths reaching to this function.(1) For the
purge_zero_lamport_accounts()
cod epath, reasoning is simple:The
reclaims
is a collection of results ofaccounts_index.purge(&pubkey)
, which in turn the sum ofget_rooted_entries()
for all such zero lamportpubkey
s. So,reclaims
(thus,dead_slots
) are always rooted slots only. This is true for normal operation because the validator callsadd_root()
as it roots slots.Also this is also true for snapshot restoration because
generate_index()
is called beforepurge_zero_lamport_accounts()
andgenerate_index()
doesaccounts_index.roots.insert(*slot_id)
.(2) For the
store_with_hashes()
, reasoning is a bit longer:The
reclaims
is ultimately only generated ataccounts_index.update()
. There is intermediatelyfn
s likeaccounts_db.update_index()
andaccounts_index.insert()
, but the destination of this call graph is same.Next,
accounts_index.update(slot, ...)
createsreclaims
by concatenating two sets:A:
can_purge
-able (=slot
<max_root
) entriesB: Old entries in
slot
by replacing with updated oneA is obviously fine because
max_root
is always member ofaccounts_index.roots
. B is ensured not to bedead
atremove_dead_accounts()
because the case B is always inserting an updated alive entry into theslot
.So, as a normal operation,
cleanup_dead_slots()
never removes anything such thatslot > last_root
.And leaving this line's
retain
causes halfway-deallocated state becausecleanup_dead_slots()
is only called once ever for each given slot. That's because later invocations ofremove_dead_accounts()
would remove any slots whose storage are already removed.Thanks for reading a rather long comment. :)
So I think this
retain
does add little value and complicates code reading with seemingly overlapping and spread responsibility for ensuringpurge
-able slots at caller-site or at callee-site.As a second thought, if you're a bit concerned for future changes that might break these assumptions, this PR can be changed to
assert!
that.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.
Ok. Thanks for the description. I think it can be removed.