chadtmiller.com/monolith
An atproto PDS
0
0
Tables: files held open, blocks of 4 KiB, a block cache in parts
bench/store/src/reads.rs+76−80
| @@ -11,11 +11,10 @@ use crate::rocks::{self, Pack, Setup}; | ||
use crate::stores::{self, block_bytes, key, path_cid}; | ||
use crate::tables::{self, Reader}; | ||
use crate::util::{Rng, at, cores, dir_bytes, mib, par, rss_kib, scratch, us}; | ||
use pds_store::table::{BlockCache, Table}; | ||
use pds_store::table::{BLOCK, BlockCache, OPEN, Table}; | ||
use rocksdb::DB; | ||
use rusqlite::{Connection, params}; | ||
use std::collections::HashMap; | ||
use std::fs::File; | ||
use std::hint::black_box; | ||
use std::path::{Path, PathBuf}; | ||
use std::sync::{Mutex, OnceLock}; | ||
| @@ -41,22 +40,23 @@ const LIST: &str = "SELECT r.path, b.bytes FROM record r JOIN block b ON b.accou | ||
/// is first read. | ||
enum Store { | ||
/// A table for each account read as the server reads one | ||
/// (`pds_store::table::Table`), through one block cache: its file | ||
/// opened for each block read, as `Table::get` does given none, or | ||
/// opened once and held. | ||
/// (`pds_store::table::Table`), through one block cache, which keeps | ||
/// the tables' files open: at most `OPEN` of them, as the server's | ||
/// does, or every one. | ||
Tables { | ||
dir: PathBuf, | ||
tables: Vec<OnceLock<(Table, Option<File>)>>, | ||
held: bool, | ||
tables: Vec<OnceLock<Table>>, | ||
cache: BlockCache, | ||
}, | ||
/// The same files read by this benchmark's reader (`tables::Reader`), | ||
/// which holds each open, keeps every block it reads, has no bloom | ||
/// filter, and computes the check it is given. | ||
/// Tables read by this benchmark's reader (`tables::Reader`), which | ||
/// keeps every block it reads, has no bloom filter, and computes the | ||
/// check it is given: each file held open, or opened for each block | ||
/// read. | ||
Own { | ||
dir: PathBuf, | ||
readers: Vec<OnceLock<Reader>>, | ||
check: Check, | ||
each: bool, | ||
}, | ||
/// A database for each account, one connection each, its page cache | ||
/// SQLite's own 2 MiB: more than the database. | ||
| @@ -85,10 +85,11 @@ enum Store { | ||
}, | ||
} | ||
| ||
/// The directory a way of reading reads: `tables`, `tables4` (the same | ||
/// tables in blocks of 4 KiB), `dbs` or `db`. A way is named for it, then | ||
/// `+held` for tables whose files are held open, or `:` and a check for | ||
/// this benchmark's reader. | ||
/// The directory a way of reading reads: `tables`, `tables16` (the same | ||
/// entries in the layout before), `dbs` or `db`. A way is named for it; | ||
/// then, for this benchmark's reader, `:` and a check; then `+open` for | ||
/// tables whose files the block cache holds open every one, or `+each` | ||
/// for a reader that opens its file for each block. | ||
fn files(way: &str) -> &str { | ||
way.split(['+', ':']).next().unwrap_or(way) | ||
} | ||
| @@ -99,17 +100,21 @@ impl Store { | ||
fn cells<T>(n: usize) -> Vec<OnceLock<T>> { | ||
(0..n).map(|_| OnceLock::new()).collect() | ||
} | ||
match (files(way), way.split_once(':')) { | ||
("tables" | "tables4", Some((_, check))) => Store::Own { | ||
let (named, flag) = way.split_once('+').unwrap_or((way, "")); | ||
match (files(way), named.split_once(':')) { | ||
("tables" | "tables16", Some((_, check))) => Store::Own { | ||
dir, | ||
readers: cells(accounts), | ||
check: Check::named(check).expect("a check"), | ||
each: flag == "each", | ||
}, | ||
("tables" | "tables4", None) => Store::Tables { | ||
("tables", None) => Store::Tables { | ||
dir, | ||
tables: cells(accounts), | ||
held: way.ends_with("+held"), | ||
cache: BlockCache::new(CACHE), | ||
cache: match flag { | ||
"open" => BlockCache::with_open(CACHE, accounts), | ||
_ => BlockCache::new(CACHE), | ||
}, | ||
}, | ||
("dbs", _) => Store::Dbs { | ||
dir, | ||
| @@ -142,28 +147,15 @@ impl Store { | ||
} | ||
| ||
/// The account's table, opened if it is not: its index and bloom | ||
/// filter read, and its file with it if it is held. | ||
fn table(&self, account: usize) -> (&Table, Option<&File>, &BlockCache) { | ||
let Store::Tables { | ||
dir, | ||
tables, | ||
held, | ||
cache, | ||
} = self | ||
else { | ||
/// filter read, and its file one of those the cache holds open. | ||
fn table(&self, account: usize) -> (&Table, &BlockCache) { | ||
let Store::Tables { dir, tables, cache } = self else { | ||
unreachable!("no table") | ||
}; | ||
let (table, file) = tables[account].get_or_init(|| { | ||
let path = dir.join(format!("{account}.table")); | ||
if *held { | ||
let file = File::open(&path).expect("the account's table"); | ||
let table = Table::open_file(&path, &file).expect("the account's table"); | ||
(table, Some(file)) | ||
} else { | ||
(Table::open(&path).expect("the account's table"), None) | ||
} | ||
let table = tables[account].get_or_init(|| { | ||
Table::open(&dir.join(format!("{account}.table")), cache).expect("the account's table") | ||
}); | ||
(table, file.as_ref(), cache) | ||
(table, cache) | ||
} | ||
| ||
/// The account's table in this benchmark's reader, opened if it is not. | ||
| @@ -172,12 +164,15 @@ impl Store { | ||
dir, | ||
readers, | ||
check, | ||
each, | ||
} = self | ||
else { | ||
unreachable!("no reader") | ||
}; | ||
readers[account] | ||
.get_or_init(|| Reader::open(&dir.join(format!("{account}.table")), *check, true)) | ||
readers[account].get_or_init(|| { | ||
let reader = Reader::open(&dir.join(format!("{account}.table")), *check, true); | ||
if *each { reader.opening_each() } else { reader } | ||
}) | ||
} | ||
| ||
/// The one RocksDB, opened if it is not. | ||
| @@ -242,13 +237,13 @@ impl Store { | ||
let path_key = key(b'P', path.as_bytes()); | ||
match self { | ||
Store::Tables { .. } => { | ||
let (t, file, cache) = self.table(account); | ||
let (t, cache) = self.table(account); | ||
let at = t | ||
.get_in(&path_key, file, cache) | ||
.get(&path_key, cache) | ||
.expect("the table read") | ||
.expect("the path"); | ||
let block = t | ||
.get_in(&key(b'B', path_cid(&at)), file, cache) | ||
.get(&key(b'B', path_cid(&at)), cache) | ||
.expect("the table read") | ||
.expect("the record's block"); | ||
block_bytes(&block).to_vec() | ||
| @@ -307,14 +302,14 @@ impl Store { | ||
let from = key(b'P', path.as_bytes()); | ||
match self { | ||
Store::Tables { .. } => { | ||
let (t, file, cache) = self.table(account); | ||
for e in t.iter(&from, file, cache).take(LISTED) { | ||
let (t, cache) = self.table(account); | ||
for e in t.iter(&from, None, cache).take(LISTED) { | ||
let (k, at) = e.expect("the table read"); | ||
if k[0] != b'P' { | ||
break; | ||
} | ||
let block = t | ||
.get_in(&key(b'B', path_cid(&at)), file, cache) | ||
.get(&key(b'B', path_cid(&at)), cache) | ||
.expect("the table read") | ||
.expect("the record's block"); | ||
n += 1; | ||
| @@ -559,7 +554,11 @@ pub fn reads(accounts: usize, records: usize) { | ||
stores::build(&dir, accounts, records); | ||
}); | ||
let stores = [ | ||
("tables", "a table for each account, one block cache", ""), | ||
( | ||
"tables", | ||
"a table for each account, as the server reads one", | ||
"", | ||
), | ||
( | ||
"dbs", | ||
"SQLite for each account", | ||
| @@ -647,18 +646,20 @@ pub fn reads(accounts: usize, records: usize) { | ||
); | ||
} | ||
| ||
// The tables again in blocks of 4 KiB, and each way of reading either. | ||
std::fs::create_dir_all(dir.join("tables4")).expect("a directory"); | ||
// The tables' entries again in the layout before, and each other way | ||
// of reading a table. | ||
std::fs::create_dir_all(dir.join("tables16")).expect("a directory"); | ||
par(accounts, cores(), |a| { | ||
let table = std::fs::read(stores::table_path(&dir, a)).expect("a table"); | ||
std::fs::write( | ||
dir.join(format!("tables4/{a}.table")), | ||
tables::reblocked(&table, 4 << 10), | ||
dir.join(format!("tables16/{a}.table")), | ||
tables::reblocked(&table, 16 << 10), | ||
) | ||
.expect("a table"); | ||
}); | ||
let blocks = format!("{} KiB", BLOCK >> 10); | ||
println!( | ||
"\nThe tables read other ways: by `pds_store::table::Table`, its file opened for each block it reads (as above) or held open, and by this benchmark's reader, which holds the file open, keeps the blocks it reads in a map for each table where `Table` has one cache and one lock for all, has no bloom filter, and computes the check named over each block it reads (compared with what the file holds for SHA-256 alone); each from the server's blocks of 16 KiB and from the same entries in blocks of 4 KiB. The first read of an account is its table opened and a record read.\n" | ||
"\nThe tables read other ways. By `pds_store::table::Table`: as the server reads one (as above), its block cache holding at most {OPEN} of the tables' files open, the least recently read closed to make room and opened again when next read; and with every file held open, which the server does not do. By this benchmark's reader, which holds every file open, keeps the blocks it reads in a map for each table where `Table` has one cache for all, has no bloom filter, and computes the check named over each block it reads (compared with what the file holds for SHA-256 alone). And by that reader from the layout before: the same entries in blocks of 16 KiB, each block's last key whole in the index, the file opened for each block read. The first read of an account is its table opened and a record read.\n" | ||
); | ||
println!( | ||
"| read by | blocks | check | file | a record by path, cold: p50 µs | p99 µs | a listing of {LISTED}, cold: p50 µs | p99 µs | first read of an account: p50 µs | p99 µs | on {THREADS} threads, cold: per s | warm: per s |" | ||
| @@ -677,34 +678,29 @@ pub fn reads(accounts: usize, records: usize) { | ||
threads["warm"] | ||
); | ||
}; | ||
for (files, blocks) in [("tables", "16 KiB"), ("tables4", "4 KiB")] { | ||
let mut ways = vec![ | ||
( | ||
files.to_string(), | ||
format!("`Table` | {blocks} | SHA-256 | opened for each block"), | ||
), | ||
( | ||
format!("{files}+held"), | ||
format!("`Table` | {blocks} | SHA-256 | held open"), | ||
), | ||
]; | ||
ways.extend(Check::ALL.map(|c| { | ||
( | ||
format!("{files}:{}", c.name()), | ||
format!("this benchmark's | {blocks} | {} | held open", c.name()), | ||
) | ||
})); | ||
for (name, cells) in ways { | ||
if name == "tables" { | ||
way(&cells, &figures[0]); | ||
} else { | ||
way( | ||
&cells, | ||
&["get", "threads", "list"] | ||
.map(|test| measure(&dir, &name, test, accounts, records)), | ||
); | ||
} | ||
} | ||
way( | ||
&format!("`Table`, as the server's | {blocks} | SHA-256 | at most {OPEN} held open"), | ||
&figures[0], | ||
); | ||
let mut ways = vec![( | ||
"tables+open".to_string(), | ||
format!("`Table`, not as the server's | {blocks} | SHA-256 | all held open"), | ||
)]; | ||
ways.extend(Check::ALL.map(|c| { | ||
( | ||
format!("tables:{}", c.name()), | ||
format!("this benchmark's | {blocks} | {} | all held open", c.name()), | ||
) | ||
})); | ||
ways.push(( | ||
format!("tables16:{}+each", Check::Sha256.name()), | ||
"this benchmark's, the layout before | 16 KiB, last keys whole | SHA-256 | opened for each block".to_string(), | ||
)); | ||
for (name, cells) in ways { | ||
way( | ||
&cells, | ||
&["get", "threads", "list"].map(|test| measure(&dir, &name, test, accounts, records)), | ||
); | ||
} | ||
for ((_, name, cells), figures) in stores.iter().zip(&figures).skip(1) { | ||
way(&format!("{name} | {cells}"), figures); | ||
bench/store/src/size.rs+2−1
| @@ -460,9 +460,10 @@ pub fn size(accounts: usize, records: usize) { | ||
} | ||
| ||
println!( | ||
"\nThe tables' data blocks, {:.0} MiB in {} blocks, compressed one at a time on one thread; the dictionary is {} bytes, trained on each {SAMPLE}th block and counted once above. A record is read by its path through two blocks, each read from its file (in the system's cache), its check computed, and searched: the median of {READS}.\n", | ||
"\nThe tables' data blocks, {:.0} MiB in {} blocks, each ended at {} KiB as the server's writer ends them, compressed one at a time on one thread; the dictionary is {} bytes, trained on each {SAMPLE}th block and counted once above. A record is read by its path through two blocks, each read from its file (in the system's cache), its check computed, and searched: the median of {READS}.\n", | ||
mib(plain), | ||
blocks.len(), | ||
pds_store::table::BLOCK >> 10, | ||
dictionary.len() | ||
); | ||
println!( | ||
bench/store/src/tables.rs+33−17
| @@ -1,8 +1,9 @@ | ||
//! A table's file (`pds_store::table`) taken apart and put together other | ||
//! ways, to measure what the server's does not do: its blocks compressed, | ||
//! its blocks smaller, and a reader with a check of its choice that holds | ||
//! its file open. What these write, `pds_store::table::Table` reads, but | ||
//! for blocks compressed; what the reader reads is any of them. | ||
//! its blocks of another size with each one's last key whole in the index, | ||
//! and a reader with a check of its choice that holds its file open, or | ||
//! opens it for each block. What these write, `pds_store::table::Table` | ||
//! reads, but for blocks compressed; what the reader reads is any of them. | ||
| ||
use crate::check::Check; | ||
use pds_store::table::MAGIC; | ||
| @@ -10,7 +11,7 @@ use std::collections::HashMap; | ||
use std::fs::File; | ||
use std::hint::black_box; | ||
use std::os::unix::fs::FileExt; | ||
use std::path::Path; | ||
use std::path::{Path, PathBuf}; | ||
use std::sync::{Arc, Mutex, PoisonError}; | ||
| ||
const FOOTER: usize = 40; | ||
| @@ -53,8 +54,9 @@ fn entries(block: &[u8]) -> impl Iterator<Item = (&[u8], &[u8], std::ops::Range< | ||
}) | ||
} | ||
| ||
/// One data block's place: its last key, its offset and its length, check | ||
/// and all. | ||
/// One data block's place: its key in the index (its last key, or a | ||
/// shorter one after it and before the next block's first), its offset | ||
/// and its length, check and all. | ||
struct Place { | ||
last: Vec<u8>, | ||
offset: u64, | ||
| @@ -144,7 +146,8 @@ pub fn packed(table: &[u8], mut pack: impl FnMut(&[u8]) -> Vec<u8>) -> Vec<u8> { | ||
} | ||
| ||
/// The same table in blocks of `size` bytes, ended as | ||
/// `pds_store::table::TableWriter` ends its own of 16 KiB. | ||
/// `pds_store::table::TableWriter` ends its own, each block's last key | ||
/// whole in the index: with a `size` of 16 KiB, the layout before. | ||
pub fn reblocked(table: &[u8], size: usize) -> Vec<u8> { | ||
let mut again = Again::default(); | ||
let mut block: Vec<u8> = Vec::with_capacity(size * 2); | ||
| @@ -169,16 +172,18 @@ pub fn reblocked(table: &[u8], size: usize) -> Vec<u8> { | ||
again.finish(table) | ||
} | ||
| ||
/// A table read by this benchmark: its file held open and its index held, | ||
/// no bloom filter, each block read checked with the check of its choice, | ||
/// unpacked if it is stored compressed, and searched. It keeps every block | ||
/// it has read, or none: with none, every read is of a block from the | ||
/// file. | ||
/// A table read by this benchmark: its index held, no bloom filter, each | ||
/// block read checked with the check of its choice, unpacked if it is | ||
/// stored compressed, and searched. Its file is held open, or opened for | ||
/// each block it reads. It keeps every block it has read, or none: with | ||
/// none, every read is of a block from the file. | ||
/// | ||
/// The files hold SHA-256's checks. Another check is computed over the | ||
/// same bytes and compared with nothing: what it costs, not what it finds. | ||
pub struct Reader { | ||
file: File, | ||
path: PathBuf, | ||
/// The file, if it is held open. | ||
file: Option<File>, | ||
places: Vec<Place>, | ||
check: Check, | ||
kept: Option<Mutex<HashMap<usize, Arc<[u8]>>>>, | ||
| @@ -198,8 +203,9 @@ impl Reader { | ||
let mut index = vec![0u8; index_len as usize]; | ||
file.read_exact_at(&mut index, index_at).expect("an index"); | ||
let r = Reader { | ||
path: path.to_path_buf(), | ||
places: places(&index), | ||
file, | ||
file: Some(file), | ||
check, | ||
kept: keep.then(Mutex::default), | ||
}; | ||
| @@ -208,6 +214,12 @@ impl Reader { | ||
r | ||
} | ||
| ||
/// The same reader with its file closed: opened for each block it reads. | ||
pub fn opening_each(mut self) -> Reader { | ||
self.file = None; | ||
self | ||
} | ||
| ||
/// Compute the check of `bytes`, and see that it is `stored` if that | ||
/// is the check the files hold. | ||
fn checked(&self, bytes: &[u8], stored: &[u8]) { | ||
| @@ -219,9 +231,13 @@ impl Reader { | ||
fn read(&self, i: usize, unpack: Unpack) -> Vec<u8> { | ||
let p = &self.places[i]; | ||
let mut stored = vec![0u8; p.len]; | ||
self.file | ||
.read_exact_at(&mut stored, p.offset) | ||
.expect("a block"); | ||
match &self.file { | ||
Some(file) => file.read_exact_at(&mut stored, p.offset), | ||
None => File::open(&self.path) | ||
.expect("a table") | ||
.read_exact_at(&mut stored, p.offset), | ||
} | ||
.expect("a block"); | ||
let (body, c) = stored.split_at(p.len - 8); | ||
self.checked(body, c); | ||
match unpack { | ||
bench/store/README.md+48−39
| @@ -288,8 +288,10 @@ them, kept these ways with the same content (`src/stores.rs`): | ||
| ||
- a table for each account as the server writes its checkpoint | ||
(`pds_store::table::TableWriter`): under `B` and a CID a block with its | ||
count and rev, under `P` and a path its record's CID and rev; read with | ||
`Table::get` and `Table::iter` through one `BlockCache` for all; | ||
count and rev, under `P` and a path its record's CID and rev, in data | ||
blocks of 4 KiB; read as the server reads one, with `Table::get` and | ||
`Table::iter` through one `BlockCache` for all, which keeps the blocks | ||
read and holds at most 64 of the tables' files open; | ||
- SQLite for each account, the first part's `block` and `record` tables, | ||
one connection each; | ||
- one SQLite for all, a connection for each thread that reads; | ||
| @@ -340,46 +342,53 @@ would keep. Measured: | ||
and one record read from each, and after the 20,000 records; | ||
- bytes on disk. | ||
| ||
The table computes each block's check as it reads it, 16 KiB at a time, | ||
and opens the file for each block it has not cached, as `Table::get` does | ||
given no file; SQLite checks nothing and reads 4 KiB pages from files it | ||
holds open. RocksDB reads blocks of 4 KiB before compression from files | ||
it holds open, all of them from when it is opened, and computes each | ||
block's check, XXH3, as it reads it; each of its files has a bloom | ||
filter of 10 bits a key, and its indexes and filters are held outside | ||
the block cache, as they are unless it is told otherwise. The databases | ||
get their rows in the order commits would give | ||
them and are not vacuumed, so their pages are part empty, as a database | ||
written over time has them; a table is written whole and dense. The | ||
`block` table is `WITHOUT ROWID`, as in the first part, which costs SQLite | ||
disk and not reads: see the next part. | ||
| ||
On 16 threads, the table's threads share the one block cache and its one | ||
lock; RocksDB's share its block cache, which is in 64 parts, each with a | ||
lock. The account databases have one connection each, behind a lock of | ||
its own, which a thread holds for a read: 16 threads over 2000 accounts | ||
seldom want the same one, and a connection for each thread and account | ||
would be 32,000 of them. The one database has a read connection for each | ||
thread, in WAL mode, its statement prepared once and kept. SQLite is | ||
built for it (`.cargo/config.toml`): without `rusqlite`'s | ||
The table reads blocks of 4 KiB and computes each one's check, SHA-256, | ||
as it reads it, from a file its block cache holds open: of the 2000 | ||
tables' files at most 64 are open at once, as in the server, the least | ||
recently read closed to make room, so that a read of an account whose | ||
file is not among them opens it again, once for the blocks it then reads. | ||
A read of a block the cache holds asks the bloom filter nothing; one that | ||
must read the file asks it first. SQLite checks nothing and reads 4 KiB | ||
pages from files it holds open. RocksDB reads blocks of 4 KiB before | ||
compression from files it holds open, all of them from when it is opened, | ||
and computes each block's check, XXH3, as it reads it; each of its files | ||
has a bloom filter of 10 bits a key, and its indexes and filters are held | ||
outside the block cache, as they are unless it is told otherwise. The | ||
databases get their rows in the order commits would give them and are not | ||
vacuumed, so their pages are part empty, as a database written over time | ||
has them; a table is written whole and dense. The `block` table is | ||
`WITHOUT ROWID`, as in the first part, which costs SQLite disk and not | ||
reads: see the next part. | ||
| ||
On 16 threads, the table's threads share the one block cache, which is in | ||
64 parts, each with a lock, and RocksDB's share its block cache, in 64 | ||
parts likewise. The account databases have one connection each, behind a | ||
lock of its own, which a thread holds for a read: 16 threads over 2000 | ||
accounts seldom want the same one, and a connection for each thread and | ||
account would be 32,000 of them. The one database has a read connection | ||
for each thread, in WAL mode, its statement prepared once and kept. SQLite | ||
is built for it (`.cargo/config.toml`): without `rusqlite`'s | ||
`SQLITE_ENABLE_MEMORY_MANAGEMENT`, which puts every connection's page | ||
cache behind one lock, and with `SQLITE_DEFAULT_MEMSTATUS=0`, which | ||
takes its count of allocations from behind another. | ||
| ||
A last table reads the tables cold other ways, to tell what of a cold | ||
read is which choice: with each table's file held open | ||
(`Table::get_in` and `Table::iter` take one: nothing of `pds_store::table` | ||
changes, only its caller); from the same entries in blocks of 4 KiB, | ||
written by this benchmark and read by `Table` as it is (it is | ||
`TableWriter`'s `BLOCK` that would change); and by this benchmark's | ||
reader (`src/tables.rs`), which holds the file open, keeps the blocks it | ||
reads in a map of each table's own, where `Table` has the one cache and | ||
its one lock for all, has no bloom filter, and computes one of four | ||
checks over each block (above): another check than SHA-256 is a change | ||
to the table's format, its writer and its reader, and a cache without | ||
one lock a change to `BlockCache`. Per way: a record by its path, a | ||
listing of 50 and the first read of an account, cold, and records per | ||
second from 16 threads, cold and warm. | ||
read is which choice. By `Table` with every table's file held open | ||
(`BlockCache::with_open`), which the server does not do: what the limit | ||
of 64 open files costs. By this benchmark's reader (`src/tables.rs`) | ||
from the same files, which holds every file open, keeps the blocks it | ||
reads in a map of each table's own, where `Table` has the one cache for | ||
all, has no bloom filter, and computes one of four checks over each | ||
block (above): another check than SHA-256 is a change to the table's | ||
format, its writer and its reader. And by that reader from the layout | ||
before, written by this benchmark from the same entries | ||
(`tables::reblocked`): blocks of 16 KiB, each block's last key whole in | ||
the index, the file opened for each block read. That row has the layout | ||
and the opens of the table as it was, and neither its bloom filter nor | ||
the one lock its block cache had, so its figures from 16 threads are not | ||
that table's. Per way: a record by its path, a listing of 50 and the | ||
first read of an account, cold, and records per second from 16 threads, | ||
cold and warm. | ||
| ||
**Size and compression** (`store-bench size <accounts> <records>`). 200 | ||
accounts of 500 realistic commits each, a record a commit, and what they | ||
| @@ -392,8 +401,8 @@ take on disk: | ||
what the commits leave, the records and the tree, and no event; | ||
- as the tables with each data block compressed with zstd at level 3, and | ||
with a dictionary of 64 KiB trained on each eighth block, counted once. | ||
A block is 16 KiB before, the index, bloom filter and footer as they | ||
were; | ||
A block is 4 KiB before, as the writer ends it, the index, bloom filter | ||
and footer as they were; | ||
- SQLite after every record is rewritten three times, each rewrite a | ||
transaction as a commit's (the record's block and each node's from it to | ||
the root taken out, new ones put in, the path's row replaced), as that | ||
bench/store/RESULTS.md+138−139
| @@ -9,13 +9,15 @@ Across both machines: | ||
on Linux, where the server is deployed. One RocksDB for all, tuned, is | ||
level with them or a little ahead; fjall as it comes is far behind, for | ||
want of a shared sync. | ||
- What the server's store loses is in how it reads and syncs, not in what | ||
it is: a table's file opened for each block, blocks of 16 KiB, SHA-256 | ||
as the check, a block cache behind one lock, a log that grows under each | ||
sync, and frames of JSON with hex. The rows that change one of these | ||
each say what it is worth. | ||
- From its cache a table is read faster than any other store here, on one | ||
thread and on sixteen, and an account touched holds no more than with | ||
RocksDB. | ||
- What the server's store loses is in how it reads what is not in its | ||
cache, and in how it syncs: SHA-256 as the check, 64 tables' files open | ||
at most, a log that grows under each sync, and frames of JSON with hex. | ||
The rows that change one of these each say what it is worth. | ||
- One file set for each account costs the first read of an account a file | ||
to open, and more memory an account touched, than one store for all. | ||
to open, where one store for all has none. | ||
| ||
## The runner, Linux | ||
| ||
| @@ -25,7 +27,8 @@ The `store-bench` workflow's run on `runner.knotless.dev`, in the image | ||
- Linux 6.8.0-146-generic x86_64, 12 cores, load 6.86; its files on the | ||
run's overlay over the runner's disk. Every sync is `fdatasync` | ||
(`sync_data`), RocksDB's and fjall's too. | ||
- cargo 1.98.1; node v24.22.1; the repository at `82bf3b3`. | ||
- cargo 1.98.1; node v24.22.1; the repository at `82bf3b3`, and for the | ||
reads and the sizes at `501762b`, beside the `gates` workflow's runs. | ||
| ||
### Writes | ||
| ||
| @@ -340,71 +343,69 @@ The events of 200000 realistic commits over 2000 accounts. A whole replay is the | ||
| ||
| store | a record by path, cold: p50 µs | p99 µs | warm: p50 µs | p99 µs | on 16 threads, cold: per s | warm: per s | | ||
|---|--:|--:|--:|--:|--:|--:| | ||
| a table for each account, one block cache | 27.1 | 63.7 | 4.2 | 6.6 | 148548 | 337125 | | ||
| SQLite for each account | 15.2 | 23.1 | 11.0 | 15.3 | 105127 | 256206 | | ||
| one SQLite for all | 15.2 | 23.2 | 8.1 | 12.2 | 59139 | 394604 | | ||
| one RocksDB for all, its blocks compressed (Snappy) | 17.6 | 28.9 | 5.0 | 7.3 | 159599 | 693856 | | ||
| one RocksDB for all, its blocks as they are | 12.6 | 20.8 | 5.0 | 7.4 | 181302 | 443008 | | ||
| one fjall for all, its blocks compressed below the second level (LZ4) | 10.5 | 15.9 | 4.7 | 6.3 | 231701 | 429569 | | ||
| one fjall for all, its blocks as they are | 10.5 | 15.9 | 4.7 | 6.2 | 249960 | 429515 | | ||
| a table for each account, as the server reads one | 21.5 | 29.0 | 1.6 | 2.6 | 153126 | 993594 | | ||
| SQLite for each account | 15.4 | 24.2 | 11.1 | 16.0 | 103237 | 256713 | | ||
| one SQLite for all | 16.2 | 26.1 | 8.7 | 14.0 | 63478 | 388229 | | ||
| one RocksDB for all, its blocks compressed (Snappy) | 18.1 | 30.8 | 5.1 | 8.1 | 162561 | 628424 | | ||
| one RocksDB for all, its blocks as they are | 12.6 | 21.8 | 5.1 | 7.8 | 178816 | 451693 | | ||
| one fjall for all, its blocks compressed below the second level (LZ4) | 11.0 | 18.6 | 4.9 | 6.8 | 235663 | 447690 | | ||
| one fjall for all, its blocks as they are | 11.0 | 18.7 | 4.9 | 6.7 | 251155 | 465581 | | ||
| ||
| store | a listing of 50 records, cold: p50 µs | p99 µs | warm: p50 µs | p99 µs | first read of an account not yet open: p50 µs | p99 µs | | ||
|---|--:|--:|--:|--:|--:|--:| | ||
| a table for each account, one block cache | 404 | 478 | 65 | 78 | 65 | 84 | | ||
| SQLite for each account | 538 | 634 | 76 | 139 | 407 | 496 | | ||
| one SQLite for all | 219 | 306 | 86 | 164 | 21 | 51 | | ||
| one RocksDB for all, its blocks compressed (Snappy) | 278 | 588 | 94 | 108 | 32 | 55 | | ||
| one RocksDB for all, its blocks as they are | 187 | 238 | 93 | 104 | 14 | 20 | | ||
| one fjall for all, its blocks compressed below the second level (LZ4) | 211 | 263 | 106 | 115 | 12 | 20 | | ||
| one fjall for all, its blocks as they are | 211 | 293 | 106 | 115 | 12 | 20 | | ||
| a table for each account, as the server reads one | 236 | 414 | 34 | 40 | 29 | 38 | | ||
| SQLite for each account | 547 | 921 | 82 | 170 | 413 | 691 | | ||
| one SQLite for all | 225 | 410 | 89 | 168 | 23 | 43 | | ||
| one RocksDB for all, its blocks compressed (Snappy) | 280 | 411 | 96 | 131 | 29 | 62 | | ||
| one RocksDB for all, its blocks as they are | 196 | 338 | 96 | 142 | 14 | 23 | | ||
| one fjall for all, its blocks compressed below the second level (LZ4) | 217 | 339 | 109 | 137 | 12 | 23 | | ||
| one fjall for all, its blocks as they are | 216 | 306 | 108 | 138 | 12 | 22 | | ||
| ||
| store | resident before any is open, MiB | with 2000 open, a record read from each, MiB | KiB an account | after 20000 more records, twice, MiB | MiB on disk | KiB an account | | ||
|---|--:|--:|--:|--:|--:|--:| | ||
| a table for each account, one block cache | 12.0 | 80.0 | 34.8 | 339.7 | 503 | 257 | | ||
| SQLite for each account | 12.3 | 372.2 | 184.2 | 426.8 | 870 | 445 | | ||
| one SQLite for all | 12.2 | 48.9 | 18.8 | 211.9 | 841 | 430 | | ||
| one RocksDB for all, its blocks compressed (Snappy) | 12.3 | 37.0 | 12.7 | 154.7 | 339 | 174 | | ||
| one RocksDB for all, its blocks as they are | 12.2 | 36.8 | 12.6 | 154.7 | 498 | 255 | | ||
| one fjall for all, its blocks compressed below the second level (LZ4) | 12.2 | 39.4 | 13.9 | 155.0 | 500 | 256 | | ||
| one fjall for all, its blocks as they are | 12.1 | 39.3 | 13.9 | 154.8 | 500 | 256 | | ||
| a table for each account, as the server reads one | 12.1 | 34.5 | 11.5 | 153.0 | 504 | 258 | | ||
| SQLite for each account | 12.0 | 372.4 | 184.5 | 427.0 | 870 | 445 | | ||
| one SQLite for all | 12.1 | 48.8 | 18.8 | 211.8 | 841 | 430 | | ||
| one RocksDB for all, its blocks compressed (Snappy) | 11.9 | 36.7 | 12.7 | 154.4 | 339 | 174 | | ||
| one RocksDB for all, its blocks as they are | 12.2 | 36.7 | 12.6 | 154.7 | 498 | 255 | | ||
| one fjall for all, its blocks compressed below the second level (LZ4) | 12.1 | 39.4 | 13.9 | 154.9 | 500 | 256 | | ||
| one fjall for all, its blocks as they are | 12.1 | 39.3 | 13.9 | 154.9 | 500 | 256 | | ||
| ||
The tables read other ways: by `pds_store::table::Table`, its file opened for each block it reads (as above) or held open, and by this benchmark's reader, which holds the file open, keeps the blocks it reads in a map for each table where `Table` has one cache and one lock for all, has no bloom filter, and computes the check named over each block it reads (compared with what the file holds for SHA-256 alone); each from the server's blocks of 16 KiB and from the same entries in blocks of 4 KiB. The first read of an account is its table opened and a record read. | ||
The tables read other ways. By `pds_store::table::Table`: as the server reads one (as above), its block cache holding at most 64 of the tables' files open, the least recently read closed to make room and opened again when next read; and with every file held open, which the server does not do. By this benchmark's reader, which holds every file open, keeps the blocks it reads in a map for each table where `Table` has one cache for all, has no bloom filter, and computes the check named over each block it reads (compared with what the file holds for SHA-256 alone). And by that reader from the layout before: the same entries in blocks of 16 KiB, each block's last key whole in the index, the file opened for each block read. The first read of an account is its table opened and a record read. | ||
| ||
| read by | blocks | check | file | a record by path, cold: p50 µs | p99 µs | a listing of 50, cold: p50 µs | p99 µs | first read of an account: p50 µs | p99 µs | on 16 threads, cold: per s | warm: per s | | ||
|---|---|---|---|--:|--:|--:|--:|--:|--:|--:|--:| | ||
| `Table` | 16 KiB | SHA-256 | opened for each block | 27.1 | 63.7 | 404 | 478 | 65 | 84 | 148548 | 337125 | | ||
| `Table` | 16 KiB | SHA-256 | held open | 20.3 | 47.0 | 309 | 498 | 51 | 70 | 172054 | 334801 | | ||
| this benchmark's | 16 KiB | SHA-256 | held open | 19.5 | 47.1 | 288 | 352 | 50 | 65 | 274891 | 978738 | | ||
| this benchmark's | 16 KiB | CRC32C | held open | 12.4 | 32.8 | 188 | 291 | 35 | 50 | 344291 | 955519 | | ||
| this benchmark's | 16 KiB | XXH3-64 | held open | 12.5 | 32.6 | 185 | 256 | 35 | 62 | 323890 | 856551 | | ||
| this benchmark's | 16 KiB | BLAKE3 | held open | 17.4 | 42.9 | 257 | 333 | 46 | 61 | 257341 | 771514 | | ||
| `Table` | 4 KiB | SHA-256 | opened for each block | 23.5 | 30.6 | 451 | 538 | 46 | 53 | 125191 | 358854 | | ||
| `Table` | 4 KiB | SHA-256 | held open | 11.5 | 17.9 | 243 | 379 | 33 | 57 | 150537 | 358366 | | ||
| this benchmark's | 4 KiB | SHA-256 | held open | 11.1 | 17.2 | 223 | 285 | 31 | 52 | 205441 | 3423572 | | ||
| this benchmark's | 4 KiB | CRC32C | held open | 8.1 | 13.6 | 164 | 213 | 26 | 32 | 204529 | 3434678 | | ||
| this benchmark's | 4 KiB | XXH3-64 | held open | 8.0 | 13.5 | 162 | 251 | 26 | 34 | 219535 | 3384139 | | ||
| this benchmark's | 4 KiB | BLAKE3 | held open | 12.4 | 18.6 | 249 | 399 | 33 | 57 | 208145 | 938655 | | ||
| SQLite for each account | 4 KiB pages | none | held open | 15.2 | 23.1 | 538 | 634 | 407 | 496 | 105127 | 256206 | | ||
| one SQLite for all | 4 KiB pages | none | held open | 15.2 | 23.2 | 219 | 306 | 21 | 51 | 59139 | 394604 | | ||
| one RocksDB for all, its blocks compressed (Snappy) | 4 KiB before Snappy | XXH3 | held open | 17.6 | 28.9 | 278 | 588 | 32 | 55 | 159599 | 693856 | | ||
| one RocksDB for all, its blocks as they are | 4 KiB | XXH3 | held open | 12.6 | 20.8 | 187 | 238 | 14 | 20 | 181302 | 443008 | | ||
| one fjall for all, its blocks compressed below the second level (LZ4) | 4 KiB before LZ4 | XXH3 | held open | 10.5 | 15.9 | 211 | 263 | 12 | 20 | 231701 | 429569 | | ||
| one fjall for all, its blocks as they are | 4 KiB | XXH3 | held open | 10.5 | 15.9 | 211 | 293 | 12 | 20 | 249960 | 429515 | | ||
| ||
- As the server reads a table, a record not in its cache takes 27 µs: | ||
RocksDB 12.6, fjall 10.5, SQLite 15.2. With the table's file held open | ||
and blocks of 4 KiB it takes 11.5, and with a fast check 8.0. A listing | ||
of 50 goes from 404 µs to 243 and 162 (RocksDB 187, fjall 211, one SQLite | ||
219). | ||
- From its cache the table is the fastest on one thread (4.2 µs; RocksDB | ||
5.0, fjall 4.7, SQLite 8.1 to 11.0), and its listing too (65 µs against | ||
93 to 106). On 16 threads it is the slowest but SQLite for each account | ||
(337 thousand a second; RocksDB 443, fjall 430, one SQLite 395): its | ||
block cache is behind one lock. | ||
- The first read of an account costs the table a file to open: 65 µs, | ||
and 26 at best, where one RocksDB, fjall or SQLite for all has nothing | ||
to open (14, 12, 21). SQLite for each account takes 407. | ||
- An account touched holds 34.8 KiB with tables, 12.6 with RocksDB, 13.9 | ||
| `Table`, as the server's | 4 KiB | SHA-256 | at most 64 held open | 21.5 | 29.0 | 236 | 414 | 29 | 38 | 153126 | 993594 | | ||
| `Table`, not as the server's | 4 KiB | SHA-256 | all held open | 12.2 | 17.9 | 232 | 331 | 28 | 39 | 213580 | 995021 | | ||
| this benchmark's | 4 KiB | SHA-256 | all held open | 11.3 | 18.2 | 226 | 306 | 30 | 40 | 192379 | 957694 | | ||
| this benchmark's | 4 KiB | CRC32C | all held open | 8.3 | 14.6 | 168 | 251 | 26 | 45 | 242389 | 1041515 | | ||
| this benchmark's | 4 KiB | XXH3-64 | all held open | 8.2 | 14.0 | 167 | 255 | 26 | 33 | 205311 | 937135 | | ||
| this benchmark's | 4 KiB | BLAKE3 | all held open | 12.6 | 19.4 | 252 | 310 | 32 | 53 | 200329 | 3272932 | | ||
| this benchmark's, the layout before | 16 KiB, last keys whole | SHA-256 | opened for each block | 26.3 | 63.1 | 382 | 477 | 64 | 84 | 205785 | 773775 | | ||
| SQLite for each account | 4 KiB pages | none | held open | 15.4 | 24.2 | 547 | 921 | 413 | 691 | 103237 | 256713 | | ||
| one SQLite for all | 4 KiB pages | none | held open | 16.2 | 26.1 | 225 | 410 | 23 | 43 | 63478 | 388229 | | ||
| one RocksDB for all, its blocks compressed (Snappy) | 4 KiB before Snappy | XXH3 | held open | 18.1 | 30.8 | 280 | 411 | 29 | 62 | 162561 | 628424 | | ||
| one RocksDB for all, its blocks as they are | 4 KiB | XXH3 | held open | 12.6 | 21.8 | 196 | 338 | 14 | 23 | 178816 | 451693 | | ||
| one fjall for all, its blocks compressed below the second level (LZ4) | 4 KiB before LZ4 | XXH3 | held open | 11.0 | 18.6 | 217 | 339 | 12 | 23 | 235663 | 447690 | | ||
| one fjall for all, its blocks as they are | 4 KiB | XXH3 | held open | 11.0 | 18.7 | 216 | 306 | 12 | 22 | 251155 | 465581 | | ||
| ||
- A table as the server reads it now, against one as it did (the last | ||
table's row of the layout before): a record not in its cache 21.5 µs | ||
against 26.3, a listing of 50 236 against 382, the first read of an | ||
account 29 against 64. With every table's file held open a record takes | ||
12.2: the server holds 64 at most, to serve under 256 descriptors | ||
(`spec/LIMITS.md`), and of 2000 accounts read at random most are opened | ||
again. | ||
- From its cache the table is the fastest here: 1.6 µs a record (RocksDB | ||
5.1, fjall 4.9, SQLite 8.7 to 11.1), a listing in 34 (82 to 109), and | ||
on 16 threads 994 thousand records a second (RocksDB 452 to 628, fjall | ||
448 to 466, one SQLite 388). | ||
- Not in its cache it is behind RocksDB and fjall: a record 21.5 µs | ||
against 12.6 and 11.0, a listing 236 against 196 and 216, the first read | ||
of an account 29 against 14 and 12, 16 threads 153 thousand a second | ||
against 179 and 251. A fast check in place of SHA-256 is worth 3 µs a | ||
record and 60 a listing (the rows of this benchmark's own reader). | ||
- An account touched holds 11.5 KiB with tables, 12.6 with RocksDB, 13.9 | ||
with fjall, 18.8 with one SQLite and 184 with SQLite for each account. | ||
| ||
### Size and compression | ||
| @@ -415,9 +416,9 @@ The tables read other ways: by `pds_store::table::Table`, its file opened for ea | ||
|---|--:|--:|--:| | ||
| a log for each account before compaction, JSON frames (the server's) | 1139.9 | 5836 | 11952 | | ||
| a log for each account before compaction, binary frames | 566.9 | 2902 | 5944 | | ||
| a table for each account | 50.3 | 257 | 527 | | ||
| the tables, each block compressed (zstd 3) | 25.3 | 129 | 265 | | ||
| the tables, each block compressed with a dictionary (zstd 3) | 23.8 | 122 | 250 | | ||
| a table for each account | 50.4 | 258 | 529 | | ||
| the tables, each block compressed (zstd 3) | 28.4 | 145 | 298 | | ||
| the tables, each block compressed with a dictionary (zstd 3) | 24.6 | 126 | 258 | | ||
| SQLite for each account | 86.3 | 442 | 905 | | ||
| one SQLite for all | 83.8 | 429 | 878 | | ||
| SQLite for each account, every record rewritten 3 times | 94.4 | 483 | 989 | | ||
| @@ -426,42 +427,43 @@ The tables read other ways: by `pds_store::table::Table`, its file opened for ea | ||
| the same, vacuumed | 75.8 | 388 | 795 | | ||
| SQLite for each account, rewritten and vacuumed, its blocks in a table with a rowid | 63.0 | 322 | 660 | | ||
| one RocksDB for all, its blocks: Snappy | 34.6 | 177 | 363 | | ||
| the same, every record rewritten 3 times | 49.7 | 254 | 521 | | ||
| the same, every record rewritten 3 times | 45.2 | 231 | 474 | | ||
| the same, compacted whole | 34.5 | 177 | 362 | | ||
| one RocksDB for all, its blocks: no compression | 49.8 | 255 | 522 | | ||
| the same, every record rewritten 3 times | 68.5 | 351 | 719 | | ||
| the same, every record rewritten 3 times | 62.0 | 317 | 650 | | ||
| the same, compacted whole | 50.1 | 257 | 526 | | ||
| one RocksDB for all, its blocks: zstd | 29.8 | 153 | 313 | | ||
| the same, every record rewritten 3 times | 43.8 | 225 | 460 | | ||
| the same, compacted whole | 29.7 | 152 | 312 | | ||
| the same, every record rewritten 3 times | 43.6 | 223 | 457 | | ||
| the same, compacted whole | 29.8 | 152 | 312 | | ||
| one fjall for all, its blocks: LZ4 below the second level | 49.9 | 256 | 523 | | ||
| the same, every record rewritten 3 times | 60.1 | 308 | 630 | | ||
| the same, every record rewritten 3 times | 60.2 | 308 | 631 | | ||
| the same, compacted whole | 35.2 | 180 | 369 | | ||
| one fjall for all, its blocks: no compression | 49.9 | 256 | 523 | | ||
| the same, every record rewritten 3 times | 60.1 | 308 | 630 | | ||
| the same, every record rewritten 3 times | 60.2 | 308 | 631 | | ||
| the same, compacted whole | 49.9 | 256 | 524 | | ||
| ||
What RocksDB wrote to its files for what it was given, keys and values as they are, through the load and the rewrites, and then with the whole compaction too: to its log, to the files it writes from memory, and to those its compactions write. | ||
| ||
| RocksDB's blocks | given MiB | to its log MiB | from memory MiB | by compactions MiB | written for each byte given | with the whole compaction MiB | for each byte given | | ||
|---|--:|--:|--:|--:|--:|--:|--:| | ||
| Snappy | 997 | 997 | 127 | 114 | 1.24 | 1272 | 1.28 | | ||
| no compression | 997 | 997 | 158 | 171 | 1.33 | 1375 | 1.38 | | ||
| zstd | 997 | 997 | 113 | 98 | 1.21 | 1237 | 1.24 | | ||
| Snappy | 997 | 997 | 125 | 114 | 1.24 | 1270 | 1.27 | | ||
| no compression | 997 | 997 | 157 | 171 | 1.33 | 1375 | 1.38 | | ||
| zstd | 997 | 997 | 114 | 98 | 1.21 | 1238 | 1.24 | | ||
| ||
The tables' data blocks, 50 MiB in 3364 blocks, compressed one at a time on one thread; the dictionary is 65536 bytes, trained on each 8th block and counted once above. A record is read by its path through two blocks, each read from its file (in the system's cache), its check computed, and searched: the median of 20000. | ||
The tables' data blocks, 50 MiB in 13583 blocks, each ended at 4 KiB as the server's writer ends them, compressed one at a time on one thread; the dictionary is 65536 bytes, trained on each 8th block and counted once above. A record is read by its path through two blocks, each read from its file (in the system's cache), its check computed, and searched: the median of 20000. | ||
| ||
| blocks | stored bytes of each 100 | compressing MiB per s | decompressing MiB per s | a record read µs | | ||
|---|--:|--:|--:|--:| | ||
| as they are | 100 | | | 21.7 | | ||
| compressed (zstd 3) | 50 | 224 | 862 | 37.8 | | ||
| compressed with a dictionary (zstd 3) | 47 | 170 | 878 | 37.2 | | ||
| as they are | 100 | | | 7.5 | | ||
| compressed (zstd 3) | 56 | 178 | 630 | 14.0 | | ||
| compressed with a dictionary (zstd 3) | 48 | 162 | 852 | 11.6 | | ||
| ||
- A table for each account, RocksDB and fjall uncompressed hold the same | ||
data in the same bytes to one part in a hundred (257, 255 and 256 KiB an | ||
account); SQLite's best is 322. With zstd the tables are 129 KiB an | ||
account and RocksDB 153: blocks of 16 KiB compress better than 4. | ||
- A compressed block costs a record read 16 µs more. | ||
data in the same bytes to one part in a hundred (258, 255 and 256 KiB an | ||
account); SQLite's best is 322. With zstd the tables are 145 KiB an | ||
account, 126 with a dictionary, and RocksDB 153. | ||
- A compressed block costs a record read 4 to 7 µs more here, 5 to 8 on | ||
the Mac. | ||
- A log before compaction is 11 times its table with binary frames and 23 | ||
times with JSON. RocksDB wrote 1.21 to 1.33 bytes for each byte it was | ||
given, a floor for it: this database never fills a level above its | ||
| @@ -491,8 +493,8 @@ The tables' data blocks, 50 MiB in 3364 blocks, compressed one at a time on one | ||
- Darwin 25.5.0 arm64, 8 cores, 32 GiB; APFS on the internal SSD | ||
(`/dev/disk3s5`). Every sync is `F_FULLFSYNC`, a flush through the | ||
drive's cache, RocksDB's and fjall's too. | ||
- cargo 1.99.1; node v24.22.1; the repository at `41ca6ca`, and at | ||
`66cde9b` for the last part. | ||
- cargo 1.99.1; node v24.22.1; the repository at `41ca6ca`; at `b571f21` | ||
for the reads and the sizes, and at `66cde9b` for the last part. | ||
- The machine was not alone: its load was 3.6 to 8 through the run, and | ||
23 as the last part began. | ||
| ||
| @@ -768,60 +770,57 @@ The events of 200000 realistic commits over 2000 accounts. A whole replay is the | ||
| ||
| store | a record by path, cold: p50 µs | p99 µs | warm: p50 µs | p99 µs | on 16 threads, cold: per s | warm: per s | | ||
|---|--:|--:|--:|--:|--:|--:| | ||
| a table for each account, one block cache | 28.1 | 82.5 | 4.4 | 9.6 | 130910 | 185419 | | ||
| SQLite for each account | 10.9 | 24.5 | 7.9 | 22.0 | 118539 | 338127 | | ||
| one SQLite for all | 9.6 | 62.6 | 5.7 | 11.2 | 114130 | 431119 | | ||
| one RocksDB for all, its blocks compressed (Snappy) | 9.0 | 18.7 | 4.5 | 9.6 | 541463 | 1003040 | | ||
| one RocksDB for all, its blocks as they are | 7.0 | 15.0 | 4.4 | 9.2 | 579530 | 1016869 | | ||
| one fjall for all, its blocks compressed below the second level (LZ4) | 7.0 | 17.8 | 4.8 | 10.2 | 472218 | 738215 | | ||
| one fjall for all, its blocks as they are | 7.2 | 43.1 | 4.8 | 10.0 | 448754 | 734144 | | ||
| a table for each account, as the server reads one | 27.6 | 63.8 | 2.2 | 5.9 | 98438 | 1799876 | | ||
| SQLite for each account | 12.9 | 31.6 | 8.9 | 16.0 | 99132 | 307460 | | ||
| one SQLite for all | 11.0 | 80.6 | 6.7 | 13.1 | 91848 | 402958 | | ||
| one RocksDB for all, its blocks compressed (Snappy) | 11.0 | 22.0 | 5.3 | 12.2 | 401988 | 833919 | | ||
| one RocksDB for all, its blocks as they are | 9.1 | 33.3 | 5.8 | 13.4 | 326502 | 830714 | | ||
| one fjall for all, its blocks compressed below the second level (LZ4) | 8.9 | 39.7 | 6.1 | 14.5 | 341002 | 595908 | | ||
| one fjall for all, its blocks as they are | 8.9 | 47.4 | 6.0 | 12.9 | 386496 | 659354 | | ||
| ||
| store | a listing of 50 records, cold: p50 µs | p99 µs | warm: p50 µs | p99 µs | first read of an account not yet open: p50 µs | p99 µs | | ||
|---|--:|--:|--:|--:|--:|--:| | ||
| a table for each account, one block cache | 374 | 518 | 61 | 95 | 66 | 148 | | ||
| SQLite for each account | 336 | 574 | 58 | 96 | 272 | 510 | | ||
| one SQLite for all | 117 | 193 | 65 | 112 | 21 | 118 | | ||
| one RocksDB for all, its blocks compressed (Snappy) | 132 | 335 | 72 | 104 | 10 | 20 | | ||
| one RocksDB for all, its blocks as they are | 106 | 162 | 71 | 98 | 8 | 17 | | ||
| one fjall for all, its blocks compressed below the second level (LZ4) | 134 | 200 | 100 | 140 | 7 | 15 | | ||
| one fjall for all, its blocks as they are | 131 | 207 | 101 | 145 | 8 | 19 | | ||
| a table for each account, as the server reads one | 202 | 446 | 40 | 67 | 35 | 72 | | ||
| SQLite for each account | 450 | 762 | 67 | 117 | 395 | 1453 | | ||
| one SQLite for all | 140 | 232 | 84 | 246 | 14 | 148 | | ||
| one RocksDB for all, its blocks compressed (Snappy) | 165 | 274 | 89 | 136 | 12 | 37 | | ||
| one RocksDB for all, its blocks as they are | 136 | 229 | 94 | 145 | 10 | 19 | | ||
| one fjall for all, its blocks compressed below the second level (LZ4) | 172 | 276 | 128 | 178 | 9 | 23 | | ||
| one fjall for all, its blocks as they are | 172 | 268 | 129 | 197 | 9 | 25 | | ||
| ||
| store | resident before any is open, MiB | with 2000 open, a record read from each, MiB | KiB an account | after 20000 more records, twice, MiB | MiB on disk | KiB an account | | ||
|---|--:|--:|--:|--:|--:|--:| | ||
| a table for each account, one block cache | 5.5 | 77.0 | 36.6 | 346.1 | 503 | 257 | | ||
| SQLite for each account | 5.5 | 363.2 | 183.1 | 406.0 | 870 | 445 | | ||
| one SQLite for all | 5.5 | 49.0 | 22.3 | 245.6 | 841 | 430 | | ||
| one RocksDB for all, its blocks compressed (Snappy) | 5.5 | 33.6 | 14.4 | 157.1 | 339 | 174 | | ||
| one RocksDB for all, its blocks as they are | 5.5 | 33.2 | 14.2 | 157.3 | 498 | 255 | | ||
| one fjall for all, its blocks compressed below the second level (LZ4) | 5.5 | 38.6 | 16.9 | 181.6 | 500 | 256 | | ||
| one fjall for all, its blocks as they are | 5.5 | 38.6 | 17.0 | 181.8 | 500 | 256 | | ||
| a table for each account, as the server reads one | 5.9 | 32.9 | 13.8 | 169.5 | 504 | 258 | | ||
| SQLite for each account | 5.9 | 363.3 | 183.0 | 406.6 | 870 | 445 | | ||
| one SQLite for all | 5.8 | 49.2 | 22.2 | 245.8 | 841 | 430 | | ||
| one RocksDB for all, its blocks compressed (Snappy) | 5.8 | 33.8 | 14.3 | 157.3 | 339 | 174 | | ||
| one RocksDB for all, its blocks as they are | 5.9 | 33.3 | 14.0 | 157.3 | 498 | 255 | | ||
| one fjall for all, its blocks compressed below the second level (LZ4) | 5.9 | 38.9 | 16.9 | 181.1 | 500 | 256 | | ||
| one fjall for all, its blocks as they are | 5.9 | 38.9 | 16.9 | 181.7 | 500 | 256 | | ||
| ||
The tables read other ways: by `pds_store::table::Table`, its file opened for each block it reads (as above) or held open, and by this benchmark's reader, which holds the file open, keeps the blocks it reads in a map for each table where `Table` has one cache and one lock for all, has no bloom filter, and computes the check named over each block it reads (compared with what the file holds for SHA-256 alone); each from the server's blocks of 16 KiB and from the same entries in blocks of 4 KiB. The first read of an account is its table opened and a record read. | ||
The tables read other ways. By `pds_store::table::Table`: as the server reads one (as above), its block cache holding at most 64 of the tables' files open, the least recently read closed to make room and opened again when next read; and with every file held open, which the server does not do. By this benchmark's reader, which holds every file open, keeps the blocks it reads in a map for each table where `Table` has one cache for all, has no bloom filter, and computes the check named over each block it reads (compared with what the file holds for SHA-256 alone). And by that reader from the layout before: the same entries in blocks of 16 KiB, each block's last key whole in the index, the file opened for each block read. The first read of an account is its table opened and a record read. | ||
| ||
| read by | blocks | check | file | a record by path, cold: p50 µs | p99 µs | a listing of 50, cold: p50 µs | p99 µs | first read of an account: p50 µs | p99 µs | on 16 threads, cold: per s | warm: per s | | ||
|---|---|---|---|--:|--:|--:|--:|--:|--:|--:|--:| | ||
| `Table` | 16 KiB | SHA-256 | opened for each block | 28.1 | 82.5 | 374 | 518 | 66 | 148 | 130910 | 185419 | | ||
| `Table` | 16 KiB | SHA-256 | held open | 15.2 | 46.1 | 220 | 347 | 43 | 85 | 143860 | 168875 | | ||
| this benchmark's | 16 KiB | SHA-256 | held open | 14.6 | 32.7 | 203 | 291 | 40 | 67 | 503888 | 1277619 | | ||
| this benchmark's | 16 KiB | CRC32C | held open | 7.8 | 18.9 | 107 | 170 | 27 | 57 | 718616 | 1477223 | | ||
| this benchmark's | 16 KiB | XXH3-64 | held open | 7.2 | 16.3 | 102 | 166 | 26 | 47 | 737441 | 1448249 | | ||
| this benchmark's | 16 KiB | BLAKE3 | held open | 18.7 | 41.8 | 265 | 361 | 50 | 85 | 491040 | 1425627 | | ||
| `Table` | 4 KiB | SHA-256 | opened for each block | 29.1 | 46.3 | 530 | 746 | 49 | 90 | 84253 | 169873 | | ||
| `Table` | 4 KiB | SHA-256 | held open | 8.8 | 16.9 | 165 | 250 | 29 | 52 | 161531 | 169011 | | ||
| this benchmark's | 4 KiB | SHA-256 | held open | 8.4 | 15.2 | 151 | 234 | 27 | 44 | 687359 | 2176609 | | ||
| this benchmark's | 4 KiB | CRC32C | held open | 5.2 | 10.8 | 93 | 151 | 23 | 42 | 802936 | 2067651 | | ||
| this benchmark's | 4 KiB | XXH3-64 | held open | 5.4 | 12.5 | 89 | 142 | 22 | 44 | 829703 | 2516383 | | ||
| this benchmark's | 4 KiB | BLAKE3 | held open | 14.2 | 24.8 | 258 | 341 | 36 | 60 | 503916 | 2051725 | | ||
| SQLite for each account | 4 KiB pages | none | held open | 10.9 | 24.5 | 336 | 574 | 272 | 510 | 118539 | 338127 | | ||
| one SQLite for all | 4 KiB pages | none | held open | 9.6 | 62.6 | 117 | 193 | 21 | 118 | 114130 | 431119 | | ||
| one RocksDB for all, its blocks compressed (Snappy) | 4 KiB before Snappy | XXH3 | held open | 9.0 | 18.7 | 132 | 335 | 10 | 20 | 541463 | 1003040 | | ||
| one RocksDB for all, its blocks as they are | 4 KiB | XXH3 | held open | 7.0 | 15.0 | 106 | 162 | 8 | 17 | 579530 | 1016869 | | ||
| one fjall for all, its blocks compressed below the second level (LZ4) | 4 KiB before LZ4 | XXH3 | held open | 7.0 | 17.8 | 134 | 200 | 7 | 15 | 472218 | 738215 | | ||
| one fjall for all, its blocks as they are | 4 KiB | XXH3 | held open | 7.2 | 43.1 | 131 | 207 | 8 | 19 | 448754 | 734144 | | ||
| ||
- The order is the runner's: the table as the server reads it is 3 to 4 | ||
times slower than RocksDB and fjall on a record not in its cache, level | ||
from its cache, and behind on 16 threads. | ||
| `Table`, as the server's | 4 KiB | SHA-256 | at most 64 held open | 27.6 | 63.8 | 202 | 446 | 35 | 72 | 98438 | 1799876 | | ||
| `Table`, not as the server's | 4 KiB | SHA-256 | all held open | 10.4 | 26.7 | 202 | 436 | 35 | 72 | 421679 | 1537859 | | ||
| this benchmark's | 4 KiB | SHA-256 | all held open | 10.3 | 19.1 | 187 | 294 | 32 | 61 | 533092 | 2048806 | | ||
| this benchmark's | 4 KiB | CRC32C | all held open | 6.5 | 15.4 | 114 | 248 | 27 | 54 | 625597 | 1861822 | | ||
| this benchmark's | 4 KiB | XXH3-64 | all held open | 6.2 | 14.3 | 110 | 181 | 29 | 63 | 739129 | 2052960 | | ||
| this benchmark's | 4 KiB | BLAKE3 | all held open | 17.8 | 27.6 | 328 | 456 | 41 | 76 | 401663 | 2150774 | | ||
| this benchmark's, the layout before | 16 KiB, last keys whole | SHA-256 | opened for each block | 33.7 | 88.2 | 472 | 679 | 80 | 130 | 175195 | 1138128 | | ||
| SQLite for each account | 4 KiB pages | none | held open | 12.9 | 31.6 | 450 | 762 | 395 | 1453 | 99132 | 307460 | | ||
| one SQLite for all | 4 KiB pages | none | held open | 11.0 | 80.6 | 140 | 232 | 14 | 148 | 91848 | 402958 | | ||
| one RocksDB for all, its blocks compressed (Snappy) | 4 KiB before Snappy | XXH3 | held open | 11.0 | 22.0 | 165 | 274 | 12 | 37 | 401988 | 833919 | | ||
| one RocksDB for all, its blocks as they are | 4 KiB | XXH3 | held open | 9.1 | 33.3 | 136 | 229 | 10 | 19 | 326502 | 830714 | | ||
| one fjall for all, its blocks compressed below the second level (LZ4) | 4 KiB before LZ4 | XXH3 | held open | 8.9 | 39.7 | 172 | 276 | 9 | 23 | 341002 | 595908 | | ||
| one fjall for all, its blocks as they are | 4 KiB | XXH3 | held open | 8.9 | 47.4 | 172 | 268 | 9 | 25 | 386496 | 659354 | | ||
| ||
- The order is the runner's, but for a record not in the cache: 27.6 µs | ||
with 64 files open at most against 10.4 with all open, as opening a file | ||
again takes about 20 µs here. From the cache a record takes 2.2 µs and | ||
16 threads read 1.8 million a second (RocksDB 5.3 to 5.8, and 831 | ||
thousand). | ||
| ||
### Size and compression | ||
| ||
| @@ -831,9 +830,9 @@ The tables read other ways: by `pds_store::table::Table`, its file opened for ea | ||
|---|--:|--:|--:| | ||
| a log for each account before compaction, JSON frames (the server's) | 1139.9 | 5836 | 11952 | | ||
| a log for each account before compaction, binary frames | 566.9 | 2902 | 5944 | | ||
| a table for each account | 50.3 | 257 | 527 | | ||
| the tables, each block compressed (zstd 3) | 25.3 | 129 | 265 | | ||
| the tables, each block compressed with a dictionary (zstd 3) | 23.8 | 122 | 250 | | ||
| a table for each account | 50.4 | 258 | 529 | | ||
| the tables, each block compressed (zstd 3) | 28.4 | 145 | 298 | | ||
| the tables, each block compressed with a dictionary (zstd 3) | 24.6 | 126 | 258 | | ||
| SQLite for each account | 86.3 | 442 | 905 | | ||
| one SQLite for all | 83.8 | 429 | 878 | | ||
| SQLite for each account, every record rewritten 3 times | 94.4 | 483 | 989 | | ||
| @@ -842,19 +841,19 @@ The tables read other ways: by `pds_store::table::Table`, its file opened for ea | ||
| the same, vacuumed | 75.8 | 388 | 795 | | ||
| SQLite for each account, rewritten and vacuumed, its blocks in a table with a rowid | 63.0 | 322 | 660 | | ||
| one RocksDB for all, its blocks: Snappy | 34.6 | 177 | 363 | | ||
| the same, every record rewritten 3 times | 42.1 | 215 | 441 | | ||
| the same, every record rewritten 3 times | 42.1 | 216 | 442 | | ||
| the same, compacted whole | 34.5 | 177 | 362 | | ||
| one RocksDB for all, its blocks: no compression | 49.8 | 255 | 522 | | ||
| the same, every record rewritten 3 times | 59.6 | 305 | 625 | | ||
| the same, compacted whole | 50.2 | 257 | 526 | | ||
| one RocksDB for all, its blocks: zstd | 29.8 | 153 | 313 | | ||
| the same, every record rewritten 3 times | 36.7 | 188 | 385 | | ||
| the same, every record rewritten 3 times | 36.8 | 188 | 385 | | ||
| the same, compacted whole | 29.8 | 152 | 312 | | ||
| one fjall for all, its blocks: LZ4 below the second level | 49.9 | 256 | 523 | | ||
| the same, every record rewritten 3 times | 67.6 | 346 | 709 | | ||
| the same, every record rewritten 3 times | 60.3 | 309 | 632 | | ||
| the same, compacted whole | 35.2 | 180 | 369 | | ||
| one fjall for all, its blocks: no compression | 49.9 | 256 | 523 | | ||
| the same, every record rewritten 3 times | 60.3 | 309 | 633 | | ||
| the same, every record rewritten 3 times | 60.3 | 309 | 632 | | ||
| the same, compacted whole | 49.9 | 256 | 524 | | ||
| ||
What RocksDB wrote to its files for what it was given, keys and values as they are, through the load and the rewrites, and then with the whole compaction too: to its log, to the files it writes from memory, and to those its compactions write. | ||
| @@ -865,13 +864,13 @@ What RocksDB wrote to its files for what it was given, keys and values as they a | ||
| no compression | 996 | 996 | 168 | 171 | 1.34 | 1385 | 1.39 | | ||
| zstd | 996 | 996 | 119 | 98 | 1.22 | 1242 | 1.25 | | ||
| ||
The tables' data blocks, 50 MiB in 3364 blocks, compressed one at a time on one thread; the dictionary is 65536 bytes, trained on each 8th block and counted once above. A record is read by its path through two blocks, each read from its file (in the system's cache), its check computed, and searched: the median of 20000. | ||
The tables' data blocks, 50 MiB in 13583 blocks, each ended at 4 KiB as the server's writer ends them, compressed one at a time on one thread; the dictionary is 65536 bytes, trained on each 8th block and counted once above. A record is read by its path through two blocks, each read from its file (in the system's cache), its check computed, and searched: the median of 20000. | ||
| ||
| blocks | stored bytes of each 100 | compressing MiB per s | decompressing MiB per s | a record read µs | | ||
|---|--:|--:|--:|--:| | ||
| as they are | 100 | | | 20.5 | | ||
| compressed (zstd 3) | 50 | 287 | 961 | 35.0 | | ||
| compressed with a dictionary (zstd 3) | 47 | 186 | 1252 | 32.0 | | ||
| as they are | 100 | | | 8.2 | | ||
| compressed (zstd 3) | 56 | 172 | 538 | 16.2 | | ||
| compressed with a dictionary (zstd 3) | 48 | 136 | 638 | 13.3 | | ||
| ||
### The service log by the number of accounts | ||
| ||
crates/pds-server/src/checkpoint/before.rs+1−1
| @@ -202,7 +202,7 @@ pub(crate) mod tests { | ||
/// of the space repos' sections. | ||
pub(crate) fn downgrade(path: &Path) { | ||
let cache = BlockCache::new(1 << 20); | ||
let all: Vec<(Vec<u8>, Vec<u8>)> = Table::open(path) | ||
let all: Vec<(Vec<u8>, Vec<u8>)> = Table::open(path, &cache) | ||
.unwrap() | ||
.iter(b"", None, &cache) | ||
.map(Result::unwrap) | ||
crates/pds-server/src/checkpoint/spaces.rs+3−3
| @@ -844,7 +844,7 @@ impl Disk { | ||
/// files open, and what the memtable keeps of them now. | ||
pub fn space_snapshot(&self, uri: &str) -> io::Result<SpaceSnapshot> { | ||
let table = match &self.table { | ||
Some(t) => Some((t.clone(), File::open(t.path())?)), | ||
Some(t) => Some((t.clone(), t.file()?)), | ||
None => None, | ||
}; | ||
let m = &self.mem.spaces; | ||
| @@ -885,7 +885,7 @@ impl Disk { | ||
/// A space repo's records as they were when taken, read as they are sent. | ||
pub(crate) struct SpaceSnapshot { | ||
uri: String, | ||
table: Option<(Arc<Table>, File)>, | ||
table: Option<(Arc<Table>, Arc<File>)>, | ||
/// What the memtable kept of its records, and voided of the table's. | ||
mem: SpaceMem, | ||
/// Each log it reads, by generation, and the number the cache holds it by. | ||
| @@ -904,7 +904,7 @@ impl SpaceSnapshot { | ||
let view = View { | ||
mem: &self.mem, | ||
old: match &self.table { | ||
Some((t, f)) => Old::Table(t, Some(f)), | ||
Some((t, f)) => Old::Table(t, Some(&**f)), | ||
None => Old::None, | ||
}, | ||
cache, | ||
crates/pds-server/src/checkpoint.rs+119−8
| @@ -47,7 +47,12 @@ | ||
//! of the tree: the nodes it made, with their bytes, those it took out, | ||
//! each record whose count of paths changed, and each record put, with its | ||
//! CID. Bytes are read from the log's frames, or the table, as they are | ||
//! asked for, through the block cache all the stores share (`BlockCache`). | ||
//! asked for, through the block cache all the stores share (`BlockCache`), | ||
//! which keeps the files of the tables read last open, at most | ||
//! `pds_store::table::OPEN` of them. What reads a store's tree after its | ||
//! lock is let go (`Snapshot`) holds the table's file itself, and so reads | ||
//! the table it began with to the end, though a compaction replaces it or | ||
//! the account is deleted meanwhile. | ||
//! | ||
//! **Compacting** (`AccountFold`) merges the table and the sealed logs, in | ||
//! order of key, into the next table, as it reads them: blocks taken out, | ||
| @@ -1758,7 +1763,7 @@ impl Disk { | ||
/// the memtable's blocks as they are now. | ||
pub fn snapshot(&self, head: (Vec<u8>, Vec<u8>)) -> io::Result<Snapshot> { | ||
let table = match &self.table { | ||
Some(t) => Some((t.clone(), File::open(t.path())?)), | ||
Some(t) => Some((t.clone(), t.file()?)), | ||
None => None, | ||
}; | ||
let mut blocks: Vec<(Vec<u8>, MemBlock)> = self | ||
| @@ -1791,7 +1796,7 @@ impl Disk { | ||
/// A store's tree as it was when taken, read as it is sent. | ||
pub(crate) struct Snapshot { | ||
head: (Vec<u8>, Vec<u8>), | ||
table: Option<(Arc<Table>, File)>, | ||
table: Option<(Arc<Table>, Arc<File>)>, | ||
blocks: Vec<(Vec<u8>, MemBlock)>, | ||
/// Each log it reads, by generation, and the number the cache holds it by. | ||
logs: HashMap<u64, (File, u64)>, | ||
| @@ -1835,7 +1840,7 @@ impl Snapshot { | ||
let mut table: Rows<'_, io::Error> = match &self.table { | ||
None => Box::new(std::iter::empty()), | ||
Some((t, f)) => Box::new( | ||
t.iter(b"B", Some(f), cache) | ||
t.iter(b"B", Some(&**f), cache) | ||
.take_while(|r| r.as_ref().map_or(true, |(k, _)| k.first() == Some(&b'B'))), | ||
), | ||
}; | ||
| @@ -1920,7 +1925,7 @@ fn table_events(t: &Table, cache: &BlockCache, out: &mut BTreeMap<u64, u64>) -> | ||
pub(crate) fn events_on_disk(ctx: &crate::cache::Ctx, did: &str) -> Result<EventsOnDisk, String> { | ||
let layout = crate::store::account_layout(&ctx.dir, did); | ||
let mut out: BTreeMap<u64, u64> = BTreeMap::new(); | ||
let (generation, mut last, let_go) = match Table::open(&layout.checkpoint()) { | ||
let (generation, mut last, let_go) = match Table::open(&layout.checkpoint(), &ctx.blocks) { | ||
Ok(t) => { | ||
let read = (|| { | ||
// A table of the version before has none: a load folds it. | ||
| @@ -1978,7 +1983,7 @@ pub(crate) fn load( | ||
let layout = crate::store::account_layout(&ctx.dir, did); | ||
let at = |why: String| format!("{}: {why}", layout.dir.display()); | ||
let named = |why: String| format!("{}: {why}", layout.checkpoint().display()); | ||
let table = match Table::open(&layout.checkpoint()) { | ||
let table = match Table::open(&layout.checkpoint(), &ctx.blocks) { | ||
Ok(t) => Some(Arc::new(t)), | ||
Err(e) if e.kind() == io::ErrorKind::NotFound => None, | ||
Err(e) => return Err(at(e.to_string())), | ||
| @@ -2173,7 +2178,7 @@ impl Fold for AccountFold { | ||
let cache = BlockCache::new(self.blocks); | ||
let passing = BlockCache::new(0); | ||
let table = match checkpoint { | ||
Some(p) => Some(Table::open(p).map_err(|e| e.to_string())?), | ||
Some(p) => Some(Table::open(p, &cache).map_err(|e| e.to_string())?), | ||
None => None, | ||
}; | ||
// A table of the version before keeps its space repos whole in its | ||
| @@ -2449,7 +2454,8 @@ impl Fold for AccountFold { | ||
a.store.installing(|st| { | ||
rename()?; | ||
let Some(st) = st else { return Ok(()) }; | ||
let table = Table::open(&st.disk.layout.checkpoint()).map_err(|e| e.to_string())?; | ||
let table = Table::open(&st.disk.layout.checkpoint(), &st.ctx.blocks) | ||
.map_err(|e| e.to_string())?; | ||
let (_, _, let_go) = table | ||
.get(b"H", &st.ctx.blocks) | ||
.map_err(|e| e.to_string())? | ||
| @@ -2914,4 +2920,109 @@ mod tests { | ||
} | ||
let _ = std::fs::remove_dir_all(&dir); | ||
} | ||
| ||
async fn put(s: &crate::Shared, did: &str, token: &str, k: &str, value: &Json) { | ||
call( | ||
s, | ||
"com.atproto.repo.putRecord", | ||
Some(token), | ||
json!({ "repo": did, "collection": "a.b.c", "rkey": k, "record": value }), | ||
) | ||
.await | ||
.unwrap(); | ||
} | ||
| ||
/// The generation of `did`'s table, once no compaction of its log runs. | ||
fn compacted(s: &crate::Shared, did: &str) -> u64 { | ||
let wal = store_of(s, did, |st| st.wal.clone()); | ||
wal.wait_compacted(); | ||
wal.checkpoint_generation() | ||
} | ||
| ||
/// A repository being sent is read whole from the table it began with, | ||
/// though a compaction replaces that table meanwhile, and the table's | ||
/// file is closed once it is sent. Of more accounts' tables than may be | ||
/// open at once, no more files are open than that, each account read | ||
/// from its file all the same; and a store let go closes its table's. | ||
#[tokio::test] | ||
async fn tables_replaced_and_held_open() { | ||
use pds_store::table::OPEN; | ||
let plc = plc().await; | ||
let dir = scratch("ckpt-open"); | ||
let s = open_with(&dir, &plc, |c| { | ||
c.load_all = false; | ||
c.log_floor = 2048; | ||
}); | ||
let open = || s.ctx.blocks.open_files(); | ||
let (did, token) = create(&s, "alice").await; | ||
let mut want: Vec<Json> = Vec::new(); | ||
for i in 0..30 { | ||
let value = json!({ "$type": "a.b.c", "n": i, "t": "x".repeat(200) }); | ||
put(&s, &did, &token, &format!("k{i:02}"), &value).await; | ||
want.push(value); | ||
} | ||
let first = compacted(&s, &did); | ||
assert!(first > 0, "never compacted"); | ||
assert_eq!(open(), 1); | ||
let (head, stream) = store_of(&s, &did, |st| { | ||
( | ||
st.repo.head.clone(), | ||
crate::sync::CarStream::of(st, None).unwrap(), | ||
) | ||
}); | ||
// Every record written again, until another table is in its place. | ||
let mut n = 0; | ||
while compacted(&s, &did) == first { | ||
let value = json!({ "$type": "a.b.c", "n": 1000 + n }); | ||
put(&s, &did, &token, &format!("k{:02}", n % 30), &value).await; | ||
n += 1; | ||
assert!(n < 1000, "never compacted again"); | ||
} | ||
assert_eq!(open(), 2); | ||
let (roots, blocks) = pds_repo::read_car(&stream.bytes().unwrap()).unwrap(); | ||
assert_eq!(roots, vec![head]); | ||
let mut records: Vec<Json> = blocks | ||
.iter() | ||
.filter_map(|(_, b)| decode(b).ok()) | ||
.map(|v| to_json(&v)) | ||
.filter(|v| v["$type"] == "a.b.c") | ||
.collect(); | ||
records.sort_by_key(|v| v["n"].as_u64()); | ||
assert_eq!(records, want); | ||
assert_eq!(open(), 1); | ||
| ||
let mut dids = vec![did]; | ||
for i in 0..OPEN + 5 { | ||
let (did, token) = create(&s, &format!("user{i}")).await; | ||
let value = json!({ "$type": "a.b.c", "t": "x".repeat(3000) }); | ||
put(&s, &did, &token, "k00", &value).await; | ||
assert!(compacted(&s, &did) > 0, "never compacted"); | ||
assert!(open() <= OPEN); | ||
dids.push(did); | ||
} | ||
assert_eq!(open(), OPEN); | ||
for did in &dids { | ||
// Nothing of its table in memory: the record is read from its file. | ||
store_of(&s, did, |st| { | ||
st.ctx.blocks.forget(st.disk.table.as_ref().unwrap().id()) | ||
}); | ||
let got = call_with( | ||
&s, | ||
"com.atproto.repo.getRecord", | ||
None, | ||
¶ms(&[("repo", did), ("collection", "a.b.c"), ("rkey", "k00")]), | ||
Json::Null, | ||
) | ||
.await | ||
.unwrap(); | ||
assert_eq!(got["value"]["$type"], "a.b.c"); | ||
assert_eq!(open(), OPEN); | ||
} | ||
for (i, did) in dids.iter().enumerate() { | ||
assert!(s.account(did).unwrap().store.evict()); | ||
assert!(open() < dids.len() - i); | ||
} | ||
assert_eq!(open(), 0); | ||
let _ = std::fs::remove_dir_all(&dir); | ||
} | ||
} | ||
crates/pds-server/src/log.rs+3−2
| @@ -58,8 +58,9 @@ | ||
//! the one its last landed change gave it, or else the one the service log | ||
//! has. | ||
//! | ||
//! At most `OPEN_LOGS` accounts' logs are open at once, and they are | ||
//! recovered on at most as many threads as the machine has cores. | ||
//! At most `OPEN_LOGS` accounts' logs are open at once, and at most | ||
//! `pds_store::table::OPEN` of their tables' files, and they are recovered | ||
//! on at most as many threads as the machine has cores. | ||
| ||
use crate::blobs::blob_dir; | ||
use crate::cache::{Ctx, Slot}; | ||
crates/pds-store/src/table.rs+1112−252
1364 lines changed: not loaded until asked for.
harness/checks/limits.mjs+28−6
| @@ -1279,11 +1279,13 @@ async function blobTtl() { | ||
count('unreferenced uploads', false) | ||
} | ||
| ||
/// 300 accounts, then the server started again under `ulimit -n 256`: it | ||
/// serves them all, and an early and a late one take writes; 20 more made | ||
/// under the limit are each 200 or 500, and it never exits; after a restart | ||
/// without the limit, those answered 500 do not exist (spec/LIMITS.md, | ||
/// "Open files and threads", "Disk"). | ||
/// 300 accounts, each with records enough that its log has been compacted | ||
/// into a table (spec/STORE.md), a file the server opens to read it; then | ||
/// the server started again under `ulimit -n 256`: it serves them all, a | ||
/// record of each read from its table, and an early and a late one take | ||
/// writes; 20 more made under the limit are each 200 or 500, and it never | ||
/// exits; after a restart without the limit, those answered 500 do not | ||
/// exist (spec/LIMITS.md, "Open files and threads", "Disk"). | ||
async function openFiles() { | ||
const made = [] | ||
let ip = freshIp() | ||
| @@ -1293,12 +1295,32 @@ async function openFiles() { | ||
} | ||
const failed = made.filter((m) => m.res.status !== 200) | ||
if (failed.length) await broke(CREATE_ACCOUNT, { accounts: 300 }, 'each made', failed.slice(0, 3).map((m) => m.res.body)) | ||
// Three records of 140000 bytes are past the mebibyte a log may hold | ||
// before it is compacted; the second for the last compactions to end. | ||
const text = 'x'.repeat(140000) | ||
for (let i = 0; i < made.length; i += 10) { | ||
if (i % 50 === 0) ip = freshIp() | ||
const written = await Promise.all(made.slice(i, i + 10).map(async (m) => { | ||
for (let n = 0; n < 3; n++) { | ||
const r = await send(cand, CREATE_RECORD, { body: { repo: m.did, collection: 'com.example.note', rkey: `big${n}`, record: { $type: 'com.example.note', n, text } }, token: m.access, ip }) | ||
if (r.status !== 200) return r | ||
} | ||
return null | ||
})) | ||
const bad = written.find((r) => r) | ||
if (bad) await broke(CREATE_RECORD, { accounts: 300, bytes: text.length }, 200, bad) | ||
} | ||
await wait(1000) | ||
await restartServer({}, 256, 'with 300 accounts, under ulimit -n 256') | ||
ip = freshIp() | ||
for (let i = 0; i < made.length; i += 25) { | ||
const answers = await Promise.all(made.slice(i, i + 25).map((m) => send(cand, 'com.atproto.sync.getLatestCommit', { params: { did: m.did }, ip }))) | ||
const some = made.slice(i, i + 25) | ||
const answers = await Promise.all(some.map((m) => send(cand, 'com.atproto.sync.getLatestCommit', { params: { did: m.did }, ip }))) | ||
const bad = answers.find((r) => r.status !== 200) | ||
if (bad) await broke('getLatestCommit', { accounts: 300, ulimit: 256 }, 200, bad) | ||
const records = await Promise.all(some.map((m) => send(cand, 'com.atproto.repo.getRecord', { params: { repo: m.did, collection: 'com.example.note', rkey: 'big0' }, ip }))) | ||
const lost = records.find((r) => r.status !== 200 || r.body?.value?.text !== text) | ||
if (lost) await broke('getRecord', { accounts: 300, ulimit: 256, rkey: 'big0' }, 'the record, as written', { status: lost.status, error: lost.body?.error, bytes: lost.body?.value?.text?.length }) | ||
} | ||
for (const m of [made[0], made[1], made.at(-2), made.at(-1)]) { | ||
const r = await send(cand, CREATE_RECORD, { body: { repo: m.did, collection: 'com.example.note', record: { $type: 'com.example.note', n: 1 } }, token: m.access, ip }) | ||