finish_backup: add chunk_upload_stats to manifest

This commit is contained in:
Dietmar Maurer 2020-07-31 07:27:57 +02:00
parent 9fa55e09a7
commit c8fff67d88
1 changed files with 32 additions and 1 deletions

View File

@ -2,6 +2,7 @@ use anyhow::{bail, format_err, Error};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use ::serde::{Serialize};
use serde_json::{json, Value}; use serde_json::{json, Value};
use proxmox::tools::digest_to_hex; use proxmox::tools::digest_to_hex;
@ -13,6 +14,7 @@ use crate::backup::*;
use crate::server::formatter::*; use crate::server::formatter::*;
use hyper::{Body, Response}; use hyper::{Body, Response};
#[derive(Copy, Clone, Serialize)]
struct UploadStatistic { struct UploadStatistic {
count: u64, count: u64,
size: u64, size: u64,
@ -31,6 +33,19 @@ impl UploadStatistic {
} }
} }
impl std::ops::Add for UploadStatistic {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
count: self.count + other.count,
size: self.size + other.size,
compressed_size: self.compressed_size + other.compressed_size,
duplicates: self.duplicates + other.duplicates,
}
}
}
struct DynamicWriterState { struct DynamicWriterState {
name: String, name: String,
index: DynamicIndexWriter, index: DynamicIndexWriter,
@ -58,6 +73,8 @@ struct SharedBackupState {
fixed_writers: HashMap<usize, FixedWriterState>, fixed_writers: HashMap<usize, FixedWriterState>,
known_chunks: HashMap<[u8;32], u32>, known_chunks: HashMap<[u8;32], u32>,
base_snapshots: HashSet<BackupDir>, base_snapshots: HashSet<BackupDir>,
backup_size: u64, // sums up size of all files
backup_stat: UploadStatistic,
} }
impl SharedBackupState { impl SharedBackupState {
@ -110,6 +127,8 @@ impl BackupEnvironment {
fixed_writers: HashMap::new(), fixed_writers: HashMap::new(),
known_chunks: HashMap::new(), known_chunks: HashMap::new(),
base_snapshots: HashSet::new(), base_snapshots: HashSet::new(),
backup_size: 0,
backup_stat: UploadStatistic::new(),
}; };
Self { Self {
@ -370,6 +389,8 @@ impl BackupEnvironment {
self.log_upload_stat(&data.name, &csum, &uuid, size, chunk_count, &data.upload_stat); self.log_upload_stat(&data.name, &csum, &uuid, size, chunk_count, &data.upload_stat);
state.file_counter += 1; state.file_counter += 1;
state.backup_size += size;
state.backup_stat = state.backup_stat + data.upload_stat;
Ok(()) Ok(())
} }
@ -412,6 +433,8 @@ impl BackupEnvironment {
self.log_upload_stat(&data.name, &expected_csum, &uuid, size, chunk_count, &data.upload_stat); self.log_upload_stat(&data.name, &expected_csum, &uuid, size, chunk_count, &data.upload_stat);
state.file_counter += 1; state.file_counter += 1;
state.backup_size += size;
state.backup_stat = state.backup_stat + data.upload_stat;
Ok(()) Ok(())
} }
@ -435,6 +458,8 @@ impl BackupEnvironment {
let mut state = self.state.lock().unwrap(); let mut state = self.state.lock().unwrap();
state.file_counter += 1; state.file_counter += 1;
state.backup_size += orig_len as u64;
state.backup_stat.size += blob_len as u64;
Ok(()) Ok(())
} }
@ -457,9 +482,15 @@ impl BackupEnvironment {
state.finished = true; state.finished = true;
// check manifest // check manifest
let _manifest = self.datastore.load_manifest(&self.backup_dir) let mut manifest = self.datastore.load_manifest_json(&self.backup_dir)
.map_err(|err| format_err!("unable to load manifest blob - {}", err))?; .map_err(|err| format_err!("unable to load manifest blob - {}", err))?;
let stats = serde_json::to_value(state.backup_stat)?;
manifest["unprotected"]["chunk_upload_stats"] = stats;
self.datastore.store_manifest(&self.backup_dir, manifest)
.map_err(|err| format_err!("unable to store manifest blob - {}", err))?;
for snap in &state.base_snapshots { for snap in &state.base_snapshots {
let path = self.datastore.snapshot_path(snap); let path = self.datastore.snapshot_path(snap);