reddb-io-server 1.24.1

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
use super::*;
use crate::runtime::execution_context::{CurrentSnapshotGuard, SnapshotContext};
use crate::storage::transaction::snapshot::TxnContext;

impl KvAtomicOps<'_> {
    pub(super) fn record_vault_read(&self, collection: &str, key: Option<&str>) {
        let snapshot = self.runtime.vault_read_snapshot();
        if let Some(reader) = snapshot.serializable_reader {
            snapshot.manager.record_serializable_vault_read(
                reader,
                collection,
                self.tenant.as_deref(),
                key,
            );
        }
    }

    pub(super) fn pending_vault_entries(
        &self,
        collection: &str,
        key: Option<&str>,
    ) -> Vec<VaultEntry> {
        let connection_id = current_connection_id();
        if !self
            .runtime
            .inner
            .transaction_state
            .in_transaction(connection_id)
        {
            return Vec::new();
        }
        self.runtime
            .inner
            .pending_vault_writes
            .read()
            .get(&connection_id)
            .into_iter()
            .flatten()
            .filter(|write| {
                write.collection == collection
                    && write.tenant == self.tenant
                    && key.is_none_or(|key| write.key == key)
            })
            .filter_map(PendingVaultWrite::entry)
            .collect()
    }

    pub(super) fn stage_or_store_vault_version(
        &self,
        collection: &str,
        key: &str,
        mut entity: crate::storage::UnifiedEntity,
        metadata: Metadata,
        base_id: Option<crate::storage::EntityId>,
    ) -> RedDBResult<VaultEntry> {
        let manager = self.runtime.snapshot_manager();
        let transaction_xid = self.runtime.current_xid();
        let writer_xid = transaction_xid.unwrap_or_else(|| manager.begin());
        entity.set_xmin(writer_xid);
        let mut write = PendingVaultWrite {
            collection: collection.into(),
            entity,
            metadata,
            tenant: self.tenant.clone(),
            key: key.into(),
            base_id,
            writer_xid,
            applied: false,
        };
        if transaction_xid.is_some() {
            let mut pending = self.runtime.inner.pending_vault_writes.write();
            let writes = pending.entry(current_connection_id()).or_default();
            if let Some(first) = writes.iter().find(|previous| {
                previous.collection == write.collection
                    && previous.tenant == write.tenant
                    && previous.key == write.key
            }) {
                write.base_id = first.base_id;
            }
            let entry = write.entry().expect("vault writes contain keyed rows");
            writes.push(write);
            return Ok(entry);
        }

        // Publication stays under vault_publication_lock until the WAL batch and
        // MVCC commit both complete. A reader with an earlier snapshot sees
        // the previous version even while these records are being inserted.
        crate::storage::UnifiedStore::begin_deferred_store_wal_capture();
        let stored = self.runtime.store_vault_write(&mut write);
        let actions = crate::storage::UnifiedStore::take_deferred_store_wal_capture();
        let result = stored.and_then(|()| {
            self.runtime
                .inner
                .db
                .store()
                .append_deferred_store_wal_actions(actions)
                .map_err(|error| RedDBError::Internal(error.to_string()))
        });
        if let Err(error) = result {
            manager.rollback(writer_xid);
            self.runtime.remove_applied_vault_writes(&[write])?;
            return Err(error);
        }
        manager.commit(writer_xid);
        self.runtime
            .inner
            .db
            .store()
            .pending_vault_versions
            .write()
            .remove(&write.entity.id);
        self.runtime.invalidate_result_cache_for_table(collection);
        Ok(write
            .entry()
            .expect("stored vault writes contain keyed rows"))
    }
}

impl PendingVaultWrite {
    fn entry(&self) -> Option<VaultEntry> {
        let crate::storage::EntityData::Row(row) = &self.entity.data else {
            return None;
        };
        let version = super::super::keyed_spine::row_version(
            self.entity.id,
            row,
            self.entity.sequence_id as i64,
        )?;
        Some(VaultEntry::from_keyed_row(
            version,
            self.metadata.clone(),
            self.entity.created_at,
            self.entity.updated_at,
            self.entity.sequence_id,
            self.tenant.clone(),
        ))
    }
}

impl RedDBRuntime {
    pub(super) fn vault_read_snapshot(&self) -> SnapshotContext {
        super::super::execution_context::capture_current_snapshot()
            .unwrap_or_else(|| self.vault_write_snapshot())
    }

    pub(super) fn vault_write_snapshot(&self) -> SnapshotContext {
        let context = self
            .inner
            .transaction_state
            .context(current_connection_id());
        let serializable_reader = context.as_ref().and_then(|context| {
            (context.isolation == crate::storage::transaction::IsolationLevel::Serializable)
                .then_some(context.xid)
        });
        SnapshotContext {
            snapshot: self.current_snapshot(),
            manager: self.snapshot_manager(),
            own_xids: self.own_transaction_xids(),
            requires_index_fallback: true,
            serializable_reader,
        }
    }

    pub(crate) fn prepare_pending_vault_writes(
        &self,
        connection_id: u64,
        context: &TxnContext,
    ) -> RedDBResult<()> {
        let writes = self
            .inner
            .pending_vault_writes
            .read()
            .get(&connection_id)
            .cloned()
            .unwrap_or_default();
        if writes.is_empty() {
            return Ok(());
        }
        // TransactionState removes the context before prepare, so this is a
        // fresh committed view without the connection's staged overlay.
        let _snapshot_guard = CurrentSnapshotGuard::install(self.vault_write_snapshot());
        let mut checked = std::collections::HashSet::new();
        let mut write_set = std::collections::HashSet::new();
        let mut vault_write_set = std::collections::HashSet::new();
        for write in &writes {
            vault_write_set.insert((
                write.collection.clone(),
                write.tenant.clone(),
                write.key.clone(),
            ));
            write_set.insert((write.collection.clone(), write.entity.id));
            if let Some(base_id) = write.base_id {
                write_set.insert((write.collection.clone(), base_id));
            }
            if !checked.insert((&write.collection, &write.tenant, &write.key)) {
                continue;
            }
            let operations = KvAtomicOps::for_tenant(self, write.tenant.clone());
            let current = operations.get_vault_entry(&write.collection, &write.key)?;
            if current.as_ref().map(|entry| entry.id) != write.base_id {
                return Err(RedDBError::Query(format!(
                    "serialization conflict: vault key {} was modified concurrently",
                    write.collection,
                )));
            }
        }
        if context.isolation == crate::storage::transaction::IsolationLevel::Serializable
            && (self
                .snapshot_manager()
                .serializable_commit_would_be_dangerous(context.xid, &write_set)
                || self
                    .snapshot_manager()
                    .serializable_vault_commit_would_be_dangerous(context.xid, &vault_write_set))
        {
            return Err(RedDBError::Query(
                "serialization conflict: serializable vault transaction".into(),
            ));
        }

        crate::storage::UnifiedStore::begin_deferred_store_wal_capture();
        let result = (|| {
            for (index, mut write) in writes.into_iter().enumerate() {
                write.entity.set_xmin(context.xid);
                // Track partial application too: an insert can fail after its
                // in-memory update and still needs removing on commit failure.
                self.inner
                    .pending_vault_writes
                    .write()
                    .get_mut(&connection_id)
                    .expect("pending vault write set remains until commit")[index]
                    .applied = true;
                self.store_vault_write(&mut write)?;
            }
            Ok(())
        })();
        let captured = crate::storage::UnifiedStore::take_deferred_store_wal_capture();
        if result.is_ok() {
            self.record_pending_store_wal_actions(connection_id, captured);
        }
        result
    }

    fn store_vault_write(&self, write: &mut PendingVaultWrite) -> RedDBResult<()> {
        let store = self.inner.db.store();
        write.applied = true;
        store
            .insert_vault_version(
                &write.collection,
                write.entity.clone(),
                write.metadata.clone(),
            )
            .map_err(|error| RedDBError::Internal(error.to_string()))?;
        write.entity = store
            .get(&write.collection, write.entity.id)
            .ok_or_else(|| RedDBError::Internal("stored vault version disappeared".into()))?;
        Ok(())
    }

    fn remove_applied_vault_writes(&self, writes: &[PendingVaultWrite]) -> RedDBResult<()> {
        // These live-only rows never entered pager trees or a committed WAL.
        let store = self.inner.db.store();
        for write in writes.iter().filter(|write| write.applied) {
            if let Some(manager) = store.get_collection(&write.collection) {
                manager
                    .delete(write.entity.id)
                    .map_err(|error| RedDBError::Internal(error.to_string()))?;
            }
            store
                .pending_vault_versions
                .write()
                .remove(&write.entity.id);
        }
        Ok(())
    }

    pub(crate) fn discard_pending_vault_writes(&self, connection_id: u64) -> RedDBResult<()> {
        let writes = self
            .inner
            .pending_vault_writes
            .write()
            .remove(&connection_id)
            .unwrap_or_default();
        self.remove_applied_vault_writes(&writes)
    }

    pub(crate) fn finalize_pending_vault_writes(&self, connection_id: u64) {
        let writes = self
            .inner
            .pending_vault_writes
            .write()
            .remove(&connection_id);
        if let Some(writes) = writes {
            for write in writes {
                self.inner
                    .db
                    .store()
                    .pending_vault_versions
                    .write()
                    .remove(&write.entity.id);
                self.invalidate_result_cache_for_table(&write.collection);
            }
        }
    }

    pub(crate) fn rollback_vault_savepoint(&self, connection_id: u64, writer_xid: u64) {
        if let Some(writes) = self
            .inner
            .pending_vault_writes
            .write()
            .get_mut(&connection_id)
        {
            writes.retain(|write| write.writer_xid < writer_xid);
        }
        if let Some(events) = self
            .inner
            .pending_kv_watch_events
            .write()
            .get_mut(&connection_id)
        {
            events.retain(|(xid, _)| *xid < writer_xid);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::auth::{AuthConfig, AuthStore};
    use crate::storage::{DeployProfile, StoragePackaging, StorageProfileSelection};
    use crate::{RedDBOptions, StorageDeployPreset};
    use std::path::Path;
    use std::sync::Arc;

    const CHILD_PATH: &str = "REDDB_VAULT_PREPARE_CRASH_PATH";
    const CHILD_PROFILE: &str = "REDDB_VAULT_PREPARE_CRASH_PROFILE";
    const TEST: &str = "runtime::impl_kv::transactions::tests::checkpoint_during_vault_prepare_cannot_recover_an_uncommitted_batch";

    fn open(path: &Path, operational: bool) -> RedDBRuntime {
        let profile = if operational {
            StorageProfileSelection {
                deploy_profile: DeployProfile::Embedded,
                packaging: StoragePackaging::OperationalDirectory,
                replica_count: 0,
                managed_backup: false,
                wal_retention: false,
            }
        } else {
            StorageDeployPreset::Serverless.selection()
        };
        let options = RedDBOptions::persistent(path)
            .with_storage_profile(profile)
            .unwrap();
        let runtime = RedDBRuntime::with_options(options).unwrap();
        let auth = Arc::new(
            AuthStore::with_vault_certificate(
                AuthConfig::default(),
                runtime.db().store().pager().unwrap().clone(),
                "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
            )
            .unwrap(),
        );
        auth.ensure_vault_secret_key();
        runtime.set_auth_store(auth);
        runtime
    }

    #[test]
    fn checkpoint_during_vault_prepare_cannot_recover_an_uncommitted_batch() {
        if let Some(path) = std::env::var_os(CHILD_PATH) {
            let operational = std::env::var(CHILD_PROFILE).unwrap() == "true";
            let runtime = open(Path::new(&path), operational);
            runtime
                .execute_query("SET SECRET token = 'original'")
                .unwrap();
            runtime.checkpoint().unwrap();
            super::super::super::execution_context::set_current_connection_id(121);
            runtime.execute_query("BEGIN").unwrap();
            runtime
                .execute_query("SET SECRET token = 'pending'")
                .unwrap();
            runtime
                .execute_query("SET SECRET absent = 'pending-new-key'")
                .unwrap();
            let vault_store = runtime.inner.db.store();
            let _publication = vault_store.vault_publication_lock.lock();
            let _: RedDBResult<_> = runtime.inner.transaction_state.commit(121, |context| {
                runtime.prepare_pending_vault_writes(121, context)?;
                // Exercise both an incidental pager flush and a full checkpoint
                // exactly after materialization, before COMMIT's WAL append.
                runtime.inner.db.store().pager().unwrap().flush().unwrap();
                runtime.checkpoint()?;
                if !operational {
                    runtime.publish_serverless_generation()?;
                }
                std::process::exit(0);
            });
            panic!("crash injection did not exit");
        }
        let directory = tempfile::tempdir().unwrap();
        for operational in [false, true] {
            let path = directory.path().join(format!("prepare-{operational}.rdb"));
            let output = std::process::Command::new(std::env::current_exe().unwrap())
                .args(["--exact", TEST, "--nocapture"])
                .env(CHILD_PATH, &path)
                .env(CHILD_PROFILE, operational.to_string())
                .output()
                .unwrap();
            assert!(
                output.status.success(),
                "child: {}",
                String::from_utf8_lossy(&output.stderr)
            );
            let runtime = open(&path, operational);
            if !operational {
                let ranges = runtime
                    .hydrate_current_serverless_collection("red.vault")
                    .unwrap()
                    .unwrap();
                assert_eq!(ranges.len(), 1);
                let snapshot = crate::storage::UnifiedStore::load_from_bytes_with_config(
                    &ranges[0].payload,
                    crate::storage::UnifiedStoreConfig::default(),
                )
                .unwrap();
                assert_eq!(snapshot.get_collection("red.vault").unwrap().count(), 1);
            }
            let revealed = runtime
                .execute_query("VAULT REVEAL red.vault.token")
                .unwrap();
            assert_eq!(
                revealed.result.records[0].get("value"),
                Some(&Value::text("original"))
            );
            assert!(runtime
                .execute_query("VAULT REVEAL red.vault.absent")
                .is_err());
            assert_eq!(
                runtime
                    .execute_query("VAULT HISTORY red.vault.token")
                    .unwrap()
                    .result
                    .len(),
                1
            );
        }
    }
}