2022-10-21 14:36:49 +00:00
|
|
|
const ZongJi = require('./zongji');
|
|
|
|
const mysql = require('mysql2/promise');
|
2022-10-23 19:46:07 +00:00
|
|
|
const amqp = require('amqplib');
|
|
|
|
require('colors');
|
2022-10-21 14:36:49 +00:00
|
|
|
|
|
|
|
const allEvents = new Set([
|
|
|
|
'writerows',
|
|
|
|
'updaterows',
|
|
|
|
'deleterows'
|
|
|
|
]);
|
|
|
|
|
2022-10-24 06:01:43 +00:00
|
|
|
module.exports = class MyCDC {
|
2022-10-21 14:37:35 +00:00
|
|
|
constructor(config) {
|
|
|
|
this.config = config;
|
|
|
|
this.running = false;
|
|
|
|
this.filename = null;
|
|
|
|
this.position = null;
|
|
|
|
this.schemaMap = new Map();
|
2022-10-23 19:46:07 +00:00
|
|
|
this.fks = new Set();
|
2022-10-21 14:37:35 +00:00
|
|
|
|
|
|
|
const includeSchema = {};
|
|
|
|
for (const schemaName in this.config.includeSchema) {
|
|
|
|
const schema = this.config.includeSchema[schemaName];
|
|
|
|
const tables = [];
|
|
|
|
const tableMap = new Map();
|
2022-10-21 14:36:49 +00:00
|
|
|
|
2022-10-21 14:37:35 +00:00
|
|
|
for (const tableName in schema) {
|
|
|
|
const table = schema[tableName];
|
|
|
|
tables.push(tableName);
|
|
|
|
|
|
|
|
const tableInfo = {
|
|
|
|
events: allEvents,
|
|
|
|
columns: true,
|
|
|
|
fk: 'id'
|
|
|
|
};
|
|
|
|
tableMap.set(tableName, tableInfo);
|
2022-10-21 14:36:49 +00:00
|
|
|
|
2022-10-21 14:37:35 +00:00
|
|
|
if (typeof table === 'object') {
|
|
|
|
if (Array.isArray(table.events))
|
|
|
|
tableInfo.events = new Set(table.events);
|
|
|
|
if (Array.isArray(table.columns))
|
|
|
|
tableInfo.columns = new Set(table.columns);
|
|
|
|
if (table.fk)
|
|
|
|
tableInfo.fk = table.fk;
|
2022-10-21 14:36:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 14:37:35 +00:00
|
|
|
includeSchema[schemaName] = tables;
|
|
|
|
this.schemaMap.set(schemaName, tableMap);
|
2022-10-21 14:36:49 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 19:46:07 +00:00
|
|
|
this.opts = {
|
2022-10-21 14:37:35 +00:00
|
|
|
includeEvents: this.config.includeEvents,
|
|
|
|
includeSchema
|
|
|
|
};
|
2022-10-23 19:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async start() {
|
|
|
|
if (this.config.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() {
|
|
|
|
this.debug('DbAsync', 'Initializing.');
|
|
|
|
this.onErrorListener = err => this.onError(err);
|
|
|
|
|
|
|
|
// DB connection
|
|
|
|
|
|
|
|
this.db = await mysql.createConnection(this.config.db);
|
|
|
|
this.db.on('error', this.onErrorListener);
|
|
|
|
|
|
|
|
// RabbitMQ
|
|
|
|
|
|
|
|
this.publisher = await amqp.connect(this.config.amqp);
|
|
|
|
this.channel = await this.publisher.createChannel();
|
|
|
|
this.channel.assertQueue(this.config.queue, {
|
|
|
|
durable: true
|
|
|
|
});
|
|
|
|
|
|
|
|
// Zongji
|
|
|
|
|
|
|
|
const zongji = new ZongJi(this.config.db);
|
|
|
|
this.zongji = zongji;
|
|
|
|
|
|
|
|
this.onBinlogListener = evt => this.onBinlog(evt);
|
|
|
|
zongji.on('binlog', this.onBinlogListener);
|
|
|
|
|
2022-10-21 14:37:35 +00:00
|
|
|
const [res] = await this.db.query(
|
|
|
|
'SELECT `logName`, `position` FROM `binlogQueue` WHERE code = ?',
|
|
|
|
[this.config.queue]
|
|
|
|
);
|
|
|
|
if (res.length) {
|
|
|
|
const [row] = res;
|
|
|
|
this.filename = row.logName;
|
|
|
|
this.position = row.position;
|
2022-10-23 19:46:07 +00:00
|
|
|
Object.assign(this.opts, {
|
2022-10-21 14:37:35 +00:00
|
|
|
filename: this.filename,
|
|
|
|
position: this.position
|
|
|
|
});
|
|
|
|
} else
|
2022-10-23 19:46:07 +00:00
|
|
|
this.opts.startAtEnd = true;
|
2022-10-21 14:36:49 +00:00
|
|
|
|
2022-10-23 19:46:07 +00:00
|
|
|
this.debug('Zongji', 'Starting.');
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
const onReady = () => {
|
|
|
|
zongji.off('error', onError);
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
const onError = err => {
|
|
|
|
this.zongji = null;
|
|
|
|
zongji.off('ready', onReady);
|
|
|
|
zongji.off('binlog', this.onBinlogListener);
|
|
|
|
reject(err);
|
|
|
|
}
|
2022-10-21 14:36:49 +00:00
|
|
|
|
2022-10-23 19:46:07 +00:00
|
|
|
zongji.once('ready', onReady);
|
|
|
|
zongji.once('error', onError);
|
|
|
|
zongji.start(this.opts);
|
|
|
|
});
|
|
|
|
this.debug('Zongji', 'Started.');
|
2022-10-21 14:37:35 +00:00
|
|
|
|
2022-10-23 19:46:07 +00:00
|
|
|
this.zongji.on('error', this.onErrorListener);
|
2022-10-21 14:37:35 +00:00
|
|
|
|
2022-10-23 19:46:07 +00:00
|
|
|
this.flushInterval = setInterval(
|
|
|
|
() => this.flushQueue(), this.config.flushInterval);
|
|
|
|
this.pingInterval = setInterval(
|
|
|
|
() => this.connectionPing(), this.config.pingInterval * 1000);
|
|
|
|
|
|
|
|
// Summary
|
|
|
|
|
|
|
|
this.running = true;
|
|
|
|
this.debug('DbAsync', 'Initialized.');
|
2022-10-21 14:37:35 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 19:46:07 +00:00
|
|
|
async end(silent) {
|
|
|
|
const zongji = this.zongji;
|
|
|
|
if (!zongji) return;
|
|
|
|
|
|
|
|
this.debug('DbAsync', 'Ending.');
|
|
|
|
|
|
|
|
// Zongji
|
|
|
|
|
2022-10-21 14:37:35 +00:00
|
|
|
clearInterval(this.flushInterval);
|
|
|
|
clearInterval(this.pingInterval);
|
2022-10-23 19:46:07 +00:00
|
|
|
zongji.off('binlog', this.onBinlogListener);
|
|
|
|
zongji.off('error', this.onErrorListener);
|
|
|
|
this.zongji = null;
|
|
|
|
this.running = false;
|
2022-10-21 14:37:35 +00:00
|
|
|
|
2022-10-23 19:46:07 +00:00
|
|
|
this.debug('Zongji', 'Stopping.');
|
|
|
|
// 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 && !silent)
|
|
|
|
console.error(err);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
zongji.ctrlConnection.destroy(() => {
|
|
|
|
console.log('zongji.ctrlConnection.destroy');
|
|
|
|
});
|
|
|
|
zongji.emit('stopped');
|
|
|
|
this.debug('Zongji', 'Stopped.');
|
2022-10-21 14:37:35 +00:00
|
|
|
|
2022-10-23 19:46:07 +00:00
|
|
|
// RabbitMQ
|
2022-10-21 14:37:35 +00:00
|
|
|
|
2022-10-23 19:46:07 +00:00
|
|
|
await this.publisher.close();
|
2022-10-21 14:37:35 +00:00
|
|
|
|
2022-10-23 19:46:07 +00:00
|
|
|
// DB connection
|
|
|
|
|
|
|
|
this.db.off('error', this.onErrorListener);
|
|
|
|
// FIXME: mysql2/promise bug, db.end() ends process
|
|
|
|
this.db.on('error', () => {});
|
|
|
|
try {
|
|
|
|
await this.db.end();
|
|
|
|
} catch (err) {
|
|
|
|
if (!silent)
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Summary
|
|
|
|
|
|
|
|
this.debug('DbAsync', 'Ended.');
|
2022-10-21 14:37:35 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 19:46:07 +00:00
|
|
|
async tryRestart() {
|
|
|
|
try {
|
|
|
|
await this.init();
|
|
|
|
console.log('Process restarted.');
|
|
|
|
} catch(err) {
|
|
|
|
setTimeout(() => this.tryRestart(), 30);
|
|
|
|
}
|
2022-10-21 14:37:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async onError(err) {
|
|
|
|
console.log(`Error: ${err.code}: ${err.message}`);
|
2022-10-23 19:46:07 +00:00
|
|
|
try {
|
|
|
|
await this.end(true);
|
|
|
|
} catch(e) {}
|
|
|
|
|
2022-10-21 14:37:35 +00:00
|
|
|
switch (err.code) {
|
|
|
|
case 'PROTOCOL_CONNECTION_LOST':
|
|
|
|
case 'ECONNRESET':
|
2022-10-23 19:46:07 +00:00
|
|
|
console.log('Trying to restart process.');
|
|
|
|
await this.tryRestart();
|
2022-10-21 14:37:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
process.exit();
|
2022-10-21 14:36:49 +00:00
|
|
|
}
|
2022-10-21 14:37:35 +00:00
|
|
|
}
|
2022-10-21 14:36:49 +00:00
|
|
|
|
2022-10-21 14:37:35 +00:00
|
|
|
onBinlog(evt) {
|
|
|
|
//evt.dump();
|
|
|
|
const eventName = evt.getEventName();
|
|
|
|
if (eventName === 'tablemap') return;
|
|
|
|
|
|
|
|
if (eventName === 'rotate') {
|
|
|
|
this.filename = evt.binlogName;
|
|
|
|
this.position = evt.position;
|
|
|
|
console.log(`[${eventName}] filename: ${this.filename}`, `position: ${this.position}`);
|
|
|
|
return;
|
2022-10-21 14:36:49 +00:00
|
|
|
}
|
2022-10-21 14:37:35 +00:00
|
|
|
|
|
|
|
const table = evt.tableMap[evt.tableId];
|
|
|
|
const tableMap = this.schemaMap.get(table.parentSchema);
|
|
|
|
if (!tableMap) return;
|
|
|
|
|
|
|
|
const tableInfo = tableMap.get(table.tableName);
|
|
|
|
if (!tableInfo) return;
|
|
|
|
|
|
|
|
if (!tableInfo.events.has(eventName)) return;
|
|
|
|
|
|
|
|
let column;
|
|
|
|
const rows = evt.rows;
|
|
|
|
|
2022-10-23 19:46:07 +00:00
|
|
|
const fks = new Set();
|
|
|
|
|
2022-10-21 14:37:35 +00:00
|
|
|
if (eventName === 'updaterows') {
|
|
|
|
if (tableInfo.columns !== true) {
|
|
|
|
let changes = false;
|
|
|
|
for (const row of rows) {
|
|
|
|
const after = row.after;
|
|
|
|
for (const col in after) {
|
|
|
|
if (tableInfo.columns.has(col) && !equals(after[col], row.before[col])) {
|
|
|
|
fks.add(after[tableInfo.fk]);
|
|
|
|
changes = true;
|
|
|
|
if (!column) column = col;
|
|
|
|
break;
|
2022-10-21 14:36:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-21 14:37:35 +00:00
|
|
|
if (!changes) return;
|
2022-10-21 14:36:49 +00:00
|
|
|
} else {
|
|
|
|
for (const row of rows)
|
2022-10-21 14:37:35 +00:00
|
|
|
fks.add(row.after[tableInfo.fk]);
|
2022-10-21 14:36:49 +00:00
|
|
|
}
|
2022-10-21 14:37:35 +00:00
|
|
|
} else {
|
|
|
|
for (const row of rows)
|
|
|
|
fks.add(row[tableInfo.fk]);
|
2022-10-21 14:36:49 +00:00
|
|
|
}
|
2022-10-21 14:37:35 +00:00
|
|
|
|
2022-10-23 19:46:07 +00:00
|
|
|
if (fks.size) {
|
|
|
|
const data = JSON.stringify(Array.from(fks));
|
|
|
|
this.channel.sendToQueue(this.config.queue,
|
|
|
|
Buffer.from(data));
|
|
|
|
this.debug('Queued', data);
|
|
|
|
}
|
|
|
|
|
2022-10-21 14:37:35 +00:00
|
|
|
const row = eventName === 'updaterows'
|
|
|
|
? rows[0].after
|
|
|
|
: rows[0];
|
|
|
|
|
|
|
|
if (this.config.debug) {
|
|
|
|
console.debug(`[${eventName}] ${table.tableName}: ${rows.length}`);
|
|
|
|
console.debug(` ${tableInfo.fk}: ${row[tableInfo.fk]}`);
|
|
|
|
if (column) {
|
|
|
|
let before = formatValue(rows[0].before[column]);
|
|
|
|
let after = formatValue(rows[0].after[column]);
|
|
|
|
console.debug(` ${column}: ${before} <- ${after}`);
|
2022-10-21 14:36:49 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-21 14:37:35 +00:00
|
|
|
|
|
|
|
this.position = evt.nextPosition;
|
2022-10-23 19:46:07 +00:00
|
|
|
this.flushed = false;
|
2022-10-21 14:36:49 +00:00
|
|
|
}
|
2022-10-21 14:37:35 +00:00
|
|
|
|
|
|
|
async flushQueue() {
|
2022-10-23 19:46:07 +00:00
|
|
|
if (this.flushed) return;
|
|
|
|
this.debug('Flush', `filename: ${this.filename}, position: ${this.position}`);
|
2022-10-21 14:37:35 +00:00
|
|
|
|
|
|
|
const replaceQuery =
|
|
|
|
'REPLACE INTO `binlogQueue` SET `code` = ?, `logName` = ?, `position` = ?';
|
2022-10-23 19:46:07 +00:00
|
|
|
if (!this.config.testMode)
|
2022-10-21 14:37:35 +00:00
|
|
|
await this.db.query(replaceQuery, [this.config.queue, this.filename, this.position]);
|
2022-10-23 19:46:07 +00:00
|
|
|
|
|
|
|
this.flushed = true;
|
2022-10-21 14:36:49 +00:00
|
|
|
}
|
2022-10-21 14:37:35 +00:00
|
|
|
|
|
|
|
async connectionPing() {
|
2022-10-23 19:46:07 +00:00
|
|
|
this.debug('Ping', 'Sending ping to database.');
|
|
|
|
|
|
|
|
// FIXME: Should Zongji.connection be pinged?
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
this.zongji.ctrlConnection.ping(err => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
})
|
2022-10-21 14:37:35 +00:00
|
|
|
await this.db.ping();
|
2022-10-21 14:36:49 +00:00
|
|
|
}
|
2022-10-23 19:46:07 +00:00
|
|
|
|
|
|
|
debug(namespace, message) {
|
|
|
|
if (this.config.debug)
|
|
|
|
console.debug(`${namespace}:`.blue, message.yellow);
|
|
|
|
}
|
2022-10-21 14:37:35 +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)
|
|
|
|
return a.getTime() === b.getTime();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatValue(value) {
|
|
|
|
if (value instanceof Date)
|
|
|
|
return value.toJSON();
|
|
|
|
return value;
|
|
|
|
}
|