sqlite-embedded 3.0.0-preview.3

Native SQLite VFS client for Rivet actors
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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! Process-wide buffer of pages SQLite has not read: pages fetched ahead of its requests, or
//! pages it committed that its pager may evict. A page leaves the buffer when SQLite reads it,
//! because SQLite's pager then owns it.
//!
//! Every database of one runtime process shares one [`PageBudget`], so the process holds at most
//! the configured number of buffered pages however many databases it opens. Each database's
//! buffers are [`PageBuffer`] handles into that budget.

use std::{
	hash::{Hash, Hasher},
	sync::{
		Arc,
		atomic::{AtomicU64, Ordering},
	},
	time::{Duration, Instant},
};

use lru::LruCache;
use parking_lot::{Mutex, RwLock};
use sqlite_types::PageBytes;

use crate::worker::SqlitePoolMetrics;

/// Expired pages an insert may drop from its shard's cold end.
const EXPIRE_PER_INSERT: usize = 4;

/// Shards of a large budget. A small budget uses one shard, so its eviction order is exact.
const SHARDS: usize = 16;

/// Budgets below this many pages use a single shard.
const MIN_SHARDED_CAPACITY: u64 = 4_096;

/// One process's page budget, shared by every database the process opens.
///
/// Eviction is least recently used across databases within each shard. A page lands in the
/// shard picked by its buffer and page number, so a busy database cannot evict pages only from
/// itself, and an idle one gives up its pages as others insert. The shard locks guard only
/// in-memory maps and are never held across storage I/O.
pub struct PageBudget {
	shards: Box<[Mutex<LruCache<PageKey, Entry>>]>,
	shard_capacity: u64,
	capacity: u64,
	ttl: Option<Duration>,
	used: AtomicU64,
	next_buffer: AtomicU64,
	metrics: Option<Arc<dyn SqlitePoolMetrics>>,
}

impl std::fmt::Debug for PageBudget {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("PageBudget")
			.field("capacity", &self.capacity)
			.field("used", &self.used.load(Ordering::Relaxed))
			.field("ttl", &self.ttl)
			.finish()
	}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct PageKey {
	buffer: u64,
	page: u32,
}

#[derive(Debug)]
struct Entry {
	page: PageBytes,
	inserted: Instant,
	/// Live pages of the buffer generation that inserted this page.
	owner: Arc<AtomicU64>,
	unread: Option<UnreadPage>,
}

/// One received page's diagnostic identity, allocated only for an enabled TRACE capture.
/// The span carries the native opening and request; `ordinal` distinguishes duplicate pgnos.
#[derive(Debug)]
pub(crate) struct PageTrace {
	span: tracing::Span,
	dispatch: tracing::Dispatch,
	ordinal: usize,
	pgno: u32,
	bytes: usize,
}
impl Drop for PageTrace {
	fn drop(&mut self) {
		// The registry closes parent references through the ambient subscriber.
		// Retained contexts can be released by another worker or request collector.
		tracing::dispatcher::with_default(&self.dispatch, || {
			drop(std::mem::replace(&mut self.span, tracing::Span::none()));
		});
	}
}

impl PageTrace {
	pub(crate) fn capture(ordinal: usize, pgno: u32, bytes: usize) -> Option<Box<Self>> {
		tracing::enabled!(target: "sqlite_prefetch::trace", tracing::Level::TRACE).then(|| {
			Box::new(Self {
				span: tracing::Span::current(),
				dispatch: tracing::dispatcher::get_default(Clone::clone),
				ordinal,
				pgno,
				bytes,
			})
		})
	}
	pub(crate) fn disposition(&self, disposition: &'static str) {
		tracing::dispatcher::with_default(&self.dispatch, || {
			self.span.in_scope(|| {
				tracing::trace!(target: "sqlite_prefetch::trace", event = "page_disposition",
                ordinal = self.ordinal, pgno = self.pgno, bytes = self.bytes, disposition);
			})
		});
	}
	fn buffered(&self, generation: &Arc<UnreadTracker>) {
		// Reuse the buffer's existing monotonic identity. Allocator addresses can be
		// recycled while the same VFS opening continues issuing requests.
		tracing::dispatcher::with_default(&self.dispatch, || {
			self.span.in_scope(|| {
				tracing::trace!(target: "sqlite_prefetch::trace", event = "page_buffered",
                buffer_generation = generation.id,
                ordinal = self.ordinal, pgno = self.pgno, bytes = self.bytes);
			})
		});
	}
}

#[derive(Debug, Clone)]
struct CloseTrace {
	span: tracing::Span,
	dispatch: tracing::Dispatch,
}
impl Drop for CloseTrace {
	fn drop(&mut self) {
		// The registry closes parent references through the ambient subscriber.
		// Retained contexts can be released by another worker or request collector.
		tracing::dispatcher::with_default(&self.dispatch, || {
			drop(std::mem::replace(&mut self.span, tracing::Span::none()));
		});
	}
}

/// Counts unread removals once, including immediate release of a whole generation.
#[derive(Debug)]
struct UnreadTracker {
	id: u64,
	state: Mutex<UnreadState>,
	total: Arc<AtomicU64>,
}
#[derive(Debug)]
struct UnreadState {
	count: Option<u64>,
	trace: Option<CloseTrace>,
	traced_unread: u64,
}
impl UnreadTracker {
	fn new(total: Arc<AtomicU64>, id: u64) -> Arc<Self> {
		Arc::new(Self {
			id,
			state: Mutex::new(UnreadState {
				count: Some(0),
				trace: None,
				traced_unread: 0,
			}),
			total,
		})
	}
	fn page(self: &Arc<Self>, trace: Option<Box<PageTrace>>) -> UnreadPage {
		let mut state = self.state.lock();
		if let Some(count) = state.count.as_mut() {
			*count += 1;
			if let Some(trace) = &trace {
				state.traced_unread += 1;
				if state.trace.is_none() {
					state.trace = Some(CloseTrace {
						span: trace.span.clone(),
						dispatch: trace.dispatch.clone(),
					});
				}
				trace.buffered(self);
			}
		} else {
			self.total.fetch_add(1, Ordering::Relaxed);
			if let Some(trace) = &trace {
				trace.disposition("unused");
			}
		}
		UnreadPage {
			owner: self.clone(),
			consumed: false,
			trace,
		}
	}
	fn release(&self, consumed: bool, trace: Option<&PageTrace>) {
		let mut state = self.state.lock();
		if let Some(count) = state.count.as_mut() {
			*count -= 1;
			if !consumed {
				self.total.fetch_add(1, Ordering::Relaxed);
			}
			if let Some(trace) = trace {
				state.traced_unread -= 1;
				trace.disposition(if consumed { "consumed" } else { "unused" });
			}
		}
	}
	fn close(&self) {
		let mut state = self.state.lock();
		if let Some(count) = state.count.take() {
			self.total.fetch_add(count, Ordering::Relaxed);
			if let Some(trace) = state.trace.take() {
				// O(1): outstanding page_buffered deliveries for this generation become
				// unused here. Ghost-entry drops after this event emit no second outcome.
				tracing::dispatcher::with_default(&trace.dispatch, || {
					trace.span.in_scope(|| {
						tracing::trace!(target: "sqlite_prefetch::trace", event = "buffer_closed",
                        buffer_generation = self.id, unread_pages = count, unread_traced_pages = state.traced_unread);
					})
				});
			}
		}
	}
}
#[derive(Debug)]
struct UnreadPage {
	owner: Arc<UnreadTracker>,
	consumed: bool,
	trace: Option<Box<PageTrace>>,
}
impl UnreadPage {
	fn consume(&mut self) {
		if !self.consumed {
			self.owner.release(true, self.trace.as_deref());
			self.consumed = true;
		}
	}
}
impl Drop for UnreadPage {
	fn drop(&mut self) {
		if !self.consumed {
			self.owner.release(false, self.trace.as_deref());
		}
	}
}

impl PageBudget {
	/// A budget of `capacity` pages shared by every buffer made from it. Zero disables
	/// buffering. Pages older than `ttl_ms` expire; zero keeps them until evicted.
	pub fn new(
		capacity: u64,
		ttl_ms: u64,
		metrics: Option<Arc<dyn SqlitePoolMetrics>>,
	) -> Arc<Self> {
		let shard_count = if capacity >= MIN_SHARDED_CAPACITY {
			SHARDS
		} else {
			1
		};
		// Allocate as pages arrive, rather than reserving the configured maximum up front.
		let shards = (0..shard_count)
			.map(|_| Mutex::new(LruCache::unbounded()))
			.collect::<Vec<_>>()
			.into_boxed_slice();
		if let Some(metrics) = &metrics {
			metrics.set_page_buffer_capacity_pages(capacity);
			metrics.set_page_buffer_pages(0);
		}
		Arc::new(Self {
			shards,
			shard_capacity: capacity.div_ceil(shard_count as u64),
			capacity,
			ttl: (ttl_ms > 0).then(|| Duration::from_millis(ttl_ms)),
			used: AtomicU64::new(0),
			next_buffer: AtomicU64::new(1),
			metrics,
		})
	}

	/// Configured page capacity of the whole process.
	pub fn capacity_pages(&self) -> u64 {
		self.capacity
	}

	/// Pages held across every database, including expired pages awaiting eviction and pages
	/// of released buffers that the LRU has not reached yet.
	pub fn used_pages(&self) -> u64 {
		self.used.load(Ordering::Relaxed)
	}

	fn shard(&self, key: &PageKey) -> &Mutex<LruCache<PageKey, Entry>> {
		if self.shards.len() == 1 {
			return &self.shards[0];
		}
		let mut hasher = std::collections::hash_map::DefaultHasher::new();
		key.hash(&mut hasher);
		&self.shards[hasher.finish() as usize % self.shards.len()]
	}

	fn allocate_buffer(&self) -> u64 {
		self.next_buffer.fetch_add(1, Ordering::Relaxed)
	}

	/// Accounts for one page leaving the budget.
	fn released(&self, entry: &Entry) {
		entry.owner.fetch_sub(1, Ordering::Relaxed);
		let used = self.used.fetch_sub(1, Ordering::Relaxed).saturating_sub(1);
		if let Some(metrics) = &self.metrics {
			metrics.set_page_buffer_pages(used);
		}
	}

	fn added(&self) {
		let used = self.used.fetch_add(1, Ordering::Relaxed).saturating_add(1);
		if let Some(metrics) = &self.metrics {
			metrics.set_page_buffer_pages(used);
		}
	}

	fn expired(&self, entry: &Entry, now: Instant) -> bool {
		self.ttl
			.is_some_and(|ttl| now.duration_since(entry.inserted) >= ttl)
	}
}

/// One database buffer in the process budget. Clones share pages with asynchronous prefetch
/// completion.
#[derive(Clone, Debug)]
pub(crate) struct PageBuffer {
	inner: Arc<BufferInner>,
}

#[derive(Debug)]
struct BufferInner {
	budget: Arc<PageBudget>,
	/// The current generation: invalidating everything moves to a fresh id, so older pages
	/// become unreachable and age out of the budget without a scan.
	generation: RwLock<Generation>,
}

#[derive(Debug, Clone)]
struct Generation {
	id: u64,
	live: Arc<AtomicU64>,
	unread: Arc<UnreadTracker>,
}

impl PageBuffer {
	pub(crate) fn new(budget: &Arc<PageBudget>) -> Self {
		Self::with_unused_counter(budget, Arc::new(AtomicU64::new(0)))
	}

	pub(crate) fn with_unused_counter(budget: &Arc<PageBudget>, total: Arc<AtomicU64>) -> Self {
		let id = budget.allocate_buffer();
		Self {
			inner: Arc::new(BufferInner {
				budget: budget.clone(),
				generation: RwLock::new(Generation {
					id,
					live: Arc::new(AtomicU64::new(0)),
					unread: UnreadTracker::new(total, id),
				}),
			}),
		}
	}

	fn generation(&self) -> Generation {
		self.inner.generation.read().clone()
	}

	fn key(&self, generation: &Generation, page: u32) -> PageKey {
		PageKey {
			buffer: generation.id,
			page,
		}
	}

	pub(crate) fn get(&self, page: &u32) -> Option<PageBytes> {
		self.get_at(page, cache_now())
	}

	fn get_at(&self, page: &u32, now: Instant) -> Option<PageBytes> {
		self.lookup_at(page, now, true)
	}

	fn lookup_at(&self, page: &u32, now: Instant, consume: bool) -> Option<PageBytes> {
		let budget = &self.inner.budget;
		let generation = self.generation();
		let key = self.key(&generation, *page);
		let mut shard = budget.shard(&key).lock();
		let entry = shard.get_mut(&key)?;
		if budget.expired(entry, now) {
			if let Some(entry) = shard.pop(&key) {
				budget.released(&entry);
			}
			None
		} else {
			if consume && let Some(unread) = entry.unread.as_mut() {
				unread.consume();
			}
			Some(entry.page.clone())
		}
	}

	/// Removes and returns a live page, handing it to the caller. An expired page is dropped.
	pub(crate) fn take(&self, page: &u32) -> Option<PageBytes> {
		self.take_at(page, cache_now())
	}

	fn take_at(&self, page: &u32, now: Instant) -> Option<PageBytes> {
		self.take_status_at(page, now).map(|(bytes, _)| bytes)
	}
	pub(crate) fn take_with_status(&self, page: &u32) -> Option<(PageBytes, bool)> {
		self.take_status_at(page, cache_now())
	}
	fn take_status_at(&self, page: &u32, now: Instant) -> Option<(PageBytes, bool)> {
		let budget = &self.inner.budget;
		let generation = self.generation();
		let key = self.key(&generation, *page);
		let mut entry = budget.shard(&key).lock().pop(&key)?;
		budget.released(&entry);
		if budget.expired(&entry, now) {
			None
		} else {
			let was_unread = entry.unread.as_ref().is_some_and(|unread| !unread.consumed);
			if let Some(unread) = entry.unread.as_mut() {
				unread.consume();
			}
			Some((entry.page, was_unread))
		}
	}

	

	pub(crate) fn insert(&self, page: u32, bytes: PageBytes) {
		self.insert_at(page, bytes, cache_now());
	}

	pub(crate) fn insert_prefetched(&self, page: u32, bytes: PageBytes) {
		self.insert_traced_at(page, bytes, cache_now(), true, None);
	}
	pub(crate) fn insert_prefetched_traced(
		&self,
		page: u32,
		bytes: PageBytes,
		trace: Option<Box<PageTrace>>,
	) {
		self.insert_traced_at(page, bytes, cache_now(), true, trace);
	}
	fn insert_at(&self, page: u32, bytes: PageBytes, now: Instant) {
		self.insert_tracked_at(page, bytes, now, false);
	}
	fn insert_tracked_at(&self, page: u32, bytes: PageBytes, now: Instant, track: bool) {
		self.insert_traced_at(page, bytes, now, track, None);
	}
	fn insert_traced_at(
		&self,
		page: u32,
		bytes: PageBytes,
		now: Instant,
		track: bool,
		trace: Option<Box<PageTrace>>,
	) {
		let budget = &self.inner.budget;
		if budget.capacity == 0 {
			if let Some(trace) = &trace {
				trace.disposition("unused");
			}
			if track {
				self.generation()
					.unread
					.total
					.fetch_add(1, Ordering::Relaxed);
			}
			return;
		}
		let generation = self.generation();
		let key = self.key(&generation, page);
		let mut shard = budget.shard(&key).lock();
		// Busy shards shed stale pages at a bounded cost per insert.
		expire_tail(budget, &mut shard, now, EXPIRE_PER_INSERT);
		generation.live.fetch_add(1, Ordering::Relaxed);
		budget.added();
		let entry = Entry {
			page: bytes,
			inserted: now,
			owner: generation.live,
			unread: track.then(|| generation.unread.page(trace)),
		};
		if let Some((_, replaced)) = shard.push(key, entry) {
			budget.released(&replaced);
		}
		while shard.len() as u64 > budget.shard_capacity {
			let Some((_, evicted)) = shard.pop_lru() else {
				break;
			};
			budget.released(&evicted);
		}
	}

	pub(crate) fn invalidate(&self, page: &u32) {
		let budget = &self.inner.budget;
		let generation = self.generation();
		let key = self.key(&generation, *page);
		let removed = budget.shard(&key).lock().pop(&key);
		if let Some(entry) = removed {
			budget.released(&entry);
		}
	}

	/// Drops every page of this buffer. Later reads miss at once; the dropped pages stay in the
	/// budget, unreachable, until the LRU or their expiry reaches them.
	pub(crate) fn invalidate_all(&self) {
		let budget = &self.inner.budget;
		let mut generation = self.inner.generation.write();
		generation.unread.close();
		let id = budget.allocate_buffer();
		*generation = Generation {
			id,
			live: Arc::new(AtomicU64::new(0)),
			unread: UnreadTracker::new(generation.unread.total.clone(), id),
		};
	}

	/// Live pages of this buffer, including lazily expired pages awaiting eviction or access.
	pub(crate) fn entry_count(&self) -> u64 {
		self.generation().live.load(Ordering::Relaxed)
	}

	pub(crate) fn weighted_size(&self) -> u64 {
		self.entry_count()
	}
}

/// Pops expired pages from the least recently used end of a shard, stopping at the first live
/// page or after `limit` pages. A stale page that a read moved to the front stays until a read
/// finds it.
fn expire_tail(
	budget: &PageBudget,
	shard: &mut LruCache<PageKey, Entry>,
	now: Instant,
	limit: usize,
) -> usize {
	if budget.ttl.is_none() {
		return 0;
	}
	let mut expired = 0;
	while expired < limit {
		let Some((_, entry)) = shard.peek_lru() else {
			break;
		};
		if !budget.expired(entry, now) {
			break;
		}
		if let Some((_, entry)) = shard.pop_lru() {
			budget.released(&entry);
		}
		expired += 1;
	}
	expired
}

// This actor-side SQLite adapter runs on synchronous native worker threads. Cache
// expiration only evicts optional read copies; it cannot affect storage durability.
#[allow(clippy::disallowed_methods)]
fn cache_now() -> Instant {
	Instant::now()
}



impl Drop for BufferInner {
	fn drop(&mut self) {
		self.generation.get_mut().unread.close();
	}
}