Merge pull request #6639 from spacewander/use_last_for_ewma

Don't pick tried endpoint & count the latest in ewma balancer
This commit is contained in:
Kubernetes Prow Robot 2020-12-23 18:50:27 -08:00 committed by GitHub
commit b022ea8c40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 104 additions and 8 deletions

View file

@ -69,6 +69,18 @@ describe("Balancer ewma", function()
assert.are.equals(expected_ewma, ngx.shared.balancer_ewma:get(ngx.var.upstream_addr))
assert.are.equals(ngx_now, ngx.shared.balancer_ewma_last_touched_at:get(ngx.var.upstream_addr))
end)
it("updates EWMA stats with the latest result", function()
ngx.var = { upstream_addr = "10.10.10.1:8080, 10.10.10.2:8080", upstream_connect_time = "0.05, 0.02", upstream_response_time = "0.2, 0.1" }
instance:after_balance()
local weight = math.exp(-5 / 10)
local expected_ewma = 0.3 * weight + 0.12 * (1.0 - weight)
assert.are.equals(expected_ewma, ngx.shared.balancer_ewma:get("10.10.10.2:8080"))
assert.are.equals(ngx_now, ngx.shared.balancer_ewma_last_touched_at:get("10.10.10.2:8080"))
end)
end)
describe("balance()", function()
@ -94,8 +106,35 @@ describe("Balancer ewma", function()
-- even though 10.10.10.1:8080 has a lower ewma score
-- algorithm picks 10.10.10.3:8080 because its decayed score is even lower
assert.equal("10.10.10.3:8080", peer)
assert.equal(true, ngx.ctx.balancer_ewma_tried_endpoints["10.10.10.3:8080"])
assert.are.equals(0.16240233988393523723, ngx.var.balancer_ewma_score)
end)
it("doesn't pick the tried endpoint while retry", function()
local two_endpoints_backend = util.deepcopy(backend)
table.remove(two_endpoints_backend.endpoints, 2)
local two_endpoints_instance = balancer_ewma:new(two_endpoints_backend)
ngx.ctx.balancer_ewma_tried_endpoints = {
["10.10.10.3:8080"] = true,
}
local peer = two_endpoints_instance:balance()
assert.equal("10.10.10.1:8080", peer)
assert.equal(true, ngx.ctx.balancer_ewma_tried_endpoints["10.10.10.1:8080"])
end)
it("all the endpoints are tried, pick the one with lowest score", function()
local two_endpoints_backend = util.deepcopy(backend)
table.remove(two_endpoints_backend.endpoints, 2)
local two_endpoints_instance = balancer_ewma:new(two_endpoints_backend)
ngx.ctx.balancer_ewma_tried_endpoints = {
["10.10.10.1:8080"] = true,
["10.10.10.3:8080"] = true,
}
local peer = two_endpoints_instance:balance()
assert.equal("10.10.10.3:8080", peer)
end)
end)
describe("sync()", function()

View file

@ -0,0 +1,15 @@
local split = require("util.split")
describe("split", function()
it("get_last_value", function()
for _, case in ipairs({
{"127.0.0.1:26157 : 127.0.0.1:26158", "127.0.0.1:26158"},
{"127.0.0.1:26157, 127.0.0.1:26158", "127.0.0.1:26158"},
{"127.0.0.1:26158", "127.0.0.1:26158"},
}) do
local last = split.get_last_value(case[1])
assert.equal(case[2], last)
end
end)
end)