mylogger/mylogger.js

633 lines
16 KiB
JavaScript
Raw Normal View History

2023-04-08 13:08:37 +00:00
require('require-yaml');
require('colors');
const ZongJi = require('./zongji');
const mysql = require('mysql2/promise');
const {loadConfig} = require('./lib/util');
const ModelLoader = require('./lib/model-loader');
2023-06-06 08:55:56 +00:00
const ShowDb = require('./lib/show-db');
2023-04-08 13:08:37 +00:00
module.exports = class MyLogger {
constructor() {
this.running = false;
this.isOk = null;
2023-04-10 09:03:26 +00:00
this.binlogName = null;
this.binlogPosition = null;
this.isFlushed = true;
this.queue = [];
this.modelLoader = new ModelLoader();
2023-06-06 08:55:56 +00:00
this.showDb = new ShowDb();
}
async start() {
const conf = this.conf = loadConfig(__dirname, 'config');
2023-06-06 08:55:56 +00:00
this.modelLoader.init(this);
this.showDb.init(this);
2023-04-08 13:08:37 +00:00
const includeSchema = {};
for (const [schemaName, tableMap] of this.schemaMap.map)
2023-04-08 13:08:37 +00:00
includeSchema[schemaName] = Array.from(tableMap.keys());
this.zongjiOpts = {
2023-04-08 13:08:37 +00:00
includeEvents: [
'rotate',
'tablemap',
'writerows',
'updaterows',
'deleterows'
],
includeSchema,
serverId: conf.serverId
2023-04-08 13:08:37 +00:00
};
if (conf.testMode)
console.log('Test mode enabled, just logging queries to console.');
console.log('Starting process.');
await this.init();
console.log('Process started.');
}
async stop() {
console.log('Stopping process.');
await this.end();
console.log('Process stopped.');
}
async init() {
const {conf} = this;
2023-04-08 13:08:37 +00:00
this.debug('MyLogger', 'Initializing.');
// DB connection
2023-04-10 09:03:26 +00:00
const db = this.db = await mysql.createConnection(conf.dstDb);
2023-04-08 13:08:37 +00:00
2023-06-06 08:55:56 +00:00
await this.modelLoader.loadSchema();
await this.showDb.loadSchema();
2023-04-08 13:08:37 +00:00
for (const logInfo of this.logMap.values()) {
const table = logInfo.table;
const sqlTable = `${db.escapeId(table.schema)}.${db.escapeId(table.name)}`;
2023-04-08 13:08:37 +00:00
logInfo.addStmt = await db.prepare(
`INSERT INTO ${sqlTable}
SET originFk = ?,
userFk = ?,
action = ?,
creationDate = ?,
changedModel = ?,
oldInstance = ?,
newInstance = ?,
changedModelId = ?,
2024-07-30 11:13:30 +00:00
changedModelValue = ?,
summaryId = ?`
2023-04-08 13:08:37 +00:00
);
logInfo.fetchStmt = await db.prepare(
`SELECT id FROM ${sqlTable}
WHERE changedModel = ?
AND changedModelId = ?
AND action = 'delete'
AND (originFk IS NULL OR originFk = ?)
LIMIT 1`
2023-04-08 13:08:37 +00:00
);
logInfo.updateStmt = await db.prepare(
`UPDATE ${sqlTable}
SET originFk = ?,
creationDate = ?,
oldInstance = ?,
2024-07-30 11:13:30 +00:00
changedModelValue = ?,
summaryId = ?
2023-04-08 13:08:37 +00:00
WHERE id = ?`
);
}
// Zongji
this.onBinlogListener = evt => this.onBinlog(evt);
const [res] = await db.query(
'SELECT `logName`, `position` FROM `util`.`binlogQueue` WHERE code = ?',
[conf.code]
);
if (res.length) {
const [row] = res;
2023-04-10 09:03:26 +00:00
this.binlogName = row.logName;
this.binlogPosition = row.position;
}
2023-04-08 13:08:37 +00:00
await this.zongjiStart();
2023-04-08 13:08:37 +00:00
this.flushInterval = setInterval(
() => this.flushQueue(), conf.flushInterval * 1000);
this.pingInterval = setInterval(
() => this.connectionPing(), conf.pingInterval * 1000);
// Summary
this.running = true;
this.isOk = true;
2023-04-08 13:08:37 +00:00
this.debug('MyLogger', 'Initialized.');
}
async end(silent) {
if (!this.running) return;
this.running = false;
2023-04-08 13:08:37 +00:00
this.debug('MyLogger', 'Ending.');
clearInterval(this.flushInterval);
clearInterval(this.pingInterval);
2023-04-10 09:03:26 +00:00
clearInterval(this.flushTimeout);
function logError(err) {
if (!silent) console.error(err);
}
try {
await this.flushQueue();
} catch (err) {
logError(err);
}
// Zongji
await this.zongjiStop();
this.zongji = null;
// DB connection
// FIXME: mysql2/promise bug, db.end() ends process
this.db.on('error', () => {});
try {
this.db.end();
} catch (err) {
logError(err);
}
// Summary
this.debug('MyLogger', 'Ended.');
}
async zongjiStart() {
await this.zongjiStop();
const zongji = new ZongJi(this.conf.srcDb);
const zongjiOpts = this.zongjiOpts;
if (this.binlogName) {
this.debug('Zongji',
`Starting: ${this.binlogName}, position: ${this.binlogPosition}`
);
Object.assign(zongjiOpts, {
filename: this.binlogName,
position: this.binlogPosition
});
} else {
this.debug('Zongji', 'Starting at end.');
zongjiOpts.startAtEnd = true;
}
zongji.on('binlog', this.onBinlogListener);
await new Promise((resolve, reject) => {
const onReady = () => {
zongji.off('error', onError);
resolve();
};
const onError = err => {
zongji.off('ready', onReady);
zongji.off('binlog', this.onBinlogListener);
reject(err);
}
zongji.once('ready', onReady);
zongji.once('error', onError);
zongji.start(zongjiOpts);
});
this.zongji = zongji;
this.debug('Zongji', 'Started.');
}
async zongjiStop() {
if (!this.zongji) return;
this.debug('Zongji',
`Stopping: ${this.binlogName}, position: ${this.binlogPosition}`
);
const zongji = this.zongji;
this.zongji = null;
2023-04-08 13:08:37 +00:00
zongji.off('binlog', this.onBinlogListener);
// FIXME: Cannot call Zongji.stop(), it doesn't wait to end connection
zongji.connection.destroy(() => {
console.log('zongji.connection.destroy');
});
await new Promise(resolve => {
zongji.ctrlConnection.query('KILL ?', [zongji.connection.threadId],
err => {
// if (err && err.code !== 'ER_NO_SUCH_THREAD');
// console.error(err);
2023-04-08 13:08:37 +00:00
resolve();
});
});
zongji.ctrlConnection.destroy(() => {
console.log('zongji.ctrlConnection.destroy');
});
zongji.emit('stopped');
this.debug('Zongji', 'Stopped.');
}
async tryRestart() {
try {
await this.init();
console.log('Process restarted.');
} catch(err) {
setTimeout(() => this.tryRestart(), this.conf.restartTimeout * 1000);
2023-04-08 13:08:37 +00:00
}
}
async handleError(err) {
if (!this.isOk) return;
this.isOk = false;
2023-04-08 13:08:37 +00:00
console.log(`Error: ${err.code}: ${err.message}`);
2023-04-08 13:08:37 +00:00
try {
await this.end(true);
} catch(e) {}
// FIXME: Error of mysql2/promise
if (err.message === `Can't add new command when connection is in closed state`)
2023-04-08 13:08:37 +00:00
await this.tryRestart();
else
switch (err.code) {
case 'PROTOCOL_CONNECTION_LOST':
case 'ECONNRESET':
console.log('Trying to restart process.');
await this.tryRestart();
break;
default:
process.exit();
}
2023-04-11 08:15:06 +00:00
}
2023-04-10 09:03:26 +00:00
async onBinlog(evt) {
2023-04-08 13:08:37 +00:00
//evt.dump();
2023-04-10 09:03:26 +00:00
try {
let shouldFlush;
const eventName = evt.getEventName();
if (eventName == 'rotate') {
if (evt.binlogName !== this.binlogName) {
shouldFlush = true;
this.binlogName = evt.binlogName;
this.binlogPosition = evt.position;
console.log(
`[${eventName}] filename: ${this.binlogName}`,
`position: ${this.binlogPosition}`
);
}
} else {
shouldFlush = true;
this.binlogPosition = evt.nextPosition;
if (catchEvents.has(eventName))
this.onRowEvent(evt, eventName);
}
2023-04-08 13:08:37 +00:00
2023-04-10 09:03:26 +00:00
if (shouldFlush) this.isFlushed = false;
if (this.queue.length > this.conf.maxQueueEvents) {
this.debug('MyLogger', 'Queue full, stopping Zongji.');
await this.zongjiStop();
}
2023-04-10 09:03:26 +00:00
} catch(err) {
this.handleError(err);
}
2023-04-08 13:08:37 +00:00
}
2023-04-10 09:03:26 +00:00
onRowEvent(evt, eventName) {
2023-04-08 13:08:37 +00:00
const table = evt.tableMap[evt.tableId];
const tableName = table.tableName;
const tableInfo = this.schemaMap.get(table.parentSchema, tableName);
2023-04-08 13:08:37 +00:00
if (!tableInfo) return;
const action = actions[eventName];
const {rowExcludeField} = tableInfo;
const changes = [];
function isExcluded(row) {
return rowExcludeField && row[rowExcludeField];
}
2023-04-08 13:08:37 +00:00
function cast(value, type) {
if (value == null || !type)
return value;
const fn = castFn[type];
return fn ? fn(value) : value;
2023-04-08 13:08:37 +00:00
}
function equals(a, b) {
if (a === b)
return true;
const type = typeof a;
if (a == null || b == null || type !== typeof b)
return false;
if (type === 'object' && a.constructor === b.constructor) {
if (a instanceof Date) {
// FIXME: zongji creates invalid dates for NULL DATE
// Error is somewhere here: zongji/lib/rows_event.js:129
let aTime = a.getTime();
if (isNaN(aTime)) aTime = null;
let bTime = b.getTime();
if (isNaN(bTime)) bTime = null;
return aTime === bTime;
}
}
return false;
}
const {castTypes} = tableInfo;
2023-04-08 13:08:37 +00:00
if (action == 'update') {
const cols = tableInfo.columns;
2023-04-08 13:08:37 +00:00
for (const row of evt.rows) {
const after = row.after;
if (isExcluded(after)) continue;
const before = row.before;
2023-04-08 13:08:37 +00:00
const oldI = {};
const newI = {};
let nColsChanged = 0;
2023-04-08 13:08:37 +00:00
for (const col in before) {
if (!cols.has(col)) continue;
const type = castTypes.get(col);
const oldValue = cast(before[col], type);
const newValue = cast(after[col], type);
if (!equals(oldValue, newValue)) {
oldI[col] = oldValue;
newI[col] = newValue;
2023-04-08 13:08:37 +00:00
nColsChanged++;
}
}
2023-04-08 13:08:37 +00:00
if (nColsChanged)
changes.push({row: after, oldI, newI});
}
} else {
const cols = tableInfo.instanceColumns;
2023-04-08 13:08:37 +00:00
for (const row of evt.rows) {
if (isExcluded(row)) continue;
2023-04-08 13:08:37 +00:00
const instance = {};
for (const col of cols) {
if (row[col] == null) continue;
const type = castTypes.get(col);
instance[col] = cast(row[col], type);
2023-04-08 13:08:37 +00:00
}
2023-04-08 13:08:37 +00:00
changes.push({row, instance});
}
}
if (!changes.length) return;
2023-04-10 09:03:26 +00:00
this.queue.push({
tableInfo,
action,
evt,
changes,
binlogName: this.binlogName
});
2023-04-10 09:03:26 +00:00
if (!this.flushTimeout)
this.flushTimeout = setTimeout(
() => this.flushQueue(),
this.conf.queueFlushDelay
);
if (this.conf.debug)
console.debug('Evt:'.blue,
`[${action}]`[actionColor[action]],
`${tableName}: ${changes.length} changes, queue: ${this.queue.length} elements`
);
2023-04-10 09:03:26 +00:00
}
async flushQueue() {
if (this.isFlushed || this.isFlushing || !this.isOk) return;
2023-04-10 09:03:26 +00:00
this.isFlushing = true;
2023-04-30 11:09:42 +00:00
const {conf, db, queue} = this;
2023-04-11 08:15:06 +00:00
let op;
2023-04-10 09:03:26 +00:00
try {
2023-04-30 11:09:42 +00:00
if (queue.length) {
2023-04-10 09:03:26 +00:00
do {
2023-04-30 11:09:42 +00:00
const ops = [];
let txStarted;
2023-06-06 08:55:56 +00:00
2023-04-10 09:03:26 +00:00
try {
for (let i = 0; i < conf.maxBulkLog && queue.length; i++)
ops.push(queue.shift());
2023-06-06 08:55:56 +00:00
await this.showDb.getValues(db, ops);
2023-04-10 09:03:26 +00:00
await db.query('START TRANSACTION');
2023-04-30 11:09:42 +00:00
txStarted = true;
for (op of ops)
2023-04-10 09:03:26 +00:00
await this.applyOp(op);
2023-04-30 11:09:42 +00:00
this.debug('Queue', `applied: ${ops.length}, remaining: ${queue.length}`);
await this.savePosition(op.binlogName, op.evt.nextPosition);
2023-04-10 09:03:26 +00:00
await db.query('COMMIT');
} catch(err) {
2023-04-30 11:09:42 +00:00
queue.unshift(...ops);
if (txStarted)
try {
await db.query('ROLLBACK');
} catch (err) {}
2023-04-10 09:03:26 +00:00
throw err;
}
2023-04-30 11:09:42 +00:00
} while (queue.length);
if (!this.zongji) {
this.debug('MyLogger', 'Queue flushed, restarting Zongji.');
await this.zongjiStart();
}
2023-04-10 09:03:26 +00:00
} else {
await this.savePosition(this.binlogName, this.binlogPosition);
}
} catch(err) {
this.handleError(err);
} finally {
this.flushTimeout = null;
this.isFlushing = false;
}
}
async applyOp(op) {
const {conf} = this;
2023-04-10 09:03:26 +00:00
const {
tableInfo,
action,
evt,
2023-04-11 08:15:06 +00:00
changes
2023-04-10 09:03:26 +00:00
} = op;
const {
logInfo,
isMain,
relation,
modelName,
logFields
} = tableInfo;
2023-04-10 09:03:26 +00:00
2023-04-08 13:08:37 +00:00
const isDelete = action == 'delete';
2023-05-30 18:27:38 +00:00
const isUpdate = action == 'update';
2023-06-06 08:55:56 +00:00
const created = new Date(evt.timestamp);
2024-08-08 12:07:20 +00:00
const showId = tableInfo.conf.showId;
2023-04-08 13:08:37 +00:00
for (const change of changes) {
let newI, oldI;
const row = change.row;
switch(action) {
case 'update':
newI = change.newI;
oldI = change.oldI;
if (logFields) {
for (const field of logFields)
if (newI[field] === undefined)
newI[field] = row[field];
}
2023-04-08 13:08:37 +00:00
break;
case 'insert':
newI = change.instance;
break;
case 'delete':
oldI = change.instance;
break;
}
2024-08-08 12:07:20 +00:00
const summaryId = showId ? row[showId] : null;
2023-04-08 13:08:37 +00:00
const modelId = row[tableInfo.idName];
2023-06-06 09:37:27 +00:00
const modelValue = change.modelValue ?? null;
2023-04-08 13:08:37 +00:00
const oldInstance = oldI ? JSON.stringify(oldI) : null;
const originFk = !isMain ? row[relation] : modelId;
const originChanged = isUpdate && !isMain
&& newI[relation] !== undefined;
2023-04-08 13:08:37 +00:00
let deleteRow;
if (conf.debug)
2023-04-30 11:09:42 +00:00
console.debug('Log:'.blue,
`[${action}]`[actionColor[action]],
`${logInfo.name}: ${originFk}, ${modelName}: ${modelId}`
);
try {
if (isDelete) {
[[deleteRow]] = await logInfo.fetchStmt.execute([
modelName, modelId, originFk
]);
if (!conf.testMode && deleteRow)
await logInfo.updateStmt.execute([
originFk,
created,
oldInstance,
modelValue,
2024-07-30 11:13:30 +00:00
summaryId,
deleteRow.id
]);
}
if (!conf.testMode && (!isDelete || !deleteRow)) {
async function log(originFk) {
if (originFk == null) return;
await logInfo.addStmt.execute([
originFk,
2023-06-06 09:37:27 +00:00
row[tableInfo.userField] ?? null,
action,
created,
modelName,
oldInstance,
newI ? JSON.stringify(newI) : null,
modelId,
2024-07-30 11:13:30 +00:00
modelValue,
summaryId
]);
}
if (originChanged)
await log(oldI[relation]);
await log(originFk);
}
} catch (err) {
if (err.code == 'ER_NO_REFERENCED_ROW_2') {
this.debug('Log', `Ignored because of constraint failed.`);
} else
throw err;
2023-04-08 13:08:37 +00:00
}
}
}
2023-04-11 08:15:06 +00:00
async savePosition(binlogName, binlogPosition) {
this.debug('Flush', `filename: ${binlogName}, position: ${binlogPosition}`);
const replaceQuery =
'REPLACE INTO `util`.`binlogQueue` SET `code` = ?, `logName` = ?, `position` = ?';
if (!this.conf.testMode)
await this.db.query(replaceQuery, [this.conf.code, binlogName, binlogPosition]);
this.isFlushed = this.binlogName == binlogName
&& this.binlogPosition == binlogPosition;
}
2023-04-08 13:08:37 +00:00
async connectionPing() {
if (!this.isOk) return;
try {
this.debug('Ping', 'Sending ping to database.');
if (this.zongji) {
// FIXME: Should Zongji.connection be pinged?
await new Promise((resolve, reject) => {
this.zongji.ctrlConnection.ping(err => {
if (err) return reject(err);
resolve();
});
})
}
await this.db.ping();
} catch(err) {
this.handleError(err);
}
2023-04-08 13:08:37 +00:00
}
debug(namespace, message) {
if (this.conf.debug)
console.debug(`${namespace}:`.blue, message.yellow);
}
}
2023-04-11 08:15:06 +00:00
const catchEvents = new Set([
'writerows',
'updaterows',
'deleterows'
]);
const actions = {
writerows: 'insert',
updaterows: 'update',
deleterows: 'delete'
};
2023-04-30 11:09:42 +00:00
const actionColor = {
insert: 'green',
update: 'yellow',
delete: 'red'
};
const castFn = {
boolean: function(value) {
return !!value;
}
};