mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
The reporter's snap, upgraded from 6.09 to 10.44, was in a systemd restart loop at restart 72, with 8 CPUs at load 7 and SQLITE_BUSY everywhere: error on boot.js Error [ValidationError]: Failed validation [msg_update.go:133 ...] [collection.go:191 sqlite.(*collection).UpdateAll] database is locked (5) (SQLITE_BUSY) Two things here, each of which made the other worse. (The third, in the FerretDB fork, is the `_txlock=immediate` default that lets the busy handler cover a contended write at all.) 1. A TRANSIENT DATABASE ERROR ENDED THE BOOT. Meteor's boot.js exits the process when a startup callback rejects, systemd restarts it, and the restart re-runs the same startup work against a database that is busy because the previous boot was doing it. SQLITE_BUSY means another writer had the lock - nothing is wrong with the data - so server/00startupResilience.js wraps every startup callback: a transient database error (the shared classifier's deadlock / too-many-connections / connection-lost / timeout) is logged, recorded for Admin Panel / Problems, and swallowed, and the step runs again next start. Anything else - a full disk, a refused login, a syntax error - is rethrown, so a real fault still stops WeKan. 2. THE BOARD-ID BACKFILL SCANNED EVERY CARD ON EVERY BOOT. It streamed the whole Cards collection and issued one multi-update per card - 130,947 of them here, for each of two collections - and its "anything left to do?" guard could never go quiet, because a checklist whose card was deleted has no board id to copy and stays missing one forever. It is driven by the rows that are MISSING the board id now (normally none), collects their distinct card ids in bounded chunks, and asks Cards only for those. What cannot be filled from a card is counted and reported once. The pass is version-gated like the schema upgrade beside it, so an unchanged version costs one findOne. tests/sqliteBusyResilience.test.cjs pins both, including that SQLITE_BUSY classifies as retryable contention while a full disk does not. Thanks to Nissulya and xet7 ! Fixes #6533,
149 lines
5.1 KiB
JavaScript
149 lines
5.1 KiB
JavaScript
import { MongoInternals } from 'meteor/mongo';
|
|
import Cards from '/models/cards';
|
|
|
|
// ============================================================================
|
|
// Memory-safe, idempotent backfill of a denormalized board id onto card-related
|
|
// collections (checklists, checklist items, comments).
|
|
//
|
|
// It must NOT load every document into memory (an early version used
|
|
// `.fetchAsync()` over all rows plus `ReactiveCache.getCard()` per row, which
|
|
// cached every card and ran the server out of heap on large databases).
|
|
//
|
|
// It must also not scan every CARD (#6533). The next version streamed the whole
|
|
// Cards collection and issued one multi-update per card - 130,947 cards on the
|
|
// reporter's instance, so ~131k writes per collection on EVERY boot, because the
|
|
// "is there anything left to do" guard could never go quiet: a checklist whose
|
|
// card has been deleted has no board id to copy and stays missing one forever.
|
|
// On a server that was already restarting in a loop that was a constant stream
|
|
// of writes into SQLite, and part of the load being reported.
|
|
//
|
|
// So the work is driven by the ROWS THAT ARE MISSING the board id - normally
|
|
// none - and never by the size of the card collection:
|
|
//
|
|
// 1. one findOne says whether any row is missing it at all;
|
|
// 2. the rows missing it are streamed and their DISTINCT card ids collected in
|
|
// bounded chunks, so peak memory is one chunk of ids;
|
|
// 3. each chunk asks Cards for just those ids and issues one multi-update per
|
|
// card that has a board id;
|
|
// 4. rows whose card is gone, or whose card has no board id, cannot be filled
|
|
// from a card at all: they are counted and reported once, not retried on
|
|
// every boot for the rest of the instance's life.
|
|
//
|
|
// And the pass is VERSION-GATED like the schema upgrade beside it
|
|
// (server/lib/schemaUpgradeSteps.js): after a completed pass the WeKan version
|
|
// goes into the `_wekan_migration` marker, so a boot on an unchanged version
|
|
// costs one findOne. A new release re-checks once. Force a re-check with
|
|
// WEKAN_FORCE_SCHEMA_UPGRADE=true.
|
|
// ============================================================================
|
|
|
|
const MARKER_COLL = '_wekan_migration';
|
|
|
|
// How many distinct card ids are resolved per round trip.
|
|
const CHUNK = 1000;
|
|
|
|
function appVersion() {
|
|
try {
|
|
return require('/package.json').version || '';
|
|
} catch (e) {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
// The raw driver collection for the marker: it is bookkeeping, not app data, so
|
|
// it has no Meteor collection, no schema and no hooks.
|
|
function markerCollection() {
|
|
const db = MongoInternals.defaultRemoteCollectionDriver()?.mongo?.db;
|
|
return db ? db.collection(MARKER_COLL) : null;
|
|
}
|
|
|
|
export async function backfillBoardIdFromCard(
|
|
collection,
|
|
{ cardField = 'cardId', boardField = 'boardId', label = 'collection' } = {},
|
|
) {
|
|
try {
|
|
const marker = markerCollection();
|
|
const markerId = `backfill-${boardField}-${label}`;
|
|
const version = appVersion();
|
|
const forced = process.env.WEKAN_FORCE_SCHEMA_UPGRADE === 'true';
|
|
|
|
if (marker && !forced) {
|
|
const done = await marker.findOne({ _id: markerId });
|
|
if (done && done.version === version) return; // done for this version
|
|
}
|
|
|
|
const anyMissing = await collection.findOneAsync(
|
|
{ [boardField]: { $exists: false } },
|
|
{ fields: { _id: 1 } },
|
|
);
|
|
|
|
let filled = 0;
|
|
let unresolved = 0;
|
|
|
|
if (anyMissing) {
|
|
const cursor = collection.find(
|
|
{ [boardField]: { $exists: false } },
|
|
{ fields: { _id: 1, [cardField]: 1 } },
|
|
);
|
|
|
|
let chunk = new Set();
|
|
|
|
const flush = async () => {
|
|
if (chunk.size === 0) return;
|
|
|
|
const ids = [...chunk];
|
|
chunk = new Set();
|
|
|
|
const cards = await Cards.find(
|
|
{ _id: { $in: ids } },
|
|
{ fields: { _id: 1, boardId: 1 } },
|
|
).fetchAsync();
|
|
|
|
for (const card of cards) {
|
|
if (!card.boardId) continue;
|
|
|
|
const n = await collection.direct.updateAsync(
|
|
{ [cardField]: card._id, [boardField]: { $exists: false } },
|
|
{ $set: { [boardField]: card.boardId } },
|
|
{ multi: true },
|
|
);
|
|
filled += n || 0;
|
|
}
|
|
};
|
|
|
|
await cursor.forEachAsync(async row => {
|
|
const id = row[cardField];
|
|
if (!id) return;
|
|
|
|
chunk.add(id);
|
|
if (chunk.size >= CHUNK) await flush();
|
|
});
|
|
|
|
await flush();
|
|
|
|
unresolved = await collection
|
|
.find({ [boardField]: { $exists: false } })
|
|
.countAsync();
|
|
|
|
console.log(
|
|
`[${label}] Backfilled ${boardField} on ${filled} row(s)` +
|
|
(unresolved ? `; ${unresolved} row(s) have no card to take it from` : '') + '.',
|
|
);
|
|
}
|
|
|
|
if (marker) {
|
|
await marker.updateOne(
|
|
{ _id: markerId },
|
|
{ $set: { version, at: new Date(), filled, unresolved } },
|
|
{ upsert: true },
|
|
);
|
|
}
|
|
} catch (e) {
|
|
// Never fatal: this runs in the background at startup, and a database that
|
|
// is momentarily busy must not cost the server its boot (#6533).
|
|
console.error(
|
|
`[${label}] Failed to backfill ${boardField} (will retry next start):`,
|
|
(e && e.message) || e,
|
|
);
|
|
}
|
|
}
|