Skip to content

Commit

Permalink
Merge pull request #1549 from Phala-Network/remove-uncompleted-checkp…
Browse files Browse the repository at this point in the history
…oint

pRuntime: remove uncompleted checkpoint
  • Loading branch information
nanometerzhu authored Mar 11, 2024
2 parents f3bc4c3 + 01ecf72 commit 1dd8911
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions crates/phactory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ fn remove_outdated_checkpoints(
) -> Result<()> {
let mut kept = 0_u32;
for (block, filename) in glob_checkpoint_files_sorted(basedir)? {
if remove_checkpoint_if_uncompleted(&filename)? {
continue;
}
if block > current_block {
continue;
}
Expand All @@ -225,6 +228,15 @@ fn remove_checkpoint(filename: &Path) -> Result<()> {
Ok(())
}

fn remove_checkpoint_if_uncompleted(filename: &Path) -> Result<bool> {
let info_filename = checkpoint_info_filename_for(&filename.display().to_string());
if !Path::new(&info_filename).exists() {
std::fs::remove_file(filename).context("Failed to remove uncompleted checkpoint file")?;
return Ok(true);
}
Ok(false)
}

#[derive(Encode, Decode, Clone, Debug)]
struct PersistentRuntimeData {
genesis_block_hash: H256,
Expand Down Expand Up @@ -806,10 +818,10 @@ impl<Platform: pal::Platform + Serialize + DeserializeOwned> Phactory<Platform>
.dump_secret_key();
let checkpoint_file = checkpoint_filename_for(current_block, &self.args.storage_path);
info!("Taking checkpoint to {checkpoint_file}...");
self.save_checkpoint_info(&checkpoint_file)?;
let file = File::create(&checkpoint_file).context("Failed to create checkpoint file")?;
self.take_checkpoint_to_writer(&key, file)
.context("Take checkpoint to writer failed")?;
self.save_checkpoint_info(&checkpoint_file)?;
info!("Checkpoint saved to {checkpoint_file}");
self.last_checkpoint = Instant::now();
remove_outdated_checkpoints(
Expand Down Expand Up @@ -858,7 +870,19 @@ impl<Platform: pal::Platform + Serialize + DeserializeOwned> Phactory<Platform>
other => other.context("Failed to load persistent data")?,
};
let files = glob_checkpoint_files_sorted(&args.storage_path)
.context("Glob checkpoint files failed")?;
.context("Glob checkpoint files failed")?
.into_iter()
.filter(|(_block, ckpt_filename)| {
let removed = remove_checkpoint_if_uncompleted(ckpt_filename);
match removed {
Ok(removed) => !removed,
Err(err) => {
error!("Met error when removing checkpoint if uncompleted: {err:?}");
false
},
}
})
.collect::<Vec<(u32, PathBuf)>>();
if files.is_empty() {
return Ok(None);
}
Expand Down

0 comments on commit 1dd8911

Please sign in to comment.