reddb-io-server 1.28.28

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
//! Combine independent row publications before entering the checkpoint fence.
//!
//! N concurrent publications cost one durable WAL batch instead of N fsyncs.
//! No staged mutation outlives the publication lock, including on failure.

use super::commit::{StoreWalAction, StoreWalCaptureGuard};
use super::*;
use std::collections::VecDeque;

#[derive(Default)]
pub(crate) struct RowPublication {
    pub inserts: Vec<(String, UnifiedEntity, Option<Metadata>)>,
    pub deletes: Vec<(String, EntityId)>,
}

struct Request {
    rows: RowPublication,
    bytes: u64,
    result: Arc<parking_lot::Mutex<Option<Result<(), String>>>>,
}

#[derive(Default)]
struct QueueState {
    active: bool,
    requests: VecDeque<Request>,
}

#[derive(Default)]
pub(super) struct RowPublicationQueue {
    state: parking_lot::Mutex<QueueState>,
    changed: parking_lot::Condvar,
}

impl UnifiedStore {
    pub(crate) fn publish_rows(&self, rows: RowPublication) -> Result<(), StoreError> {
        assert!(!Self::deferred_store_wal_capture_active());
        // A reentrant caller must not wait for another thread to take its lock.
        if self.commit.is_none()
            || self.config.durability_mode != DurabilityMode::WalDurableGrouped
            || self.vault_publication_lock.is_owned_by_current_thread()
        {
            return self.publish_row_group(vec![rows]);
        }
        let bytes = rows
            .inserts
            .iter()
            .fold(0u64, |sum, (collection, entity, metadata)| {
                sum.saturating_add(
                    StoreWalAction::upsert_entity(
                        collection,
                        entity,
                        metadata.as_ref(),
                        self.format_version(),
                    )
                    .encode()
                    .len() as u64,
                )
            })
            .saturating_add(
                rows.deletes
                    .iter()
                    .map(|(name, _)| name.len() as u64 + 16)
                    .sum::<u64>(),
            );
        let result = Arc::new(parking_lot::Mutex::new(None));
        let queue = &self.row_publications;
        let mut state = queue.state.lock();
        state.requests.push_back(Request {
            rows,
            bytes,
            result: Arc::clone(&result),
        });
        queue.changed.notify_all();
        loop {
            if let Some(result) = result.lock().take() {
                return result.map_err(|error| StoreError::Io(std::io::Error::other(error)));
            }
            if state.active {
                queue.changed.wait(&mut state);
                continue;
            }
            state.active = true;
            let options = self.config.group_commit;
            let deadline =
                std::time::Instant::now() + StoreCommitCoordinator::resolve_window(&options);
            let limit = options.max_statements.max(1);
            while state.requests.len() < limit
                && state
                    .requests
                    .iter()
                    .map(|request| request.bytes)
                    .sum::<u64>()
                    < options.max_wal_bytes
                && std::time::Instant::now() < deadline
            {
                queue.changed.wait_until(&mut state, deadline);
            }
            let mut requests = Vec::new();
            let mut bytes = 0u64;
            while requests.len() < limit {
                let Some(next) = state.requests.front() else {
                    break;
                };
                if !requests.is_empty() && bytes.saturating_add(next.bytes) > options.max_wal_bytes
                {
                    break;
                }
                let request = state.requests.pop_front().expect("front exists");
                bytes = bytes.saturating_add(request.bytes);
                requests.push(request);
            }
            drop(state);
            let (rows, results): (Vec<_>, Vec<_>) = requests
                .into_iter()
                .map(|request| (request.rows, request.result))
                .unzip();
            let published = self
                .publish_row_group(rows)
                .map_err(|error| error.to_string());
            state = queue.state.lock();
            for result in results {
                *result.lock() = Some(published.clone());
            }
            state.active = false;
            queue.changed.notify_all();
        }
    }

    fn publish_row_group(&self, rows: Vec<RowPublication>) -> Result<(), StoreError> {
        let _publication = self.vault_publication_lock.lock();
        let session = self
            .config
            .embedded_wal_path
            .as_deref()
            .map(|path| {
                self.embedded_commit_session
                    .get_or_init(|| crate::storage::EmbeddedRdbArtifact::group_commit_session(path))
            })
            .or_else(|| self.embedded_commit_session.get());
        if let Some(session) = session {
            session
                .check()
                .map_err(|error| StoreError::Io(std::io::Error::other(error.to_string())))?;
        }
        // Read every preimage before changing anything. Failed cold reads must
        // not turn a partial view into a successful delete or replacement.
        let mut before = Vec::new();
        let mut inserted = Vec::new();
        let mut deleted_ids = std::collections::HashSet::new();
        for request in &rows {
            for (collection, id) in &request.deletes {
                if !deleted_ids.insert((collection.as_str(), *id)) {
                    return Err(StoreError::Io(std::io::Error::other(
                        "overlapping row publications",
                    )));
                }
                let entity = self
                    .try_get(collection, *id, usize::MAX)?
                    .ok_or(StoreError::EntityNotFound(*id))?;
                let metadata = self.try_get_metadata(collection, *id, usize::MAX)?;
                before.push((
                    collection.clone(),
                    *id,
                    StoreWalAction::upsert_entity(
                        collection,
                        &entity,
                        metadata.as_ref(),
                        self.format_version(),
                    ),
                ));
            }
        }
        let capture = StoreWalCaptureGuard::begin(session);
        let staged = (|| {
            for request in rows {
                for (collection, mut entity, metadata) in request.inserts {
                    if entity.id.raw() == 0 {
                        entity.id = self.next_entity_id();
                    }
                    let id = entity.id;
                    inserted.push((collection.clone(), id));
                    self.insert_auto(&collection, entity)?;
                    if let Some(metadata) = metadata {
                        self.set_metadata(&collection, id, metadata)?;
                    }
                }
                for (collection, id) in request.deletes {
                    self.delete(&collection, id)?;
                    self.resident.context_index.remove_entity(id);
                }
            }
            Ok(())
        })();
        let actions = capture.finish();
        let committed = staged.and_then(|()| {
            #[cfg(test)]
            if self
                .test_fail_deferred_wal_append
                .swap(false, Ordering::AcqRel)
            {
                return Err(StoreError::Io(std::io::Error::other(
                    "injected row publication failure",
                )));
            }
            if let Some(commit) = self.commit.as_ref().filter(|_| {
                self.config.durability_mode == DurabilityMode::WalDurableGrouped
                    && !self.is_read_only()
            }) {
                let track = self.config.resident_memory_bytes_max.is_some();
                let references = commit
                    .append_ready_actions(&actions.actions, track)
                    .map_err(StoreError::Io)?;
                if track {
                    self.remember_checkpoint_rows(actions.actions, references);
                }
                Ok(())
            } else {
                self.finish_paged_write(actions.actions)
            }
        });
        if let Err(error) = committed {
            for (collection, id) in inserted {
                self.apply_replayed_action(
                    &StoreWalAction::DeleteEntityRecord {
                        collection,
                        entity_id: id.raw(),
                    },
                    true,
                )?;
            }
            for (collection, id, action) in before {
                self.apply_replayed_action(
                    &StoreWalAction::DeleteEntityRecord {
                        collection: collection.clone(),
                        entity_id: id.raw(),
                    },
                    true,
                )?;
                self.get_collection(&collection)
                    .expect("preimage collection")
                    .force_seal()?;
                self.apply_replayed_action(&action, true)?;
            }
            return Err(error);
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn row(key: usize) -> UnifiedEntity {
        UnifiedEntity::table_row(
            EntityId::new(0),
            "keys",
            0,
            vec![Value::Integer(key as i64)],
        )
    }

    #[cfg(unix)]
    #[test]
    fn native_publications_share_fsync_and_survive_checkpoint_and_cold_boot() {
        const WRITERS: usize = 8;
        for bounded in [false, true] {
            let directory = tempfile::tempdir().expect("directory");
            let path = directory.path().join("group.rdb");
            let mut config = UnifiedStoreConfig::default()
                .with_durability_mode(DurabilityMode::WalDurableGrouped);
            config.group_commit.window_ms = 10;
            config.group_commit.max_statements = WRITERS;
            if bounded {
                config.resident_memory_bytes_max = Some(64 * 1024 * 1024);
            }
            let store =
                Arc::new(UnifiedStore::open_with_config(&path, config.clone()).expect("open"));
            store.create_collection("keys").expect("collection");
            let before = store.commit.as_ref().expect("coordinator").fsync_count();
            let start = Arc::new(std::sync::Barrier::new(WRITERS));
            let writers: Vec<_> = (0..WRITERS)
                .map(|key| {
                    let store = Arc::clone(&store);
                    let start = Arc::clone(&start);
                    std::thread::spawn(move || {
                        start.wait();
                        store
                            .write_keyed_row("keys", row(key), None, None)
                            .expect("durable row")
                    })
                })
                .collect();
            let ids: Vec<_> = writers
                .into_iter()
                .map(|writer| writer.join().expect("writer"))
                .collect();
            let fsyncs = store.commit.as_ref().expect("coordinator").fsync_count() - before;
            assert!(fsyncs > 0 && fsyncs < WRITERS as u64, "{fsyncs} fsyncs");
            store.persist().expect("checkpoint");
            drop(store);
            for _ in 0..3 {
                let store =
                    UnifiedStore::open_with_config(&path, config.clone()).expect("cold boot");
                for (key, id) in ids.iter().enumerate() {
                    assert_eq!(
                        store
                            .try_get("keys", *id, usize::MAX)
                            .expect("read")
                            .expect("row")
                            .data
                            .as_row()
                            .expect("row data")
                            .columns,
                        row(key).data.as_row().expect("expected row").columns
                    );
                }
                store.persist().expect("repeat checkpoint");
            }
        }
    }

    #[cfg(unix)]
    #[test]
    fn concurrent_publication_checkpoint_and_failed_replacement_recover() {
        let directory = tempfile::tempdir().expect("directory");
        let path = directory.path().join("concurrent.rdb");
        let mut config =
            UnifiedStoreConfig::default().with_durability_mode(DurabilityMode::WalDurableGrouped);
        config.resident_memory_bytes_max = Some(64 * 1024 * 1024);
        let store = UnifiedStore::open_with_config(&path, config.clone()).expect("open");
        store.create_collection("keys").expect("keys");
        let ids = std::thread::scope(|scope| {
            let checkpoint = scope.spawn(|| {
                for _ in 0..4 {
                    store.persist().expect("concurrent checkpoint");
                }
            });
            let writers: Vec<_> = (0..8)
                .map(|key| {
                    let store = &store;
                    scope.spawn(move || {
                        store
                            .write_keyed_row("keys", row(key), None, None)
                            .expect("publish")
                    })
                })
                .collect();
            let ids: Vec<_> = writers
                .into_iter()
                .map(|writer| writer.join().expect("writer"))
                .collect();
            checkpoint.join().expect("checkpoint");
            ids
        });
        store
            .test_fail_deferred_wal_append
            .store(true, Ordering::Release);
        assert!(store
            .write_keyed_row("keys", row(99), None, Some(ids[0]))
            .is_err());
        store.persist().expect("checkpoint restored preimage");
        drop(store);
        for _ in 0..3 {
            let store = UnifiedStore::open_with_config(&path, config.clone()).expect("cold boot");
            for (key, id) in ids.iter().enumerate() {
                let value = store
                    .try_get("keys", *id, usize::MAX)
                    .expect("read")
                    .expect("row");
                assert_eq!(
                    value.data.as_row().expect("row").columns,
                    vec![Value::Integer(key as i64)]
                );
            }
            store.persist().expect("checkpoint");
        }
    }

    #[test]
    fn failed_compound_publication_restores_all_rows() {
        let store = UnifiedStore::new();
        let first = store.insert_auto("keys", row(1)).expect("first");
        let second = store.insert_auto("keys", row(2)).expect("second");
        store
            .test_fail_deferred_wal_append
            .store(true, Ordering::Release);
        assert!(store
            .publish_rows(RowPublication {
                inserts: vec![("keys".into(), row(3), None)],
                deletes: vec![("keys".into(), first), ("keys".into(), second)],
            })
            .is_err());
        assert!(!UnifiedStore::deferred_store_wal_capture_active());
        assert_eq!(store.get_collection("keys").expect("keys").count(), 2);
        assert_eq!(
            store
                .get("keys", first)
                .expect("restored first")
                .data
                .as_row()
                .expect("row")
                .columns,
            row(1).data.as_row().expect("row").columns
        );
        assert_eq!(
            store
                .get("keys", second)
                .expect("restored second")
                .data
                .as_row()
                .expect("row")
                .columns,
            row(2).data.as_row().expect("row").columns
        );
    }
}