Skip to content

Commit

Permalink
feat(cluster) return peers on :refresh()
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Sep 28, 2016
1 parent 40fd870 commit 34f5f11
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/resty/cassandra/cluster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ local function set_peer(self, host, up, reconn_delay, unhealthy_at,
-- status
local ok, err = self.shm:set(host, up)
if not ok then
return nil, 'could not set host details in shm: '..err
return nil, 'could not set host status in shm: '..err
end

-- host health and info
Expand Down Expand Up @@ -404,6 +404,7 @@ end
-- called if further updates are required.
-- @treturn boolean `ok`: `true` if success, `nil` if failure.
-- @treturn string `err`: String describing the error if failure.
-- @treturn table `peers`: A list of peers retrieved from the cluster
function _Cluster:refresh()
local old_peers, err = get_peers(self)
if err then return nil, err
Expand Down Expand Up @@ -476,7 +477,7 @@ function _Cluster:refresh()

self.lb_policy:init(peers)
self.init = true
return true
return true, nil, peers
end

--------------------
Expand Down
46 changes: 40 additions & 6 deletions t/06-cluster.t
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,41 @@ info: no host details for 127.0.0.254
=== TEST 11: cluster.refresh() does not alter existing peers records and status
=== TEST 12: cluster.refresh() peers in 3rd return value
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local Cluster = require 'resty.cassandra.cluster'
local cluster, err = Cluster.new()
if not cluster then
ngx.log(ngx.ERR, err)
end
local ok, err, peers = cluster:refresh()
if not ok then
ngx.log(ngx.ERR, err)
return
end
for i = 1, #peers do
local p = peers[i]
ngx.say(p.host, ' ', p.up)
end
}
}
--- request
GET /t
--- response_body
127.0.0.3 true
127.0.0.2 true
127.0.0.1 true
--- no_error_log
[error]
=== TEST 13: cluster.refresh() does not alter existing peers records and status
--- http_config eval: $::HttpConfig
--- config
location /t {
Expand Down Expand Up @@ -712,7 +746,7 @@ after delay: true true
=== TEST 16: next_coordinator() uses load balancing policy
=== TEST 18: next_coordinator() uses load balancing policy
--- http_config eval: $::HttpConfig
--- config
location /t {
Expand Down Expand Up @@ -749,7 +783,7 @@ coordinator 3: 127.0.0.1
=== TEST 17: next_coordinator() returns no host available errors
=== TEST 19: next_coordinator() returns no host available errors
--- http_config eval: $::HttpConfig
--- config
location /t {
Expand Down Expand Up @@ -793,7 +827,7 @@ all hosts tried for query failed. 127.0.0.2: host still considered down. 127.0.0
=== TEST 18: next_coordinator() avoids down hosts
=== TEST 20: next_coordinator() avoids down hosts
--- http_config eval: $::HttpConfig
--- config
location /t {
Expand Down Expand Up @@ -837,7 +871,7 @@ GET /t
=== TEST 19: next_coordinator() marks nodes as down
=== TEST 21: next_coordinator() marks nodes as down
--- http_config eval: $::HttpConfig
--- config
location /t {
Expand Down Expand Up @@ -901,7 +935,7 @@ can try peer 255.255.255.253: false
=== TEST 20: next_coordinator() retries down host as per reconnection policy and ups them back
=== TEST 22: next_coordinator() retries down host as per reconnection policy and ups them back
--- http_config eval: $::HttpConfig
--- config
location /t {
Expand Down

0 comments on commit 34f5f11

Please sign in to comment.