mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
The crash loop is gone - a transient database error no longer ends the boot
(00startupResilience) or the process (00processErrors) - and the reporter's 10.49
log is the same contention arriving in front of a user instead:
Exception while invoking method '/users/updateAsync' MongoServerError:
... [collection.go:191 sqlite.(*collection).UpdateAll]
[db.go:151 fsql.(*DB).InTransaction] database is locked (5) (SQLITE_BUSY)
That error goes to the CLIENT. The user's edit fails, and they reload to find out
what happened - which is exactly the complaint, "they only see new cards after a
reload". And it was not supposed to: models/lib/databaseErrors.js has classified a
locked SQLite as { id: 'deadlock', act: 'retry' } with "Retried automatically."
since it was written, and 00processErrors says "the write is retried by whatever
issued it". Nothing read `act`. No code in WeKan retried anything, anywhere.
SQLITE_BUSY is the one database error a client is meant to handle itself. SQLite
has a single writer; a second writer arriving while the lock is held is told to
come back, and nothing was applied - FerretDB takes the lock before the
transaction (_txlock=immediate), so a BUSY answer means the write did not happen.
Retrying the same write is the documented behaviour and stays idempotent: Meteor
has already chosen the _id, so a retry that raced a duplicate is refused by the
unique index rather than inserted twice.
server/00retryBusyWrites.js wraps insertAsync / updateAsync / upsertAsync /
removeAsync on Mongo.Collection.prototype: a transient database error is retried
with exponential backoff and full jitter, bounded at 5 attempts and 2s in total
(WEKAN_DB_RETRY_*). Bounded on purpose - retrying for longer holds a method
invocation and its slot in the connection pool while the contention it is waiting
for gets worse. When it still fails the ORIGINAL error is thrown unchanged, so a
database that is genuinely stuck looks exactly as it did before and nothing is
hidden; anything that is not transient is rethrown on the first attempt, so a full
disk is not waited on four times before being reported.
It is loaded from server/imports.js, which runs after the Meteor packages, so this
wrapper sits OUTSIDE the ones collection2 and collection-hooks put on the same
prototype - and that is how it sees the error at all, because by then the busy
error is collection2's ValidationError with the database's message inside it,
which is the form in the reporter's SyncedCron traces.
Writes only. Every error in the reports is a write, which is what contends for the
one writer; SQLite in WAL mode does not block a reader against a writer, and
wrapping every read would put this on the hottest path in the server for no
reported failure.
Retries are counted and summarised at most once a minute, so a contended database
is visible without the log becoming the new problem, and a write finally given up
on is recorded for Admin Panel / Problems - except an eventlog write, which is how
the recorder writes, and reporting its failure would call the recorder from inside
itself.
The reporter asked outright whether PostgreSQL is an option for a snap
installation. It is, and it has been: the deadlock rule's advice now says how -
snap set wekan wekan-ferretdb-handler=postgresql wekan-ferretdb-url=... , or
docker-compose-ferretdb-v1-postgresql.yml in Docker - instead of only saying that
SQLite has one writer.
tests/sqliteBusyRetry.test.cjs loads the real module against a stub collection
prototype and exercises the behaviour: a busy write is retried and succeeds with
its arguments and its collection unchanged, collection2's ValidationError form is
recognised too, a full disk is thrown at once and never retried, an endlessly busy
database gets the original error after a bounded number of attempts AND a bounded
wall-clock time, the backoff grows and is jittered and capped on the shipped
numbers, the summary is rate-limited, reads are left unwrapped, wrapping twice is
a no-op, and the eventlog write is retried without reporting on itself.
Thanks to Nissulya and xet7 !
Fixes #6533,
313 lines
13 KiB
JavaScript
313 lines
13 KiB
JavaScript
// What the database just said, what it means, and what to do about it.
|
|
//
|
|
// WeKan runs on MongoDB or on FerretDB v1 over SQLite, PostgreSQL, MySQL,
|
|
// MariaDB or SAP HANA. Each of those answers a broken query, a full disk or a
|
|
// refused login in its own words, and the words reach WeKan as an opaque error
|
|
// string - so an admin sees "Error 1064 (42000): You have an error in your SQL
|
|
// syntax" and has no way to know it came from MySQL, that it is FerretDB's bug
|
|
// and not their data, or that nothing they can configure will fix it.
|
|
//
|
|
// This module is the translation, and it is deliberately pure: a string in, a
|
|
// classification out, no database and no Meteor. server/lib/databaseProblems.js
|
|
// records what it returns, and Admin Panel / Problems shows it.
|
|
|
|
// The databases WeKan can be running on, as they are named in the UI.
|
|
const DATABASES = ['mongodb', 'sqlite', 'postgresql', 'mysql', 'mariadb', 'hana', 'unknown'];
|
|
|
|
// One rule: how to recognise it, what it is, and what the admin should do.
|
|
//
|
|
// `act` is the thing WeKan can do BY ITSELF when it sees this - and only where
|
|
// doing it is obviously right. Everything else is advice, because a database
|
|
// that is out of disk is not something an application should "handle".
|
|
const RULES = [
|
|
// ── injection and malformed SQL: FerretDB's bug, never the admin's ────────
|
|
{
|
|
id: 'sql-guard-refused',
|
|
match: /statement rejected by the SQL guard|SECURITY: refusing to execute/i,
|
|
databases: ['postgresql', 'mysql', 'mariadb', 'sqlite'],
|
|
severity: 'critical',
|
|
kind: 'injection',
|
|
means: 'FerretDB refused to run a statement that looked like it carried injected SQL.',
|
|
whatToDo: 'Nothing was executed. Report it at https://github.com/wekan/FerretDB/issues ' +
|
|
'with the statement from the FerretDB log - a client should not be able to cause this.',
|
|
act: null,
|
|
},
|
|
{
|
|
id: 'mysql-syntax',
|
|
match: /Error 1064 \(42000\)|You have an error in your SQL syntax/i,
|
|
databases: ['mysql', 'mariadb'],
|
|
severity: 'critical',
|
|
kind: 'syntax',
|
|
means: 'MySQL/MariaDB rejected a statement FerretDB built. This is a FerretDB bug, ' +
|
|
'not something in your data.',
|
|
whatToDo: 'Upgrade FerretDB (wekan/FerretDB releases). If it persists, report it with ' +
|
|
'the statement from the log.',
|
|
act: null,
|
|
},
|
|
{
|
|
id: 'postgres-syntax',
|
|
match: /SQLSTATE 42601|syntax error at or near/i,
|
|
databases: ['postgresql'],
|
|
severity: 'critical',
|
|
kind: 'syntax',
|
|
means: 'PostgreSQL rejected a statement FerretDB built - a FerretDB bug, not your data.',
|
|
whatToDo: 'Upgrade FerretDB. If it persists, report it with the statement from the log.',
|
|
act: null,
|
|
},
|
|
{
|
|
id: 'sqlite-syntax',
|
|
match: /SQL logic error|near ".*": syntax error/i,
|
|
databases: ['sqlite'],
|
|
severity: 'critical',
|
|
kind: 'syntax',
|
|
means: 'SQLite rejected a statement FerretDB built - a FerretDB bug, not your data.',
|
|
whatToDo: 'Upgrade FerretDB. If it persists, report it with the statement from the log.',
|
|
act: null,
|
|
},
|
|
|
|
// ── permissions: the commonest FerretDB-on-SQL misconfiguration ───────────
|
|
{
|
|
id: 'mysql-access-denied-database',
|
|
match: /Access denied for user .* to database/i,
|
|
databases: ['mysql', 'mariadb'],
|
|
severity: 'critical',
|
|
kind: 'permission',
|
|
means: 'FerretDB creates one SQL database per MongoDB database, and the user it ' +
|
|
'connects as may not create them.',
|
|
whatToDo: 'Connect FerretDB as a user that may CREATE DATABASE - the WeKan compose ' +
|
|
'files use root for exactly this reason - or grant that right to the user in ' +
|
|
'--mysql-url.',
|
|
act: null,
|
|
},
|
|
{
|
|
id: 'mysql-native-password',
|
|
match: /this user requires mysql native password authentication/i,
|
|
databases: ['mysql', 'mariadb'],
|
|
severity: 'critical',
|
|
kind: 'auth',
|
|
means: 'The MySQL driver refused the server\'s password handshake.',
|
|
whatToDo: 'Upgrade FerretDB: its driver configuration disabled native passwords, ' +
|
|
'which every default MariaDB root account asks for. Fixed in wekan/FerretDB.',
|
|
act: null,
|
|
},
|
|
{
|
|
id: 'postgres-permission',
|
|
match: /SQLSTATE 42501|permission denied for (schema|database|relation)/i,
|
|
databases: ['postgresql'],
|
|
severity: 'critical',
|
|
kind: 'permission',
|
|
means: 'PostgreSQL refused FerretDB the right to create or use a schema.',
|
|
whatToDo: 'The role in --postgresql-url needs CREATE on the database. The WeKan ' +
|
|
'compose file uses the database owner.',
|
|
act: null,
|
|
},
|
|
{
|
|
id: 'auth-failed',
|
|
match: /authentication failed|password authentication failed|Authentication failed|bad auth/i,
|
|
databases: DATABASES,
|
|
severity: 'critical',
|
|
kind: 'auth',
|
|
means: 'The database rejected the credentials in the connection URL.',
|
|
whatToDo: 'Check MONGO_URL (or FerretDB\'s backend URL) and the password of that user.',
|
|
act: null,
|
|
},
|
|
|
|
// ── the machine underneath ───────────────────────────────────────────────
|
|
{
|
|
id: 'disk-full',
|
|
match: /No space left on device|disk I\/O error|SQLSTATE 53100|database or disk is full|ENOSPC/i,
|
|
databases: DATABASES,
|
|
severity: 'critical',
|
|
kind: 'disk',
|
|
means: 'The database cannot write: the disk holding it is full.',
|
|
whatToDo: 'Free space on the volume holding the database, then restart WeKan. ' +
|
|
'Admin Panel / Problems / Disk usage shows what is using it.',
|
|
act: null,
|
|
},
|
|
{
|
|
id: 'too-many-connections',
|
|
match: /too many connections|SQLSTATE 53300|max_connections|connection limit exceeded/i,
|
|
databases: ['postgresql', 'mysql', 'mariadb', 'hana'],
|
|
severity: 'warning',
|
|
kind: 'connections',
|
|
means: 'The database is refusing new connections because its limit is reached.',
|
|
whatToDo: 'Raise max_connections on the database, or lower FerretDB\'s pool size. ' +
|
|
'WeKan retries, so this is usually a burst rather than a fault.',
|
|
act: 'retry',
|
|
},
|
|
{
|
|
id: 'deadlock',
|
|
match: /deadlock detected|SQLSTATE 40P01|Deadlock found when trying to get lock|database is locked/i,
|
|
databases: ['postgresql', 'mysql', 'mariadb', 'sqlite'],
|
|
severity: 'warning',
|
|
kind: 'contention',
|
|
means: 'Two writes wanted the same rows in a different order, or SQLite\'s single ' +
|
|
'writer was busy.',
|
|
// "Retried automatically" is true because server/00retryBusyWrites.js retries
|
|
// every collection write that fails this way; for years nothing read `act` and
|
|
// the write simply failed in front of the user (#6533).
|
|
whatToDo: 'Retried automatically. If it is constant, SQLite\'s one writer is the ' +
|
|
'limit and the backend should be PostgreSQL: on a snap, ' +
|
|
'"snap set wekan wekan-ferretdb-handler=postgresql ' +
|
|
'wekan-ferretdb-url=postgres://user:pass@HOST:5432/ferretdb"; in Docker, the ' +
|
|
'docker-compose-ferretdb-v1-postgresql.yml compose file.',
|
|
act: 'retry',
|
|
},
|
|
{
|
|
id: 'connection-lost',
|
|
match: /connection refused|ECONNREFUSED|server closed the connection|connection reset by peer|Topology is closed|no reachable servers/i,
|
|
databases: DATABASES,
|
|
severity: 'critical',
|
|
kind: 'connection',
|
|
means: 'WeKan cannot reach the database at all.',
|
|
whatToDo: 'Check that the database container is running and that MONGO_URL points at ' +
|
|
'it. WeKan reconnects by itself once it is back.',
|
|
act: 'reconnect',
|
|
},
|
|
{
|
|
id: 'timeout',
|
|
match: /context deadline exceeded|operation exceeded time limit|ETIMEDOUT|statement timeout/i,
|
|
databases: DATABASES,
|
|
severity: 'warning',
|
|
kind: 'timeout',
|
|
means: 'A query took longer than the database or the driver allows.',
|
|
whatToDo: 'Usually an unindexed query on a large collection. Admin Panel / Problems / ' +
|
|
'Speed shows the slow ones.',
|
|
act: 'retry',
|
|
},
|
|
|
|
// ── data and schema ──────────────────────────────────────────────────────
|
|
{
|
|
id: 'duplicate-key',
|
|
match: /E11000|duplicate key|Duplicate entry|UNIQUE constraint failed|SQLSTATE 23505/i,
|
|
databases: DATABASES,
|
|
severity: 'info',
|
|
kind: 'duplicate',
|
|
means: 'A unique index rejected a write, which is the index doing its job.',
|
|
whatToDo: 'Nothing, unless it repeats for the same document - then the data has two ' +
|
|
'rows that must be one.',
|
|
act: null,
|
|
},
|
|
{
|
|
id: 'not-implemented',
|
|
match: /is not implemented yet|is not supported/i,
|
|
databases: ['sqlite', 'postgresql', 'mysql', 'mariadb', 'hana'],
|
|
severity: 'warning',
|
|
kind: 'unsupported',
|
|
means: 'FerretDB does not implement something WeKan asked for.',
|
|
whatToDo: 'Upgrade FerretDB, and report the operation if it stays missing. ' +
|
|
'Admin Panel / Problems lists which operation it was.',
|
|
act: null,
|
|
},
|
|
{
|
|
id: 'unknown-handler',
|
|
match: /unknown handler/i,
|
|
databases: ['hana', 'mysql', 'mariadb', 'postgresql', 'sqlite'],
|
|
severity: 'critical',
|
|
kind: 'configuration',
|
|
means: 'FerretDB was started with a --handler it was not built with.',
|
|
whatToDo: 'The hana handler needs a binary built with the ferretdb_hana build tag. ' +
|
|
'The wekan/FerretDB releases are; a binary built elsewhere may not be.',
|
|
act: null,
|
|
},
|
|
];
|
|
|
|
// A database that refuses a login or cannot be reached usually quotes the
|
|
// connection URL back, and that URL carries the password - "failed to connect to
|
|
// mongodb://wekan:s3cret@mongo:27017". The message is stored and SHOWN in Admin
|
|
// Panel / Problems, so the credentials come out here, before anything keeps it.
|
|
// Only the userinfo part goes: the host, the port and the database name are what
|
|
// makes the message useful.
|
|
function redactCredentials(text) {
|
|
return String(text || '').replace(
|
|
/([a-z][a-z0-9+.-]*:\/\/)([^/\s@]+)@/gi,
|
|
(all, scheme, userinfo) => {
|
|
const user = userinfo.split(':')[0];
|
|
return userinfo.includes(':') ? `${scheme}${user}:***@` : all;
|
|
},
|
|
);
|
|
}
|
|
|
|
// Which database produced this, as far as the text betrays it. `configured` is
|
|
// what WeKan believes it is talking to (from MONGO_URL / the FerretDB handler),
|
|
// and it wins unless the message itself names another - a MySQL error code in a
|
|
// PostgreSQL-configured instance means the configuration is wrong, and saying so
|
|
// is more useful than trusting it.
|
|
function databaseOf(message, configured) {
|
|
const text = String(message || '');
|
|
|
|
if (/Error \d+ \(\d{5}\)|mysqld|MySQL/i.test(text)) {
|
|
return /maria/i.test(text) ? 'mariadb' : 'mysql';
|
|
}
|
|
if (/SQLSTATE|pq: |pgx|PostgreSQL/i.test(text)) return 'postgresql';
|
|
if (/SQL logic error|sqlite/i.test(text)) return 'sqlite';
|
|
if (/hdb|SAP HANA/i.test(text)) return 'hana';
|
|
if (/E11000|Topology is closed|MongoServerError/i.test(text) && configured === 'mongodb') {
|
|
return 'mongodb';
|
|
}
|
|
|
|
return DATABASES.includes(configured) ? configured : 'unknown';
|
|
}
|
|
|
|
// Classify one database error.
|
|
//
|
|
// Always returns a classification - an unrecognised error is `kind: 'unknown'`
|
|
// with the message kept, because an admin looking at Problems needs to see that
|
|
// something happened even when this module has no rule for it.
|
|
function classifyDatabaseError(error, options = {}) {
|
|
const message = redactCredentials(
|
|
String((error && (error.message || error.errmsg || error.reason)) || error || ''),
|
|
).slice(0, 2000);
|
|
const configured = options.configured || 'unknown';
|
|
const database = databaseOf(message, configured);
|
|
|
|
for (const rule of RULES) {
|
|
if (!rule.match.test(message)) continue;
|
|
if (!rule.databases.includes(database) && database !== 'unknown') continue;
|
|
|
|
return {
|
|
id: rule.id,
|
|
database,
|
|
severity: rule.severity,
|
|
kind: rule.kind,
|
|
means: rule.means,
|
|
whatToDo: rule.whatToDo,
|
|
act: rule.act,
|
|
message,
|
|
operation: options.operation || '',
|
|
};
|
|
}
|
|
|
|
return {
|
|
id: 'unclassified',
|
|
database,
|
|
severity: 'warning',
|
|
kind: 'unknown',
|
|
means: 'The database returned an error WeKan has no rule for.',
|
|
// The message follows in the same cell of the table (the Detail column joins
|
|
// the two), so this points at where the message actually is - it used to say
|
|
// "below", where there was nothing.
|
|
whatToDo: 'What the database said follows; if it is one WeKan should recognise, ' +
|
|
'add a rule to models/lib/databaseErrors.js.',
|
|
act: null,
|
|
message,
|
|
operation: options.operation || '',
|
|
};
|
|
}
|
|
|
|
// The database WeKan believes it is talking to, from the environment. FerretDB
|
|
// speaks the MongoDB protocol, so MONGO_URL alone cannot tell them apart - the
|
|
// handler is what decides, and WEKAN_DB names it when the launcher set it.
|
|
function configuredDatabase(env = {}) {
|
|
const db = String(env.WEKAN_DB || '').toLowerCase();
|
|
if (DATABASES.includes(db)) return db;
|
|
if (db === 'ferretdb') return 'sqlite'; // the default FerretDB backend WeKan ships
|
|
if (env.MONGO_URL && /ferretdb/i.test(env.MONGO_URL)) return 'sqlite';
|
|
if (env.MONGO_URL) return 'mongodb';
|
|
return 'unknown';
|
|
}
|
|
|
|
module.exports = {
|
|
classifyDatabaseError, configuredDatabase, databaseOf, redactCredentials, DATABASES, RULES,
|
|
};
|