Skip to content

Commit c176a9c

Browse files
committed
fix(cluster) use 'rpc_address' to contact local peer
A follow up to 8b9fcd9, in which we used `listen_address` as the peer's addr instead of `rpc_address`. We also issue warning and implement fallbacks when `rpc_address` is set to all interfaces (`0.0.0.0`). Additionally, we handle the IPv6 unspecified address (`::`). From #122
1 parent 2fe82cc commit c176a9c

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

lib/resty/cassandra/cluster.lua

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ local _log_prefix = '[lua-cassandra] '
3232
local _rec_key = 'host:rec:'
3333
local _prepared_key = 'prepared:id:'
3434
local _protocol_version_key = 'protocol:version:'
35-
local _bind_all_address = '0.0.0.0'
3635

3736
local function get_now()
3837
return now() * 1000
@@ -409,7 +408,7 @@ local function first_coordinator(self)
409408
if not peer then
410409
errors[cp[i]] = err
411410
else
412-
return peer
411+
return peer, nil, cp[i]
413412
end
414413
end
415414

@@ -475,11 +474,11 @@ function _Cluster:refresh()
475474
if err then return nil, err
476475
elseif not peers then
477476
-- we are the first ones to get there
478-
local coordinator, err = first_coordinator(self)
477+
local coordinator, err, local_cp = first_coordinator(self)
479478
if not coordinator then return nil, err end
480479

481480
local local_rows, err = coordinator:execute [[
482-
SELECT data_center,listen_address,release_version FROM system.local
481+
SELECT data_center,rpc_address,release_version FROM system.local
483482
]]
484483
if not local_rows then return nil, err end
485484

@@ -492,8 +491,18 @@ function _Cluster:refresh()
492491

493492
coordinator:setkeepalive()
494493

494+
local local_addr = local_rows[1].rpc_address
495+
if local_addr == "0.0.0.0" or local_addr == "::" then
496+
log(WARN, _log_prefix, 'found contact point with \'', local_addr, '\' ',
497+
'as rpc_address, using \'', local_cp, '\' to ',
498+
'contact it instead. If this is incorrect ',
499+
'you should avoid using \'', local_addr, '\' ',
500+
'in rpc_address')
501+
local_addr = local_cp
502+
end
503+
495504
rows[#rows+1] = { -- local host
496-
rpc_address = local_rows[1].listen_address,
505+
rpc_address = local_addr,
497506
data_center = local_rows[1].data_center,
498507
release_version = local_rows[1].release_version
499508
}
@@ -506,12 +515,12 @@ function _Cluster:refresh()
506515
' in ', coordinator.host, '\'s peers system ',
507516
'table. ', row.peer, ' will be ignored.')
508517
else
509-
if host == _bind_all_address then
510-
log(WARN, _log_prefix, 'found host with 0.0.0.0 as rpc_address, ',
511-
'using listen_address ', row.peer, ' to ',
512-
'contact it instead. If this is ',
513-
'incorrect you should avoid using 0.0.0.0 ',
514-
'server-side.')
518+
if host == "0.0.0.0" or host == "::" then
519+
log(WARN, _log_prefix, 'found host with \'', host, '\' as ',
520+
'rpc_address, using \'', row.peer, '\' ',
521+
'to contact it instead. If this is ',
522+
'incorrect you should avoid using \'', host,
523+
'\' in rpc_address')
515524
host = row.peer
516525
end
517526

0 commit comments

Comments
 (0)