Skip to content

Commit

Permalink
fix(cluster) use 'rpc_address' to contact local peer
Browse files Browse the repository at this point in the history
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
  • Loading branch information
thibaultcha authored Nov 9, 2018
1 parent 2fe82cc commit 7bdcd17
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions lib/resty/cassandra/cluster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ local _log_prefix = '[lua-cassandra] '
local _rec_key = 'host:rec:'
local _prepared_key = 'prepared:id:'
local _protocol_version_key = 'protocol:version:'
local _bind_all_address = '0.0.0.0'

local function get_now()
return now() * 1000
Expand Down Expand Up @@ -409,7 +408,7 @@ local function first_coordinator(self)
if not peer then
errors[cp[i]] = err
else
return peer
return peer, nil, cp[i]
end
end

Expand Down Expand Up @@ -475,11 +474,11 @@ function _Cluster:refresh()
if err then return nil, err
elseif not peers then
-- we are the first ones to get there
local coordinator, err = first_coordinator(self)
local coordinator, err, local_cp = first_coordinator(self)
if not coordinator then return nil, err end

local local_rows, err = coordinator:execute [[
SELECT data_center,listen_address,release_version FROM system.local
SELECT data_center,rpc_address,release_version FROM system.local
]]
if not local_rows then return nil, err end

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

coordinator:setkeepalive()

local local_addr = local_rows[1].rpc_address
if local_addr == "0.0.0.0" or local_addr == "::" then
log(WARN, _log_prefix, 'found contact point with \'', local_addr, '\' ',
'as rpc_address, using \'', local_cp, '\' to ',
'contact it instead. If this is incorrect ',
'you should avoid using \'', local_cp, '\' ',
'in rpc_address')
local_addr = local_cp
end

rows[#rows+1] = { -- local host
rpc_address = local_rows[1].listen_address,
rpc_address = local_addr,
data_center = local_rows[1].data_center,
release_version = local_rows[1].release_version
}
Expand All @@ -506,12 +515,12 @@ function _Cluster:refresh()
' in ', coordinator.host, '\'s peers system ',
'table. ', row.peer, ' will be ignored.')
else
if host == _bind_all_address then
log(WARN, _log_prefix, 'found host with 0.0.0.0 as rpc_address, ',
'using listen_address ', row.peer, ' to ',
'contact it instead. If this is ',
'incorrect you should avoid using 0.0.0.0 ',
'server-side.')
if host == "0.0.0.0" or host == "::" then
log(WARN, _log_prefix, 'found host with \'', host, '\' as ',
'rpc_address, using \'', row.peer, '\' ',
'to contact it instead. If this is ',
'incorrect you should avoid using \'', host,
'\' in rpc_address')
host = row.peer
end

Expand Down

0 comments on commit 7bdcd17

Please sign in to comment.