chadtmiller.com/monolith
An atproto PDS
0
0
//! RocksDB beside SQLite, set up as a server built on it would set it up:
//! three column families, `block` (an account's blocks by CID), `record`
//! (a path's CID and rev) and `event` (the firehose's frames by seq),
//! which share one log (WAL), so that a commit is one `WriteBatch` across
//! the three and one sync. The families are apart because their keys are:
//! events come in order of seq and are never written again, so their files
//! never overlap and a compaction moves them without rewriting them, and
//! an iterator over them steps over no block; blocks come at random keys
//! and are deleted, and are only ever looked up, so they get a bloom
//! filter; paths are few bytes each and are listed in order.
//!
//! A key of `block` and `record` begins with its account, four bytes big
//! end first, so that one database holds every account's and an account's
//! keys are together. Blocks are 4 KiB before compression and each carries
//! a check, XXH3, computed when it is read from its file: RocksDB's own of
//! both.
use crate::data::Commit;
use crate::stores::block_value;
use rocksdb::statistics::{Histogram, Ticker};
use rocksdb::{
BlockBasedOptions, Cache, ColumnFamily, ColumnFamilyDescriptor, DB, DBCompressionType, Options,
WriteBatch,
};
use std::path::Path;
pub const BLOCK: &str = "block";
pub const RECORD: &str = "record";
pub const EVENT: &str = "event";
/// The bytes a block cache may hold where a part gives it no other size:
/// what RocksDB gives one it makes for itself.
pub const CACHE: usize = 32 << 20;
/// How the blocks of a database's files are compressed: with Snappy, as
/// RocksDB does when it is not told; not at all; or with zstd.
#[derive(Clone, Copy, PartialEq)]
pub enum Pack {
Snappy,
None,
Zstd,
}
impl Pack {
pub fn name(self) -> &'static str {
match self {
Pack::Snappy => "Snappy",
Pack::None => "no compression",
Pack::Zstd => "zstd",
}
}
}
/// The options of a database and of its families: every database opened
/// from one `Setup` shares its block cache, its background threads and
/// its statistics.
pub struct Setup {
options: Options,
/// A family that is looked up by key, and one that is only iterated.
keyed: Options,
ordered: Options,
}
impl Setup {
/// `cache` is the block cache; `more` is options in RocksDB's own
/// words, as `write_buffer_size=1048576`, or nothing; with `counted`,
/// the databases count their syncs and the bytes they write (`syncs`,
/// `written`), which they do not unless asked.
pub fn new(cache: &Cache, pack: Pack, more: &str, counted: bool) -> Setup {
let mut options = Options::default();
options.create_if_missing(true);
options.create_missing_column_families(true);
// Its syncs are RocksDB's own: `fdatasync`, as a log's is
// `File::sync_data`; on macOS that is `F_FULLFSYNC`, as this
// workspace builds RocksDB (.cargo/config.toml).
options.increase_parallelism(crate::util::cores() as i32);
if counted {
options.enable_statistics();
}
match pack {
Pack::Snappy => {}
Pack::None => options.set_compression_type(DBCompressionType::None),
Pack::Zstd => options.set_compression_type(DBCompressionType::Zstd),
}
if !more.is_empty() {
options = options.get_options_from_string(more).expect("options");
}
let family = |bloom: bool| {
let mut table = BlockBasedOptions::default();
table.set_block_cache(cache);
if bloom {
table.set_bloom_filter(10.0, false);
}
let mut o = options.clone();
o.set_block_based_table_factory(&table);
o
};
Setup {
keyed: family(true),
ordered: family(false),
options,
}
}
fn families(&self) -> [ColumnFamilyDescriptor; 3] {
[
ColumnFamilyDescriptor::new(BLOCK, self.keyed.clone()),
ColumnFamilyDescriptor::new(RECORD, self.keyed.clone()),
ColumnFamilyDescriptor::new(EVENT, self.ordered.clone()),
]
}
/// The database at `path`, made if it is not there.
pub fn open(&self, path: &Path) -> DB {
DB::open_cf_descriptors(&self.options, path, self.families()).expect("a RocksDB")
}
/// The syncs of a log (WAL) the databases made, and the nanoseconds
/// they took.
pub fn syncs(&self) -> (u64, u64) {
let h = self
.options
.get_histogram_data(Histogram::WalFileSyncMicros);
(h.count(), h.sum() * 1000)
}
/// The bytes written since the databases were opened: what they were
/// given (keys and values), and what went to their logs, to files
/// written from memory, and to files written by compactions.
pub fn written(&self) -> [u64; 4] {
[
Ticker::BytesWritten,
Ticker::WalFileBytes,
Ticker::FlushWriteBytes,
Ticker::CompactWriteBytes,
]
.map(|t| self.options.get_ticker_count(t))
}
}
/// A family of `db`.
pub fn family<'a>(db: &'a DB, name: &str) -> &'a ColumnFamily {
db.cf_handle(name).expect("the family")
}
/// A key of `block` or `record`: the account, then the CID or the path.
pub fn key(account: usize, k: &[u8]) -> Vec<u8> {
let mut out = Vec::with_capacity(4 + k.len());
out.extend_from_slice(&(account as u32).to_be_bytes());
out.extend_from_slice(k);
out
}
/// A value of `record`, as a table's `P` value: `[CID length u8][CID][rev]`.
pub fn record_value(cid: &[u8], rev: &str) -> Vec<u8> {
let mut out = Vec::with_capacity(1 + cid.len() + rev.len());
out.push(cid.len() as u8);
out.extend_from_slice(cid);
out.extend_from_slice(rev.as_bytes());
out
}
/// What a commit puts in a database, as one batch: its blocks, the nodes
/// the account's commit before made taken out, its record's path, and its
/// event under its seq, big end first, with its account and time.
pub fn commit(db: &DB, account: usize, c: &Commit, last: &[Vec<u8>]) -> WriteBatch {
let mut batch = WriteBatch::default();
let block = family(db, BLOCK);
for (cid, b) in &c.blocks {
batch.put_cf(block, key(account, cid), block_value(&c.rev, b));
}
for cid in last {
batch.delete_cf(block, key(account, cid));
}
batch.put_cf(
family(db, RECORD),
key(account, c.path.as_bytes()),
record_value(&c.blocks[1].0, &c.rev),
);
let event = event(account, c.op, &c.event);
batch.put_cf(family(db, EVENT), c.op.to_be_bytes(), event);
batch
}
/// A value of `event`, under its seq: `[account u32 LE][time u64 LE]`, then
/// the frame.
pub fn event(account: usize, seq: u64, frame: &[u8]) -> Vec<u8> {
let mut out = Vec::with_capacity(12 + frame.len());
out.extend_from_slice(&(account as u32).to_le_bytes());
out.extend_from_slice(&(1_700_000_000_000u64 + seq).to_le_bytes());
out.extend_from_slice(frame);
out
}
/// The frame a value of `event` holds.
pub fn frame(value: &[u8]) -> &[u8] {
&value[12..]
}