From 20836cc3dbc2e22c6d7da99ec199930b6d4c7ad4 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 14 Nov 2024 22:44:18 +0000 Subject: [PATCH] flush=false for database-backup in read-only/secondary modes; improve error Signed-off-by: Jason Volk --- src/admin/server/commands.rs | 2 +- src/database/engine.rs | 14 ++++++++------ src/service/globals/data.rs | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/admin/server/commands.rs b/src/admin/server/commands.rs index f5879b03..94f695ce 100644 --- a/src/admin/server/commands.rs +++ b/src/admin/server/commands.rs @@ -107,7 +107,7 @@ pub(super) async fn backup_database(&self) -> Result { .runtime() .spawn_blocking(move || match globals.db.backup() { Ok(()) => String::new(), - Err(e) => (*e).to_string(), + Err(e) => e.to_string(), }) .await?; diff --git a/src/database/engine.rs b/src/database/engine.rs index b57fd75e..1fa53b01 100644 --- a/src/database/engine.rs +++ b/src/database/engine.rs @@ -17,6 +17,7 @@ use rocksdb::{ use crate::{ opts::{cf_options, db_options}, or_else, result, + util::map_err, }; pub struct Engine { @@ -183,19 +184,20 @@ impl Engine { } #[tracing::instrument(skip(self))] - pub fn backup(&self) -> Result<(), Box> { + pub fn backup(&self) -> Result { let config = &self.server.config; let path = config.database_backup_path.as_ref(); if path.is_none() || path.is_some_and(|path| path.as_os_str().is_empty()) { return Ok(()); } - let options = BackupEngineOptions::new(path.expect("valid database backup path"))?; - let mut engine = BackupEngine::open(&options, &self.env)?; + let options = BackupEngineOptions::new(path.expect("valid database backup path")).map_err(map_err)?; + let mut engine = BackupEngine::open(&options, &self.env).map_err(map_err)?; if config.database_backups_to_keep > 0 { - if let Err(e) = engine.create_new_backup_flush(&self.db, true) { - return Err(Box::new(e)); - } + let flush = !self.is_read_only(); + engine + .create_new_backup_flush(&self.db, flush) + .map_err(map_err)?; let engine_info = engine.get_backup_info(); let info = &engine_info.last().expect("backup engine info is not empty"); diff --git a/src/service/globals/data.rs b/src/service/globals/data.rs index bcfe101e..f715e944 100644 --- a/src/service/globals/data.rs +++ b/src/service/globals/data.rs @@ -73,7 +73,7 @@ impl Data { } #[inline] - pub fn backup(&self) -> Result<(), Box> { self.db.db.backup() } + pub fn backup(&self) -> Result { self.db.db.backup() } #[inline] pub fn backup_list(&self) -> Result { self.db.db.backup_list() }