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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// SPDX-License-Identifier: BUSL-1.1

//! Vector insert/delete handler for sync sessions.
//!
//! Decodes `VectorInsertMsg` / `VectorDeleteMsg` from a Lite client,
//! allocates a surrogate for the document ID via `SurrogateAssigner`,
//! dispatches `VectorOp::Insert` / `VectorOp::DeleteBySurrogate` to the
//! Data Plane, and returns an ACK frame.
//!
//! Structural pattern mirrors `columnar_handler.rs`:
//! a dispatcher trait ties ingest and ACK together so an ACK can never be
//! returned without at least attempting dispatch.

use async_trait::async_trait;

use nodedb_types::Surrogate;

use crate::types::{DatabaseId, TenantId, VShardId};

// ── Dispatcher trait ─────────────────────────────────────────────────────────

/// Parameters for a single vector insert dispatch.
pub struct VectorInsertParams {
    pub collection: String,
    pub vector: Vec<f32>,
    pub dim: usize,
    pub field_name: String,
    pub surrogate: Surrogate,
    /// Raw PK bytes (the sync message's document id) used to bind the
    /// surrogate on follower apply. Always `Some` on this path — the sync
    /// producer always supplies a document id.
    pub pk_bytes: Option<Vec<u8>>,
}

/// Encapsulates async Data Plane dispatch for vector insert/delete.
///
/// Returns the raw `Response.payload` bytes from the Data Plane so that the
/// handler can decode the [`SyncAckResult`] for gate status propagation.
#[async_trait]
pub trait VectorDispatcher: Send + Sync {
    /// Insert a vector into the HNSW index on the Data Plane.
    async fn dispatch_insert(
        &self,
        tenant_id: TenantId,
        vshard: VShardId,
        params: VectorInsertParams,
        provenance: nodedb_types::sync::wire::SyncProvenance,
    ) -> crate::Result<Vec<u8>>;

    /// Delete a vector by surrogate from the HNSW index on the Data Plane.
    async fn dispatch_delete(
        &self,
        tenant_id: TenantId,
        vshard: VShardId,
        collection: String,
        surrogate: Surrogate,
        field_name: String,
        provenance: nodedb_types::sync::wire::SyncProvenance,
    ) -> crate::Result<Vec<u8>>;

    /// Assign a stable surrogate for `(collection, doc_id)`.
    fn assign_surrogate(
        &self,
        database_id: DatabaseId,
        tenant_id: TenantId,
        collection: &str,
        doc_id: &str,
    ) -> crate::Result<Surrogate>;
}

// ── SharedState adapter ──────────────────────────────────────────────────────

/// Production dispatcher: routes vector ops to the Data Plane via the SPSC
/// bridge using `EventSource::CrdtSync` (suppresses AFTER triggers on synced
/// data).
pub struct SharedStateVectorDispatcher<'a> {
    pub shared: &'a crate::control::state::SharedState,
}

#[async_trait]
impl<'a> VectorDispatcher for SharedStateVectorDispatcher<'a> {
    async fn dispatch_insert(
        &self,
        tenant_id: TenantId,
        vshard: VShardId,
        params: VectorInsertParams,
        provenance: nodedb_types::sync::wire::SyncProvenance,
    ) -> crate::Result<Vec<u8>> {
        use crate::bridge::envelope::PhysicalPlan;
        use crate::control::server::wal_dispatch::{VectorPutWalArgs, wal_append_vector_put};
        use nodedb_physical::physical_plan::VectorOp;

        let prov = provenance;

        // Allocate WAL LSN on the Control Plane before dispatching to the
        // Data Plane. Sync path MUST write to WAL; non-sync path already does
        // this via `wal_append_if_write_with_creds` in the main dispatch.
        wal_append_vector_put(
            &self.shared.wal,
            tenant_id,
            vshard,
            DatabaseId::DEFAULT,
            VectorPutWalArgs {
                collection: &params.collection,
                vector: &params.vector,
                dim: params.dim,
                field_name: &params.field_name,
                surrogate: params.surrogate,
                provenance: Some(&prov),
            },
        )?;

        let plan = PhysicalPlan::Vector(VectorOp::Insert {
            collection: params.collection,
            vector: params.vector,
            dim: params.dim,
            field_name: params.field_name,
            surrogate: params.surrogate,
            pk_bytes: params.pk_bytes,
            provenance: Some(prov),
        });

        super::raft_dispatch::dispatch_sync_payload(self.shared, tenant_id, vshard, plan).await
    }

    async fn dispatch_delete(
        &self,
        tenant_id: TenantId,
        vshard: VShardId,
        collection: String,
        surrogate: Surrogate,
        field_name: String,
        provenance: nodedb_types::sync::wire::SyncProvenance,
    ) -> crate::Result<Vec<u8>> {
        use crate::bridge::envelope::PhysicalPlan;
        use crate::control::server::wal_dispatch::{
            VectorDeleteWalArgs, wal_append_vector_delete_by_surrogate,
        };
        use nodedb_physical::physical_plan::VectorOp;

        let prov = provenance;

        // Allocate WAL LSN on the Control Plane before dispatching to the
        // Data Plane.
        wal_append_vector_delete_by_surrogate(
            &self.shared.wal,
            tenant_id,
            vshard,
            DatabaseId::DEFAULT,
            VectorDeleteWalArgs {
                collection: &collection,
                surrogate,
                field_name: &field_name,
                provenance: Some(&prov),
            },
        )?;

        let plan = PhysicalPlan::Vector(VectorOp::DeleteBySurrogate {
            collection,
            surrogate,
            field_name,
            provenance: Some(prov),
        });

        super::raft_dispatch::dispatch_sync_payload(self.shared, tenant_id, vshard, plan).await
    }

    fn assign_surrogate(
        &self,
        database_id: DatabaseId,
        tenant_id: TenantId,
        collection: &str,
        doc_id: &str,
    ) -> crate::Result<Surrogate> {
        self.shared
            .surrogate_assigner
            .assign(database_id, tenant_id, collection, doc_id.as_bytes())
    }
}

// ── NoOp dispatcher (loud failure) ──────────────────────────────────────────

/// Dispatcher used when `SharedState` is unavailable.
pub struct NoOpVectorDispatcher;

#[async_trait]
impl VectorDispatcher for NoOpVectorDispatcher {
    async fn dispatch_insert(
        &self,
        _tenant_id: TenantId,
        _vshard: VShardId,
        _params: VectorInsertParams,
        _provenance: nodedb_types::sync::wire::SyncProvenance,
    ) -> crate::Result<Vec<u8>> {
        Err(super::raft_dispatch::noop_dispatch_error("vector insert"))
    }

    async fn dispatch_delete(
        &self,
        _tenant_id: TenantId,
        _vshard: VShardId,
        _collection: String,
        _surrogate: Surrogate,
        _field_name: String,
        _provenance: nodedb_types::sync::wire::SyncProvenance,
    ) -> crate::Result<Vec<u8>> {
        Err(super::raft_dispatch::noop_dispatch_error("vector delete"))
    }

    fn assign_surrogate(
        &self,
        _database_id: DatabaseId,
        _tenant_id: TenantId,
        _collection: &str,
        _doc_id: &str,
    ) -> crate::Result<Surrogate> {
        Ok(Surrogate::ZERO)
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────
//
// Session handler methods (`handle_vector_insert` / `handle_vector_delete`) live
// in `vector_session.rs`; tests below drive them via the trait + session API.

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

    use super::super::session::SyncSession;
    use super::super::wire::*;
    use super::*;

    type MockCallLog = Arc<Mutex<Vec<(TenantId, String, String)>>>;

    struct MockDispatcher {
        insert_calls: MockCallLog,
        delete_calls: MockCallLog,
        result: crate::Result<()>,
    }

    impl MockDispatcher {
        fn ok() -> (Self, MockCallLog, MockCallLog) {
            let inserts = Arc::new(Mutex::new(Vec::new()));
            let deletes = Arc::new(Mutex::new(Vec::new()));
            (
                Self {
                    insert_calls: inserts.clone(),
                    delete_calls: deletes.clone(),
                    result: Ok(()),
                },
                inserts,
                deletes,
            )
        }

        fn err() -> Self {
            Self {
                insert_calls: Arc::new(Mutex::new(Vec::new())),
                delete_calls: Arc::new(Mutex::new(Vec::new())),
                result: Err(crate::Error::Internal {
                    detail: "mock failure".to_string(),
                }),
            }
        }
    }

    #[async_trait]
    impl VectorDispatcher for MockDispatcher {
        async fn dispatch_insert(
            &self,
            tenant_id: TenantId,
            _vshard: VShardId,
            params: VectorInsertParams,
            provenance: nodedb_types::sync::wire::SyncProvenance,
        ) -> crate::Result<Vec<u8>> {
            let seq = provenance.seq;
            self.insert_calls
                .lock()
                .unwrap()
                .push((tenant_id, params.collection, String::new()));
            super::super::test_support::mock_applied_ack(&self.result, seq)
        }

        async fn dispatch_delete(
            &self,
            tenant_id: TenantId,
            _vshard: VShardId,
            collection: String,
            _surrogate: Surrogate,
            _field_name: String,
            provenance: nodedb_types::sync::wire::SyncProvenance,
        ) -> crate::Result<Vec<u8>> {
            let seq = provenance.seq;
            self.delete_calls
                .lock()
                .unwrap()
                .push((tenant_id, collection, String::new()));
            super::super::test_support::mock_applied_ack(&self.result, seq)
        }

        fn assign_surrogate(
            &self,
            _database_id: DatabaseId,
            _tenant_id: TenantId,
            _collection: &str,
            _doc_id: &str,
        ) -> crate::Result<Surrogate> {
            Ok(Surrogate::ZERO)
        }
    }

    fn make_session() -> SyncSession {
        SyncSession::new("test-vector-session".to_string())
    }

    fn make_insert_msg(collection: &str, id: &str, vector: Vec<f32>) -> VectorInsertMsg {
        let dim = vector.len();
        VectorInsertMsg {
            lite_id: "lite-test".to_string(),
            collection: collection.to_string(),
            id: id.to_string(),
            vector,
            dim,
            field_name: String::new(),
            batch_id: 1,
            producer_id: 0,
            epoch: 0,
            seq: 0,
        }
    }

    fn make_delete_msg(collection: &str, id: &str) -> VectorDeleteMsg {
        VectorDeleteMsg {
            lite_id: "lite-test".to_string(),
            collection: collection.to_string(),
            id: id.to_string(),
            field_name: String::new(),
            batch_id: 2,
            producer_id: 0,
            epoch: 0,
            seq: 0,
        }
    }

    #[tokio::test]
    async fn unauthenticated_insert_returns_rejection() {
        let mut session = make_session();
        let (mock, inserts, _) = MockDispatcher::ok();
        let msg = make_insert_msg("vecs", "v1", vec![1.0, 0.0, 0.0]);

        let frame = session.handle_vector_insert(&msg, &mock).await;
        assert!(frame.is_some());
        let ack: VectorInsertAckMsg = frame.unwrap().decode_body().unwrap();
        assert!(!ack.accepted);
        assert!(inserts.lock().unwrap().is_empty());
    }

    #[tokio::test]
    async fn authenticated_insert_dispatches_and_acks() {
        let mut session = make_session();
        session.authenticated = true;
        let (mock, inserts, _) = MockDispatcher::ok();
        let msg = make_insert_msg("vecs", "v1", vec![1.0, 0.0, 0.0]);

        let frame = session.handle_vector_insert(&msg, &mock).await;
        assert!(frame.is_some());
        let ack: VectorInsertAckMsg = frame.unwrap().decode_body().unwrap();
        assert!(ack.accepted);
        assert_eq!(ack.id, "v1");
        assert_eq!(inserts.lock().unwrap().len(), 1);
    }

    #[tokio::test]
    async fn insert_dimension_mismatch_rejects() {
        let mut session = make_session();
        session.authenticated = true;
        let (mock, _, _) = MockDispatcher::ok();
        let mut msg = make_insert_msg("vecs", "v1", vec![1.0, 0.0, 0.0]);
        msg.dim = 5; // Mismatch: vector.len() == 3, dim == 5

        let frame = session.handle_vector_insert(&msg, &mock).await;
        let ack: VectorInsertAckMsg = frame.unwrap().decode_body().unwrap();
        assert!(!ack.accepted);
        assert!(ack.reject_reason.unwrap().contains("dimension mismatch"));
    }

    #[tokio::test]
    async fn insert_dispatch_failure_rejects() {
        let mut session = make_session();
        session.authenticated = true;
        let mock = MockDispatcher::err();
        let msg = make_insert_msg("vecs", "v1", vec![1.0, 0.0]);

        let frame = session.handle_vector_insert(&msg, &mock).await;
        let ack: VectorInsertAckMsg = frame.unwrap().decode_body().unwrap();
        assert!(!ack.accepted);
        assert!(ack.reject_reason.is_some());
    }

    #[tokio::test]
    async fn authenticated_delete_dispatches_and_acks() {
        let mut session = make_session();
        session.authenticated = true;
        let (mock, _, deletes) = MockDispatcher::ok();
        let msg = make_delete_msg("vecs", "v1");

        let frame = session.handle_vector_delete(&msg, &mock).await;
        let ack: VectorDeleteAckMsg = frame.unwrap().decode_body().unwrap();
        assert!(ack.accepted);
        assert_eq!(ack.id, "v1");
        assert_eq!(deletes.lock().unwrap().len(), 1);
    }

    #[tokio::test]
    async fn unauthenticated_delete_returns_rejection() {
        let mut session = make_session();
        let (mock, _, deletes) = MockDispatcher::ok();
        let msg = make_delete_msg("vecs", "v1");

        let frame = session.handle_vector_delete(&msg, &mock).await;
        let ack: VectorDeleteAckMsg = frame.unwrap().decode_body().unwrap();
        assert!(!ack.accepted);
        assert!(deletes.lock().unwrap().is_empty());
    }
}