nodedb 0.2.0

Local-first, real-time, edge-to-cloud hybrid database for multi-modal workloads
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// SPDX-License-Identifier: BUSL-1.1

//! Transaction rollback matrix tests for KV, Columnar, and Timeseries engines.
//!
//! Each test follows the same pattern as `test_transaction_matrix`:
//!   1. Pre-condition: write a known state.
//!   2. TransactionBatch: valid write (first op) + deterministically failing write (second op).
//!   3. Assert: the first write was fully rolled back.

use nodedb::bridge::envelope::{PhysicalPlan, Status};
use nodedb::bridge::physical_plan::{
    AggregateSpec, ColumnarInsertIntent, ColumnarOp, DocumentOp, KvOp, MetaOp, QueryOp,
    TimeseriesOp,
};

use crate::helpers::*;

// ---------------------------------------------------------------------------
// Shared plan builders
// ---------------------------------------------------------------------------

fn kv_put(key: &[u8], value: &[u8]) -> PhysicalPlan {
    PhysicalPlan::Kv(KvOp::Put {
        collection: "kv_coll".into(),
        key: key.to_vec(),
        value: value.to_vec(),
        ttl_ms: 0,
        surrogate: nodedb_types::Surrogate::ZERO,
    })
}

fn kv_get(key: &[u8]) -> PhysicalPlan {
    PhysicalPlan::Kv(KvOp::Get {
        collection: "kv_coll".into(),
        key: key.to_vec(),
        rls_filters: Vec::new(),
        surrogate_ceiling: None,
    })
}

fn doc_put_conflict_seed(coll: &str) -> PhysicalPlan {
    PhysicalPlan::Document(DocumentOp::PointPut {
        collection: coll.into(),
        document_id: "conflict_doc".into(),
        value: b"seed".to_vec(),
        surrogate: nodedb_types::Surrogate::ZERO,
        pk_bytes: Vec::new(),
    })
}

fn doc_insert_conflict(coll: &str) -> PhysicalPlan {
    PhysicalPlan::Document(DocumentOp::PointInsert {
        collection: coll.into(),
        document_id: "conflict_doc".into(),
        value: b"conflict".to_vec(),
        surrogate: nodedb_types::Surrogate::ZERO,
        if_absent: false,
    })
}

fn doc_get(coll: &str, doc_id: &str) -> PhysicalPlan {
    PhysicalPlan::Document(DocumentOp::PointGet {
        collection: coll.into(),
        document_id: doc_id.into(),
        rls_filters: Vec::new(),
        system_as_of_ms: None,
        valid_at_ms: None,
        surrogate: nodedb_types::Surrogate::ZERO,
        pk_bytes: Vec::new(),
    })
}

// ---------------------------------------------------------------------------
// Pair: KV write (first) × Document conflict (second) — doc fails
// ---------------------------------------------------------------------------

#[test]
fn rollback_matrix_kv_then_doc_fail() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    // Pre-condition: key "k1" = "original".
    send_ok(&mut core, &mut tx, &mut rx, kv_put(b"k1", b"original"));
    // Seed the doc that will be used as a conflict trigger.
    send_ok(&mut core, &mut tx, &mut rx, doc_put_conflict_seed("docs"));

    // TransactionBatch: overwrite KV key + failing doc insert (key exists, not if_absent).
    let resp = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Meta(MetaOp::TransactionBatch {
            plans: vec![kv_put(b"k1", b"modified"), doc_insert_conflict("docs")],
        }),
    );
    assert_eq!(resp.status, Status::Error, "batch must fail on conflict");

    // KV key must be rolled back to "original".
    let r = send_raw(&mut core, &mut tx, &mut rx, kv_get(b"k1"));
    assert_eq!(r.status, Status::Ok);
    assert_eq!(&*r.payload, b"original", "kv put must be rolled back");
}

// ---------------------------------------------------------------------------
// Pair: Document write (first) × KV DDL inside batch (rejected)
// ---------------------------------------------------------------------------
// KV DDL ops (RegisterIndex, Truncate, etc.) are rejected with a typed error
// when inside a TransactionBatch. This test verifies the document write rolled
// back correctly when the KV write itself fails due to a prior-doc write
// combined with a KV Put that fails.
//
// Since we cannot force a raw KV failure after a successful doc write without
// custom error injection, we use a new-key KV put followed by a doc conflict
// as the second op — this exercises doc-first rollback as the symmetric case.

#[test]
fn rollback_matrix_doc_then_kv_fail() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    // Pre-condition: doc "rollback_doc" does not exist yet.
    // Seed the conflict document.
    send_ok(&mut core, &mut tx, &mut rx, doc_put_conflict_seed("docs"));

    // TransactionBatch: write a new KV key + failing doc insert.
    // On failure the new KV key must not persist.
    let resp = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Meta(MetaOp::TransactionBatch {
            plans: vec![
                kv_put(b"new_key", b"should_not_persist"),
                doc_insert_conflict("docs"),
            ],
        }),
    );
    assert_eq!(resp.status, Status::Error, "batch must fail on conflict");

    // "new_key" must have been rolled back — Get should return empty/NotFound.
    let r = send_raw(&mut core, &mut tx, &mut rx, kv_get(b"new_key"));
    let is_absent = r.status == Status::Error || r.payload.is_empty();
    assert!(
        is_absent,
        "new_key must not persist after rollback; payload={:?}",
        r.payload
    );
}

// ---------------------------------------------------------------------------
// Pair: KV delete (first) × Document conflict (second) — delete rolled back
// ---------------------------------------------------------------------------

#[test]
fn rollback_matrix_kv_delete_then_doc_fail() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    // Pre-condition: key "del_key" = "keep_me".
    send_ok(&mut core, &mut tx, &mut rx, kv_put(b"del_key", b"keep_me"));
    // Seed conflict doc.
    send_ok(&mut core, &mut tx, &mut rx, doc_put_conflict_seed("docs"));

    // TransactionBatch: delete the KV key + failing doc insert.
    let resp = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Meta(MetaOp::TransactionBatch {
            plans: vec![
                PhysicalPlan::Kv(KvOp::Delete {
                    collection: "kv_coll".into(),
                    keys: vec![b"del_key".to_vec()],
                }),
                doc_insert_conflict("docs"),
            ],
        }),
    );
    assert_eq!(resp.status, Status::Error, "batch must fail");

    // "del_key" must be restored to "keep_me".
    let r = send_raw(&mut core, &mut tx, &mut rx, kv_get(b"del_key"));
    assert_eq!(r.status, Status::Ok);
    assert_eq!(&*r.payload, b"keep_me", "kv delete must be rolled back");
}

// ---------------------------------------------------------------------------
// Verify: doc conflict after KV batch put rolls back all keys
// ---------------------------------------------------------------------------

#[test]
fn rollback_matrix_kv_batch_put_then_doc_fail() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    // Pre-conditions: k_a = "a_orig", k_b does not exist.
    send_ok(&mut core, &mut tx, &mut rx, kv_put(b"k_a", b"a_orig"));
    send_ok(&mut core, &mut tx, &mut rx, doc_put_conflict_seed("docs"));

    let resp = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Meta(MetaOp::TransactionBatch {
            plans: vec![
                PhysicalPlan::Kv(KvOp::BatchPut {
                    collection: "kv_coll".into(),
                    entries: vec![
                        (b"k_a".to_vec(), b"a_new".to_vec()),
                        (b"k_b".to_vec(), b"b_new".to_vec()),
                    ],
                    ttl_ms: 0,
                }),
                doc_insert_conflict("docs"),
            ],
        }),
    );
    assert_eq!(resp.status, Status::Error, "batch must fail");

    // k_a must be restored to "a_orig".
    let r = send_raw(&mut core, &mut tx, &mut rx, kv_get(b"k_a"));
    assert_eq!(r.status, Status::Ok);
    assert_eq!(&*r.payload, b"a_orig", "k_a must be rolled back");

    // k_b must not persist.
    let r = send_raw(&mut core, &mut tx, &mut rx, kv_get(b"k_b"));
    let is_absent = r.status == Status::Error || r.payload.is_empty();
    assert!(is_absent, "k_b must not persist after rollback");
}

// ---------------------------------------------------------------------------
// Verify: doc get after the batch returns the pre-batch state
// ---------------------------------------------------------------------------

#[test]
fn rollback_matrix_doc_then_doc_conflict_kv_intact() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    // Pre-condition: "anchor" = "anchor_val".
    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        kv_put(b"anchor", b"anchor_val"),
    );
    send_ok(&mut core, &mut tx, &mut rx, doc_put_conflict_seed("docs"));

    // Batch: KV write + doc conflict — the KV write happened, must roll back.
    let resp = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Meta(MetaOp::TransactionBatch {
            plans: vec![
                kv_put(b"anchor", b"anchor_modified"),
                doc_insert_conflict("docs"),
            ],
        }),
    );
    assert_eq!(resp.status, Status::Error);

    // "anchor" must still be "anchor_val".
    let r = send_raw(&mut core, &mut tx, &mut rx, kv_get(b"anchor"));
    assert_eq!(r.status, Status::Ok);
    assert_eq!(&*r.payload, b"anchor_val");

    // The seeded conflict doc must still be readable (it was not part of the batch).
    let r = send_raw(&mut core, &mut tx, &mut rx, doc_get("docs", "conflict_doc"));
    assert_eq!(r.status, Status::Ok, "conflict_doc must still exist");
}

// ---------------------------------------------------------------------------
// Pair: Columnar insert (first) × Document conflict (second) — columnar rolled back
//
// A row is inserted into a plain columnar collection inside a TransactionBatch.
// The second plan is a PointInsert that conflicts (doc already exists).
// After rollback, the columnar collection must be empty.
// ---------------------------------------------------------------------------

#[test]
fn rollback_matrix_columnar_then_doc_fail() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    // Seed the document that will trigger the conflict.
    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        doc_put_conflict_seed("conflict_coll"),
    );

    // Confirm columnar collection is empty before the batch.
    let before = core.scan_collection(1, "metrics", 100).unwrap();
    assert!(before.is_empty(), "columnar must be empty before batch");

    // Build a columnar insert payload: one row.
    let rows = serde_json::json!([{"id": "r1", "val": 42}]);
    let payload = nodedb_types::json_to_msgpack(&rows).unwrap();

    // TransactionBatch: columnar insert + failing doc insert.
    let resp = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Meta(MetaOp::TransactionBatch {
            plans: vec![
                PhysicalPlan::Columnar(ColumnarOp::Insert {
                    collection: "metrics".into(),
                    payload,
                    format: "msgpack".into(),
                    intent: ColumnarInsertIntent::Insert,
                    on_conflict_updates: Vec::new(),
                    surrogates: Vec::new(),
                }),
                doc_insert_conflict("conflict_coll"),
            ],
        }),
    );
    assert_eq!(
        resp.status,
        Status::Error,
        "batch must fail on doc conflict"
    );
    assert!(
        !matches!(
            resp.error_code,
            Some(nodedb::bridge::envelope::ErrorCode::RollbackFailed { .. })
        ),
        "rollback itself must succeed; got {:?}",
        resp.error_code
    );

    // Columnar collection must be empty — rollback_memtable_inserts was called.
    let after = core.scan_collection(1, "metrics", 100).unwrap();
    assert!(
        after.is_empty(),
        "columnar insert must be rolled back; found {} rows after rollback",
        after.len()
    );
}

// ---------------------------------------------------------------------------
// Pair: Columnar insert (first) × Columnar insert — verify aggregate count
//
// Inserts one row outside the batch (baseline), then in a failing batch inserts
// another row + conflicts. After rollback the aggregate count must be 1.
// ---------------------------------------------------------------------------

#[test]
fn rollback_matrix_columnar_count_after_rollback() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    // Seed conflict doc.
    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        doc_put_conflict_seed("conflict_coll"),
    );

    // Baseline: insert one row outside any batch (committed).
    let baseline = serde_json::json!([{"id": "baseline", "val": 1}]);
    let baseline_payload = nodedb_types::json_to_msgpack(&baseline).unwrap();
    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Columnar(ColumnarOp::Insert {
            collection: "metrics2".into(),
            payload: baseline_payload,
            format: "msgpack".into(),
            intent: ColumnarInsertIntent::Insert,
            on_conflict_updates: Vec::new(),
            surrogates: Vec::new(),
        }),
    );

    // Failed batch: inserts a second row + doc conflict.
    let extra = serde_json::json!([{"id": "rolled_back", "val": 2}]);
    let extra_payload = nodedb_types::json_to_msgpack(&extra).unwrap();
    let resp = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Meta(MetaOp::TransactionBatch {
            plans: vec![
                PhysicalPlan::Columnar(ColumnarOp::Insert {
                    collection: "metrics2".into(),
                    payload: extra_payload,
                    format: "msgpack".into(),
                    intent: ColumnarInsertIntent::Insert,
                    on_conflict_updates: Vec::new(),
                    surrogates: Vec::new(),
                }),
                doc_insert_conflict("conflict_coll"),
            ],
        }),
    );
    assert_eq!(resp.status, Status::Error, "batch must fail");

    // Aggregate count must be 1 (only the baseline row).
    let agg_payload = send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Query(QueryOp::Aggregate {
            collection: "metrics2".into(),
            group_by: Vec::new(),
            aggregates: vec![AggregateSpec {
                function: "count".into(),
                alias: "count(*)".into(),
                user_alias: None,
                field: "*".into(),
                expr: None,
            }],
            filters: Vec::new(),
            having: Vec::new(),
            limit: 10,
            sub_group_by: Vec::new(),
            sub_aggregates: Vec::new(),
            grouping_sets: Vec::new(),
            sort_keys: Vec::new(),
        }),
    );
    let json = crate::helpers::payload_json(&agg_payload);
    let val: serde_json::Value = serde_json::from_str(&json).unwrap();
    let count = val
        .as_array()
        .and_then(|a| a.first())
        .and_then(|row| row.get("count(*)"))
        .and_then(|v| v.as_i64())
        .unwrap_or(-1);
    assert_eq!(
        count, 1,
        "columnar row count after rollback must be 1 (baseline only); got count={count} json={json}"
    );
}

// ---------------------------------------------------------------------------
// Pair: Timeseries ingest (first) × Document conflict (second) — ts rolled back
//
// A timeseries ingest inside a TransactionBatch followed by a failing doc insert
// must leave the timeseries memtable empty (truncated back by apply_undo_timeseries).
// ---------------------------------------------------------------------------

#[test]
fn rollback_matrix_timeseries_then_doc_fail() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    // Seed conflict doc.
    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        doc_put_conflict_seed("conflict_coll"),
    );

    // TransactionBatch: timeseries ingest (3 rows) + doc conflict (fails).
    let ilp = "cpu,host=s1 value=0.5 1000000000\n\
               cpu,host=s1 value=0.6 2000000000\n\
               cpu,host=s1 value=0.7 3000000000\n";
    let resp = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Meta(MetaOp::TransactionBatch {
            plans: vec![
                PhysicalPlan::Timeseries(TimeseriesOp::Ingest {
                    collection: "cpu".into(),
                    payload: ilp.as_bytes().to_vec(),
                    format: "ilp".into(),
                    wal_lsn: None,
                    surrogates: Vec::new(),
                }),
                doc_insert_conflict("conflict_coll"),
            ],
        }),
    );
    assert_eq!(
        resp.status,
        Status::Error,
        "batch must fail on doc conflict"
    );
    assert!(
        !matches!(
            resp.error_code,
            Some(nodedb::bridge::envelope::ErrorCode::RollbackFailed { .. })
        ),
        "rollback itself must succeed; got {:?}",
        resp.error_code
    );

    // Scan the timeseries — must return zero rows (memtable was truncated to 0).
    let scan_resp = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Timeseries(TimeseriesOp::Scan {
            collection: "cpu".into(),
            time_range: (0, i64::MAX),
            projection: Vec::new(),
            limit: 100,
            filters: Vec::new(),
            bucket_interval_ms: 0,
            group_by: Vec::new(),
            aggregates: Vec::new(),
            gap_fill: String::new(),
            rls_filters: Vec::new(),
            system_as_of_ms: None,
            valid_at_ms: None,
            computed_columns: Vec::new(),
        }),
    );
    assert_eq!(scan_resp.status, Status::Ok);
    let json = crate::helpers::payload_json(&scan_resp.payload);
    let val: serde_json::Value =
        serde_json::from_str(&json).unwrap_or(serde_json::Value::Array(vec![]));
    let empty = vec![];
    let arr = val.as_array().unwrap_or(&empty);
    assert!(
        arr.is_empty(),
        "timeseries rows must be rolled back (truncate_to called); got {} rows: {json}",
        arr.len()
    );
}

// ---------------------------------------------------------------------------
// Pair: Timeseries ingest (first) — baseline + batch — count after rollback
//
// Ingests rows outside any batch (committed), then a failing batch ingests more.
// After rollback the scan must return only the committed rows.
// ---------------------------------------------------------------------------

#[test]
fn rollback_matrix_timeseries_count_after_rollback() {
    let (mut core, mut tx, mut rx, _dir) = make_core();

    // Seed conflict doc.
    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        doc_put_conflict_seed("conflict_coll"),
    );

    // Baseline: commit 2 rows.
    let baseline_ilp = "temp,host=s1 value=1.0 1000000000\n\
                        temp,host=s1 value=2.0 2000000000\n";
    send_ok(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Timeseries(TimeseriesOp::Ingest {
            collection: "temp".into(),
            payload: baseline_ilp.as_bytes().to_vec(),
            format: "ilp".into(),
            wal_lsn: None,
            surrogates: Vec::new(),
        }),
    );

    // Failed batch: ingest 3 more rows + doc conflict.
    let extra_ilp = "temp,host=s1 value=3.0 3000000000\n\
                     temp,host=s1 value=4.0 4000000000\n\
                     temp,host=s1 value=5.0 5000000000\n";
    let resp = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Meta(MetaOp::TransactionBatch {
            plans: vec![
                PhysicalPlan::Timeseries(TimeseriesOp::Ingest {
                    collection: "temp".into(),
                    payload: extra_ilp.as_bytes().to_vec(),
                    format: "ilp".into(),
                    wal_lsn: None,
                    surrogates: Vec::new(),
                }),
                doc_insert_conflict("conflict_coll"),
            ],
        }),
    );
    assert_eq!(resp.status, Status::Error, "batch must fail");

    // Scan must return exactly 2 rows (the baseline only).
    let scan_resp = send_raw(
        &mut core,
        &mut tx,
        &mut rx,
        PhysicalPlan::Timeseries(TimeseriesOp::Scan {
            collection: "temp".into(),
            time_range: (0, i64::MAX),
            projection: Vec::new(),
            limit: 100,
            filters: Vec::new(),
            bucket_interval_ms: 0,
            group_by: Vec::new(),
            aggregates: Vec::new(),
            gap_fill: String::new(),
            rls_filters: Vec::new(),
            system_as_of_ms: None,
            valid_at_ms: None,
            computed_columns: Vec::new(),
        }),
    );
    assert_eq!(scan_resp.status, Status::Ok);
    let json = crate::helpers::payload_json(&scan_resp.payload);
    let val: serde_json::Value =
        serde_json::from_str(&json).unwrap_or(serde_json::Value::Array(vec![]));
    let empty = vec![];
    let arr = val.as_array().unwrap_or(&empty);
    assert_eq!(
        arr.len(),
        2,
        "timeseries must have only baseline 2 rows after rollback; got {} rows: {json}",
        arr.len()
    );
}