[pbs-devel] [PATCH v2 proxmox-backup] fix #5710: api: backup: stat known chunks on backup finish

Gabriel Goller g.goller at proxmox.com
Mon Oct 7 11:47:19 CEST 2024


>@@ -256,7 +280,23 @@ impl BackupEnvironment {
>     pub fn lookup_chunk(&self, digest: &[u8; 32]) -> Option<u32> {
>         let state = self.state.lock().unwrap();
>
>-        state.known_chunks.get(digest).copied()
>+        state
>+            .known_chunks
>+            .get(digest)
>+            .map(|known_chunk_info| known_chunk_info.length)
>+    }
>+
>+    /// stat known chunks from previous backup, so excluding newly uploaded ones
>+    pub fn stat_prev_known_chunks(&self) -> Result<(), Error> {
>+        let state = self.state.lock().unwrap();
>+        for (digest, known_chunk_info) in &state.known_chunks {
>+            if !known_chunk_info.uploaded {
>+                self.datastore
>+                    .stat_chunk(digest)
>+                    .map_err(|err| format_err!("stat failed on {} - {err}", hex::encode(digest)))?;

`err` is an anyhow::Error, so we could add a context, e.g. something
like:

     .map_err(|err| err.context(format!("stat failed on {}", hex::encode(digest))))?;

>+            }
>+        }
>+        Ok(())
>     }
>
>     /// Store the writer with an unique ID
>diff --git a/src/api2/backup/mod.rs b/src/api2/backup/mod.rs
>index ea0d0292e..468824565 100644
>--- a/src/api2/backup/mod.rs
>+++ b/src/api2/backup/mod.rs
>@@ -785,6 +785,26 @@ fn finish_backup(
> ) -> Result<Value, Error> {
>     let env: &BackupEnvironment = rpcenv.as_ref();
>
>+    if let Err(err) = env.stat_prev_known_chunks() {
>+        env.debug(format!("stat registered chunks failed - {err}"));

We want to print the full error, so we need to use `{err:?}`.

>+
>+        if let Some(last) = env.last_backup.as_ref() {
>+            // No need to acquire snapshot lock, already locked when starting the backup
>+            let verify_state = SnapshotVerifyState {
>+                state: VerifyState::Failed,
>+                upid: env.worker.upid().clone(), // backp writer UPID

s/backp/backup/

>+            };
>+            let verify_state = serde_json::to_value(verify_state)?;
>+            last.backup_dir
>+                .update_manifest(|manifest| {
>+                    manifest.unprotected["verify_state"] = verify_state;
>+                })
>+                .map_err(|err| format_err!("manifest update failed - {err}"))?;

Same here, adding anyhow::Context is nicer.

>+        }
>+
>+        bail!("stat known chunks failed - {err}");

Again, use `{err:?}` instead.

>+    }
>+
>     env.finish_backup()?;
>     env.log("successfully finished backup");

Also did a quick test which worked perfectly. When the problems above
are fixed you can consider:

Tested-by: Gabriel Goller <g.goller at proxmox.com>
Reviewed-by: Gabriel Goller <g.goller at proxmox.com>




More information about the pbs-devel mailing list