From bc7b48dfeb4171cf3f74a049ea42493de6b62f23 Mon Sep 17 00:00:00 2001 From: DarkSky Date: Thu, 26 Oct 2023 12:38:07 +0800 Subject: [PATCH] feat: offload merge update in async --- libs/jwst-storage/src/storage/docs/database.rs | 9 +++++++-- libs/jwst-storage/src/storage/docs/utils.rs | 3 +-- libs/jwst-storage/src/types.rs | 2 ++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/libs/jwst-storage/src/storage/docs/database.rs b/libs/jwst-storage/src/storage/docs/database.rs index a5f41f45..3cc136d9 100644 --- a/libs/jwst-storage/src/storage/docs/database.rs +++ b/libs/jwst-storage/src/storage/docs/database.rs @@ -3,6 +3,7 @@ use std::collections::hash_map::Entry; use jwst_codec::{encode_update_as_message, CrdtReader, Doc, DocOptions, RawDecoder, StateVector}; use jwst_core::{DocStorage, Workspace}; use sea_orm::Condition; +use tokio::task::spawn_blocking; use super::{entities::prelude::*, *}; use crate::types::JwstStorageResult; @@ -258,7 +259,9 @@ impl DocDBStorage { let can_merge = all_data.len() > 1; - let doc = utils::migrate_update(all_data, doc)?; + let doc = spawn_blocking(|| utils::migrate_update(all_data, doc)) + .await + .map_err(JwstStorageError::BlockingThread)??; if can_merge { let update = doc.encode_state_as_update_v1(&StateVector::default())?; @@ -340,7 +343,9 @@ impl DocStorage for DocDBStorage { return Ok(None); } - let doc = utils::migrate_update(records, Doc::default())?; + let doc = spawn_blocking(|| utils::migrate_update(records, Doc::default())) + .await + .map_err(JwstStorageError::BlockingThread)??; Ok(Some(doc)) } diff --git a/libs/jwst-storage/src/storage/docs/utils.rs b/libs/jwst-storage/src/storage/docs/utils.rs index 3a7efa7c..93e1010f 100644 --- a/libs/jwst-storage/src/storage/docs/utils.rs +++ b/libs/jwst-storage/src/storage/docs/utils.rs @@ -12,8 +12,7 @@ pub fn migrate_update(update_records: Vec<::Model>, mut doc warn!("update {} merge failed, skip it: {:?}", id, e); } } - // temporarily disable due to the multiple client issue - // doc.gc()?; + doc.gc()?; doc.publisher.start(); diff --git a/libs/jwst-storage/src/types.rs b/libs/jwst-storage/src/types.rs index f71543ad..c9ab4c8c 100644 --- a/libs/jwst-storage/src/types.rs +++ b/libs/jwst-storage/src/types.rs @@ -5,6 +5,8 @@ use thiserror::Error; pub enum JwstStorageError { #[error("failed to init sync thread")] SyncThread(std::io::Error), + #[error("failed to execute blocking thread")] + BlockingThread(tokio::task::JoinError), #[error("failed to create data directory")] CreateDataFolder(std::io::Error), #[error("db manipulate error: {0}")]