Skip to content

Commit

Permalink
A0-2244: Remove old junk backups (#1490)
Browse files Browse the repository at this point in the history
### Description
Changed the backup removal code to remove everything from the backup
stash folder except the subfolder corresponding to the current session.

### Testing
Tested manually - ran for many short sessions, verified finalization,
verified backup folder contents
  • Loading branch information
woocash2 authored Nov 15, 2023
1 parent 3c1c12d commit 623e477
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
39 changes: 22 additions & 17 deletions finality-aleph/src/party/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
str::FromStr,
};

use log::{debug, warn};
use log::debug;

const BACKUP_FILE_EXTENSION: &str = ".abfts";

Expand Down Expand Up @@ -118,27 +118,32 @@ pub fn rotate(
Ok((backup_saver, backup_loader))
}

/// Removes the backup directory for a session.
/// Removes the backup directory for all old sessions except the current session.
///
/// `backup_path` is the path to the backup directory (i.e. the argument to `--backup-saving-path`).
/// If it is `None`, nothing is done.
///
/// Any filesystem errors are logged and dropped.
/// Any filesystem errors are returned.
///
/// This should be done after the end of the session.
pub fn remove(path: Option<PathBuf>, session_id: u32) {
let path = match path {
Some(path) => path.join(session_id.to_string()),
None => return,
};
match fs::remove_dir_all(path) {
Ok(()) => {
debug!(target: "aleph-party", "Removed backup for session {}", session_id);
}
Err(err) => {
if err.kind() != io::ErrorKind::NotFound {
warn!(target: "aleph-party", "Error cleaning up backup for session {}: {}", session_id, err);
}
/// This should be done at the beginning of the new session.
pub fn remove_old_backups(path: Option<PathBuf>, current_session: u32) -> io::Result<()> {
if let Some(path) = path {
for read_dir in fs::read_dir(path)? {
let item = read_dir?;
match item.file_name().to_str() {
Some(name) => match name.parse::<u32>() {
Ok(session_id) => {
if session_id < current_session {
fs::remove_dir_all(item.path())?;
}
}
Err(_) => {
debug!(target: "aleph-party", "backup directory contains unexpected data.")
}
},
None => debug!(target: "aleph-party", "backup directory contains unexpected data."),
};
}
}
Ok(())
}
8 changes: 6 additions & 2 deletions finality-aleph/src/party/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ where

async fn run_session(&mut self, session_id: SessionId) {
let last_block = self.session_info.last_block_of_session(session_id);
if let Some(previous_session_id) = session_id.0.checked_sub(1) {
if session_id.0.checked_sub(1).is_some() {
let backup_saving_path = self.backup_saving_path.clone();
spawn_blocking(move || backup::remove(backup_saving_path, previous_session_id));
spawn_blocking(move || {
if let Err(e) = backup::remove_old_backups(backup_saving_path, session_id.0) {
warn!(target: "aleph-party", "Error when clearing old backups: {}", e);
}
});
}

// Early skip attempt -- this will trigger during catching up (initial sync).
Expand Down

0 comments on commit 623e477

Please sign in to comment.