Each JMAP sync pass reads every row of the account: _refresh calls source_flags per changes page #171
Labels
No labels
bug
enhancement
epic
needs-info
needs-triage
ready-for-agent
ready-for-human
svd:go
svd:hold
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
dev/mail-mind-mcp#171
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
On a large JMAP mirror, every sync pass reads every row of the account from disk, even when
Email/changesreports nothing._refreshcallsStore.source_flags(account)once per changes page, and that query reads three columns that sit afterbody_textin the row. On a store of about 249,000 messages this costs about 1 GB of disk reads per pass, and the pass interval grows from about 50 s to about 5 minutes once the page cache is smaller than the table.Where (v0.11.0; the same code in 0.10.6)
jmap.py:650_changescalls_refresh(created + updated, …)once perEmail/changespage.jmap.py:664_refreshcallsself.store.source_flags(account)before it looks atids, so an empty page still pays the full read.store.py:1951source_flags:SELECT source_id, flags, mailboxes FROM messages WHERE account = ? AND source_id IS NOT NULL, underself._lock, materialised into a dict of every row.jmap.py:698_reconcilereads it a second time on a mailbox change.In the
messagestable,source_id(column 26) andmailboxes(column 27) come afterbody_text(column 21). SQLite must walk past the body, including its overflow pages, to reach them. So the query reads close to the whole table, not three short columns.Evidence
One deployment, image
0.10.6@sha256:beace95e…a74, schema v15. One JMAP account with about 249,000 messages and a 2.7 GB database. One small IMAP account in the same store.synccontainer memory limit 768Mi. Counts and timings only.syncsince pod start (about 13.4 h)syncanonymous memorysource_flagsdict)syncpage cacheThe small IMAP account's pass interval also grows (p50 373 s, max 1,687 s) because it waits on the same store lock.
The interval change between 0.10.4 and 0.10.5 has no matching code change in the sync path. The likeliest cause is that the table outgrew the page cache the container limit allows, so each pass now goes to disk. That is an inference, not a measurement.
Proposed fixes
In order of size:
_refresh, return early whenidsis empty.SELECT source_id, flags, mailboxes FROM messages WHERE account = ? AND source_id IN (…), chunked to SQLite's variable limit. The existing partial unique indexmessages_source (account, source_id)serves the lookup, so it reads one row per id._backfilland_reconcile, which do need every id: extendmessages_sourceinto a covering index on(account, source_id, flags, mailboxes)with the sameWHERE source_id IS NOT NULL, so the scan reads the index, not the table.body_textto its own table, so a row scan never walks the body.Workaround in use
The deployment raises the
syncmemory limit so more of the table stays in page cache. This hides the cost; it does not remove it, and it does not scale with the mirror.Workaround measured: same deployment on 0.11.0 (same
source_flagscode),syncmemory limit raised from 768Mi to 2Gi. One hour, sampled every 10 minutes after the roll.syncdisk read ratesyncpage cachesyncanonymous memoryMore cache cuts the disk reads by about three quarters and halves the pass interval, but it does not remove the per-pass scan: the interval is still about three times the 47–60 s seen before the table outgrew the cache, and the cost grows with the mirror.