Skip to content
quarry View Ticket
Login

View Ticket

Bound and queue expensive work: admission control, job surface, lane metrics

Closed8152617b9986ca2663c953bb069b15b71e2407bbCreated 105.1 days agoUpdated 68.0 days ago
Description2026-08-17 21:21:59
Every expensive operation quarry performs runs synchronously in the request goroutine, with no admission control anywhere in the process. There is no semaphore, no worker pool, and no queue: the only bound on concurrent work is how many requests arrive.

## What that costs today

Measured on the eleven-repository corpus:

| operation | cost | bound | |---|---|---| | `zit ls --long` (tk, 2,198 files) | 2,632 ms | none | | `zit cat` (small file) | 147 ms | none | | `zit info` (resolve a name) | 148 ms | none | | `archive.get` (tarball) | seconds | 256 MiB **buffered in memory** | | `attachment.get` | — | 32 MiB buffered |

Before the content cache landed, the file page saturated at **32 req/s** and then went backwards — 24 req/s at twenty concurrent readers, with latency 4x the single-request cost. Caching raised that ceiling to ~200 req/s but did not introduce a bound; it moved where the wall is.

**The urgent case: `archive.get` is `ops.Anonymous` and buffers up to `maxArchiveBytes` (256 MiB) per request with no concurrency limit.** Ten concurrent anonymous requests is 2.5 GiB resident and ten zit subprocesses. This is a denial of service reachable without credentials and should be bounded ahead of the wider design.

## Two problems, two mechanisms

They are worth separating, because conflating them produces a queue for things a reader should simply wait for.

**1. Bounded concurrency for synchronous reads** — `ls`, `cat`, `diff`, `info`.

A reader wants the file now; making it a job is worse than making it wait. What these need is a semaphore sized to the machine, so N concurrent readers do not fork N subprocesses, plus **load shedding**: past a wait threshold, refuse with 503 and `Retry-After` rather than accept work that will time out anyway. An accepted request that cannot be served on time is worse than a refused one, because it consumes the capacity that would have served someone else.

Queue depth and wait time become the scaling signal.

**2. Async jobs for genuinely long work** — `archive`, `reindex`, mirror pull, and eventually bulk operations.

These return a job id immediately. The UI shows queued → running → done with a link to the result. This is what makes cost visible instead of felt.

## quarry already has the machinery

None of this is new infrastructure, which is the argument for doing it properly rather than scattering semaphores:

- **JetStream work-queue** — the webhook dispatcher already uses one for durable, at-least-once delivery. - **`ci_run` in OLTP** — the precedent for tracking async work with a status a page can render (`internal/oltp/ci.go`). A `job` table is the same shape. - **Datastar SSE** — the fleet page already streams live state, and 0001 Principle 3's rule is already satisfied there: it renders state at load and a refresh works without JavaScript. A job page follows the same pattern. - **`/fleet`** — the admin surface where queue depth, wait percentiles and rejection counts belong, beside the ingest health already there.

## The SQLite single-writer argument

This is the part that makes a queue structural rather than defensive.

SQLite admits one writer at a time. Today that constraint is handled emergently — `busy_timeout=5000` on both the index and OLTP — so contention surfaces as a request that stalls up to five seconds and then fails with SQLITE_BUSY. The lease (`Repo.Guard`, 0001 §3) serializes writes ACROSS nodes but nothing serializes them within one.

A write queue makes the constraint explicit: writes are serialized by design, the depth is observable, and a caller learns "queued, position 3" instead of stalling on a lock and timing out. It also removes the pathological case where several writers each hold a five-second timeout while a long fold holds the write lock.

## Proposed shape

1. **`internal/work`** — a bounded executor: named lanes, each with a concurrency limit and a queue depth cap. Lanes because a slow archive must not starve a `cat`; one global pool would let the most expensive operation monopolise the cheapest. 2. **Lanes to start with**: `read` (zit reads, sized to cores), `write` (depth 1 — the SQLite writer), `bulk` (archive, reindex, mirror; depth 1–2). 3. **Shedding** — past the depth cap, refuse with `ops.ErrUnavailable`, which the router already maps to 503. 4. **`job` table in OLTP** — id, repo, kind, state, queued/started/finished, result ref, error. Modelled on `ci_run`, including its retention problem (see 8d5aa739f8: `ci_run` grows without bound — the job table must not repeat that). 5. **`job.list` / `job.get` ops**, a job page, and SSE for live state. Renders at load, refresh works without JS. 6. **Metrics on `/fleet`** — per-lane depth, in-flight, wait percentiles, sheds. This is the "do we need another node" number, and it does not exist today in any form.

## Order

The admission control is worth more than the job UI and is much smaller: bounding concurrency fixes the DoS and the degradation, while the queued/async surface makes it legible. Suggested sequence:

1. Bound `archive.get` immediately — the live exposure. 2. `internal/work` with the three lanes, wired into the zit read path and the write path. 3. Lane metrics on `/fleet`. 4. `job` table, ops, and the async UI for `bulk`.

Steps 1–2 remove the failure mode; 3–4 make it observable, which is the point of the exercise.

2026-09-04 09:52:54

The bound half landed (admission): --zit-parallel caps store subprocesses process-wide (default CPU count), callers past it are told busy — a 503 with Retry-After — after a five-second wait, and archive builds are bounded at two. Still open here: the job surface and lane metrics.

2026-09-04 09:57:42

Closed with the bound landed: that is the part that protects the server. The jobs page and lane metrics are observability and have their own Low ticket now.