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

Tables: files held open, blocks of 4 KiB, a block cache in parts

Merged
Merged chadtmiller.com asked to merge into main from chadtmiller.com/monolith:store/table-reads

An account's table is read faster and by more threads at once: its file stays open between reads, its blocks are 4 KiB, and the block cache is in 64 parts. On the runner a record from the cache takes 1.6 µs where it took 4.2, sixteen threads read 994 thousand a second where they read 337 thousand, and an account touched holds 11.5 KiB where it held 34.8.

Claude Fable 5.2

What changes

spec/LIMITS.md, "Open files and threads". At most 64 accounts' tables are open at once, each one file descriptor, the least recently read closed to make room and opened again when next read; a repository or a space repo being sent holds its table's file until it is sent. Its check gives each of its 300 accounts a table.

harness/checks/limits.mjs. That check, openFiles.

crates/pds-store, table.rs.

  • A table's file is held open between reads, in a bound of 64 shared by every table of a BlockCache (pds_store::table::OPEN). A table whose file was closed opens it again at its next read and takes it only if its device, inode, size and time of change are those it was opened from: else the read is an error, never another table's bytes.
  • TableWriter writes blocks of 4 KiB, where it wrote 16. The index is cheaper, which is what makes that affordable: the shortest key that parts two blocks in place of a block's whole last key, and in memory each of sixteen keys as what follows the bytes it shares with the first: 17 to 23 bytes a block, where it took about 77. Tables written before read as they did.
  • BlockCache is in 64 parts, each behind its own lock, under one budget of bytes for all, counted before an item is held. A read looks in the cache before the bloom filter.

crates/pds-server. The callers: a store opens its table once with the cache; a getRepo and a space repo being sent hold the file they began with.

bench/store. The reads part reads the tables as the server reads them now; a row with every file held open, one of the layout before, and the benchmark's own reader with other checks on the same 4 KiB files.

Why

The store benchmark, beside RocksDB on the runner: a record not in the cache took a table 27 µs against 12.6, a listing of 50 took 404 against 187, sixteen threads read 337 thousand records a second from the cache against 443 thousand, and an account touched held 34.8 KiB against 12.6. The file was opened for each block read, a block was 16 KiB, and the cache was behind one lock.

How it was checked

  • cargo test -p pds-store -p pds-server: 31 and 387. New: more tables read than may be open, none leaked, each still read; a table replaced or deleted while open (one replaced by another whose blocks sit at the same offsets with good checks reads as an error); a table in the layout before, built byte for byte, still reads; the cache's budget held under sixteen threads.
  • The benchmark's reads on a Mac, before and after (2000 accounts of 500 records):
beforeafterRocksDB
a record from the cache, p50 µs4.92.25.8
sixteen threads from the cache, per s145 thousand1.8 million831 thousand
a listing of 50, cold, p50 µs448202136
the first read of an account, p50 µs773510
a record, cold, p50 µs31.727.6 (10.4 with every file open)9.1
KiB held an account touched36.613.814.0
  • The same on the runner (Linux), from the whole benchmark there as it then ran on a pull, beside the gates workflow's runs; before is bench/store/RESULTS.md as it was:
beforeafterRocksDB
a record from the cache, p50 µs4.21.65.1
sixteen threads from the cache, per s337 thousand994 thousand452 thousand
a listing of 50 from the cache, p50 µs653496
a listing of 50, cold, p50 µs404236196
the first read of an account, p50 µs652914
a record, cold, p50 µs27.121.5 (12.2 with every file open)12.6
sixteen threads, cold, per s149 thousand153 thousand179 thousand
KiB held an account touched34.811.512.6
  • The harness's run of 300 accounts under ulimit -n 256 (--limits, openFiles) had no account with a table: planted, tables' files never closed, it passed. It now gives each account three records of 140000 bytes, which compacts its log into a table, and reads one back from each after the start. Planted again: getLatestCommit under the limit fails, the server out of descriptors. Against the server as it was, which opened a table's file for each block and held none, it passes, as it must: the rule is one this change could break, not one the server broke.
  • just nightly here on this branch's head, the space repos' change under it as it has since merged: the six crash runs, the performance step and the scale run all pass.

Shortcomings

  • A record not in the cache gains less than it could: with 64 files open at most, most of 2000 accounts read at random are opened again, 21.5 µs on the runner against 12.2 with every file open, and 27.6 against 10.4 on macOS, where opening a file costs about 20 µs. The bound is fixed, to serve under 256 descriptors; one taken from the process's own limit would close most of that.
  • The first read of an account still opens its table and checks its index and filter: 29 µs on the runner against RocksDB's 14, which has nothing to open.
  • A cold listing reads 50 blocks, each checked with SHA-256: another check is a change of format, not made here.
  • Opening a large table takes about twice as long (0.8 ms against 0.37 for a repository of 64 MiB): four times the index entries to check. Once a load.
  • A record still in the log is read by opening the log's file for each: as before.

Review

table.rs: that a table opened again is the one that was opened, and that the index's shortened keys part blocks as whole keys did.

Approved

This pull holds an account's table file open between reads, with at most 64 files open across the cache. It cuts data blocks to 4 KiB, stores shortened index keys, and splits the block cache into 64 locked parts under one byte budget. I found nothing that must change.

  • Reopen check: a table opened again is accepted only if its device, inode, size and mtime match the ones it was opened from, and otherwise the read is an error. A snapshot holds the Arc<File> it began with, so a compaction or delete can't swap it mid-send.
  • Shortened index keys: shorten gives a key at or after the block's last key and before the next block's first. I checked it for the prefix case, the 0xff case and strict ordering, which open_file enforces. find, before and the grouped shared-prefix compare are correct, including when the search key is shorter than the shared prefix. Old tables with whole last keys read the same way, since readers never assumed whole keys.
  • File slots: Slot::hold never holds its own file lock while it takes the order lock, and eviction takes only one other slot's file lock at a time, so I see no lock inversion. Drops decrement the open count.
  • Cache: put counts bytes before it holds the item, takes one part lock at a time, and skips items over an eighth of the budget.
  • Spec and harness: the openFiles check now builds real tables, so it can fail on this rule, and LIMITS.md states the 64-table bound. STORE.md doesn't describe the table layout, so the block-size change needs no spec edit.

I did not read bench/store/src/reads.rs, bench/store/README.md and bench/store/RESULTS.md, which are benchmark code and generated output. I also did not read the tests in table.rs from line 1230 on, and the checks that ran were not consulted.

Merged as 0a88af0