redis / ioredis

🚀 A robust, performance-focused, and full-featured Redis client for Node.js.
MIT License
14.31k stars 1.19k forks source link

ioredis tries to connect to localhost when sentinels are provided #1067

Open zizzfizzix opened 4 years ago

zizzfizzix commented 4 years ago

I'm trying to connect to a sentinel with:

const redisConfig = { host: '10.82.102.71', port: 31691 };
const redisClient = new Redis({
        sentinels: [ redisConfig ],
        name: "redis-cluster"
    });

The debug log of ioredis shows me that it tries to connect to localhost first, though, and I'm getting connect ECONNREFUSED 127.0.0.1:6379:

{"level":"debug","message":"Chosen redis-sentinel config: { host: 10.82.102.71, port: 31691 }"}
  ioredis:redis status[localhost:6379]: [empty] -> connecting +0ms
  ioredis:redis status[10.82.102.71:31691]: [empty] -> connecting +2ms
  ioredis:redis queue command[10.82.102.71:31691]: 0 -> sentinel([ 'get-master-addr-by-name', 'redis-cluster' ]) +1ms
  ioredis:redis status[10.82.102.71:31691]: connecting -> connect +91ms
  ioredis:redis status[10.82.102.71:31691]: connect -> ready +1ms
  ioredis:connection send 1 commands in offline queue +0ms
  ioredis:redis write command[10.82.102.71:31691]: 0 -> sentinel([ 'get-master-addr-by-name', 'redis-cluster' ]) +1ms
  ioredis:redis write command[10.82.102.71:31691]: 0 -> sentinel([ 'sentinels', 'redis-cluster' ]) +21ms
  ioredis:SentinelConnector Updated internal sentinels: [{"host":"10.82.102.71","port":31691}] @1 +0ms
  ioredis:SentinelConnector resolved: 10.82.102.71:31084 +1ms
  ioredis:redis status[10.82.102.71:31084]: connecting -> connect +35ms
  ioredis:redis write command[10.82.102.71:31084]: 0 -> info([]) +1ms
  ioredis:redis status[10.82.102.71:31691]: ready -> close +1ms
  ioredis:connection skip reconnecting since the connection is manually closed. +59ms
  ioredis:redis status[10.82.102.71:31691]: close -> end +1ms
  ioredis:redis status[10.82.102.71:31084]: connect -> ready +20ms
  ioredis:redis status[127.0.0.1:6379]: [empty] -> connecting +6ms
  ioredis:redis queue command[127.0.0.1:6379]: 0 -> info([]) +1ms
  ioredis:connection error: Error: connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16) {
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 6379
} +32ms

I don't think it matters (commented it out) but later on, I'm using the ioredis connection in a bull queue

new Queue("cronJobQueue", {
            createClient: redisClient
        });

I've found the following 2 related issues but mine seems to be a little different: https://github.com/luin/ioredis/issues/796, https://github.com/luin/ioredis/issues/367

My SENTINEL sentinels redis-cluster output is:

1)  1) "name"
    2) "f01bb48b8f7e272bcc016e9511c7ff068bdb1b09"
    3) "ip"
    4) "10.82.98.49"
    5) "port"
    6) "31556"
    7) "runid"
    8) "f01bb48b8f7e272bcc016e9511c7ff068bdb1b09"
    9) "flags"
   10) "s_down,sentinel,disconnected"
   11) "link-pending-commands"
   12) "2"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "6326210"
   17) "last-ok-ping-reply"
   18) "6326834"
   19) "last-ping-reply"
   20) "6326834"
   21) "s-down-time"
   22) "6324616"
   23) "down-after-milliseconds"
   24) "1500"
   25) "last-hello-message"
   26) "6326795"
   27) "voted-leader"
   28) "?"
   29) "voted-leader-epoch"
   30) "1"
2)  1) "name"
    2) "8255b32b11d92e6bfa18e3312611610d3fd126b5"
    3) "ip"
    4) "10.82.98.85"
    5) "port"
    6) "31947"
    7) "runid"
    8) "8255b32b11d92e6bfa18e3312611610d3fd126b5"
    9) "flags"
   10) "s_down,sentinel,disconnected"
   11) "link-pending-commands"
   12) "2"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "6326210"
   17) "last-ok-ping-reply"
   18) "6326834"
   19) "last-ping-reply"
   20) "6326834"
   21) "s-down-time"
   22) "6324616"
   23) "down-after-milliseconds"
   24) "1500"
   25) "last-hello-message"
   26) "6326646"
   27) "voted-leader"
   28) "?"
   29) "voted-leader-epoch"
   30) "1"

And this is my get-master-addr-by-name output:

10.82.102.71:31691> SENTINEL get-master-addr-by-name redis-cluster
1) "10.82.102.71"
2) "31084"

Any help will be much appreciated!

Murena7 commented 4 years ago

I have the same problem, first two time - redis trying connect to 127.0.0.1:6379 and got error You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection: Error: connect ECONNREFUSED 127.0.0.1:6379 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) but third reconnect - ioredis set correct url what i set in config.

This only happen when i try connect between two docker containers, in local environment - all ok

lukeescude commented 4 months ago

This is happening to us as well. It seems ioredis is making the assumption that a redis server exists on localhost, when that is not the case and sentinel connection strings are being provided.

max-sym commented 3 months ago

@lukeescude how did you fix it? Having the same problem and it's super annoying...