mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
A reported test environment (mongo:8.2.2 in Kubernetes) filled its data
volume: the checkpoint's fdatasync returned ENOSPC, WiredTiger panicked and
mongod aborted - and then it kept failing on every start, with the disk long
since freed. MongoDB 8.2 keeps a throwaway WiredTiger instance under
<dbPath>/_tmp/spilldb for queries that spill to disk and empties it itself on
each start; after the abort that emptying failed ("Failed to clear dbpath of
the internal WiredTiger instance: Directory not empty"), mongod opened the
half-emptied directory, found no version file, reported "Failed to open the
spill WiredTiger instance ... database corruption detected" and fasserted.
Nothing retries, so the pod restarts forever. The data itself is intact;
deleting that one directory is the whole fix.
- snap-src/bin/mongodb-control deletes _tmp/spilldb before every mongod
start (no mongod is running at that point, and mongod recreates it), and
its start-failure handler recognises the log line and names the directory.
The snap ships MongoDB 7 today, so this is a no-op until it does not.
- Admin Panel / Problems: the db.restart row now says what a "No space left
on device" abort means for a full disk as well as for a network
filesystem, and both it and db.disk-space name the exact spill directory
to delete when MongoDB 8.2 will not start afterwards. The probe looks the
data directory up once and passes it to both rows.
- docs/Databases/MongoDB/Storage-Requirements.md gets the log signature of
both stages, why the second one never ends, the one command, what must
not be touched, and that Docker/Kubernetes users run it themselves since
the database container is MongoDB's own image.
- tests/databaseHealth.test.cjs pins the spill path, both rows' advice, the
probe wiring, the snap's pre-start deletion (before the first start, after
the running-instance check, and the only recursive delete apart from the
existing FerretDB reset) and the documentation.
Thanks to xet7 !
119 lines
3.9 KiB
JavaScript
119 lines
3.9 KiB
JavaScript
// Database health probe, the Meteor half of models/lib/databaseHealth.js.
|
|
//
|
|
// Runs once at startup and then every five minutes, asks MongoDB what it can
|
|
// answer from WeKan's side of the socket - serverStatus, dbStats,
|
|
// getCmdLineOpts - and, when the data directory is a path this process can
|
|
// see, asks the kernel what filesystem it is on. Each finding is one row in
|
|
// Admin Panel / Problems / Database problems (the `database` stream), with
|
|
// what it means and what to do, folded per type so a problem that persists
|
|
// is one row with a count, not a row per probe.
|
|
//
|
|
// FerretDB answers only some of these commands (no opLatencies, no
|
|
// fsTotalSize); every step is independent and wrapped, so what it cannot
|
|
// answer is skipped and the rest still runs. Nothing here can throw into
|
|
// startup, and nothing here is on a request path.
|
|
import { Meteor } from 'meteor/meteor';
|
|
import fs from 'fs';
|
|
import { getRawDb } from '/server/lib/mongoStartup';
|
|
import { recordDatabaseHealth } from '/server/lib/databaseProblems';
|
|
const health = require('/models/lib/databaseHealth');
|
|
|
|
const PROBE_MS = 5 * 60 * 1000;
|
|
|
|
const state = {
|
|
uptime: null,
|
|
restarts: 0,
|
|
opLatencies: null,
|
|
networkFsReported: false,
|
|
};
|
|
|
|
async function serverStatus(db) {
|
|
try {
|
|
return await db.admin().command({ serverStatus: 1 });
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function dbStats(db) {
|
|
try {
|
|
return await db.command({ dbStats: 1 });
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function dataDirectory(db) {
|
|
try {
|
|
const opts = await db.admin().command({ getCmdLineOpts: 1 });
|
|
const parsed = opts && opts.parsed;
|
|
return (parsed && parsed.storage && parsed.storage.dbPath) || '';
|
|
} catch (e) {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
// The data directory is only inspectable when this process shares a
|
|
// filesystem with mongod - the snap, the bundle with its embedded database,
|
|
// or a container that mounts the same volume. A separate database container
|
|
// or host has a dbPath that does not exist here, and that is simply skipped.
|
|
function checkDataDirectory(dbPath) {
|
|
if (state.networkFsReported || !dbPath || typeof fs.statfs !== 'function') return;
|
|
if (!fs.existsSync(dbPath)) return;
|
|
fs.statfs(dbPath, (error, stats) => {
|
|
if (error || !stats) return;
|
|
const name = health.networkFilesystem(stats.type);
|
|
if (!name) return;
|
|
state.networkFsReported = true;
|
|
recordDatabaseHealth(health.networkFilesystemProblem(name, dbPath));
|
|
});
|
|
}
|
|
|
|
export async function probeDatabaseHealth() {
|
|
let db;
|
|
try {
|
|
db = getRawDb();
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
if (!db) return;
|
|
|
|
const dbPath = await dataDirectory(db);
|
|
|
|
const status = await serverStatus(db);
|
|
if (status) {
|
|
const uptime = Number(status.uptime);
|
|
if (health.restartDetected(state.uptime, uptime)) {
|
|
state.restarts += 1;
|
|
recordDatabaseHealth(health.restartProblem(state.restarts, dbPath));
|
|
}
|
|
if (Number.isFinite(uptime)) state.uptime = uptime;
|
|
|
|
if (status.opLatencies) {
|
|
const avg = health.readLatencyMs(state.opLatencies, status.opLatencies);
|
|
const ops = state.opLatencies && state.opLatencies.reads
|
|
? Number(status.opLatencies.reads.ops) - Number(state.opLatencies.reads.ops) : 0;
|
|
if (health.slowStorage(avg, ops)) recordDatabaseHealth(health.slowStorageProblem(avg, ops));
|
|
state.opLatencies = status.opLatencies;
|
|
}
|
|
}
|
|
|
|
const stats = await dbStats(db);
|
|
const space = health.diskSpace(stats);
|
|
if (space && space.critical) recordDatabaseHealth(health.diskSpaceProblem(space, dbPath));
|
|
|
|
checkDataDirectory(dbPath);
|
|
}
|
|
|
|
Meteor.startup(() => {
|
|
const run = () => {
|
|
probeDatabaseHealth().catch(e => {
|
|
if (process.env.DEBUG === 'true') console.warn('database health probe failed:', e && e.message);
|
|
});
|
|
};
|
|
// Not in the way of the rest of startup: the first probe waits until the
|
|
// server has settled, and the indexes ensureIndex may be creating are done.
|
|
Meteor.setTimeout(run, 30 * 1000);
|
|
Meteor.setInterval(run, PROBE_MS);
|
|
});
|