xmachines_workflow/journal.rs
1//! What a run's journal holds ([`JournalEntry`]): how the run started, first
2//! ([`StartEntry`]), each chunk it read, rejected and faulted ones too
3//! ([`ChunkEntry`]), now and then a checkpoint ([`SnapshotEntry`]), and the
4//! claim of each holder of its lease ([`ClaimEntry`]). A run reopens of its
5//! last checkpoint and the chunks after it, with no clock. A checkpoint a
6//! host gives out carries the start of its run ([`HostedCheckpoint`]).
7//!
8//! The entries are the JSON xmachines-js's hosts write, so a journal moves
9//! between a host in JavaScript and one in Rust.
10
11use std::collections::BTreeMap;
12
13use serde::de::DeserializeOwned;
14use serde::{Deserialize, Serialize};
15use serde_json::Value;
16
17use crate::input::Input;
18use crate::record::Snapshot;
19use crate::serde_util::{present, whole, whole_opt};
20
21/// How a run started, the first entry of its journal: what a replay needs to
22/// give the same records, with no clock. `maxTimers` is there because a
23/// run's records depend on it.
24#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
25#[serde(rename_all = "camelCase", bound(serialize = "I: Serialize", deserialize = "I: Deserialize<'de>"))]
26pub struct StartEntry<I = Value> {
27 /// The run's id, as the host named it.
28 pub run: String,
29 /// The root actor's id.
30 pub actor: String,
31 /// The machine's version, as the host named it: a journal of another
32 /// version is refused.
33 #[serde(default, skip_serializing_if = "Option::is_none")]
34 pub version: Option<String>,
35 /// The run's input, when it has one: `null` is kept.
36 #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "present")]
37 pub input: Option<I>,
38 /// The time the run started at, in whole milliseconds, a safe integer of
39 /// either sign.
40 #[serde(deserialize_with = "crate::serde_util::safe")]
41 pub time: i64,
42 /// How many timers a time input fires at most in a chunk of control
43 /// inputs alone.
44 #[serde(deserialize_with = "whole")]
45 pub max_timers: u64,
46}
47
48/// Where a chunk came from, as a binding sends it, of a source that delivers
49/// at least once, as a partition of a broker: the producer, as
50/// `<group>/<topic>/<partition>`, the chunk's offset in it, a whole number
51/// as decimal text, so a 64-bit offset fits, and the `epoch` of the claim the
52/// holder wrote the chunk under.
53#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
54pub struct Source {
55 /// The source, as `<group>/<topic>/<partition>`.
56 pub producer: String,
57 /// The chunk's place in the source, a whole number as decimal text.
58 pub offset: String,
59 /// The epoch of the claim the holder wrote the chunk under.
60 #[serde(deserialize_with = "whole")]
61 pub epoch: u64,
62}
63
64/// A chunk the run read, as the host stamped it, under its `seq`: rejected
65/// and faulted ones too.
66#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
67pub struct ChunkEntry<In = Input> {
68 /// The chunk's number.
69 #[serde(deserialize_with = "whole")]
70 pub seq: u64,
71 /// The chunk, as a batch.
72 pub chunk: Vec<In>,
73 /// Where the chunk came from, of a chunk a binding sent with its producer.
74 #[serde(default, skip_serializing_if = "Option::is_none")]
75 pub source: Option<Source>,
76 /// The epoch of the claim the holder wrote the chunk under: 0, or none,
77 /// of a holder of no lease.
78 #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "whole_opt")]
79 pub epoch: Option<u64>,
80}
81
82/// A checkpoint: the run's snapshot with the engine's state, and the last
83/// `seq` it read. A runner keeps beside it the epoch of its claim, the
84/// position of each producer, and `published`, the last `seq` whose records a
85/// binding acknowledged.
86#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
87pub struct SnapshotEntry<Snap = Snapshot> {
88 /// The epoch of the claim the holder wrote it under.
89 #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "whole_opt")]
90 pub epoch: Option<u64>,
91 /// Where each producer's chunks reached, by producer.
92 #[serde(default, skip_serializing_if = "Option::is_none")]
93 pub positions: Option<BTreeMap<String, Source>>,
94 /// The last `seq` whose records a binding acknowledged.
95 #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "whole_opt")]
96 pub published: Option<u64>,
97 /// The last chunk the run had read.
98 #[serde(deserialize_with = "whole")]
99 pub seq: u64,
100 /// The snapshot, with the engine's `state`.
101 pub snapshot: Snap,
102}
103
104/// A claim of the run by a holder of its lease, as the holder opens it: the
105/// `epoch`, one more than the highest the journal held, and the `holder`, a
106/// nonce of this open alone. Each entry the holder writes after it carries the
107/// epoch, so a store refuses a holder of an older one.
108#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
109pub struct ClaimEntry {
110 /// One more than the highest epoch the journal held.
111 #[serde(deserialize_with = "whole")]
112 pub epoch: u64,
113 /// The nonce of the holder's open.
114 pub holder: String,
115}
116
117/// An entry of a run's journal, told apart by `kind`.
118#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
119#[serde(
120 tag = "kind",
121 rename_all = "lowercase",
122 bound(
123 serialize = "In: Serialize, Snap: Serialize, I: Serialize",
124 deserialize = "In: Deserialize<'de>, Snap: Deserialize<'de>, I: Deserialize<'de>"
125 )
126)]
127pub enum JournalEntry<In = Input, Snap = Snapshot, I = Value> {
128 /// How the run started: entry 0.
129 Start(StartEntry<I>),
130 /// A chunk the run read.
131 Chunk(ChunkEntry<In>),
132 /// A checkpoint.
133 Snapshot(SnapshotEntry<Snap>),
134 /// A claim of a holder of the run's lease.
135 Claim(ClaimEntry),
136}
137
138/// A checkpoint of a hosted run, as a host gives it out: the run's snapshot
139/// with the engine's state, the last `seq` it read, and how the run started.
140/// A host reopens a run of it as the run started, with no input beside it.
141#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
142#[serde(bound(
143 serialize = "Snap: Serialize, I: Serialize",
144 deserialize = "Snap: Deserialize<'de>, I: DeserializeOwned"
145))]
146pub struct HostedCheckpoint<Snap = Snapshot, I = Value> {
147 /// The last chunk the run had read.
148 #[serde(deserialize_with = "whole")]
149 pub seq: u64,
150 /// The snapshot, with the engine's `state`.
151 pub snapshot: Snap,
152 /// How the run started, as the start entry of its journal, `kind`
153 /// included.
154 #[serde(with = "tagged_start")]
155 pub start: StartEntry<I>,
156}
157
158/// A [`StartEntry`] alone, with its `kind`, as a checkpoint carries it.
159mod tagged_start {
160 use serde::de::DeserializeOwned;
161 use serde::{Deserialize, Deserializer, Serialize, Serializer};
162
163 use super::StartEntry;
164
165 #[derive(Serialize)]
166 #[serde(tag = "kind", rename_all = "lowercase", bound = "I: Serialize")]
167 enum Ref<'a, I> {
168 Start(&'a StartEntry<I>),
169 }
170
171 #[derive(Deserialize)]
172 #[serde(tag = "kind", rename_all = "lowercase", bound = "I: DeserializeOwned")]
173 enum Owned<I> {
174 Start(StartEntry<I>),
175 }
176
177 pub fn serialize<S: Serializer, I: Serialize>(start: &StartEntry<I>, serializer: S) -> Result<S::Ok, S::Error> {
178 Ref::Start(start).serialize(serializer)
179 }
180
181 pub fn deserialize<'de, D: Deserializer<'de>, I: DeserializeOwned>(
182 deserializer: D,
183 ) -> Result<StartEntry<I>, D::Error> {
184 let Owned::Start(start) = Owned::deserialize(deserializer)?;
185 Ok(start)
186 }
187}