2018-12-17 12:00:39 +00:00
|
|
|
use failure::*;
|
|
|
|
|
2019-02-27 09:02:22 +00:00
|
|
|
use std::io;
|
2018-12-18 10:06:03 +00:00
|
|
|
use std::path::{PathBuf, Path};
|
2018-12-22 16:37:25 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use std::sync::{Mutex, Arc};
|
2018-12-17 12:00:39 +00:00
|
|
|
|
2019-01-20 17:02:06 +00:00
|
|
|
use crate::tools;
|
2018-12-17 12:00:39 +00:00
|
|
|
use crate::config::datastore;
|
|
|
|
use super::chunk_store::*;
|
2019-02-12 10:50:45 +00:00
|
|
|
use super::fixed_index::*;
|
2019-02-12 11:05:33 +00:00
|
|
|
use super::dynamic_index::*;
|
2019-02-28 09:21:56 +00:00
|
|
|
use super::index::*;
|
2019-03-05 06:18:12 +00:00
|
|
|
use super::backup_info::*;
|
2019-06-13 09:47:23 +00:00
|
|
|
use super::DataChunk;
|
2019-04-06 15:57:38 +00:00
|
|
|
use crate::server::WorkerTask;
|
2018-12-17 12:00:39 +00:00
|
|
|
|
2019-03-05 06:18:12 +00:00
|
|
|
lazy_static!{
|
2019-03-18 09:00:58 +00:00
|
|
|
static ref DATASTORE_MAP: Mutex<HashMap<String, Arc<DataStore>>> = Mutex::new(HashMap::new());
|
2019-03-05 06:18:12 +00:00
|
|
|
}
|
2019-01-18 11:01:37 +00:00
|
|
|
|
2019-02-12 13:13:31 +00:00
|
|
|
/// Datastore Management
|
|
|
|
///
|
|
|
|
/// A Datastore can store severals backups, and provides the
|
|
|
|
/// management interface for backup.
|
2018-12-17 12:00:39 +00:00
|
|
|
pub struct DataStore {
|
2019-01-15 10:38:26 +00:00
|
|
|
chunk_store: Arc<ChunkStore>,
|
2018-12-22 15:58:16 +00:00
|
|
|
gc_mutex: Mutex<bool>,
|
2019-04-11 10:04:25 +00:00
|
|
|
last_gc_status: Mutex<GarbageCollectionStatus>,
|
2018-12-17 12:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DataStore {
|
|
|
|
|
2018-12-22 16:37:25 +00:00
|
|
|
pub fn lookup_datastore(name: &str) -> Result<Arc<DataStore>, Error> {
|
|
|
|
|
|
|
|
let config = datastore::config()?;
|
|
|
|
let (_, store_config) = config.sections.get(name)
|
|
|
|
.ok_or(format_err!("no such datastore '{}'", name))?;
|
|
|
|
|
|
|
|
let path = store_config["path"].as_str().unwrap();
|
|
|
|
|
2019-03-18 09:00:58 +00:00
|
|
|
let mut map = DATASTORE_MAP.lock().unwrap();
|
2018-12-22 16:37:25 +00:00
|
|
|
|
|
|
|
if let Some(datastore) = map.get(name) {
|
|
|
|
// Compare Config - if changed, create new Datastore object!
|
2018-12-25 12:29:27 +00:00
|
|
|
if datastore.chunk_store.base == PathBuf::from(path) {
|
2018-12-22 16:37:25 +00:00
|
|
|
return Ok(datastore.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 13:33:03 +00:00
|
|
|
let datastore = DataStore::open(name)?;
|
|
|
|
|
|
|
|
let datastore = Arc::new(datastore);
|
|
|
|
map.insert(name.to_string(), datastore.clone());
|
2018-12-22 16:37:25 +00:00
|
|
|
|
2019-02-15 13:33:03 +00:00
|
|
|
Ok(datastore)
|
2018-12-22 16:37:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 12:13:49 +00:00
|
|
|
pub fn open(store_name: &str) -> Result<Self, Error> {
|
2018-12-17 12:00:39 +00:00
|
|
|
|
|
|
|
let config = datastore::config()?;
|
|
|
|
let (_, store_config) = config.sections.get(store_name)
|
|
|
|
.ok_or(format_err!("no such datastore '{}'", store_name))?;
|
|
|
|
|
|
|
|
let path = store_config["path"].as_str().unwrap();
|
|
|
|
|
2018-12-19 12:40:26 +00:00
|
|
|
let chunk_store = ChunkStore::open(store_name, path)?;
|
2018-12-17 12:00:39 +00:00
|
|
|
|
2019-04-11 10:04:25 +00:00
|
|
|
let gc_status = GarbageCollectionStatus::default();
|
|
|
|
|
2018-12-17 12:00:39 +00:00
|
|
|
Ok(Self {
|
2019-01-15 10:38:26 +00:00
|
|
|
chunk_store: Arc::new(chunk_store),
|
2018-12-22 15:58:16 +00:00
|
|
|
gc_mutex: Mutex::new(false),
|
2019-04-11 10:04:25 +00:00
|
|
|
last_gc_status: Mutex::new(gc_status),
|
2018-12-17 12:00:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-14 10:39:09 +00:00
|
|
|
pub fn get_chunk_iterator(
|
|
|
|
&self,
|
|
|
|
) -> Result<
|
2019-07-04 07:26:44 +00:00
|
|
|
impl Iterator<Item = (Result<tools::fs::ReadDirEntry, Error>, usize)>,
|
2019-02-14 10:39:09 +00:00
|
|
|
Error
|
|
|
|
> {
|
2019-07-04 07:26:44 +00:00
|
|
|
self.chunk_store.get_chunk_iterator()
|
2019-02-14 10:39:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
pub fn create_fixed_writer<P: AsRef<Path>>(&self, filename: P, size: usize, chunk_size: usize) -> Result<FixedIndexWriter, Error> {
|
2018-12-17 12:00:39 +00:00
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
let index = FixedIndexWriter::create(self.chunk_store.clone(), filename.as_ref(), size, chunk_size)?;
|
2018-12-17 12:00:39 +00:00
|
|
|
|
|
|
|
Ok(index)
|
|
|
|
}
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
pub fn open_fixed_reader<P: AsRef<Path>>(&self, filename: P) -> Result<FixedIndexReader, Error> {
|
2018-12-17 12:00:39 +00:00
|
|
|
|
2019-07-04 06:09:48 +00:00
|
|
|
let full_path = self.chunk_store.relative_path(filename.as_ref());
|
|
|
|
|
|
|
|
let index = FixedIndexReader::open(&full_path)?;
|
2018-12-17 12:00:39 +00:00
|
|
|
|
|
|
|
Ok(index)
|
|
|
|
}
|
2018-12-18 10:06:03 +00:00
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
pub fn create_dynamic_writer<P: AsRef<Path>>(
|
2018-12-31 16:30:08 +00:00
|
|
|
&self, filename: P,
|
2019-02-12 11:05:33 +00:00
|
|
|
) -> Result<DynamicIndexWriter, Error> {
|
2018-12-31 16:30:08 +00:00
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
let index = DynamicIndexWriter::create(
|
2019-05-29 06:49:57 +00:00
|
|
|
self.chunk_store.clone(), filename.as_ref())?;
|
2018-12-31 16:30:08 +00:00
|
|
|
|
|
|
|
Ok(index)
|
|
|
|
}
|
2019-01-18 11:01:37 +00:00
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
pub fn open_dynamic_reader<P: AsRef<Path>>(&self, filename: P) -> Result<DynamicIndexReader, Error> {
|
2019-01-02 13:27:04 +00:00
|
|
|
|
2019-06-28 14:35:00 +00:00
|
|
|
let full_path = self.chunk_store.relative_path(filename.as_ref());
|
|
|
|
|
|
|
|
let index = DynamicIndexReader::open(&full_path)?;
|
2019-01-02 13:27:04 +00:00
|
|
|
|
|
|
|
Ok(index)
|
|
|
|
}
|
|
|
|
|
2019-02-28 09:21:56 +00:00
|
|
|
pub fn open_index<P>(&self, filename: P) -> Result<Box<dyn IndexFile + Send>, Error>
|
|
|
|
where
|
|
|
|
P: AsRef<Path>,
|
|
|
|
{
|
|
|
|
let filename = filename.as_ref();
|
|
|
|
let out: Box<dyn IndexFile + Send> =
|
|
|
|
match filename.extension().and_then(|ext| ext.to_str()) {
|
|
|
|
Some("didx") => Box::new(self.open_dynamic_reader(filename)?),
|
|
|
|
Some("fidx") => Box::new(self.open_fixed_reader(filename)?),
|
|
|
|
_ => bail!("cannot open index file of unknown type: {:?}", filename),
|
|
|
|
};
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
|
2019-01-18 11:01:37 +00:00
|
|
|
pub fn base_path(&self) -> PathBuf {
|
|
|
|
self.chunk_store.base_path()
|
|
|
|
}
|
|
|
|
|
2019-02-28 11:51:27 +00:00
|
|
|
/// Remove a backup directory including all content
|
2019-03-01 11:32:10 +00:00
|
|
|
pub fn remove_backup_dir(&self, backup_dir: &BackupDir,
|
2019-02-28 11:51:27 +00:00
|
|
|
) -> Result<(), io::Error> {
|
|
|
|
|
2019-03-01 11:32:10 +00:00
|
|
|
let relative_path = backup_dir.relative_path();
|
2019-02-28 11:51:27 +00:00
|
|
|
let mut full_path = self.base_path();
|
|
|
|
full_path.push(&relative_path);
|
|
|
|
|
|
|
|
log::info!("removing backup {:?}", full_path);
|
|
|
|
std::fs::remove_dir_all(full_path)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-03-05 06:18:12 +00:00
|
|
|
pub fn create_backup_dir(&self, backup_dir: &BackupDir) -> Result<(PathBuf, bool), io::Error> {
|
2019-01-18 11:01:37 +00:00
|
|
|
|
2019-02-27 09:02:22 +00:00
|
|
|
// create intermediate path first:
|
|
|
|
let mut full_path = self.base_path();
|
2019-03-05 06:18:12 +00:00
|
|
|
full_path.push(backup_dir.group().group_path());
|
2019-02-27 09:02:22 +00:00
|
|
|
std::fs::create_dir_all(&full_path)?;
|
|
|
|
|
2019-03-05 06:18:12 +00:00
|
|
|
let relative_path = backup_dir.relative_path();
|
|
|
|
let mut full_path = self.base_path();
|
|
|
|
full_path.push(&relative_path);
|
2019-01-18 11:01:37 +00:00
|
|
|
|
2019-02-27 09:02:22 +00:00
|
|
|
// create the last component now
|
|
|
|
match std::fs::create_dir(&full_path) {
|
|
|
|
Ok(_) => Ok((relative_path, true)),
|
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok((relative_path, false)),
|
|
|
|
Err(e) => Err(e)
|
|
|
|
}
|
2019-01-18 11:01:37 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 10:06:03 +00:00
|
|
|
pub fn list_images(&self) -> Result<Vec<PathBuf>, Error> {
|
2019-01-18 11:01:37 +00:00
|
|
|
let base = self.base_path();
|
2018-12-18 10:06:03 +00:00
|
|
|
|
|
|
|
let mut list = vec![];
|
|
|
|
|
2019-01-18 11:24:58 +00:00
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
|
|
|
let walker = WalkDir::new(&base).same_file_system(true).into_iter();
|
|
|
|
|
|
|
|
// make sure we skip .chunks (and other hidden files to keep it simple)
|
|
|
|
fn is_hidden(entry: &walkdir::DirEntry) -> bool {
|
|
|
|
entry.file_name()
|
|
|
|
.to_str()
|
|
|
|
.map(|s| s.starts_with("."))
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
for entry in walker.filter_entry(|e| !is_hidden(e)) {
|
|
|
|
let path = entry?.into_path();
|
|
|
|
if let Some(ext) = path.extension() {
|
2019-02-12 10:50:45 +00:00
|
|
|
if ext == "fidx" {
|
2019-01-18 11:24:58 +00:00
|
|
|
list.push(path);
|
2019-02-12 11:05:33 +00:00
|
|
|
} else if ext == "didx" {
|
2019-01-18 11:24:58 +00:00
|
|
|
list.push(path);
|
2018-12-18 10:06:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(list)
|
|
|
|
}
|
|
|
|
|
2019-07-04 05:57:43 +00:00
|
|
|
// mark chunks used by ``index`` as used
|
|
|
|
fn index_mark_used_chunks<I: IndexFile>(
|
|
|
|
&self,
|
|
|
|
index: I,
|
|
|
|
file_name: &Path, // only used for error reporting
|
|
|
|
status: &mut GarbageCollectionStatus,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
status.index_file_count += 1;
|
|
|
|
status.index_data_bytes += index.index_bytes();
|
|
|
|
|
|
|
|
for pos in 0..index.index_count() {
|
|
|
|
tools::fail_on_shutdown()?;
|
|
|
|
let digest = index.index_digest(pos).unwrap();
|
|
|
|
if let Err(err) = self.chunk_store.touch_chunk(digest) {
|
|
|
|
bail!("unable to access chunk {}, required by {:?} - {}",
|
|
|
|
proxmox::tools::digest_to_hex(digest), file_name, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-22 15:58:16 +00:00
|
|
|
fn mark_used_chunks(&self, status: &mut GarbageCollectionStatus) -> Result<(), Error> {
|
2018-12-18 10:06:03 +00:00
|
|
|
|
|
|
|
let image_list = self.list_images()?;
|
|
|
|
|
|
|
|
for path in image_list {
|
2019-04-01 10:13:02 +00:00
|
|
|
|
|
|
|
tools::fail_on_shutdown()?;
|
|
|
|
|
2019-01-02 13:27:04 +00:00
|
|
|
if let Some(ext) = path.extension() {
|
2019-02-12 10:50:45 +00:00
|
|
|
if ext == "fidx" {
|
|
|
|
let index = self.open_fixed_reader(&path)?;
|
2019-07-04 05:57:43 +00:00
|
|
|
self.index_mark_used_chunks(index, &path, status)?;
|
2019-02-12 11:05:33 +00:00
|
|
|
} else if ext == "didx" {
|
|
|
|
let index = self.open_dynamic_reader(&path)?;
|
2019-07-04 05:57:43 +00:00
|
|
|
self.index_mark_used_chunks(index, &path, status)?;
|
2019-01-02 13:27:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-18 10:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2019-04-11 10:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn last_gc_status(&self) -> GarbageCollectionStatus {
|
|
|
|
self.last_gc_status.lock().unwrap().clone()
|
|
|
|
}
|
2018-12-18 10:06:03 +00:00
|
|
|
|
2019-04-06 15:57:38 +00:00
|
|
|
pub fn garbage_collection(&self, worker: Arc<WorkerTask>) -> Result<(), Error> {
|
2018-12-18 10:06:03 +00:00
|
|
|
|
2018-12-25 12:29:27 +00:00
|
|
|
if let Ok(ref mut _mutex) = self.gc_mutex.try_lock() {
|
2018-12-22 13:04:05 +00:00
|
|
|
|
2019-03-22 08:42:15 +00:00
|
|
|
let _exclusive_lock = self.chunk_store.try_exclusive_lock()?;
|
|
|
|
|
2019-03-31 15:21:36 +00:00
|
|
|
let oldest_writer = self.chunk_store.oldest_writer();
|
|
|
|
|
2018-12-22 15:58:16 +00:00
|
|
|
let mut gc_status = GarbageCollectionStatus::default();
|
2019-04-11 10:04:25 +00:00
|
|
|
gc_status.upid = Some(worker.to_string());
|
2018-12-18 10:18:55 +00:00
|
|
|
|
2019-04-06 15:57:38 +00:00
|
|
|
worker.log("Start GC phase1 (mark chunks)");
|
2018-12-22 15:58:16 +00:00
|
|
|
|
|
|
|
self.mark_used_chunks(&mut gc_status)?;
|
|
|
|
|
2019-04-06 15:57:38 +00:00
|
|
|
worker.log("Start GC phase2 (sweep unused chunks)");
|
2019-07-04 07:26:44 +00:00
|
|
|
self.chunk_store.sweep_unused_chunks(oldest_writer, &mut gc_status, worker.clone())?;
|
2018-12-22 15:58:16 +00:00
|
|
|
|
2019-07-04 05:57:43 +00:00
|
|
|
worker.log(&format!("Removed bytes: {}", gc_status.removed_bytes));
|
|
|
|
worker.log(&format!("Removed chunks: {}", gc_status.removed_chunks));
|
|
|
|
worker.log(&format!("Original data bytes: {}", gc_status.index_data_bytes));
|
|
|
|
let comp_per = (gc_status.disk_bytes*100)/gc_status.index_data_bytes;
|
|
|
|
worker.log(&format!("Disk bytes: {} ({} %)", gc_status.disk_bytes, comp_per));
|
2019-04-06 15:57:38 +00:00
|
|
|
worker.log(&format!("Disk chunks: {}", gc_status.disk_chunks));
|
2019-07-04 05:57:43 +00:00
|
|
|
let avg_chunk = gc_status.index_data_bytes/(gc_status.disk_chunks as u64);
|
|
|
|
worker.log(&format!("Average chunk size: {}", avg_chunk));
|
2018-12-22 15:58:16 +00:00
|
|
|
|
2019-04-11 10:04:25 +00:00
|
|
|
*self.last_gc_status.lock().unwrap() = gc_status;
|
|
|
|
|
2018-12-22 15:58:16 +00:00
|
|
|
} else {
|
2019-04-06 15:57:38 +00:00
|
|
|
bail!("Start GC failed - (already running/locked)");
|
2018-12-22 15:58:16 +00:00
|
|
|
}
|
2018-12-18 10:06:03 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-03-06 09:19:07 +00:00
|
|
|
|
2019-06-28 14:35:00 +00:00
|
|
|
pub fn chunk_path(&self, digest:&[u8; 32]) -> (PathBuf, String) {
|
|
|
|
self.chunk_store.chunk_path(digest)
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:47:23 +00:00
|
|
|
pub fn insert_chunk(
|
2019-03-06 09:19:07 +00:00
|
|
|
&self,
|
2019-06-13 09:47:23 +00:00
|
|
|
chunk: &DataChunk,
|
2019-03-06 09:19:07 +00:00
|
|
|
) -> Result<(bool, u64), Error> {
|
2019-06-13 09:47:23 +00:00
|
|
|
self.chunk_store.insert_chunk(chunk)
|
2019-03-06 09:19:07 +00:00
|
|
|
}
|
2018-12-17 12:00:39 +00:00
|
|
|
}
|