Skip to content

Commit

Permalink
fix(lb) allow req-aware lb policies to run in init_by_lua* with resty…
Browse files Browse the repository at this point in the history
….core

resty.core makes `ngx.ctx` behave differently than the CFunction-based
implementation in ngx_lua. The latter returns `nil` in init_by_lua*, the
former throws an error.

From #117
  • Loading branch information
thibaultcha authored Jul 2, 2018
1 parent 43814cc commit 93cea38
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 10 deletions.
1 change: 0 additions & 1 deletion .ci/setup_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ if [ "$OPENRESTY_TESTS" = true ]; then
--without-http_coolkit_module \
--without-http_coolkit_module \
--without-lua_resty_dns \
--without-lua_resty_lrucache \
--without-lua_resty_upstream_healthcheck \
--without-lua_resty_websocket \
--without-lua_resty_upload \
Expand Down
9 changes: 8 additions & 1 deletion lib/resty/cassandra/policies/lb/req_dc_rr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

local _M = require('resty.cassandra.policies.lb').new_policy('req_and_dc_aware_round_robin')

local past_init

--- Create a request and DC-aware round robin policy.
-- Instanciates a request and DC-aware round robin policy for
-- `resty.cassandra.cluster`.
Expand Down Expand Up @@ -105,7 +107,12 @@ end
function _M:iter()
self.local_tried = 0
self.remote_tried = 0
self.ctx = ngx and ngx.ctx

if past_init or ngx.get_phase() ~= "init" then
self.ctx = ngx and ngx.ctx
past_init = true
end

self.initial_cassandra_coordinator = self.ctx and self.ctx.cassandra_coordinator
self.local_idx = (self.start_local_idx % #self.local_peers) + 1
self.remote_idx = (self.start_remote_idx % #self.remote_peers) + 1
Expand Down
9 changes: 7 additions & 2 deletions lib/resty/cassandra/policies/lb/req_rr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

local _rr_lb = require('resty.cassandra.policies.lb').new_policy('req_round_robin')

local past_init

--- Create a request-aware round robin policy.
-- Instanciates a request-aware round robin policy for `resty.cassandra.cluster`.
--
Expand Down Expand Up @@ -61,8 +63,11 @@ local function next_peer(state, i)
end

function _rr_lb:iter()
print()
self.ctx = ngx and ngx.ctx
if past_init or ngx.get_phase() ~= "init" then
self.ctx = ngx and ngx.ctx
past_init = true
end

self.initial_cassandra_coordinator = self.ctx and self.ctx.cassandra_coordinator
self.idx = (self.start_idx % #self.peers) + 1
self.start_idx = self.start_idx + 1
Expand Down
73 changes: 71 additions & 2 deletions t/14-lb_req_dc_rr.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use t::Util;

no_long_string();

our $HttpConfig = $t::Util::HttpConfig;

plan tests => repeat_each() * blocks() * 3 - 2;

run_tests();
Expand Down Expand Up @@ -367,3 +365,74 @@ local_dc: dc1
3. 127.0.0.3
--- no_error_log
[error]



=== TEST 8: lb_req_dc_rr can be used in init_by_lua* context with resty.core
--- http_config eval
qq{
$::LuaPackagePath
init_by_lua_block {
require "resty.core"
_G.res = {}
local req_dc_rr = require 'resty.cassandra.policies.lb.req_dc_rr'
table.insert(res, req_dc_rr.name)
local peers = {
{host = '10.0.0.1', data_center = 'dc2'},
{host = '127.0.0.1', data_center = 'dc1'},
{host = '127.0.0.2', data_center = 'dc1'},
{host = '127.0.0.3', data_center = 'dc1'},
{host = '10.0.0.2', data_center = 'dc2'},
{host = '10.0.0.3', data_center = 'dc2'}
}
local lb = req_dc_rr.new('dc1')
table.insert(res, 'local_dc: ' .. lb.local_dc)
lb:init(peers)
for i, peer in lb:iter() do
table.insert(res, "1. " .. peer.host)
if i == 1 then
break
end
end
for i, peer in lb:iter() do
table.insert(res, "2. " .. peer.host)
if i == 1 then
break
end
end
for i, peer in lb:iter() do
table.insert(res, "3. " .. peer.host)
if i == 1 then
break
end
end
}
}
--- config
location /t {
content_by_lua_block {
ngx.say(table.concat(res, "\n"))
}
}
--- request
GET /t
--- response_body
req_and_dc_aware_round_robin
local_dc: dc1
1. 127.0.0.1
2. 127.0.0.2
3. 127.0.0.3
--- no_error_log
[error]
64 changes: 62 additions & 2 deletions t/15-req_rr.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
use Test::Nginx::Socket::Lua;
use t::Util;

our $HttpConfig = $t::Util::HttpConfig;

no_long_string();

plan tests => repeat_each() * blocks() * 3;
Expand Down Expand Up @@ -213,3 +211,65 @@ req_round_robin
3. 127.0.0.3
--- no_error_log
[error]



=== TEST 5: req_rr can be used in init_by_lua* context with resty.core
--- http_config eval
qq{
$::LuaPackagePath
init_by_lua_block {
require "resty.core"
_G.res = {}
local lb_req_rr = require 'resty.cassandra.policies.lb.req_rr'
table.insert(res, lb_req_rr.name)
local peers = {
{host = '127.0.0.1'},
{host = '127.0.0.2'},
{host = '127.0.0.3'}
}
local lb = lb_req_rr.new()
lb:init(peers)
for i, peer in lb:iter() do
table.insert(res, '1. ' .. peer.host)
if i == 1 then
break
end
end
for i, peer in lb:iter() do
table.insert(res, '2. ' .. peer.host)
if i == 1 then
break
end
end
for i, peer in lb:iter() do
table.insert(res, '3. ' .. peer.host)
if i == 1 then
break
end
end
}
}
--- config
location /t {
content_by_lua_block {
ngx.say(table.concat(res, "\n"))
}
}
--- request
GET /t
--- response_body
req_round_robin
1. 127.0.0.1
2. 127.0.0.2
3. 127.0.0.3
--- no_error_log
[error]
13 changes: 11 additions & 2 deletions t/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package t::Util;
use strict;
use warnings;

use base 'Exporter';

my $TEST_COVERAGE_ENABLED = $ENV{TEST_COVERAGE_ENABLED};

my $LuaCovRunner = '';
Expand All @@ -15,12 +17,19 @@ $LuaCovRunner = <<_EOC_;
_EOC_
}

our @EXPORT = qw(
$LuaPackagePath
$HttpConfig
);

our $LuaPackagePath = "lua_package_path './lib/?.lua;./lib/?/init.lua;;';";

our $HttpConfig = <<_EOC_;
lua_package_path \'./lib/?.lua;./lib/?/init.lua;;\';
$LuaPackagePath
lua_shared_dict cassandra 1m;
init_by_lua_block {
$LuaCovRunner
$LuaCovRunner
}
_EOC_

Expand Down

0 comments on commit 93cea38

Please sign in to comment.