From 0c5af23e6b88299a7ec1fb5807a778a0f9c6f3ed Mon Sep 17 00:00:00 2001 From: Prudhvi Date: Tue, 28 Mar 2017 16:03:16 -0700 Subject: [PATCH 1/4] Logging actual message --- index.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index f9e97ae..0b02fd8 100644 --- a/index.js +++ b/index.js @@ -36,11 +36,18 @@ Sentinel.prototype.createClient = function(masterName, opts) { console.error("Unable to subscribe to Sentinel PUBSUB"); } }); - pubsubClient.on("message", function(channel, message) { - console.warn("Received +switch-master message from Redis Sentinel.", - " Reconnecting clients."); - self.reconnectAllClients(); - }); + pubsubClient.on("message", function (channel, message) { + var failedOverMaster = message.split(" ")[0]; + console.warn("Received +switch-master message from Redis Sentinel for master", failedOverMaster); + if (failedOverMaster === masterName) { + console.warn("Reconnecting clients."); + self.reconnectAllClients(); + } + else { + console.warn("Ignoring the message"); + } + + }); pubsubClient.on("error", function(error) {}); self.pubsub.push(pubsubClient); } From 5819cd3d66cf1970181fb74e8ccc68ddeb618ab7 Mon Sep 17 00:00:00 2001 From: Shreekar Shetty Date: Tue, 20 Nov 2018 13:33:40 +0000 Subject: [PATCH 2/4] Connect Timeout for Redis & shuffle sentinels --- index.js | 41 +++++++++++++++++++++++++++++++---------- test/test.js | 4 ++-- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 0b02fd8..fc58626 100644 --- a/index.js +++ b/index.js @@ -36,7 +36,7 @@ Sentinel.prototype.createClient = function(masterName, opts) { console.error("Unable to subscribe to Sentinel PUBSUB"); } }); - pubsubClient.on("message", function (channel, message) { + pubsubClient.on("message", function (channel, message) { var failedOverMaster = message.split(" ")[0]; console.warn("Received +switch-master message from Redis Sentinel for master", failedOverMaster); if (failedOverMaster === masterName) { @@ -46,8 +46,7 @@ Sentinel.prototype.createClient = function(masterName, opts) { else { console.warn("Ignoring the message"); } - - }); + }); pubsubClient.on("error", function(error) {}); self.pubsub.push(pubsubClient); } @@ -62,7 +61,7 @@ Sentinel.prototype.createClientInternal = function(masterName, opts) { opts = opts || {}; var role = opts.role || 'master'; - + console.info("Sentinel.createClientInternal: role - " + role ) var endpoints = this.endpoints; @@ -121,7 +120,7 @@ Sentinel.prototype.createClientInternal = function(masterName, opts) { } else { // Try reconnecting - remove the old stream first. client.stream.end(); - + console.info("refreshEndpoints : " + resolver.name + " responded with host(" + ip + ") & port(" + port + ")") client.connectionOption.port = port; client.connectionOption.host = ip; client.connection_gone("sentinel induced refresh"); @@ -132,7 +131,6 @@ Sentinel.prototype.createClientInternal = function(masterName, opts) { // Crude but may do for now. On error re-resolve the master // and retry the connection function hitError(eventName, err) { - var _args = arguments; function reemit() { oldEmit.apply(client, _args); @@ -203,6 +201,7 @@ function resolveClient() { // Because finding the master is going to be an async list we will terminate // when we find one then use promises... promise = endpoints.reduce(function(soFar, endpoint) { + console.info("Calling " + checkEndpointFn.name +" with endpoint(" + endpoint.host + ":" +endpoint.port + ")") return soFar.then(function() { var deferred = when.defer(); @@ -215,6 +214,7 @@ function resolveClient() { } else { // This is the endpoint that has responded so stick it on the top of // the list + console.info(checkEndpointFn.name + " got a response on sentinal endpoint host:"+ endpoint.host + ", port:" + endpoint.port) var index = endpoints.indexOf(endpoint); endpoints.splice(index, 1); endpoints.unshift(endpoint); @@ -239,9 +239,10 @@ function resolveClient() { } function isSentinelOk(endpoint, callback) { - var client = redis.createClient(endpoint.port, endpoint.host); + var client = redis.createClient(endpoint.port, endpoint.host, {connect_timeout: 1000}); var callbackSent = false; client.on("error", function(err) { + console.log("isSentinelOk Error - " + endpoint.host + ":" + endpoint.port + " - " + err) if (!callbackSent) { callbackSent = true; callback(err); @@ -260,7 +261,7 @@ function isSentinelOk(endpoint, callback) { } function getMasterFromEndpoint(endpoint, masterName, callback) { - var sentinelClient = redis.createClient(endpoint.port, endpoint.host); + var sentinelClient = redis.createClient(endpoint.port, endpoint.host, {connect_timeout: 1000}); var callbackSent = false; // If there is an error then callback with it @@ -284,6 +285,7 @@ function getMasterFromEndpoint(endpoint, masterName, callback) { } else { var ip = result[0]; var port = result[1]; + console.info("getMasterFromEndpoint - Redis master host: " + ip + ", port: " + port ) callback(null, ip, port); } }); @@ -291,7 +293,7 @@ function getMasterFromEndpoint(endpoint, masterName, callback) { } function getSlaveFromEndpoint(endpoint, masterName, callback) { - var sentinelClient = redis.createClient(endpoint.port, endpoint.host); + var sentinelClient = redis.createClient(endpoint.port, endpoint.host, {connect_timeout: 1000}); var callbackSent = false; // If there is an error then callback with it @@ -349,10 +351,29 @@ function parseSentinelResponse(resArr){ // Shortcut for quickly getting a client from endpoints function createClient(endpoints, masterName, options) { - var sentinel = Sentinel(endpoints); + var sentinel = Sentinel(shuffle(endpoints)); return sentinel.createClient(masterName, options); } +// From https://bost.ocks.org/mike/shuffle/ +function shuffle(array) { + var m = array.length, t, i; + + // While there remain elements to shuffle… + while (m) { + + // Pick a remaining element… + i = Math.floor(Math.random() * m--); + + // And swap it with the current element. + t = array[m]; + array[m] = array[i]; + array[i] = t; + } + + return array; +} + module.exports.Sentinel = Sentinel; module.exports.createClient = createClient; module.exports.redis = redis; diff --git a/test/test.js b/test/test.js index 99a8528..512fa78 100644 --- a/test/test.js +++ b/test/test.js @@ -70,7 +70,7 @@ describe('Redis Sentinel tests', function() { var redisClient = sentinel.createClient(endpoints, {role: 'sentinel'}); redisClient.on('ready', function() { expect(redisClient.connectionOption.host).to.equal('127.0.0.1'); - expect(redisClient.connectionOption.port).to.equal("26380"); + expect(["26380", "26379"]).to.contain(redisClient.connectionOption.port); done(); }); }); @@ -113,7 +113,7 @@ describe('Redis Sentinel tests', function() { var redisClient = sentinel.createClient(endpoints, {role: 'sentinel'}); redisClient.on('ready', function() { expect(redisClient.connectionOption.host).to.equal('127.0.0.1'); - expect(redisClient.connectionOption.port).to.equal("26380"); + expect(["26380", "26379"]).to.contain(redisClient.connectionOption.port); done(); }); }); From 03c091e62e3f8899a57abad0d83187a14bf90c01 Mon Sep 17 00:00:00 2001 From: Shreekar Shetty Date: Tue, 20 Nov 2018 17:05:12 +0000 Subject: [PATCH 3/4] Moved to minilog logging --- index.js | 23 ++++++++++++----------- package.json | 3 ++- test/test.js | 3 +++ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index fc58626..a4959dc 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ var redis = require('redis'), net = require('net'), + logging = require('minilog')('redis-sentinel:Sentinel'), when = require('when'); function Sentinel(endpoints) { @@ -33,18 +34,18 @@ Sentinel.prototype.createClient = function(masterName, opts) { pubsubClient = this.createClientInternal(masterName, pubsubOpts); pubsubClient.subscribe("+switch-master", function(error) { if (error) { - console.error("Unable to subscribe to Sentinel PUBSUB"); + logging.error("Unable to subscribe to Sentinel PUBSUB"); } }); pubsubClient.on("message", function (channel, message) { var failedOverMaster = message.split(" ")[0]; - console.warn("Received +switch-master message from Redis Sentinel for master", failedOverMaster); + logging.warn("Received +switch-master message from Redis Sentinel for master", failedOverMaster); if (failedOverMaster === masterName) { - console.warn("Reconnecting clients."); - self.reconnectAllClients(); + logging.warn("Reconnecting clients."); + self.reconnectAllClients(); } else { - console.warn("Ignoring the message"); + logging.warn("Ignoring the message"); } }); pubsubClient.on("error", function(error) {}); @@ -61,7 +62,7 @@ Sentinel.prototype.createClientInternal = function(masterName, opts) { opts = opts || {}; var role = opts.role || 'master'; - console.info("Sentinel.createClientInternal: role - " + role ) + logging.debug("Sentinel.createClientInternal: role - " + role ) var endpoints = this.endpoints; @@ -120,7 +121,7 @@ Sentinel.prototype.createClientInternal = function(masterName, opts) { } else { // Try reconnecting - remove the old stream first. client.stream.end(); - console.info("refreshEndpoints : " + resolver.name + " responded with host(" + ip + ") & port(" + port + ")") + logging.debug("refreshEndpoints : " + resolver.name + " responded with host(" + ip + ") & port(" + port + ")") client.connectionOption.port = port; client.connectionOption.host = ip; client.connection_gone("sentinel induced refresh"); @@ -201,7 +202,7 @@ function resolveClient() { // Because finding the master is going to be an async list we will terminate // when we find one then use promises... promise = endpoints.reduce(function(soFar, endpoint) { - console.info("Calling " + checkEndpointFn.name +" with endpoint(" + endpoint.host + ":" +endpoint.port + ")") + logging.debug("Calling " + checkEndpointFn.name +" with endpoint(" + endpoint.host + ":" +endpoint.port + ")") return soFar.then(function() { var deferred = when.defer(); @@ -214,7 +215,7 @@ function resolveClient() { } else { // This is the endpoint that has responded so stick it on the top of // the list - console.info(checkEndpointFn.name + " got a response on sentinal endpoint host:"+ endpoint.host + ", port:" + endpoint.port) + logging.debug(checkEndpointFn.name + " got a response on sentinel endpoint host:"+ endpoint.host + ", port:" + endpoint.port) var index = endpoints.indexOf(endpoint); endpoints.splice(index, 1); endpoints.unshift(endpoint); @@ -242,7 +243,7 @@ function isSentinelOk(endpoint, callback) { var client = redis.createClient(endpoint.port, endpoint.host, {connect_timeout: 1000}); var callbackSent = false; client.on("error", function(err) { - console.log("isSentinelOk Error - " + endpoint.host + ":" + endpoint.port + " - " + err) + logging.error("isSentinelOk Error - " + endpoint.host + ":" + endpoint.port + " - " + err) if (!callbackSent) { callbackSent = true; callback(err); @@ -285,7 +286,7 @@ function getMasterFromEndpoint(endpoint, masterName, callback) { } else { var ip = result[0]; var port = result[1]; - console.info("getMasterFromEndpoint - Redis master host: " + ip + ", port: " + port ) + logging.debug("getMasterFromEndpoint - Redis master host: " + ip + ", port: " + port ) callback(null, ip, port); } }); diff --git a/package.json b/package.json index c433029..d041166 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ ], "dependencies": { "redis": "0.12.x", - "when": "^3.5.1" + "when": "^3.5.1", + "minilog": "*" }, "devDependencies": { "mocha": "*", diff --git a/test/test.js b/test/test.js index 512fa78..bebbae5 100644 --- a/test/test.js +++ b/test/test.js @@ -1,9 +1,12 @@ var sentinel = require('../'); var expect = require('chai').expect; var redis = require('redis'); +var Minilog = require('minilog'); describe('Redis Sentinel tests', function() { + Minilog.enable() + describe('initial connection', function() { it('should get master correctly with single sentinel', function(done) { From a2b38b7231364dc52a347fc95940797b815e4992 Mon Sep 17 00:00:00 2001 From: Shreekar Shetty Date: Fri, 7 Dec 2018 11:10:33 +0000 Subject: [PATCH 4/4] version bump to 0.3.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d041166..8ca0547 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "redis-sentinel", - "version": "0.3.3", + "version": "0.3.4", "description": "Redis sentinel client for nodejs", "main": "index.js", "scripts": {