rev_manager.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. use crate::rev_queue::{RevCommand, RevCommandSender, RevQueue};
  2. use crate::{
  3. RevisionPersistence, RevisionSnapshot, RevisionSnapshotController, RevisionSnapshotDiskCache,
  4. WSDataProviderDataSource,
  5. };
  6. use bytes::Bytes;
  7. use flowy_error::{internal_error, FlowyError, FlowyResult};
  8. use flowy_http_model::revision::{Revision, RevisionRange};
  9. use flowy_http_model::util::md5;
  10. use lib_infra::future::FutureResult;
  11. use std::sync::atomic::AtomicI64;
  12. use std::sync::atomic::Ordering::SeqCst;
  13. use std::sync::Arc;
  14. use tokio::sync::{mpsc, oneshot};
  15. pub trait RevisionCloudService: Send + Sync {
  16. /// Read the object's revision from remote
  17. /// Returns a list of revisions that used to build the object
  18. /// # Arguments
  19. ///
  20. /// * `user_id`: the id of the user
  21. /// * `object_id`: the id of the object
  22. ///
  23. fn fetch_object(&self, user_id: &str, object_id: &str) -> FutureResult<Vec<Revision>, FlowyError>;
  24. }
  25. pub trait RevisionObjectDeserializer: Send + Sync {
  26. type Output;
  27. /// Deserialize the list of revisions into an concrete object type.
  28. ///
  29. /// # Arguments
  30. ///
  31. /// * `object_id`: the id of the object
  32. /// * `revisions`: a list of revisions that represent the object
  33. ///
  34. fn deserialize_revisions(object_id: &str, revisions: Vec<Revision>) -> FlowyResult<Self::Output>;
  35. }
  36. pub trait RevisionObjectSerializer: Send + Sync {
  37. /// Serialize a list of revisions into one in `Bytes` format
  38. ///
  39. /// * `revisions`: a list of revisions will be serialized to `Bytes`
  40. ///
  41. fn combine_revisions(revisions: Vec<Revision>) -> FlowyResult<Bytes>;
  42. }
  43. /// `RevisionCompress` is used to compress multiple revisions into one revision
  44. ///
  45. pub trait RevisionMergeable: Send + Sync {
  46. fn merge_revisions(&self, _user_id: &str, object_id: &str, mut revisions: Vec<Revision>) -> FlowyResult<Revision> {
  47. if revisions.is_empty() {
  48. return Err(FlowyError::internal().context("Can't compact the empty revisions"));
  49. }
  50. if revisions.len() == 1 {
  51. return Ok(revisions.pop().unwrap());
  52. }
  53. let first_revision = revisions.first().unwrap();
  54. let last_revision = revisions.last().unwrap();
  55. let (base_rev_id, rev_id) = first_revision.pair_rev_id();
  56. let md5 = last_revision.md5.clone();
  57. let bytes = self.combine_revisions(revisions)?;
  58. Ok(Revision::new(object_id, base_rev_id, rev_id, bytes, md5))
  59. }
  60. fn combine_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes>;
  61. }
  62. pub struct RevisionManager<Connection> {
  63. pub object_id: String,
  64. user_id: String,
  65. rev_id_counter: Arc<RevIdCounter>,
  66. rev_persistence: Arc<RevisionPersistence<Connection>>,
  67. rev_snapshot: Arc<RevisionSnapshotController<Connection>>,
  68. rev_compress: Arc<dyn RevisionMergeable>,
  69. #[cfg(feature = "flowy_unit_test")]
  70. rev_ack_notifier: tokio::sync::broadcast::Sender<i64>,
  71. rev_queue: RevCommandSender,
  72. }
  73. impl<Connection: 'static> RevisionManager<Connection> {
  74. pub fn new<SP, C>(
  75. user_id: &str,
  76. object_id: &str,
  77. rev_persistence: RevisionPersistence<Connection>,
  78. rev_compress: C,
  79. snapshot_persistence: SP,
  80. ) -> Self
  81. where
  82. SP: 'static + RevisionSnapshotDiskCache,
  83. C: 'static + RevisionMergeable,
  84. {
  85. let rev_id_counter = Arc::new(RevIdCounter::new(0));
  86. let rev_compress = Arc::new(rev_compress);
  87. let rev_persistence = Arc::new(rev_persistence);
  88. let rev_snapshot = RevisionSnapshotController::new(
  89. user_id,
  90. object_id,
  91. snapshot_persistence,
  92. rev_id_counter.clone(),
  93. rev_persistence.clone(),
  94. rev_compress.clone(),
  95. );
  96. let (rev_queue, receiver) = mpsc::channel(1000);
  97. let queue = RevQueue::new(
  98. object_id.to_owned(),
  99. rev_id_counter.clone(),
  100. rev_persistence.clone(),
  101. rev_compress.clone(),
  102. receiver,
  103. );
  104. tokio::spawn(queue.run());
  105. Self {
  106. object_id: object_id.to_string(),
  107. user_id: user_id.to_owned(),
  108. rev_id_counter,
  109. rev_persistence,
  110. rev_snapshot: Arc::new(rev_snapshot),
  111. rev_compress,
  112. #[cfg(feature = "flowy_unit_test")]
  113. rev_ack_notifier: tokio::sync::broadcast::channel(1).0,
  114. rev_queue,
  115. }
  116. }
  117. #[tracing::instrument(level = "debug", skip_all, fields(deserializer, object) err)]
  118. pub async fn initialize<B>(&mut self, _cloud: Option<Arc<dyn RevisionCloudService>>) -> FlowyResult<B::Output>
  119. where
  120. B: RevisionObjectDeserializer,
  121. {
  122. let revision_records = self.rev_persistence.load_all_records(&self.object_id)?;
  123. tracing::Span::current().record("object", &self.object_id.as_str());
  124. tracing::Span::current().record("deserializer", &std::any::type_name::<B>());
  125. let revisions: Vec<Revision> = revision_records.iter().map(|record| record.revision.clone()).collect();
  126. let current_rev_id = revisions.last().as_ref().map(|revision| revision.rev_id).unwrap_or(0);
  127. match B::deserialize_revisions(&self.object_id, revisions) {
  128. Ok(object) => {
  129. let _ = self.rev_persistence.sync_revision_records(&revision_records).await?;
  130. self.rev_id_counter.set(current_rev_id);
  131. Ok(object)
  132. }
  133. Err(err) => match self.rev_snapshot.restore_from_snapshot::<B>(current_rev_id) {
  134. None => Err(err),
  135. Some((object, snapshot_rev)) => {
  136. let snapshot_rev_id = snapshot_rev.rev_id;
  137. let _ = self.rev_persistence.reset(vec![snapshot_rev]).await;
  138. // revision_records.retain(|record| record.revision.rev_id <= snapshot_rev_id);
  139. // let _ = self.rev_persistence.sync_revision_records(&revision_records).await?;
  140. self.rev_id_counter.set(snapshot_rev_id);
  141. Ok(object)
  142. }
  143. },
  144. }
  145. }
  146. pub async fn close(&self) {
  147. let _ = self.rev_persistence.compact_lagging_revisions(&self.rev_compress).await;
  148. }
  149. pub async fn generate_snapshot(&self) {
  150. self.rev_snapshot.generate_snapshot().await;
  151. }
  152. pub async fn read_snapshot(&self, rev_id: Option<i64>) -> FlowyResult<Option<RevisionSnapshot>> {
  153. match rev_id {
  154. None => self.rev_snapshot.read_last_snapshot(),
  155. Some(rev_id) => self.rev_snapshot.read_snapshot(rev_id),
  156. }
  157. }
  158. pub async fn load_revisions(&self) -> FlowyResult<Vec<Revision>> {
  159. let revisions = RevisionLoader {
  160. object_id: self.object_id.clone(),
  161. user_id: self.user_id.clone(),
  162. cloud: None,
  163. rev_persistence: self.rev_persistence.clone(),
  164. }
  165. .load_revisions()
  166. .await?;
  167. Ok(revisions)
  168. }
  169. #[tracing::instrument(level = "debug", skip(self, revisions), err)]
  170. pub async fn reset_object(&self, revisions: Vec<Revision>) -> FlowyResult<()> {
  171. let rev_id = pair_rev_id_from_revisions(&revisions).1;
  172. let _ = self.rev_persistence.reset(revisions).await?;
  173. self.rev_id_counter.set(rev_id);
  174. Ok(())
  175. }
  176. #[tracing::instrument(level = "debug", skip(self, revision), err)]
  177. pub async fn add_remote_revision(&self, revision: &Revision) -> Result<(), FlowyError> {
  178. if revision.bytes.is_empty() {
  179. return Err(FlowyError::internal().context("Remote revisions is empty"));
  180. }
  181. let _ = self.rev_persistence.add_ack_revision(revision).await?;
  182. self.rev_id_counter.set(revision.rev_id);
  183. Ok(())
  184. }
  185. /// Adds the revision that generated by user editing
  186. // #[tracing::instrument(level = "trace", skip_all, err)]
  187. pub async fn add_local_revision(&self, data: Bytes, object_md5: String) -> Result<i64, FlowyError> {
  188. if data.is_empty() {
  189. return Err(FlowyError::internal().context("The data of the revisions is empty"));
  190. }
  191. self.rev_snapshot.generate_snapshot_if_need();
  192. let (ret, rx) = oneshot::channel();
  193. self.rev_queue
  194. .send(RevCommand::RevisionData { data, object_md5, ret })
  195. .await
  196. .map_err(internal_error)?;
  197. rx.await.map_err(internal_error)?
  198. }
  199. #[tracing::instrument(level = "debug", skip(self), err)]
  200. pub async fn ack_revision(&self, rev_id: i64) -> Result<(), FlowyError> {
  201. if self.rev_persistence.ack_revision(rev_id).await.is_ok() {
  202. #[cfg(feature = "flowy_unit_test")]
  203. let _ = self.rev_ack_notifier.send(rev_id);
  204. }
  205. Ok(())
  206. }
  207. /// Returns the current revision id
  208. pub fn rev_id(&self) -> i64 {
  209. self.rev_id_counter.value()
  210. }
  211. pub async fn next_sync_rev_id(&self) -> Option<i64> {
  212. self.rev_persistence.next_sync_rev_id().await
  213. }
  214. pub fn next_rev_id_pair(&self) -> (i64, i64) {
  215. let cur = self.rev_id_counter.value();
  216. let next = self.rev_id_counter.next_id();
  217. (cur, next)
  218. }
  219. pub fn number_of_sync_revisions(&self) -> usize {
  220. self.rev_persistence.number_of_sync_records()
  221. }
  222. pub fn number_of_revisions_in_disk(&self) -> usize {
  223. self.rev_persistence.number_of_records_in_disk()
  224. }
  225. pub async fn get_revisions_in_range(&self, range: RevisionRange) -> Result<Vec<Revision>, FlowyError> {
  226. let revisions = self.rev_persistence.revisions_in_range(&range).await?;
  227. Ok(revisions)
  228. }
  229. pub async fn next_sync_revision(&self) -> FlowyResult<Option<Revision>> {
  230. self.rev_persistence.next_sync_revision().await
  231. }
  232. pub async fn get_revision(&self, rev_id: i64) -> Option<Revision> {
  233. self.rev_persistence.get(rev_id).await.map(|record| record.revision)
  234. }
  235. }
  236. impl<Connection: 'static> WSDataProviderDataSource for Arc<RevisionManager<Connection>> {
  237. fn next_revision(&self) -> FutureResult<Option<Revision>, FlowyError> {
  238. let rev_manager = self.clone();
  239. FutureResult::new(async move { rev_manager.next_sync_revision().await })
  240. }
  241. fn ack_revision(&self, rev_id: i64) -> FutureResult<(), FlowyError> {
  242. let rev_manager = self.clone();
  243. FutureResult::new(async move { (*rev_manager).ack_revision(rev_id).await })
  244. }
  245. fn current_rev_id(&self) -> i64 {
  246. self.rev_id()
  247. }
  248. }
  249. #[cfg(feature = "flowy_unit_test")]
  250. impl<Connection: 'static> RevisionManager<Connection> {
  251. pub async fn revision_cache(&self) -> Arc<RevisionPersistence<Connection>> {
  252. self.rev_persistence.clone()
  253. }
  254. pub fn ack_notify(&self) -> tokio::sync::broadcast::Receiver<i64> {
  255. self.rev_ack_notifier.subscribe()
  256. }
  257. pub fn get_all_revision_records(&self) -> FlowyResult<Vec<crate::disk::SyncRecord>> {
  258. self.rev_persistence.load_all_records(&self.object_id)
  259. }
  260. }
  261. pub struct RevisionLoader<Connection> {
  262. pub object_id: String,
  263. pub user_id: String,
  264. pub cloud: Option<Arc<dyn RevisionCloudService>>,
  265. pub rev_persistence: Arc<RevisionPersistence<Connection>>,
  266. }
  267. impl<Connection: 'static> RevisionLoader<Connection> {
  268. pub async fn load_revisions(&self) -> Result<Vec<Revision>, FlowyError> {
  269. let records = self.rev_persistence.load_all_records(&self.object_id)?;
  270. let revisions = records.into_iter().map(|record| record.revision).collect::<_>();
  271. Ok(revisions)
  272. }
  273. }
  274. /// Represents as the md5 of the revision object after applying the
  275. /// revision. For example, RevisionMD5 will be the md5 of the document
  276. /// content.
  277. #[derive(Debug, Clone)]
  278. pub struct RevisionMD5(String);
  279. impl RevisionMD5 {
  280. pub fn from_bytes<T: AsRef<[u8]>>(bytes: T) -> Result<Self, FlowyError> {
  281. Ok(RevisionMD5(md5(bytes)))
  282. }
  283. pub fn into_inner(self) -> String {
  284. self.0
  285. }
  286. pub fn is_equal(&self, s: &str) -> bool {
  287. self.0 == s
  288. }
  289. }
  290. impl std::convert::From<RevisionMD5> for String {
  291. fn from(md5: RevisionMD5) -> Self {
  292. md5.0
  293. }
  294. }
  295. impl std::convert::From<&str> for RevisionMD5 {
  296. fn from(s: &str) -> Self {
  297. Self(s.to_owned())
  298. }
  299. }
  300. impl std::convert::From<String> for RevisionMD5 {
  301. fn from(s: String) -> Self {
  302. Self(s)
  303. }
  304. }
  305. impl std::ops::Deref for RevisionMD5 {
  306. type Target = String;
  307. fn deref(&self) -> &Self::Target {
  308. &self.0
  309. }
  310. }
  311. impl PartialEq<Self> for RevisionMD5 {
  312. fn eq(&self, other: &Self) -> bool {
  313. self.0 == other.0
  314. }
  315. }
  316. impl std::cmp::Eq for RevisionMD5 {}
  317. fn pair_rev_id_from_revisions(revisions: &[Revision]) -> (i64, i64) {
  318. let mut rev_id = 0;
  319. revisions.iter().for_each(|revision| {
  320. if rev_id < revision.rev_id {
  321. rev_id = revision.rev_id;
  322. }
  323. });
  324. if rev_id > 0 {
  325. (rev_id - 1, rev_id)
  326. } else {
  327. (0, rev_id)
  328. }
  329. }
  330. #[derive(Debug)]
  331. pub struct RevIdCounter(pub AtomicI64);
  332. impl RevIdCounter {
  333. pub fn new(n: i64) -> Self {
  334. Self(AtomicI64::new(n))
  335. }
  336. pub fn next_id(&self) -> i64 {
  337. let _ = self.0.fetch_add(1, SeqCst);
  338. self.value()
  339. }
  340. pub fn value(&self) -> i64 {
  341. self.0.load(SeqCst)
  342. }
  343. pub fn set(&self, n: i64) {
  344. let _ = self.0.fetch_update(SeqCst, SeqCst, |_| Some(n));
  345. }
  346. }