Skip to content

Commit

Permalink
CNS-180 use multiple connections to redis to avoid blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
arekinath committed Dec 5, 2016
1 parent 2913219 commit d485234
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 122 deletions.
37 changes: 24 additions & 13 deletions lib/api-server.js
Expand Up @@ -37,8 +37,8 @@ var NS_TTL = consts.NS_TTL;
function APIServer(opts) {
assert.object(opts, 'options');

assert.object(opts.client, 'options.client');
this.redis = opts.client;
assert.object(opts.redisPool, 'options.redisPool');
this.redisPool = opts.redisPool;

assert.object(opts.dnsServer, 'options.dnsServer');
this.dnsServer = opts.dnsServer;
Expand Down Expand Up @@ -122,7 +122,7 @@ function APIServer(opts) {
log: this.log,
config: this.config,
agent: agent,
client: this.redis,
redisPool: this.redisPool,
ufdsConnections: 1
};
popts.ufdsPool = createUfdsPool(popts);
Expand Down Expand Up @@ -213,7 +213,13 @@ function setupServer(addr) {

server.pre(function (req, res, next) {
req.log = slog.child({req: req, res: res});
next();
self.redisPool.claim(function (err, handle, redis) {
if (err)
throw (err);
req.redis = redis;
req.redisHandle = handle;
next();
});
});

server.use(restify.queryParser());
Expand All @@ -224,6 +230,13 @@ function setupServer(addr) {
server.on('after', restify.auditLogger({
log: slog
}));
server.on('after', function (req) {
if (req.redisHandle) {
req.redisHandle.release();
delete (req.redisHandle);
delete (req.redis);
}
});
server.on('uncaughtException', function (req, res, route, err) {
req.log.error(err);
res.send(err);
Expand Down Expand Up @@ -283,12 +296,11 @@ function ping_v1(req, res, next) {
}

function getVM_v1(req, res, next) {
var self = this;
var uuid = req.params.uuid;
var result = {};
result.uuid = uuid;

self.redis.hgetall('vm:' + uuid, function (err, val) {
req.redis.hgetall('vm:' + uuid, function (err, val) {
var e;
if (err) {
e = new Error('Error communicating with ' +
Expand Down Expand Up @@ -347,7 +359,7 @@ function getPeer_v1(req, res, next) {
});

function fetchSerials(_, cb) {
self.redis.hgetall('peer:' + addr, function (err, serials) {
req.redis.hgetall('peer:' + addr, function (err, serials) {
var rerr;
if (err) {
rerr = new Error('Error communicating with ' +
Expand All @@ -373,7 +385,7 @@ function getPeer_v1(req, res, next) {
}

function fetchVersion(_, cb) {
self.redis.get('peer:' + addr + ':version',
req.redis.get('peer:' + addr + ':version',
function (err, ver) {
var rerr;
if (err) {
Expand Down Expand Up @@ -413,7 +425,7 @@ function getPeer_v1(req, res, next) {
function delPeer_v1(req, res, next) {
var addr = normalizeIP(req.params.addr);

this.redis.del('peer:' + addr, 'peer:' + addr + ':version',
req.redis.del('peer:' + addr, 'peer:' + addr + ':version',
function (err) {
if (err) {
next(err);
Expand Down Expand Up @@ -449,7 +461,7 @@ function getPeers_v1(req, res, next) {
});

function fetchSerials(_, cb) {
self.dnsServer.getPeerSerials(function (err, sres) {
self.dnsServer.getPeerSerials(req.redis, function (err, sres) {
if (err) {
cb(err);
return;
Expand Down Expand Up @@ -492,11 +504,10 @@ function getPeers_v1(req, res, next) {
}

function getZones_v1(req, res, next) {
var self = this;
var zoneNames = Object.keys(this.config.forward_zones);
var zones = [];

this.redis.keys('zone:*.arpa', function (err, keys) {
req.redis.keys('zone:*.arpa', function (err, keys) {
if (!err && keys !== null) {
for (var i = 0; i < keys.length; ++i) {
var k = keys[i].split(':')[1];
Expand All @@ -517,7 +528,7 @@ function getZones_v1(req, res, next) {
});

function addZone(z, cb) {
self.redis.get('zone:' + z + ':latest',
req.redis.get('zone:' + z + ':latest',
function (err2, val) {
if (err2) {
var e = new Error('Redis error: ' +
Expand Down

0 comments on commit d485234

Please sign in to comment.