mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -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 !
124 lines
5.9 KiB
JavaScript
124 lines
5.9 KiB
JavaScript
'use strict';
|
|
|
|
// #6533 — FerretDB at 737% CPU and constant SQLITE_BUSY on a snap instance.
|
|
// Run: node tests/notificationCap.test.cjs
|
|
//
|
|
// The reporter's logs name the write every time:
|
|
//
|
|
// [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)
|
|
//
|
|
// addNotification is `$addToSet` on `profile.notifications` — an array INSIDE the
|
|
// user document. Adding one means reading the whole document, scanning the array
|
|
// for a duplicate and writing the whole document back, so the cost grows with the
|
|
// array. FerretDB on SQLite has a single writer, so those rewrites queue behind
|
|
// each other, start failing with SQLITE_BUSY, and login — which also writes to the
|
|
// user document — queues behind them. That is the slow login and the pinned CPU.
|
|
//
|
|
// WHY THE ARRAY GROWS FOREVER: the existing cleanup (#5685) only removes
|
|
// notifications that have been READ and are past their removal age. A user who
|
|
// never clears their tray accumulates entries with no upper bound at all, and
|
|
// nothing in the product ever shrinks it.
|
|
//
|
|
// So the cleanup now also CAPS the array. This suite pins the cap's decisions,
|
|
// which is where the judgement is: what to keep, what never to touch, and that a
|
|
// user needing no change is not written to at all.
|
|
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const repoRoot = path.resolve(__dirname, '..');
|
|
const {
|
|
cappedNotifications,
|
|
notificationCapFromEnv,
|
|
expiredNotificationActivityIds,
|
|
} = require(path.join(repoRoot, 'models/lib/notificationCleanup.js'));
|
|
const serverSrc = fs.readFileSync(
|
|
path.join(repoRoot, 'server/models/users.js'), 'utf8');
|
|
|
|
let passed = 0;
|
|
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
|
|
|
|
const notif = (n, read = null) => ({ activity: `a${n}`, read });
|
|
|
|
test('an array under the cap is left completely alone', () => {
|
|
const list = [notif(1), notif(2), notif(3)];
|
|
assert.strictEqual(cappedNotifications(list, 10), null,
|
|
'null means "nothing to do" - the caller must then skip the write entirely, ' +
|
|
'because rewriting the array with itself is exactly the write this is about');
|
|
assert.strictEqual(cappedNotifications(list, 3), null, 'exactly at the cap is not over it');
|
|
});
|
|
|
|
test('an oversized array keeps the NEWEST entries', () => {
|
|
const list = [notif(1), notif(2), notif(3), notif(4), notif(5)];
|
|
const kept = cappedNotifications(list, 2);
|
|
assert.deepStrictEqual(kept.map(n => n.activity), ['a4', 'a5'],
|
|
'$addToSet appends, so the tail is the newest - and an unread notification ' +
|
|
'from long ago is not something anyone is going to act on');
|
|
assert.strictEqual(kept.length, 2);
|
|
});
|
|
|
|
test('unread entries are capped too - that is the whole point', () => {
|
|
// The existing rule cannot touch these: it only removes READ ones.
|
|
const unread = Array.from({ length: 50 }, (_, i) => notif(i));
|
|
assert.deepStrictEqual(expiredNotificationActivityIds(unread, 2, new Date()), [],
|
|
'confirming the existing rule leaves every unread entry in place');
|
|
const kept = cappedNotifications(unread, 10);
|
|
assert.strictEqual(kept.length, 10, 'so the cap has to be what bounds them');
|
|
});
|
|
|
|
test('junk input never produces a write', () => {
|
|
for (const bad of [null, undefined, 'nope', 42, {}]) {
|
|
assert.strictEqual(cappedNotifications(bad, 10), null,
|
|
`${JSON.stringify(bad)} must be left alone rather than turned into an array`);
|
|
}
|
|
assert.strictEqual(cappedNotifications([notif(1), notif(2)], 0), null,
|
|
'a cap of 0 disables capping instead of deleting everything');
|
|
assert.strictEqual(cappedNotifications([notif(1), notif(2)], -5), null,
|
|
'and so does a negative one');
|
|
});
|
|
|
|
test('the default cap is generous, and configurable', () => {
|
|
assert.strictEqual(notificationCapFromEnv({}), 1000,
|
|
'this is a backstop against unbounded growth, not a retention policy');
|
|
assert.strictEqual(notificationCapFromEnv({ NOTIFICATION_TRAY_MAX_PER_USER: '50' }), 50);
|
|
for (const bad of ['', 'abc', '0', '-1', undefined]) {
|
|
assert.strictEqual(notificationCapFromEnv({ NOTIFICATION_TRAY_MAX_PER_USER: bad }), 1000,
|
|
`a nonsense setting (${JSON.stringify(bad)}) must fall back to the default, ` +
|
|
'not disable the backstop');
|
|
}
|
|
});
|
|
|
|
// ── how the cleanup uses it ─────────────────────────────────────────────────
|
|
|
|
test('the cap is applied to what is left AFTER the expiry pull', () => {
|
|
assert.ok(/const remaining = activityIds\.length/.test(serverSrc),
|
|
'capping the array before the $pull would trim entries that were about to be ' +
|
|
'removed anyway, and keep fewer than the cap');
|
|
assert.ok(/cappedNotifications\(remaining, maxPerUser\)/.test(serverSrc));
|
|
});
|
|
|
|
test('a user who needs neither is not written to', () => {
|
|
assert.ok(/if \(activityIds\.length === 0 && kept === null\) continue;/.test(serverSrc),
|
|
'the whole problem is writes to the user document; the cleanup must not add ' +
|
|
'one per user per day for users with nothing to change');
|
|
});
|
|
|
|
test('it stays ONE write per user', () => {
|
|
const at = serverSrc.indexOf('const maxPerUser = notificationCapFromEnv');
|
|
const body = serverSrc.slice(at, serverSrc.indexOf('const startNotificationCleanup', at));
|
|
const updates = (body.match(/await Users\.updateAsync\(/g) || []).length;
|
|
assert.strictEqual(updates, 1,
|
|
'#5685 collapsed K writes per user into one; the cap must not undo that by ' +
|
|
'adding a second update alongside the $pull');
|
|
assert.ok(/\$set: \{ 'profile\.notifications': kept \}/.test(body),
|
|
'the capped case replaces the array outright - a $pull would have to list the ' +
|
|
'entries it removes, and there being too many of them is the problem');
|
|
});
|
|
|
|
console.log(`\n${passed} passed`);
|