2022-01-14 07:23:21 +00:00
|
|
|
use crate::{RevisionCache, RevisionRecord};
|
2022-01-01 08:16:06 +00:00
|
|
|
use dashmap::DashMap;
|
2021-12-15 15:01:50 +00:00
|
|
|
use flowy_collaboration::{
|
2022-01-14 07:23:21 +00:00
|
|
|
entities::revision::{RepeatedRevision, Revision, RevisionRange, RevisionState},
|
|
|
|
util::{pair_rev_id_from_revisions, RevIdCounter},
|
2021-12-15 15:01:50 +00:00
|
|
|
};
|
2022-01-14 07:23:21 +00:00
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
2021-12-13 05:55:44 +00:00
|
|
|
use lib_infra::future::FutureResult;
|
2022-01-01 08:16:06 +00:00
|
|
|
use std::{collections::VecDeque, sync::Arc};
|
2022-01-24 09:35:58 +00:00
|
|
|
use tokio::sync::RwLock;
|
2021-10-02 09:19:54 +00:00
|
|
|
|
2022-01-14 07:23:21 +00:00
|
|
|
pub trait RevisionCloudService: Send + Sync {
|
|
|
|
fn fetch_object(&self, user_id: &str, object_id: &str) -> FutureResult<Vec<Revision>, FlowyError>;
|
2021-10-02 09:19:54 +00:00
|
|
|
}
|
2021-10-01 11:39:08 +00:00
|
|
|
|
2022-01-14 07:23:21 +00:00
|
|
|
pub trait RevisionObjectBuilder: Send + Sync {
|
|
|
|
type Output;
|
|
|
|
fn build_with_revisions(object_id: &str, revisions: Vec<Revision>) -> FlowyResult<Self::Output>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RevisionManager {
|
|
|
|
pub object_id: String,
|
2021-12-09 14:28:11 +00:00
|
|
|
user_id: String,
|
2021-10-01 11:39:08 +00:00
|
|
|
rev_id_counter: RevIdCounter,
|
2022-01-14 07:23:21 +00:00
|
|
|
revision_cache: Arc<RevisionCache>,
|
2022-01-12 09:08:50 +00:00
|
|
|
revision_sync_seq: Arc<RevisionSyncSequence>,
|
2022-01-22 10:48:43 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "flowy_unit_test")]
|
2022-01-24 09:35:58 +00:00
|
|
|
revision_ack_notifier: tokio::sync::broadcast::Sender<i64>,
|
2021-10-01 11:39:08 +00:00
|
|
|
}
|
2021-10-02 09:19:54 +00:00
|
|
|
|
2022-01-14 07:23:21 +00:00
|
|
|
impl RevisionManager {
|
|
|
|
pub fn new(user_id: &str, object_id: &str, revision_cache: Arc<RevisionCache>) -> Self {
|
2021-10-07 12:46:29 +00:00
|
|
|
let rev_id_counter = RevIdCounter::new(0);
|
2022-01-12 09:08:50 +00:00
|
|
|
let revision_sync_seq = Arc::new(RevisionSyncSequence::new());
|
2022-01-22 10:48:43 +00:00
|
|
|
#[cfg(feature = "flowy_unit_test")]
|
2022-01-24 09:35:58 +00:00
|
|
|
let (revision_ack_notifier, _) = tokio::sync::broadcast::channel(1);
|
2022-01-22 10:48:43 +00:00
|
|
|
|
2021-10-03 03:33:19 +00:00
|
|
|
Self {
|
2022-01-14 07:23:21 +00:00
|
|
|
object_id: object_id.to_string(),
|
2021-12-09 14:28:11 +00:00
|
|
|
user_id: user_id.to_owned(),
|
2021-10-01 11:39:08 +00:00
|
|
|
rev_id_counter,
|
2022-01-12 09:08:50 +00:00
|
|
|
revision_cache,
|
|
|
|
revision_sync_seq,
|
2022-01-22 10:48:43 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "flowy_unit_test")]
|
|
|
|
revision_ack_notifier,
|
2021-10-03 03:33:19 +00:00
|
|
|
}
|
2021-10-01 11:39:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 07:23:21 +00:00
|
|
|
pub async fn load<Builder>(&mut self, cloud: Arc<dyn RevisionCloudService>) -> FlowyResult<Builder::Output>
|
|
|
|
where
|
|
|
|
Builder: RevisionObjectBuilder,
|
|
|
|
{
|
2022-01-23 14:33:47 +00:00
|
|
|
let (revisions, rev_id) = RevisionLoader {
|
2022-01-14 07:23:21 +00:00
|
|
|
object_id: self.object_id.clone(),
|
2021-12-17 16:23:26 +00:00
|
|
|
user_id: self.user_id.clone(),
|
2022-01-14 07:23:21 +00:00
|
|
|
cloud,
|
2022-01-12 09:08:50 +00:00
|
|
|
revision_cache: self.revision_cache.clone(),
|
|
|
|
revision_sync_seq: self.revision_sync_seq.clone(),
|
2021-12-17 16:23:26 +00:00
|
|
|
}
|
|
|
|
.load()
|
|
|
|
.await?;
|
2022-01-23 14:33:47 +00:00
|
|
|
self.rev_id_counter.set(rev_id);
|
2022-01-14 07:23:21 +00:00
|
|
|
Builder::build_with_revisions(&self.object_id, revisions)
|
2021-10-07 12:46:29 +00:00
|
|
|
}
|
|
|
|
|
2022-01-01 06:23:58 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, revisions), err)]
|
2022-01-14 07:23:21 +00:00
|
|
|
pub async fn reset_object(&self, revisions: RepeatedRevision) -> FlowyResult<()> {
|
2022-01-02 02:34:42 +00:00
|
|
|
let rev_id = pair_rev_id_from_revisions(&revisions).1;
|
2022-01-09 07:13:45 +00:00
|
|
|
let _ = self
|
2022-01-12 09:08:50 +00:00
|
|
|
.revision_cache
|
2022-01-14 07:23:21 +00:00
|
|
|
.reset_with_revisions(&self.object_id, revisions.into_inner())
|
2022-01-09 07:13:45 +00:00
|
|
|
.await?;
|
2022-01-02 02:34:42 +00:00
|
|
|
self.rev_id_counter.set(rev_id);
|
|
|
|
Ok(())
|
2022-01-01 06:23:58 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 02:34:42 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, revision), err)]
|
2021-12-14 10:04:51 +00:00
|
|
|
pub async fn add_remote_revision(&self, revision: &Revision) -> Result<(), FlowyError> {
|
2022-01-01 15:09:13 +00:00
|
|
|
if revision.delta_data.is_empty() {
|
|
|
|
return Err(FlowyError::internal().context("Delta data should be empty"));
|
|
|
|
}
|
2022-01-12 09:08:50 +00:00
|
|
|
let _ = self
|
|
|
|
.revision_cache
|
|
|
|
.add(revision.clone(), RevisionState::Ack, true)
|
|
|
|
.await?;
|
2022-01-07 09:37:11 +00:00
|
|
|
self.rev_id_counter.set(revision.rev_id);
|
2021-12-13 14:46:35 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-01 08:16:06 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, revision))]
|
2021-12-14 10:04:51 +00:00
|
|
|
pub async fn add_local_revision(&self, revision: &Revision) -> Result<(), FlowyError> {
|
2022-01-01 15:09:13 +00:00
|
|
|
if revision.delta_data.is_empty() {
|
|
|
|
return Err(FlowyError::internal().context("Delta data should be empty"));
|
|
|
|
}
|
|
|
|
|
2022-01-12 09:08:50 +00:00
|
|
|
let record = self
|
|
|
|
.revision_cache
|
2022-01-14 07:23:21 +00:00
|
|
|
.add(revision.clone(), RevisionState::Sync, true)
|
2022-01-12 09:08:50 +00:00
|
|
|
.await?;
|
|
|
|
self.revision_sync_seq.add_revision_record(record).await?;
|
2021-10-07 12:46:29 +00:00
|
|
|
Ok(())
|
2021-10-01 11:39:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-01 08:16:06 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(self), err)]
|
2021-12-16 13:31:36 +00:00
|
|
|
pub async fn ack_revision(&self, rev_id: i64) -> Result<(), FlowyError> {
|
2022-01-24 13:23:57 +00:00
|
|
|
// For the moment, we comment out the following code in order to cause sync bugs.
|
|
|
|
// if self.revision_sync_seq.ack(&rev_id).await.is_ok() {
|
|
|
|
// self.revision_cache.ack(rev_id).await;
|
|
|
|
//
|
|
|
|
// #[cfg(feature = "flowy_unit_test")]
|
|
|
|
// let _ = self.revision_ack_notifier.send(rev_id);
|
|
|
|
// }
|
2021-10-01 11:39:08 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-24 09:35:58 +00:00
|
|
|
pub fn rev_id(&self) -> i64 {
|
|
|
|
self.rev_id_counter.value()
|
|
|
|
}
|
2021-10-01 11:39:08 +00:00
|
|
|
|
2022-01-02 02:34:42 +00:00
|
|
|
pub fn next_rev_id_pair(&self) -> (i64, i64) {
|
2021-10-01 11:39:08 +00:00
|
|
|
let cur = self.rev_id_counter.value();
|
|
|
|
let next = self.rev_id_counter.next();
|
|
|
|
(cur, next)
|
|
|
|
}
|
|
|
|
|
2021-12-25 13:44:45 +00:00
|
|
|
pub async fn get_revisions_in_range(&self, range: RevisionRange) -> Result<Vec<Revision>, FlowyError> {
|
2022-01-14 07:23:21 +00:00
|
|
|
debug_assert!(range.object_id == self.object_id);
|
2022-01-12 09:08:50 +00:00
|
|
|
let revisions = self.revision_cache.revisions_in_range(range.clone()).await?;
|
2021-12-25 13:44:45 +00:00
|
|
|
Ok(revisions)
|
2021-10-04 06:24:35 +00:00
|
|
|
}
|
2021-12-08 13:51:06 +00:00
|
|
|
|
2022-01-01 08:16:06 +00:00
|
|
|
pub fn next_sync_revision(&self) -> FutureResult<Option<Revision>, FlowyError> {
|
2022-01-12 09:08:50 +00:00
|
|
|
let revision_sync_seq = self.revision_sync_seq.clone();
|
|
|
|
let revision_cache = self.revision_cache.clone();
|
2022-01-01 08:16:06 +00:00
|
|
|
FutureResult::new(async move {
|
2022-01-12 09:08:50 +00:00
|
|
|
match revision_sync_seq.next_sync_revision_record().await {
|
|
|
|
None => match revision_sync_seq.next_sync_rev_id().await {
|
2022-01-01 08:16:06 +00:00
|
|
|
None => Ok(None),
|
2022-01-12 09:08:50 +00:00
|
|
|
Some(rev_id) => Ok(revision_cache.get(rev_id).await.map(|record| record.revision)),
|
2022-01-01 08:16:06 +00:00
|
|
|
},
|
|
|
|
Some((_, record)) => Ok(Some(record.revision)),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-12-18 10:35:45 +00:00
|
|
|
|
2022-01-24 09:35:58 +00:00
|
|
|
pub async fn latest_revision(&self) -> Revision {
|
|
|
|
self.revision_cache.latest_revision().await
|
|
|
|
}
|
2021-12-25 13:44:45 +00:00
|
|
|
|
|
|
|
pub async fn get_revision(&self, rev_id: i64) -> Option<Revision> {
|
2022-01-12 09:08:50 +00:00
|
|
|
self.revision_cache.get(rev_id).await.map(|record| record.revision)
|
2021-12-25 13:44:45 +00:00
|
|
|
}
|
2021-10-02 09:19:54 +00:00
|
|
|
}
|
2021-12-08 06:17:40 +00:00
|
|
|
|
2022-01-02 02:34:42 +00:00
|
|
|
struct RevisionSyncSequence {
|
2022-01-01 08:16:06 +00:00
|
|
|
revs_map: Arc<DashMap<i64, RevisionRecord>>,
|
|
|
|
local_revs: Arc<RwLock<VecDeque<i64>>>,
|
|
|
|
}
|
|
|
|
|
2022-01-02 02:34:42 +00:00
|
|
|
impl std::default::Default for RevisionSyncSequence {
|
2022-01-01 08:16:06 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
let local_revs = Arc::new(RwLock::new(VecDeque::new()));
|
2022-01-02 02:34:42 +00:00
|
|
|
RevisionSyncSequence {
|
2022-01-01 08:16:06 +00:00
|
|
|
revs_map: Arc::new(DashMap::new()),
|
|
|
|
local_revs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 02:34:42 +00:00
|
|
|
impl RevisionSyncSequence {
|
2022-01-24 09:35:58 +00:00
|
|
|
fn new() -> Self {
|
|
|
|
RevisionSyncSequence::default()
|
|
|
|
}
|
2022-01-01 08:16:06 +00:00
|
|
|
|
2022-01-12 09:08:50 +00:00
|
|
|
async fn add_revision_record(&self, record: RevisionRecord) -> FlowyResult<()> {
|
2022-01-14 07:23:21 +00:00
|
|
|
if !record.state.is_need_sync() {
|
2022-01-12 09:08:50 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2022-01-01 08:16:06 +00:00
|
|
|
// The last revision's rev_id must be greater than the new one.
|
|
|
|
if let Some(rev_id) = self.local_revs.read().await.back() {
|
|
|
|
if *rev_id >= record.revision.rev_id {
|
2022-01-12 09:08:50 +00:00
|
|
|
return Err(
|
|
|
|
FlowyError::internal().context(format!("The new revision's id must be greater than {}", rev_id))
|
|
|
|
);
|
2022-01-01 08:16:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.local_revs.write().await.push_back(record.revision.rev_id);
|
|
|
|
self.revs_map.insert(record.revision.rev_id, record);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn ack(&self, rev_id: &i64) -> FlowyResult<()> {
|
|
|
|
if let Some(pop_rev_id) = self.next_sync_rev_id().await {
|
|
|
|
if &pop_rev_id != rev_id {
|
|
|
|
let desc = format!(
|
|
|
|
"The ack rev_id:{} is not equal to the current rev_id:{}",
|
|
|
|
rev_id, pop_rev_id
|
|
|
|
);
|
|
|
|
return Err(FlowyError::internal().context(desc));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.revs_map.remove(&pop_rev_id);
|
|
|
|
let _ = self.local_revs.write().await.pop_front();
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-12 09:08:50 +00:00
|
|
|
async fn next_sync_revision_record(&self) -> Option<(i64, RevisionRecord)> {
|
2022-01-01 08:16:06 +00:00
|
|
|
match self.local_revs.read().await.front() {
|
|
|
|
None => None,
|
|
|
|
Some(rev_id) => self.revs_map.get(rev_id).map(|r| (*r.key(), r.value().clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 09:35:58 +00:00
|
|
|
async fn next_sync_rev_id(&self) -> Option<i64> {
|
|
|
|
self.local_revs.read().await.front().copied()
|
|
|
|
}
|
2021-12-08 06:17:40 +00:00
|
|
|
}
|
2021-12-17 16:23:26 +00:00
|
|
|
|
|
|
|
struct RevisionLoader {
|
2022-01-14 07:23:21 +00:00
|
|
|
object_id: String,
|
2021-12-17 16:23:26 +00:00
|
|
|
user_id: String,
|
2022-01-14 07:23:21 +00:00
|
|
|
cloud: Arc<dyn RevisionCloudService>,
|
|
|
|
revision_cache: Arc<RevisionCache>,
|
2022-01-12 09:08:50 +00:00
|
|
|
revision_sync_seq: Arc<RevisionSyncSequence>,
|
2021-12-17 16:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RevisionLoader {
|
2022-01-23 14:33:47 +00:00
|
|
|
async fn load(&self) -> Result<(Vec<Revision>, i64), FlowyError> {
|
2022-01-14 07:23:21 +00:00
|
|
|
let records = self.revision_cache.batch_get(&self.object_id)?;
|
2021-12-17 16:23:26 +00:00
|
|
|
let revisions: Vec<Revision>;
|
2022-01-23 14:33:47 +00:00
|
|
|
let mut rev_id = 0;
|
2021-12-17 16:23:26 +00:00
|
|
|
if records.is_empty() {
|
2022-01-14 07:23:21 +00:00
|
|
|
let remote_revisions = self.cloud.fetch_object(&self.user_id, &self.object_id).await?;
|
|
|
|
for revision in &remote_revisions {
|
2022-01-23 14:33:47 +00:00
|
|
|
rev_id = revision.rev_id;
|
2022-01-14 07:23:21 +00:00
|
|
|
let _ = self
|
|
|
|
.revision_cache
|
|
|
|
.add(revision.clone(), RevisionState::Ack, true)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
revisions = remote_revisions;
|
2021-12-17 16:23:26 +00:00
|
|
|
} else {
|
2022-01-23 14:33:47 +00:00
|
|
|
for record in records.clone() {
|
|
|
|
let f = || async {
|
|
|
|
rev_id = record.revision.rev_id;
|
|
|
|
if record.state == RevisionState::Sync {
|
|
|
|
// Sync the records if their state is RevisionState::Sync.
|
2022-01-12 09:08:50 +00:00
|
|
|
let _ = self.revision_sync_seq.add_revision_record(record.clone()).await?;
|
|
|
|
let _ = self.revision_cache.add(record.revision, record.state, false).await?;
|
2022-01-02 02:34:42 +00:00
|
|
|
}
|
2022-01-23 14:33:47 +00:00
|
|
|
Ok::<(), FlowyError>(())
|
|
|
|
};
|
|
|
|
match f().await {
|
2022-01-24 09:35:58 +00:00
|
|
|
Ok(_) => {}
|
2022-01-23 14:33:47 +00:00
|
|
|
Err(e) => tracing::error!("[RevisionLoader]: {}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 16:23:26 +00:00
|
|
|
revisions = records.into_iter().map(|record| record.revision).collect::<_>();
|
|
|
|
}
|
|
|
|
|
2022-01-23 14:33:47 +00:00
|
|
|
if let Some(revision) = revisions.last() {
|
|
|
|
debug_assert_eq!(rev_id, revision.rev_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((revisions, rev_id))
|
2021-12-17 16:23:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-01 08:16:06 +00:00
|
|
|
#[cfg(feature = "flowy_unit_test")]
|
2022-01-02 02:34:42 +00:00
|
|
|
impl RevisionSyncSequence {
|
2022-01-01 08:16:06 +00:00
|
|
|
#[allow(dead_code)]
|
2022-01-24 09:35:58 +00:00
|
|
|
pub fn revs_map(&self) -> Arc<DashMap<i64, RevisionRecord>> {
|
|
|
|
self.revs_map.clone()
|
|
|
|
}
|
2022-01-01 08:16:06 +00:00
|
|
|
#[allow(dead_code)]
|
2022-01-24 09:35:58 +00:00
|
|
|
pub fn pending_revs(&self) -> Arc<RwLock<VecDeque<i64>>> {
|
|
|
|
self.local_revs.clone()
|
|
|
|
}
|
2022-01-01 08:16:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "flowy_unit_test")]
|
2022-01-14 07:23:21 +00:00
|
|
|
impl RevisionManager {
|
2022-01-24 09:35:58 +00:00
|
|
|
pub fn revision_cache(&self) -> Arc<RevisionCache> {
|
|
|
|
self.revision_cache.clone()
|
|
|
|
}
|
|
|
|
pub fn revision_ack_receiver(&self) -> tokio::sync::broadcast::Receiver<i64> {
|
|
|
|
self.revision_ack_notifier.subscribe()
|
|
|
|
}
|
2022-01-01 08:16:06 +00:00
|
|
|
}
|