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

fix(balancer) avoid needless rebuild of balancer when adding target #3477

Merged
merged 1 commit into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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: 3 additions & 2 deletions kong/runloop/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,9 @@ local function check_target_history(upstream, balancer)
-- compare balancer history with db-loaded history
local last_equal_index = 0 -- last index where history is the same
for i, entry in ipairs(old_history) do
if entry.order ~= (new_history[i] or EMPTY_T).order then
last_equal_index = i - 1
if new_history[i] and entry.order == new_history[i].order then
last_equal_index = i
else
break
end
end
Expand Down
36 changes: 36 additions & 0 deletions spec/01-unit/011-balancer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe("Balancer", function()
local uuid = require("kong.tools.utils").uuid
local upstream_hc
local upstream_ph
local upstream_ote

teardown(function()
ngx.log:revert()
Expand Down Expand Up @@ -78,9 +79,11 @@ describe("Balancer", function()
[6] = { id = "f", name = "upstream_f", slots = 10, healthchecks = hc_defaults },
[7] = { id = "hc", name = "upstream_hc", slots = 10, healthchecks = passive_hc },
[8] = { id = "ph", name = "upstream_ph", slots = 10, healthchecks = passive_hc },
[9] = { id = "ote", name = "upstream_ote", slots = 10, healthchecks = hc_defaults },
}
upstream_hc = UPSTREAMS_FIXTURES[7]
upstream_ph = UPSTREAMS_FIXTURES[8]
upstream_ote = UPSTREAMS_FIXTURES[9]

TARGETS_FIXTURES = {
-- 1st upstream; a
Expand Down Expand Up @@ -187,6 +190,14 @@ describe("Balancer", function()
target = "127.0.0.1:2222",
weight = 10,
},
-- upstream_ote
{
id = "ote1",
created_at = "001",
upstream_id = "ote",
target = "localhost:1111",
weight = 10,
},
}

local function find_all_in_fixture_fn(fixture)
Expand Down Expand Up @@ -374,6 +385,31 @@ describe("Balancer", function()
end)
end)

describe("on_target_event()", function()
setup(function()
balancer._load_targets_into_memory("ote")
end)

it("adding a target does not recreate a balancer", function()
local b1 = balancer._create_balancer(upstream_ote)
assert.same(1, #(balancer._get_target_history(b1)))

table.insert(TARGETS_FIXTURES, {
id = "ote2",
created_at = "002",
upstream_id = "ote",
target = "localhost:1112",
weight = 10,
})
balancer.on_target_event("create", { upstream_id = "ote" })

local b2 = balancer._create_balancer(upstream_ote)
assert.same(2, #(balancer._get_target_history(b2)))

assert(b1 == b2)
end)
end)

describe("post_health()", function()
local hc, my_balancer

Expand Down