Designs
Page 3 of 8 · Never blocks

The daemon must never stall while it serves many ferrets

The design is still being written; the numbers from before the fix are in.

Status

The never-block design is still being written, and its first numbers are in

Status: coming. The full design document is not finished, so this page does not claim a design yet. When its pull request exists, this page and the site will be updated.

What we do have is the first piece: a measured problem and a plan to fix it.

The daemon does its core work in one lane, the main thread. Anything that hogs that lane delays every request.

The daemon also keeps a list of every past conversation. That list is the index, built by reading every transcript file on the machine. Our own machine has about 66,000 of them.

The question is simple. Does building and refreshing the index keep the main thread busy?

Before numbers

Refreshing the index keeps the main thread 12 to 98 percent busy

We built a fake store of 66,000 transcript files and measured the unchanged code on it. Each bar shows how busy the main thread was in one scenario:

Main thread busy by scenario
Higher is worse. Unchanged code, 66,000 synthetic files.
Idle, five untitled ferrets
12.1 %
Listing every 5 s
20.6 %
Hot appends, then listings
23 %
One append to a 1 GiB file
23.4 %
Cold build, 66,000 tiny files
89.1 %
Cold build, 2,000 big files
98.2 %
Show as table
%
Idle, five untitled ferrets12.1
Listing every 5 s20.6
Hot appends, then listings23
One append to a 1 GiB file23.4
Cold build, 66,000 tiny files89.1
Cold build, 2,000 big files98.2

Measured on one busy 32-core machine, load about 12 from other work.

Even an idle daemon with five untitled ferrets spends 12.1% of its main thread on this (M). The cause is the title sweep, a check every 5 seconds that gives untitled ferrets a name. It lists the whole file tree just to look at a few files, about 88,000 file calls per tick (C).

The worst case is the first start. Parsing 2,000 files of 512 KiB took 8.0 ms of main-thread time each (M). If all 66,000 files were that big, it would be 525 seconds of main-thread work on first start (C). Real files vary, so read it as a rough ceiling.

What users feel

Listings wait a quarter second and tails reach over fifty milliseconds

A busy main thread shows up as slow answers, not one long freeze. Here are the numbers a person would feel:

Stalls and latencies, unchanged code. p50 is the typical answer; p99 is the slowest one in a hundred.
ScenarioLongest stallListing p50Fleet list p99
Cold build, tiny files146 ms (M)49 ms (M)1.9 ms (M)
Idle, title sweep110 ms (M)none0.3 ms (M)
Listing every 5 s135 ms (M)305 ms (M)0.3 ms (M)
One append to a 1 GiB file149 ms (M)309 ms (M)56.5 ms (M)

A listing took 305 ms typically, because it waits up to 250 ms for the refresh to finish (D, from the code). A file appended to long ago was not seen until the second listing (M).

No single stall passed 160 ms, so the damage is steady busyness and queueing, not a freeze. That matters: a daemon that is busy 12% of the time forever is a slow tax on everything.

The plan

Three small fixes are planned, and the biggest one removes the tree walk

The plan was read from the code and its own probes. The bench numbers above came after it. It has a test-only first step and three fixes, all in one plugin.

  1. Measure first
    A bench on a fake 66,000-file store records the before numbers above.
  2. Title sweep reads only running ferrets
    No tree walk. The idle cost should fall near zero (G).
  3. List only what changed
    Re-list folders whose time changed, and run a slow paced full sweep as a backstop.
  4. Parse in a worker, under a budget
    Move parsing off the main thread and cap busy time at about 20 percent (G).

We decided not to build a file watcher, because watch limits differ by system. A fourth fix, paging the history read, is held back until the bench shows a 391 ms blocking read (M, second planner). We dropped database paging for lists, because the query itself takes only 50 ms for 50 rows (M).

A known gap: an old file that gets an append is seen only by the backstop, within about 15 minutes (G). The first start is the reason to ship the worker now.