mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 10:48:06 -05:00
The reported crash series (.tools/crash) was invisible from inside WeKan: MongoDB aborting on every checkpoint, restarting, aborting again, with every query in between slow - and the admin found it in the container log. Admin Panel / Problems / Database problems now gets a row for each thing WeKan can measure from its side of the socket, from a probe that runs 30 s after startup and every five minutes (server/lib/databaseHealth.js, the pure decisions in models/lib/databaseHealth.js): - db.restart: serverStatus.uptime went down while WeKan kept running - the database process restarted; points at the fatal-assertion/fsync cause and the storage page. - db.network-filesystem: getCmdLineOpts' dbPath, when it is a directory this process can see (snap, embedded database, shared volume), is on a CIFS/SMB, NFS or FUSE filesystem (fs.statfs f_type). - db.slow-storage: serverStatus.opLatencies reads averaged over 100 ms across the interval, on a real number of reads - the storage latency behind the "Slow query" lines. - db.disk-space: dbStats fsUsedSize/fsTotalSize below 5% or 512 MiB free - the real "no space left on device", before it happens. - db.index-created: the one remediation WeKan can do itself. ensureIndex (server/lib/mongoStartup.js) now reports an index it created on a collection that already held documents - until then every query on it was a collection scan - as found and fixed; a new, empty collection getting its indexes at first start stays silent. All rows go through the existing `database` stream (recordDatabaseHealth in server/lib/databaseProblems.js: same shape, same per-minute quieting per type), so they land on the same page as what the database itself says. FerretDB answers only some of these commands; each step is independent and what it cannot answer is skipped. Nothing here can throw into startup or sits on a request path. tests/databaseHealth.test.cjs drives every decision (including the reported case: 519 MB used, 10 GiB free is NOT a space problem) and pins the wiring. Thanks to xet7 !
189 lines
7.6 KiB
JavaScript
189 lines
7.6 KiB
JavaScript
import { MongoInternals } from 'meteor/mongo';
|
|
|
|
// ============================================================================
|
|
// WeKan MongoDB startup helpers
|
|
//
|
|
// Two problems this module solves, both seen when upgrading WeKan/MongoDB
|
|
// (e.g. the "Topology is closed" / "Server selection timed out" crash on
|
|
// startup, https://github.com/wekan/wekan/issues ):
|
|
//
|
|
// 1. WeKan must WAIT until MongoDB is actually reachable and has an elected
|
|
// replica-set primary before it starts creating indexes. Otherwise the
|
|
// first createIndex() throws and Node exits.
|
|
//
|
|
// 2. Index creation must be IDEMPOTENT: check the existing indexes and only
|
|
// add the ones that are missing, so an upgrade never crashes on an index
|
|
// that already exists (or has different options).
|
|
//
|
|
// All log messages here are intentionally English only (no i18n) so they are
|
|
// readable in `docker logs`, `snap logs wekan` and the Node.js terminal.
|
|
// ============================================================================
|
|
|
|
// How long to wait for MongoDB before printing the "you may need to upgrade
|
|
// the database with mongodump/mongorestore" guidance. We keep retrying after
|
|
// this; we just stop staying silent. Override with WEKAN_DB_WAIT_TIMEOUT
|
|
// (seconds).
|
|
const WAIT_HINT_AFTER_SECONDS = parseInt(
|
|
process.env.WEKAN_DB_WAIT_TIMEOUT || '120',
|
|
10,
|
|
);
|
|
const RETRY_INTERVAL_MS = 5000;
|
|
|
|
// Printed once, in English only, when MongoDB stays unreachable long enough
|
|
// that a database/MongoDB-version upgrade is the likely cause.
|
|
const UPGRADE_HINT = `
|
|
========================================================================
|
|
WeKan: still cannot connect to MongoDB.
|
|
|
|
If you just upgraded WeKan or MongoDB, the existing database may have been
|
|
created by an OLDER MongoDB version that the new MongoDB cannot open, so
|
|
MongoDB never finishes starting and WeKan keeps waiting here.
|
|
|
|
How to upgrade the database (dump with the old version, restore with the new):
|
|
|
|
1. Start the OLD, previously-working MongoDB version.
|
|
2. Back up: mongodump --archive=/dump/wekan.archive --gzip
|
|
3. Stop the old MongoDB. Start the NEW MongoDB version on an EMPTY data dir.
|
|
4. Restore: mongorestore --archive=/dump/wekan.archive --gzip --drop
|
|
5. Start WeKan again.
|
|
|
|
Attachments and avatars are stored ON DISK under WRITABLE_PATH, NOT in
|
|
MongoDB, so when moving to a new server ALSO copy that whole directory
|
|
(it contains "files", "attachments" and "avatars"):
|
|
- Snap: /var/snap/wekan/common/files
|
|
- Docker: the "wekan-files" volume mounted at /data
|
|
- Source: the WRITABLE_PATH you configured in start-wekan.sh
|
|
|
|
Also check the MongoDB logs (docker logs wekan-db / snap logs wekan.mongodb)
|
|
for the real reason, for example:
|
|
- incompatible featureCompatibilityVersion (step major versions one by one),
|
|
- "wrong mongod version" on the data files,
|
|
- an unclean shutdown that needs mongod --repair.
|
|
WeKan will keep retrying every ${RETRY_INTERVAL_MS / 1000}s in case MongoDB is
|
|
just slow to replay its journal.
|
|
========================================================================
|
|
`;
|
|
|
|
export function getRawDb() {
|
|
// Available once the `mongo` package has loaded, which is before any
|
|
// application code runs.
|
|
return MongoInternals.defaultRemoteCollectionDriver().mongo.db;
|
|
}
|
|
|
|
// Resolve a node-mongodb rawCollection from a Meteor Mongo.Collection, a
|
|
// Meteor-Files FilesCollection (its Mongo.Collection is at `.collection`), or
|
|
// anything that already exposes rawCollection().
|
|
function getRawCollection(collection) {
|
|
if (!collection) return null;
|
|
if (typeof collection.rawCollection === 'function') {
|
|
return collection.rawCollection();
|
|
}
|
|
if (collection._collection && typeof collection._collection.rawCollection === 'function') {
|
|
return collection._collection.rawCollection();
|
|
}
|
|
if (collection.collection && typeof collection.collection.rawCollection === 'function') {
|
|
return collection.collection.rawCollection();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// One connectivity probe: must answer AND be a writable primary, because we
|
|
// are about to create indexes (a write). Throws on any failure.
|
|
async function probeMongo() {
|
|
const hello = await getRawDb().command({ hello: 1 });
|
|
if (!hello.isWritablePrimary) {
|
|
throw new Error('MongoDB reachable but replica-set primary not elected yet');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Block until MongoDB is reachable and a writable primary is available.
|
|
* Never rejects: it keeps retrying, and after WAIT_HINT_AFTER_SECONDS it prints
|
|
* the (English) mongodump/mongorestore upgrade guidance once.
|
|
*/
|
|
export async function waitForMongoReady() {
|
|
let waited = 0;
|
|
let hintShown = false;
|
|
// Fast path: already connected.
|
|
try {
|
|
await probeMongo();
|
|
return;
|
|
} catch (e) {
|
|
console.log(`WeKan: waiting for MongoDB to become ready (${e.message})...`);
|
|
}
|
|
// eslint-disable-next-line no-constant-condition
|
|
while (true) {
|
|
await new Promise(resolve => setTimeout(resolve, RETRY_INTERVAL_MS));
|
|
waited += RETRY_INTERVAL_MS / 1000;
|
|
try {
|
|
await probeMongo();
|
|
console.log('WeKan: MongoDB is ready.');
|
|
return;
|
|
} catch (e) {
|
|
if (!hintShown && waited >= WAIT_HINT_AFTER_SECONDS) {
|
|
console.error(UPGRADE_HINT);
|
|
hintShown = true;
|
|
} else {
|
|
console.log(`WeKan: MongoDB not ready yet (${e.message}), retrying in ${RETRY_INTERVAL_MS / 1000}s...`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Idempotent index creation: look at the indexes that already exist on the
|
|
* collection and only create the index when no index with the same key pattern
|
|
* is present. Never throws — a single index problem must not crash startup.
|
|
*
|
|
* @param {Mongo.Collection|{rawCollection:Function}} collection
|
|
* @param {Object} keys index key spec, e.g. { boardId: 1 }
|
|
* @param {Object} [options] passed straight through to createIndex (unique, etc.)
|
|
* @returns {Promise<boolean>} true if an index was created, false if skipped
|
|
*/
|
|
export async function ensureIndex(collection, keys, options = {}) {
|
|
const raw = getRawCollection(collection);
|
|
if (!raw) {
|
|
console.error('WeKan ensureIndex: could not resolve rawCollection, skipping', keys);
|
|
return false;
|
|
}
|
|
const wanted = JSON.stringify(keys);
|
|
try {
|
|
let existing = [];
|
|
try {
|
|
existing = await raw.indexes();
|
|
} catch (e) {
|
|
// Collection may not exist yet — that is fine, createIndex will make it.
|
|
existing = [];
|
|
}
|
|
const alreadyThere = existing.some(idx => JSON.stringify(idx.key) === wanted);
|
|
if (alreadyThere) {
|
|
return false;
|
|
}
|
|
await raw.createIndex(keys, options);
|
|
// An index created on a collection that already holds documents is a
|
|
// remediation, not setup: every query on it was a collection scan until
|
|
// now (the "Slow query" lines a reported MongoDB log was full of -
|
|
// .tools/crash, card_comment_reactions). Tell Admin Panel / Problems it
|
|
// was found and fixed. A brand-new, empty collection getting its indexes
|
|
// is ordinary startup and stays silent.
|
|
try {
|
|
const documents = await raw.estimatedDocumentCount();
|
|
if (documents > 0) {
|
|
const { indexCreatedRemediation } = require('/models/lib/databaseHealth');
|
|
const { recordDatabaseHealth } = require('/server/lib/databaseProblems');
|
|
recordDatabaseHealth(indexCreatedRemediation(raw.collectionName, keys, documents));
|
|
}
|
|
} catch (e) {
|
|
// reporting must never undo the fix
|
|
}
|
|
return true;
|
|
} catch (e) {
|
|
// e.g. transient connection error or an option conflict on an existing
|
|
// index. Log in English and continue; do not crash the whole server.
|
|
console.error(
|
|
`WeKan ensureIndex: failed to ensure index ${wanted} on "${raw.collectionName}": ${e.message}`,
|
|
);
|
|
return false;
|
|
}
|
|
}
|