state and dbs: backups

This commit is contained in:
bitful-pannul 2023-12-26 13:29:29 -03:00
parent 8e633809b4
commit a45d14470d
3 changed files with 31 additions and 13 deletions

View File

@ -267,8 +267,11 @@ async fn handle_request(
} }
} }
KvAction::Backup => { KvAction::Backup => {
// loop through all db directories and backup. // looping through open dbs and flushing their memtables
// for db_ref in open_kvs.iter() {
let db = db_ref.value();
db.flush()?;
}
(serde_json::to_vec(&KvResponse::Ok).unwrap(), None) (serde_json::to_vec(&KvResponse::Ok).unwrap(), None)
} }
}; };

View File

@ -448,10 +448,11 @@ async fn check_caps(
Ok(()) Ok(())
} }
SqliteAction::Backup => { SqliteAction::Backup => {
if source.process != *STATE_PROCESS_ID { // flushing WALs for backup
return Err(SqliteError::NoCap { // check caps.
error: request.action.to_string(), for db_ref in open_dbs.iter() {
}); let db = db_ref.value().lock().await;
db.execute("pragma wal_checkpoint", [])?;
} }
Ok(()) Ok(())
} }

View File

@ -218,15 +218,21 @@ async fn handle_request(
} }
} }
StateAction::Backup => { StateAction::Backup => {
// handle Backup action let checkpoint_dir = format!("{}/vfs/kernel_backup", &home_directory_path);
println!("got backup");
let checkpoint_dir = format!("{}/kernel/checkpoint", &home_directory_path);
if Path::new(&checkpoint_dir).exists() { if Path::new(&checkpoint_dir).exists() {
let _ = fs::remove_dir_all(&checkpoint_dir).await; fs::remove_dir_all(&checkpoint_dir).await?;
} }
let checkpoint = Checkpoint::new(&db).unwrap(); let checkpoint = Checkpoint::new(&db).map_err(|e| StateError::RocksDBError {
checkpoint.create_checkpoint(&checkpoint_dir).unwrap(); action: "BackupCheckpointNew".into(),
error: e.to_string(),
})?;
checkpoint.create_checkpoint(&checkpoint_dir).map_err(|e| StateError::RocksDBError {
action: "BackupCheckpointCreate".into(),
error: e.to_string(),
})?;
(serde_json::to_vec(&StateResponse::Backup).unwrap(), None) (serde_json::to_vec(&StateResponse::Backup).unwrap(), None)
} }
}; };
@ -609,6 +615,14 @@ async fn get_zipped_packages() -> Vec<(String, zip::ZipArchive<std::io::Cursor<&
packages packages
} }
impl From<std::io::Error> for StateError {
fn from(err: std::io::Error) -> Self {
StateError::IOError {
error: err.to_string(),
}
}
}
fn make_error_message(our_name: String, km: &KernelMessage, error: StateError) -> KernelMessage { fn make_error_message(our_name: String, km: &KernelMessage, error: StateError) -> KernelMessage {
KernelMessage { KernelMessage {
id: km.id, id: km.id,