2024-08-08 00:28:15 +02:00
|
|
|
use std::sync::Arc;
|
2023-11-10 04:29:50 +01:00
|
|
|
|
2024-08-13 00:36:09 +02:00
|
|
|
use diamond_types::LocalVersion;
|
|
|
|
use tokio::sync::{mpsc, oneshot, watch};
|
2024-09-05 23:26:56 +02:00
|
|
|
use tonic::Streaming;
|
2024-01-25 02:13:45 +01:00
|
|
|
use uuid::Uuid;
|
2023-08-11 15:33:40 +02:00
|
|
|
|
2024-08-15 19:32:01 +02:00
|
|
|
use crate::api::controller::{ControllerCallback, ControllerWorker};
|
2023-11-17 05:45:31 +01:00
|
|
|
use crate::api::TextChange;
|
2024-09-01 03:08:43 +02:00
|
|
|
use crate::ext::{IgnorableError, InternallyMutable};
|
2024-08-13 00:36:09 +02:00
|
|
|
|
2024-03-09 19:59:36 +01:00
|
|
|
use codemp_proto::buffer::{BufferEvent, Operation};
|
2023-08-11 15:33:40 +02:00
|
|
|
|
2024-08-08 00:28:15 +02:00
|
|
|
use super::controller::{BufferController, BufferControllerInner};
|
2023-08-11 15:33:40 +02:00
|
|
|
|
2024-08-17 00:17:01 +02:00
|
|
|
pub(crate) type DeltaOp = (LocalVersion, Option<TextChange>);
|
|
|
|
pub(crate) type DeltaRequest = (LocalVersion, oneshot::Sender<DeltaOp>);
|
|
|
|
|
2024-01-25 02:13:45 +01:00
|
|
|
pub(crate) struct BufferWorker {
|
2024-08-13 00:36:09 +02:00
|
|
|
user_id: Uuid,
|
|
|
|
latest_version: watch::Sender<diamond_types::LocalVersion>,
|
2024-08-14 15:55:18 +02:00
|
|
|
ops_in: mpsc::UnboundedReceiver<(TextChange, oneshot::Sender<LocalVersion>)>,
|
2023-11-30 03:03:09 +01:00
|
|
|
poller: mpsc::UnboundedReceiver<oneshot::Sender<()>>,
|
2023-11-23 15:52:36 +01:00
|
|
|
pollers: Vec<oneshot::Sender<()>>,
|
2024-08-13 18:03:20 +02:00
|
|
|
content_checkout: mpsc::Receiver<oneshot::Sender<String>>,
|
2024-08-17 00:17:01 +02:00
|
|
|
delta_req: mpsc::Receiver<DeltaRequest>,
|
2023-11-27 14:55:46 +01:00
|
|
|
stop: mpsc::UnboundedReceiver<()>,
|
2024-08-08 02:27:06 +02:00
|
|
|
controller: BufferController,
|
2024-08-15 20:41:53 +02:00
|
|
|
callback: watch::Receiver<Option<ControllerCallback<BufferController>>>,
|
2023-08-11 15:33:40 +02:00
|
|
|
}
|
|
|
|
|
2024-01-25 02:13:45 +01:00
|
|
|
impl BufferWorker {
|
|
|
|
pub fn new(user_id: Uuid, path: &str) -> Self {
|
2024-08-13 00:36:09 +02:00
|
|
|
let init = diamond_types::LocalVersion::default();
|
|
|
|
|
|
|
|
let (latest_version_tx, latest_version_rx) = watch::channel(init.clone());
|
|
|
|
let (opin_tx, opin_rx) = mpsc::unbounded_channel();
|
|
|
|
|
2024-08-13 18:03:20 +02:00
|
|
|
let (req_tx, req_rx) = mpsc::channel(1);
|
2024-08-14 00:24:32 +02:00
|
|
|
let (recv_tx, recv_rx) = mpsc::channel(1);
|
2024-08-15 19:32:01 +02:00
|
|
|
let (cb_tx, cb_rx) = watch::channel(None);
|
2024-08-13 00:36:09 +02:00
|
|
|
|
2024-08-13 18:03:20 +02:00
|
|
|
let (poller_tx, poller_rx) = mpsc::unbounded_channel();
|
2024-08-13 00:36:09 +02:00
|
|
|
|
|
|
|
let (end_tx, end_rx) = mpsc::unbounded_channel();
|
|
|
|
|
2024-08-14 00:24:32 +02:00
|
|
|
let controller = BufferControllerInner {
|
|
|
|
name: path.to_string(),
|
|
|
|
latest_version: latest_version_rx,
|
|
|
|
last_update: InternallyMutable::new(diamond_types::LocalVersion::default()),
|
|
|
|
ops_in: opin_tx,
|
|
|
|
poller: poller_tx,
|
|
|
|
stopper: end_tx,
|
|
|
|
content_request: req_tx,
|
|
|
|
delta_request: recv_tx,
|
2024-08-15 19:32:01 +02:00
|
|
|
callback: cb_tx,
|
2024-08-14 00:24:32 +02:00
|
|
|
};
|
2024-08-13 00:36:09 +02:00
|
|
|
|
2024-01-25 02:13:45 +01:00
|
|
|
BufferWorker {
|
2024-08-13 00:36:09 +02:00
|
|
|
user_id,
|
|
|
|
latest_version: latest_version_tx,
|
|
|
|
ops_in: opin_rx,
|
2023-11-27 14:55:46 +01:00
|
|
|
poller: poller_rx,
|
|
|
|
pollers: Vec::new(),
|
2023-08-19 04:02:21 +02:00
|
|
|
stop: end_rx,
|
2024-08-08 02:27:06 +02:00
|
|
|
controller: BufferController(Arc::new(controller)),
|
2024-08-13 18:03:20 +02:00
|
|
|
content_checkout: req_rx,
|
2024-08-14 00:24:32 +02:00
|
|
|
delta_req: recv_rx,
|
2024-08-15 19:32:01 +02:00
|
|
|
callback: cb_rx,
|
2023-08-16 18:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-25 16:31:38 +01:00
|
|
|
impl ControllerWorker<TextChange> for BufferWorker {
|
2023-08-16 23:09:47 +02:00
|
|
|
type Controller = BufferController;
|
2024-01-25 02:13:45 +01:00
|
|
|
type Tx = mpsc::Sender<Operation>;
|
2024-02-07 01:09:28 +01:00
|
|
|
type Rx = Streaming<BufferEvent>;
|
2023-08-16 18:58:42 +02:00
|
|
|
|
2024-08-08 02:27:06 +02:00
|
|
|
fn controller(&self) -> BufferController {
|
|
|
|
self.controller.clone()
|
2023-08-11 15:33:40 +02:00
|
|
|
}
|
|
|
|
|
2024-01-25 02:13:45 +01:00
|
|
|
async fn work(mut self, tx: Self::Tx, mut rx: Self::Rx) {
|
2024-08-13 18:03:20 +02:00
|
|
|
let mut branch = diamond_types::list::Branch::new();
|
|
|
|
let mut oplog = diamond_types::list::OpLog::new();
|
2024-08-14 00:24:32 +02:00
|
|
|
let mut timer = Timer::new(10); // TODO configurable!!
|
2023-08-11 15:33:40 +02:00
|
|
|
loop {
|
2023-09-05 20:13:09 +02:00
|
|
|
// block until one of these is ready
|
2023-09-04 03:09:32 +02:00
|
|
|
tokio::select! {
|
2023-09-10 03:01:37 +02:00
|
|
|
biased;
|
|
|
|
|
2023-11-09 05:21:24 +01:00
|
|
|
// received stop signal
|
|
|
|
_ = self.stop.recv() => break,
|
|
|
|
|
2023-11-23 15:52:36 +01:00
|
|
|
// received a new poller, add it to collection
|
2023-11-27 14:55:46 +01:00
|
|
|
res = self.poller.recv() => match res {
|
2023-11-23 15:52:36 +01:00
|
|
|
None => break tracing::error!("poller channel closed"),
|
|
|
|
Some(tx) => self.pollers.push(tx),
|
|
|
|
},
|
|
|
|
|
2023-11-09 05:21:24 +01:00
|
|
|
// received a text change from editor
|
2024-08-13 00:36:09 +02:00
|
|
|
res = self.ops_in.recv() => match res {
|
2024-02-09 00:35:08 +01:00
|
|
|
None => break tracing::debug!("stopping: editor closed channel"),
|
2024-08-14 15:55:18 +02:00
|
|
|
Some((change, ack)) => {
|
2024-08-13 18:03:20 +02:00
|
|
|
let agent_id = oplog.get_or_create_agent_id(&self.user_id.to_string());
|
|
|
|
let last_ver = oplog.local_version();
|
2024-09-02 11:31:36 +02:00
|
|
|
// clip to buffer extents
|
|
|
|
let clip_end = std::cmp::min(branch.len(), change.end as usize);
|
|
|
|
let clip_start = std::cmp::max(0, change.start as usize);
|
2024-08-13 00:36:09 +02:00
|
|
|
|
2024-09-02 11:31:36 +02:00
|
|
|
// in case we have a "replace" span
|
2024-08-14 23:46:16 +02:00
|
|
|
if change.is_delete() {
|
2024-09-02 11:31:36 +02:00
|
|
|
branch.delete_without_content(&mut oplog, agent_id, clip_start..clip_end);
|
2024-08-14 23:46:16 +02:00
|
|
|
}
|
|
|
|
|
2024-08-13 00:36:09 +02:00
|
|
|
if change.is_insert() {
|
2024-09-02 11:31:36 +02:00
|
|
|
branch.insert(&mut oplog, agent_id, clip_start, &change.content);
|
2024-08-14 23:46:16 +02:00
|
|
|
}
|
2024-08-13 00:36:09 +02:00
|
|
|
|
2024-08-14 23:53:51 +02:00
|
|
|
if change.is_delete() || change.is_insert() {
|
|
|
|
tx.send(Operation { data: oplog.encode_from(Default::default(), &last_ver) }).await
|
|
|
|
.unwrap_or_warn("failed to send change!");
|
|
|
|
self.latest_version.send(oplog.local_version())
|
|
|
|
.unwrap_or_warn("failed to update latest version!");
|
|
|
|
}
|
2024-08-14 15:55:18 +02:00
|
|
|
ack.send(branch.local_version()).unwrap_or_warn("controller didn't wait for ack");
|
2024-08-13 00:36:09 +02:00
|
|
|
},
|
2023-08-11 15:33:40 +02:00
|
|
|
},
|
2023-09-10 03:01:37 +02:00
|
|
|
|
2024-08-14 00:24:32 +02:00
|
|
|
// received a message from server: add to oplog and update latest version (+unlock pollers)
|
2023-11-09 05:21:24 +01:00
|
|
|
res = rx.message() => match res {
|
|
|
|
Err(_e) => break,
|
|
|
|
Ok(None) => break,
|
2024-08-13 00:36:09 +02:00
|
|
|
Ok(Some(change)) => {
|
2024-08-13 18:03:20 +02:00
|
|
|
match oplog.decode_and_add(&change.op.data) {
|
2024-08-13 00:36:09 +02:00
|
|
|
Ok(local_version) => {
|
2024-08-13 18:03:20 +02:00
|
|
|
self.latest_version.send(local_version)
|
|
|
|
.unwrap_or_warn("failed to update latest version!");
|
2024-08-13 00:36:09 +02:00
|
|
|
for tx in self.pollers.drain(..) {
|
|
|
|
tx.send(()).unwrap_or_warn("could not wake up poller");
|
|
|
|
}
|
2024-08-15 19:32:01 +02:00
|
|
|
if let Some(cb) = self.callback.borrow().as_ref() {
|
2024-08-15 20:41:53 +02:00
|
|
|
cb.call(self.controller.clone()); // TODO should we run this on another task/thread?
|
2024-08-15 19:32:01 +02:00
|
|
|
}
|
2024-08-13 00:36:09 +02:00
|
|
|
},
|
|
|
|
Err(e) => tracing::error!("could not deserialize operation from server: {}", e),
|
|
|
|
}
|
2023-11-09 05:21:24 +01:00
|
|
|
},
|
2024-08-13 18:03:20 +02:00
|
|
|
},
|
|
|
|
|
2024-08-14 00:24:32 +02:00
|
|
|
// controller is ready to apply change and recv(), calculate it and send it back
|
|
|
|
res = self.delta_req.recv() => match res {
|
|
|
|
None => break tracing::error!("no more active controllers: can't send changes"),
|
|
|
|
Some((last_ver, tx)) => {
|
|
|
|
if let Some((lv, Some(dtop))) = oplog.iter_xf_operations_from(&last_ver, oplog.local_version_ref()).next() {
|
|
|
|
// x.0.start should always be after lastver!
|
|
|
|
// this step_ver will be the version after we apply the operation
|
|
|
|
// we give it to the controller so that he knows where it's at.
|
2024-08-14 16:31:28 +02:00
|
|
|
let step_ver = oplog.version_union(&[lv.end-1], &last_ver);
|
2024-08-14 15:55:18 +02:00
|
|
|
branch.merge(&oplog, &step_ver);
|
|
|
|
let new_local_v = branch.local_version();
|
2024-08-14 11:08:10 +02:00
|
|
|
|
2024-08-14 00:24:32 +02:00
|
|
|
let hash = if timer.step() {
|
2024-09-04 20:03:34 +02:00
|
|
|
Some(crate::ext::hash(branch.content().to_string()))
|
2024-08-14 00:24:32 +02:00
|
|
|
} else { None };
|
2024-08-14 11:08:10 +02:00
|
|
|
|
2024-08-14 17:16:05 +02:00
|
|
|
let tc = match dtop.kind {
|
|
|
|
diamond_types::list::operation::OpKind::Ins => {
|
|
|
|
if dtop.end() - dtop.start() != dtop.content_as_str().unwrap_or_default().len() {
|
|
|
|
tracing::error!("[?!?!] Insert span differs from effective content len (TODO remove this error after a bit)");
|
|
|
|
}
|
|
|
|
crate::api::change::TextChange {
|
|
|
|
start: dtop.start() as u32,
|
|
|
|
end: dtop.start() as u32,
|
|
|
|
content: dtop.content_as_str().unwrap_or_default().to_string(),
|
|
|
|
hash
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
diamond_types::list::operation::OpKind::Del => {
|
|
|
|
crate::api::change::TextChange {
|
|
|
|
start: dtop.start() as u32,
|
|
|
|
end: dtop.end() as u32,
|
|
|
|
content: dtop.content_as_str().unwrap_or_default().to_string(),
|
|
|
|
hash
|
|
|
|
}
|
|
|
|
}
|
2024-08-14 00:24:32 +02:00
|
|
|
};
|
2024-08-17 00:17:01 +02:00
|
|
|
tx.send((new_local_v, Some(tc))).unwrap_or_warn("could not update ops channel -- is controller dead?");
|
|
|
|
} else {
|
|
|
|
tx.send((last_ver, None)).unwrap_or_warn("could not update ops channel -- is controller dead?");
|
2024-08-14 00:24:32 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// received a request for full CRDT content
|
2024-08-13 18:03:20 +02:00
|
|
|
res = self.content_checkout.recv() => match res {
|
2024-08-14 00:24:32 +02:00
|
|
|
None => break tracing::error!("no more active controllers: can't update content"),
|
2024-08-13 18:03:20 +02:00
|
|
|
Some(tx) => {
|
|
|
|
branch.merge(&oplog, oplog.local_version_ref());
|
|
|
|
let content = branch.content().to_string();
|
|
|
|
tx.send(content).unwrap_or_warn("checkout request dropped");
|
|
|
|
},
|
2024-08-13 00:36:09 +02:00
|
|
|
}
|
2023-08-11 15:33:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-16 18:58:42 +02:00
|
|
|
}
|
2024-08-14 00:24:32 +02:00
|
|
|
|
|
|
|
struct Timer(u32, u32);
|
|
|
|
impl Timer {
|
2024-08-14 11:08:10 +02:00
|
|
|
fn new(period: u32) -> Self {
|
|
|
|
Timer(0, period)
|
|
|
|
}
|
2024-08-14 00:24:32 +02:00
|
|
|
fn step(&mut self) -> bool {
|
|
|
|
self.0 += 1;
|
|
|
|
if self.0 >= self.1 {
|
|
|
|
self.0 = 0;
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|