reddb-io-server 1.24.0

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
use super::*;
use std::sync::mpsc;
use std::time::Duration;

fn row(id: i64) -> MutationRow {
    MutationRow {
        fields: vec![("record_key".to_string(), Value::Integer(id))],
        metadata: Vec::new(),
        node_links: Vec::new(),
        vector_links: Vec::new(),
    }
}

#[test]
fn admitted_rows_keep_their_index_topology_until_maintenance_finishes() {
    for batch_size in [1, 3] {
        for operation in [
            "create",
            "drop",
            "rebuild",
            "tenant",
            "tenant_drop",
            "rehydrate",
            "drop_table",
        ] {
            let runtime = crate::RedDBRuntime::in_memory().expect("runtime");
            runtime
                .execute_query("CREATE TABLE topology_records (record_key INT, tenant_key INT)")
                .expect("table");
            if operation != "create" {
                runtime
                    .execute_query(
                        "CREATE INDEX record_lookup ON topology_records (record_key) USING HASH",
                    )
                    .expect("existing index");
            }
            if operation == "tenant_drop" {
                runtime.register_tenant_table("topology_records", "tenant_key");
            }
            if operation == "rehydrate" {
                // Retain the persisted descriptor, as at startup before replay.
                runtime
                    .index_store_ref()
                    .drop_index("record_lookup", "topology_records");
            }
            let (admitted_send, admitted_receive) = mpsc::channel();
            let (release_send, release_receive) = mpsc::channel();
            let release_receive = std::sync::Mutex::new(release_receive);
            let hook = || {
                admitted_send.send(()).expect("announce admission");
                release_receive
                    .lock()
                    .expect("release receiver")
                    .recv_timeout(Duration::from_secs(10))
                    .expect("release admitted writer");
            };
            let completed_early = std::thread::scope(|scope| {
                let writer = scope.spawn(|| {
                    let mut engine = MutationEngine::new(&runtime).with_suppress_events();
                    engine.after_admission = Some(&hook);
                    engine.apply(
                        "topology_records".to_string(),
                        (0..batch_size).map(row).collect(),
                    )
                });
                admitted_receive
                    .recv_timeout(Duration::from_secs(10))
                    .expect("writer admitted");
                let (started_send, started_receive) = mpsc::channel();
                let (done_send, done_receive) = mpsc::channel();
                let runtime = &runtime;
                let ddl = scope.spawn(move || {
                    started_send.send(()).expect("DDL started");
                    let result = match operation {
                        "create" => runtime.execute_query(
                            "CREATE INDEX record_lookup ON topology_records (record_key) USING HASH"
                        ).map(|_| ()),
                        "drop" => runtime.execute_query(
                            "DROP INDEX record_lookup ON topology_records"
                        ).map(|_| ()),
                        "rebuild" => runtime.rebuild_runtime_indexes_for_table("topology_records"),
                        "tenant" => {
                            runtime.register_tenant_table("topology_records", "tenant_key");
                            Ok(())
                        }
                        "tenant_drop" => {
                            runtime.unregister_tenant_table("topology_records");
                            Ok(())
                        }
                        "rehydrate" => runtime.rehydrate_runtime_index_registry(),
                        "drop_table" => runtime.execute_query("DROP TABLE topology_records").map(|_| ()),
                        _ => unreachable!(),
                    };
                    done_send.send(()).expect("DDL completed");
                    result
                });
                started_receive
                    .recv_timeout(Duration::from_secs(10))
                    .expect("DDL attempted");
                let completed_early = done_receive
                    .recv_timeout(Duration::from_millis(100))
                    .is_ok();
                release_send.send(()).expect("resume writer");
                writer
                    .join()
                    .expect("writer thread")
                    .expect("insert succeeds");
                ddl.join().expect("DDL thread").expect("DDL succeeds");
                completed_early
            });
            assert!(!completed_early,
                "{operation} changed the admitted index set before a {batch_size}-row write finished");
            if matches!(operation, "create" | "rebuild" | "rehydrate") {
                for id in 0..batch_size {
                    assert_eq!(
                        runtime
                            .index_store_ref()
                            .hash_lookup("topology_records", "record_lookup", &id.to_le_bytes())
                            .expect("lookup")
                            .len(),
                        1,
                        "backfill includes every admitted row once"
                    );
                }
            }
        }
    }
}

#[test]
fn index_backfill_keeps_rows_stable_until_registration() {
    for batch_size in [1, 3] {
        let runtime = crate::RedDBRuntime::in_memory().expect("runtime");
        runtime
            .execute_query("CREATE TABLE topology_records (record_key INT)")
            .expect("table");
        let (snapshot_send, snapshot_receive) = mpsc::channel();
        let (release_send, release_receive) = mpsc::channel();
        let release_receive = std::sync::Mutex::new(release_receive);
        *runtime.index_store_ref().before_build.lock() = Some(Arc::new(move || {
            snapshot_send.send(()).expect("snapshot collected");
            release_receive
                .lock()
                .expect("release receiver")
                .recv_timeout(Duration::from_secs(10))
                .expect("release index builder");
        }));
        let published_early = std::thread::scope(|scope| {
            let ddl = scope.spawn(|| {
                runtime.execute_query(
                    "CREATE INDEX record_lookup ON topology_records (record_key) USING HASH",
                )
            });
            snapshot_receive
                .recv_timeout(Duration::from_secs(10))
                .expect("snapshot taken");
            let (started_send, started_receive) = mpsc::channel();
            let (done_send, done_receive) = mpsc::channel();
            let runtime = &runtime;
            let writer = scope.spawn(move || {
                started_send.send(()).expect("writer started");
                let result = MutationEngine::new(runtime).with_suppress_events().apply(
                    "topology_records".to_string(),
                    (0..batch_size).map(row).collect(),
                );
                done_send.send(()).expect("writer completed");
                result
            });
            started_receive
                .recv_timeout(Duration::from_secs(10))
                .expect("writer attempted");
            let published_early = done_receive
                .recv_timeout(Duration::from_millis(100))
                .is_ok();
            release_send.send(()).expect("resume backfill");
            ddl.join().expect("DDL thread").expect("create index");
            writer.join().expect("writer thread").expect("insert");
            published_early
        });
        // This content check also detects the old missed-backfill bug if the
        // scheduler did not run the writer during the bounded blocking probe.
        for id in 0..batch_size {
            assert_eq!(
                runtime
                    .index_store_ref()
                    .hash_lookup("topology_records", "record_lookup", &id.to_le_bytes())
                    .expect("lookup")
                    .len(),
                1,
                "no row may escape snapshot and maintenance"
            );
        }
        assert!(
            !published_early,
            "row publication waits for the index snapshot to be installed"
        );
    }
}

#[test]
fn admitted_writer_allows_other_row_writers_and_unrelated_ddl() {
    let runtime = crate::RedDBRuntime::in_memory().expect("runtime");
    runtime
        .execute_query("CREATE TABLE topology_records (record_key INT)")
        .expect("table");
    runtime
        .execute_query("CREATE TABLE other_records (record_key INT)")
        .expect("other table");
    let (admitted_send, admitted_receive) = mpsc::channel();
    let (release_send, release_receive) = mpsc::channel();
    let release_receive = std::sync::Mutex::new(release_receive);
    let hook = || {
        admitted_send.send(()).expect("admitted");
        release_receive
            .lock()
            .expect("release receiver")
            .recv_timeout(Duration::from_secs(10))
            .expect("resume writer");
    };
    std::thread::scope(|scope| {
        let first = scope.spawn(|| {
            let mut engine = MutationEngine::new(&runtime).with_suppress_events();
            engine.after_admission = Some(&hook);
            engine.apply("topology_records".to_string(), vec![row(1)])
        });
        admitted_receive
            .recv_timeout(Duration::from_secs(10))
            .expect("writer paused");
        // Both operations must complete while the first writer remains paused.
        MutationEngine::new(&runtime)
            .with_suppress_events()
            .apply("topology_records".to_string(), vec![row(2)])
            .expect("parallel row writer");
        runtime
            .execute_query("CREATE INDEX other_lookup ON other_records (record_key) USING HASH")
            .expect("independent collection DDL");
        release_send.send(()).expect("release first writer");
        first.join().expect("first thread").expect("first insert");
    });
    assert_eq!(
        runtime
            .db()
            .store()
            .get_collection("topology_records")
            .expect("collection")
            .query_all(|_| true)
            .len(),
        2
    );
}

#[test]
fn concurrent_first_id_inserts_share_one_complete_automatic_index() {
    for batch_size in [1, 3] {
        let runtime = crate::RedDBRuntime::in_memory().expect("runtime");
        runtime
            .execute_query("CREATE TABLE topology_records (id INT)")
            .expect("table");
        let start = std::sync::Barrier::new(8);
        std::thread::scope(|scope| {
            for worker in 0..8 {
                let runtime = &runtime;
                let start = &start;
                scope.spawn(move || {
                    start.wait();
                    let rows = (0..batch_size)
                        .map(|offset| {
                            let mut row = row(worker * batch_size + offset);
                            row.fields[0].0 = "id".to_string();
                            row
                        })
                        .collect();
                    MutationEngine::new(runtime)
                        .with_suppress_events()
                        .apply("topology_records".to_string(), rows)
                        .expect("concurrent first insert");
                });
            }
        });
        assert_eq!(
            runtime
                .index_store_ref()
                .list_indices("topology_records")
                .len(),
            1
        );
        for id in 0..8 * batch_size {
            assert_eq!(
                runtime
                    .index_store_ref()
                    .hash_lookup("topology_records", "idx_id", &id.to_le_bytes())
                    .expect("automatic index")
                    .len(),
                1
            );
        }
    }
}

#[test]
fn denied_admission_releases_topology_for_ddl_and_retry() {
    let runtime = crate::RedDBRuntime::with_options(
        crate::RedDBOptions::in_memory().with_memory_budget(128 * 1024),
    )
    .expect("runtime");
    runtime
        .execute_query("CREATE TABLE topology_records (id INT)")
        .expect("table");
    runtime.refresh_memory_accounting();
    let remaining =
        runtime.memory_budget().resolved_bytes - runtime.memory_accounting().total_used_bytes();
    let reservation = runtime
        .admit_non_evictable_growth(
            crate::storage::memory_pools::MemoryPool::SegmentArena,
            "test pressure",
            remaining,
        )
        .expect("reserve headroom");
    assert!(runtime
        .execute_query("INSERT INTO topology_records (id) VALUES (1)")
        .is_err());
    let lock = runtime
        .index_store_ref()
        .collection_topology_lock("topology_records");
    assert!(
        lock.try_write().is_some(),
        "error releases the exclusive first-insert guard"
    );
    assert!(runtime
        .index_store_ref()
        .list_indices("topology_records")
        .is_empty());
    drop(reservation);
    runtime
        .execute_query("CREATE INDEX record_lookup ON topology_records (id) USING HASH")
        .expect("DDL after rejected admission");
    runtime
        .execute_query("INSERT INTO topology_records (id) VALUES (1)")
        .expect("retry");
}

#[test]
fn newly_published_unique_index_serializes_waiting_writers_before_storage() {
    use std::sync::atomic::{AtomicUsize, Ordering};
    let runtime = crate::RedDBRuntime::in_memory().expect("runtime");
    runtime
        .execute_query("CREATE TABLE topology_records (record_key INT)")
        .expect("table");
    let (snapshot_send, snapshot_receive) = mpsc::channel();
    let (build_release_send, build_release_receive) = mpsc::channel();
    let build_release_receive = std::sync::Mutex::new(build_release_receive);
    *runtime.index_store_ref().before_build.lock() = Some(Arc::new(move || {
        snapshot_send.send(()).expect("builder paused");
        build_release_receive
            .lock()
            .expect("build receiver")
            .recv_timeout(Duration::from_secs(10))
            .expect("resume builder");
    }));
    let admitted = AtomicUsize::new(0);
    let (admitted_send, admitted_receive) = mpsc::channel();
    let release = (std::sync::Mutex::new(false), std::sync::Condvar::new());
    let hook = || {
        admitted.fetch_add(1, Ordering::SeqCst);
        admitted_send.send(()).expect("writer admitted");
        let (_release_guard, timeout) = release
            .1
            .wait_timeout_while(
                release.0.lock().expect("release flag"),
                Duration::from_secs(10),
                |released| !*released,
            )
            .expect("wait for release");
        assert!(!timeout.timed_out(), "resume admitted writer");
    };
    let successes = std::thread::scope(|scope| {
        let ddl = scope.spawn(|| {
            runtime.execute_query(
                "CREATE UNIQUE INDEX record_lookup ON topology_records (record_key) USING HASH",
            )
        });
        snapshot_receive
            .recv_timeout(Duration::from_secs(10))
            .expect("unique snapshot taken");
        let (started_send, started_receive) = mpsc::channel();
        let mut writers = Vec::new();
        for _ in 0..8 {
            let runtime = &runtime;
            let hook = &hook;
            let started_send = started_send.clone();
            writers.push(scope.spawn(move || {
                started_send.send(()).expect("writer started");
                let mut engine = MutationEngine::new(runtime).with_suppress_events();
                engine.after_admission = Some(hook);
                engine.apply("topology_records".to_string(), vec![row(42)])
            }));
        }
        for _ in 0..8 {
            started_receive
                .recv_timeout(Duration::from_secs(10))
                .expect("writers attempting");
        }
        // None may reach admission while the unique descriptor is unpublished.
        let admitted_before_publication = admitted_receive
            .recv_timeout(Duration::from_millis(100))
            .is_ok();
        build_release_send.send(()).expect("publish unique index");
        ddl.join().expect("DDL thread").expect("unique index built");
        if !admitted_before_publication {
            admitted_receive
                .recv_timeout(Duration::from_secs(10))
                .expect("first admitted writer");
        }
        let second_admitted = admitted_receive
            .recv_timeout(Duration::from_millis(100))
            .is_ok();
        *release.0.lock().expect("release flag") = true;
        release.1.notify_all();
        let successes = writers
            .into_iter()
            .map(|writer| writer.join().expect("writer thread"))
            .filter(Result::is_ok)
            .count();
        assert!(
            !admitted_before_publication,
            "writers wait for unique publication"
        );
        assert!(
            !second_admitted,
            "only one equal key reaches admission before storage"
        );
        successes
    });
    assert_eq!(successes, 1, "only one writer wins the unique key");
    assert_eq!(admitted.load(Ordering::SeqCst), 1);
    assert_eq!(
        runtime
            .db()
            .store()
            .get_collection("topology_records")
            .expect("collection")
            .query_all(|_| true)
            .len(),
        1,
        "losing writers must fail before installing rows"
    );
    assert_eq!(
        runtime
            .index_store_ref()
            .hash_lookup("topology_records", "record_lookup", &42i64.to_le_bytes())
            .expect("unique index lookup")
            .len(),
        1
    );
}