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

After a shutdown, the firehose's first message comes within a second

Merged
Merged chadtmiller.com asked to merge into main from chadtmiller.com/monolith:store/firehose-first-event

After a shutdown and a start, the firehose's first message no longer waits while the server reads every account: with 100,000 accounts it took 8.2 s. The server writes an index of the window's events as it shuts down and reads that at the next start. After a kill or a power cut it reads its accounts as before; nothing is added to a write.

Claude Fable 5.2

What changes

spec/STORE.md, "Scale". After a start that follows a shutdown (SIGTERM), a subscribeRepos with a cursor is sent its first message within a second of asking, whatever the number of accounts; after a kill or a power cut the first such subscription may wait while the server reads its accounts' files, and is sent the same events. The scale run ends by asking for the firehose from cursor 0 on a start that followed a kill, as a relay would, however long that takes; writing a record; shutting the server down and starting it; asking from 0 and from the seq before that record's commit, each first message within a second; then writing another record, killing the server, and asking from the seq before that one's commit, which must be sent.

spec/DURABILITY.md. Data that cannot be recovered stops the server as it starts, for what it reads then, and for what is one account's when that account is first used: what the review of at://did:plc:bcgltzqazw5tb6k2g3ttenbj/space/dev.knotless.space/spec-pds/did:plc:bcgltzqazw5tb6k2g3ttenbj/dev.knotless.pull/3n22xnkbjng7t asked to have said.

crates/pds-server.

  • events/index.rs, new: events.index in the data directory, written beside itself, synced and renamed: a header (the greatest seq reserved in the service log, the greatest seq of an event before, the greatest let go, the window) and each event of an account's log in the window, its seq, time and account. About 36 bytes an event.
  • ops.rs, events.rs: an orderly shutdown stops numbering events, makes the logs durable, and writes the index, if the server knows every event in the window: its own, and those before its start.
  • events.rs: find_old takes the events before the start from the index when it holds, reading no account's files, and by the scan of every account when it does not. Once the server serves it reads a valid index in the background.
  • The index holds exactly when no event was numbered after it was written: when the greatest seq reserved it names is the one the start read from the service log. A run's first event always reserves seqs anew, durably, before it is numbered, so a run that numbered one and was killed leaves the service log ahead of the index, and nothing has to delete or mark it. A snapshot of a running server is the same.
  • An event's time in memory is its frame's, not when its seq was reserved: so that the index and the scan agree.

Why

After a start, the first subscription with a cursor waited while the server read every account's files for the events made before the start: 8.2 s with 100,000 accounts on this Mac, the files in memory, and growing with the accounts. A relay asks with a cursor every time it reconnects.

An index made durable with each commit would hold after a power cut too, and was measured with the store benchmark as it then was: each commit waits on one more sync, shared by all accounts. From 16 writers that took writes from 363 a second to 144 on the Mac, and from 413 to 130 on the runner. So the index is written once, at shutdown: a deploy is the common restart, and a crash is where waiting is fair.

How it was checked

  • The scale run against the server before: the firehose from 0, the first request after a start, 8200 ms against 1000. With this, 100,000 accounts, the machine busy: after a kill 14.9 s (8.2 s on a quiet one, as before); after a shutdown and a start, 438 ms from cursor 0 and 44 ms from the seq before the last commit (176 and 1 on a quiet one); after a shutdown, a start, a write and a kill, the commit written since is sent.
  • Planted, the scale run at 20,000 accounts to save the wait: the index never taken, and the firehose from 0, after a shutdown and a start fails, 1507 ms against 1000; the index taken whatever its ceiling, and the firehose after a shutdown, a start, a write and a kill fails, the commit written since the shutdown never sent. No run of the harness shut the server down and then killed it before: that last step is new for it.
  • The server's own tests, 377. New with the index: a start after a shutdown reads no account and replays byte for byte what a scan does; an index is ignored once an event was numbered after it, and holds while none was; events out of the window as it is read; a deleted account's events; the index cut at every byte and its temporary file left. With the rule that a run's first event reserves anew taken out, two of them fail. New since, seven in events::tests: what published is at each kind of start, before and after a run's first events; 64 histories of kills, shutdowns and runs with no event, the events before the start found from the index before the run's first events, after them, on a subscriber's need or never; the index taken on one thread as events are numbered on another; a subscription from cursor 0 opened as a run's first events land; one over more accounts than logs are held open. Each holds a replay from 0 to every event made, in order, none twice, and published to the last one's seq. Planted, exact not lowering what is published and merge dropping the run's own events each fail several.
  • just quick and the crash runs here. Of the crash runs' six, one failed after its 1000 steps agreed, in the spans check, with the machine at a load of 20 from other work; the --limits run fails the same way under that load on main.
  • The gates workflow on this pull ran beside the store-bench workflow, which loads the runner's disk, and failed three times in three places it had passed before, none in what this pull changes. --migrate: at://did:plc:bcgltzqazw5tb6k2g3ttenbj/space/dev.knotless.space/spec-pds/did:plc:bcgltzqazw5tb6k2g3ttenbj/dev.knotless.issue/3n23dcwizalsq. An OAuth token refused after a kill: at://did:plc:bcgltzqazw5tb6k2g3ttenbj/space/dev.knotless.space/spec-pds/did:plc:bcgltzqazw5tb6k2g3ttenbj/dev.knotless.issue/3n242f36ybwsq. The metrics check's pds_firehose_seq: followed through the run's own seqs, the metric was right and the check had stopped listening in the middle of the replay, after a second with no event; its wait is fixed in a pull of its own (at://did:plc:bcgltzqazw5tb6k2g3ttenbj/space/dev.knotless.space/spec-pds/did:plc:bcgltzqazw5tb6k2g3ttenbj/dev.knotless.pull/3n242qjib4rsq), and the tests above were written to rule the server out.

Shortcomings

  • After a kill or a power cut, the first subscription with a cursor still waits on a read of every account: 8.2 s for 100,000 here, a minute or more for 500,000.
  • A run that followed a kill and was never asked for the firehose with a cursor shuts down without an index: the next start is as after a kill.
  • The index is read whole: its time and memory follow the events in the window, about 36 bytes each.
Approved

This adds events.index, written at an orderly shutdown and read at the next start, so the firehose's first message from a cursor no longer waits on a scan of every account. The index is taken only when its ceiling equals the one the start read from the service log, and the run's first event reserves seqs anew, so any run that numbered an event invalidates it. I found no flaw in that reasoning.

What I checked

  • index.rs writes the file to a temp name, syncs it and renames it. Reads reject a wrong version, ceiling, count or ordering, any torn or altered bytes, and a window shorter than the start's.
  • settle in ops.rs seals, syncs the logs, and only then writes the index. It writes none if any event is still on its way to disk. It also writes none if the events before the start are not yet found, or a scan is still running.
  • In find_old, find_indexed, none_before, indexed and merge, a deleted account's events and the service log's own events stay out of the index. A rejected index falls back to the scan.
  • The spec covers the behaviour: STORE.md "Scale" and DURABILITY.md are updated, and the scale run adds the new steps. The harness change makes the run stricter, and it adds a case that kills the server after a shutdown. docs/guide/comparing.md is updated. The change adds no crates.

What I did not read

  • I read only events.rs lines 431–860 at the head commit, plus the new tests the searches returned. The rest of that 1,572-line diff was not read: the seq reservation, land and published changes, find_indexed_soon, and most of the new tests.
  • The gates and store-bench runs on this head were still running when I looked, so I have not seen their results.

Minor

  • spec/DURABILITY.md also adds "two accounts kept with one handle or one email" to the list of unrecoverable data. That is outside this pull's topic. Its body ties it to the earlier review that asked for it, so I took it as intended.
Approved

Since my approval at 2d42845, the only change is in harness/checks/scale.mjs. seqOf now resolves only once the write has also been answered, and rejects if the write fails. This fixes a race: the step that kills the server could otherwise run while the write was still in flight. The check gets stricter, not looser, and the change needs no spec or docs update. What I approved before stands.

Approved

Carried forward: the change this pull makes at af9a152fb609e78c1bf54835cd5cd91f47b3c5da is the one reviewed at 7ae74952e529093e47ce6b5181fa007c55d9b324, so that review stands. What is new comes from its base.

Merged as e2badf2