From ae1655586c907508fd4e6241c40519e12f75e32d Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Thu, 8 Apr 2021 16:35:28 +0300 Subject: [PATCH] fixes after review --- Cargo.lock | 16 -------- meilisearch-http/Cargo.lock | 16 -------- meilisearch-http/src/data/mod.rs | 32 +++++++++++++++- .../src/index_controller/index_actor/actor.rs | 19 +++++----- .../index_controller/update_actor/actor.rs | 13 ------- .../update_actor/handle_impl.rs | 7 ---- .../index_controller/update_actor/message.rs | 4 -- .../src/index_controller/update_actor/mod.rs | 1 - meilisearch-http/src/routes/stats.rs | 37 ++++++++----------- 9 files changed, 56 insertions(+), 89 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a79898d99..7ef514ffe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1894,22 +1894,6 @@ dependencies = [ "whatlang", ] -[[package]] -name = "meilisearch-tokenizer" -version = "0.1.1" -source = "git+https://github.com/meilisearch/Tokenizer.git?tag=v0.2.0#833c48b2ee39071f8b4f51abd15122afdb3c8c06" -dependencies = [ - "character_converter", - "cow-utils", - "deunicode", - "fst", - "jieba-rs", - "once_cell", - "slice-group-by", - "unicode-segmentation", - "whatlang", -] - [[package]] name = "memchr" version = "2.3.4" diff --git a/meilisearch-http/Cargo.lock b/meilisearch-http/Cargo.lock index 0bdc739d5..b9bfd06ac 100644 --- a/meilisearch-http/Cargo.lock +++ b/meilisearch-http/Cargo.lock @@ -1827,22 +1827,6 @@ dependencies = [ "vergen", ] -[[package]] -name = "meilisearch-tokenizer" -version = "0.1.1" -source = "git+https://github.com/meilisearch/Tokenizer.git?branch=main#147b6154b1b34cb8f5da2df6a416b7da191bc850" -dependencies = [ - "character_converter", - "cow-utils", - "deunicode", - "fst", - "jieba-rs", - "once_cell", - "slice-group-by", - "unicode-segmentation", - "whatlang", -] - [[package]] name = "memchr" version = "2.3.4" diff --git a/meilisearch-http/src/data/mod.rs b/meilisearch-http/src/data/mod.rs index 2d0a543d4..9227454a5 100644 --- a/meilisearch-http/src/data/mod.rs +++ b/meilisearch-http/src/data/mod.rs @@ -4,9 +4,11 @@ use std::sync::Arc; use sha2::Digest; use crate::index::Settings; -use crate::index_controller::IndexController; +use crate::index_controller::{IndexController, IndexStats}; use crate::index_controller::{IndexMetadata, IndexSettings}; use crate::option::Opt; +use std::collections::HashMap; +use chrono::{DateTime, Utc}; pub mod search; mod updates; @@ -37,6 +39,13 @@ pub struct ApiKeys { pub master: Option, } +#[derive(Default)] +pub struct Stats { + pub database_size: u64, + pub last_update: Option>, + pub indexes: HashMap, +} + impl ApiKeys { pub fn generate_missing_api_keys(&mut self) { if let Some(master_key) = &self.master { @@ -104,6 +113,27 @@ impl Data { Ok(meta) } + pub async fn get_index_stats(&self, uid: String) -> anyhow::Result { + Ok(self.index_controller.get_stats(uid).await?) + } + + pub async fn get_stats(&self) -> anyhow::Result { + let mut stats = Stats::default(); + + for index in self.index_controller.list_indexes().await? { + let index_stats = self.index_controller.get_stats(index.uid.clone()).await?; + + stats.database_size += index_stats.size; + stats.last_update = Some(match stats.last_update { + Some(last_update) => last_update.max(index.meta.updated_at), + None => index.meta.updated_at, + }); + stats.indexes.insert(index.uid, index_stats); + } + + Ok(stats) + } + #[inline] pub fn http_payload_size_limit(&self) -> usize { self.options.http_payload_size_limit.get_bytes() as usize diff --git a/meilisearch-http/src/index_controller/index_actor/actor.rs b/meilisearch-http/src/index_controller/index_actor/actor.rs index 0620765d5..099bb97ca 100644 --- a/meilisearch-http/src/index_controller/index_actor/actor.rs +++ b/meilisearch-http/src/index_controller/index_actor/actor.rs @@ -44,7 +44,7 @@ impl IndexActor { read_receiver, write_receiver, update_handler, - processing: RwLock::new(Default::default()), + processing: RwLock::new(None), store, }) } @@ -183,23 +183,22 @@ impl IndexActor { meta: Processing, data: File, ) -> Result { - let uuid = meta.index_uuid().clone(); - - *self.processing.write().await = Some(uuid); - - let result = { + async fn get_result(actor: &IndexActor, meta: Processing, data: File) -> Result { debug!("Processing update {}", meta.id()); - let update_handler = self.update_handler.clone(); - let index = match self.store.get(uuid).await? { + let uuid = *meta.index_uuid(); + let update_handler = actor.update_handler.clone(); + let index = match actor.store.get(uuid).await? { Some(index) => index, - None => self.store.create(uuid, None).await?, + None => actor.store.create(uuid, None).await?, }; spawn_blocking(move || update_handler.handle_update(meta, data, index)) .await .map_err(|e| IndexError::Error(e.into())) - }; + } + *self.processing.write().await = Some(meta.index_uuid().clone()); + let result = get_result(self, meta, data).await; *self.processing.write().await = None; result diff --git a/meilisearch-http/src/index_controller/update_actor/actor.rs b/meilisearch-http/src/index_controller/update_actor/actor.rs index c99c0059f..d87b910d6 100644 --- a/meilisearch-http/src/index_controller/update_actor/actor.rs +++ b/meilisearch-http/src/index_controller/update_actor/actor.rs @@ -72,9 +72,6 @@ where Some(Snapshot { uuid, path, ret }) => { let _ = ret.send(self.handle_snapshot(uuid, path).await); } - Some(IsLocked { uuid, ret }) => { - let _ = ret.send(self.handle_is_locked(uuid).await); - } None => break, } } @@ -226,14 +223,4 @@ where Ok(()) } - - async fn handle_is_locked(&self, uuid: Uuid) -> Result { - let store = self - .store - .get(uuid) - .await? - .ok_or(UpdateError::UnexistingIndex(uuid))?; - - Ok(store.update_lock.is_locked()) - } } diff --git a/meilisearch-http/src/index_controller/update_actor/handle_impl.rs b/meilisearch-http/src/index_controller/update_actor/handle_impl.rs index 621675955..8778a3674 100644 --- a/meilisearch-http/src/index_controller/update_actor/handle_impl.rs +++ b/meilisearch-http/src/index_controller/update_actor/handle_impl.rs @@ -95,11 +95,4 @@ where let _ = self.sender.send(msg).await; receiver.await.expect("update actor killed.") } - - async fn is_locked(&self, uuid: Uuid) -> Result { - let (ret, receiver) = oneshot::channel(); - let msg = UpdateMsg::IsLocked { uuid, ret }; - let _ = self.sender.send(msg).await; - receiver.await.expect("update actor killed.") - } } diff --git a/meilisearch-http/src/index_controller/update_actor/message.rs b/meilisearch-http/src/index_controller/update_actor/message.rs index 409fbcebc..8e6e3c212 100644 --- a/meilisearch-http/src/index_controller/update_actor/message.rs +++ b/meilisearch-http/src/index_controller/update_actor/message.rs @@ -34,8 +34,4 @@ pub enum UpdateMsg { path: PathBuf, ret: oneshot::Sender>, }, - IsLocked { - uuid: Uuid, - ret: oneshot::Sender>, - }, } diff --git a/meilisearch-http/src/index_controller/update_actor/mod.rs b/meilisearch-http/src/index_controller/update_actor/mod.rs index 095e068e8..f3c3caf04 100644 --- a/meilisearch-http/src/index_controller/update_actor/mod.rs +++ b/meilisearch-http/src/index_controller/update_actor/mod.rs @@ -52,5 +52,4 @@ pub trait UpdateActorHandle { data: mpsc::Receiver>, uuid: Uuid, ) -> Result; - async fn is_locked(&self, uuid: Uuid) -> Result; } diff --git a/meilisearch-http/src/routes/stats.rs b/meilisearch-http/src/routes/stats.rs index bab637cd6..c40a20609 100644 --- a/meilisearch-http/src/routes/stats.rs +++ b/meilisearch-http/src/routes/stats.rs @@ -12,6 +12,7 @@ use crate::helpers::Authentication; use crate::index_controller::IndexStats; use crate::routes::IndexParam; use crate::Data; +use crate::data::Stats; pub fn services(cfg: &mut web::ServiceConfig) { cfg.service(get_index_stats) @@ -42,11 +43,7 @@ async fn get_index_stats( data: web::Data, path: web::Path, ) -> Result { - let response: IndexStatsResponse = data - .index_controller - .get_stats(path.index_uid.clone()) - .await? - .into(); + let response: IndexStatsResponse = data.get_index_stats(path.index_uid.clone()).await?.into(); Ok(HttpResponse::Ok().json(response)) } @@ -59,24 +56,22 @@ struct StatsResponse { indexes: HashMap, } +impl From for StatsResponse { + fn from(stats: Stats) -> Self { + Self { + database_size: stats.database_size, + last_update: stats.last_update, + indexes: stats.indexes + .into_iter() + .map(|(uid, index_stats)| (uid, index_stats.into())) + .collect(), + } + } +} + #[get("/stats", wrap = "Authentication::Private")] async fn get_stats(data: web::Data) -> Result { - let mut response = StatsResponse { - database_size: 0, - last_update: None, - indexes: HashMap::new(), - }; - - for index in data.index_controller.list_indexes().await? { - let stats = data.index_controller.get_stats(index.uid.clone()).await?; - - response.database_size += stats.size; - response.last_update = Some(match response.last_update { - Some(last_update) => last_update.max(index.meta.updated_at), - None => index.meta.updated_at, - }); - response.indexes.insert(index.uid, stats.into()); - } + let response: StatsResponse = data.get_stats().await?.into(); Ok(HttpResponse::Ok().json(response)) }