chore: refactor logs (#236)

* chore: add logs

* chore: return ws state

* fix: init the collab from disk
This commit is contained in:
Nathan.fooo 2023-12-27 05:20:40 +08:00 committed by GitHub
parent 6b119fbf5b
commit a455c9de8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 51 additions and 21 deletions

View File

@ -819,7 +819,7 @@ impl Client {
AppResponse::<()>::from_response(resp).await?.into_error() AppResponse::<()>::from_response(resp).await?.into_error()
} }
#[instrument(level = "debug", skip_all, err)] #[instrument(level = "debug", skip_all)]
pub async fn get_collab( pub async fn get_collab(
&self, &self,
params: QueryCollabParams, params: QueryCollabParams,

View File

@ -310,6 +310,10 @@ impl WSClient {
self.sender.clone() self.sender.clone()
} }
pub fn get_state(&self) -> ConnectState {
self.state_notify.lock().state.clone()
}
async fn set_state(&self, state: ConnectState) { async fn set_state(&self, state: ConnectState) {
self.state_notify.lock().set_state(state); self.state_notify.lock().set_state(state);
} }

View File

@ -157,8 +157,8 @@ pub async fn insert_into_af_collab(
tracing::Level::TRACE, tracing::Level::TRACE,
"did insert new collab row: {}:{}:{}", "did insert new collab row: {}:{}:{}",
uid, uid,
workspace_id,
params.object_id, params.object_id,
workspace_id
); );
}, },
} }

View File

@ -84,6 +84,7 @@ pub trait CollabStorage: Send + Sync + 'static {
/// # Arguments /// # Arguments
/// ///
/// * `params` - The parameters required to query a collab object. /// * `params` - The parameters required to query a collab object.
/// * `force_from_disk` - If `true`, the data will be retrieved from the disk instead of the cache.
/// ///
/// # Returns /// # Returns
/// ///
@ -92,6 +93,7 @@ pub trait CollabStorage: Send + Sync + 'static {
&self, &self,
uid: &i64, uid: &i64,
params: QueryCollabParams, params: QueryCollabParams,
force_from_disk: bool,
) -> DatabaseResult<EncodedCollabV1>; ) -> DatabaseResult<EncodedCollabV1>;
async fn batch_get_collab( async fn batch_get_collab(
@ -152,8 +154,12 @@ where
&self, &self,
uid: &i64, uid: &i64,
params: QueryCollabParams, params: QueryCollabParams,
force_from_disk: bool,
) -> DatabaseResult<EncodedCollabV1> { ) -> DatabaseResult<EncodedCollabV1> {
self.as_ref().get_collab_encoded_v1(uid, params).await self
.as_ref()
.get_collab_encoded_v1(uid, params, force_from_disk)
.await
} }
async fn batch_get_collab( async fn batch_get_collab(
@ -253,6 +259,7 @@ impl CollabStorage for CollabStoragePgImpl {
&self, &self,
_uid: &i64, _uid: &i64,
params: QueryCollabParams, params: QueryCollabParams,
_force_from_disk: bool,
) -> DatabaseResult<EncodedCollabV1> { ) -> DatabaseResult<EncodedCollabV1> {
match collab_db_ops::select_blob_from_af_collab( match collab_db_ops::select_blob_from_af_collab(
&self.pg_pool, &self.pg_pool,

View File

@ -173,7 +173,11 @@ where
async fn init(&self, object_id: &str, _origin: &CollabOrigin, doc: &Doc) { async fn init(&self, object_id: &str, _origin: &CollabOrigin, doc: &Doc) {
let params = QueryCollabParams::new(object_id, self.collab_type.clone(), &self.workspace_id); let params = QueryCollabParams::new(object_id, self.collab_type.clone(), &self.workspace_id);
match self.storage.get_collab_encoded_v1(&self.uid, params).await { match self
.storage
.get_collab_encoded_v1(&self.uid, params, true)
.await
{
Ok(encoded_collab_v1) => match init_collab_with_raw_data(object_id, &encoded_collab_v1, doc) Ok(encoded_collab_v1) => match init_collab_with_raw_data(object_id, &encoded_collab_v1, doc)
.await .await
{ {

View File

@ -287,7 +287,7 @@ async fn get_collab_handler(
.map_err(AppResponseError::from)?; .map_err(AppResponseError::from)?;
let data = state let data = state
.collab_storage .collab_storage
.get_collab_encoded_v1(&uid, payload.into_inner()) .get_collab_encoded_v1(&uid, payload.into_inner(), false)
.await .await
.map_err(AppResponseError::from)?; .map_err(AppResponseError::from)?;
@ -325,6 +325,7 @@ async fn create_collab_snapshot_handler(
.get_collab_encoded_v1( .get_collab_encoded_v1(
&uid, &uid,
QueryCollabParams::new(&object_id, collab_type, &workspace_id), QueryCollabParams::new(&object_id, collab_type, &workspace_id),
false,
) )
.await? .await?
.encode_to_bytes() .encode_to_bytes()

View File

@ -291,7 +291,7 @@ impl CollabAccessControl for CasbinCollabAccessControl {
access_level.ok_or(AppError::RecordNotFound(format!( access_level.ok_or(AppError::RecordNotFound(format!(
"collab:{} does not exist or user:{} is not a member", "collab:{} does not exist or user:{} is not a member",
uid, oid oid, uid
))) )))
} }

View File

@ -35,6 +35,7 @@ pub(crate) async fn enforcer_update(
}?; }?;
let mut enforcer = enforcer.write().await; let mut enforcer = enforcer.write().await;
// TODO(jireh): if the policy already exists and doesn't need to be updated, return early.
enforcer_remove(&mut enforcer, uid, obj).await?; enforcer_remove(&mut enforcer, uid, obj).await?;
event!( event!(
tracing::Level::INFO, tracing::Level::INFO,

View File

@ -24,15 +24,23 @@ where
C: CollabAccessControl, C: CollabAccessControl,
{ {
for params in params_list { for params in params_list {
if !params.override_if_exist if database::collab::collab_exists(pg_pool, &params.object_id).await? {
&& database::collab::collab_exists(pg_pool, &params.object_id).await? if params.override_if_exist {
{ event!(
// When calling this function, the caller should have already checked if the collab exists. tracing::Level::INFO,
return Err(AppError::RecordAlreadyExists(format!( "Collab:{} with object_id {} already exists, override it",
"Collab with object_id {} already exists", params.collab_type,
params.object_id params.object_id
))); );
} else {
// When calling this function, the caller should have already checked if the collab exists.
return Err(AppError::RecordAlreadyExists(format!(
"Collab:{} with object_id {} already exists",
params.collab_type, params.object_id
)));
}
} }
collab_access_control collab_access_control
.cache_collab_access_level( .cache_collab_access_level(
CollabUserId::UserUuid(user_uuid), CollabUserId::UserUuid(user_uuid),

View File

@ -146,6 +146,7 @@ where
&self, &self,
uid: &i64, uid: &i64,
params: QueryCollabParams, params: QueryCollabParams,
force_from_disk: bool,
) -> DatabaseResult<EncodedCollabV1> { ) -> DatabaseResult<EncodedCollabV1> {
params.validate()?; params.validate()?;
self self
@ -153,12 +154,16 @@ where
.get_collab_access_level(uid, &params.object_id) .get_collab_access_level(uid, &params.object_id)
.await?; .await?;
let collab = self let collab = if force_from_disk {
.collab_by_object_id None
.read() } else {
.await self
.get(&params.object_id) .collab_by_object_id
.and_then(|collab| collab.upgrade()); .read()
.await
.get(&params.object_id)
.and_then(|collab| collab.upgrade())
};
match collab { match collab {
None => { None => {
@ -167,7 +172,7 @@ where
"Get collab data:{} from disk", "Get collab data:{} from disk",
params.object_id params.object_id
); );
self.inner.get_collab_encoded_v1(uid, params).await self.inner.get_collab_encoded_v1(uid, params, true).await
}, },
Some(collab) => { Some(collab) => {
event!( event!(