distributed 4.3.0

CQRS/ES framework for Rust using Plain Old Rust Structs — append-only events, replay, snapshots, outbox, service bus, and pluggable infrastructure
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
use std::sync::Arc;

use distributed::{
    Aggregate, AggregateBuilder, CommitBatch, Entity, GetStream, RepositoryError, SnapshotRecord,
    SnapshotStore, SnapshotWrite, StreamIdentity, StreamWrite, TraceContext, TransactionalCommit,
};
use tokio::sync::Barrier;

use super::checkout::{CHECKOUT_SEAT_RESERVED_STATUS, SEAT_RESERVED_STATUS};
use super::checkout_saga::CheckoutSaga;
use super::seat::Seat;

pub use super::ids::unique_id;

pub async fn aggregate_checkout_flow_persists_reloaded_state<R>(repo: R)
where
    R: GetStream + TransactionalCommit + Clone + Send + Sync + 'static,
{
    let checkout_id = unique_id("checkout");
    let seat_id = unique_id("seat");
    let seat_category = "balcony".to_string();

    let seat_added = add_seat(repo.clone(), seat_id.clone(), seat_category.clone())
        .await
        .expect("seat add should commit");
    assert_eq!(seat_added.seat_id, seat_id);

    let checkout_started = start_checkout(
        repo.clone(),
        checkout_id.clone(),
        seat_id.clone(),
        seat_category.clone(),
    )
    .await
    .expect("checkout start should commit");

    let seat_reserved = reserve_started_checkout_seat(repo.clone(), checkout_started)
        .await
        .expect("seat reservation should commit");
    assert_eq!(seat_reserved.checkout_id, checkout_id);

    let completed = record_seat_reserved(repo.clone(), seat_reserved)
        .await
        .expect("checkout saga should record reservation");
    assert_eq!(completed.seat_id, seat_id);

    let checkout_repo = repo.clone().aggregate::<CheckoutSaga>();
    let loaded_checkout = checkout_repo
        .get(&checkout_id)
        .await
        .expect("checkout reload should succeed")
        .expect("checkout should exist");
    assert_eq!(loaded_checkout.status, CHECKOUT_SEAT_RESERVED_STATUS);
    assert_eq!(loaded_checkout.reserved_seat_id, seat_id);
    assert_eq!(loaded_checkout.entity.events().len(), 2);

    let seat_repo = repo.aggregate::<Seat>();
    let loaded_seat = seat_repo
        .get(&seat_id)
        .await
        .expect("seat reload should succeed")
        .expect("seat should exist");
    assert_eq!(loaded_seat.status, SEAT_RESERVED_STATUS);
    assert_eq!(loaded_seat.checkout_id, checkout_id);
    assert_eq!(loaded_seat.entity.events().len(), 2);
}

pub async fn get_all_and_commit_all_round_trip<R>(repo: R)
where
    R: GetStream + TransactionalCommit + Clone + Send + Sync + 'static,
{
    let first_id = unique_id("seat-a");
    let second_id = unique_id("seat-b");
    let seat_repo = repo.aggregate::<Seat>();

    let mut first = Seat::default();
    first
        .add(first_id.clone(), "floor".into())
        .expect("first seat should be valid");
    let mut second = Seat::default();
    second
        .add(second_id.clone(), "box".into())
        .expect("second seat should be valid");

    seat_repo
        .commit_all(&mut [&mut first, &mut second])
        .await
        .expect("commit_all should persist both seats");

    let loaded = seat_repo
        .get_all(&[first_id.as_str(), second_id.as_str()])
        .await
        .expect("get_all should reload both seats");
    assert_eq!(loaded.len(), 2);
    let mut actual_ids = loaded
        .iter()
        .map(|seat| seat.entity.id().to_string())
        .collect::<Vec<_>>();
    actual_ids.sort();
    let mut expected_ids = vec![first_id, second_id];
    expected_ids.sort();
    assert_eq!(actual_ids, expected_ids);
}

pub async fn multi_stream_conflict_rolls_back_other_stream_and_snapshot<R>(repo: R)
where
    R: GetStream + TransactionalCommit + SnapshotStore + Clone + Send + Sync + 'static,
{
    let seat_repo = repo.clone().aggregate::<Seat>();
    let seat_id = unique_id("conflict-seat");

    let mut original = Seat::default();
    original
        .add(seat_id.clone(), "balcony".into())
        .expect("seat should be valid");
    seat_repo
        .commit(&mut original)
        .await
        .expect("initial seat commit should succeed");

    let mut stale = seat_repo
        .get(&seat_id)
        .await
        .expect("stale load should succeed")
        .expect("stale seat should exist");
    let mut winner = seat_repo
        .get(&seat_id)
        .await
        .expect("winner load should succeed")
        .expect("winner seat should exist");
    stale
        .reserve(
            unique_id("checkout-stale"),
            seat_id.clone(),
            stale.category.clone(),
        )
        .expect("stale reservation should be valid locally");
    winner
        .reserve(
            unique_id("checkout-winner"),
            seat_id.clone(),
            winner.category.clone(),
        )
        .expect("winner reservation should be valid locally");
    seat_repo
        .commit(&mut winner)
        .await
        .expect("winner commit should succeed");

    let checkout_id = unique_id("rollback-checkout");
    let mut checkout = CheckoutSaga::default();
    checkout
        .start(
            checkout_id.clone(),
            unique_id("rollback-seat"),
            "floor".into(),
        )
        .expect("checkout should be valid locally");

    let stale_identity = StreamIdentity::new(Seat::aggregate_type(), &seat_id)
        .expect("stale identity should be valid");
    let checkout_identity = StreamIdentity::new(CheckoutSaga::aggregate_type(), &checkout_id)
        .expect("checkout identity should be valid");
    let err = repo
        .commit_batch(CommitBatch {
            inbox_receipts: Vec::new(),
            streams: vec![
                StreamWrite::new(stale_identity, stale.entity_mut()),
                StreamWrite::new(checkout_identity.clone(), checkout.entity_mut()),
            ],
            outbox_messages: Vec::new(),
            read_model_plans: Vec::new(),
            snapshots: vec![SnapshotWrite::Save {
                identity: checkout_identity.clone(),
                record: SnapshotRecord::new(
                    CheckoutSaga::aggregate_type(),
                    checkout_id,
                    1,
                    1,
                    vec![7],
                ),
            }],
        })
        .await
        .expect_err("stale batch should conflict");

    assert!(matches!(err, RepositoryError::ConcurrentWrite { .. }));
    assert!(repo
        .get_stream(&checkout_identity)
        .await
        .expect("rollback stream lookup should succeed")
        .is_none());
    assert!(repo
        .get_snapshot(&checkout_identity)
        .await
        .expect("rollback snapshot lookup should succeed")
        .is_none());
    assert_eq!(stale.entity.committed_version(), 1);
    assert_eq!(stale.entity.new_events().len(), 1);
}

pub async fn duplicate_stream_identity_is_rejected_before_write<R>(repo: R)
where
    R: GetStream + TransactionalCommit + Send + Sync + 'static,
{
    let id = unique_id("duplicate-seat");
    let identity =
        StreamIdentity::new(Seat::aggregate_type(), &id).expect("identity should be valid");
    let mut first = Entity::with_id(&id);
    first
        .digest_empty("first_recorded")
        .expect("first event should encode");
    let mut second = Entity::with_id(&id);
    second
        .digest_empty("second_recorded")
        .expect("second event should encode");

    let err = repo
        .commit_batch(CommitBatch::new(vec![
            StreamWrite::new(identity.clone(), &mut first),
            StreamWrite::new(identity.clone(), &mut second),
        ]))
        .await
        .expect_err("duplicate stream should be rejected");

    assert!(
        matches!(
            &err,
            RepositoryError::DuplicateStreamInBatch { id: dup }
                if *dup == format!("{}:{id}", Seat::aggregate_type())
        ),
        "unexpected error: {err}"
    );
    assert!(repo
        .get_stream(&identity)
        .await
        .expect("duplicate stream lookup should succeed")
        .is_none());
}

pub async fn metadata_round_trips<R>(repo: R)
where
    R: GetStream + TransactionalCommit + Clone + Send + Sync + 'static,
{
    let id = unique_id("metadata-seat");
    let mut seat = Seat::default();
    seat.entity.set_correlation_id("corr-conformance");
    seat.entity.set_causation_id("cmd-conformance");
    let trace_context = TraceContext {
        traceparent: Some("00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01".to_string()),
        tracestate: Some("vendor=value".to_string()),
    };
    seat.entity.set_trace_context(&trace_context);
    seat.add(id.clone(), "gallery".into())
        .expect("seat should be valid");

    repo.clone()
        .aggregate::<Seat>()
        .commit(&mut seat)
        .await
        .expect("metadata seat should commit");

    let identity =
        StreamIdentity::new(Seat::aggregate_type(), &id).expect("identity should be valid");
    let entity = repo
        .get_stream(&identity)
        .await
        .expect("metadata stream should reload")
        .expect("metadata stream should exist");
    let event = entity
        .events()
        .first()
        .expect("metadata stream should contain one event");
    assert_eq!(event.correlation_id(), Some("corr-conformance"));
    assert_eq!(event.causation_id(), Some("cmd-conformance"));
    assert_eq!(event.trace_context(), trace_context);
}

pub async fn unsupported_codec_is_rejected_on_write<R>(repo: R)
where
    R: GetStream + TransactionalCommit + Send + Sync + 'static,
{
    let id = unique_id("bad-codec-seat");
    let identity =
        StreamIdentity::new(Seat::aggregate_type(), &id).expect("identity should be valid");
    let mut entity = Entity::with_id(&id);
    entity
        .digest_empty("bad_codec_recorded")
        .expect("event should encode before codec mutation");

    let mut value = serde_json::to_value(&entity).expect("entity should serialize");
    value["events"][0]["payload_codec"] = serde_json::json!("json");
    let mut bad_entity: Entity =
        serde_json::from_value(value).expect("mutated entity should deserialize");

    let err = repo
        .commit_batch(CommitBatch::new(vec![StreamWrite::new(
            identity.clone(),
            &mut bad_entity,
        )]))
        .await
        .expect_err("unsupported codec should be rejected");
    assert!(
        matches!(
            &err,
            RepositoryError::Storage {
                retryable: false,
                ..
            }
        ),
        "unexpected error: {err}"
    );
    assert!(
        err.to_string().contains("unsupported payload codec"),
        "unexpected error: {err}"
    );
    assert!(repo
        .get_stream(&identity)
        .await
        .expect("bad codec stream lookup should succeed")
        .is_none());
}

pub async fn snapshots_use_full_stream_identity<R>(repo: R)
where
    R: SnapshotStore + Send + Sync + 'static,
{
    let id = unique_id("snapshot");
    let seat_identity =
        StreamIdentity::new(Seat::aggregate_type(), &id).expect("seat identity should be valid");
    let checkout_identity = StreamIdentity::new(CheckoutSaga::aggregate_type(), &id)
        .expect("checkout identity should be valid");

    repo.save_snapshot(
        &seat_identity,
        SnapshotRecord::new(Seat::aggregate_type(), id.clone(), 1, 1, vec![1]),
    )
    .await
    .expect("seat snapshot should save");
    repo.save_snapshot(
        &checkout_identity,
        SnapshotRecord::new(CheckoutSaga::aggregate_type(), id, 2, 1, vec![2]),
    )
    .await
    .expect("checkout snapshot should save");

    let loaded_seat = repo
        .get_snapshot(&seat_identity)
        .await
        .expect("seat snapshot should load")
        .expect("seat snapshot should exist");
    let loaded_checkout = repo
        .get_snapshot(&checkout_identity)
        .await
        .expect("checkout snapshot should load")
        .expect("checkout snapshot should exist");

    assert_eq!(loaded_seat.version, 1);
    assert_eq!(loaded_seat.aggregate_type, Seat::aggregate_type());
    assert_eq!(loaded_seat.payload, vec![1]);
    assert_eq!(loaded_checkout.version, 2);
    assert_eq!(
        loaded_checkout.aggregate_type,
        CheckoutSaga::aggregate_type()
    );
    assert_eq!(loaded_checkout.payload, vec![2]);
}

/// Red-team concurrency: `N` tasks each load the **same** aggregate at the same
/// committed version and race to append a conflicting event, synchronized by a
/// barrier so the writes overlap as tightly as the runtime allows. Optimistic
/// concurrency must let **exactly one** win; the rest must observe
/// `ConcurrentWrite`. Crucially, the surviving stream must be gap-free and
/// duplicate-free: this exercises the unique `(aggregate, sequence)` PK backstop
/// behind the read-then-check version window under real concurrency (the
/// existing `concurrent_writes_detected` test only races sequentially).
///
/// `concurrency` must be >= 2 (the test asserts at least one conflict).
pub async fn racing_commits_one_wins_one_conflicts<R>(repo: R, concurrency: usize)
where
    R: GetStream + TransactionalCommit + Clone + Send + Sync + 'static,
{
    assert!(
        concurrency >= 2,
        "racing test needs at least two writers to produce a conflict"
    );

    let seat_id = unique_id("racing-seat");
    let seat_repo = repo.clone().aggregate::<Seat>();

    // Seed at version 1. Every racer will load this exact state.
    let mut seed = Seat::default();
    seed.add(seat_id.clone(), "balcony".into())
        .expect("seed seat should be valid");
    seat_repo
        .commit(&mut seed)
        .await
        .expect("seed seat commit should succeed");

    // Each racer loads independently *before* the barrier so all of them hold
    // a v1 snapshot; the barrier then releases the commits together.
    let barrier = Arc::new(Barrier::new(concurrency));
    let mut handles = Vec::with_capacity(concurrency);
    for index in 0..concurrency {
        let repo = repo.clone();
        let barrier = Arc::clone(&barrier);
        let seat_id = seat_id.clone();
        handles.push(tokio::spawn(async move {
            let seat_repo = repo.aggregate::<Seat>();
            let mut seat = seat_repo
                .get(&seat_id)
                .await
                .expect("racer load should succeed")
                .expect("racer should see the seeded seat");
            assert_eq!(
                seat.entity.committed_version(),
                1,
                "every racer must start from the same committed version"
            );
            // A reservation is a single new event on top of v1 for every racer,
            // so they all target sequence 2 — exactly one can land it.
            seat.reserve(
                format!("checkout-{index}"),
                seat_id.clone(),
                seat.category.clone(),
            )
            .expect("reservation should be locally valid");

            // Synchronize the commit attempts as tightly as possible.
            barrier.wait().await;
            seat_repo.commit(&mut seat).await
        }));
    }

    let mut successes = 0usize;
    let mut conflicts = 0usize;
    for handle in handles {
        match handle.await.expect("racer task should not panic") {
            Ok(()) => successes += 1,
            Err(RepositoryError::ConcurrentWrite { .. }) => conflicts += 1,
            Err(other) => panic!("unexpected racer error: {other:?}"),
        }
    }

    assert_eq!(
        successes, 1,
        "exactly one racer must win the optimistic race"
    );
    assert_eq!(
        conflicts,
        concurrency - 1,
        "every other racer must observe ConcurrentWrite"
    );

    // The surviving stream must be a clean append-only log: versions 1..=2 with
    // no gaps and no duplicate sequences. A TOCTOU leak would manifest as a
    // duplicate sequence or a third event here.
    let identity =
        StreamIdentity::new(Seat::aggregate_type(), &seat_id).expect("identity should be valid");
    let entity = repo
        .get_stream(&identity)
        .await
        .expect("final stream lookup should succeed")
        .expect("final stream should exist");
    let sequences = entity
        .events()
        .iter()
        .map(|event| event.sequence)
        .collect::<Vec<_>>();
    assert_eq!(
        sequences,
        vec![1, 2],
        "winning stream must be exactly two contiguous events with no gaps or duplicates"
    );
    assert_eq!(
        entity.committed_version(),
        2,
        "the stream advances by exactly one version despite N racers"
    );
}

async fn add_seat<R>(
    repo: R,
    seat_id: String,
    category: String,
) -> Result<super::checkout::SeatAdded, RepositoryError>
where
    R: TransactionalCommit + Clone + Send + Sync + 'static,
{
    let mut seat = Seat::default();
    seat.add(seat_id.clone(), category.clone())
        .map_err(|err| RepositoryError::Model(err.to_string()))?;
    repo.aggregate::<Seat>().commit(&mut seat).await?;
    Ok(super::checkout::SeatAdded { seat_id, category })
}

async fn start_checkout<R>(
    repo: R,
    checkout_id: String,
    seat_id: String,
    seat_category: String,
) -> Result<super::checkout::CheckoutStarted, RepositoryError>
where
    R: TransactionalCommit + Clone + Send + Sync + 'static,
{
    let mut checkout = CheckoutSaga::default();
    checkout
        .start(checkout_id.clone(), seat_id.clone(), seat_category.clone())
        .map_err(|err| RepositoryError::Model(err.to_string()))?;
    repo.aggregate::<CheckoutSaga>()
        .commit(&mut checkout)
        .await?;
    Ok(super::checkout::CheckoutStarted {
        checkout_id,
        seat_id,
        seat_category,
    })
}

async fn reserve_started_checkout_seat<R>(
    repo: R,
    event: super::checkout::CheckoutStarted,
) -> Result<super::checkout::SeatReserved, RepositoryError>
where
    R: GetStream + TransactionalCommit + Clone + Send + Sync + 'static,
{
    let seat_repo = repo.aggregate::<Seat>();
    let mut seat =
        seat_repo
            .get(&event.seat_id)
            .await?
            .ok_or_else(|| RepositoryError::NotFound {
                id: event.seat_id.clone(),
            })?;
    let reserved = super::checkout::SeatReserved {
        checkout_id: event.checkout_id,
        seat_id: event.seat_id.clone(),
        seat_category: event.seat_category.clone(),
    };
    seat.reserve(
        reserved.checkout_id.clone(),
        reserved.seat_id.clone(),
        reserved.seat_category.clone(),
    )
    .map_err(|err| RepositoryError::Model(err.to_string()))?;
    seat_repo.commit(&mut seat).await?;
    Ok(reserved)
}

async fn record_seat_reserved<R>(
    repo: R,
    event: super::checkout::SeatReserved,
) -> Result<super::checkout::SeatReservationCompleted, RepositoryError>
where
    R: GetStream + TransactionalCommit + Clone + Send + Sync + 'static,
{
    let checkout_repo = repo.aggregate::<CheckoutSaga>();
    let mut checkout = checkout_repo
        .get(&event.checkout_id)
        .await?
        .ok_or_else(|| RepositoryError::NotFound {
            id: event.checkout_id.clone(),
        })?;
    checkout
        .record_seat_reserved(
            event.checkout_id.clone(),
            event.seat_id.clone(),
            event.seat_category.clone(),
        )
        .map_err(|err| RepositoryError::Model(err.to_string()))?;
    checkout_repo.commit(&mut checkout).await?;
    Ok(super::checkout::SeatReservationCompleted {
        checkout_id: event.checkout_id,
        seat_id: event.seat_id,
        seat_category: event.seat_category,
    })
}