Skip to content

Commit 735e80a

Browse files
[ENG-1776] Delete cloud ops after they've been ingested (#2512)
* delete cloud ops after they've been ingested * give wait_tx to p2p sync ingest * remove Ingested event * add sync docs for setting relation fields * Update core/crates/sync/README.md Co-authored-by: Oscar Beaumont <oscar@otbeaumont.me> --------- Co-authored-by: Oscar Beaumont <oscar@otbeaumont.me>
1 parent ab97572 commit 735e80a

File tree

7 files changed

+77
-18
lines changed

7 files changed

+77
-18
lines changed

core/crates/sync/README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
Spacedrive's sync system. Consumes types and helpers from `sd-sync`.
44

5-
## Using Sync
6-
75
### Creating Records
86

97
Prepare a sync id by creating or obtaining its value,
@@ -101,3 +99,34 @@ sync.write_ops(
10199
)
102100
)
103101
```
102+
103+
### Setting Relation Fields
104+
105+
Setting relation fields requires providing the Sync ID of the relation.
106+
Setting the relation field's scalar fields instead will not properly sync then relation,
107+
usually because the scalar fields are local and disconnected from the Sync ID.
108+
109+
```rs
110+
let (sync_params, db_params): (Vec<_>, Vec<_>) = [
111+
sync_db_entry!(
112+
prisma_sync::object::SyncId { pub_id: object_pub_id },
113+
file_path::object
114+
)
115+
].into_iter().unzip();
116+
117+
sync.write_ops(
118+
db,
119+
(
120+
sync.shared_update(
121+
prisma_sync::file_path::SyncId {
122+
pub_id: file_path_pub_id
123+
},
124+
sync_params
125+
),
126+
db.file_path().update(
127+
file_path::id::equals(file_path_id),
128+
db_params
129+
)
130+
)
131+
)
132+
```

core/crates/sync/src/db_operation.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@ impl cloud_crdt_include::Data {
4141
Uuid::from_slice(&self.instance.pub_id).unwrap()
4242
}
4343

44-
pub fn into_operation(self) -> CRDTOperation {
45-
CRDTOperation {
46-
instance: self.instance(),
47-
timestamp: self.timestamp(),
48-
record_id: rmp_serde::from_slice(&self.record_id).unwrap(),
49-
model: self.model as u16,
50-
data: serde_json::from_slice(&self.data).unwrap(),
51-
}
44+
pub fn into_operation(self) -> (i32, CRDTOperation) {
45+
(
46+
self.id,
47+
CRDTOperation {
48+
instance: self.instance(),
49+
timestamp: self.timestamp(),
50+
record_id: rmp_serde::from_slice(&self.record_id).unwrap(),
51+
model: self.model as u16,
52+
data: serde_json::from_slice(&self.data).unwrap(),
53+
},
54+
)
5255
}
5356
}
5457

core/crates/sync/src/ingest.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub enum Request {
3131
timestamps: Vec<(Uuid, NTP64)>,
3232
tx: oneshot::Sender<()>,
3333
},
34-
Ingested,
34+
// Ingested,
3535
FinishedIngesting,
3636
}
3737

@@ -129,6 +129,10 @@ impl Actor {
129129
}
130130
}
131131

132+
if let Some(tx) = event.wait_tx {
133+
tx.send(()).ok();
134+
}
135+
132136
match event.has_more {
133137
true => State::RetrievingMessages,
134138
false => {
@@ -421,7 +425,7 @@ impl Actor {
421425

422426
self.timestamps.write().await.insert(instance, new_ts);
423427

424-
self.io.req_tx.send(Request::Ingested).await.ok();
428+
// self.io.req_tx.send(Request::Ingested).await.ok();
425429

426430
Ok(())
427431
}
@@ -445,6 +449,7 @@ pub struct MessagesEvent {
445449
pub instance_id: Uuid,
446450
pub messages: CompressedCRDTOperations,
447451
pub has_more: bool,
452+
pub wait_tx: Option<oneshot::Sender<()>>,
448453
}
449454

450455
impl ActorTypes for Actor {

core/crates/sync/src/manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl Manager {
246246
pub async fn get_cloud_ops(
247247
&self,
248248
args: GetOpsArgs,
249-
) -> prisma_client_rust::Result<Vec<CRDTOperation>> {
249+
) -> prisma_client_rust::Result<Vec<(i32, CRDTOperation)>> {
250250
let db = &self.db;
251251

252252
macro_rules! db_args {

core/crates/sync/tests/mock_instance.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,14 @@ impl Instance {
128128
messages: CompressedCRDTOperations::new(messages),
129129
has_more: false,
130130
instance_id: instance1.id,
131+
wait_tx: None,
131132
}))
132133
.await
133134
.unwrap();
134135
}
135-
ingest::Request::Ingested => {
136-
instance2.sync.tx.send(SyncMessage::Ingested).ok();
137-
}
136+
// ingest::Request::Ingested => {
137+
// instance2.sync.tx.send(SyncMessage::Ingested).ok();
138+
// }
138139
ingest::Request::FinishedIngesting => {}
139140
}
140141
}

core/src/cloud/sync/ingest.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use sd_prisma::prisma::cloud_crdt_operation;
12
use sd_sync::CompressedCRDTOperations;
23
use std::sync::{
34
atomic::{AtomicBool, Ordering},
@@ -44,13 +45,15 @@ pub async fn run_actor(
4445
_ => continue,
4546
};
4647

47-
let ops = err_break!(
48+
let (ops_ids, ops): (Vec<_>, Vec<_>) = err_break!(
4849
sync.get_cloud_ops(GetOpsArgs {
4950
clocks: timestamps,
5051
count: OPS_PER_REQUEST,
5152
})
5253
.await
53-
);
54+
)
55+
.into_iter()
56+
.unzip();
5457

5558
if ops.is_empty() {
5659
break;
@@ -63,16 +66,29 @@ pub async fn run_actor(
6366
ops.last().map(|operation| operation.timestamp.as_u64()),
6467
);
6568

69+
let (wait_tx, wait_rx) = tokio::sync::oneshot::channel::<()>();
70+
6671
err_break!(
6772
sync.ingest
6873
.event_tx
6974
.send(sd_core_sync::Event::Messages(MessagesEvent {
7075
instance_id: sync.instance,
7176
has_more: ops.len() == OPS_PER_REQUEST as usize,
7277
messages: CompressedCRDTOperations::new(ops),
78+
wait_tx: Some(wait_tx)
7379
}))
7480
.await
7581
);
82+
83+
err_break!(wait_rx.await);
84+
85+
err_break!(
86+
sync.db
87+
.cloud_crdt_operation()
88+
.delete_many(vec![cloud_crdt_operation::id::in_vec(ops_ids)])
89+
.exec()
90+
.await
91+
);
7692
}
7793
}
7894
}

core/src/p2p/sync/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,20 @@ mod responder {
244244

245245
let rx::Operations(ops) = rx::Operations::from_stream(stream).await.unwrap();
246246

247+
let (wait_tx, wait_rx) = tokio::sync::oneshot::channel::<()>();
248+
247249
ingest
248250
.event_tx
249251
.send(Event::Messages(MessagesEvent {
250252
instance_id: library.sync.instance,
251253
has_more: ops.len() == OPS_PER_REQUEST as usize,
252254
messages: ops,
255+
wait_tx: Some(wait_tx),
253256
}))
254257
.await
255258
.expect("TODO: Handle ingest channel closed, so we don't loose ops");
259+
260+
wait_rx.await.unwrap()
256261
}
257262

258263
debug!("Sync responder done");

0 commit comments

Comments
 (0)