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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Catalog-backed operations: realm-quota persistence and commit-history
//! maintenance.
use std::sync::atomic::Ordering;
use crate::btree::BTree;
use crate::catalog::codec::{Catalog, RealmQuotas};
use crate::errors::PagedbError;
use crate::pager::anchor::HeaderCursor;
use crate::pager::header::commit_header;
use crate::vfs::Vfs;
use crate::{RealmId, Result};
use super::core::{
CommitHistoryMeta, Db, HeaderFieldsParams, decode_commit_meta, encode_commit_meta,
encode_root_ref,
};
use super::pending::PendingWriterState;
/// Named-counter rows read per batch while validating them at open. Rows are a
/// fixed-width authenticated value, so this is a few KiB resident regardless of
/// how many counters the embedder has named.
const COUNTER_ROW_BATCH: usize = 512;
/// Commit-history rows read per batch while pruning by age. A key is 8 bytes
/// and a row 40, so this is a few tens of KiB resident regardless of how deep
/// retention has run.
const HISTORY_PRUNE_BATCH: usize = 512;
impl<V: Vfs + Clone> Db<V> {
/// The oldest commit id still retained in the commit-history index, or
/// `None` when history is disabled or the index is empty. Pages reachable
/// from this commit's root (or any newer one) must not be recycled, so it
/// is one of the two floors gating free-page reclamation (the other is the
/// oldest live reader pin). Reads only the leftmost spine of the history
/// tree — O(height), not O(retained count).
pub(crate) async fn oldest_retained_history_commit(
&self,
commit_history_root_page_id: u64,
next_page_id: u64,
) -> Result<Option<u64>> {
if matches!(
self.options.commit_history_retain,
crate::options::RetainPolicy::Disabled
) || commit_history_root_page_id == 0
{
return Ok(None);
}
let hist = BTree::open(
self.pager.clone(),
self.realm_id,
commit_history_root_page_id,
next_page_id,
self.page_size,
);
let Some(key) = hist.first_key().await? else {
return Ok(None);
};
if key.len() != 8 {
return Err(PagedbError::catalog_row_invalid("commit_history.key"));
}
let mut b = [0u8; 8];
b.copy_from_slice(&key[..8]);
Ok(Some(u64::from_be_bytes(b)))
}
/// Authenticate and decode every persisted named-counter row during open.
///
/// Named counters are already atomic with catalog-root publication, so
/// recovery validates their encoding but never rewrites their values.
///
/// Rows are streamed in bounded batches: how many counters an embedder has
/// named is its business, and open must not size an allocation by it.
pub(super) async fn validate_counter_rows(
&self,
catalog_root_page_id: u64,
next_page_id: u64,
) -> Result<()> {
if catalog_root_page_id == 0 {
return Ok(());
}
let prefix = [crate::catalog::codec::CatalogRowKind::Counter as u8];
let tree = BTree::open(
self.pager.clone(),
self.realm_id,
catalog_root_page_id,
next_page_id,
self.page_size,
);
let mut cursor: Vec<u8> = prefix.to_vec();
loop {
let batch = tree
.collect_prefix_batch_from(&prefix, &cursor, COUNTER_ROW_BATCH)
.await?;
let Some((last_key, _)) = batch.last() else {
return Ok(());
};
cursor.clear();
cursor.extend_from_slice(last_key);
// The exact successor of `last_key` in the key ordering: resume
// strictly past the row just validated without re-reading it.
cursor.push(0);
let exhausted = batch.len() < COUNTER_ROW_BATCH;
for (_key, value) in &batch {
Catalog::decode_counter(value)?;
}
if exhausted {
return Ok(());
}
}
}
/// Write per-realm quota caps into the catalog B+ tree and persist the
/// updated catalog root to the A/B header.
pub async fn set_realm_quotas(&self, realm: RealmId, quotas: RealmQuotas) -> Result<()> {
self.ensure_usable()?;
let mut state = self.writer.lock().await;
self.ensure_usable()?;
let key = Catalog::quota_key(realm);
let value = Catalog::encode_realm_quotas("as);
let mut cat_tree = BTree::open(
self.pager.clone(),
self.realm_id,
state.catalog_root_page_id,
state.next_page_id,
self.page_size,
);
cat_tree.put(&key, &value).await?;
cat_tree.flush().await?;
let new_catalog_root = cat_tree.root_page_id();
let new_next = cat_tree.next_page_id();
let new_catalog_txn_id = state
.latest_commit_id
.checked_add(1)
.ok_or_else(|| PagedbError::arithmetic_overflow("catalog transaction id"))?;
let header_cursor = self.pager.header_cursor()?;
let new_seq = header_cursor.next_seq()?;
let counter_anchor = self.pager.pending_anchor();
let catalog_root_bytes = encode_root_ref(new_catalog_root, new_catalog_txn_id);
let fields = self.header_fields(HeaderFieldsParams {
mk_epoch: self.mk_epoch.load(Ordering::SeqCst),
seq: new_seq,
active_root_page_id: state.root_page_id,
active_root_txn_id: state.latest_commit_id,
counter_anchor,
commit_id: state.latest_commit_id,
catalog_root: catalog_root_bytes,
commit_history_root_page_id: state.commit_history_root_page_id,
commit_history_root_version: state.commit_history_root_version,
free_list_root_page_id: state.free_list_root_page_id,
next_page_id: new_next,
})?;
let hk_clone = { self.hk.read().clone() };
let new_slot = commit_header(
&*self.vfs,
&self.main_db_path,
&hk_clone,
&fields,
header_cursor.slot,
self.page_size,
)
.await?;
self.pager.note_header_written(HeaderCursor {
slot: new_slot,
seq: new_seq,
});
state.catalog_root_page_id = new_catalog_root;
state.catalog_root_txn_id = new_catalog_txn_id;
state.next_page_id = new_next;
let _ = self
.finish_durable_commit(
&state,
crate::CommitId(state.latest_commit_id),
counter_anchor,
&[],
)
.await?;
Ok(())
}
/// Read per-realm quota caps from the catalog B+ tree. Returns
/// `RealmQuotas::default()` if no entry has been written for this realm.
pub async fn realm_quotas(&self, realm: RealmId) -> Result<RealmQuotas> {
self.ensure_usable()?;
let snapshot = *self.snapshot.read();
let key = Catalog::quota_key(realm);
let cat_tree = BTree::open(
self.pager.clone(),
self.realm_id,
snapshot.catalog_root_page_id,
snapshot.next_page_id,
self.page_size,
);
match cat_tree.get(&key).await? {
Some(bytes) => Catalog::decode_realm_quotas(&bytes),
None => Ok(RealmQuotas::default()),
}
}
/// Insert the new commit-history entry and prune per the retention policy.
/// Returns the page ids freed by this tree's copy-on-write and pruning.
///
/// Those ids go into the commit's free-list entry set like any other free,
/// and deliberately **not** straight into the shared allocator cache. The
/// cache is loaded once, at `begin_write`, from the bounded window of the
/// durable chain that the following commit rewrites; a page pushed in from
/// anywhere else would be handed to an allocator without the chain entry
/// naming it ever being located, so nothing would delete it and the
/// unscanned tail would keep naming it. One page id, two owners.
///
/// `state` is the *candidate* writer state, not the shared one: this call
/// is fallible at several points and runs long before the header that
/// would make its new root durable, so a commit that never publishes must
/// leave the shared state naming the old root. See [`PendingWriterState`].
#[allow(clippy::too_many_lines)]
pub(crate) async fn write_commit_history_entry(
&self,
state: &mut PendingWriterState,
new_commit_id: u64,
meta: CommitHistoryMeta,
) -> Result<Vec<u64>> {
let min_pinned = {
let readers = self.tracked_readers.lock();
readers.iter().map(|r| r.commit_id.0).min()
};
let mut hist_tree = BTree::open(
self.pager.clone(),
self.realm_id,
state.commit_history_root_page_id,
state.next_page_id,
self.page_size,
);
// The commit-history tree is not part of any reader's pinned snapshot
// (readers track the data and catalog roots, never the history root), so
// every page its copy-on-write/prune frees is immediately reusable
// in-session — hence the zero reuse threshold.
//
// Sharing the allocator cache is a one-way street: this tree *draws*
// from it (recording each draw in the consumed sink, which is what
// deletes the entry from the rewritten window at commit) and never
// pushes into it. Its own frees leave through `drain_freed` into the
// commit's entry set instead, so no page reaches an allocator without
// the window entry that names it having been located first.
hist_tree.set_reuse_threshold(0);
hist_tree.set_free_page_cache(self.free_page_cache.clone());
hist_tree.set_free_page_consumed(self.free_page_consumed.clone());
// Insert the new entry.
let key = new_commit_id.to_be_bytes().to_vec();
let value = encode_commit_meta(&meta);
let was_new = hist_tree.get(&key).await?.is_none();
hist_tree.put(&key, &value).await?;
// Prune according to retention policy.
let policy = &self.options.commit_history_retain;
match policy {
crate::options::RetainPolicy::Unbounded => {
// No pruning.
if was_new {
state.commit_history_count =
Some(state.commit_history_count.unwrap_or(0).saturating_add(1));
}
}
crate::options::RetainPolicy::Count(n) => {
let count = *n as usize;
// Fast path: if the cached count is known and the post-insert
// count is at or below the retain limit, we can skip the
// full-tree scan entirely.
let projected = state
.commit_history_count
.map(|c| if was_new { c.saturating_add(1) } else { c });
if let Some(p) = projected {
if p <= u64::from(*n) {
state.commit_history_count = Some(p);
// Materialize and return below.
} else {
// Over-limit: do the scan + prune.
let all = hist_tree.collect_all().await?;
let mut current = all.len() as u64;
if all.len() > count {
let to_delete = all.len() - count;
for (k, _) in all.iter().take(to_delete) {
let mut b = [0u8; 8];
b.copy_from_slice(&k[..8]);
let cid = u64::from_be_bytes(b);
if let Some(min) = min_pinned {
if cid >= min {
continue;
}
}
if hist_tree.delete(k).await? {
current = current.saturating_sub(1);
}
}
}
state.commit_history_count = Some(current);
}
} else {
// No cached count — do the scan to populate it.
let all = hist_tree.collect_all().await?;
let mut current = all.len() as u64;
if all.len() > count {
let to_delete = all.len() - count;
for (k, _) in all.iter().take(to_delete) {
let mut b = [0u8; 8];
b.copy_from_slice(&k[..8]);
let cid = u64::from_be_bytes(b);
if let Some(min) = min_pinned {
if cid >= min {
continue;
}
}
if hist_tree.delete(k).await? {
current = current.saturating_sub(1);
}
}
}
state.commit_history_count = Some(current);
}
}
crate::options::RetainPolicy::Age(duration) => {
let now_secs = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_secs());
let threshold = now_secs.saturating_sub(duration.as_secs());
// History keys are the commit id big-endian, so lexicographic
// key order is commit order and the prunable rows are always a
// prefix of the oldest ones. Streaming that prefix in
// fixed-size batches holds one batch resident instead of one
// entry per retained commit — retention can be arbitrarily
// deep, and this runs on every commit. The first row that must
// be kept ends the walk: every row after it is a later commit,
// so it is neither older than the threshold nor below the
// reader floor.
let mut deleted: u64 = 0;
let mut cursor: Vec<u8> = Vec::new();
'prune: loop {
let batch = hist_tree
.collect_batch_from(&cursor, HISTORY_PRUNE_BATCH)
.await?;
let Some((last_key, _)) = batch.last() else {
break;
};
cursor.clear();
cursor.extend_from_slice(last_key);
// The exact successor of `last_key` in the key ordering:
// resume strictly past the row just examined.
cursor.push(0);
let exhausted = batch.len() < HISTORY_PRUNE_BATCH;
for (k, v) in &batch {
// Never delete the entry we just inserted.
if k == &key {
break 'prune;
}
if k.len() != 8 {
return Err(PagedbError::catalog_row_invalid("commit_history.key"));
}
let mut b = [0u8; 8];
b.copy_from_slice(&k[..8]);
let cid = u64::from_be_bytes(b);
if min_pinned.is_some_and(|min| cid >= min) {
break 'prune;
}
if decode_commit_meta(v)?.unix_seconds >= threshold {
break 'prune;
}
if hist_tree.delete(k).await? {
deleted = deleted.saturating_add(1);
}
}
if exhausted {
break;
}
}
let projected = state
.commit_history_count
.map(|c| if was_new { c.saturating_add(1) } else { c });
state.commit_history_count = projected.map(|c| c.saturating_sub(deleted));
}
crate::options::RetainPolicy::Disabled => {
// Unreachable: `WriteTxn::commit` skips this call entirely
// when the policy is `Disabled`. Treat any accidental call as
// a no-op rather than panicking, to be defensive.
}
}
// Materialize the history tree's dirty leaves into the pager (so the
// commit's unified `pager.flush_main` picks them up) without issuing a
// separate fsync. The caller is responsible for flushing the pager.
hist_tree.materialize_dirty().await?;
// Capture spine/prune frees after materialization (they are realized
// during the flush, not before it).
let freed: Vec<u64> = hist_tree
.drain_freed()
.into_iter()
.filter(|&p| p >= 4)
.collect();
let new_hist_root = hist_tree.root_page_id();
let new_next = hist_tree.next_page_id().max(state.next_page_id);
state.commit_history_root_page_id = new_hist_root;
state.commit_history_root_version = new_commit_id;
state.next_page_id = new_next;
Ok(freed)
}
}
#[cfg(test)]
mod tests {
use crate::vfs::memory::MemVfs;
use crate::{Db, PagedbError, RealmId};
use super::*;
const PAGE: usize = 4096;
const REALM: RealmId = RealmId::new([0xA7; 16]);
#[tokio::test(flavor = "current_thread")]
async fn counter_recovery_surfaces_malformed_counter_row() {
let db = Db::open_internal(MemVfs::new(), [9u8; 32], PAGE, REALM)
.await
.unwrap();
{
let mut txn = db.begin_write().await.unwrap();
let mut counter = txn.counter("bad-counter").unwrap();
counter.set(5).await.unwrap();
drop(counter);
txn.commit().await.unwrap();
}
let (catalog_root, next_page_id) = {
let state = db.writer.lock().await;
(state.catalog_root_page_id, state.next_page_id)
};
let mut tree = BTree::open(
db.pager.clone(),
db.realm_id,
catalog_root,
next_page_id,
db.page_size,
);
tree.put(&Catalog::counter_key(&[0xFF]).unwrap(), b"bad")
.await
.unwrap();
tree.flush().await.unwrap();
let err = db
.validate_counter_rows(tree.root_page_id(), tree.next_page_id())
.await
.expect_err("malformed counter row must surface during recovery validation");
assert!(matches!(err, PagedbError::Corruption(_)));
}
#[tokio::test(flavor = "current_thread")]
async fn oldest_retained_history_commit_surfaces_malformed_history_key() {
for malformed_key in [b"x".as_slice(), b"123456789".as_slice()] {
let db = Db::open_internal(MemVfs::new(), [9u8; 32], PAGE, REALM)
.await
.unwrap();
let next_page_id = db.writer.lock().await.next_page_id;
let mut history =
BTree::open(db.pager.clone(), db.realm_id, 0, next_page_id, db.page_size);
history
.put(malformed_key, b"malformed history")
.await
.unwrap();
history.flush().await.unwrap();
let err = db
.oldest_retained_history_commit(history.root_page_id(), history.next_page_id())
.await
.expect_err("malformed history key must surface");
assert!(matches!(err, PagedbError::Corruption(_)));
}
}
}