2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-05-25 23:42:30 +08:00
|
|
|
const PSQL = require('cartodb-psql');
|
|
|
|
const copyTo = require('pg-copy-streams').to;
|
|
|
|
const copyFrom = require('pg-copy-streams').from;
|
|
|
|
|
2018-06-21 19:39:33 +08:00
|
|
|
const ACTION_TO = 'to';
|
|
|
|
const ACTION_FROM = 'from';
|
2018-07-20 21:32:32 +08:00
|
|
|
const DEFAULT_TIMEOUT = "'5h'";
|
2018-06-21 19:39:33 +08:00
|
|
|
|
2019-05-27 21:11:52 +08:00
|
|
|
const cancelQuery = pid => `SELECT pg_cancel_backend(${pid}) as cancelled`;
|
|
|
|
const terminateQuery = pid => `SELECT pg_terminate_backend(${pid}) as terminated`;
|
2019-05-14 21:39:54 +08:00
|
|
|
const timeoutQuery = timeout => `SET statement_timeout=${timeout}`;
|
2018-07-20 21:32:32 +08:00
|
|
|
|
2019-05-14 21:39:54 +08:00
|
|
|
module.exports = class StreamCopy {
|
2020-07-01 23:38:35 +08:00
|
|
|
constructor (sql, userDbParams) {
|
2019-05-14 21:39:54 +08:00
|
|
|
this.dbParams = Object.assign({}, userDbParams, {
|
2018-07-20 16:22:54 +08:00
|
|
|
port: global.settings.db_batch_port || userDbParams.port
|
|
|
|
});
|
2018-06-12 22:56:18 +08:00
|
|
|
this.sql = sql;
|
2018-06-21 20:53:07 +08:00
|
|
|
this.stream = null;
|
2018-07-20 21:32:32 +08:00
|
|
|
this.timeout = global.settings.copy_timeout || DEFAULT_TIMEOUT;
|
2018-06-12 22:56:18 +08:00
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
static get ACTION_TO () {
|
2018-06-21 19:39:33 +08:00
|
|
|
return ACTION_TO;
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
static get ACTION_FROM () {
|
2018-06-21 19:39:33 +08:00
|
|
|
return ACTION_FROM;
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
getPGStream (action, callback) {
|
2019-05-14 21:39:54 +08:00
|
|
|
const pg = new PSQL(this.dbParams);
|
|
|
|
|
|
|
|
pg.connect((err, client, done) => {
|
2018-06-08 22:58:32 +08:00
|
|
|
if (err) {
|
2019-05-14 21:39:54 +08:00
|
|
|
return callback(err);
|
2018-06-08 22:58:32 +08:00
|
|
|
}
|
|
|
|
|
2019-05-14 21:39:54 +08:00
|
|
|
client.query(timeoutQuery(this.timeout), (err) => {
|
2018-07-20 22:24:33 +08:00
|
|
|
if (err) {
|
2019-05-14 21:39:54 +08:00
|
|
|
return callback(err);
|
2018-07-20 22:24:33 +08:00
|
|
|
}
|
2018-06-14 00:26:58 +08:00
|
|
|
|
2019-05-24 21:39:56 +08:00
|
|
|
this.clientProcessID = client.processID;
|
|
|
|
|
2019-05-28 01:24:48 +08:00
|
|
|
this.stream = action === ACTION_TO ? copyTo(this.sql) : copyFrom(this.sql);
|
2019-05-24 23:31:56 +08:00
|
|
|
|
2018-07-20 22:24:33 +08:00
|
|
|
const pgstream = client.query(this.stream);
|
2018-06-21 21:16:47 +08:00
|
|
|
|
2019-05-24 21:53:09 +08:00
|
|
|
if (action === ACTION_TO) {
|
2019-05-24 23:31:56 +08:00
|
|
|
pgstream.on('end', () => done());
|
2020-07-23 00:29:05 +08:00
|
|
|
pgstream.on('warning', (msg) => this.logger.warn({ exception: new Error(msg) }, msg));
|
2019-05-24 21:53:09 +08:00
|
|
|
} else if (action === ACTION_FROM) {
|
2019-05-24 23:31:56 +08:00
|
|
|
pgstream.on('finish', () => done());
|
2020-05-19 15:23:48 +08:00
|
|
|
pgstream.on('client-close', err => client.connection.sendCopyFail(err.message));
|
2019-05-24 21:53:09 +08:00
|
|
|
}
|
|
|
|
|
2019-05-30 17:36:57 +08:00
|
|
|
pgstream.on('error', err => done(err));
|
2018-07-20 22:24:33 +08:00
|
|
|
|
2019-05-14 21:39:54 +08:00
|
|
|
callback(null, pgstream);
|
2018-07-20 22:24:33 +08:00
|
|
|
});
|
2018-06-08 22:50:12 +08:00
|
|
|
});
|
2018-05-25 23:42:30 +08:00
|
|
|
}
|
2018-06-21 19:21:23 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
getRowCount () {
|
2018-06-21 20:53:07 +08:00
|
|
|
return this.stream.rowCount;
|
2018-06-21 19:21:23 +08:00
|
|
|
}
|
2019-05-14 21:39:54 +08:00
|
|
|
|
2020-07-01 23:38:35 +08:00
|
|
|
cancel (pid, action, callback) {
|
2019-05-14 21:39:54 +08:00
|
|
|
const pg = new PSQL(this.dbParams);
|
2019-05-28 01:24:48 +08:00
|
|
|
const actionType = action === ACTION_TO ? ACTION_TO : ACTION_FROM;
|
2019-05-14 21:39:54 +08:00
|
|
|
|
|
|
|
pg.query(cancelQuery(pid), (err, result) => {
|
|
|
|
if (err) {
|
2020-07-01 23:38:35 +08:00
|
|
|
return callback(err);
|
2019-05-14 21:39:54 +08:00
|
|
|
}
|
2019-05-16 18:19:03 +08:00
|
|
|
|
2019-05-27 21:11:52 +08:00
|
|
|
const isCancelled = result.rows.length && result.rows[0].cancelled;
|
2019-05-14 21:39:54 +08:00
|
|
|
|
2019-05-27 21:11:52 +08:00
|
|
|
if (isCancelled) {
|
2020-07-01 23:38:35 +08:00
|
|
|
return callback();
|
2019-05-14 21:39:54 +08:00
|
|
|
}
|
|
|
|
|
2019-05-27 21:11:52 +08:00
|
|
|
return pg.query(terminateQuery(pid), (err, result) => {
|
|
|
|
if (err) {
|
2020-07-01 23:38:35 +08:00
|
|
|
return callback(err);
|
2019-05-27 21:11:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const isTerminated = result.rows.length && result.rows[0].terminated;
|
|
|
|
|
|
|
|
if (!isTerminated) {
|
2020-07-01 23:38:35 +08:00
|
|
|
return callback(new Error(`Unable to terminate "copy ${actionType}" stream query (pid: ${pid})`));
|
2019-05-27 21:11:52 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 23:38:35 +08:00
|
|
|
return callback();
|
2019-05-27 21:11:52 +08:00
|
|
|
});
|
2019-05-14 21:39:54 +08:00
|
|
|
});
|
|
|
|
}
|
2018-05-26 00:50:56 +08:00
|
|
|
};
|