chadtmiller.com/monolith
An atproto PDS
0
0
//! `pds`: `pds serve` runs the server, `pds migrate-blobs` moves its blobs
//! while it is stopped; anything else is the CLI, calling a running one.
#![forbid(unsafe_code)]
/// jemalloc rather than the system's allocator, built to keep freed pages
/// for no time at all, in a few arenas whatever the machine's cores
/// (`.cargo/config.toml`): the server's resident memory is what
/// `spec/STORE.md`'s "Scale" bounds, and the system's allocators keep what
/// a large write freed resident, each in its own way. So memory follows
/// what is in use, the same on Linux and macOS.
#[global_allocator]
static ALLOCATOR: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
const SERVE_HELP: &str = "Usage: pds serve\n\n\
Run the server, configured by its environment: PDS_PORT, PDS_BIND_ADDRESS\n\
(127.0.0.1 if unset), PDS_DATA_DIR, PDS_HOSTNAME, PDS_PUBLIC_URL,\n\
PDS_HANDLE_DOMAINS, PDS_PLC_URL, PDS_PLC_ROTATION_KEY_HEX and, if set,\n\
PDS_RECOVERY_DID_KEY; PDS_EMAIL_SMTP_URL and PDS_EMAIL_FROM, both or\n\
neither; PDS_DEV_MAIL_DIR, for development only; its log, metrics and traces\n\
by PDS_LOG_LEVEL, PDS_METRICS_PORT and the OTEL_* variables. SIGTERM or\n\
SIGINT shuts it down.\n";
const MIGRATE_HELP: &str = "Usage: pds migrate-blobs --to bucket|disk\n\n\
Move the data directory's blobs into the bucket PDS_BLOBSTORE_S3_BUCKET\n\
names (--to bucket), or out of it into the data directory (--to disk),\n\
with no server running on it. It is given the environment pds serve is,\n\
with the bucket's variables set either way. Killed or stopped, run it\n\
again: with the same --to it carries on, with the other it moves them\n\
back. Done, it prints {\"moved\":<blobs>,\"bytes\":<bytes>}; then start pds\n\
serve with the environment of where they are.\n";
/// No transparent huge pages for this process, where the system gives them
/// to every process (Linux, set to `always`): with them a thread's stack,
/// and each region the allocator maps, is resident 2 MiB at a time however
/// little of it is used, and the server's resident memory follows its
/// threads, not what it holds. A system that refuses is left as it is.
fn small_pages() {
#[cfg(target_os = "linux")]
let _ = rustix::thread::disable_transparent_huge_pages(true);
}
fn main() {
let args: Vec<String> = std::env::args().skip(1).collect();
match args.first().map(String::as_str) {
Some("serve") => serve(&args[1..]),
Some("migrate-blobs") => migrate_blobs(&args[1..]),
_ => std::process::exit(pds_cli::run(&args)),
}
}
fn serve(args: &[String]) {
match args.first().map(String::as_str) {
None => {
small_pages();
pds_server::run()
}
Some("--help" | "-h") => print!("{SERVE_HELP}"),
Some(a) => {
eprint!("pds: pds serve takes no argument {a:?}\n\n{SERVE_HELP}");
std::process::exit(2);
}
}
}
fn migrate_blobs(args: &[String]) {
let to = match args {
[h] if h == "--help" || h == "-h" => return print!("{MIGRATE_HELP}"),
[flag, to] if flag == "--to" => pds_server::Place::named(to),
_ => None,
};
let Some(to) = to else {
eprint!(
"pds: pds migrate-blobs takes --to bucket or --to disk, not {args:?}\n\n{MIGRATE_HELP}"
);
std::process::exit(2);
};
pds_server::migrate_blobs(to)
}
#[cfg(test)]
mod tests {
/// The server's memory is in the system's small pages.
#[cfg(target_os = "linux")]
#[test]
fn no_huge_pages() {
super::small_pages();
assert_eq!(
rustix::thread::transparent_huge_pages_are_disabled(),
Ok(true)
);
}
/// The build is given the allocator's options, as `.cargo/config.toml`
/// sets them for jemalloc's build and this crate's alike: a build
/// without them keeps freed pages resident for seconds, and slabs part
/// full in four arenas for each core, past `spec/STORE.md`'s memory
/// bound. What the allocator then does with them is the scale run's to
/// hold (`harness/compare.mjs --scale`).
#[test]
fn the_allocator_keeps_no_freed_pages() {
let built = option_env!("JEMALLOC_SYS_WITH_MALLOC_CONF").unwrap_or("");
for option in ["dirty_decay_ms:0", "muzzy_decay_ms:0", "narenas:8"] {
assert!(
built.split(',').any(|o| o == option),
"jemalloc built with {built:?}, without {option}"
);
}
}
}