Merge pull request #4514 from alexmaret/4475-stickyness-mode
Added new affinity mode for maximum session stickyness.
This commit is contained in:
commit
f6c2f5fb97
18 changed files with 572 additions and 138 deletions
|
|
@ -1,4 +1,5 @@
|
|||
local sticky = require("balancer.sticky")
|
||||
local sticky_balanced = require("balancer.sticky_balanced")
|
||||
local sticky_persistent = require("balancer.sticky_persistent")
|
||||
local cookie = require("resty.cookie")
|
||||
local util = require("util")
|
||||
|
||||
|
|
@ -15,11 +16,16 @@ local function reset_ngx()
|
|||
end
|
||||
|
||||
function get_mocked_cookie_new()
|
||||
local o = { value = nil }
|
||||
local mock = {
|
||||
get = function(self, n) return self.value end,
|
||||
set = function(self, c) self.value = c.value ; return true, nil end
|
||||
}
|
||||
setmetatable(o, mock)
|
||||
mock.__index = mock
|
||||
|
||||
return function(self)
|
||||
return {
|
||||
get = function(self, n) return nil, "error" end,
|
||||
set = function(self, n) return true, "" end
|
||||
}
|
||||
return o;
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -52,21 +58,27 @@ describe("Sticky", function()
|
|||
|
||||
describe("new(backend)", function()
|
||||
context("when backend specifies cookie name", function()
|
||||
it("returns an instance containing the corresponding cookie name", function()
|
||||
local function test(sticky)
|
||||
local sticky_balancer_instance = sticky:new(test_backend)
|
||||
local test_backend_cookie_name = test_backend.sessionAffinityConfig.cookieSessionAffinity.name
|
||||
assert.equal(sticky_balancer_instance:cookie_name(), test_backend_cookie_name)
|
||||
end)
|
||||
end
|
||||
|
||||
it("returns an instance containing the corresponding cookie name", function() test(sticky_balanced) end)
|
||||
it("returns an instance containing the corresponding cookie name", function() test(sticky_persistent) end)
|
||||
end)
|
||||
|
||||
context("when backend does not specify cookie name", function()
|
||||
it("returns an instance with 'route' as cookie name", function()
|
||||
local function test(sticky)
|
||||
local temp_backend = util.deepcopy(test_backend)
|
||||
temp_backend.sessionAffinityConfig.cookieSessionAffinity.name = nil
|
||||
local sticky_balancer_instance = sticky:new(temp_backend)
|
||||
local default_cookie_name = "route"
|
||||
assert.equal(sticky_balancer_instance:cookie_name(), default_cookie_name)
|
||||
end)
|
||||
end
|
||||
|
||||
it("returns an instance with 'route' as cookie name", function() test(sticky_balanced) end)
|
||||
it("returns an instance with 'route' as cookie name", function() test(sticky_persistent) end)
|
||||
end)
|
||||
end)
|
||||
|
||||
|
|
@ -74,8 +86,10 @@ describe("Sticky", function()
|
|||
local mocked_cookie_new = cookie.new
|
||||
|
||||
before_each(function()
|
||||
package.loaded["balancer.sticky"] = nil
|
||||
sticky = require("balancer.sticky")
|
||||
package.loaded["balancer.sticky_balanced"] = nil
|
||||
package.loaded["balancer.sticky_persistent"] = nil
|
||||
sticky_balanced = require("balancer.sticky_balanced")
|
||||
sticky_persistent = require("balancer.sticky_persistent")
|
||||
end)
|
||||
|
||||
after_each(function()
|
||||
|
|
@ -83,13 +97,17 @@ describe("Sticky", function()
|
|||
end)
|
||||
|
||||
context("when client doesn't have a cookie set and location is in cookie_locations", function()
|
||||
it("picks an endpoint for the client", function()
|
||||
|
||||
local function test_pick_endpoint(sticky)
|
||||
local sticky_balancer_instance = sticky:new(test_backend)
|
||||
local peer = sticky_balancer_instance:balance()
|
||||
assert.equal(peer, test_backend_endpoint)
|
||||
end)
|
||||
assert.equal(test_backend_endpoint, peer)
|
||||
end
|
||||
|
||||
it("sets a cookie on the client", function()
|
||||
it("picks an endpoint for the client", function() test_pick_endpoint(sticky_balanced) end)
|
||||
it("picks an endpoint for the client", function() test_pick_endpoint(sticky_persistent) end)
|
||||
|
||||
local function test_set_cookie(sticky)
|
||||
local s = {}
|
||||
cookie.new = function(self)
|
||||
local cookie_instance = {
|
||||
|
|
@ -112,9 +130,12 @@ describe("Sticky", function()
|
|||
local sticky_balancer_instance = sticky:new(b)
|
||||
assert.has_no.errors(function() sticky_balancer_instance:balance() end)
|
||||
assert.spy(s).was_called()
|
||||
end)
|
||||
end
|
||||
|
||||
it("sets a secure cookie on the client when being in ssl mode", function()
|
||||
it("sets a cookie on the client", function() test_set_cookie(sticky_balanced) end)
|
||||
it("sets a cookie on the client", function() test_set_cookie(sticky_persistent) end)
|
||||
|
||||
local function test_set_ssl_cookie(sticky)
|
||||
ngx.var.https = "on"
|
||||
local s = {}
|
||||
cookie.new = function(self)
|
||||
|
|
@ -138,10 +159,18 @@ describe("Sticky", function()
|
|||
local sticky_balancer_instance = sticky:new(b)
|
||||
assert.has_no.errors(function() sticky_balancer_instance:balance() end)
|
||||
assert.spy(s).was_called()
|
||||
end
|
||||
|
||||
it("sets a secure cookie on the client when being in ssl mode", function()
|
||||
test_set_ssl_cookie(sticky_balanced)
|
||||
end)
|
||||
it("sets a secure cookie on the client when being in ssl mode", function()
|
||||
test_set_ssl_cookie(sticky_persistent)
|
||||
end)
|
||||
end)
|
||||
|
||||
context("when client doesn't have a cookie set and cookie_locations contains a matching wildcard location", function()
|
||||
context("when client doesn't have a cookie set and cookie_locations contains a matching wildcard location",
|
||||
function()
|
||||
before_each(function ()
|
||||
ngx.var.host = "dev.test.com"
|
||||
end)
|
||||
|
|
@ -149,7 +178,7 @@ describe("Sticky", function()
|
|||
ngx.var.host = "test.com"
|
||||
end)
|
||||
|
||||
it("sets a cookie on the client", function()
|
||||
local function test(sticky)
|
||||
local s = {}
|
||||
cookie.new = function(self)
|
||||
local cookie_instance = {
|
||||
|
|
@ -173,17 +202,24 @@ describe("Sticky", function()
|
|||
local sticky_balancer_instance = sticky:new(b)
|
||||
assert.has_no.errors(function() sticky_balancer_instance:balance() end)
|
||||
assert.spy(s).was_called()
|
||||
end)
|
||||
end
|
||||
|
||||
it("sets a cookie on the client", function() test(sticky_balanced) end)
|
||||
it("sets a cookie on the client", function() test(sticky_persistent) end)
|
||||
end)
|
||||
|
||||
context("when client doesn't have a cookie set and location not in cookie_locations", function()
|
||||
it("picks an endpoint for the client", function()
|
||||
|
||||
local function test_pick_endpoint(sticky)
|
||||
local sticky_balancer_instance = sticky:new(test_backend)
|
||||
local peer = sticky_balancer_instance:balance()
|
||||
assert.equal(peer, test_backend_endpoint)
|
||||
end)
|
||||
end
|
||||
|
||||
it("does not set a cookie on the client", function()
|
||||
it("picks an endpoint for the client", function() test_pick_endpoint(sticky_balanced) end)
|
||||
it("picks an endpoint for the client", function() test_pick_endpoint(sticky_persistent) end)
|
||||
|
||||
local function test_no_cookie(sticky)
|
||||
local s = {}
|
||||
cookie.new = function(self)
|
||||
local cookie_instance = {
|
||||
|
|
@ -202,11 +238,15 @@ describe("Sticky", function()
|
|||
local sticky_balancer_instance = sticky:new(get_test_backend())
|
||||
assert.has_no.errors(function() sticky_balancer_instance:balance() end)
|
||||
assert.spy(s).was_not_called()
|
||||
end)
|
||||
end
|
||||
|
||||
it("does not set a cookie on the client", function() test_no_cookie(sticky_balanced) end)
|
||||
it("does not set a cookie on the client", function() test_no_cookie(sticky_persistent) end)
|
||||
end)
|
||||
|
||||
context("when client has a cookie set", function()
|
||||
it("does not set a cookie", function()
|
||||
|
||||
local function test_no_cookie(sticky)
|
||||
local s = {}
|
||||
cookie.new = function(self)
|
||||
local return_obj = {
|
||||
|
|
@ -219,13 +259,19 @@ describe("Sticky", function()
|
|||
local sticky_balancer_instance = sticky:new(test_backend)
|
||||
assert.has_no.errors(function() sticky_balancer_instance:balance() end)
|
||||
assert.spy(s).was_not_called()
|
||||
end)
|
||||
end
|
||||
|
||||
it("returns the correct endpoint for the client", function()
|
||||
it("does not set a cookie", function() test_no_cookie(sticky_balanced) end)
|
||||
it("does not set a cookie", function() test_no_cookie(sticky_persistent) end)
|
||||
|
||||
local function test_correct_endpoint(sticky)
|
||||
local sticky_balancer_instance = sticky:new(test_backend)
|
||||
local peer = sticky_balancer_instance:balance()
|
||||
assert.equal(peer, test_backend_endpoint)
|
||||
end)
|
||||
end
|
||||
|
||||
it("returns the correct endpoint for the client", function() test_correct_endpoint(sticky_balanced) end)
|
||||
it("returns the correct endpoint for the client", function() test_correct_endpoint(sticky_persistent) end)
|
||||
end)
|
||||
end)
|
||||
|
||||
|
|
@ -238,7 +284,12 @@ describe("Sticky", function()
|
|||
},
|
||||
sessionAffinityConfig = {
|
||||
name = "cookie",
|
||||
cookieSessionAffinity = { name = "test_name", hash = "sha1", change_on_failure = change_on_failure }
|
||||
cookieSessionAffinity = {
|
||||
name = "test_name",
|
||||
hash = "sha1",
|
||||
change_on_failure = change_on_failure,
|
||||
locations = { ['test.com'] = {'/'} }
|
||||
}
|
||||
},
|
||||
}
|
||||
end
|
||||
|
|
@ -247,53 +298,58 @@ describe("Sticky", function()
|
|||
local mocked_cookie_new = cookie.new
|
||||
|
||||
before_each(function()
|
||||
package.loaded["balancer.sticky"] = nil
|
||||
sticky = require("balancer.sticky")
|
||||
package.loaded["balancer.sticky_balanced"] = nil
|
||||
package.loaded["balancer.sticky_persistent"] = nil
|
||||
sticky_balanced = require("balancer.sticky_balanced")
|
||||
sticky_persistent = require("balancer.sticky_persistent")
|
||||
mock_ngx({ var = { location_path = "/", host = "test.com" } })
|
||||
end)
|
||||
|
||||
after_each(function()
|
||||
cookie.new = mocked_cookie_new
|
||||
reset_ngx()
|
||||
end)
|
||||
|
||||
context("when request to upstream fails", function()
|
||||
|
||||
local function test(sticky, change_on_failure)
|
||||
local sticky_balancer_instance = sticky:new(get_several_test_backends(change_on_failure))
|
||||
|
||||
local old_upstream = sticky_balancer_instance:balance()
|
||||
assert.is.Not.Nil(old_upstream)
|
||||
for _ = 1, 100 do
|
||||
-- make sure upstream doesn't change on subsequent calls of balance()
|
||||
assert.equal(old_upstream, sticky_balancer_instance:balance())
|
||||
end
|
||||
|
||||
-- simulate request failure
|
||||
sticky_balancer_instance.get_last_failure = function()
|
||||
return "failed"
|
||||
end
|
||||
_G.ngx.var.upstream_addr = old_upstream
|
||||
|
||||
for _ = 1, 100 do
|
||||
local new_upstream = sticky_balancer_instance:balance()
|
||||
if change_on_failure == false then
|
||||
-- upstream should be the same inspite of error, if change_on_failure option is false
|
||||
assert.equal(new_upstream, old_upstream)
|
||||
else
|
||||
-- upstream should change after error, if change_on_failure option is true
|
||||
assert.not_equal(new_upstream, old_upstream)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it("changes upstream when change_on_failure option is true", function()
|
||||
-- create sticky cookie
|
||||
cookie.new = function(self)
|
||||
local return_obj = {
|
||||
set = function(v) return false, nil end,
|
||||
get = function(k) return "" end,
|
||||
}
|
||||
return return_obj, false
|
||||
end
|
||||
|
||||
local options = {false, true}
|
||||
|
||||
for _, option in ipairs(options) do
|
||||
local sticky_balancer_instance = sticky:new(get_several_test_backends(option))
|
||||
|
||||
local old_upstream = sticky_balancer_instance:balance()
|
||||
for _ = 1, 100 do
|
||||
-- make sure upstream doesn't change on subsequent calls of balance()
|
||||
assert.equal(old_upstream, sticky_balancer_instance:balance())
|
||||
end
|
||||
|
||||
-- simulate request failure
|
||||
sticky_balancer_instance.get_last_failure = function()
|
||||
return "failed"
|
||||
end
|
||||
_G.ngx.var = { upstream_addr = old_upstream }
|
||||
|
||||
for _ = 1, 100 do
|
||||
local new_upstream = sticky_balancer_instance:balance()
|
||||
if option == false then
|
||||
-- upstream should be the same inspite of error, if change_on_failure option is false
|
||||
assert.equal(new_upstream, old_upstream)
|
||||
else
|
||||
-- upstream should change after error, if change_on_failure option is true
|
||||
assert.not_equal(new_upstream, old_upstream)
|
||||
end
|
||||
end
|
||||
end
|
||||
test(sticky_balanced, true)
|
||||
end)
|
||||
it("changes upstream when change_on_failure option is true", function()
|
||||
test(sticky_balanced, false)
|
||||
end)
|
||||
it("changes upstream when change_on_failure option is true", function()
|
||||
test(sticky_persistent, true)
|
||||
end)
|
||||
it("changes upstream when change_on_failure option is true", function()
|
||||
test(sticky_persistent, false)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
|
|
|||
|
|
@ -23,9 +23,9 @@ local function reset_expected_implementations()
|
|||
["access-router-production-web-80"] = package.loaded["balancer.round_robin"],
|
||||
["my-dummy-app-1"] = package.loaded["balancer.round_robin"],
|
||||
["my-dummy-app-2"] = package.loaded["balancer.chash"],
|
||||
["my-dummy-app-3"] = package.loaded["balancer.sticky"],
|
||||
["my-dummy-app-3"] = package.loaded["balancer.sticky_persistent"],
|
||||
["my-dummy-app-4"] = package.loaded["balancer.ewma"],
|
||||
["my-dummy-app-5"] = package.loaded["balancer.sticky"],
|
||||
["my-dummy-app-5"] = package.loaded["balancer.sticky_balanced"]
|
||||
}
|
||||
end
|
||||
|
||||
|
|
@ -55,7 +55,7 @@ local function reset_backends()
|
|||
},
|
||||
{
|
||||
name = "my-dummy-app-3", ["load-balance"] = "ewma",
|
||||
sessionAffinityConfig = { name = "cookie", cookieSessionAffinity = { name = "route" } }
|
||||
sessionAffinityConfig = { name = "cookie", mode = 'persistent', cookieSessionAffinity = { name = "route" } }
|
||||
},
|
||||
{ name = "my-dummy-app-4", ["load-balance"] = "ewma", },
|
||||
{
|
||||
|
|
|
|||
167
rootfs/etc/nginx/lua/test/util/nodemap_test.lua
Normal file
167
rootfs/etc/nginx/lua/test/util/nodemap_test.lua
Normal file
|
|
@ -0,0 +1,167 @@
|
|||
local util = require("util")
|
||||
local nodemap = require("util.nodemap")
|
||||
|
||||
local function get_test_backend_single()
|
||||
return {
|
||||
name = "access-router-production-web-80",
|
||||
endpoints = {
|
||||
{ address = "10.184.7.40", port = "8080", maxFails = 0, failTimeout = 0 }
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
local function get_test_backend_multi()
|
||||
return {
|
||||
name = "access-router-production-web-80",
|
||||
endpoints = {
|
||||
{ address = "10.184.7.40", port = "8080", maxFails = 0, failTimeout = 0 },
|
||||
{ address = "10.184.7.41", port = "8080", maxFails = 0, failTimeout = 0 }
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
local function get_test_nodes_ignore(endpoint)
|
||||
local ignore = {}
|
||||
ignore[endpoint] = true
|
||||
return ignore
|
||||
end
|
||||
|
||||
describe("Node Map", function()
|
||||
|
||||
local test_backend_single = get_test_backend_single()
|
||||
local test_backend_multi = get_test_backend_multi()
|
||||
local test_salt = test_backend_single.name
|
||||
local test_nodes_single = util.get_nodes(test_backend_single.endpoints)
|
||||
local test_nodes_multi = util.get_nodes(test_backend_multi.endpoints)
|
||||
local test_endpoint1 = test_backend_multi.endpoints[1].address .. ":" .. test_backend_multi.endpoints[1].port
|
||||
local test_endpoint2 = test_backend_multi.endpoints[2].address .. ":" .. test_backend_multi.endpoints[2].port
|
||||
local test_nodes_ignore = get_test_nodes_ignore(test_endpoint1)
|
||||
|
||||
describe("new()", function()
|
||||
context("when no salt has been provided", function()
|
||||
it("random() returns an unsalted key", function()
|
||||
local nodemap_instance = nodemap:new(test_nodes_single, nil)
|
||||
local expected_endpoint = test_endpoint1
|
||||
local expected_hash_key = ngx.md5(expected_endpoint)
|
||||
local actual_endpoint
|
||||
local actual_hash_key
|
||||
|
||||
actual_endpoint, actual_hash_key = nodemap_instance:random()
|
||||
|
||||
assert.equal(actual_endpoint, expected_endpoint)
|
||||
assert.equal(expected_hash_key, actual_hash_key)
|
||||
end)
|
||||
end)
|
||||
|
||||
context("when a salt has been provided", function()
|
||||
it("random() returns a salted key", function()
|
||||
local nodemap_instance = nodemap:new(test_nodes_single, test_salt)
|
||||
local expected_endpoint = test_endpoint1
|
||||
local expected_hash_key = ngx.md5(test_salt .. expected_endpoint)
|
||||
local actual_endpoint
|
||||
local actual_hash_key
|
||||
|
||||
actual_endpoint, actual_hash_key = nodemap_instance:random()
|
||||
|
||||
assert.equal(actual_endpoint, expected_endpoint)
|
||||
assert.equal(expected_hash_key, actual_hash_key)
|
||||
end)
|
||||
end)
|
||||
|
||||
context("when no nodes have been provided", function()
|
||||
it("random() returns nil", function()
|
||||
local nodemap_instance = nodemap:new({}, test_salt)
|
||||
local actual_endpoint
|
||||
local actual_hash_key
|
||||
|
||||
actual_endpoint, actual_hash_key = nodemap_instance:random()
|
||||
|
||||
assert.equal(actual_endpoint, nil)
|
||||
assert.equal(expected_hash_key, nil)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("find()", function()
|
||||
before_each(function()
|
||||
package.loaded["util.nodemap"] = nil
|
||||
nodemap = require("util.nodemap")
|
||||
end)
|
||||
|
||||
context("when a hash key is valid", function()
|
||||
it("find() returns the correct endpoint", function()
|
||||
local nodemap_instance = nodemap:new(test_nodes_single, test_salt)
|
||||
local test_hash_key
|
||||
local expected_endpoint
|
||||
local actual_endpoint
|
||||
|
||||
expected_endpoint, test_hash_key = nodemap_instance:random()
|
||||
assert.not_equal(expected_endpoint, nil)
|
||||
assert.not_equal(test_hash_key, nil)
|
||||
|
||||
actual_endpoint = nodemap_instance:find(test_hash_key)
|
||||
assert.equal(actual_endpoint, expected_endpoint)
|
||||
end)
|
||||
end)
|
||||
|
||||
context("when a hash key is invalid", function()
|
||||
it("find() returns nil", function()
|
||||
local nodemap_instance = nodemap:new(test_nodes_single, test_salt)
|
||||
local test_hash_key = "invalid or nonexistent hash key"
|
||||
local actual_endpoint
|
||||
|
||||
actual_endpoint = nodemap_instance:find(test_hash_key)
|
||||
|
||||
assert.equal(actual_endpoint, nil)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
|
||||
describe("random_except()", function()
|
||||
before_each(function()
|
||||
package.loaded["util.nodemap"] = nil
|
||||
nodemap = require("util.nodemap")
|
||||
end)
|
||||
|
||||
context("when nothing has been excluded", function()
|
||||
it("random_except() returns the correct endpoint", function()
|
||||
local nodemap_instance = nodemap:new(test_nodes_single, test_salt)
|
||||
local expected_endpoint = test_endpoint1
|
||||
local test_hash_key
|
||||
local actual_endpoint
|
||||
|
||||
actual_endpoint, test_hash_key = nodemap_instance:random_except({})
|
||||
assert.equal(expected_endpoint, actual_endpoint)
|
||||
assert.not_equal(test_hash_key, nil)
|
||||
end)
|
||||
end)
|
||||
|
||||
context("when everything has been excluded", function()
|
||||
it("random_except() returns nil", function()
|
||||
local nodemap_instance = nodemap:new(test_nodes_single, test_salt)
|
||||
local actual_hash_key
|
||||
local actual_endpoint
|
||||
|
||||
actual_endpoint, actual_hash_key = nodemap_instance:random_except(test_nodes_ignore)
|
||||
|
||||
assert.equal(actual_endpoint, nil)
|
||||
assert.equal(actual_hash_key, nil)
|
||||
end)
|
||||
end)
|
||||
|
||||
context("when an endpoint has been excluded", function()
|
||||
it("random_except() does not return it", function()
|
||||
local nodemap_instance = nodemap:new(test_nodes_multi, test_salt)
|
||||
local expected_endpoint = test_endpoint2
|
||||
local actual_endpoint
|
||||
local test_hash_key
|
||||
|
||||
actual_endpoint, test_hash_key = nodemap_instance:random_except(test_nodes_ignore)
|
||||
|
||||
assert.equal(actual_endpoint, expected_endpoint)
|
||||
assert.not_equal(test_hash_key, nil)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
Loading…
Add table
Add a link
Reference in a new issue