Repository navigation
perf(traffic): write the full-text index off the request path - #543
Conversation
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.
|
Review notes The approach is reasonable and the ordering is correct: Required:
Should fix:
Minor: memory is bounded by job count rather than bytes (worst case about 130 MiB pinned), and |
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.
|
Thanks, these were good catches. Pushed 8090668:
I left |
|
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. |
c6cb91b to
6fa1049
Compare
|
Merged, thanks @Phantom-IN. Rebased on current main and verified with its tests and negative control. |
Fixes #542.
Recording no longer writes the full-text index inline.
recordViewswrites 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:
ftsFilterwaits until everything queued before the call is indexed, so a body search finds any exchange recorded before it. That coverstraffic_search,ResponseContainingand the traffic page search, since they all go throughftsFilter.WHERE EXISTS (exchange with this rowid and id), so ifDeleteHostremoves an exchange before its entry is written, the entry is dropped and can't later match a new exchange that reuses the rowid.addis called afterwmuis released, because the worker needswmu.Closeindexes what's left, then closes the db. Without FTS5, or on a zero-valueTraffic, recording indexes inline like before.BenchmarkRecordScriptmeasures 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.