vertep-runtime 0.1.1

Event-sourced actor runtime for Vertep: activations, mailboxes, journal, timers and versioning
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Mailboxes: where messages wait for an actor, independently of its activations.
//!
//! A mailbox keeps messages in arrival order, which preserves the order of every single sender.
//! [`Mailbox::receive`] hands out the oldest message that is not in flight and not deferred by
//! the receiver's active states, and marks it in flight; [`Mailbox::ack`] removes it once its
//! turn is durable; [`Mailbox::release`] puts it back when the turn failed, so it is delivered
//! again. Deferred messages are skipped, never removed, so they keep their place.

use std::collections::VecDeque;
use std::sync::{Arc, Mutex, MutexGuard};

use oxrdf::{Literal, Quad, Term};
use serde::{Deserialize, Serialize};
use vertep_core::datamodel::Event;
use vertep_core::kernel::descriptor_matches;

use crate::address::Address;
use crate::storage::{Storage, select, text};
use crate::vocab::{self as vt, node};

/// A message on its way to an actor.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Envelope {
    /// The deduplication key: stable across redeliveries and retries.
    pub message_id: String,
    /// The receiver's address.
    pub to: String,
    /// The event, with origin and origin type set by the sender's transport.
    pub event: Event,
    /// A request's correlation id, or the correlation id a reply answers.
    pub correlation: Option<String>,
    /// The timer whose firing this message is.
    pub timer: Option<String>,
}

impl Envelope {
    /// A message `event` to `to` with message id `message_id`.
    pub fn new(message_id: impl Into<String>, to: &Address, event: Event) -> Self {
        Envelope {
            message_id: message_id.into(),
            to: to.as_str().to_owned(),
            event,
            correlation: None,
            timer: None,
        }
    }
}

/// A mailbox failure.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MailboxError(pub String);

impl core::fmt::Display for MailboxError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "mailbox error: {}", self.0)
    }
}

impl std::error::Error for MailboxError {}

/// True when an event named `name` matches one of the `vt:defer` descriptors.
pub fn is_deferred(deferred: &[String], name: &str) -> bool {
    deferred.iter().any(|d| descriptor_matches(d, name))
}

/// The mailbox contract: enqueue, ordered receive with deferral, and acknowledge.
// @tg: implements:: [[openspec:messaging#Pluggable Mailboxes]]
// @tg: implements:: [[openspec:messaging#Mailbox Ordering]]
pub trait Mailbox: Send + Sync {
    /// Appends a message.
    ///
    /// # Errors
    ///
    /// [`MailboxError`] when the message cannot be stored.
    fn enqueue(&self, env: Envelope) -> Result<(), MailboxError>;

    /// The oldest message that is neither in flight nor deferred (its event name matches no
    /// descriptor in `deferred`), now marked in flight.
    fn receive(&self, deferred: &[String]) -> Option<Envelope>;

    /// Removes the in-flight message `message_id` (its turn is durable).
    ///
    /// # Errors
    ///
    /// [`MailboxError`] when the removal cannot be stored.
    fn ack(&self, message_id: &str) -> Result<(), MailboxError>;

    /// Puts the in-flight message `message_id` back, to be received again.
    fn release(&self, message_id: &str);

    /// Puts every in-flight message back.
    fn release_all(&self);

    /// Every message not yet acknowledged, in arrival order.
    fn pending(&self) -> Vec<Envelope>;

    /// True when [`Mailbox::receive`] would return a message.
    fn has_deliverable(&self, deferred: &[String]) -> bool {
        self.pending_deliverable(deferred) > 0
    }

    /// The number of messages that are neither in flight nor deferred.
    fn pending_deliverable(&self, deferred: &[String]) -> usize;

    /// The number of messages not yet acknowledged.
    fn len(&self) -> usize {
        self.pending().len()
    }

    /// True when every message is acknowledged.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

struct Row {
    env: Envelope,
    seq: u64,
    in_flight: bool,
}

#[derive(Default)]
struct Rows {
    rows: VecDeque<Row>,
    next: u64,
}

impl Rows {
    fn receive(&mut self, deferred: &[String]) -> Option<Envelope> {
        let r = self
            .rows
            .iter_mut()
            .find(|r| !r.in_flight && !is_deferred(deferred, &r.env.event.name))?;
        r.in_flight = true;
        Some(r.env.clone())
    }

    fn remove(&mut self, id: &str) -> Option<Row> {
        let i = self
            .rows
            .iter()
            .position(|r| r.in_flight && r.env.message_id == id)
            .or_else(|| self.rows.iter().position(|r| r.env.message_id == id))?;
        self.rows.remove(i)
    }

    fn release(&mut self, id: &str) {
        for r in &mut self.rows {
            if r.env.message_id == id {
                r.in_flight = false;
            }
        }
    }

    fn deliverable(&self, deferred: &[String]) -> usize {
        self.rows
            .iter()
            .filter(|r| !r.in_flight && !is_deferred(deferred, &r.env.event.name))
            .count()
    }
}

fn lock(m: &Mutex<Rows>) -> MutexGuard<'_, Rows> {
    m.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
}

/// An in-memory mailbox; its messages are lost with the process.
#[derive(Default)]
pub struct MemoryMailbox {
    rows: Mutex<Rows>,
}

impl std::fmt::Debug for MemoryMailbox {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MemoryMailbox")
            .field("len", &self.len())
            .finish()
    }
}

impl Mailbox for MemoryMailbox {
    fn enqueue(&self, env: Envelope) -> Result<(), MailboxError> {
        let mut g = lock(&self.rows);
        let seq = g.next;
        g.next += 1;
        g.rows.push_back(Row {
            env,
            seq,
            in_flight: false,
        });
        Ok(())
    }

    fn receive(&self, deferred: &[String]) -> Option<Envelope> {
        lock(&self.rows).receive(deferred)
    }

    fn ack(&self, message_id: &str) -> Result<(), MailboxError> {
        lock(&self.rows).remove(message_id);
        Ok(())
    }

    fn release(&self, message_id: &str) {
        lock(&self.rows).release(message_id);
    }

    fn release_all(&self) {
        for r in &mut lock(&self.rows).rows {
            r.in_flight = false;
        }
    }

    fn pending(&self) -> Vec<Envelope> {
        lock(&self.rows)
            .rows
            .iter()
            .map(|r| r.env.clone())
            .collect()
    }

    fn pending_deliverable(&self, deferred: &[String]) -> usize {
        lock(&self.rows).deliverable(deferred)
    }
}

/// A mailbox kept in the actor's graph store, in graph `<address>/mailbox`: it survives
/// restarts, and its messages are delivered again in their original order.
// @tg: implements:: [[openspec:messaging#Mailboxes Outlive Activations]]
pub struct StoreMailbox {
    address: Address,
    storage: Arc<dyn Storage>,
    rows: Mutex<Rows>,
}

impl std::fmt::Debug for StoreMailbox {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("StoreMailbox")
            .field("address", &self.address)
            .field("len", &self.len())
            .finish()
    }
}

impl StoreMailbox {
    /// Opens the mailbox of `address`, loading the messages stored for it.
    pub fn open(address: Address, storage: Arc<dyn Storage>) -> Self {
        let mut rows = Rows::default();
        let g = address.mailbox_graph();
        let q = format!(
            "SELECT ?seq ?env WHERE {{ GRAPH {g} {{ ?m <{seq}> ?seq ; <{env}> ?env }} }}",
            seq = vt::SEQUENCE,
            env = vt::ENVELOPE
        );
        if let Ok(sol) = select(storage.graph(address.node()), &q) {
            let mut found: Vec<(u64, Envelope)> = Vec::new();
            for i in 0..sol.len() {
                let seq = sol
                    .get(i, "seq")
                    .and_then(text)
                    .and_then(|s| s.parse::<u64>().ok());
                let env = sol
                    .get(i, "env")
                    .and_then(text)
                    .and_then(|s| serde_json::from_str::<Envelope>(&s).ok());
                if let (Some(seq), Some(env)) = (seq, env) {
                    found.push((seq, env));
                }
            }
            // Rows written by other nodes sharing the store may repeat a sequence number; the
            // message id orders those deterministically.
            found.sort_by(|(s, a), (t, b)| s.cmp(t).then_with(|| a.message_id.cmp(&b.message_id)));
            for (seq, env) in found {
                rows.next = rows.next.max(seq + 1);
                rows.rows.push_back(Row {
                    env,
                    seq,
                    in_flight: false,
                });
            }
        }
        StoreMailbox {
            address,
            storage,
            rows: Mutex::new(rows),
        }
    }

    fn quads(&self, seq: u64, env: &Envelope) -> Vec<Quad> {
        let g = self.address.mailbox_graph();
        let json = serde_json::to_string(env).unwrap_or_default();
        let m = self.address.mailbox_row_iri(seq, &json);
        let q = |p: &str, o: Term| Quad::new(m.clone(), node(p), o, g.clone());
        vec![
            q(vt::RDF_TYPE, node(vt::MESSAGE).into()),
            q(vt::RECIPIENT, self.address.node().clone().into()),
            q(
                vt::SEQUENCE,
                Literal::new_typed_literal(seq.to_string(), oxrdf::vocab::xsd::INTEGER).into(),
            ),
            q(
                vt::MESSAGE_ID,
                Literal::new_simple_literal(&env.message_id).into(),
            ),
            q(vt::ENVELOPE, Literal::new_simple_literal(json).into()),
        ]
    }
}

impl Mailbox for StoreMailbox {
    fn enqueue(&self, env: Envelope) -> Result<(), MailboxError> {
        let mut g = lock(&self.rows);
        let seq = g.next;
        self.storage
            .graph(self.address.node())
            .insert_quads(&self.quads(seq, &env))
            .map_err(|e| MailboxError(e.to_string()))?;
        g.next += 1;
        g.rows.push_back(Row {
            env,
            seq,
            in_flight: false,
        });
        Ok(())
    }

    fn receive(&self, deferred: &[String]) -> Option<Envelope> {
        lock(&self.rows).receive(deferred)
    }

    fn ack(&self, message_id: &str) -> Result<(), MailboxError> {
        let mut g = lock(&self.rows);
        if let Some(r) = g.remove(message_id) {
            self.storage
                .graph(self.address.node())
                .remove_quads(&self.quads(r.seq, &r.env))
                .map_err(|e| MailboxError(e.to_string()))?;
        }
        Ok(())
    }

    fn release(&self, message_id: &str) {
        lock(&self.rows).release(message_id);
    }

    fn release_all(&self) {
        for r in &mut lock(&self.rows).rows {
            r.in_flight = false;
        }
    }

    fn pending(&self) -> Vec<Envelope> {
        lock(&self.rows)
            .rows
            .iter()
            .map(|r| r.env.clone())
            .collect()
    }

    fn pending_deliverable(&self, deferred: &[String]) -> usize {
        lock(&self.rows).deliverable(deferred)
    }
}

/// Creates the mailbox of an address.
pub trait MailboxFactory: Send + Sync {
    /// The mailbox of `address`.
    fn create(&self, address: &Address) -> Arc<dyn Mailbox>;
    /// The addresses that have stored messages, so that a restarted node delivers them.
    fn recover(&self) -> Vec<Address> {
        Vec::new()
    }
}

/// In-memory mailboxes.
#[derive(Clone, Copy, Debug, Default)]
pub struct MemoryMailboxes;

impl MailboxFactory for MemoryMailboxes {
    fn create(&self, _address: &Address) -> Arc<dyn Mailbox> {
        Arc::new(MemoryMailbox::default())
    }
}

/// Store-backed mailboxes on a node's storage.
#[derive(Clone)]
pub struct StoreMailboxes {
    storage: Arc<dyn Storage>,
}

impl std::fmt::Debug for StoreMailboxes {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("StoreMailboxes").finish_non_exhaustive()
    }
}

impl StoreMailboxes {
    /// Mailboxes stored in `storage`.
    pub fn new(storage: Arc<dyn Storage>) -> Self {
        StoreMailboxes { storage }
    }
}

impl MailboxFactory for StoreMailboxes {
    fn create(&self, address: &Address) -> Arc<dyn Mailbox> {
        Arc::new(StoreMailbox::open(address.clone(), self.storage.clone()))
    }

    fn recover(&self) -> Vec<Address> {
        let q = format!(
            "SELECT DISTINCT ?a WHERE {{ GRAPH ?g {{ ?m a <{msg}> ; <{rcpt}> ?a }} }}",
            msg = vt::MESSAGE,
            rcpt = vt::RECIPIENT
        );
        let mut out = Vec::new();
        for g in self.storage.graphs() {
            if let Ok(sol) = select(g, &q) {
                for t in sol.values("a") {
                    if let Some(a) = text(&t).and_then(|s| Address::parse(&s).ok()) {
                        out.push(a);
                    }
                }
            }
        }
        out.sort();
        out.dedup();
        out
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use vertep_core::datamodel::EventType;

    fn env(id: &str, name: &str) -> Envelope {
        Envelope::new(
            id,
            &Address::parse("urn:t:a").unwrap(),
            Event::named(name, EventType::External),
        )
    }

    #[test]
    fn receive_skips_deferred_and_in_flight_and_keeps_order() {
        let m = MemoryMailbox::default();
        m.enqueue(env("1", "order.cancel")).unwrap();
        m.enqueue(env("2", "ping")).unwrap();
        m.enqueue(env("3", "pong")).unwrap();
        let d = vec!["order.*".to_owned()];
        assert_eq!(m.receive(&d).unwrap().message_id, "2");
        assert_eq!(m.receive(&d).unwrap().message_id, "3");
        assert!(m.receive(&d).is_none());
        m.release("2");
        assert_eq!(m.receive(&[]).unwrap().message_id, "1");
        m.ack("1").unwrap();
        assert_eq!(m.receive(&[]).unwrap().message_id, "2");
        assert_eq!(m.len(), 2);
    }
}