diff --git a/proxmox-rrd/Cargo.toml b/proxmox-rrd/Cargo.toml index 900b8fef..67095689 100644 --- a/proxmox-rrd/Cargo.toml +++ b/proxmox-rrd/Cargo.toml @@ -12,6 +12,7 @@ proxmox-router = "1.1" anyhow = "1.0" bitflags = "1.2.1" crossbeam-channel = "0.5" +libc = "0.2" log = "0.4" nix = "0.19.1" serde = { version = "1.0", features = ["derive"] } diff --git a/proxmox-rrd/src/cache.rs b/proxmox-rrd/src/cache.rs index faeae89f..547f6603 100644 --- a/proxmox-rrd/src/cache.rs +++ b/proxmox-rrd/src/cache.rs @@ -147,6 +147,10 @@ impl RRDCache { Ok(JournalEntry { time, value, dst, rel_path }) } + pub fn sync_journal(&self) -> Result<(), Error> { + self.state.read().unwrap().sync_journal() + } + /// Apply and commit the journal. Should be used at server startup. pub fn apply_journal(&self) -> Result { let state = Arc::clone(&self.state); @@ -387,6 +391,8 @@ fn commit_journal_impl( } } + state.read().unwrap().syncfs()?; + if errors != 0 { bail!("errors during rrd flush - unable to commit rrd journal"); } diff --git a/proxmox-rrd/src/cache/journal.rs b/proxmox-rrd/src/cache/journal.rs index 592e18ec..3514e9e7 100644 --- a/proxmox-rrd/src/cache/journal.rs +++ b/proxmox-rrd/src/cache/journal.rs @@ -3,6 +3,7 @@ use std::path::PathBuf; use std::sync::Arc; use std::io::{Write, BufReader}; use std::ffi::OsStr; +use std::os::unix::io::AsRawFd; use anyhow::Error; use nix::fcntl::OFlag; @@ -50,6 +51,16 @@ impl JournalState { }) } + pub fn sync_journal(&self) -> Result<(), Error> { + nix::unistd::fdatasync(self.journal.as_raw_fd())?; + Ok(()) + } + + pub fn syncfs(&self) -> Result<(), nix::Error> { + let res = unsafe { libc::syncfs(self.journal.as_raw_fd()) }; + nix::errno::Errno::result(res).map(drop) + } + pub fn append_journal_entry( &mut self, time: f64, diff --git a/proxmox-rrd/src/rrd.rs b/proxmox-rrd/src/rrd.rs index 3550e30a..15d73856 100644 --- a/proxmox-rrd/src/rrd.rs +++ b/proxmox-rrd/src/rrd.rs @@ -14,7 +14,6 @@ use std::path::Path; use anyhow::{bail, format_err, Error}; - use serde::{Serialize, Deserialize}; use proxmox::tools::fs::{replace_file, CreateOptions};