From 44dcfe29aa809bc50d42e5cc528dc1262e0932c3 Mon Sep 17 00:00:00 2001 From: mpostma Date: Mon, 22 Mar 2021 16:51:53 +0100 Subject: [PATCH] clean snapshot creation --- meilisearch-http/src/helpers/compression.rs | 2 +- .../src/index_controller/snapshot.rs | 29 ++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/meilisearch-http/src/helpers/compression.rs b/meilisearch-http/src/helpers/compression.rs index e12e5b479..4fc39a353 100644 --- a/meilisearch-http/src/helpers/compression.rs +++ b/meilisearch-http/src/helpers/compression.rs @@ -7,7 +7,7 @@ use tar::{Archive, Builder}; use crate::error::Error; -pub fn to_tar_gz(src: &Path, dest: &Path) -> Result<(), Error> { +pub fn to_tar_gz(src: impl AsRef, dest: impl AsRef) -> Result<(), Error> { let f = File::create(dest)?; let gz_encoder = GzEncoder::new(f, Compression::default()); let mut tar_encoder = Builder::new(gz_encoder); diff --git a/meilisearch-http/src/index_controller/snapshot.rs b/meilisearch-http/src/index_controller/snapshot.rs index 8e26fbfac..b2bbf17d2 100644 --- a/meilisearch-http/src/index_controller/snapshot.rs +++ b/meilisearch-http/src/index_controller/snapshot.rs @@ -1,10 +1,12 @@ use std::path::PathBuf; use std::time::Duration; -use std::fs::create_dir_all; +use anyhow::bail; +use tokio::fs; +use tokio::task::spawn_blocking; use tokio::time::interval; -use uuid::Uuid; +use crate::helpers::compression; use super::index_actor::IndexActorHandle; use super::update_actor::UpdateActorHandle; use super::uuid_resolver::UuidResolverHandle; @@ -45,15 +47,28 @@ impl SnapshotService { } async fn perform_snapshot(&self) -> anyhow::Result<()> { - let temp_snapshot_path = self - .snapshot_path - .join(format!("tmp-{}", Uuid::new_v4())); - create_dir_all(&temp_snapshot_path)?; + if self.snapshot_path.file_name().is_none() { + bail!("invalid snapshot file path"); + } + + let temp_snapshot_dir = spawn_blocking(move || tempfile::tempdir_in(".")).await??; + let temp_snapshot_path = temp_snapshot_dir.path().to_owned(); + + fs::create_dir_all(&temp_snapshot_path).await?; + let uuids = self.uuid_resolver_handle.snapshot(temp_snapshot_path.clone()).await?; for uuid in uuids { self.update_handle.snapshot(uuid, temp_snapshot_path.clone()).await?; - println!("performed snapshot for index {}", uuid); } + + let temp_snapshot_file = temp_snapshot_path.with_extension("temp"); + + let temp_snapshot_file_clone = temp_snapshot_file.clone(); + let temp_snapshot_path_clone = temp_snapshot_path.clone(); + spawn_blocking(move || compression::to_tar_gz(temp_snapshot_path_clone, temp_snapshot_file_clone)).await??; + + fs::rename(temp_snapshot_file, &self.snapshot_path).await?; + Ok(()) } }