Skip to content

fix(gc): backfill new db records since snapshot epoch #5640

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 13 commits into from
May 23, 2025

Conversation

hanabi1224
Copy link
Contributor

@hanabi1224 hanabi1224 commented May 12, 2025

Summary of changes

This PR patches the snapshot GC to avoid losing db records that are inserted during snapshot export

Changes introduced in this pull request:

Reference issue to close (if applicable)

Closes

Other information and links

Change checklist

  • I have performed a self-review of my own code,
  • I have made corresponding changes to the documentation. All new code adheres to the team's documentation standards,
  • I have added tests that prove my fix is effective or that my feature works (if possible),
  • I have made sure the CHANGELOG is up-to-date. All user-facing changes should be reflected in this document.

if let Err(e) = db.put_many_keyed(mem_db) {
tracing::warn!("{e}");
}
tracing::info!(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sample log from my laptop on mainnet:

2025-05-12T09:19:35.957856Z  INFO forest::db::gc::snapshot: backfilled 57345 new db records since snapshot epoch, approximate heap size: 82.9 MiB, took 559ms 304us 167ns

@hanabi1224 hanabi1224 force-pushed the hm/backfill-db-records-since-snapshot-epoch branch from 61cc52d to e694b48 Compare May 12, 2025 12:52
@hanabi1224 hanabi1224 marked this pull request as ready for review May 12, 2025 14:07
@hanabi1224 hanabi1224 requested a review from a team as a code owner May 12, 2025 14:07
@hanabi1224 hanabi1224 requested review from elmattic and sudo-shashank and removed request for a team May 12, 2025 14:07

if let Err(e) = self.reboot_tx.send(()) {
tracing::warn!("{e}");
}

*self.memory_db_head_key.write() = db.heaviest_tipset_key().ok();
tokio::time::sleep(Duration::from_secs(1)).await;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 1s? Why do we need to sleep?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was to ensure write_ops_rx to be fully read but it was hacky. This commit refactors the logic and removes the sleep hack.

Comment on lines 281 to 297
// Backfill new db records during snapshot export
if let Ok(db) = open_db(self.db_root_dir.clone(), &self.db_config) {
if let Some(mem_db) = self.memory_db.write().take() {
let count = mem_db.len();
let approximate_heap_size = {
let mut s = 0;
for (_k, v) in mem_db.iter() {
s += 64;
s += v.len();
}
s
};
let start = Instant::now();
if let Err(e) = db.put_many_keyed(mem_db) {
tracing::warn!("{e}");
}
tracing::info!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use more meaningful variable names. Especially the meaning of s is not clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

let approximate_heap_size = {
let mut s = 0;
for (_k, v) in mem_db.iter() {
s += 64;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is 64?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the size of Cid

pub type Cid = CidGeneric<64>;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it's better to use std::mem::size_of::<Cid>()

blessed_lite_snapshot: RwLock<Option<PathBuf>>,
db: RwLock<Option<Arc<DB>>>,
// On mainnet, it takes ~50MiB-100MiB RAM
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can mention this in the gc guide also

@LesnyRumcajs
Copy link
Member

@hanabi1224?

@LesnyRumcajs LesnyRumcajs added this pull request to the merge queue May 23, 2025
Merged via the queue into main with commit f1bb967 May 23, 2025
34 checks passed
@LesnyRumcajs LesnyRumcajs deleted the hm/backfill-db-records-since-snapshot-epoch branch May 23, 2025 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants