2018-04-13 03:25:28 +08:00
|
|
|
'use strict';
|
|
|
|
|
2020-07-01 18:29:51 +08:00
|
|
|
const initLogger = require('../middlewares/logger');
|
2020-07-23 18:14:39 +08:00
|
|
|
const logReqRes = require('../middlewares/log-req-res');
|
2020-07-01 18:29:51 +08:00
|
|
|
const profiler = require('../middlewares/profiler');
|
2019-10-04 01:35:18 +08:00
|
|
|
const user = require('../middlewares/user');
|
|
|
|
const authorization = require('../middlewares/authorization');
|
|
|
|
const connectionParams = require('../middlewares/connection-params');
|
|
|
|
const dbQuota = require('../middlewares/db-quota');
|
|
|
|
const bodyParser = require('../middlewares/body-parser');
|
|
|
|
const rateLimits = require('../middlewares/rate-limit');
|
|
|
|
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimits;
|
2019-10-04 00:24:39 +08:00
|
|
|
const errorHandlerFactory = require('../../services/error-handler-factory');
|
|
|
|
const StreamCopy = require('../../services/stream-copy');
|
|
|
|
const StreamCopyMetrics = require('../../services/stream-copy-metrics');
|
|
|
|
const Throttler = require('../../services/throttler-stream');
|
2018-06-08 21:03:21 +08:00
|
|
|
const zlib = require('zlib');
|
2019-05-29 17:20:18 +08:00
|
|
|
const { PassThrough } = require('stream');
|
2019-07-29 22:24:48 +08:00
|
|
|
const params = require('../middlewares/params');
|
2020-06-30 23:42:59 +08:00
|
|
|
const tag = require('../middlewares/tag');
|
2018-04-13 03:25:28 +08:00
|
|
|
|
2019-10-01 18:31:05 +08:00
|
|
|
module.exports = class CopyController {
|
2020-07-01 18:32:51 +08:00
|
|
|
constructor ({ metadataBackend, userDatabaseService, statsdClient, userLimitsService, logger }) {
|
2019-10-01 18:31:05 +08:00
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.userDatabaseService = userDatabaseService;
|
2020-07-01 18:29:51 +08:00
|
|
|
this.statsdClient = statsdClient;
|
2019-10-01 18:31:05 +08:00
|
|
|
this.userLimitsService = userLimitsService;
|
2020-07-01 18:29:51 +08:00
|
|
|
this.logger = logger;
|
2019-10-01 18:31:05 +08:00
|
|
|
}
|
|
|
|
|
2019-10-02 22:02:13 +08:00
|
|
|
route (sqlRouter) {
|
2019-10-01 18:31:05 +08:00
|
|
|
const copyFromMiddlewares = endpointGroup => {
|
|
|
|
return [
|
2020-07-01 18:29:51 +08:00
|
|
|
initLogger({ logger: this.logger }),
|
2020-07-23 17:58:52 +08:00
|
|
|
user(this.metadataBackend),
|
2020-07-23 18:14:39 +08:00
|
|
|
logReqRes(),
|
2020-07-01 18:29:51 +08:00
|
|
|
profiler({ statsClient: this.statsdClient }),
|
2020-06-30 23:42:59 +08:00
|
|
|
tag({ tags: ['ingestion', 'copyfrom'] }),
|
2019-10-04 01:35:18 +08:00
|
|
|
rateLimits(this.userLimitsService, endpointGroup),
|
|
|
|
authorization(this.metadataBackend),
|
|
|
|
connectionParams(this.userDatabaseService),
|
|
|
|
dbQuota(),
|
2019-10-01 18:31:05 +08:00
|
|
|
params({ strategy: 'copyfrom' }),
|
2020-06-30 23:42:59 +08:00
|
|
|
handleCopyFrom(),
|
|
|
|
errorHandler()
|
2019-10-01 18:31:05 +08:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
const copyToMiddlewares = endpointGroup => {
|
|
|
|
return [
|
2020-07-23 18:14:39 +08:00
|
|
|
initLogger({ logger: this.logger }),
|
2020-07-23 17:58:52 +08:00
|
|
|
user(this.metadataBackend),
|
2020-07-23 18:14:39 +08:00
|
|
|
logReqRes({ logOnEvent: 'log' }),
|
2020-07-02 02:23:16 +08:00
|
|
|
profiler({ statsClient: this.statsdClient, logOnEvent: 'log' }),
|
|
|
|
tag({ tags: ['ingestion', 'copyto'], logOnEvent: 'log' }),
|
2019-10-04 01:35:18 +08:00
|
|
|
bodyParser(),
|
|
|
|
rateLimits(this.userLimitsService, endpointGroup),
|
|
|
|
authorization(this.metadataBackend),
|
|
|
|
connectionParams(this.userDatabaseService),
|
2019-10-01 18:31:05 +08:00
|
|
|
params({ strategy: 'copyto' }),
|
2020-07-02 02:23:16 +08:00
|
|
|
handleCopyTo({ logger: this.logger }),
|
2020-06-30 23:42:59 +08:00
|
|
|
errorHandler()
|
2019-10-01 18:31:05 +08:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2019-10-02 22:02:13 +08:00
|
|
|
sqlRouter.post('/copyfrom', copyFromMiddlewares(RATE_LIMIT_ENDPOINTS_GROUPS.COPY_FROM));
|
|
|
|
sqlRouter.get('/copyto', copyToMiddlewares(RATE_LIMIT_ENDPOINTS_GROUPS.COPY_TO));
|
|
|
|
sqlRouter.post('/copyto', copyToMiddlewares(RATE_LIMIT_ENDPOINTS_GROUPS.COPY_TO));
|
2019-10-01 18:31:05 +08:00
|
|
|
}
|
2018-04-13 03:25:28 +08:00
|
|
|
};
|
|
|
|
|
2020-07-02 02:23:16 +08:00
|
|
|
function handleCopyTo ({ logger: mainLogger }) {
|
2018-05-22 17:54:10 +08:00
|
|
|
return function handleCopyToMiddleware (req, res, next) {
|
2020-06-30 23:42:59 +08:00
|
|
|
const { logger } = res.locals;
|
2020-07-23 00:29:05 +08:00
|
|
|
const { request_id: requestId } = logger.bindings();
|
2019-07-26 22:06:53 +08:00
|
|
|
const { userDbParams, user } = res.locals;
|
|
|
|
const { sql, filename } = res.locals.params;
|
2018-05-08 18:52:33 +08:00
|
|
|
|
2018-08-14 21:14:22 +08:00
|
|
|
// it is not sure, nginx may choose not to compress the body
|
2018-08-14 21:30:51 +08:00
|
|
|
// but we want to know it and save it in the metrics
|
|
|
|
// https://github.com/CartoDB/CartoDB-SQL-API/issues/515
|
2018-08-14 21:14:22 +08:00
|
|
|
const isGzip = req.get('accept-encoding') && req.get('accept-encoding').includes('gzip');
|
|
|
|
|
2019-05-14 21:39:54 +08:00
|
|
|
const streamCopy = new StreamCopy(sql, userDbParams, logger);
|
2018-08-14 21:14:22 +08:00
|
|
|
const metrics = new StreamCopyMetrics(logger, 'copyto', sql, user, isGzip);
|
2018-06-08 21:03:21 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
res.header('Content-Disposition', `attachment; filename=${encodeURIComponent(filename)}`);
|
|
|
|
res.header('Content-Type', 'application/octet-stream');
|
2018-05-08 19:08:29 +08:00
|
|
|
|
2018-06-22 16:50:39 +08:00
|
|
|
streamCopy.getPGStream(StreamCopy.ACTION_TO, (err, pgstream) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
2018-06-08 23:09:28 +08:00
|
|
|
}
|
2018-06-22 16:50:39 +08:00
|
|
|
|
2020-07-02 02:23:16 +08:00
|
|
|
req.on('aborted', () => {
|
|
|
|
const err = new Error('Connection closed by client');
|
2018-06-22 16:50:39 +08:00
|
|
|
|
2020-07-02 02:23:16 +08:00
|
|
|
metrics.end(null, err);
|
|
|
|
res.emit('log');
|
|
|
|
pgstream.emit('error', err);
|
2020-07-01 23:38:35 +08:00
|
|
|
|
2020-07-02 02:23:16 +08:00
|
|
|
const pid = streamCopy.clientProcessID;
|
|
|
|
streamCopy.cancel(pid, StreamCopy.ACTION_TO, (err) => {
|
|
|
|
if (err) {
|
2020-07-23 00:29:05 +08:00
|
|
|
return mainLogger.error({ request_id: requestId, exception: err, action: `copy-${StreamCopy.ACTION_TO}`, pid }, 'Unable to cancel stream query');
|
2020-07-02 02:23:16 +08:00
|
|
|
}
|
|
|
|
|
2020-07-23 00:29:05 +08:00
|
|
|
mainLogger.info({ request_id: requestId, action: `copy-${StreamCopy.ACTION_TO}`, pid }, 'Canceled stream query successfully');
|
2020-07-02 02:23:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return next(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
pgstream
|
|
|
|
.on('data', data => metrics.addSize(data.length))
|
2020-07-02 02:59:21 +08:00
|
|
|
.on('error', err => {
|
|
|
|
metrics.end(null, err);
|
|
|
|
return next(err);
|
|
|
|
})
|
2020-02-27 00:19:07 +08:00
|
|
|
.on('end', () => {
|
|
|
|
metrics.end(streamCopy.getRowCount());
|
2020-07-02 02:23:16 +08:00
|
|
|
res.emit('log');
|
2020-02-27 00:19:07 +08:00
|
|
|
return next();
|
|
|
|
})
|
2020-07-02 02:23:16 +08:00
|
|
|
.pipe(res);
|
2018-06-22 16:50:39 +08:00
|
|
|
});
|
2018-05-22 17:54:10 +08:00
|
|
|
};
|
|
|
|
}
|
2018-05-08 18:52:33 +08:00
|
|
|
|
2020-06-30 23:42:59 +08:00
|
|
|
function handleCopyFrom () {
|
2018-05-22 17:56:50 +08:00
|
|
|
return function handleCopyFromMiddleware (req, res, next) {
|
2020-06-30 23:42:59 +08:00
|
|
|
const { logger } = res.locals;
|
2019-07-26 22:06:53 +08:00
|
|
|
const { userDbParams, user, dbRemainingQuota } = res.locals;
|
|
|
|
const { sql } = res.locals.params;
|
2018-06-11 18:55:30 +08:00
|
|
|
const isGzip = req.get('content-encoding') === 'gzip';
|
2018-07-26 18:52:18 +08:00
|
|
|
const COPY_FROM_MAX_POST_SIZE = global.settings.copy_from_max_post_size || 2 * 1024 * 1024 * 1024; // 2 GB
|
2018-07-24 00:24:48 +08:00
|
|
|
const COPY_FROM_MAX_POST_SIZE_PRETTY = global.settings.copy_from_max_post_size_pretty || '2 GB';
|
2018-06-08 21:03:21 +08:00
|
|
|
|
2019-05-14 21:39:54 +08:00
|
|
|
const streamCopy = new StreamCopy(sql, userDbParams, logger);
|
2019-05-16 21:06:27 +08:00
|
|
|
const decompress = isGzip ? zlib.createGunzip() : new PassThrough();
|
2019-05-16 23:24:21 +08:00
|
|
|
const metrics = new StreamCopyMetrics(logger, 'copyfrom', sql, user, isGzip);
|
2018-06-08 21:03:21 +08:00
|
|
|
|
2018-06-22 16:50:39 +08:00
|
|
|
streamCopy.getPGStream(StreamCopy.ACTION_FROM, (err, pgstream) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2019-05-29 01:24:29 +08:00
|
|
|
const throttle = new Throttler(pgstream);
|
2019-05-28 01:32:43 +08:00
|
|
|
|
2018-06-22 16:50:39 +08:00
|
|
|
req
|
|
|
|
.on('data', data => isGzip ? metrics.addGzipSize(data.length) : undefined)
|
2018-08-10 20:38:24 +08:00
|
|
|
.on('error', err => {
|
|
|
|
metrics.end(null, err);
|
|
|
|
pgstream.emit('error', err);
|
|
|
|
})
|
2020-05-19 15:23:48 +08:00
|
|
|
.on('close', () => pgstream.emit('client-close', new Error('Connection closed by client')))
|
2019-12-24 01:19:08 +08:00
|
|
|
.pipe(throttle)
|
|
|
|
.pipe(decompress)
|
2018-07-23 21:05:22 +08:00
|
|
|
.on('data', data => {
|
|
|
|
metrics.addSize(data.length);
|
2018-07-23 22:59:05 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
if (metrics.size > dbRemainingQuota) {
|
2019-05-16 21:07:26 +08:00
|
|
|
return pgstream.emit('error', new Error('DB Quota exceeded'));
|
2018-07-23 22:59:05 +08:00
|
|
|
}
|
2019-05-16 21:07:26 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
if ((metrics.gzipSize || metrics.size) > COPY_FROM_MAX_POST_SIZE) {
|
2019-05-16 21:07:26 +08:00
|
|
|
return pgstream.emit('error', new Error(
|
2018-07-24 00:24:48 +08:00
|
|
|
`COPY FROM maximum POST size of ${COPY_FROM_MAX_POST_SIZE_PRETTY} exceeded`
|
2019-05-16 21:07:26 +08:00
|
|
|
));
|
2018-07-23 21:05:22 +08:00
|
|
|
}
|
|
|
|
})
|
2019-05-16 21:07:26 +08:00
|
|
|
.on('error', err => {
|
|
|
|
err.message = `Error while gunzipping: ${err.message}`;
|
|
|
|
metrics.end(null, err);
|
|
|
|
pgstream.emit('error', err);
|
|
|
|
})
|
2019-12-24 01:19:08 +08:00
|
|
|
.pipe(pgstream)
|
2018-06-22 16:50:39 +08:00
|
|
|
.on('error', err => {
|
|
|
|
metrics.end(null, err);
|
2019-05-24 21:39:56 +08:00
|
|
|
|
2018-06-12 21:18:28 +08:00
|
|
|
return next(err);
|
2018-06-22 16:50:39 +08:00
|
|
|
})
|
|
|
|
.on('end', () => {
|
2019-05-24 18:45:27 +08:00
|
|
|
metrics.end(streamCopy.getRowCount());
|
2018-06-22 16:50:39 +08:00
|
|
|
|
|
|
|
const { time, rows } = metrics;
|
|
|
|
|
|
|
|
if (!rows) {
|
2019-12-24 01:19:08 +08:00
|
|
|
return next(new Error('No rows copied'));
|
2018-06-22 16:50:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
res.send({
|
|
|
|
time,
|
|
|
|
total_rows: rows
|
2018-06-14 02:11:01 +08:00
|
|
|
});
|
2020-02-27 00:19:07 +08:00
|
|
|
|
|
|
|
return next();
|
2018-06-22 16:50:39 +08:00
|
|
|
});
|
|
|
|
});
|
2018-05-22 17:56:50 +08:00
|
|
|
};
|
|
|
|
}
|
2018-05-08 18:52:33 +08:00
|
|
|
|
2020-06-30 23:42:59 +08:00
|
|
|
function errorHandler () {
|
2018-05-25 23:50:59 +08:00
|
|
|
return function errorHandlerMiddleware (err, req, res, next) {
|
2020-06-30 23:42:59 +08:00
|
|
|
const { logger } = res.locals;
|
2018-05-25 23:50:59 +08:00
|
|
|
if (res.headersSent) {
|
2020-07-23 00:29:05 +08:00
|
|
|
logger.error({ exception: err }, 'Error while handling the request');
|
2018-05-25 23:50:59 +08:00
|
|
|
const errorHandler = errorHandlerFactory(err);
|
|
|
|
res.write(JSON.stringify(errorHandler.getResponse()));
|
|
|
|
res.end();
|
2020-02-27 00:19:07 +08:00
|
|
|
next();
|
2018-05-25 23:50:59 +08:00
|
|
|
} else {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|