Skip to content

perf(traffic): write the full-text index off the request path - #543

Merged
BaaRai merged 3 commits into
BaaRai:mainfrom
Phantom-IN:perf/traffic-async-fts-index
Nov 26, 2026
Merged

BaaRai merged 3 commits into
BaaRai:mainfrom
Phantom-IN:perf/traffic-async-fts-index

Conversation

@Phantom-IN

Copy link
Copy Markdown
Contributor

Fixes #542.

Recording no longer writes the full-text index inline. recordViews writes the exchange, bodies, blob refs and trace as before, then hands the index text to a single background worker (traffic/fts_queue.go). The proxy replies without waiting for the trigram indexing.

How it stays correct:

  • Searches stay consistent. ftsFilter waits until everything queued before the call is indexed, so a body search finds any exchange recorded before it. That covers traffic_search, ResponseContaining and the traffic page search, since they all go through ftsFilter.
  • Deletes are handled. The worker inserts with WHERE EXISTS (exchange with this rowid and id), so if DeleteHost removes an exchange before its entry is written, the entry is dropped and can't later match a new exchange that reuses the rowid.
  • Memory is bounded. At most 16 bodies wait in the queue; a recorder blocks when it's full. add is called after wmu is released, because the worker needs wmu.
  • Shutdown drains it. Close indexes what's left, then closes the db. Without FTS5, or on a zero-value Traffic, recording indexes inline like before.

BenchmarkRecordScript measures record time for a 1 MiB script with the indexer idle: 52.3 ms on main, 1.47 ms here. With a real 1.3 MB minified bundle, main took 163 ms.

Tests: a body search right after 20 back-to-back recordings (more than the queue holds) finds the last one, and a queued entry for a missing exchange isn't written. go test -race ./traffic/ passes, and ./server/ and ./agent/ pass apart from the two tests that fail on my Mac on main too.

The proxy records an exchange in its Response hook, before the client gets
the response, and recordViews wrote the trigram index of each body (up to
4 MiB) inside its transaction while holding wmu. That is almost all of the
recording cost: a 1 MB minified script takes ~163 ms with the index and
~1 ms without, and every other request being recorded waits on wmu behind it.

Queue the index write instead and let one background worker do it.
Searches that use the index wait for everything queued before them, so a
body search still finds an exchange recorded before it. The worker only
writes an entry whose exchange still exists with the same rowid and id, so
a delete in between can't leave an entry that matches a later exchange.
The queue holds at most 16 bodies; a recorder waits when it is full.

Fixes BaaRai#542.
@5P34R

5P34R commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Review notes

The approach is reasonable and the ordering is correct: add runs before the proxy replies, so a search issued after a response waits for that exchange's entry, and I found no deadlock. A few things should be addressed before merge.

Required:

  • Index entries are lost permanently on crash/kill. The exchange commits in writeViews, but its index entry waits in the in-memory queue (traffic/fts_queue.go, up to 16 jobs). After OOM, SIGKILL or power loss, those exchanges exist in exchanges but traffic_search body search and ResponseContaining never find them, and the package has no rebuild/backfill path. On Open, re-index rows missing from the index (e.g. rowids above the highest indexed one), or keep a small persisted pending marker.
  • The shutdown drain is unbounded. Close → ftsq.close() indexes every queued job serially under wmu, and isn't interrupted by t.closed. With 16 bodies of about 4 MiB each (~650 ms per body at the PR's own 160 ms/MB), that's about 10 s, which hits Docker's default stop grace. The process is then SIGKILLed and the entries are lost (previous point).

Should fix:

  • TestReclaimMergesFTSTombstones (reclaim_test.go:74) now deletes hosts before their entries are indexed, so it no longer creates tombstones and passes without testing anything. Call tr.ftsq.wait() before the deletes.
  • The recordViews doc comment (traffic.go ~708) still says the index is written in the same transaction.
  • ftsQueue.index holds wmu for the whole trigram insert, so under a burst a response recorded behind a large body still waits about 650 ms. The gain is real mainly for an idle recorder, so please scope the CHANGELOG wording to that.

Minor: memory is bounded by job count rather than bytes (worst case about 130 MiB pinned), and wait() has no context or timeout.

Review follow-up for the background indexer.

The queue held index text in memory only, so a kill or crash left committed
exchanges that body search never found, and Close drained every queued body
under wmu, which could run past a container stop timeout.

The text now goes into ex_fts_pending in the same transaction as the
exchange. The worker moves one entry into ex_fts and deletes its pending row
in one transaction; Open queues whatever is left. Jobs carry only rowid and
id, so memory no longer grows with body size, and Close stops after the
current entry. wait() gives up after 30 s so a search during a large
post-crash backlog still returns.

Also: TestReclaimMergesFTSTombstones waits for the indexer before deleting,
so it creates tombstones again; recordViews' comment describes the new
flow; the CHANGELOG no longer claims the gain holds under a burst.
@Phantom-IN

Copy link
Copy Markdown
Contributor Author

Thanks, these were good catches. Pushed 8090668:

  • Crash/kill: the index text now goes into ex_fts_pending in the same transaction as the exchange. The worker moves it into ex_fts and deletes the pending row in one transaction, and Open queues whatever is left. TestPendingIndexSurvivesRestart stops the worker, records, reopens, and finds the exchange by body search.
  • Shutdown: Close now stops after the entry in progress instead of draining the queue; the rest waits in ex_fts_pending for the next start.
  • Jobs are just (rowid, id) now, so memory doesn't scale with body size. wait() gives up after 30 s and logs, so a search during a post-crash backlog still returns.
  • The reclaim test waits for the indexer before deleting, the recordViews comment is updated, and the CHANGELOG now says the gain is for an idle recorder (record time for a 1 MiB script is ~5 ms here vs 52 ms on main).

I left wmu around the insert. Dropping it wouldn't help, since SQLite allows one writer at a time anyway.

BaaRai added a commit to Phantom-IN/ARTEX that referenced this pull request Nov 26, 2026
@BaaRai

BaaRai commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Thanks @Phantom-IN, not merged yet: a CHANGELOG conflict appeared after other PRs landed while this was queued. I am rebuilding it on current main and merging next.

@BaaRai
BaaRai force-pushed the perf/traffic-async-fts-index branch from c6cb91b to 6fa1049 Compare November 26, 2026 22:24
BaaRai added a commit to Phantom-IN/ARTEX that referenced this pull request Nov 26, 2026
@BaaRai
BaaRai merged commit acc383c into BaaRai:main Nov 26, 2026
6 checks passed
@BaaRai

BaaRai commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Merged, thanks @Phantom-IN. Rebased on current main and verified with its tests and negative control.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Capture proxy holds each response while the full-text index is written

3 participants