nodedb 0.4.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
// SPDX-License-Identifier: BUSL-1.1

//! WAL replay for columnar predicate DML (`ColumnarOp::Update` /
//! `ColumnarOp::Delete`, autocommit path).
//!
//! `wal_append_if_write` appends a [`nodedb_types::columnar::ColumnarDmlWalRecord`]
//! BEFORE dispatch (see `control::server::wal_dispatch::core`), carrying the
//! predicate — collection, filters, and (for `Update`) field assignments —
//! rather than a row post-image, because the matching set is only known once
//! the Data Plane scans current state. Replay re-executes the exact predicate
//! through the exact same live handler (`execute_columnar_update` /
//! `execute_columnar_delete`) that ran on first application, so replay and
//! live semantics cannot diverge.
//!
//! ## Idempotence constraint, and the floor that satisfies it
//!
//! `Delete` (tombstone bit + PK-index removal) is idempotent. `Update` is
//! **not**: it is implemented as delete-old-PK + insert-new-row, so a second
//! application appends a duplicate row. Correctness rests entirely on this
//! function being invoked exactly once per record, in ascending LSN order,
//! against engine state that does not already contain it.
//!
//! That used to be guaranteed by the state being rebuilt from scratch on every
//! restart: `ColumnarOp::Update` / `ColumnarOp::Delete` target only the
//! plain-columnar and spatial profiles (`sql_plan_convert/dml/update_delete`
//! routes `EngineType::Columnar | EngineType::Spatial` here and nothing else),
//! and those profiles live in `CoreLoop::columnar_engines` /
//! `columnar_flushed_segments` — an in-memory `MutationEngine` plus in-memory
//! flushed segment bytes with no store behind them. Replay always started from
//! empty, so there was no durable partial state to double-apply against.
//!
//! `columnar_checkpoint` ends that. Restoring a generation means replay now
//! starts from state that already contains every record at or below the
//! manifest's LSN, which is exactly the precondition this function's
//! non-idempotence depends on. [`ReplayFloors::columnar`] carries that LSN and
//! gates those records out; everything above it is absent from the restored
//! state and must still replay. Without the gate the checkpoint would duplicate
//! rows; without the replay above it the checkpoint would lose them.
//!
//! Note the gate is NOT the `last_flushed_wal_lsn` watermark of the timeseries
//! profile: that field exists only on `nodedb_types::timeseries::PartitionMeta`,
//! used by the separate `ts_registries` / bucketed-partition machinery, which
//! this op pair never targets.

use tracing::warn;

use super::core_loop::CoreLoop;
use crate::bridge::envelope::{PhysicalPlan, Status};
use crate::types::{DatabaseId, Lsn, TenantId, VShardId};
use nodedb_physical::physical_plan::ColumnarOp;
use nodedb_types::columnar::ColumnarDmlWalRecord;

impl CoreLoop {
    /// Try to decode `payload` as a `columnar_dml` predicate-DML record and,
    /// if it is one, replay it. Returns `None` when the payload does not
    /// decode as this shape (caller falls back to `decode_batch_record`'s
    /// row-payload / legacy-tuple attempts), `Some(0)` when it decoded but was
    /// tombstoned, already covered by the restored columnar checkpoint, or the
    /// live re-execution reported a typed error (logged, never panics),
    /// `Some(1)` on successful replay.
    pub(in crate::data::executor) fn try_replay_columnar_predicate_dml(
        &mut self,
        payload: &[u8],
        tenant_id: u64,
        database_id: DatabaseId,
        record_lsn: u64,
        tombstones: &nodedb_wal::TombstoneSet,
    ) -> Option<usize> {
        let record: ColumnarDmlWalRecord = zerompk::from_msgpack(payload).ok()?;
        if record.kind != "columnar_dml" {
            return None;
        }

        if tombstones.is_tombstoned(
            database_id.as_u64(),
            tenant_id,
            &record.collection,
            record_lsn,
        ) {
            return Some(0);
        }

        // Already folded into the restored checkpoint. Re-executing an `Update`
        // here would append a duplicate row (delete-old-PK + insert-new-row is
        // not idempotent), so the gate precedes the re-execution rather than
        // trying to detect the duplicate afterwards. Returns `Some(0)` and not
        // `None`: the record decoded as this shape, so the caller must not fall
        // through to its row-payload decoders and mis-classify it.
        if self.floors.replay_floors.columnar.covers(record_lsn) {
            return Some(0);
        }

        let tid = TenantId::new(tenant_id);
        let vshard_id = VShardId::from_collection_in_database(database_id, &record.collection);

        // The task carries the real predicate even though today's handlers read
        // only `task.request.{database_id, tenant_id}`. A placeholder plan would
        // silently degrade to a no-op against an empty collection name the day a
        // handler starts reading the plan; on a once-per-record startup path the
        // clone costs nothing worth that risk.
        let plan = if record.is_update {
            PhysicalPlan::Columnar(ColumnarOp::Update {
                collection: record.collection.clone(),
                filters: record.filters.clone(),
                updates: record.updates.clone(),
            })
        } else {
            PhysicalPlan::Columnar(ColumnarOp::Delete {
                collection: record.collection.clone(),
                filters: record.filters.clone(),
            })
        };
        let task = Self::replay_task(
            tid,
            database_id,
            vshard_id,
            plan,
            Some(Lsn::new(record_lsn)),
        );

        // Re-execute via the same live handlers the autocommit dispatch used —
        // `undo_log: None` mirrors the autocommit path (no transaction batch
        // to roll back).
        let response = if record.is_update {
            self.execute_columnar_update(
                &task,
                &record.collection,
                &record.filters,
                &record.updates,
                None,
            )
        } else {
            self.execute_columnar_delete(&task, &record.collection, &record.filters, None)
        };

        if response.status != Status::Ok {
            warn!(
                core = self.core_id,
                collection = %record.collection,
                lsn = record_lsn,
                is_update = record.is_update,
                error = ?response.error_code,
                "columnar predicate DML WAL replay failed; skipping record"
            );
            return Some(0);
        }
        Some(1)
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use crate::bridge::envelope::PhysicalPlan;
    use crate::control::server::wal_dispatch::wal_append_if_write;
    use crate::types::{DatabaseId, TenantId, VShardId};
    use crate::wal::manager::WalManager;
    use nodedb_physical::physical_plan::{ColumnarInsertIntent, ColumnarOp};
    use nodedb_query::scan_filter::{FilterOp, ScanFilter};
    use nodedb_types::Value;
    use nodedb_wal::TombstoneSet;

    use super::CoreLoop;

    const TID: u64 = 1;
    const COLLECTION: &str = "m";

    struct CoreHarness {
        core: CoreLoop,
        _req_tx: nodedb_bridge::buffer::Producer<crate::bridge::dispatch::BridgeRequest>,
        _resp_rx: nodedb_bridge::buffer::Consumer<crate::bridge::dispatch::BridgeResponse>,
        _dir: tempfile::TempDir,
    }

    fn make_core() -> CoreHarness {
        use crate::bridge::dispatch::{BridgeRequest, BridgeResponse};
        use nodedb_bridge::buffer::RingBuffer;

        let dir = tempfile::tempdir().expect("tempdir");
        let (req_tx, req_rx) = RingBuffer::channel::<BridgeRequest>(64);
        let (resp_tx, resp_rx) = RingBuffer::channel::<BridgeResponse>(64);
        let core = CoreLoop::open(
            0,
            req_rx,
            resp_tx,
            dir.path(),
            Arc::new(nodedb_types::OrdinalClock::new()),
        )
        .expect("open core");
        CoreHarness {
            core,
            _req_tx: req_tx,
            _resp_rx: resp_rx,
            _dir: dir,
        }
    }

    fn insert_plan(rows: Vec<Value>) -> PhysicalPlan {
        // Must match what the planner emits (`rows_to_msgpack_array`): a plain
        // msgpack array of row maps. `zerompk::to_msgpack_vec` on a `Value`
        // would emit the tagged `[variant, payload]` enum encoding instead,
        // which the replay path decodes as zero rows.
        let payload = nodedb_types::value_to_msgpack(&Value::Array(rows))
            .expect("encode columnar insert payload");
        PhysicalPlan::Columnar(ColumnarOp::Insert {
            collection: COLLECTION.into(),
            payload,
            format: "msgpack".into(),
            intent: ColumnarInsertIntent::Insert,
            on_conflict_updates: Vec::new(),
            surrogates: Vec::new(),
            schema_bytes: Vec::new(),
            provenance: None,
            wal_lsn: None,
        })
    }

    fn row(id: i64, v: i64) -> Value {
        Value::Object(std::collections::HashMap::from([
            ("id".to_string(), Value::Integer(id)),
            ("v".to_string(), Value::Integer(v)),
        ]))
    }

    fn eq_filter_bytes(field: &str, value: Value) -> Vec<u8> {
        let filters = vec![ScanFilter {
            field: field.to_string(),
            op: FilterOp::Eq,
            value,
            clauses: Vec::new(),
            expr: None,
        }];
        zerompk::to_msgpack_vec(&filters).expect("encode filters")
    }

    fn append_via_autocommit(plans: &[PhysicalPlan]) -> Vec<nodedb_wal::WalRecord> {
        let dir = tempfile::tempdir().expect("wal tempdir");
        let wal = WalManager::open_for_testing(&dir.path().join("wal")).expect("open wal");
        for plan in plans {
            let outcome = wal_append_if_write(
                &wal,
                TenantId::new(TID),
                VShardId::new(0),
                DatabaseId::DEFAULT,
                plan,
            )
            .expect("wal append");
            assert!(
                outcome.lsn.is_some(),
                "columnar predicate DML autocommit writes must produce a durable WAL record"
            );
        }
        wal.sync().expect("wal sync");
        wal.replay().expect("wal replay read")
    }

    fn scan_ids(core: &mut CoreLoop) -> Vec<(i64, i64)> {
        let key = (
            DatabaseId::DEFAULT,
            TenantId::new(TID),
            COLLECTION.to_string(),
        );
        let engine = core
            .columnar_engines
            .get(&key)
            .expect("columnar engine present after replay");
        let schema = engine.schema();
        // Schema-inferred column order comes from `HashMap` iteration and is
        // not guaranteed stable, so look up each field by name rather than by
        // position.
        let id_idx = schema
            .columns
            .iter()
            .position(|c| c.name == "id")
            .expect("schema has 'id' column");
        let v_idx = schema
            .columns
            .iter()
            .position(|c| c.name == "v")
            .expect("schema has 'v' column");
        engine
            .scan_memtable_rows()
            .map(|row| {
                let id = match &row[id_idx] {
                    Value::Integer(n) => *n,
                    other => panic!("expected integer id, got {other:?}"),
                };
                let v = match &row[v_idx] {
                    Value::Integer(n) => *n,
                    other => panic!("expected integer v, got {other:?}"),
                };
                (id, v)
            })
            .collect()
    }

    #[test]
    fn autocommit_delete_produces_durable_lsn() {
        let dir = tempfile::tempdir().expect("wal tempdir");
        let wal = WalManager::open_for_testing(&dir.path().join("wal")).expect("open wal");
        let delete = PhysicalPlan::Columnar(ColumnarOp::Delete {
            collection: COLLECTION.into(),
            filters: eq_filter_bytes("id", Value::Integer(2)),
        });
        let outcome = wal_append_if_write(
            &wal,
            TenantId::new(TID),
            VShardId::new(0),
            DatabaseId::DEFAULT,
            &delete,
        )
        .expect("wal append delete");
        assert!(
            outcome.lsn.is_some(),
            "autocommit columnar predicate DELETE must be durably WAL-appended \
             (pre-fix: ColumnarOp::Delete fell through the catch-all and was never logged)"
        );
    }

    #[test]
    fn autocommit_update_produces_durable_lsn() {
        let dir = tempfile::tempdir().expect("wal tempdir");
        let wal = WalManager::open_for_testing(&dir.path().join("wal")).expect("open wal");
        let update = PhysicalPlan::Columnar(ColumnarOp::Update {
            collection: COLLECTION.into(),
            filters: eq_filter_bytes("id", Value::Integer(1)),
            updates: vec![(
                "v".to_string(),
                // `execute_columnar_update` decodes each update value with the
                // plain reader (`value_from_msgpack`), matching the planner's
                // `sql_value_to_msgpack` output — not the tagged `Value` enum
                // encoding `zerompk::to_msgpack_vec(&Value)` would emit.
                nodedb_types::value_to_msgpack(&Value::Integer(999)).expect("encode"),
            )],
        });
        let outcome = wal_append_if_write(
            &wal,
            TenantId::new(TID),
            VShardId::new(0),
            DatabaseId::DEFAULT,
            &update,
        )
        .expect("wal append update");
        assert!(
            outcome.lsn.is_some(),
            "autocommit columnar predicate UPDATE must be durably WAL-appended \
             (pre-fix: ColumnarOp::Update fell through the catch-all and was never logged)"
        );
    }

    #[test]
    fn deleted_rows_do_not_reappear_after_replay_from_empty() {
        let insert = insert_plan(vec![row(1, 10), row(2, 20), row(3, 30)]);
        let delete = PhysicalPlan::Columnar(ColumnarOp::Delete {
            collection: COLLECTION.into(),
            filters: eq_filter_bytes("id", Value::Integer(2)),
        });

        let records = append_via_autocommit(&[insert, delete]);

        let mut h = make_core();
        h.core
            .replay_timeseries_wal(&records, 1, &TombstoneSet::new());

        let mut rows = scan_ids(&mut h.core);
        rows.sort();
        assert_eq!(
            rows,
            vec![(1, 10), (3, 30)],
            "deleted row (id=2) must NOT reappear after replay from empty \
             (pre-fix: the Delete was never WAL-logged, so replay only re-ran \
             the Insert and the deleted row resurrected)"
        );
    }

    #[test]
    fn updated_row_present_exactly_once_after_replay_from_empty() {
        let insert = insert_plan(vec![row(1, 10), row(2, 20)]);
        let update = PhysicalPlan::Columnar(ColumnarOp::Update {
            collection: COLLECTION.into(),
            filters: eq_filter_bytes("id", Value::Integer(1)),
            // The planner emits raw msgpack primitives here
            // (`sql_value_to_msgpack`), not the tagged `Value` enum encoding.
            updates: vec![(
                "v".to_string(),
                nodedb_types::value_to_msgpack(&Value::Integer(999)).expect("encode"),
            )],
        });

        let records = append_via_autocommit(&[insert, update]);

        let mut h = make_core();
        h.core
            .replay_timeseries_wal(&records, 1, &TombstoneSet::new());

        let mut rows = scan_ids(&mut h.core);
        rows.sort();
        assert_eq!(
            rows,
            vec![(1, 999), (2, 20)],
            "updated row must carry the new value exactly once after replay \
             (pre-fix: the Update was never WAL-logged, so the value reverted; \
             a non-idempotent double-apply would instead duplicate the row)"
        );
    }
}