~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

The store reads and syncs less: open files, XXH3, clean starts, room

Merged chadtmiller.com asked to merge into main from chadtmiller.com/monolith:store/reads-and-syncs
bench/store/src/check.rs+7−5
@@ -1,8 +1,10 @@
//! The check a frame or a table's block carries, four ways: the first 8
//! bytes of SHA-256, as `pds-store` computes it, and what could stand in
//! its place: CRC32C, XXH3-64, and the first 8 bytes of BLAKE3. Each is
//! over the same bytes; only SHA-256 and BLAKE3 withstand someone who
//! means to forge one, and CRC32C is 4 bytes of the 8.
//! The check a frame or a table's block carries, four ways. Two are
//! `pds-store`'s, computed as it computes them: the first 8 bytes of
//! SHA-256, a frame's and a table's of the version before, and XXH3-64, a
//! table's. And two that could stand in their place: CRC32C, and the first
//! 8 bytes of BLAKE3. Each is over the same bytes; only SHA-256 and BLAKE3
//! withstand someone who means to forge one, and CRC32C is 4 bytes of the
//! 8.
use sha2::{Digest, Sha256};
bench/store/src/reads.rs+39−16
@@ -10,8 +10,8 @@ use crate::fj;
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::{BLOCK, BlockCache, OPEN, Table};
use crate::util::{Rng, at, cores, dir_bytes, mib, par, rss_kib, scratch, share, us};
use pds_store::table::{BLOCK, BlockCache, Table, Version};
use rocksdb::DB;
use rusqlite::{Connection, params};
use std::collections::HashMap;
@@ -41,8 +41,8 @@ const LIST: &str = "SELECT r.path, b.bytes FROM record r JOIN block b ON b.accou
enum Store {
/// A table for each account read as the server reads one
/// (`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.
/// the tables' files open: at most the server's share of them
/// (`util::share`), or every one.
Tables {
dir: PathBuf,
tables: Vec<OnceLock<Table>>,
@@ -113,7 +113,7 @@ impl Store {
tables: cells(accounts),
cache: match flag {
"open" => BlockCache::with_open(CACHE, accounts),
_ => BlockCache::new(CACHE),
_ => BlockCache::with_open(CACHE, share()),
},
},
("dbs", _) => Store::Dbs {
@@ -170,7 +170,8 @@ impl Store {
unreachable!("no reader")
};
readers[account].get_or_init(|| {
let reader = Reader::open(&dir.join(format!("{account}.table")), *check, true);
let path = dir.join(format!("{account}.table"));
let reader = Reader::open(&path, Some(*check), true);
if *each { reader.opening_each() } else { reader }
})
}
@@ -538,6 +539,24 @@ fn measure(
.collect()
}
/// See that the table at `before`, which this benchmark wrote in the
/// layout before, is one of the version before that `Table` reads, and
/// holds the entries of the table at `now`.
fn same_entries(now: &Path, before: &Path) {
let cache = BlockCache::new(CACHE);
let entries = |path: &Path, version: Version| {
let t = Table::open(path, &cache).expect("a table");
assert_eq!(t.version(), version, "{}", path.display());
t.iter(b"", None, &cache)
.map(|e| e.expect("the table read"))
.collect::<Vec<_>>()
};
assert!(
entries(now, Version::WRITTEN) == entries(before, Version::Sha256),
"the layout before holds other entries"
);
}
pub fn reads(accounts: usize, records: usize) {
assert!(records > LISTED, "more records than a listing");
let dir = scratch("reads");
@@ -553,12 +572,12 @@ pub fn reads(accounts: usize, records: usize) {
});
stores::build(&dir, accounts, records);
});
let share = share();
let as_the_server = format!(
"a table for each account, as the server reads one, at most {share} of their files open"
);
let stores = [
(
"tables",
"a table for each account, as the server reads one",
"",
),
("tables", as_the_server.as_str(), ""),
(
"dbs",
"SQLite for each account",
@@ -657,9 +676,13 @@ pub fn reads(accounts: usize, records: usize) {
)
.expect("a table");
});
same_entries(&stores::table_path(&dir, 0), &dir.join("tables16/0.table"));
let blocks = format!("{} KiB", BLOCK >> 10);
// The check of the tables as they are written, and of the layout before.
let own = tables::held(Version::WRITTEN).name();
let before = tables::held(Version::Sha256).name();
println!(
"\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"
"\nThe tables read other ways. By `pds_store::table::Table`: as the server reads one (as above), its block cache holding at most {share} of the tables' files open, the server's share of the file descriptors this process may have, the least recently read closed to make room and opened again when next read; and with every file held open, whatever the share. 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: {own} is the one these files hold, and is compared with it; each other is compared with nothing. And by that reader from the layout before, a table of the version before: the same entries in blocks of 16 KiB, each block's last key whole in the index, its check {before}, which that file holds and is compared with, 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 |"
@@ -679,12 +702,12 @@ pub fn reads(accounts: usize, records: usize) {
);
};
way(
&format!("`Table`, as the server's | {blocks} | SHA-256 | at most {OPEN} held open"),
&format!("`Table`, as the server's | {blocks} | {own} | at most {share} 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"),
format!("`Table`, every file open | {blocks} | {own} | all held open"),
)];
ways.extend(Check::ALL.map(|c| {
(
@@ -693,8 +716,8 @@ pub fn reads(accounts: usize, records: usize) {
)
}));
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(),
format!("tables16:{before}+each"),
format!("this benchmark's, the layout before | 16 KiB, last keys whole | {before} | opened for each block"),
));
for (name, cells) in ways {
way(
bench/store/src/size.rs+1−2
@@ -6,7 +6,6 @@
//! RocksDB wrote to get there. And what zstd costs a table: its speed,
//! and a read of a record through blocks that must be decompressed.
use crate::check::Check;
use crate::data::{self, SEED, State};
use crate::fj;
use crate::frame::Encoding;
@@ -67,7 +66,7 @@ fn read_records(
) -> Duration {
crate::util::read_through(&dir.join(kind));
let readers: Vec<Reader> = (0..accounts)
.map(|a| Reader::open(&dir.join(format!("{kind}/{a}.table")), Check::Sha256, false))
.map(|a| Reader::open(&dir.join(format!("{kind}/{a}.table")), None, false))
.collect();
let mut r = Rng::of(&[SEED, 11]);
let mut times: Vec<Duration> = (0..READS)
bench/store/src/tables.rs+119−50
@@ -1,12 +1,14 @@
//! 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 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.
//! its blocks of another size with each one's last key whole in the index
//! in a table of the version before, 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;
use pds_store::table::Version;
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::fs::File;
use std::hint::black_box;
@@ -14,7 +16,9 @@ use std::os::unix::fs::FileExt;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, PoisonError};
const FOOTER: usize = 40;
/// The bytes of a file's end that hold a table's footer, whichever its
/// version.
const TAIL: usize = 48;
fn varint(out: &mut Vec<u8>, mut n: u64) {
while n >= 0x80 {
@@ -63,11 +67,31 @@ struct Place {
len: usize,
}
/// The four numbers a table's footer holds: where its index is, the bytes
/// of its index and of its bloom filter, and its entries.
fn footer(tail: &[u8]) -> [u64; 4] {
assert_eq!(&tail[..8], MAGIC, "a table");
[0, 1, 2, 3].map(|i| u64::from_le_bytes(tail[8 + i * 8..16 + i * 8].try_into().expect("8")))
/// The check a table of `version` holds.
pub fn held(version: Version) -> Check {
match version {
Version::Sha256 => Check::Sha256,
Version::Xxh3 => Check::Xxh3,
}
}
/// What the footer of a table that ends as `tail` does holds: its version,
/// and four numbers: where its index is, the bytes of its index and of
/// its bloom filter, and its entries.
fn footer(tail: &[u8]) -> (Version, [u64; 4]) {
let version = Version::of(tail).expect("a table");
let numbers = &tail[tail.len() - version.footer() + 8..];
let n = |i: usize| u64::from_le_bytes(numbers[i * 8..i * 8 + 8].try_into().expect("8"));
(version, [0, 1, 2, 3].map(n))
}
/// A table's version, its index and its bloom filter, each with its
/// check, and its count of entries.
fn parts(table: &[u8]) -> (Version, &[u8], &[u8], u64) {
let (version, [index_at, index_len, filter_len, count]) = footer(table);
let (index_at, filter_at) = (index_at as usize, (index_at + index_len) as usize);
let filter = &table[filter_at..filter_at + filter_len as usize];
(version, &table[index_at..filter_at], filter, count)
}
/// A table's index: where each data block is.
@@ -86,70 +110,107 @@ fn places(index: &[u8]) -> Vec<Place> {
out
}
/// A table being put together again: its blocks as they are given, each
/// with SHA-256's check over what is stored, and the index of where they
/// are.
#[derive(Default)]
/// A table being put together again, of a version: its blocks as they are
/// given, each with that version's check over what is stored, and the
/// index of where they are.
struct Again {
version: Version,
out: Vec<u8>,
index: Vec<u8>,
}
impl Again {
fn new(version: Version) -> Again {
Again {
version,
out: Vec::new(),
index: Vec::new(),
}
}
fn block(&mut self, last: &[u8], stored: &[u8]) {
varint(&mut self.index, last.len() as u64);
self.index.extend_from_slice(last);
varint(&mut self.index, self.out.len() as u64);
varint(&mut self.index, stored.len() as u64 + 8);
self.out.extend_from_slice(stored);
self.out.extend_from_slice(&Check::Sha256.of(stored));
self.out.extend_from_slice(&held(self.version).of(stored));
}
/// The file: the blocks, the index, and the bloom filter and count of
/// entries `table` had.
fn finish(mut self, table: &[u8]) -> Vec<u8> {
let [index_at, index_len, bloom_len, count] = footer(&table[table.len() - FOOTER..]);
/// The file: the blocks, the index, `filter`, a bloom filter of its
/// version with its check, and the footer, with the `count` of entries
/// and, in a version whose footer has one, its check.
fn finish(mut self, filter: &[u8], count: u64) -> Vec<u8> {
let blocks = self.out.len() as u64;
let c = Check::Sha256.of(&self.index);
let c = held(self.version).of(&self.index);
self.index.extend_from_slice(&c);
self.out.extend_from_slice(&self.index);
self.out
.extend_from_slice(&table[(index_at + index_len) as usize..table.len() - FOOTER]);
self.out.extend_from_slice(MAGIC);
for n in [blocks, self.index.len() as u64, bloom_len, count] {
self.out.extend_from_slice(&n.to_le_bytes());
self.out.extend_from_slice(filter);
let mut footer = self.version.magic().to_vec();
for n in [blocks, self.index.len() as u64, filter.len() as u64, count] {
footer.extend_from_slice(&n.to_le_bytes());
}
if self.version.footer() > footer.len() {
let c = held(self.version).of(&footer);
footer.extend_from_slice(&c);
}
self.out.extend_from_slice(&footer);
self.out
}
}
/// The entries of each of a table's data blocks, in order.
fn blocks(table: &[u8]) -> impl Iterator<Item = (Vec<u8>, &[u8])> {
let [index_at, index_len, _, _] = footer(&table[table.len() - FOOTER..]);
places(&table[index_at as usize..(index_at + index_len) as usize])
.into_iter()
.map(move |p| {
(
p.last,
&table[p.offset as usize..p.offset as usize + p.len - 8],
)
})
places(parts(table).1).into_iter().map(move |p| {
(
p.last,
&table[p.offset as usize..p.offset as usize + p.len - 8],
)
})
}
/// The same table with each data block's entries put through `pack`.
/// The same table, of the same version, with each data block's entries
/// put through `pack`.
pub fn packed(table: &[u8], mut pack: impl FnMut(&[u8]) -> Vec<u8>) -> Vec<u8> {
let mut again = Again::default();
let (version, _, filter, count) = parts(table);
let mut again = Again::new(version);
for (last, block) in blocks(table) {
again.block(&last, &pack(block));
}
again.finish(table)
again.finish(filter, count)
}
/// The bloom filter a table of the version before has over the keys of
/// `table`, with its check, as `pds_store::table` made one: 10 bits an
/// entry, 64 at least, and 7 of them a key's, numbered by the first 16
/// bytes of SHA-256 of the key.
fn filter_before(table: &[u8], count: u64) -> Vec<u8> {
let mut filter = vec![0u8; 1 + (count.max(1) * 10).div_ceil(8).max(8) as usize];
filter[0] = 7;
let m = (filter.len() as u64 - 1) * 8;
for (_, block) in blocks(table) {
for (key, _, _) in entries(block) {
let d = Sha256::digest(key);
let a = u64::from_le_bytes(d[..8].try_into().expect("8"));
let b = u64::from_le_bytes(d[8..16].try_into().expect("8")) | 1;
for i in 0..7 {
let p = (a.wrapping_add(b.wrapping_mul(i)) % m) as usize;
filter[1 + p / 8] |= 1 << (p % 8);
}
}
}
let c = Check::Sha256.of(&filter);
filter.extend_from_slice(&c);
filter
}
/// The same table in blocks of `size` bytes, ended as
/// The same entries in a table of the version before, its checks and its
/// bloom filter SHA-256's, in blocks of `size` bytes, ended as
/// `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 count = parts(table).3;
let mut again = Again::new(Version::Sha256);
let mut block: Vec<u8> = Vec::with_capacity(size * 2);
let mut last: &[u8] = &[];
for (_, old) in blocks(table) {
@@ -169,7 +230,7 @@ pub fn reblocked(table: &[u8], size: usize) -> Vec<u8> {
if !block.is_empty() {
again.block(last, &block);
}
again.finish(table)
again.finish(&filter_before(table, count), count)
}
/// A table read by this benchmark: its index held, no bloom filter, each
@@ -178,14 +239,18 @@ pub fn reblocked(table: &[u8], size: usize) -> Vec<u8> {
/// 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.
/// A file holds the checks of its version: XXH3-64, or SHA-256 in a table
/// of the version before. The reader's check is compared with the file's
/// where it is that one; another is computed over the same bytes and
/// compared with nothing: what it costs, not what it finds.
pub struct Reader {
path: PathBuf,
/// The file, if it is held open.
file: Option<File>,
places: Vec<Place>,
check: Check,
/// The check its file holds.
held: Check,
kept: Option<Mutex<HashMap<usize, Arc<[u8]>>>>,
}
@@ -193,20 +258,24 @@ pub struct Reader {
pub type Unpack<'a, 'b> = &'a mut Option<zstd::bulk::Decompressor<'b>>;
impl Reader {
pub fn open(path: &Path, check: Check, keep: bool) -> Reader {
/// The table at `path`, read with `check`, or with the check its file
/// holds if it is given none.
pub fn open(path: &Path, check: Option<Check>, keep: bool) -> Reader {
let file = File::open(path).expect("a table");
let size = file.metadata().expect("a table").len();
let mut tail = [0u8; FOOTER];
file.read_exact_at(&mut tail, size - FOOTER as u64)
let mut tail = [0u8; TAIL];
file.read_exact_at(&mut tail, size - TAIL as u64)
.expect("a footer");
let [index_at, index_len, _, _] = footer(&tail);
let (version, [index_at, index_len, _, _]) = footer(&tail);
let held = held(version);
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: Some(file),
check,
check: check.unwrap_or(held),
held,
kept: keep.then(Mutex::default),
};
let (body, c) = index.split_at(index.len() - 8);
@@ -221,10 +290,10 @@ impl Reader {
}
/// Compute the check of `bytes`, and see that it is `stored` if that
/// is the check the files hold.
/// is the check the file holds.
fn checked(&self, bytes: &[u8], stored: &[u8]) {
let c = black_box(self.check.of(bytes));
assert!(self.check != Check::Sha256 || c == stored, "a check");
assert!(self.check != self.held || c == stored, "a check");
}
/// Block `i`'s entries, read from the file.
bench/store/src/util.rs+12−25
@@ -187,36 +187,23 @@ impl Drop for Rss {
}
}
/// Let the process hold a file for each of a few thousand accounts, three
/// for a SQLite database in WAL mode and nine for a RocksDB, where the
/// limit it was started with is lower and may be raised.
/// Let the process hold as many files as it may, as the server does when
/// it starts (`pds_store::files::raise`): one for each of a few thousand
/// accounts, three for a SQLite database in WAL mode and nine for a
/// RocksDB.
pub fn raise_files() {
use rustix::process::{Resource, Rlimit, getrlimit, setrlimit};
let now = getrlimit(Resource::Nofile);
for want in [65536u64, 16384, 8192] {
if now.current.is_some_and(|c| c >= want) || now.current.is_none() {
return;
}
let want = now.maximum.map_or(want, |m| m.min(want));
if setrlimit(
Resource::Nofile,
Rlimit {
current: Some(want),
maximum: now.maximum,
},
)
.is_ok()
{
return;
}
}
pds_store::files::raise();
}
/// The files the process may hold open at once.
pub fn files() -> u64 {
rustix::process::getrlimit(rustix::process::Resource::Nofile)
.current
.unwrap_or(u64::MAX)
pds_store::files::limit().unwrap_or(u64::MAX)
}
/// The tables' files the server would hold open at once in this process:
/// its share of the files the process may hold.
pub fn share() -> usize {
pds_store::files::share(pds_store::files::limit())
}
/// The time `per_cent` of `sorted` are within.
bench/store/src/write.rs+21−18
@@ -1,6 +1,7 @@
//! The write path of each layout under the same commits: logs of
//! `pds_store`, appended to or allocated ahead, with JSON frames or binary
//! ones, one for each account or one for all; SQLite likewise, a
//! `pds_store`, which give themselves room as they are written to, and
//! this benchmark's own, growing or allocated ahead, with JSON frames or
//! binary ones, one for each account or one for all; SQLite likewise, a
//! transaction a commit or one for all that wait; and RocksDB likewise, a
//! batch a commit. A commit is answered once it is on disk; or, to see
//! what the processor alone allows, never synced at all.
@@ -258,13 +259,14 @@ impl Log for Placed {
}
}
/// Which log a layout's are: the server's, appended to; this benchmark's,
/// growing as it is written; that with each waiting commit answered in its
/// turn, or with each sync letting the commits begun be written first; or
/// this benchmark's allocated ahead in chunks of so many bytes, got so.
/// Which log a layout's are: the server's (`Wal`), with the room it gives
/// itself as it is written to; this benchmark's, growing as it is written;
/// that with each waiting commit answered in its turn, or with each sync
/// letting the commits begun be written first; or this benchmark's
/// allocated ahead in chunks of so many bytes, got so.
#[derive(Clone, Copy)]
enum Grow {
Appended,
Server,
Placed,
InTurn,
Gathered,
@@ -288,7 +290,7 @@ impl Logs {
let path = dir.join(format!("{i}/repo.log"));
let placed = |ahead, wait| Box::new(Placed::open(&path, ahead, wait, durable));
let log: Box<dyn Log> = match grow {
Grow::Appended => Box::new(Wal::open(&path, None).expect("a log").0),
Grow::Server => Box::new(Wal::open(&path, None).expect("a log").0),
Grow::Placed => placed(None, Wait::Together),
Grow::InTurn => placed(None, Wait::InTurn),
Grow::Gathered => placed(None, Wait::Gathered),
@@ -1056,7 +1058,7 @@ impl Rows<'_> {
// This benchmark's own logs replay as the server's do, and
// one allocated ahead ends where its frames do, not its
// file: every commit is there.
if !matches!(grow, Grow::Appended) {
if !matches!(grow, Grow::Server) {
let found: u64 = par(n, cores(), |i| {
frame::count(&d.join(format!("{i}/repo.log"))).expect("the log read")
})
@@ -1069,9 +1071,10 @@ impl Rows<'_> {
);
}
/// The rows of realistic commits: every layout, the logs with the
/// server's frames, with binary ones, and with binary ones written at
/// their offsets, in a file that grows and in one allocated ahead.
/// The rows of realistic commits: every layout, the server's logs,
/// which make their own room, with its frames and with binary ones,
/// and this benchmark's own of binary frames, in a file that grows and
/// in one allocated ahead.
fn layouts(&self) {
use Encoding::{Binary, Json};
let n = self.load.accounts;
@@ -1079,13 +1082,13 @@ impl Rows<'_> {
"a log for each account, JSON frames (the server's)",
n,
Json,
Grow::Appended,
Grow::Server,
);
self.logs(
"a log for each account, binary frames",
n,
Binary,
Grow::Appended,
Grow::Server,
);
self.logs(
"a log for each account, binary frames, each written at its offset",
@@ -1111,8 +1114,8 @@ impl Rows<'_> {
Grow::Ahead(chunk, Room::Zeros),
);
}
self.logs("one log for all, JSON frames", 1, Json, Grow::Appended);
self.logs("one log for all, binary frames", 1, Binary, Grow::Appended);
self.logs("one log for all, JSON frames", 1, Json, Grow::Server);
self.logs("one log for all, binary frames", 1, Binary, Grow::Server);
self.logs(
"one log for all, binary frames, each written at its offset",
1,
@@ -1252,9 +1255,9 @@ pub fn write(commits: usize, writers: usize) {
"a log for each account (the server's)",
ACCOUNTS,
Encoding::Json,
Grow::Appended,
Grow::Server,
);
t.logs("one log for all", 1, Encoding::Json, Grow::Appended);
t.logs("one log for all", 1, Encoding::Json, Grow::Server);
t.sqlite("SQLite for each account", ACCOUNTS, Flush::Disk);
t.sqlite("one SQLite for all", 1, Flush::Disk);
t.rocks(
bench/store/Cargo.lock+2−0
@@ -558,8 +558,10 @@ dependencies = [
name = "pds-store"
version = "0.1.0"
dependencies = [
"rustix",
"serde_json",
"sha2",
"xxhash-rust",
]
[[package]]
bench/store/Cargo.toml+3−2
@@ -12,13 +12,14 @@ pds-store = { path = "../../crates/pds-store" }
rusqlite = { version = "0.32", features = ["bundled"] }
serde_json = "1"
hex = "0.4"
# The table's and the frames' checks, computed as `pds-store` computes them.
# The frames' check, and the check of a table of the version before,
# computed as `pds-store` computes them.
sha2 = { version = "0.10", features = ["asm"] }
# The size part's compressed blocks.
zstd = "0.13"
# A log's room allocated ahead, and the limit on open files raised.
rustix = { version = "1", features = ["fs", "process"] }
# The checks that could stand in SHA-256's place.
# The tables' check, XXH3, and what could stand in a check's place.
crc32c = "0.6"
xxhash-rust = { version = "0.8", features = ["xxh3"] }
blake3 = "1"
bench/store/README.md+63−40
@@ -33,9 +33,9 @@ harness's; without a server
at `target/release/pds` (or `PDS_BIN`) the last part is left out, and says
so. The parts before the last take about sixteen minutes on a laptop, at
most 5 GiB under `BENCH_DIR` and 1.5 GiB of memory, and a limit on open
files of 20,000 or more, which the benchmark raises itself where it may:
under a lower one the row of a RocksDB for each of 2000 accounts is left
out, and says so.
files of 20,000 or more: the benchmark raises its limit to the most it is
allowed, as the server does when it starts, and under a lower one the row
of a RocksDB for each of 2000 accounts is left out, and says so.
On the runner, the `store-bench` workflow runs the same script when the
owner asks, on `main` or a branch (`knotless run rerun
@@ -62,7 +62,7 @@ first. The layouts:
| layout | is |
|---|---|
| a log for each account | the server's: each commit one frame of JSON, bytes as hex, appended to its account's log (`pds_store::Wal`), commits to one account that wait at once sharing a sync |
| a log for each account | the server's: each commit one frame of JSON, bytes as hex, written where the last ended in its account's log (`pds_store::Wal`), commits to one account that wait at once sharing a sync. A `Wal` gives its file room as it is written to: a frame that grows the file grows it by itself and by as many zeros as there are bytes of frames written to the log since it was opened, that frame's among them, 256 KiB at most, and the frames after are written over the zeros |
| one log for all | the same frames in one log, so that commits to any accounts share a sync |
| SQLite for each account | a database for each account in WAL mode, `synchronous=FULL`: a commit is a transaction of its blocks into a table by CID, the nodes taken out deleted, its record's path and its event into tables of their own |
| one SQLite for all | the same tables in one database, on one connection |
@@ -107,25 +107,38 @@ each account and one for all:
| log | is |
|---|---|
| JSON frames | the server's, as above |
| binary frames | the same content with every length a varint and bytes as bytes (`src/frame.rs`), made with `pds_store::frame_of` and appended with `Wal::append_frame_at`: half the bytes |
| binary frames, each written at its offset | a log of this benchmark's own (`Placed` in `src/write.rs`): the same frames written with `write_at` where the last ended, in a file that grows as they are, and synced with `sync_data`, commits that wait at once sharing a sync as a `Wal`'s do. It is there to compare the next with: it differs from a `Wal` in what it leaves out (generations, a pool of files, mending a torn append) |
| JSON frames | the server's, as above, in a `Wal`, with the room it makes |
| binary frames | the same content with every length a varint and bytes as bytes (`src/frame.rs`), made with `pds_store::frame_of` and written with `Wal::append_frame_at`, so with a `Wal`'s room too: half the bytes |
| binary frames, each written at its offset | a log of this benchmark's own (`Placed` in `src/write.rs`): the same frames written with `write_at` where the last ended, in a file that grows as they are, and synced with `sync_data`, commits that wait at once sharing a sync as a `Wal`'s do. It is there to compare the next with: it differs from a `Wal` in what it leaves out (room, generations, a pool of files, mending a torn append) |
| the same, allocated ahead | that log in a file allocated ahead a chunk at a time, so that a sync has no new size of the file to make durable: 16 MiB for one log for all; 64 KiB and 16 KiB, a row each, for an account's, since a server with many accounts, most of which write little, pays for each chunk begun. Where its frames end is found at a replay by the first that fails its check, and after each row every commit is replayed from it to see that they are there |
| the same, its waiting commits answered in turn | that log, growing, for one log for all alone, sharing a sync as a `Wal` did before it woke its waiters together: the lock on what is durable held through each sync, and a commit learning that a sync covered it when it gets that lock in its turn, behind any commit that began the next sync first. The syncs are the same; how many commits each answers is the "log syncs" column against the commits |
| the same, a sync first letting the commits begun be written | that log, growing, for one log for all alone: a commit says when it has begun, and a sync waits, for at most a millisecond, until every commit begun has written its frame, and so covers them. With writers that each begin their next commit as the last is answered, all of them are in every sync, where without it a commit that begins during a sync waits for the one after and each sync has half of them. RocksDB and fjall have a row with the same wait, so that the three are set beside each other under one way of sharing a sync |
The room is got two ways: as zeros written once, which leaves nothing
but the frame's bytes to write, and with `fallocate`, on Linux alone
(one row for each layout), which leaves the room unwritten to the file
system, so that a frame first landing there may still give the file
system a record of its own to change. The last column is the bytes of
zeros written for each commit, the room a log began with and each chunk
after. A chunk used up, the next is got under the log's lock, inside a
commit that is timed. macOS has no `fallocate`, and its `F_FULLFSYNC`
flushes the drive whatever the file's size did: Linux is where allocating
ahead is expected to matter. A `Wal` cannot be given such a file as it
is: it appends at the file's end, and cuts off, when it is opened,
whatever follows the last whole frame.
The room of the logs allocated ahead is got two ways: as zeros written
once, which leaves nothing but the frame's bytes to write, and with
`fallocate`, on Linux alone (one row for each layout), which leaves the
room unwritten to the file system, so that a frame first landing there
may still give the file system a record of its own to change. A log
allocated ahead has its first chunk as it is made, before any commit;
a chunk used up, the next is got under the log's lock, inside a commit
that is timed. macOS has no `fallocate`, and its `F_FULLFSYNC` flushes
the drive whatever the file's size did: Linux is where allocating ahead
is expected to matter.
The rows of JSON frames and of binary frames with no more said are a
`Wal`'s, and have the room a `Wal` makes, which is none until it is
written to: its file is made empty, the first commit to it grows the
file by its frame and as much again, and a later one that the room left
does not hold by its frame and as much as was written to the log so far.
All of that is inside commits, warming or timed. So over 2000 accounts,
where a log gets a commit or two, a `Wal`'s rows time the giving of room
together with what it is worth, a file that has not grown under the
second commit's sync; and the rows allocated ahead, every commit of
which lands in room made before, time what it is worth alone. The last
column is the bytes of zeros this benchmark's own logs wrote for each
commit, the room a log began with and each chunk after: of a `Wal`'s
rows it is empty, and what a `Wal` writes of zeros is never more than
twice the bytes of its frames.
SQLite has three rows. Two are as in the first part: one connection a
database, a transaction a commit, so commits to one database wait in
@@ -273,10 +286,13 @@ own compactions leave them, which for keys that come in order is as they
were first written.
**The check.** A frame and a table's block each carry 8 bytes of check,
in `pds-store` the first 8 of SHA-256, computed whenever one is read. The
firehose and read parts measure three others in its place
(`src/check.rs`): CRC32C, with the processor's own instruction for it,
4 bytes of the 8; XXH3-64; and the first 8 bytes of BLAKE3. Each finds a
computed whenever one is read: in `pds-store` a frame's is the first 8 of
SHA-256, and a table's block's, like its index's, its bloom filter's and
its footer's, is XXH3-64 (the first 8 of SHA-256 in a table of the
version before, which is still read, and whose footer has none). The
firehose and read parts measure each of four in that place
(`src/check.rs`): those two; CRC32C, with the processor's own instruction
for it, 4 bytes of the 8; and the first 8 bytes of BLAKE3. Each finds a
frame torn or a block rotted, CRC32C missing one in four thousand million
where the others miss none to speak of; CRC32C and XXH3 do nothing
against bytes put there on purpose, which a check kept beside the bytes
@@ -291,7 +307,10 @@ them, kept these ways with the same content (`src/stores.rs`):
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;
read and holds at most a share of the tables' files open: the share the
server takes of the file descriptors the process may have, a quarter of
them and at most 4096 (`pds_store::files::share`; `spec/LIMITS.md`,
"Open files and threads"), which the row's name says;
- 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;
@@ -342,11 +361,12 @@ would keep. Measured:
and one record read from each, and after the 20,000 records;
- bytes on disk.
The table reads blocks of 4 KiB and computes each one's check, SHA-256,
The table reads blocks of 4 KiB and computes each one's check, XXH3-64,
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.
tables' files at most a share are open at once, as in the server, the
least recently read closed to make room, so that where the share is fewer
than the accounts 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
@@ -374,21 +394,24 @@ 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. 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`)
(`BlockCache::with_open`), whatever the share: what a share fewer than
the accounts costs, and the same as the row before where the share is no
fewer. 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.
block (above): XXH3-64 is the one the files hold, and is compared with
it; each other is computed over the same bytes and compared with
nothing, since a table with another check is of another version of its
format. And by that reader from the layout before, written by this
benchmark from the same entries (`tables::reblocked`): a table of the
version before, its check SHA-256, in 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, the check 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
bench/store/RESULTS.md+323−319
642 lines changed: not loaded until asked for.
crates/pds-server/src/checkpoint/before.rs+4−3
@@ -193,13 +193,14 @@ pub(crate) mod tests {
use super::super::{header_value, read_header};
use super::*;
use pds_repo::data::encode;
use pds_store::table::{BlockCache, Table, TableWriter};
use pds_store::table::{BLOCK, BlockCache, Table, TableWriter, Version};
use std::path::Path;
/// Make the table at `path`, of this version, one of the version
/// before holding the same: its space repos whole in its `S`, with its
/// generation, last `seq` and greatest seq let go, and no `H` nor any
/// of the space repos' sections.
/// of the space repos' sections; and of the table format's version
/// before, as every such table is.
pub(crate) fn downgrade(path: &Path) {
let cache = BlockCache::new(1 << 20);
let all: Vec<(Vec<u8>, Vec<u8>)> = Table::open(path, &cache)
@@ -267,7 +268,7 @@ pub(crate) mod tests {
m.push(("letGo".into(), int(let_go)));
assert_eq!(header_value(generation, last_seq, let_go), get(b"H"));
let out = std::io::BufWriter::new(std::fs::File::create(path).unwrap());
let mut w = TableWriter::new(out, all.len() as u64);
let mut w = TableWriter::of_version(out, all.len() as u64, BLOCK, Version::Sha256);
for (k, v) in &all {
match k[0] {
b'B' | b'D' | b'E' | b'P' => w.add(k, v).unwrap(),
crates/pds-server/src/events/index.rs+1−1
crates/pds-server/src/lifecycle/tests.rs+2−2
crates/pds-server/src/blobs.rs+1−1
crates/pds-server/src/blobstore.rs+2−2
crates/pds-server/src/cache.rs+768−13
781 lines changed: not loaded until asked for.
crates/pds-server/src/checkpoint.rs+21−7
crates/pds-server/src/compact.rs+11−7
crates/pds-server/src/directory.rs+5−5
crates/pds-server/src/events.rs+1−7
crates/pds-server/src/lib.rs+32−0
crates/pds-server/src/limits.rs+24−3
crates/pds-server/src/log.rs+609−56
665 lines changed: not loaded until asked for.
crates/pds-server/src/oauth.rs+1−1
crates/pds-server/src/ops.rs+242−22
crates/pds-server/src/sessions.rs+1−1
crates/pds-server/src/store.rs+337−23
crates/pds-store/src/files.rsnew+97−0
crates/pds-store/src/generations.rs+1006−99
1105 lines changed: not loaded until asked for.
crates/pds-store/src/lib.rs+1075−86
1161 lines changed: not loaded until asked for.
crates/pds-store/src/table.rs+492−129
621 lines changed: not loaded until asked for.
crates/pds-store/Cargo.toml+2−0
docs/guide/operating.md+13−1
harness/checks/scale.mjs+48−2
spec/LIMITS.md+13−9
spec/OPERATIONS.md+1−1
spec/STORE.md+15−1
Cargo.lock+8−0
Cargo.toml+1−0