spate-datagen 0.2.0

Synthetic commerce-event source for the Spate framework: a built-in storefront dataset with referentially consistent orders, payments and refunds, so an example or a test runs with no broker and no object store. A demo and test source; it keeps no durable progress.
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
//! Turning an event into the bytes a payload borrows.
//!
//! Both encoders append **into a buffer the caller owns** rather than returning
//! one, so a lane appends event after event to a single reused arena and hands
//! out subslices of it. Once the arena has grown to a batch, neither encoder
//! allocates it again: the JSON encoder allocates nothing at all, and the Avro
//! encoder allocates only inside `apache-avro`'s serializer.
//!
//! `encoding:` selects the format. The generator authors its own payloads, so
//! an encoder is chosen from that set rather than supplied by the assembly.

use crate::config::Encoding;
use crate::events::StorefrontEvent;
use spate_core::error::{ErrorClass, SourceError};

/// A resolved payload encoder. Built once at `open`, never on the record path.
#[derive(Debug)]
pub(crate) enum Encoder {
    /// One JSON document per payload.
    Json,
    /// One bare Avro datum per payload, against the parsed
    /// [`EVENT_SCHEMA_JSON`](crate::EVENT_SCHEMA_JSON).
    #[cfg(feature = "avro")]
    Avro(Box<apache_avro::Schema>),
}

impl Encoder {
    /// Resolve `encoding`. The Avro schema is parsed here, once, so the record
    /// path never touches it.
    pub(crate) fn new(encoding: Encoding) -> Result<Encoder, SourceError> {
        match encoding {
            Encoding::Json => Ok(Encoder::Json),
            #[cfg(feature = "avro")]
            Encoding::Avro => {
                let schema =
                    apache_avro::Schema::parse_str(crate::EVENT_SCHEMA_JSON).map_err(|e| {
                        SourceError::Client {
                            class: ErrorClass::Fatal,
                            reason: format!("parsing the built-in datagen Avro schema: {e}"),
                        }
                    })?;
                Ok(Encoder::Avro(Box::new(schema)))
            }
            // Unreachable through configuration, since `validate` rejects
            // this combination at load time, but a hand-built config reaches
            // `open` without passing through it.
            #[cfg(not(feature = "avro"))]
            Encoding::Avro => Err(SourceError::Client {
                class: ErrorClass::Fatal,
                reason: crate::config::AVRO_FEATURE_OFF.into(),
            }),
        }
    }

    /// Append `event`'s encoded bytes to `out`.
    ///
    /// On error `out` is left as it was found: both encoders write
    /// incrementally, so the partial bytes are rolled back before returning.
    pub(crate) fn encode(
        &self,
        event: &StorefrontEvent,
        out: &mut Vec<u8>,
    ) -> Result<(), SourceError> {
        let start = out.len();
        let result = match self {
            // Infallible in practice, since the event model has no map
            // keys, no non-finite floats and no custom `Serialize`, but the
            // writer API is fallible.
            Encoder::Json => serde_json::to_writer(&mut *out, event)
                .map_err(|e| format!("encoding a datagen event as JSON: {e}")),
            #[cfg(feature = "avro")]
            Encoder::Avro(schema) => {
                // The built-in schema defines its four records inline and
                // carries no `Schema::Ref`, so the resolution map is empty.
                // `HashMap::new` does not allocate.
                let names = apache_avro::schema::NamesRef::new();
                apache_avro::write_avro_datum_ref(schema, &names, &AvroDatum(event), out)
                    .map(|_bytes_written| ())
                    .map_err(|e| format!("encoding a datagen event as an Avro datum: {e}"))
            }
        };
        result.map_err(|reason| {
            out.truncate(start);
            SourceError::Client {
                class: ErrorClass::Fatal,
                reason,
            }
        })
    }
}

/// An event in the shape `EVENT_SCHEMA_JSON`'s top-level union declares,
/// serialized straight into the caller's buffer.
///
/// [`StorefrontEvent`]'s own `Serialize` is the JSON contract, internally
/// tagged by `type`, which is not a union. This view selects the branch by
/// **index** instead, and those indices are the contract this crate publishes:
/// they follow the union order in
/// [`EVENT_SCHEMA_JSON`](crate::EVENT_SCHEMA_JSON) and cannot be reordered
/// independently of it.
#[cfg(feature = "avro")]
struct AvroDatum<'a>(&'a StorefrontEvent);

#[cfg(feature = "avro")]
impl serde::Serialize for AvroDatum<'_> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        const NAME: &str = "StorefrontEvent";
        match self.0 {
            StorefrontEvent::OrderPlaced(e) => {
                serializer.serialize_newtype_variant(NAME, 0, "OrderPlaced", &AvroRecord(e))
            }
            StorefrontEvent::PaymentCaptured(e) => {
                serializer.serialize_newtype_variant(NAME, 1, "PaymentCaptured", &AvroRecord(e))
            }
            StorefrontEvent::RefundIssued(e) => {
                serializer.serialize_newtype_variant(NAME, 2, "RefundIssued", &AvroRecord(e))
            }
        }
    }
}

/// A record in the numeric types `EVENT_SCHEMA_JSON` declares.
///
/// Avro has no unsigned types, so the schema declares `int` and `long` where
/// the event model carries `u32` and `u64`. This view narrows each field to
/// the declared type, and refuses a value the declared type cannot carry
/// rather than wrapping it. The field order and names are the record's own;
/// only the numeric types differ.
#[cfg(feature = "avro")]
struct AvroRecord<'a, T>(&'a T);

/// `v` as the Avro `int` the schema declares.
#[cfg(feature = "avro")]
fn avro_int<E: serde::ser::Error>(field: &str, v: u32) -> Result<i32, E> {
    i32::try_from(v)
        .map_err(|_| E::custom(format!("{field} exceeds the Avro int the schema declares")))
}

/// `v` as the Avro `long` the schema declares.
#[cfg(feature = "avro")]
fn avro_long<E: serde::ser::Error>(field: &str, v: u64) -> Result<i64, E> {
    i64::try_from(v)
        .map_err(|_| E::custom(format!("{field} exceeds the Avro long the schema declares")))
}

#[cfg(feature = "avro")]
impl serde::Serialize for AvroRecord<'_, crate::events::OrderPlaced> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        let e = self.0;
        let mut s = serializer.serialize_struct("OrderPlaced", 5)?;
        s.serialize_field("order_id", &avro_long("order_id", e.order_id)?)?;
        s.serialize_field("customer_id", &avro_int("customer_id", e.customer_id)?)?;
        s.serialize_field("region", &e.region)?;
        s.serialize_field("placed_at", &e.placed_at)?;
        s.serialize_field("lines", &AvroLines(&e.lines))?;
        s.end()
    }
}

/// The `lines` array, each element in the declared numeric types.
#[cfg(feature = "avro")]
struct AvroLines<'a>(&'a [crate::events::OrderLine]);

#[cfg(feature = "avro")]
impl serde::Serialize for AvroLines<'_> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeSeq;
        let mut s = serializer.serialize_seq(Some(self.0.len()))?;
        for line in self.0 {
            s.serialize_element(&AvroRecord(line))?;
        }
        s.end()
    }
}

#[cfg(feature = "avro")]
impl serde::Serialize for AvroRecord<'_, crate::events::OrderLine> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        let e = self.0;
        let mut s = serializer.serialize_struct("OrderLine", 3)?;
        s.serialize_field("sku", &e.sku)?;
        s.serialize_field("qty", &avro_int("qty", e.qty)?)?;
        s.serialize_field("unit_cents", &avro_int("unit_cents", e.unit_cents)?)?;
        s.end()
    }
}

#[cfg(feature = "avro")]
impl serde::Serialize for AvroRecord<'_, crate::events::PaymentCaptured> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        let e = self.0;
        let mut s = serializer.serialize_struct("PaymentCaptured", 2)?;
        s.serialize_field("order_id", &avro_long("order_id", e.order_id)?)?;
        s.serialize_field("amount_cents", &avro_long("amount_cents", e.amount_cents)?)?;
        s.end()
    }
}

#[cfg(feature = "avro")]
impl serde::Serialize for AvroRecord<'_, crate::events::RefundIssued> {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        let e = self.0;
        let mut s = serializer.serialize_struct("RefundIssued", 3)?;
        s.serialize_field("order_id", &avro_long("order_id", e.order_id)?)?;
        s.serialize_field("amount_cents", &avro_long("amount_cents", e.amount_cents)?)?;
        s.serialize_field("reason", &e.reason)?;
        s.end()
    }
}

#[cfg(test)]
#[expect(deprecated, reason = "fixtures call the datum free functions directly")]
mod tests {
    use super::*;
    use crate::config::DatagenSourceConfig;
    use crate::plan::EventPlan;

    /// Every encoding this build can resolve, so a property asserted of "the
    /// encoder" is asserted of each of them.
    #[cfg(feature = "avro")]
    const ENCODINGS: [Encoding; 2] = [Encoding::Json, Encoding::Avro];
    #[cfg(not(feature = "avro"))]
    const ENCODINGS: [Encoding; 1] = [Encoding::Json];

    fn events(count: usize) -> Vec<StorefrontEvent> {
        plan().take(count).collect()
    }

    /// An endless lane, so a caller can draw *different* batches one after
    /// another the way a running lane does.
    fn plan() -> impl Iterator<Item = StorefrontEvent> {
        let cfg = DatagenSourceConfig {
            seed: 21,
            ..DatagenSourceConfig::default()
        };
        let mut plan = EventPlan::new(&cfg, 0);
        std::iter::from_fn(move || Some(plan.next().0))
    }

    /// The encoder appends, and the caller keeps the buffer. A lane depends on
    /// both halves of that: the spans it hands out are only correct if nothing
    /// before them moved, and the arena is only reusable if `encode` never
    /// clears it.
    #[test]
    fn encoding_appends_to_the_caller_s_buffer_and_leaves_earlier_bytes_alone() {
        let encoder = Encoder::new(Encoding::Json).unwrap();
        let mut arena = Vec::new();
        let mut spans = Vec::new();
        let generated = events(64);
        for event in &generated {
            let start = arena.len();
            encoder.encode(event, &mut arena).unwrap();
            spans.push(start..arena.len());
        }
        assert_eq!(spans.len(), generated.len());
        for (span, event) in spans.iter().zip(&generated) {
            let decoded: StorefrontEvent = serde_json::from_slice(&arena[span.clone()]).unwrap();
            assert_eq!(&decoded, event, "a span decodes to the event that wrote it");
        }
    }

    /// A warm arena is never reallocated, which keeps a steady-state poll
    /// allocation-free.
    ///
    /// Every batch here is *different*, because the lane runs on rather than
    /// restarting from its seed, so the assertion is about the encoder rather
    /// than about `Vec::clear` keeping a capacity that identical input refills.
    #[test]
    fn a_warm_arena_is_never_reallocated() {
        for encoding in ENCODINGS {
            let encoder = Encoder::new(encoding).unwrap();
            let mut lane = plan();
            let mut arena = Vec::new();

            // Comfortably past the largest a 256-event batch can reach: every
            // event a placement of five lines, each field the longest its
            // dimension table holds. The per-batch length assertion below is
            // what keeps that judgement honest.
            const BOUND: usize = 256 * 1024;
            arena.reserve(BOUND);
            let warm = arena.capacity();

            let mut batches = Vec::new();
            for _ in 0..50 {
                arena.clear();
                for event in lane.by_ref().take(256) {
                    encoder.encode(&event, &mut arena).unwrap();
                }
                assert!(
                    arena.len() < BOUND,
                    "{encoding:?}: a batch reached {} bytes, so BOUND no longer bounds it",
                    arena.len()
                );
                assert_eq!(
                    arena.capacity(),
                    warm,
                    "{encoding:?}: a warm arena reallocated"
                );
                batches.push(arena.clone());
            }
            assert!(
                batches[0] != batches[1],
                "{encoding:?}: the batches are identical, so nothing was exercised"
            );
        }
    }

    /// Every field of every datum, decoded under `EVENT_SCHEMA_JSON` by the
    /// reference implementation and compared against the event that wrote it.
    ///
    /// `apache-avro` catches a datum whose *shape* drifts from the published
    /// schema on its own (a misnamed field, a wrong type, a branch index past
    /// the union) and rejects it at encode time. It cannot catch a field
    /// carrying the wrong value, which is what the assertions here cover: two
    /// fields of the same Avro type transposed, or one zeroed, fails on this
    /// test rather than on a consumer.
    #[cfg(feature = "avro")]
    #[test]
    fn every_avro_datum_reads_back_field_for_field() {
        use apache_avro::types::Value;

        let schema = apache_avro::Schema::parse_str(crate::EVENT_SCHEMA_JSON).unwrap();
        let encoder = Encoder::new(Encoding::Avro).unwrap();
        let mut seen = [0usize; 3];

        for event in events(512) {
            let mut buf = Vec::new();
            encoder.encode(&event, &mut buf).unwrap();
            let decoded = apache_avro::from_avro_datum(&schema, &mut buf.as_slice(), None).unwrap();
            let Value::Union(branch, inner) = decoded else {
                panic!("the top-level schema is a union; got {decoded:?}");
            };
            let Value::Record(fields) = *inner else {
                panic!("every union branch is a record");
            };
            // By name rather than by position: the schema fixes the names, and
            // a lookup that followed the encoder's own order could not detect
            // two fields swapped.
            let field = |name: &str| {
                fields
                    .iter()
                    .find(|(f, _)| f == name)
                    .unwrap_or_else(|| panic!("branch {branch} has no field {name}"))
                    .1
                    .clone()
            };

            seen[branch as usize] += 1;
            match &event {
                StorefrontEvent::OrderPlaced(e) => {
                    assert_eq!(branch, 0);
                    assert_eq!(field("order_id"), Value::Long(e.order_id as i64));
                    assert_eq!(field("customer_id"), Value::Int(e.customer_id as i32));
                    assert_eq!(field("region"), Value::String(e.region.to_string()));
                    assert_eq!(field("placed_at"), Value::TimestampMillis(e.placed_at));
                    let Value::Array(lines) = field("lines") else {
                        panic!("lines is an array");
                    };
                    assert_eq!(lines.len(), e.lines.len());
                    for (decoded, line) in lines.iter().zip(&e.lines) {
                        let Value::Record(cells) = decoded else {
                            panic!("a line is a record");
                        };
                        let cell =
                            |name: &str| cells.iter().find(|(f, _)| f == name).unwrap().1.clone();
                        assert_eq!(cell("sku"), Value::String(line.sku.to_string()));
                        assert_eq!(cell("qty"), Value::Int(line.qty as i32));
                        assert_eq!(cell("unit_cents"), Value::Int(line.unit_cents as i32));
                    }
                }
                StorefrontEvent::PaymentCaptured(e) => {
                    assert_eq!(branch, 1);
                    assert_eq!(field("order_id"), Value::Long(e.order_id as i64));
                    assert_eq!(field("amount_cents"), Value::Long(e.amount_cents as i64));
                }
                StorefrontEvent::RefundIssued(e) => {
                    assert_eq!(branch, 2);
                    assert_eq!(field("order_id"), Value::Long(e.order_id as i64));
                    assert_eq!(field("amount_cents"), Value::Long(e.amount_cents as i64));
                    assert_eq!(field("reason"), Value::String(e.reason.to_string()));
                }
            }
        }
        assert!(
            seen.iter().all(|&n| n > 0),
            "some union branch went untested: {seen:?}"
        );
    }

    /// The datum bytes, pinned. Decoding proves the value survives a
    /// round trip through this build; this holds the wire format itself
    /// steady, including across a change in how `apache-avro` encodes.
    #[cfg(feature = "avro")]
    #[test]
    fn the_avro_wire_format_is_pinned_across_builds() {
        let encoder = Encoder::new(Encoding::Avro).unwrap();
        let mut arena = Vec::new();
        for event in events(500) {
            encoder.encode(&event, &mut arena).unwrap();
        }
        let digest = arena.iter().fold(0xcbf2_9ce4_8422_2325_u64, |hash, &byte| {
            (hash ^ u64::from(byte)).wrapping_mul(0x100_0000_01b3)
        });
        assert_eq!(arena.len(), 17_343, "the encoded length moved");
        assert_eq!(
            digest, 9_404_063_270_987_324_100,
            "the encoded datums moved"
        );
    }

    #[cfg(not(feature = "avro"))]
    #[test]
    fn avro_without_the_feature_fails_at_open_rather_than_silently_emitting_json() {
        let err = Encoder::new(Encoding::Avro).unwrap_err().to_string();
        assert!(err.contains("avro"), "{err}");
    }
}