Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Round Robin with optional random start #26

Merged
merged 2 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Both `resty.chash` and `resty.roundrobin` have the same apis.

new
---
**syntax:** `obj, err = class.new(nodes)`
**syntax:** `obj, err = class.new(nodes, random_start?)`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can always make it start randomly.


Instantiates an object of this class. The `class` value is returned by the call `require "resty.chash"`.

Expand All @@ -135,6 +135,9 @@ when we need to keep consistency with nginx chash.

The `id` can be any string value when we do not need to keep consistency with nginx chash.

`random_start` can optionally be set when initializing `resty.roundrobin`. If it is set then
the algorithm will pick first node uniformly at random instead of starting with the first one.

```lua
local nodes = {
-- id => weight
Expand Down
31 changes: 29 additions & 2 deletions lib/resty/roundrobin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local pairs = pairs
local next = next
local tonumber = tonumber
local setmetatable = setmetatable
local math_random = math.random


local _M = {}
Expand Down Expand Up @@ -46,18 +47,40 @@ local function get_gcd(nodes)
return only_key, gcd, max_weight
end

local function get_random_node_id(nodes)
local count = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: need four space as indent.

for _, _ in pairs(nodes) do
count = count + 1
end

function _M.new(_, nodes)
local id = nil
local random_index = math_random(count)

for _ = 1, random_index do
id = next(nodes, id)
end

return id
end


function _M.new(_, nodes, random_start)
local newnodes = copy(nodes)
local only_key, gcd, max_weight = get_gcd(newnodes)
local last_id = nil

if random_start then
last_id = get_random_node_id(nodes)
end

local self = {
nodes = newnodes, -- it's safer to copy one
only_key = only_key,
max_weight = max_weight,
gcd = gcd,
cw = max_weight,
last_id = nil,
last_id = last_id,
random_start = random_start,
}
return setmetatable(self, mt)
end
Expand All @@ -70,6 +93,10 @@ function _M.reinit(self, nodes)
self.nodes = newnodes
self.last_id = nil
self.cw = self.max_weight

if self.random_start then
self.last_id = get_random_node_id(nodes)
end
end


Expand Down
73 changes: 73 additions & 0 deletions t/roundrobin.t
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,76 @@ server3: 16666
server2: 33333
--- no_error_log
[error]



=== TEST 3: new with random start
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
math.randomseed(75098)

local roundrobin = require "resty.roundrobin"

local servers = {
["server1"] = 1,
["server2"] = 1,
["server3"] = 1,
}

local rr = roundrobin:new(servers, true)
local id = rr:find()
ngx.say(id)
}
}
--- request
GET /t
--- response_body
server3
--- no_error_log
[error]



=== TEST 4: reinit with random start
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
math.randomseed(75098)

local roundrobin = require "resty.roundrobin"

local servers = {
["server1"] = 1,
["server2"] = 1,
["server3"] = 1,
}

local rr = roundrobin:new(servers, true)
local id = rr:find()
ngx.say(id)

math.randomseed(99111)

local new_servers = {
["server1"] = 1,
["server2"] = 1,
["server3"] = 1,
["server4"] = 1,
["server5"] = 1,
}

rr:reinit(new_servers)
id = rr:find()
ngx.say(id)
}
}
--- request
GET /t
--- response_body
server3
server5
--- no_error_log
[error]