Merge pull request #542 from CartoDB/copy-quota-exceeded

cancel query when quota exceeded or max POST size
This commit is contained in:
Simon Martín 2018-11-30 10:29:01 +01:00 committed by GitHub
commit 93c0ef4e0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 22 deletions

View File

@ -140,12 +140,14 @@ function handleCopyFrom (logger) {
if(metrics.size > dbRemainingQuota) { if(metrics.size > dbRemainingQuota) {
const quotaError = new Error('DB Quota exceeded'); const quotaError = new Error('DB Quota exceeded');
pgstream.emit('cancelQuery', err);
pgstream.emit('error', quotaError); pgstream.emit('error', quotaError);
} }
if((metrics.gzipSize || metrics.size) > COPY_FROM_MAX_POST_SIZE) { if((metrics.gzipSize || metrics.size) > COPY_FROM_MAX_POST_SIZE) {
const maxPostSizeError = new Error( const maxPostSizeError = new Error(
`COPY FROM maximum POST size of ${COPY_FROM_MAX_POST_SIZE_PRETTY} exceeded` `COPY FROM maximum POST size of ${COPY_FROM_MAX_POST_SIZE_PRETTY} exceeded`
); );
pgstream.emit('cancelQuery', err);
pgstream.emit('error', maxPostSizeError); pgstream.emit('error', maxPostSizeError);
} }
}) })

View File

@ -21,6 +21,26 @@ const statsClient = StatsClient.getInstance(global.settings.statsd);
const server = require('../../app/server')(statsClient); const server = require('../../app/server')(statsClient);
// Give it enough time to connect and issue the query
// but not too much so as to disconnect in the middle of the query.
const CLIENT_DISCONNECT_TIMEOUT = 100;
const assertCanReuseCanceledConnection = function (done) {
assert.response(server, {
url: '/api/v1/sql?' + querystring.stringify({
q: 'SELECT count(*) FROM copy_endpoints_test',
}),
headers: { host: 'vizzuality.cartodb.com' },
method: 'GET'
}, {}, function(err, res) {
assert.ifError(err);
assert.ok(res.statusCode === 200);
const result = JSON.parse(res.body);
assert.strictEqual(result.rows[0].count, 0);
done();
});
};
describe('copy-endpoints', function() { describe('copy-endpoints', function() {
before(function() { before(function() {
this.client = new Client({ this.client = new Client({
@ -347,10 +367,6 @@ describe('copy-endpoints', function() {
}); });
describe('client disconnection', function() { describe('client disconnection', function() {
// Give it enough time to connect and issue the query
// but not too much so as to disconnect in the middle of the query.
const CLIENT_DISCONNECT_TIMEOUT = 100;
before(function() { before(function() {
this.db_pool_size = global.settings.db_pool_size; this.db_pool_size = global.settings.db_pool_size;
global.settings.db_pool_size = 1; global.settings.db_pool_size = 1;
@ -374,22 +390,6 @@ describe('copy-endpoints', function() {
}); });
}; };
const assertCanReuseCanceledConnection = function (done) {
assert.response(server, {
url: '/api/v1/sql?' + querystring.stringify({
q: 'SELECT count(*) FROM copy_endpoints_test',
}),
headers: { host: 'vizzuality.cartodb.com' },
method: 'GET'
}, {}, function(err, res) {
assert.ifError(err);
assert.ok(res.statusCode === 200);
const result = JSON.parse(res.body);
assert.strictEqual(result.rows[0].count, 0);
done();
});
};
it('COPY TO returns the connection to the pool if the client disconnects', function(done) { it('COPY TO returns the connection to the pool if the client disconnects', function(done) {
const listener = server.listen(0, '127.0.0.1'); const listener = server.listen(0, '127.0.0.1');
@ -511,6 +511,9 @@ describe('copy-endpoints', function() {
END; END;
$$ LANGUAGE 'plpgsql' VOLATILE; $$ LANGUAGE 'plpgsql' VOLATILE;
`, err => done(err)); `, err => done(err));
this.db_pool_size = global.settings.db_pool_size;
global.settings.db_pool_size = 1;
}); });
after('Restore the old quota', function(done) { after('Restore the old quota', function(done) {
@ -523,6 +526,8 @@ describe('copy-endpoints', function() {
END; END;
$$ LANGUAGE 'plpgsql' VOLATILE; $$ LANGUAGE 'plpgsql' VOLATILE;
`, err => done(err)); `, err => done(err));
global.settings.db_pool_size = this.db_pool_size;
}); });
it('COPY FROM fails with an error if DB quota is exhausted', function(done) { it('COPY FROM fails with an error if DB quota is exhausted', function(done) {
@ -540,7 +545,8 @@ describe('copy-endpoints', function() {
}, function(err, res) { }, function(err, res) {
const response = JSON.parse(res.body); const response = JSON.parse(res.body);
assert.deepEqual(response, { error: ["DB Quota exceeded"] }); assert.deepEqual(response, { error: ["DB Quota exceeded"] });
done();
setTimeout(() => assertCanReuseCanceledConnection(done), CLIENT_DISCONNECT_TIMEOUT);
}); });
}); });
@ -566,10 +572,13 @@ describe('copy-endpoints', function() {
this.previous_max_post_size_pretty = global.settings.copy_from_max_post_size_pretty; this.previous_max_post_size_pretty = global.settings.copy_from_max_post_size_pretty;
global.settings.copy_from_max_post_size = 10; global.settings.copy_from_max_post_size = 10;
global.settings.copy_from_max_post_size_pretty = '10 bytes'; global.settings.copy_from_max_post_size_pretty = '10 bytes';
this.db_pool_size = global.settings.db_pool_size;
global.settings.db_pool_size = 1;
}); });
after('Restore the max POST size limit values', function() { after('Restore the max POST size limit values', function() {
global.settings.copy_from_max_post_size = this.previous_max_post_size; global.settings.copy_from_max_post_size = this.previous_max_post_size;
global.settings.copy_from_max_post_size_pretty = this.previous_max_post_size_pretty; global.settings.copy_from_max_post_size_pretty = this.previous_max_post_size_pretty;
global.settings.db_pool_size = this.db_pool_size;
}); });
it('honors the max POST size limit', function(done) { it('honors the max POST size limit', function(done) {
@ -587,7 +596,8 @@ describe('copy-endpoints', function() {
}, function(err, res) { }, function(err, res) {
const response = JSON.parse(res.body); const response = JSON.parse(res.body);
assert.deepEqual(response, { error: ["COPY FROM maximum POST size of 10 bytes exceeded"] }); assert.deepEqual(response, { error: ["COPY FROM maximum POST size of 10 bytes exceeded"] });
done();
setTimeout(() => assertCanReuseCanceledConnection(done), CLIENT_DISCONNECT_TIMEOUT);
}); });
}); });
}); });