From 7f68b83cb72714eb81eabea0cbffa151ae9cccc2 Mon Sep 17 00:00:00 2001 From: mpostma Date: Fri, 26 Mar 2021 11:34:37 +0100 Subject: [PATCH 1/2] fix snapshot --- meilisearch-http/src/snapshot.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/meilisearch-http/src/snapshot.rs b/meilisearch-http/src/snapshot.rs index 505ddd269..eb32bd48c 100644 --- a/meilisearch-http/src/snapshot.rs +++ b/meilisearch-http/src/snapshot.rs @@ -7,7 +7,6 @@ use std::fs::create_dir_all; use std::path::Path; use std::thread; use std::time::Duration; -use tempfile::TempDir; pub fn load_snapshot( db_path: &str, @@ -28,12 +27,22 @@ pub fn load_snapshot( } } -pub fn create_snapshot(data: &Data, snapshot_path: &Path) -> Result<(), Error> { - let tmp_dir = TempDir::new_in(snapshot_path)?; +pub fn create_snapshot(data: &Data, snapshot_dir: impl AsRef, snapshot_name: impl AsRef) -> Result<(), Error> { + create_dir_all(&snapshot_dir)?; + let tmp_dir = tempfile::tempdir_in(&snapshot_dir)?; data.db.copy_and_compact_to_path(tmp_dir.path())?; - compression::to_tar_gz(tmp_dir.path(), snapshot_path).map_err(|e| Error::Internal(format!("something went wrong during snapshot compression: {}", e))) + let temp_snapshot_file = tempfile::NamedTempFile::new_in(&snapshot_dir)?; + + compression::to_tar_gz(tmp_dir.path(), temp_snapshot_file.path()) + .map_err(|e| Error::Internal(format!("something went wrong during snapshot compression: {}", e)))?; + + let snapshot_path = snapshot_dir.as_ref().join(snapshot_name.as_ref()); + + temp_snapshot_file.persist(snapshot_path).map_err(|e| Error::Internal(e.to_string()))?; + + Ok(()) } pub fn schedule_snapshot(data: Data, snapshot_dir: &Path, time_gap_s: u64) -> Result<(), Error> { @@ -42,10 +51,11 @@ pub fn schedule_snapshot(data: Data, snapshot_dir: &Path, time_gap_s: u64) -> Re } let db_name = Path::new(&data.db_path).file_name().ok_or_else(|| Error::Internal("invalid database name".to_string()))?; create_dir_all(snapshot_dir)?; - let snapshot_path = snapshot_dir.join(format!("{}.snapshot", db_name.to_str().unwrap_or("data.ms"))); + let snapshot_name = format!("{}.snapshot", db_name.to_str().unwrap_or("data.ms")); + let snapshot_dir = snapshot_dir.to_owned(); thread::spawn(move || loop { - if let Err(e) = create_snapshot(&data, &snapshot_path) { + if let Err(e) = create_snapshot(&data, &snapshot_dir, &snapshot_name) { error!("Unsuccessful snapshot creation: {}", e); } thread::sleep(Duration::from_secs(time_gap_s)); From 9a1ab4e69f5e4f32df2b5f7ba37999d38c8087eb Mon Sep 17 00:00:00 2001 From: Marin Postma Date: Mon, 29 Mar 2021 13:25:59 +0200 Subject: [PATCH 2/2] fix test --- meilisearch-http/src/snapshot.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meilisearch-http/src/snapshot.rs b/meilisearch-http/src/snapshot.rs index eb32bd48c..90db8460a 100644 --- a/meilisearch-http/src/snapshot.rs +++ b/meilisearch-http/src/snapshot.rs @@ -72,7 +72,7 @@ mod tests { #[test] fn test_pack_unpack() { - let tempdir = TempDir::new().unwrap(); + let tempdir = tempfile::tempdir().unwrap(); let test_dir = tempdir.path(); let src_dir = test_dir.join("src");