datastore: move manifest locking into BackupDir impl

the manifest is owned by the backup dir (snapshot) so it should also
handle locking, makes no sense to have the implementation somewhere
higher up.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht
2022-04-24 18:37:15 +02:00
parent b298e9f16e
commit 5c9c23b6b2
2 changed files with 30 additions and 40 deletions

View File

@ -3,11 +3,12 @@ use std::os::unix::io::RawFd;
use std::path::PathBuf;
use std::sync::Arc;
use anyhow::{bail, Error};
use anyhow::{bail, format_err, Error};
use pbs_api_types::{BackupType, GroupFilter, BACKUP_DATE_REGEX, BACKUP_FILE_REGEX};
use pbs_config::{open_backup_lockfile, BackupLockGuard};
use crate::manifest::MANIFEST_BLOB_NAME;
use crate::manifest::{MANIFEST_BLOB_NAME, MANIFEST_LOCK_NAME};
use crate::DataStore;
/// BackupGroup is a directory containing a list of BackupDir
@ -313,6 +314,29 @@ impl BackupDir {
// fixme: can this fail? (avoid unwrap)
proxmox_time::epoch_to_rfc3339_utc(backup_time)
}
/// Returns the filename to lock a manifest
///
/// Also creates the basedir. The lockfile is located in
/// '/run/proxmox-backup/locks/{datastore}/{type}/{id}/{timestamp}.index.json.lck'
pub(crate) fn manifest_lock_path(&self) -> Result<String, Error> {
let mut path = format!("/run/proxmox-backup/locks/{}/{self}", self.store.name());
std::fs::create_dir_all(&path)?;
use std::fmt::Write;
let ts = self.backup_time_string();
write!(path, "/{ts}{}", &MANIFEST_LOCK_NAME)?;
Ok(path)
}
/// Locks the manifest of a snapshot, for example, to update or delete it.
pub(crate) fn lock_manifest(&self) -> Result<BackupLockGuard, Error> {
let path = self.manifest_lock_path()?;
// actions locking the manifest should be relatively short, only wait a few seconds
open_backup_lockfile(&path, Some(std::time::Duration::from_secs(5)), true)
.map_err(|err| format_err!("unable to acquire manifest lock {:?} - {}", &path, err))
}
}
impl AsRef<pbs_api_types::BackupDir> for BackupDir {