chadtmiller.com/monolith
An atproto PDS
0
0
harness: pds_log_bytes grows across a write that lets nothing go
harness/checks/operations.mjs+59−12
| @@ -178,11 +178,36 @@ const valuesOf = (families, family, keys, name = family) => { | ||
} | ||
const gauge = (families, name) => families[name]?.samples[0]?.value | ||
| ||
/// Each file under `dir`, at any depth, by its path: its inode and size. A | ||
/// file or directory gone while it is read is one not there. | ||
function filesUnder(dir) { | ||
for (;;) { | ||
try { | ||
const out = new Map() | ||
for (const f of readdirSync(dir, { recursive: true })) { | ||
const st = statSync(join(dir, String(f)), { bigint: true, throwIfNoEntry: false }) | ||
if (st?.isFile()) out.set(String(f), { ino: st.ino, size: Number(st.size) }) | ||
} | ||
return out | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') throw err | ||
} | ||
} | ||
} | ||
| ||
/// Every file under `dir`, in bytes. | ||
const bytesUnder = (dir) => readdirSync(dir, { recursive: true }).reduce((n, f) => { | ||
const st = statSync(join(dir, String(f))) | ||
return n + (st.isFile() ? st.size : 0) | ||
}, 0) | ||
const bytesUnder = (dir) => [...filesUnder(dir).values()].reduce((n, f) => n + f.size, 0) | ||
| ||
/// Whether a file of `after` was not in `before`, or another file there, | ||
/// or shorter; or one of `before` is not in `after` (each as `filesUnder` | ||
/// gives them): only a file that grew, or stayed as it was, is no change. | ||
function changedFile(before, after) { | ||
for (const [path, was] of before) { | ||
const is = after.get(path) | ||
if (!is || is.ino !== was.ino || is.size < was.size) return true | ||
} | ||
return [...after.keys()].some((path) => !before.has(path)) | ||
} | ||
| ||
/// A subscription from the start of the firehose: answers `{ws, seqs, | ||
/// opened, close()}`. | ||
| @@ -319,9 +344,20 @@ async function traceContext() { | ||
} | ||
} | ||
| ||
/// An account of the run's own on the server, and a record whose text the | ||
/// log must not hold: answers that record's commit, as its account's DID, | ||
/// How many records the canary writes, at most, for one written while the | ||
/// server let nothing of its logs go. | ||
const LOG_TRIES = 20 | ||
| ||
/// An account of the run's own on the server, and records whose text the | ||
/// log must not hold: answers the last one's commit, as its account's DID, | ||
/// `@` and its rev, the last write before the metrics are read. | ||
/// | ||
/// `pds_log_bytes` grows across each, unless the server let go of what its | ||
/// logs held meanwhile, which it does only as it makes, deletes, replaces | ||
/// or cuts short a file in the data directory (spec/OPERATIONS.md, | ||
/// "Metrics"): read the directory before the metrics and after them, | ||
/// around the write, and where a file there changed but by growing, write | ||
/// another record, until one is written with none so changed. | ||
async function canary() { | ||
const ip = '198.51.100.9' | ||
const front = `opscanary${SEED}` | ||
| @@ -335,11 +371,23 @@ async function canary() { | ||
} | ||
const made = await post('com.atproto.server.createAccount', { handle: `${front}.test`, email: `${front}@example.com`, password: `pw-${hash('canary password').slice(0, 20)}`, ...(inviteCode ? { inviteCode } : {}) }) | ||
if (made.status !== 200) await broke('createAccount', { handle: `${front}.test` }, 200, made) | ||
const text = `canary-${hash('canary record').slice(0, 24)}` | ||
noteSecret(text) | ||
const wrote = await post('com.atproto.repo.createRecord', { repo: made.body.did, collection: 'com.example.note', record: { $type: 'com.example.note', text } }, { authorization: `Bearer ${made.body.accessJwt}` }) | ||
if (wrote.status !== 200) await broke('createRecord', { canary: true }, 200, wrote) | ||
return `${made.body.did}@${wrote.body?.commit?.rev}` | ||
let rev | ||
let held = false | ||
for (let n = 0; n < LOG_TRIES && !held; n++) { | ||
const files = filesUnder(dataDir) | ||
const before = gauge(await scrape('before a canary record'), 'pds_log_bytes') | ||
const text = `canary-${hash(n ? `canary record ${n}` : 'canary record').slice(0, 24)}` | ||
noteSecret(text) | ||
const wrote = await post('com.atproto.repo.createRecord', { repo: made.body.did, collection: 'com.example.note', record: { $type: 'com.example.note', text } }, { authorization: `Bearer ${made.body.accessJwt}` }) | ||
if (wrote.status !== 200) await broke('createRecord', { canary: true }, 200, wrote) | ||
rev = wrote.body?.commit?.rev | ||
const after = gauge(await scrape('after a canary record'), 'pds_log_bytes') | ||
if (changedFile(files, filesUnder(dataDir))) continue | ||
if (!(after > before)) await broke('metrics', { metric: 'pds_log_bytes', after: 'a createRecord', meanwhile: 'no file in the data directory made, deleted, replaced or cut short' }, `more than ${before}`, after) | ||
held = true | ||
} | ||
count('metrics: pds_log_bytes across a write that let nothing go', held) | ||
return `${made.body.did}@${rev}` | ||
} | ||
| ||
/// The metrics: the format, each family as the spec gives it, and each held | ||
| @@ -381,7 +429,6 @@ async function checkMetrics(before, last) { | ||
const disk = bytesUnder(dataDir) | ||
const logBytes = gauge(families, 'pds_log_bytes') | ||
if (!(logBytes > 0 && logBytes <= disk)) await broke('metrics', { metric: 'pds_log_bytes', dataDir: `${disk} bytes` }, 'more than 0, and no more than the data directory holds', logBytes) | ||
if (!(logBytes > gauge(before, 'pds_log_bytes'))) await broke('metrics', { metric: 'pds_log_bytes', after: 'a createRecord' }, `more than ${gauge(before, 'pds_log_bytes')}`, logBytes) | ||
// Requests, by route and status, as this start's lines count them. | ||
const life = lives.at(-1) | ||
const lines = new Map() | ||
spec/OPERATIONS.md+25−9
| @@ -277,6 +277,18 @@ start again from 0 when it does, as Prometheus expects. A histogram has a | ||
| `pds_outbound_requests_total` | counter | `target`, `outcome` | each request the server makes | | ||
| `pds_outbound_request_duration_seconds` | histogram | `target` | each request the server makes, until its answer or failure | | ||
| ||
`pds_log_bytes` grows by what each write puts in the logs, and falls only | ||
as the server lets go of what they held: a log's generations folded into | ||
its checkpoint (an account's, its table: `STORE.md`, "Bounds"), which a write | ||
may start and which the server may run in the background at any time; | ||
and the journal's records, once what they hold is durable where it is | ||
kept (`DURABILITY.md`). It lets go of nothing but as it makes, deletes, | ||
replaces or cuts short a file in `PDS_DATA_DIR`, and what it lets go of | ||
leaves the gauge no sooner than that. So across a write during which no | ||
file there was made, deleted, replaced or cut short, each only grown or | ||
left as it was, the gauge grows, by however little; across one during | ||
which one was, it may be less after than before. | ||
| ||
The buckets: | ||
| ||
- requests and outbound requests: `0.005`, `0.01`, `0.025`, `0.05`, `0.1`, | ||
| @@ -647,15 +659,19 @@ is back at two thirds; the steps go on. Then, at the end: | ||
one of version `01` with more after it: each line's `trace_id` is new, or | ||
the one sent; each span the root of a new trace, or the child of the one | ||
sent; none for the unsampled. | ||
- **Metrics**: an account and a record of the harness's own, then: one more | ||
subscriber while it subscribes and `pds_firehose_seq` the last event's; | ||
`pds_accounts` as `listRepos` lists; `pds_log_bytes` grown by the record | ||
and no more than the data directory holds; every family's type, labels, | ||
label values and buckets; `pds_requests_total` equal to the `request` | ||
lines since the server started, by route and status, and no less than the | ||
harness's requests; each route's `_count` equal to its requests; | ||
`pds_rate_limited_total` equal to the 429s; the syncs grown by the | ||
record; a PLC request for every account made; `404` for another path. | ||
- **Metrics**: an account and records of the harness's own, then: one | ||
more subscriber while it subscribes and `pds_firehose_seq` the last | ||
event's; `pds_accounts` as `listRepos` lists; `pds_log_bytes` no more | ||
than the data directory holds, and grown across a record written while | ||
no file in the data directory was made, deleted, replaced or cut short, | ||
the directory read before the metrics and after them, around the write | ||
(a record written again, up to 20 in all, while one was); every | ||
family's type, labels, label values and buckets; `pds_requests_total` | ||
equal to the `request` lines since the server started, by route and | ||
status, and no less than the harness's requests; each route's `_count` | ||
equal to its requests; `pds_rate_limited_total` equal to the 429s; the | ||
syncs grown by the records; a PLC request for every account made; `404` | ||
for another path. | ||
- **The admin's reads**: `_logs` and `_stats` with no `Authorization`, a | ||
bearer token, the user `admin` with another password, and another user: | ||
each `401 AuthenticationRequired`; `POST`, `400 InvalidRequest`; `_logs` | ||