2 use std
::path
::{Path, PathBuf}
;
3 use std
::sync
::{Arc, RwLock}
;
4 use std
::io
::{BufRead, BufReader}
;
5 use std
::time
::SystemTime
;
6 use std
::thread
::spawn
;
7 use std
::os
::unix
::io
::AsRawFd
;
8 use std
::collections
::BTreeSet
;
10 use crossbeam_channel
::{bounded, TryRecvError}
;
11 use anyhow
::{format_err, bail, Error}
;
13 use proxmox_sys
::fs
::{create_path, CreateOptions}
;
15 use crate::rrd
::{DST, CF, RRD, RRA}
;
23 /// RRD cache - keep RRD data in RAM, but write updates to disk
25 /// This cache is designed to run as single instance (no concurrent
26 /// access from other processes).
28 config
: Arc
<CacheConfig
>,
29 state
: Arc
<RwLock
<JournalState
>>,
30 rrd_map
: Arc
<RwLock
<RRDMap
>>,
33 pub(crate) struct CacheConfig
{
36 file_options
: CreateOptions
,
37 dir_options
: CreateOptions
,
43 /// Creates a new instance
45 /// `basedir`: All files are stored relative to this path.
47 /// `file_options`: Files are created with this options.
49 /// `dir_options`: Directories are created with this options.
51 /// `apply_interval`: Commit journal after `apply_interval` seconds.
53 /// `load_rrd_cb`; The callback function is used to load RRD files,
54 /// and should return a newly generated RRD if the file does not
55 /// exists (or is unreadable). This may generate RRDs with
56 /// different configurations (dependent on `rel_path`).
57 pub fn new
<P
: AsRef
<Path
>>(
59 file_options
: Option
<CreateOptions
>,
60 dir_options
: Option
<CreateOptions
>,
62 load_rrd_cb
: fn(path
: &Path
, rel_path
: &str, dst
: DST
) -> RRD
,
63 ) -> Result
<Self, Error
> {
64 let basedir
= basedir
.as_ref().to_owned();
66 let file_options
= file_options
.unwrap_or_else(CreateOptions
::new
);
67 let dir_options
= dir_options
.unwrap_or_else(CreateOptions
::new
);
69 create_path(&basedir
, Some(dir_options
.clone()), Some(dir_options
.clone()))
70 .map_err(|err
: Error
| format_err
!("unable to create rrdb stat dir - {}", err
))?
;
72 let config
= Arc
::new(CacheConfig
{
79 let state
= JournalState
::new(Arc
::clone(&config
))?
;
80 let rrd_map
= RRDMap
::new(Arc
::clone(&config
), load_rrd_cb
);
83 config
: Arc
::clone(&config
),
84 state
: Arc
::new(RwLock
::new(state
)),
85 rrd_map
: Arc
::new(RwLock
::new(rrd_map
)),
89 /// Create a new RRD as used by the proxmox backup server
91 /// It contains the following RRAs:
93 /// * cf=average,r=60,n=1440 => 1day
94 /// * cf=maximum,r=60,n=1440 => 1day
95 /// * cf=average,r=30*60,n=1440 => 1month
96 /// * cf=maximum,r=30*60,n=1440 => 1month
97 /// * cf=average,r=6*3600,n=1440 => 1year
98 /// * cf=maximum,r=6*3600,n=1440 => 1year
99 /// * cf=average,r=7*86400,n=570 => 10years
100 /// * cf=maximum,r=7*86400,n=570 => 10year
102 /// The resultion data file size is about 80KB.
103 pub fn create_proxmox_backup_default_rrd(dst
: DST
) -> RRD
{
105 // 1 min * 1440 => 1 day
106 RRA
::new(CF
::Average
, 60, 1440),
107 RRA
::new(CF
::Maximum
, 60, 1440),
108 // 30 min * 1440 => 30 days ~ 1 month
109 RRA
::new(CF
::Average
, 30 * 60, 1440),
110 RRA
::new(CF
::Maximum
, 30 * 60, 1440),
111 // 6 h * 1440 => 360 days ~ 1 year
112 RRA
::new(CF
::Average
, 6 * 3600, 1440),
113 RRA
::new(CF
::Maximum
, 6 * 3600, 1440),
114 // 1 week * 570 => 10 years
115 RRA
::new(CF
::Average
, 7 * 86400, 570),
116 RRA
::new(CF
::Maximum
, 7 * 86400, 570),
119 RRD
::new(dst
, rra_list
)
122 /// Sync the journal data to disk (using `fdatasync` syscall)
123 pub fn sync_journal(&self) -> Result
<(), Error
> {
124 self.state
.read().unwrap().sync_journal()
127 /// Apply and commit the journal. Should be used at server startup.
128 pub fn apply_journal(&self) -> Result
<bool
, Error
> {
129 let config
= Arc
::clone(&self.config
);
130 let state
= Arc
::clone(&self.state
);
131 let rrd_map
= Arc
::clone(&self.rrd_map
);
134 let mut state_guard
= self.state
.write().unwrap();
135 let journal_applied
= state_guard
.journal_applied
;
137 if let Some(ref recv
) = state_guard
.apply_thread_result
{
138 match recv
.try_recv() {
140 // finished without errors, OK
141 state_guard
.apply_thread_result
= None
;
144 // finished with errors, log them
145 log
::error
!("{}", err
);
146 state_guard
.apply_thread_result
= None
;
148 Err(TryRecvError
::Empty
) => {
150 return Ok(journal_applied
);
152 Err(TryRecvError
::Disconnected
) => {
153 // crashed, start again
154 log
::error
!("apply journal thread crashed - try again");
155 state_guard
.apply_thread_result
= None
;
160 let now
= proxmox_time
::epoch_f64();
161 let wants_commit
= (now
- state_guard
.last_journal_flush
) > self.config
.apply_interval
;
163 if journal_applied
&& !wants_commit { return Ok(journal_applied); }
165 state_guard
.last_journal_flush
= proxmox_time
::epoch_f64();
167 let (sender
, receiver
) = bounded(1);
168 state_guard
.apply_thread_result
= Some(receiver
);
171 let result
= apply_and_commit_journal_thread(config
, state
, rrd_map
, journal_applied
)
172 .map_err(|err
| err
.to_string());
173 sender
.send(result
).unwrap();
180 /// Update data in RAM and write file back to disk (journal)
187 ) -> Result
<(), Error
> {
189 let journal_applied
= self.apply_journal()?
;
191 self.state
.write().unwrap()
192 .append_journal_entry(time
, value
, dst
, rel_path
)?
;
195 self.rrd_map
.write().unwrap().update(rel_path
, time
, value
, dst
, false)?
;
201 /// Extract data from cached RRD
203 /// `start`: Start time. If not sepecified, we simply extract 10 data points.
205 /// `end`: End time. Default is to use the current time.
206 pub fn extract_cached_data(
214 ) -> Result
<Option
<(u64, u64, Vec
<Option
<f64>>)>, Error
> {
215 self.rrd_map
.read().unwrap()
216 .extract_cached_data(base
, name
, cf
, resolution
, start
, end
)
221 fn apply_and_commit_journal_thread(
222 config
: Arc
<CacheConfig
>,
223 state
: Arc
<RwLock
<JournalState
>>,
224 rrd_map
: Arc
<RwLock
<RRDMap
>>,
226 ) -> Result
<(), Error
> {
229 state
.write().unwrap().rotate_journal()?
; // start new journal, keep old one
231 let start_time
= SystemTime
::now();
232 log
::debug
!("applying rrd journal");
234 match apply_journal_impl(Arc
::clone(&state
), Arc
::clone(&rrd_map
)) {
236 let elapsed
= start_time
.elapsed().unwrap().as_secs_f64();
237 log
::info
!("applied rrd journal ({} entries in {:.3} seconds)", entries
, elapsed
);
239 Err(err
) => bail
!("apply rrd journal failed - {}", err
),
243 let start_time
= SystemTime
::now();
244 log
::debug
!("commit rrd journal");
246 match commit_journal_impl(config
, state
, rrd_map
) {
247 Ok(rrd_file_count
) => {
248 let elapsed
= start_time
.elapsed().unwrap().as_secs_f64();
249 log
::info
!("rrd journal successfully committed ({} files in {:.3} seconds)",
250 rrd_file_count
, elapsed
);
252 Err(err
) => bail
!("rrd journal commit failed: {}", err
),
257 fn apply_journal_lines(
258 state
: Arc
<RwLock
<JournalState
>>,
259 rrd_map
: Arc
<RwLock
<RRDMap
>>,
260 journal_name
: &str, // used for logging
261 reader
: &mut BufReader
<File
>,
262 lock_read_line
: bool
,
263 ) -> Result
<usize, Error
> {
269 let mut line
= String
::new();
270 let len
= if lock_read_line
{
271 let _lock
= state
.read().unwrap(); // make sure we read entire lines
272 reader
.read_line(&mut line
)?
274 reader
.read_line(&mut line
)?
277 if len
== 0 { break; }
279 let entry
: JournalEntry
= match line
.parse() {
283 "unable to parse rrd journal '{}' line {} (skip) - {}",
284 journal_name
, linenr
, err
,
286 continue; // skip unparsable lines
290 rrd_map
.write().unwrap().update(&entry
.rel_path
, entry
.time
, entry
.value
, entry
.dst
, true)?
;
295 fn apply_journal_impl(
296 state
: Arc
<RwLock
<JournalState
>>,
297 rrd_map
: Arc
<RwLock
<RRDMap
>>,
298 ) -> Result
<usize, Error
> {
302 // Apply old journals first
303 let journal_list
= state
.read().unwrap().list_old_journals()?
;
305 for entry
in journal_list
{
306 log
::info
!("apply old journal log {}", entry
.name
);
307 let file
= std
::fs
::OpenOptions
::new().read(true).open(&entry
.path
)?
;
308 let mut reader
= BufReader
::new(file
);
309 lines
+= apply_journal_lines(
311 Arc
::clone(&rrd_map
),
318 let mut journal
= state
.read().unwrap().open_journal_reader()?
;
320 lines
+= apply_journal_lines(
322 Arc
::clone(&rrd_map
),
329 let mut state_guard
= state
.write().unwrap(); // block other writers
331 lines
+= apply_journal_lines(
333 Arc
::clone(&rrd_map
),
339 state_guard
.rotate_journal()?
; // start new journal, keep old one
341 // We need to apply the journal only once, because further updates
342 // are always directly applied.
343 state_guard
.journal_applied
= true;
350 fn fsync_file_or_dir(path
: &Path
) -> Result
<(), Error
> {
351 let file
= std
::fs
::File
::open(path
)?
;
352 nix
::unistd
::fsync(file
.as_raw_fd())?
;
356 pub(crate)fn fsync_file_and_parent(path
: &Path
) -> Result
<(), Error
> {
357 let file
= std
::fs
::File
::open(path
)?
;
358 nix
::unistd
::fsync(file
.as_raw_fd())?
;
359 if let Some(parent
) = path
.parent() {
360 fsync_file_or_dir(parent
)?
;
365 fn rrd_parent_dir(basedir
: &Path
, rel_path
: &str) -> PathBuf
{
366 let mut path
= basedir
.to_owned();
367 let rel_path
= Path
::new(rel_path
);
368 if let Some(parent
) = rel_path
.parent() {
374 fn commit_journal_impl(
375 config
: Arc
<CacheConfig
>,
376 state
: Arc
<RwLock
<JournalState
>>,
377 rrd_map
: Arc
<RwLock
<RRDMap
>>,
378 ) -> Result
<usize, Error
> {
380 let files
= rrd_map
.read().unwrap().file_list();
382 let mut rrd_file_count
= 0;
385 let mut dir_set
= BTreeSet
::new();
387 log
::info
!("write rrd data back to disk");
389 // save all RRDs - we only need a read lock here
390 // Note: no fsync here (we do it afterwards)
391 for rel_path
in files
.iter() {
392 let parent_dir
= rrd_parent_dir(&config
.basedir
, rel_path
);
393 dir_set
.insert(parent_dir
);
395 if let Err(err
) = rrd_map
.read().unwrap().flush_rrd_file(rel_path
) {
397 log
::error
!("unable to save rrd {}: {}", rel_path
, err
);
402 bail
!("errors during rrd flush - unable to commit rrd journal");
405 // Important: We fsync files after writing all data! This increase
406 // the likelihood that files are already synced, so this is
407 // much faster (although we need to re-open the files).
409 log
::info
!("starting rrd data sync");
411 for rel_path
in files
.iter() {
412 let mut path
= config
.basedir
.clone();
413 path
.push(&rel_path
);
414 fsync_file_or_dir(&path
)
415 .map_err(|err
| format_err
!("fsync rrd file {} failed - {}", rel_path
, err
))?
;
418 // also fsync directories
419 for dir_path
in dir_set
{
420 fsync_file_or_dir(&dir_path
)
421 .map_err(|err
| format_err
!("fsync rrd dir {:?} failed - {}", dir_path
, err
))?
;
424 // if everything went ok, remove the old journal files
425 state
.write().unwrap().remove_old_journals()?
;