feltdb 0.2.0

Embedded durable state store: an append-only log with atomic multi-record transactions, version-conditional writes, and idempotent retries
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
//! Durable operation identity.
//!
//! `(origin, sequence)` is an identity, not a counter. Receivers deduplicate on
//! it, so two operations must never be assigned the same one -- and that has to
//! hold across a process restart, which is where it did not.
//!
//! `dedup_bound_investigation.rs` established the failure: `feltdb_node` kept
//! the counter in memory and started it at 1 on every launch, while the causal
//! clock was rebuilt from the log. A restarted origin came back knowing its
//! causal position and not its identity position, re-issued identities its own
//! earlier operations already held, and its peers discarded the new writes as
//! duplicates -- with no error and no divergence signal.
//!
//! The authority is now the durable log, and specifically the log rather than
//! the causal clock. Those are **different identity domains**: the clock
//! component advances only on a successful apply, the sequence is burned by an
//! attempt, and they disagree by exactly the number of refused transactions.
//! `replication-convergence.md` fixes that the sequence is sparse and that
//! making it dense is a separate deferred decision, so nothing here derives
//! identity from the clock and nothing here makes the sequence dense.
//!
//! The invariant, stated precisely, because the weaker version is not enough:
//!
//! > no two **persisted** envelopes from one origin ever share a sequence.
//!
//! Not "restart usually finds the highest sequence". An identity is reserved
//! forever exactly when an envelope bearing it becomes durable, which is why a
//! number burned and then lost to a crash is safe to re-issue -- nothing
//! carrying it was ever written.

#[cfg(test)]
mod tests {
    use crate::convergence::VectorClock;
    use crate::distributed_transactions::{
        DistributedTransactionExecutor, ReceiveOutcome, ReplicationMessage, SubmitOutcome,
        TransactionEnvelope,
    };
    use crate::state_hash::StateHash;
    use crate::transactions::{
        ConsistencyContract, Operation, OperationCommand, OperationId, StateVersion,
    };
    use std::collections::HashMap;
    use std::path::PathBuf;

    fn zero_version() -> StateVersion {
        StateVersion::new(VectorClock::new(), "0".repeat(64))
    }

    fn operation_at(node: &str, sequence: u64, payload: &str) -> Operation {
        let mut fields = HashMap::new();
        fields.insert("record".to_string(), serde_json::json!(payload));
        Operation::new(
            OperationId::new(node.to_string(), sequence),
            zero_version(),
            format!("tx_{payload}"),
            OperationCommand {
                op_type: "set".to_string(),
                collection: "items".to_string(),
                record_id: payload.to_string(),
                fields,
            },
            node.to_string(),
        )
    }

    /// Open a node over a log path, replaying it when it already exists --
    /// which is what the node binary does on start.
    fn open(node: &str, path: &PathBuf) -> DistributedTransactionExecutor {
        let mut executor = DistributedTransactionExecutor::with_log(
            node.to_string(),
            StateHash::from_hex("0".repeat(64)),
            Some(path.clone()),
        )
        .expect("executor");
        if path.exists() {
            executor.load_from_disk(path.clone()).expect("recover");
        }
        executor
    }

    /// Commit as the node binary does: chain the parent version forward on
    /// success, and give each operation a distinct id.
    ///
    /// The operation id is taken from the executor's next identity, which is
    /// exactly what the binary does now that the executor owns allocation.
    fn commit(
        executor: &mut DistributedTransactionExecutor,
        node: &str,
        payload: &str,
    ) -> TransactionEnvelope {
        let sequence = executor.next_origin_sequence();
        let parent = chained_version(executor, node);
        match executor
            .submit_local_transaction(
                format!("tx_{payload}"),
                parent,
                vec![operation_at(node, sequence, payload)],
                ConsistencyContract::local(),
            )
            .expect("submit")
        {
            SubmitOutcome::Admitted(envelope) => envelope,
            SubmitOutcome::CapacityExceeded { .. } => panic!("unexpected capacity refusal"),
        }
    }

    /// The parent version this node would commit on top of.
    fn chained_version(executor: &DistributedTransactionExecutor, node: &str) -> StateVersion {
        match executor.get_replica_state(node) {
            Some(replica) => {
                StateVersion::new(replica.vector_clock.clone(), replica.state_hash.to_string())
            }
            None => zero_version(),
        }
    }

    fn message(envelope: TransactionEnvelope) -> ReplicationMessage {
        let origin = envelope.originating_node.clone();
        let sequence = envelope.envelope_id.sequence;
        ReplicationMessage::new(envelope, origin, "receiver".to_string(), sequence)
    }

    // -- allocation --------------------------------------------------------

    #[test]
    fn identity_a_fresh_origin_starts_at_one() {
        let dir = tempfile::tempdir().unwrap();
        let mut node = open("A", &dir.path().join("operations.jsonl"));
        assert_eq!(
            node.next_origin_sequence(),
            1,
            "a brand-new origin legitimately starts at 1"
        );
        assert_eq!(commit(&mut node, "A", "first").envelope_id.sequence, 1);
    }

    #[test]
    fn identity_restart_after_three_operations_continues_at_four() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("operations.jsonl");
        {
            let mut node = open("A", &path);
            for payload in ["one", "two", "three"] {
                commit(&mut node, "A", payload);
            }
            assert_eq!(node.next_origin_sequence(), 4);
        }
        let restarted = open("A", &path);
        assert_eq!(
            restarted.next_origin_sequence(),
            4,
            "identity is recovered from the durable log, not reset"
        );
    }

    #[test]
    fn identity_is_recovered_even_without_replaying_the_log() {
        // `with_log` recovers identity too, so a caller that opens an existing
        // log and issues an operation without calling load_from_disk still
        // cannot re-issue an identity that log contains.
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("operations.jsonl");
        {
            let mut node = open("A", &path);
            commit(&mut node, "A", "one");
            commit(&mut node, "A", "two");
        }
        let reopened = DistributedTransactionExecutor::with_log(
            "A".to_string(),
            StateHash::from_hex("0".repeat(64)),
            Some(path),
        )
        .expect("executor");
        assert_eq!(reopened.next_origin_sequence(), 3);
    }

    #[test]
    fn identity_spaces_are_independent_per_origin() {
        // A receiver that has applied another node's high sequences must not
        // inflate its own identity space: the log holds replicated envelopes
        // too, and another node's sequence says nothing about this one's.
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("operations.jsonl");
        let mut receiver = open("receiver", &path);

        for position in 1..=9u64 {
            let mut envelope = TransactionEnvelope::new(
                "A".to_string(),
                position,
                format!("tx_a{position}"),
                vec![operation_at("A", position, &format!("a{position}"))],
                ConsistencyContract::local(),
            );
            let mut clock = VectorClock::new();
            clock.clocks.insert("A".to_string(), position);
            envelope.vector_clock = clock;
            receiver
                .receive_replicated(message(envelope), zero_version())
                .expect("receive");
        }

        assert_eq!(
            receiver.next_origin_sequence(),
            1,
            "nine applied envelopes from A leave this node's own identity space untouched"
        );
        assert_eq!(
            commit(&mut receiver, "receiver", "mine")
                .envelope_id
                .sequence,
            1
        );

        let restarted = open("receiver", &path);
        assert_eq!(
            restarted.next_origin_sequence(),
            2,
            "and recovery filters by origin too"
        );
    }

    // -- crash boundaries --------------------------------------------------

    #[test]
    fn identity_survives_a_crash_after_the_operation_became_durable() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("operations.jsonl");
        {
            let mut node = open("A", &path);
            commit(&mut node, "A", "durable");
            // The process dies here: no clean shutdown, no flush of any
            // in-memory counter. The envelope is already fsynced.
        }
        let restarted = open("A", &path);
        assert_eq!(
            restarted.next_origin_sequence(),
            2,
            "the identity of a durable operation is never re-issued"
        );
    }

    #[test]
    fn identity_burned_without_persistence_is_safely_reissued_after_a_crash() {
        // allocate -> crash before persistence -> restart.
        //
        // The number is re-issued, and that is correct rather than a lapse: no
        // envelope bearing it was ever written, so nothing can collide with it.
        // Reserving it forever would leak identity on every refused
        // transaction and would make the durable sequence advance past
        // operations that never existed.
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("operations.jsonl");
        {
            let mut node = open("A", &path);
            commit(&mut node, "A", "one");
            let burned = node.allocate_origin_sequence();
            assert_eq!(burned, 2, "claimed but never persisted");
        }
        let mut restarted = open("A", &path);
        assert_eq!(
            restarted.next_origin_sequence(),
            2,
            "the log holds only sequence 1, so 2 is free and is re-issued"
        );
        assert_eq!(commit(&mut restarted, "A", "two").envelope_id.sequence, 2);
    }

    #[test]
    fn identity_allocation_stays_sparse_because_the_frozen_contract_says_so() {
        // A burned number leaves a gap within a single process lifetime.
        // Making the sequence dense is a separate deferred decision
        // (replication-convergence.md) and is deliberately not made here.
        let dir = tempfile::tempdir().unwrap();
        let mut node = open("A", &dir.path().join("operations.jsonl"));
        commit(&mut node, "A", "one");
        let _burned = node.allocate_origin_sequence();
        assert_eq!(
            commit(&mut node, "A", "three").envelope_id.sequence,
            3,
            "the attempt burned 2; the sequence is sparse, as the contract fixes it"
        );
    }

    #[test]
    fn identity_is_not_burned_by_a_capacity_refusal() {
        // A refusal admits nothing, so it must not consume an identity either:
        // there is no operation for that number to account for.
        use crate::distributed_transactions::CausalCapacity;
        let dir = tempfile::tempdir().unwrap();
        let mut node = open("A", &dir.path().join("operations.jsonl"));
        node.set_causal_capacity(CausalCapacity::new(0, usize::MAX));

        let before = node.next_origin_sequence();
        let outcome = node
            .submit_local_transaction(
                "tx_refused".to_string(),
                zero_version(),
                vec![operation_at("A", 1, "refused")],
                ConsistencyContract::local(),
            )
            .expect("submit");
        assert!(matches!(outcome, SubmitOutcome::CapacityExceeded { .. }));
        assert_eq!(
            node.next_origin_sequence(),
            before,
            "a refusal consumes no identity"
        );
    }

    #[test]
    fn identity_holds_across_many_restart_cycles() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("operations.jsonl");
        let mut issued = Vec::new();
        for cycle in 0..25 {
            let mut node = open("A", &path);
            for index in 0..4 {
                issued.push(
                    commit(&mut node, "A", &format!("c{cycle}_{index}"))
                        .envelope_id
                        .sequence,
                );
            }
        }
        let mut unique = issued.clone();
        unique.sort_unstable();
        unique.dedup();
        assert_eq!(
            unique.len(),
            issued.len(),
            "100 operations across 25 restart cycles produced no repeated identity"
        );
        assert_eq!(issued.len(), 100);
    }

    // -- the regression, at executor level ---------------------------------

    #[test]
    fn identity_a_receiver_accepts_writes_from_an_origin_that_restarted() {
        // The exact failure #210 found, now at the executor boundary.
        let origin_dir = tempfile::tempdir().unwrap();
        let origin_path = origin_dir.path().join("operations.jsonl");
        let receiver_dir = tempfile::tempdir().unwrap();
        let mut receiver = open("receiver", &receiver_dir.path().join("operations.jsonl"));

        let before = {
            let mut origin = open("A", &origin_path);
            commit(&mut origin, "A", "before")
        };
        assert!(matches!(
            receiver
                .receive_replicated(message(before), zero_version())
                .expect("receive"),
            ReceiveOutcome::Applied { .. }
        ));

        // The origin restarts and commits again.
        let after = {
            let mut origin = open("A", &origin_path);
            commit(&mut origin, "A", "after")
        };
        assert_eq!(
            after.envelope_id.sequence, 2,
            "the identity did not restart"
        );

        let outcome = receiver
            .receive_replicated(message(after), zero_version())
            .expect("receive");
        assert!(
            matches!(outcome, ReceiveOutcome::Applied { .. }),
            "a write from a restarted origin must be applied, not discarded: {outcome:?}"
        );
        assert_eq!(
            receiver.causal_frontier().get("A"),
            2,
            "and the frontier advances"
        );

        let state = receiver.canonical_state().expect("state");
        assert!(state
            .records
            .contains_key(&("items".to_string(), "after".to_string())));
        assert!(state
            .records
            .contains_key(&("items".to_string(), "before".to_string())));
    }

    #[test]
    fn identity_a_genuine_retransmission_is_still_refused() {
        // The fix must not weaken deduplication: the same envelope twice is
        // still one operation, and a *new* operation after it is still new.
        let origin_dir = tempfile::tempdir().unwrap();
        let receiver_dir = tempfile::tempdir().unwrap();
        let mut origin = open("A", &origin_dir.path().join("operations.jsonl"));
        let mut receiver = open("receiver", &receiver_dir.path().join("operations.jsonl"));

        let first = commit(&mut origin, "A", "one");
        receiver
            .receive_replicated(message(first.clone()), zero_version())
            .expect("receive");
        assert_eq!(
            receiver
                .receive_replicated(message(first), zero_version())
                .expect("receive"),
            ReceiveOutcome::AlreadyKnown,
            "a retransmission is refused"
        );

        let second = commit(&mut origin, "A", "two");
        assert!(
            matches!(
                receiver
                    .receive_replicated(message(second), zero_version())
                    .expect("receive"),
                ReceiveOutcome::Applied { .. }
            ),
            "and a new operation after a duplicate is still accepted"
        );
        assert_eq!(receiver.causal_frontier().get("A"), 2);
    }

    #[test]
    fn identity_no_collision_when_the_origin_restarts_while_the_receiver_holds_a_backlog() {
        // The two mechanisms have to compose: the receiver is holding causal
        // backlog from this origin when the origin restarts, so a colliding
        // identity would land on a pending entry rather than an applied one.
        let origin_dir = tempfile::tempdir().unwrap();
        let origin_path = origin_dir.path().join("operations.jsonl");
        let receiver_dir = tempfile::tempdir().unwrap();
        let mut receiver = open("receiver", &receiver_dir.path().join("operations.jsonl"));

        let (first, second) = {
            let mut origin = open("A", &origin_path);
            (
                commit(&mut origin, "A", "one"),
                commit(&mut origin, "A", "two"),
            )
        };

        // Only the second is delivered, so it is held waiting for the first.
        assert!(matches!(
            receiver
                .receive_replicated(message(second), zero_version())
                .expect("receive"),
            ReceiveOutcome::PendingDependencies { .. }
        ));
        assert_eq!(receiver.pending_causal_count(), 1);

        // The origin restarts, with the receiver still holding that backlog.
        let third = {
            let mut origin = open("A", &origin_path);
            commit(&mut origin, "A", "three")
        };
        assert_eq!(
            third.envelope_id.sequence, 3,
            "no collision with the held entry"
        );

        let held = receiver
            .receive_replicated(message(third), zero_version())
            .expect("receive");
        assert!(
            matches!(held, ReceiveOutcome::PendingDependencies { .. }),
            "it is held on its own merits, not mistaken for the entry already pending: {held:?}"
        );
        assert_eq!(receiver.pending_causal_count(), 2);

        // The gap closes and everything applies exactly once.
        receiver
            .receive_replicated(message(first), zero_version())
            .expect("receive");
        assert_eq!(receiver.pending_causal_count(), 0);
        assert_eq!(receiver.causal_frontier().get("A"), 3);
        let state = receiver.canonical_state().expect("state");
        for record in ["one", "two", "three"] {
            assert!(state
                .records
                .contains_key(&("items".to_string(), record.to_string())));
        }
    }
}