chadtmiller.com/monolith
An atproto PDS
0
0
//! A record read a stage at a time (README.md): the read `getRecord` makes,
//! taken apart, each stage the one before and one step more, over the same
//! records in a table for each account, read through one block cache as
//! the server reads them. The last stage, over HTTP, is `ladder.mjs`'s,
//! of the records `records` prints.
use crate::data::{SEED, State};
use crate::stores::{self, key, table_path};
use crate::util::{Rng, at, cores, par, scratch, us};
use pds_repo::data::{cid_string, decode, encode, to_json};
use pds_store::table::{BlockCache, Table};
use serde_json::json;
use std::hint::black_box;
use std::sync::Arc;
use std::time::{Duration, Instant};
/// The bytes the block cache may hold: more than the data, so that every
/// read after the first pass is of blocks it holds.
const CACHE: u64 = 1 << 30;
/// The records read one after another, and on each of the threads.
const LOOKUPS: usize = 20_000;
const THREADS: usize = 16;
const EACH: usize = 5_000;
/// The stages, each with what it adds to the one before.
const STAGES: [&str; 4] = [
"the path's CID, from the account's table",
"the record's block, by its CID",
"the block decoded, as the server decodes it (`pds_repo::data::decode`)",
"the record as `getRecord` answers it: `{uri, cid, value}` as JSON, `value` from `pds_repo::data::to_json`, serialized",
];
/// An account: its DID and its table.
struct Account {
did: String,
table: Table,
}
/// The CID and rev of the record at `path`, as the server takes them from
/// its `P` entry.
fn path_entry(a: &Account, cache: &BlockCache, path: &str) -> (Vec<u8>, String) {
let v = a
.table
.get(&key(b'P', path.as_bytes()), cache)
.expect("the table read")
.expect("the path");
let n = usize::from(v[0]);
let rev = String::from_utf8(v[1 + n..].to_vec()).expect("a rev");
(v[1..1 + n].to_vec(), rev)
}
/// The bytes of the block of `cid`, as the server takes them from its `B`
/// entry: its rev read, its bytes held as the server's source answers them
/// and copied into the record, as `pds_repo::Repo::get` does.
fn block(a: &Account, cache: &BlockCache, cid: &[u8]) -> Vec<u8> {
let v = a
.table
.get(&key(b'B', cid), cache)
.expect("the table read")
.expect("the record's block");
let n = usize::from(v[4]);
std::str::from_utf8(&v[5..5 + n]).expect("a rev");
let held: Arc<[u8]> = v[5 + n..].into();
held.to_vec()
}
/// A read of the record at `path` of `a`, as far as `stage`: the bytes it
/// leaves, so that nothing of it is left out.
fn read(stage: usize, a: &Account, cache: &BlockCache, path: &str) -> usize {
let (cid, rev) = path_entry(a, cache, path);
if stage == 1 {
return black_box((cid, rev)).0.len();
}
let bytes = block(a, cache, &cid);
if stage == 2 {
return black_box(bytes).len();
}
let value = decode(&bytes).expect("a record that decodes");
if stage == 3 {
return black_box((value, bytes)).1.len();
}
let (collection, rkey) = path.split_once('/').expect("a collection and a key");
let body = json!({
"uri": format!("at://{}/{collection}/{rkey}", a.did),
"cid": cid_string(&cid),
"value": to_json(&value),
})
.to_string()
.into_bytes();
black_box(body).len()
}
/// A record to read: its account and its path.
type Want = (usize, String);
/// `n` records drawn from all of them, the same for the same `which`.
fn wants(paths: &[Want], which: u64, n: usize) -> Vec<Want> {
let mut r = Rng::of(&[SEED, 9, which]);
(0..n)
.map(|_| paths[r.below(paths.len() as u64) as usize].clone())
.collect()
}
pub fn ladder(accounts: usize, records: usize) {
let dir = scratch("ladder");
std::fs::create_dir_all(dir.join("tables")).expect("a directory");
// The accounts' tables, as `reads` writes them; and every record held
// to decode, and to encode again to its own bytes, as the server's
// records do.
let made: Vec<(Vec<Want>, usize)> = par(accounts, cores(), |a| {
let s = State::new(SEED, a as u64, accounts as u64, records);
stores::write_table(&table_path(&dir, a), &s);
let mut bytes = 0;
for r in &s.records {
let v = decode(&r.bytes).expect("a record that decodes");
assert_eq!(encode(&v), r.bytes, "a record in canonical DAG-CBOR");
bytes += r.bytes.len();
}
(
s.records.iter().map(|r| (a, r.path.clone())).collect(),
bytes,
)
});
let block_bytes: usize = made.iter().map(|(_, b)| b).sum();
let paths: Vec<Want> = made.into_iter().flat_map(|(p, _)| p).collect();
let cache = BlockCache::new(CACHE);
let open: Vec<Account> = (0..accounts)
.map(|a| Account {
did: crate::data::did(SEED, a as u64),
table: Table::open(&table_path(&dir, a), &cache).expect("the account's table"),
})
.collect();
// One pass over every record, the whole read, so that the cache holds
// all a read touches.
let mut body_bytes = 0;
for (a, path) in &paths {
body_bytes += read(4, &open[*a], &cache, path);
}
let one = wants(&paths, 0, LOOKUPS);
let mut order: Vec<usize> = (0..one.len()).collect();
Rng::of(&[SEED, 10]).shuffle(&mut order);
let many: Vec<Vec<Want>> = (0..THREADS)
.map(|t| wants(&paths, 1 + t as u64, EACH))
.collect();
let rows: Vec<(Vec<Duration>, f64)> = (1..=STAGES.len())
.map(|stage| {
let mut times: Vec<Duration> = order
.iter()
.map(|&i| {
let (a, path) = &one[i];
let at = Instant::now();
black_box(read(stage, &open[*a], &cache, path));
at.elapsed()
})
.collect();
times.sort();
let started = Instant::now();
std::thread::scope(|scope| {
for mine in &many {
let (open, cache) = (&open, &cache);
scope.spawn(move || {
for (a, path) in mine {
black_box(read(stage, &open[*a], cache, path));
}
});
}
});
let rate = (THREADS * EACH) as f64 / started.elapsed().as_secs_f64();
(times, rate)
})
.collect();
println!(
"{accounts} accounts of {records} realistic records each, a table for each account read as the server reads one (`Table` through one `BlockCache`), after one pass over every record: every block a read touches is in the cache. A record's block is {} bytes on average; its `getRecord` body {} bytes. One at a time: {LOOKUPS} records drawn from all, on one thread; on {THREADS} threads, {EACH} each.\n",
block_bytes / paths.len(),
body_bytes / paths.len()
);
println!(
"| stage | adds to the one before | one at a time: p50 µs | p99 µs | on {THREADS} threads: per s |"
);
println!("|--:|---|--:|--:|--:|");
for (i, (adds, (times, rate))) in STAGES.iter().zip(&rows).enumerate() {
println!(
"| {} | {adds} | {:.2} | {:.2} | {rate:.0} |",
i + 1,
us(times[times.len() / 2]),
us(at(times, 99)),
);
}
drop(open);
let _ = std::fs::remove_dir_all(&dir);
}
/// The records of `accounts` accounts of `records` each, a line of JSON a
/// record, `{account, rkey, value}`: what `ladder.mjs` writes through the
/// server.
pub fn records(accounts: usize, records: usize) {
use std::io::Write;
let mut out = std::io::BufWriter::new(std::io::stdout().lock());
for a in 0..accounts {
for r in State::new(SEED, a as u64, accounts as u64, records).records {
let value = decode(&r.bytes).expect("a record that decodes");
let (_, rkey) = r.path.split_once('/').expect("a collection and a key");
let line = json!({ "account": a, "rkey": rkey, "value": to_json(&value) });
writeln!(out, "{line}").expect("written");
}
}
}