improve data sync reliability to underlying storage medium

This commit is contained in:
Aiden McClelland
2022-02-25 13:13:20 -07:00
committed by Aiden McClelland
parent 690324e68d
commit f84637b89d

View File

@@ -83,8 +83,7 @@ impl Store {
backup_file.flush()?; backup_file.flush()?;
backup_file.sync_all()?; backup_file.sync_all()?;
std::fs::rename(&bak_tmp, &bak)?; std::fs::rename(&bak_tmp, &bak)?;
nix::unistd::ftruncate(std::os::unix::io::AsRawFd::as_raw_fd(&*f), 0) f.set_len(0)?;
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
serde_cbor::to_writer(&mut *f, &revision)?; serde_cbor::to_writer(&mut *f, &revision)?;
serde_cbor::to_writer(&mut *f, &data)?; serde_cbor::to_writer(&mut *f, &data)?;
f.flush()?; f.flush()?;
@@ -187,7 +186,7 @@ impl Store {
async fn sync_to_disk(file: &mut File, patch_bin: &[u8]) -> Result<(), IOError> { async fn sync_to_disk(file: &mut File, patch_bin: &[u8]) -> Result<(), IOError> {
file.write_all(patch_bin).await?; file.write_all(patch_bin).await?;
file.flush().await?; file.flush().await?;
file.sync_data().await?; file.sync_all().await?;
Ok(()) Ok(())
} }
if let Err(e) = sync_to_disk(&mut *self.file, &patch_bin).await { if let Err(e) = sync_to_disk(&mut *self.file, &patch_bin).await {