mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 04:49:41 -05:00
#6533. A snap instance reports FerretDB at 737% CPU, logins taking over a minute, boards not appearing, and logs full of: [db-retry] the database was busy: 0 write(s) retried, 0 succeeded on a retry, 3 given up on. ValidationError: ... [collection.go:191 sqlite.(*collection).UpdateAll] database is locked (5) (SQLITE_BUSY) at _helpersConstructor.addNotification (models/users.js:2248) The stack names the same write every time. addNotification is an `$addToSet` on `profile.notifications`, which is an array INSIDE the user document: adding one entry means reading the whole document, scanning the array for a duplicate, and writing the whole document back. The cost is proportional to the array. FerretDB on SQLite has a SINGLE writer, so those rewrites queue behind each other and start failing outright - and login, which also writes to the user document, queues behind them. That is the slow login, the boards that never arrive and the pinned CPU, all from one array. WHY IT GROWS WITHOUT LIMIT: the cleanup added in #5685 removes a notification only when every entry for its activity has been READ and is past its removal age. A user who does not clear their tray accumulates entries forever, and nothing else in the product shrinks that array. The instance in the report has been running since at least 2026-07-29 with contention warnings, which is what an array that only ever grows looks like from the outside. So the same cleanup pass now also caps it: the newest NOTIFICATION_TRAY_MAX_PER_USER entries are kept (default 1000, a backstop rather than a retention policy) and the rest dropped. Newest rather than oldest because `$addToSet` appends, so the tail is the recent activity a tray is for; an unread notification from months ago is not something anybody is going to act on. Three things it is careful about. The cap is applied to what is LEFT AFTER the expiry pull, not before, or it would trim entries that were about to be removed anyway and keep fewer than the cap. A user needing neither change is not written to at all - the whole problem here is writes to user documents, so the cleanup must not add one per user per day for users with nothing to do. And it stays ONE write per user, which is what #5685 collapsed K writes into; the capped case uses `$set` because a `$pull` would have to list the entries it is removing, and there being too many of them is the point. This bounds the array. It does not make SQLite a multi-writer engine: an instance this busy still wants the PostgreSQL backend, which is what the Problems pane already tells the admin. tests/notificationCap.test.cjs pins the decisions - keeping the newest, leaving an array under the cap completely alone (so no write), refusing to act on junk or a disabled cap, that unread entries are what this catches because the existing rule provably cannot, and that it is still one write per user. Thanks to Nissulya and xet7 !