meilisearch/meilisearch-http/src/index_controller/dump_actor/mod.rs

146 lines
3.7 KiB
Rust
Raw Normal View History

2021-05-27 02:42:09 +08:00
use std::{fs::File, path::Path};
2021-05-11 02:25:09 +08:00
2021-05-27 02:42:09 +08:00
use log::error;
#[cfg(test)]
use mockall::automock;
2021-05-11 02:23:12 +08:00
use serde::{Deserialize, Serialize};
use thiserror::Error;
2021-04-28 22:43:49 +08:00
2021-05-27 02:42:09 +08:00
use loaders::v1::MetadataV1;
use loaders::v2::MetadataV2;
2021-05-11 02:25:09 +08:00
pub use actor::DumpActor;
pub use handle_impl::*;
2021-05-11 02:25:09 +08:00
pub use message::DumpMsg;
2021-05-27 04:52:06 +08:00
use crate::option::IndexerOpts;
use super::uuid_resolver::store::UuidStore;
mod actor;
mod handle_impl;
mod loaders;
mod message;
2021-05-11 02:25:09 +08:00
pub type DumpResult<T> = std::result::Result<T, DumpError>;
#[derive(Error, Debug)]
pub enum DumpError {
#[error("error with index: {0}")]
Error(#[from] anyhow::Error),
#[error("Heed error: {0}")]
HeedError(#[from] heed::Error),
#[error("dump already running")]
DumpAlreadyRunning,
#[error("dump `{0}` does not exist")]
DumpDoesNotExist(String),
}
2021-04-28 22:43:49 +08:00
2021-05-11 02:25:09 +08:00
#[async_trait::async_trait]
#[cfg_attr(test, automock)]
pub trait DumpActorHandle {
/// Start the creation of a dump
/// Implementation: [handle_impl::DumpActorHandleImpl::create_dump]
async fn create_dump(&self) -> DumpResult<DumpInfo>;
/// Return the status of an already created dump
/// Implementation: [handle_impl::DumpActorHandleImpl::dump_status]
async fn dump_info(&self, uid: String) -> DumpResult<DumpInfo>;
}
2021-04-28 22:43:49 +08:00
#[derive(Debug, Serialize, Deserialize)]
2021-05-27 02:42:09 +08:00
#[serde(rename_all = "camelCase", tag = "dump_version")]
pub enum Metadata {
V1 {
#[serde(flatten)]
meta: MetadataV1,
},
V2 {
#[serde(flatten)]
meta: MetadataV2,
},
2021-04-28 22:43:49 +08:00
}
2021-04-29 20:45:08 +08:00
impl Metadata {
2021-05-27 16:51:19 +08:00
pub fn new_v2(index_db_size: u64, update_db_size: u64) -> Self {
let meta = MetadataV2::new(index_db_size, update_db_size);
Self::V2 { meta }
}
2021-04-29 20:45:08 +08:00
/// Extract Metadata from `metadata.json` file present at provided `dir_path`
2021-04-28 22:43:49 +08:00
fn from_path(dir_path: &Path) -> anyhow::Result<Self> {
let path = dir_path.join("metadata.json");
let file = File::open(path)?;
let reader = std::io::BufReader::new(file);
let metadata = serde_json::from_reader(reader)?;
Ok(metadata)
}
2021-04-29 20:45:08 +08:00
/// Write Metadata in `metadata.json` file at provided `dir_path`
2021-05-05 20:11:56 +08:00
pub async fn to_path(&self, dir_path: &Path) -> anyhow::Result<()> {
2021-04-28 22:43:49 +08:00
let path = dir_path.join("metadata.json");
2021-05-05 20:11:56 +08:00
tokio::fs::write(path, serde_json::to_string(self)?).await?;
2021-04-28 22:43:49 +08:00
Ok(())
}
}
2021-05-11 02:25:09 +08:00
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "snake_case")]
pub enum DumpStatus {
Done,
InProgress,
Failed,
2021-04-28 22:43:49 +08:00
}
2021-05-11 02:25:09 +08:00
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct DumpInfo {
pub uid: String,
pub status: DumpStatus,
2021-05-25 16:48:57 +08:00
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
2021-05-11 02:25:09 +08:00
}
2021-04-28 22:43:49 +08:00
2021-05-11 02:25:09 +08:00
impl DumpInfo {
pub fn new(uid: String, status: DumpStatus) -> Self {
Self {
uid,
status,
error: None,
}
2021-05-05 20:11:56 +08:00
}
2021-04-28 22:43:49 +08:00
2021-05-11 02:25:09 +08:00
pub fn with_error(&mut self, error: String) {
self.status = DumpStatus::Failed;
self.error = Some(error);
2021-05-05 20:11:56 +08:00
}
2021-04-28 22:43:49 +08:00
2021-05-11 02:25:09 +08:00
pub fn done(&mut self) {
self.status = DumpStatus::Done;
}
2021-04-28 22:43:49 +08:00
2021-05-11 02:25:09 +08:00
pub fn dump_already_in_progress(&self) -> bool {
self.status == DumpStatus::InProgress
}
2021-04-28 22:43:49 +08:00
}
2021-05-27 04:52:06 +08:00
pub fn load_dump<U: UuidStore>(
2021-05-27 02:42:09 +08:00
dst_path: impl AsRef<Path>,
src_path: impl AsRef<Path>,
_index_db_size: u64,
_update_db_size: u64,
2021-05-27 04:52:06 +08:00
indexer_opts: &IndexerOpts,
2021-04-28 22:43:49 +08:00
) -> anyhow::Result<()> {
2021-05-27 02:42:09 +08:00
let meta_path = src_path.as_ref().join("metadat.json");
let mut meta_file = File::open(&meta_path)?;
let meta: Metadata = serde_json::from_reader(&mut meta_file)?;
2021-04-28 22:43:49 +08:00
2021-05-27 02:42:09 +08:00
match meta {
Metadata::V1 { meta } => meta.load_dump(src_path, dst_path)?,
2021-05-27 04:52:06 +08:00
Metadata::V2 { meta } => meta.load_dump(src_path.as_ref(), dst_path.as_ref(), indexer_opts)?,
}
2021-05-07 00:44:16 +08:00
2021-04-28 22:43:49 +08:00
Ok(())
}