2021-10-13 08:24:38 +00:00
|
|
|
use std::fs::File;
|
2021-10-06 05:06:17 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2021-10-15 10:26:33 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
2021-10-13 08:24:38 +00:00
|
|
|
use std::io::{BufRead, BufReader};
|
2021-10-14 14:41:08 +00:00
|
|
|
use std::time::SystemTime;
|
2021-10-15 10:26:33 +00:00
|
|
|
use std::thread::spawn;
|
2021-10-19 16:33:19 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
use std::collections::BTreeSet;
|
|
|
|
|
2021-10-16 10:00:25 +00:00
|
|
|
use crossbeam_channel::{bounded, TryRecvError};
|
2021-10-13 08:24:38 +00:00
|
|
|
use anyhow::{format_err, bail, Error};
|
2021-10-06 05:06:17 +00:00
|
|
|
|
2021-11-23 16:57:00 +00:00
|
|
|
use proxmox_sys::fs::{create_path, CreateOptions};
|
2021-10-06 05:06:17 +00:00
|
|
|
|
2021-10-13 08:24:41 +00:00
|
|
|
use crate::rrd::{DST, CF, RRD, RRA};
|
2021-10-06 05:06:17 +00:00
|
|
|
|
2021-10-16 10:00:25 +00:00
|
|
|
mod journal;
|
|
|
|
use journal::*;
|
2021-10-13 08:24:38 +00:00
|
|
|
|
2021-10-16 10:22:40 +00:00
|
|
|
mod rrd_map;
|
|
|
|
use rrd_map::*;
|
|
|
|
|
2021-10-06 05:06:17 +00:00
|
|
|
/// RRD cache - keep RRD data in RAM, but write updates to disk
|
|
|
|
///
|
|
|
|
/// This cache is designed to run as single instance (no concurrent
|
|
|
|
/// access from other processes).
|
|
|
|
pub struct RRDCache {
|
2021-10-15 10:26:33 +00:00
|
|
|
config: Arc<CacheConfig>,
|
|
|
|
state: Arc<RwLock<JournalState>>,
|
|
|
|
rrd_map: Arc<RwLock<RRDMap>>,
|
|
|
|
}
|
|
|
|
|
2021-10-16 10:00:25 +00:00
|
|
|
pub(crate) struct CacheConfig {
|
2021-10-13 08:24:38 +00:00
|
|
|
apply_interval: f64,
|
2021-10-06 05:06:17 +00:00
|
|
|
basedir: PathBuf,
|
|
|
|
file_options: CreateOptions,
|
2021-10-15 10:26:33 +00:00
|
|
|
dir_options: CreateOptions,
|
2021-10-15 07:22:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 10:26:33 +00:00
|
|
|
|
2021-10-06 05:06:17 +00:00
|
|
|
impl RRDCache {
|
|
|
|
|
|
|
|
/// Creates a new instance
|
2021-10-14 09:41:26 +00:00
|
|
|
///
|
|
|
|
/// `basedir`: All files are stored relative to this path.
|
|
|
|
///
|
|
|
|
/// `file_options`: Files are created with this options.
|
|
|
|
///
|
|
|
|
/// `dir_options`: Directories are created with this options.
|
|
|
|
///
|
|
|
|
/// `apply_interval`: Commit journal after `apply_interval` seconds.
|
|
|
|
///
|
2021-10-14 09:53:54 +00:00
|
|
|
/// `load_rrd_cb`; The callback function is used to load RRD files,
|
2021-10-14 09:41:26 +00:00
|
|
|
/// and should return a newly generated RRD if the file does not
|
|
|
|
/// exists (or is unreadable). This may generate RRDs with
|
|
|
|
/// different configurations (dependent on `rel_path`).
|
2021-10-06 05:06:17 +00:00
|
|
|
pub fn new<P: AsRef<Path>>(
|
|
|
|
basedir: P,
|
|
|
|
file_options: Option<CreateOptions>,
|
|
|
|
dir_options: Option<CreateOptions>,
|
2021-10-13 08:24:38 +00:00
|
|
|
apply_interval: f64,
|
2021-10-15 07:22:07 +00:00
|
|
|
load_rrd_cb: fn(path: &Path, rel_path: &str, dst: DST) -> RRD,
|
2021-10-13 08:24:38 +00:00
|
|
|
) -> Result<Self, Error> {
|
2021-10-06 05:06:17 +00:00
|
|
|
let basedir = basedir.as_ref().to_owned();
|
2021-10-13 08:24:38 +00:00
|
|
|
|
|
|
|
let file_options = file_options.unwrap_or_else(|| CreateOptions::new());
|
|
|
|
let dir_options = dir_options.unwrap_or_else(|| CreateOptions::new());
|
|
|
|
|
|
|
|
create_path(&basedir, Some(dir_options.clone()), Some(dir_options.clone()))
|
|
|
|
.map_err(|err: Error| format_err!("unable to create rrdb stat dir - {}", err))?;
|
|
|
|
|
2021-10-15 10:26:33 +00:00
|
|
|
let config = Arc::new(CacheConfig {
|
2021-10-15 07:22:07 +00:00
|
|
|
basedir: basedir.clone(),
|
|
|
|
file_options: file_options.clone(),
|
|
|
|
dir_options: dir_options,
|
2021-10-15 10:26:33 +00:00
|
|
|
apply_interval,
|
|
|
|
});
|
|
|
|
|
|
|
|
let state = JournalState::new(Arc::clone(&config))?;
|
|
|
|
let rrd_map = RRDMap::new(Arc::clone(&config), load_rrd_cb);
|
2021-10-15 07:22:07 +00:00
|
|
|
|
2021-10-13 08:24:38 +00:00
|
|
|
Ok(Self {
|
2021-10-15 10:26:33 +00:00
|
|
|
config: Arc::clone(&config),
|
|
|
|
state: Arc::new(RwLock::new(state)),
|
|
|
|
rrd_map: Arc::new(RwLock::new(rrd_map)),
|
|
|
|
})
|
2021-10-13 08:24:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 09:41:26 +00:00
|
|
|
/// Create a new RRD as used by the proxmox backup server
|
|
|
|
///
|
|
|
|
/// It contains the following RRAs:
|
|
|
|
///
|
|
|
|
/// * cf=average,r=60,n=1440 => 1day
|
|
|
|
/// * cf=maximum,r=60,n=1440 => 1day
|
|
|
|
/// * cf=average,r=30*60,n=1440 => 1month
|
|
|
|
/// * cf=maximum,r=30*60,n=1440 => 1month
|
|
|
|
/// * cf=average,r=6*3600,n=1440 => 1year
|
|
|
|
/// * cf=maximum,r=6*3600,n=1440 => 1year
|
|
|
|
/// * cf=average,r=7*86400,n=570 => 10years
|
|
|
|
/// * cf=maximum,r=7*86400,n=570 => 10year
|
|
|
|
///
|
|
|
|
/// The resultion data file size is about 80KB.
|
|
|
|
pub fn create_proxmox_backup_default_rrd(dst: DST) -> RRD {
|
2021-10-13 08:24:41 +00:00
|
|
|
|
|
|
|
let mut rra_list = Vec::new();
|
|
|
|
|
|
|
|
// 1min * 1440 => 1day
|
|
|
|
rra_list.push(RRA::new(CF::Average, 60, 1440));
|
|
|
|
rra_list.push(RRA::new(CF::Maximum, 60, 1440));
|
|
|
|
|
|
|
|
// 30min * 1440 => 30days = 1month
|
|
|
|
rra_list.push(RRA::new(CF::Average, 30*60, 1440));
|
|
|
|
rra_list.push(RRA::new(CF::Maximum, 30*60, 1440));
|
|
|
|
|
|
|
|
// 6h * 1440 => 360days = 1year
|
|
|
|
rra_list.push(RRA::new(CF::Average, 6*3600, 1440));
|
|
|
|
rra_list.push(RRA::new(CF::Maximum, 6*3600, 1440));
|
|
|
|
|
|
|
|
// 1week * 570 => 10years
|
|
|
|
rra_list.push(RRA::new(CF::Average, 7*86400, 570));
|
|
|
|
rra_list.push(RRA::new(CF::Maximum, 7*86400, 570));
|
|
|
|
|
|
|
|
RRD::new(dst, rra_list)
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:14:57 +00:00
|
|
|
/// Sync the journal data to disk (using `fdatasync` syscall)
|
2021-10-18 11:45:30 +00:00
|
|
|
pub fn sync_journal(&self) -> Result<(), Error> {
|
|
|
|
self.state.read().unwrap().sync_journal()
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:41:08 +00:00
|
|
|
/// Apply and commit the journal. Should be used at server startup.
|
2021-10-15 10:26:33 +00:00
|
|
|
pub fn apply_journal(&self) -> Result<bool, Error> {
|
2021-10-19 16:33:19 +00:00
|
|
|
let config = Arc::clone(&self.config);
|
2021-10-15 10:26:33 +00:00
|
|
|
let state = Arc::clone(&self.state);
|
|
|
|
let rrd_map = Arc::clone(&self.rrd_map);
|
2021-10-06 05:06:17 +00:00
|
|
|
|
2021-10-18 12:52:27 +00:00
|
|
|
|
2021-10-15 10:26:33 +00:00
|
|
|
let mut state_guard = self.state.write().unwrap();
|
|
|
|
let journal_applied = state_guard.journal_applied;
|
2021-10-13 08:24:38 +00:00
|
|
|
|
2021-10-15 10:26:33 +00:00
|
|
|
if let Some(ref recv) = state_guard.apply_thread_result {
|
|
|
|
match recv.try_recv() {
|
|
|
|
Ok(Ok(())) => {
|
|
|
|
// finished without errors, OK
|
2021-10-18 09:57:19 +00:00
|
|
|
state_guard.apply_thread_result = None;
|
2021-10-15 10:26:33 +00:00
|
|
|
}
|
|
|
|
Ok(Err(err)) => {
|
|
|
|
// finished with errors, log them
|
|
|
|
log::error!("{}", err);
|
2021-10-18 09:57:19 +00:00
|
|
|
state_guard.apply_thread_result = None;
|
2021-10-15 10:26:33 +00:00
|
|
|
}
|
|
|
|
Err(TryRecvError::Empty) => {
|
|
|
|
// still running
|
|
|
|
return Ok(journal_applied);
|
|
|
|
}
|
|
|
|
Err(TryRecvError::Disconnected) => {
|
|
|
|
// crashed, start again
|
|
|
|
log::error!("apply journal thread crashed - try again");
|
2021-10-18 09:57:19 +00:00
|
|
|
state_guard.apply_thread_result = None;
|
2021-10-14 14:41:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 09:57:19 +00:00
|
|
|
let now = proxmox_time::epoch_f64();
|
|
|
|
let wants_commit = (now - state_guard.last_journal_flush) > self.config.apply_interval;
|
|
|
|
|
|
|
|
if journal_applied && !wants_commit { return Ok(journal_applied); }
|
|
|
|
|
2021-10-15 10:26:33 +00:00
|
|
|
state_guard.last_journal_flush = proxmox_time::epoch_f64();
|
2021-10-13 08:24:38 +00:00
|
|
|
|
2021-10-15 10:26:33 +00:00
|
|
|
let (sender, receiver) = bounded(1);
|
|
|
|
state_guard.apply_thread_result = Some(receiver);
|
2021-10-13 08:24:38 +00:00
|
|
|
|
2021-10-15 10:26:33 +00:00
|
|
|
spawn(move || {
|
2021-10-19 16:33:19 +00:00
|
|
|
let result = apply_and_commit_journal_thread(config, state, rrd_map, journal_applied)
|
2021-10-15 10:26:33 +00:00
|
|
|
.map_err(|err| err.to_string());
|
|
|
|
sender.send(result).unwrap();
|
|
|
|
});
|
2021-10-14 14:41:08 +00:00
|
|
|
|
2021-10-15 10:26:33 +00:00
|
|
|
Ok(journal_applied)
|
2021-10-14 14:41:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 05:06:17 +00:00
|
|
|
|
2021-10-13 08:24:46 +00:00
|
|
|
/// Update data in RAM and write file back to disk (journal)
|
2021-10-06 05:06:17 +00:00
|
|
|
pub fn update_value(
|
|
|
|
&self,
|
|
|
|
rel_path: &str,
|
2021-10-14 06:11:04 +00:00
|
|
|
time: f64,
|
2021-10-06 05:06:17 +00:00
|
|
|
value: f64,
|
|
|
|
dst: DST,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
2021-10-15 10:26:33 +00:00
|
|
|
let journal_applied = self.apply_journal()?;
|
2021-10-13 08:24:38 +00:00
|
|
|
|
2021-10-16 10:00:25 +00:00
|
|
|
self.state.write().unwrap()
|
|
|
|
.append_journal_entry(time, value, dst, rel_path)?;
|
2021-10-13 08:24:38 +00:00
|
|
|
|
2021-10-15 10:26:33 +00:00
|
|
|
if journal_applied {
|
|
|
|
self.rrd_map.write().unwrap().update(rel_path, time, value, dst, false)?;
|
|
|
|
}
|
2021-10-06 05:06:17 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extract data from cached RRD
|
2021-10-13 08:24:42 +00:00
|
|
|
///
|
|
|
|
/// `start`: Start time. If not sepecified, we simply extract 10 data points.
|
2021-10-14 09:53:54 +00:00
|
|
|
///
|
2021-10-13 08:24:42 +00:00
|
|
|
/// `end`: End time. Default is to use the current time.
|
2021-10-06 05:06:17 +00:00
|
|
|
pub fn extract_cached_data(
|
|
|
|
&self,
|
|
|
|
base: &str,
|
|
|
|
name: &str,
|
2021-10-13 08:24:42 +00:00
|
|
|
cf: CF,
|
|
|
|
resolution: u64,
|
|
|
|
start: Option<u64>,
|
|
|
|
end: Option<u64>,
|
2021-10-13 08:24:41 +00:00
|
|
|
) -> Result<Option<(u64, u64, Vec<Option<f64>>)>, Error> {
|
2021-10-15 07:22:07 +00:00
|
|
|
self.rrd_map.read().unwrap()
|
|
|
|
.extract_cached_data(base, name, cf, resolution, start, end)
|
2021-10-06 05:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-15 10:26:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
fn apply_and_commit_journal_thread(
|
2021-10-19 16:33:19 +00:00
|
|
|
config: Arc<CacheConfig>,
|
2021-10-15 10:26:33 +00:00
|
|
|
state: Arc<RwLock<JournalState>>,
|
|
|
|
rrd_map: Arc<RwLock<RRDMap>>,
|
|
|
|
commit_only: bool,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
if commit_only {
|
|
|
|
state.write().unwrap().rotate_journal()?; // start new journal, keep old one
|
|
|
|
} else {
|
|
|
|
let start_time = SystemTime::now();
|
|
|
|
log::debug!("applying rrd journal");
|
|
|
|
|
|
|
|
match apply_journal_impl(Arc::clone(&state), Arc::clone(&rrd_map)) {
|
|
|
|
Ok(entries) => {
|
|
|
|
let elapsed = start_time.elapsed().unwrap().as_secs_f64();
|
|
|
|
log::info!("applied rrd journal ({} entries in {:.3} seconds)", entries, elapsed);
|
|
|
|
}
|
|
|
|
Err(err) => bail!("apply rrd journal failed - {}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let start_time = SystemTime::now();
|
|
|
|
log::debug!("commit rrd journal");
|
|
|
|
|
2021-10-19 16:33:19 +00:00
|
|
|
match commit_journal_impl(config, state, rrd_map) {
|
2021-10-15 10:26:33 +00:00
|
|
|
Ok(rrd_file_count) => {
|
|
|
|
let elapsed = start_time.elapsed().unwrap().as_secs_f64();
|
|
|
|
log::info!("rrd journal successfully committed ({} files in {:.3} seconds)",
|
|
|
|
rrd_file_count, elapsed);
|
|
|
|
}
|
|
|
|
Err(err) => bail!("rrd journal commit failed: {}", err),
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_journal_lines(
|
|
|
|
state: Arc<RwLock<JournalState>>,
|
|
|
|
rrd_map: Arc<RwLock<RRDMap>>,
|
|
|
|
journal_name: &str, // used for logging
|
|
|
|
reader: &mut BufReader<File>,
|
|
|
|
lock_read_line: bool,
|
|
|
|
) -> Result<usize, Error> {
|
|
|
|
|
|
|
|
let mut linenr = 0;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
linenr += 1;
|
|
|
|
let mut line = String::new();
|
|
|
|
let len = if lock_read_line {
|
|
|
|
let _lock = state.read().unwrap(); // make sure we read entire lines
|
|
|
|
reader.read_line(&mut line)?
|
|
|
|
} else {
|
|
|
|
reader.read_line(&mut line)?
|
|
|
|
};
|
|
|
|
|
|
|
|
if len == 0 { break; }
|
|
|
|
|
2021-10-19 08:51:22 +00:00
|
|
|
let entry: JournalEntry = match line.parse() {
|
2021-10-15 10:26:33 +00:00
|
|
|
Ok(entry) => entry,
|
|
|
|
Err(err) => {
|
|
|
|
log::warn!(
|
|
|
|
"unable to parse rrd journal '{}' line {} (skip) - {}",
|
|
|
|
journal_name, linenr, err,
|
|
|
|
);
|
|
|
|
continue; // skip unparsable lines
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
rrd_map.write().unwrap().update(&entry.rel_path, entry.time, entry.value, entry.dst, true)?;
|
|
|
|
}
|
|
|
|
Ok(linenr)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_journal_impl(
|
|
|
|
state: Arc<RwLock<JournalState>>,
|
|
|
|
rrd_map: Arc<RwLock<RRDMap>>,
|
|
|
|
) -> Result<usize, Error> {
|
|
|
|
|
|
|
|
let mut lines = 0;
|
|
|
|
|
|
|
|
// Apply old journals first
|
|
|
|
let journal_list = state.read().unwrap().list_old_journals()?;
|
|
|
|
|
2021-10-16 10:38:34 +00:00
|
|
|
for entry in journal_list {
|
|
|
|
log::info!("apply old journal log {}", entry.name);
|
|
|
|
let file = std::fs::OpenOptions::new().read(true).open(&entry.path)?;
|
2021-10-15 10:26:33 +00:00
|
|
|
let mut reader = BufReader::new(file);
|
|
|
|
lines += apply_journal_lines(
|
|
|
|
Arc::clone(&state),
|
|
|
|
Arc::clone(&rrd_map),
|
2021-10-16 10:38:34 +00:00
|
|
|
&entry.name,
|
2021-10-15 10:26:33 +00:00
|
|
|
&mut reader,
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut journal = state.read().unwrap().open_journal_reader()?;
|
|
|
|
|
|
|
|
lines += apply_journal_lines(
|
|
|
|
Arc::clone(&state),
|
|
|
|
Arc::clone(&rrd_map),
|
|
|
|
"rrd.journal",
|
|
|
|
&mut journal,
|
|
|
|
true,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut state_guard = state.write().unwrap(); // block other writers
|
|
|
|
|
|
|
|
lines += apply_journal_lines(
|
|
|
|
Arc::clone(&state),
|
|
|
|
Arc::clone(&rrd_map),
|
|
|
|
"rrd.journal",
|
|
|
|
&mut journal,
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
state_guard.rotate_journal()?; // start new journal, keep old one
|
|
|
|
|
|
|
|
// We need to apply the journal only once, because further updates
|
|
|
|
// are always directly applied.
|
|
|
|
state_guard.journal_applied = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok(lines)
|
|
|
|
}
|
|
|
|
|
2021-10-19 16:33:19 +00:00
|
|
|
fn fsync_file_or_dir(path: &Path) -> Result<(), Error> {
|
|
|
|
let file = std::fs::File::open(path)?;
|
|
|
|
nix::unistd::fsync(file.as_raw_fd())?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate)fn fsync_file_and_parent(path: &Path) -> Result<(), Error> {
|
|
|
|
let file = std::fs::File::open(path)?;
|
|
|
|
nix::unistd::fsync(file.as_raw_fd())?;
|
|
|
|
if let Some(parent) = path.parent() {
|
|
|
|
fsync_file_or_dir(parent)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rrd_parent_dir(basedir: &Path, rel_path: &str) -> PathBuf {
|
|
|
|
let mut path = basedir.to_owned();
|
|
|
|
let rel_path = Path::new(rel_path);
|
|
|
|
if let Some(parent) = rel_path.parent() {
|
|
|
|
path.push(parent);
|
|
|
|
}
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2021-10-15 10:26:33 +00:00
|
|
|
fn commit_journal_impl(
|
2021-10-19 16:33:19 +00:00
|
|
|
config: Arc<CacheConfig>,
|
2021-10-15 10:26:33 +00:00
|
|
|
state: Arc<RwLock<JournalState>>,
|
|
|
|
rrd_map: Arc<RwLock<RRDMap>>,
|
|
|
|
) -> Result<usize, Error> {
|
|
|
|
|
2021-10-18 12:52:27 +00:00
|
|
|
let files = rrd_map.read().unwrap().file_list();
|
|
|
|
|
|
|
|
let mut rrd_file_count = 0;
|
|
|
|
let mut errors = 0;
|
|
|
|
|
2021-10-19 16:33:19 +00:00
|
|
|
let mut dir_set = BTreeSet::new();
|
|
|
|
|
|
|
|
log::info!("write rrd data back to disk");
|
|
|
|
|
2021-10-15 10:26:33 +00:00
|
|
|
// save all RRDs - we only need a read lock here
|
2021-10-19 16:33:19 +00:00
|
|
|
// Note: no fsync here (we do it afterwards)
|
2021-10-18 12:52:27 +00:00
|
|
|
for rel_path in files.iter() {
|
2021-12-30 11:57:37 +00:00
|
|
|
let parent_dir = rrd_parent_dir(&config.basedir, rel_path);
|
2021-10-19 16:33:19 +00:00
|
|
|
dir_set.insert(parent_dir);
|
2021-10-18 12:52:27 +00:00
|
|
|
rrd_file_count += 1;
|
2021-12-30 11:57:37 +00:00
|
|
|
if let Err(err) = rrd_map.read().unwrap().flush_rrd_file(rel_path) {
|
2021-10-18 12:52:27 +00:00
|
|
|
errors += 1;
|
|
|
|
log::error!("unable to save rrd {}: {}", rel_path, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors != 0 {
|
|
|
|
bail!("errors during rrd flush - unable to commit rrd journal");
|
|
|
|
}
|
2021-10-15 10:26:33 +00:00
|
|
|
|
2021-10-19 16:33:19 +00:00
|
|
|
// Important: We fsync files after writing all data! This increase
|
|
|
|
// the likelihood that files are already synced, so this is
|
|
|
|
// much faster (although we need to re-open the files).
|
|
|
|
|
|
|
|
log::info!("starting rrd data sync");
|
|
|
|
|
|
|
|
for rel_path in files.iter() {
|
|
|
|
let mut path = config.basedir.clone();
|
|
|
|
path.push(&rel_path);
|
|
|
|
fsync_file_or_dir(&path)
|
|
|
|
.map_err(|err| format_err!("fsync rrd file {} failed - {}", rel_path, err))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// also fsync directories
|
|
|
|
for dir_path in dir_set {
|
|
|
|
fsync_file_or_dir(&dir_path)
|
|
|
|
.map_err(|err| format_err!("fsync rrd dir {:?} failed - {}", dir_path, err))?;
|
|
|
|
}
|
|
|
|
|
2021-10-15 10:26:33 +00:00
|
|
|
// if everything went ok, remove the old journal files
|
|
|
|
state.write().unwrap().remove_old_journals()?;
|
|
|
|
|
|
|
|
Ok(rrd_file_count)
|
|
|
|
}
|