xmachines-workflow 6.0.0

The record protocol of a statechart run, as serde types: chunks of events, time and work results in, events, snapshots and rejections out, faults as data
Documentation
//! What a run's journal holds ([`JournalEntry`]): how the run started, first
//! ([`StartEntry`]), each chunk it read, rejected and faulted ones too
//! ([`ChunkEntry`]), now and then a checkpoint ([`SnapshotEntry`]), and the
//! claim of each holder of its lease ([`ClaimEntry`]). A run reopens of its
//! last checkpoint and the chunks after it, with no clock. A checkpoint a
//! host gives out carries the start of its run ([`HostedCheckpoint`]).
//!
//! The entries are the JSON xmachines-js's hosts write, so a journal moves
//! between a host in JavaScript and one in Rust.

use std::collections::BTreeMap;

use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::input::Input;
use crate::record::Snapshot;
use crate::serde_util::{present, whole, whole_opt};

/// How a run started, the first entry of its journal: what a replay needs to
/// give the same records, with no clock. `maxTimers` is there because a
/// run's records depend on it.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", bound(serialize = "I: Serialize", deserialize = "I: Deserialize<'de>"))]
pub struct StartEntry<I = Value> {
    /// The run's id, as the host named it.
    pub run: String,
    /// The root actor's id.
    pub actor: String,
    /// The machine's version, as the host named it: a journal of another
    /// version is refused.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
    /// The run's input, when it has one: `null` is kept.
    #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "present")]
    pub input: Option<I>,
    /// The time the run started at, in whole milliseconds, a safe integer of
    /// either sign.
    #[serde(deserialize_with = "crate::serde_util::safe")]
    pub time: i64,
    /// How many timers a time input fires at most in a chunk of control
    /// inputs alone.
    #[serde(deserialize_with = "whole")]
    pub max_timers: u64,
}

/// Where a chunk came from, as a binding sends it, of a source that delivers
/// at least once, as a partition of a broker: the producer, as
/// `<group>/<topic>/<partition>`, the chunk's offset in it, a whole number
/// as decimal text, so a 64-bit offset fits, and the `epoch` of the claim the
/// holder wrote the chunk under.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Source {
    /// The source, as `<group>/<topic>/<partition>`.
    pub producer: String,
    /// The chunk's place in the source, a whole number as decimal text.
    pub offset: String,
    /// The epoch of the claim the holder wrote the chunk under.
    #[serde(deserialize_with = "whole")]
    pub epoch: u64,
}

/// A chunk the run read, as the host stamped it, under its `seq`: rejected
/// and faulted ones too.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ChunkEntry<In = Input> {
    /// The chunk's number.
    #[serde(deserialize_with = "whole")]
    pub seq: u64,
    /// The chunk, as a batch.
    pub chunk: Vec<In>,
    /// Where the chunk came from, of a chunk a binding sent with its producer.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source: Option<Source>,
    /// The epoch of the claim the holder wrote the chunk under: 0, or none,
    /// of a holder of no lease.
    #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "whole_opt")]
    pub epoch: Option<u64>,
}

/// A checkpoint: the run's snapshot with the engine's state, and the last
/// `seq` it read. A runner keeps beside it the epoch of its claim, the
/// position of each producer, and `published`, the last `seq` whose records a
/// binding acknowledged.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SnapshotEntry<Snap = Snapshot> {
    /// The epoch of the claim the holder wrote it under.
    #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "whole_opt")]
    pub epoch: Option<u64>,
    /// Where each producer's chunks reached, by producer.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub positions: Option<BTreeMap<String, Source>>,
    /// The last `seq` whose records a binding acknowledged.
    #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "whole_opt")]
    pub published: Option<u64>,
    /// The last chunk the run had read.
    #[serde(deserialize_with = "whole")]
    pub seq: u64,
    /// The snapshot, with the engine's `state`.
    pub snapshot: Snap,
}

/// A claim of the run by a holder of its lease, as the holder opens it: the
/// `epoch`, one more than the highest the journal held, and the `holder`, a
/// nonce of this open alone. Each entry the holder writes after it carries the
/// epoch, so a store refuses a holder of an older one.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ClaimEntry {
    /// One more than the highest epoch the journal held.
    #[serde(deserialize_with = "whole")]
    pub epoch: u64,
    /// The nonce of the holder's open.
    pub holder: String,
}

/// An entry of a run's journal, told apart by `kind`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
    tag = "kind",
    rename_all = "lowercase",
    bound(
        serialize = "In: Serialize, Snap: Serialize, I: Serialize",
        deserialize = "In: Deserialize<'de>, Snap: Deserialize<'de>, I: Deserialize<'de>"
    )
)]
pub enum JournalEntry<In = Input, Snap = Snapshot, I = Value> {
    /// How the run started: entry 0.
    Start(StartEntry<I>),
    /// A chunk the run read.
    Chunk(ChunkEntry<In>),
    /// A checkpoint.
    Snapshot(SnapshotEntry<Snap>),
    /// A claim of a holder of the run's lease.
    Claim(ClaimEntry),
}

/// A checkpoint of a hosted run, as a host gives it out: the run's snapshot
/// with the engine's state, the last `seq` it read, and how the run started.
/// A host reopens a run of it as the run started, with no input beside it.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(bound(
    serialize = "Snap: Serialize, I: Serialize",
    deserialize = "Snap: Deserialize<'de>, I: DeserializeOwned"
))]
pub struct HostedCheckpoint<Snap = Snapshot, I = Value> {
    /// The last chunk the run had read.
    #[serde(deserialize_with = "whole")]
    pub seq: u64,
    /// The snapshot, with the engine's `state`.
    pub snapshot: Snap,
    /// How the run started, as the start entry of its journal, `kind`
    /// included.
    #[serde(with = "tagged_start")]
    pub start: StartEntry<I>,
}

/// A [`StartEntry`] alone, with its `kind`, as a checkpoint carries it.
mod tagged_start {
    use serde::de::DeserializeOwned;
    use serde::{Deserialize, Deserializer, Serialize, Serializer};

    use super::StartEntry;

    #[derive(Serialize)]
    #[serde(tag = "kind", rename_all = "lowercase", bound = "I: Serialize")]
    enum Ref<'a, I> {
        Start(&'a StartEntry<I>),
    }

    #[derive(Deserialize)]
    #[serde(tag = "kind", rename_all = "lowercase", bound = "I: DeserializeOwned")]
    enum Owned<I> {
        Start(StartEntry<I>),
    }

    pub fn serialize<S: Serializer, I: Serialize>(start: &StartEntry<I>, serializer: S) -> Result<S::Ok, S::Error> {
        Ref::Start(start).serialize(serializer)
    }

    pub fn deserialize<'de, D: Deserializer<'de>, I: DeserializeOwned>(
        deserializer: D,
    ) -> Result<StartEntry<I>, D::Error> {
        let Owned::Start(start) = Owned::deserialize(deserializer)?;
        Ok(start)
    }
}