Skip to content

Commit

Permalink
Merge pull request #71 from thibaultcha/fix/opts-validation
Browse files Browse the repository at this point in the history
fix(cluster) options validation and retry reason in logging
  • Loading branch information
thibaultcha authored Oct 20, 2016
2 parents 2c1fbc2 + c749b5d commit 61571d5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
22 changes: 11 additions & 11 deletions lib/resty/cassandra/cluster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,13 @@ function _Cluster.new(opts)
if type(v) ~= 'table' then
return nil, 'contact_points must be a table'
end
elseif k == 'read_timeout' then
elseif k == 'timeout_read' then
if type(v) ~= 'number' then
return nil, 'read_timeout must be a number'
return nil, 'timeout_read must be a number'
end
elseif k == 'connect_timeout' then
elseif k == 'timeout_connect' then
if type(v) ~= 'number' then
return nil, 'connect_timeout must be a number'
return nil, 'timeout_connect must be a number'
end
elseif k == 'max_schema_consensus_wait' then
if type(v) ~= 'number' then
Expand Down Expand Up @@ -635,12 +635,13 @@ end

local send_request

function _Cluster:send_retry(request)
function _Cluster:send_retry(request, ...)
local coordinator, err = next_coordinator(self)
if not coordinator then return nil, err end

if self.logging then
log(NOTICE, _log_prefix, 'retrying request on host at ', coordinator.host)
log(NOTICE, _log_prefix, 'retrying request on host at ', coordinator.host,
' reason: ', ...)
end

request.retries = request.retries + 1
Expand All @@ -653,7 +654,7 @@ local function prepare_and_retry(self, coordinator, request)
-- prepared batch
if self.logging then
log(NOTICE, _log_prefix, 'some requests from this batch were not prepared on host ',
coordinator.host, ', preparing and retrying')
coordinator.host, ', preparing and retrying')
end
for i = 1, #request.queries do
local query_id, err = prepare(self, coordinator, request.queries[i][1])
Expand Down Expand Up @@ -697,18 +698,17 @@ local function handle_error(self, err, cql_code, coordinator, request)
end

if retry then
return self:send_retry(request)
return self:send_retry(request, 'CQL code: ', cql_code)
end
elseif err == 'timeout' then
if self.retry_on_timeout then
return self:send_retry(request)
return self:send_retry(request, 'timeout')
end
else
-- host seems down?
local ok, err = set_peer_down(self, coordinator.host)
if not ok then return nil, err end

return self:send_retry(request)
return self:send_retry(request, 'coordinator seems down')
end

return nil, err, cql_code
Expand Down
20 changes: 15 additions & 5 deletions t/06-cluster.t
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ GET /t
if not cluster then
ngx.say(err)
end

cluster, err = Cluster.new({timeout_read = 'foo'})
if not cluster then
ngx.say(err)
end

cluster, err = Cluster.new({timeout_connect = 'foo'})
if not cluster then
ngx.say(err)
end
}
}
--- request
Expand All @@ -62,6 +72,8 @@ opts must be a table
shm must be a string
no shared dict invalid_shm
keyspace must be a string
timeout_read must be a number
timeout_connect must be a number
--- no_error_log
[error]

Expand Down Expand Up @@ -1108,9 +1120,7 @@ can try peer 255.255.255.253: false
location /t {
content_by_lua_block {
local Cluster = require 'resty.cassandra.cluster'
local cluster, err = Cluster.new {
timeout_connect = 100
}
local cluster, err = Cluster.new()
if not cluster then
ngx.log(ngx.ERR, err)
return
Expand Down Expand Up @@ -1161,8 +1171,8 @@ can try peer 255.255.255.253: false
for i = 1, #peers do
local ok, err = cluster:can_try_peer(peers[i].host)
if err then
ngx.log(ngx.ERR, 'error in can_try_peer ', peers[i].host..': ', err)
return
ngx.log(ngx.ERR, 'error in can_try_peer ', peers[i].host..': ', err)
return
end
ngx.say(peers[i].host, ' is back up: ', ok)
end
Expand Down
2 changes: 1 addition & 1 deletion t/12-iterate.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ run_tests();
__DATA__

=== TEST 1: cluster.iterate() sanity
--- timeout: 30
--- timeout: 45
--- http_config eval
qq{
$::HttpConfig
Expand Down
2 changes: 1 addition & 1 deletion util/prove_ccm.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

CASSANDRA=${1}
CASSANDRA=${1:-3.9}

ccm stop
if [[ ! $(ccm list | grep lua_cassandra_prove) ]]; then
Expand Down

0 comments on commit 61571d5

Please sign in to comment.