Skip to content

Commit

Permalink
separate checkpoints_to_keep and inmemory_states_to_keep
Browse files Browse the repository at this point in the history
  • Loading branch information
ShuoWangNSL committed Aug 15, 2024
1 parent 6968299 commit 8f37b43
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions rs/state_manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2200,17 +2200,19 @@ impl StateManagerImpl {
state_metadata.bundled_manifest.as_ref().map(|_| *height)
});

let heights_to_keep: BTreeSet<Height> = states
let checkpoint_heights_to_keep: BTreeSet<Height> = states
.states_metadata
.keys()
.copied()
.filter(|height| {
*height == Self::INITIAL_STATE_HEIGHT || *height >= last_checkpoint_to_keep
})
.chain(std::iter::once(latest_certified_height))
.chain(latest_manifest_height)
.collect();

let extra_inmemory_heights_to_keep: BTreeSet<Height> =
std::iter::once(latest_certified_height).collect();

// Send object to deallocation thread if it has capacity.
let deallocate = |x| {
if self.deallocation_sender.len() < DEALLOCATION_BACKLOG_THRESHOLD {
Expand All @@ -2224,7 +2226,7 @@ impl StateManagerImpl {

let (removed, retained) = states.snapshots.drain(0..).partition(|snapshot| {
heights_to_remove.contains(&snapshot.height)
&& !heights_to_keep.contains(&snapshot.height)
&& !extra_inmemory_heights_to_keep.contains(&snapshot.height)
});
states.snapshots = retained;

Expand Down Expand Up @@ -2262,7 +2264,7 @@ impl StateManagerImpl {
deallocate(Box::new(removed));

for (height, metadata) in states.states_metadata.range(heights_to_remove) {
if heights_to_keep.contains(height) {
if checkpoint_heights_to_keep.contains(height) {
continue;
}
if let Some(ref checkpoint_layout) = metadata.checkpoint_layout {
Expand All @@ -2275,7 +2277,7 @@ impl StateManagerImpl {
.certifications_metadata
.split_off(&last_height_to_keep);

for h in heights_to_keep.iter() {
for h in extra_inmemory_heights_to_keep.iter() {
if let Some(cert_metadata) = states.certifications_metadata.remove(h) {
certifications_metadata.insert(*h, cert_metadata);
}
Expand Down Expand Up @@ -2305,7 +2307,7 @@ impl StateManagerImpl {

let mut metadata_to_keep = states.states_metadata.split_off(&last_height_to_keep);

for h in heights_to_keep.iter() {
for h in checkpoint_heights_to_keep.iter() {
if let Some(metadata) = states.states_metadata.remove(h) {
metadata_to_keep.insert(*h, metadata);
}
Expand Down Expand Up @@ -2345,10 +2347,15 @@ impl StateManagerImpl {
});

let state_heights = self.list_state_heights(CERT_ANY);
let certified_state_heights = self.list_state_heights(CERT_CERTIFIED);

debug_assert!(checkpoint_heights_to_keep
.iter()
.all(|h| unfiltered_checkpoint_heights.contains(h)));

debug_assert!(heights_to_keep.iter().all(|h| unfiltered_checkpoint_heights
.contains(h)
|| *h == latest_certified_height));
debug_assert!(extra_inmemory_heights_to_keep
.iter()
.all(|h| certified_state_heights.contains(h)));

debug_assert!(state_heights.contains(&latest_state_height));
debug_assert!(state_heights.contains(&latest_certified_height));
Expand Down

0 comments on commit 8f37b43

Please sign in to comment.