2019-08-30 09:40:29 +00:00
|
|
|
local affinity_balanced = require("affinity.balanced")
|
|
|
|
local affinity_persistent = require("affinity.persistent")
|
2018-05-18 21:36:43 +00:00
|
|
|
local balancer_resty = require("balancer.resty")
|
|
|
|
local util = require("util")
|
|
|
|
local ck = require("resty.cookie")
|
2019-04-29 13:20:30 +00:00
|
|
|
local ngx_balancer = require("ngx.balancer")
|
|
|
|
local split = require("util.split")
|
2018-05-18 21:36:43 +00:00
|
|
|
|
2019-05-26 03:50:18 +00:00
|
|
|
local string_format = string.format
|
|
|
|
local ngx_log = ngx.log
|
|
|
|
local INFO = ngx.INFO
|
|
|
|
|
2019-08-30 09:40:29 +00:00
|
|
|
local _M = balancer_resty:new({ name = "sticky" })
|
2019-02-19 22:27:08 +00:00
|
|
|
local DEFAULT_COOKIE_NAME = "route"
|
2018-05-18 21:36:43 +00:00
|
|
|
|
2019-04-29 13:20:30 +00:00
|
|
|
|
2019-02-19 22:27:08 +00:00
|
|
|
function _M.cookie_name(self)
|
|
|
|
return self.cookie_session_affinity.name or DEFAULT_COOKIE_NAME
|
|
|
|
end
|
|
|
|
|
2019-08-30 09:40:29 +00:00
|
|
|
local function init_affinity_mode(self, backend)
|
|
|
|
local mode = backend["sessionAffinityConfig"]["mode"] or 'balanced'
|
|
|
|
|
|
|
|
-- set default mode to 'balanced' for backwards compatibility
|
|
|
|
if mode == nil or mode == '' then
|
|
|
|
mode = 'balanced'
|
|
|
|
end
|
2018-05-18 21:36:43 +00:00
|
|
|
|
2019-08-30 09:40:29 +00:00
|
|
|
self.affinity_mode = mode
|
|
|
|
|
|
|
|
if mode == 'persistent' then
|
|
|
|
return affinity_persistent:new(self, backend)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- default is 'balanced' for backwards compatibility
|
|
|
|
if mode ~= 'balanced' then
|
|
|
|
ngx.log(ngx.WARN, string.format("Invalid affinity mode '%s'! Using 'balanced' as a default.", mode))
|
|
|
|
end
|
|
|
|
|
|
|
|
return affinity_balanced:new(self, backend)
|
|
|
|
end
|
|
|
|
|
|
|
|
function _M.new(self, backend)
|
2018-05-18 21:36:43 +00:00
|
|
|
local o = {
|
2019-08-30 09:40:29 +00:00
|
|
|
instance = nil,
|
|
|
|
affinity_mode = nil,
|
2018-11-13 11:44:57 +00:00
|
|
|
traffic_shaping_policy = backend.trafficShapingPolicy,
|
|
|
|
alternative_backends = backend.alternativeBackends,
|
2019-02-19 22:27:08 +00:00
|
|
|
cookie_session_affinity = backend["sessionAffinityConfig"]["cookieSessionAffinity"]
|
2018-05-18 21:36:43 +00:00
|
|
|
}
|
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
2019-08-30 09:40:29 +00:00
|
|
|
|
|
|
|
return init_affinity_mode(o, backend)
|
|
|
|
end
|
|
|
|
|
|
|
|
function _M.get_cookie(self)
|
|
|
|
local cookie, err = ck:new()
|
|
|
|
if not cookie then
|
|
|
|
ngx.log(ngx.ERR, err)
|
|
|
|
end
|
|
|
|
|
|
|
|
return cookie:get(self:cookie_name())
|
2018-05-18 21:36:43 +00:00
|
|
|
end
|
|
|
|
|
2019-08-30 09:40:29 +00:00
|
|
|
function _M.set_cookie(self, value)
|
2018-05-18 21:36:43 +00:00
|
|
|
local cookie, err = ck:new()
|
|
|
|
if not cookie then
|
|
|
|
ngx.log(ngx.ERR, err)
|
|
|
|
end
|
|
|
|
|
2019-02-19 22:27:08 +00:00
|
|
|
local cookie_path = self.cookie_session_affinity.path
|
2018-11-19 14:15:24 +00:00
|
|
|
if not cookie_path then
|
|
|
|
cookie_path = ngx.var.location_path
|
|
|
|
end
|
|
|
|
|
2018-10-29 07:21:10 +00:00
|
|
|
local cookie_data = {
|
2019-02-19 22:27:08 +00:00
|
|
|
key = self:cookie_name(),
|
2018-05-18 21:36:43 +00:00
|
|
|
value = value,
|
2018-11-19 14:15:24 +00:00
|
|
|
path = cookie_path,
|
2018-05-18 21:36:43 +00:00
|
|
|
httponly = true,
|
2018-12-04 09:51:52 +00:00
|
|
|
secure = ngx.var.https == "on",
|
2018-10-29 07:21:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 22:27:08 +00:00
|
|
|
if self.cookie_session_affinity.expires and self.cookie_session_affinity.expires ~= "" then
|
|
|
|
cookie_data.expires = ngx.cookie_time(ngx.time() + tonumber(self.cookie_session_affinity.expires))
|
2018-10-29 07:21:10 +00:00
|
|
|
end
|
|
|
|
|
2019-02-19 22:27:08 +00:00
|
|
|
if self.cookie_session_affinity.maxage and self.cookie_session_affinity.maxage ~= "" then
|
|
|
|
cookie_data.max_age = tonumber(self.cookie_session_affinity.maxage)
|
2018-10-29 07:21:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local ok
|
|
|
|
ok, err = cookie:set(cookie_data)
|
2018-05-18 21:36:43 +00:00
|
|
|
if not ok then
|
|
|
|
ngx.log(ngx.ERR, err)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-29 13:20:30 +00:00
|
|
|
function _M.get_last_failure()
|
|
|
|
return ngx_balancer.get_last_failure()
|
|
|
|
end
|
|
|
|
|
2019-06-06 20:40:59 +00:00
|
|
|
local function get_failed_upstreams()
|
|
|
|
local indexed_upstream_addrs = {}
|
|
|
|
local upstream_addrs = split.split_upstream_var(ngx.var.upstream_addr) or {}
|
|
|
|
|
|
|
|
for _, addr in ipairs(upstream_addrs) do
|
|
|
|
indexed_upstream_addrs[addr] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
return indexed_upstream_addrs
|
2019-06-06 16:51:38 +00:00
|
|
|
end
|
|
|
|
|
2019-08-30 09:40:29 +00:00
|
|
|
--- get_routing_key gets the current routing key from the cookie
|
|
|
|
-- @treturn string, string The routing key and an error message if an error occured.
|
|
|
|
function _M.get_routing_key(self)
|
|
|
|
-- interface method to get the routing key from the cookie
|
|
|
|
-- has to be overridden by an affinity mode
|
|
|
|
ngx.log(ngx.ERR, "[BUG] Failed to get routing key as no implementation has been provided!")
|
|
|
|
return nil, nil
|
|
|
|
end
|
2019-06-06 16:51:38 +00:00
|
|
|
|
2019-08-30 09:40:29 +00:00
|
|
|
--- set_routing_key sets the current routing key on the cookie
|
|
|
|
-- @tparam string key The routing key to set on the cookie.
|
|
|
|
function _M.set_routing_key(self, key)
|
|
|
|
-- interface method to set the routing key on the cookie
|
|
|
|
-- has to be overridden by an affinity mode
|
|
|
|
ngx.log(ngx.ERR, "[BUG] Failed to set routing key as no implementation has been provided!")
|
|
|
|
end
|
2019-06-06 16:51:38 +00:00
|
|
|
|
2019-08-30 09:40:29 +00:00
|
|
|
--- pick_new_upstream picks a new upstream while ignoring the given failed upstreams.
|
|
|
|
-- @tparam {[string]=boolean} A table of upstreams to ignore where the key is the endpoint and the value a boolean.
|
|
|
|
-- @treturn string, string The endpoint and its key.
|
|
|
|
function _M.pick_new_upstream(self, failed_upstreams)
|
|
|
|
-- interface method to get a new upstream
|
|
|
|
-- has to be overridden by an affinity mode
|
|
|
|
ngx.log(ngx.ERR, "[BUG] Failed to pick new upstream as no implementation has been provided!")
|
2019-06-06 20:40:59 +00:00
|
|
|
return nil, nil
|
2019-06-06 16:51:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function should_set_cookie(self)
|
2019-08-26 15:22:07 +00:00
|
|
|
if self.cookie_session_affinity.locations and ngx.var.host then
|
2019-06-06 16:51:38 +00:00
|
|
|
local locs = self.cookie_session_affinity.locations[ngx.var.host]
|
2019-08-21 23:17:42 +00:00
|
|
|
if locs == nil then
|
|
|
|
-- Based off of wildcard hostname in ../certificate.lua
|
2019-08-26 15:22:07 +00:00
|
|
|
local wildcard_host, _, err = ngx.re.sub(ngx.var.host, "^[^\\.]+\\.", "*.", "jo")
|
2019-08-21 23:17:42 +00:00
|
|
|
if err then
|
|
|
|
ngx.log(ngx.ERR, "error: ", err);
|
2019-08-26 15:22:07 +00:00
|
|
|
elseif wildcard_host then
|
|
|
|
locs = self.cookie_session_affinity.locations[wildcard_host]
|
2019-08-21 23:17:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-06 16:51:38 +00:00
|
|
|
if locs ~= nil then
|
|
|
|
for _, path in pairs(locs) do
|
|
|
|
if ngx.var.location_path == path then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2018-05-30 21:14:28 +00:00
|
|
|
function _M.balance(self)
|
2019-06-06 16:51:38 +00:00
|
|
|
local upstream_from_cookie
|
2019-04-29 13:20:30 +00:00
|
|
|
|
2019-08-30 09:40:29 +00:00
|
|
|
local key = self:get_routing_key()
|
2019-04-29 13:20:30 +00:00
|
|
|
if key then
|
|
|
|
upstream_from_cookie = self.instance:find(key)
|
|
|
|
end
|
|
|
|
|
2019-06-06 16:51:38 +00:00
|
|
|
local last_failure = self.get_last_failure()
|
2019-07-08 17:51:24 +00:00
|
|
|
local should_pick_new_upstream = last_failure ~= nil and self.cookie_session_affinity.change_on_failure or
|
|
|
|
upstream_from_cookie == nil
|
2019-04-29 13:20:30 +00:00
|
|
|
|
2019-06-06 16:51:38 +00:00
|
|
|
if not should_pick_new_upstream then
|
|
|
|
return upstream_from_cookie
|
2019-04-29 13:20:30 +00:00
|
|
|
end
|
|
|
|
|
2019-07-08 17:51:24 +00:00
|
|
|
local new_upstream
|
|
|
|
|
2019-08-30 09:40:29 +00:00
|
|
|
new_upstream, key = self:pick_new_upstream(get_failed_upstreams())
|
2019-06-06 16:51:38 +00:00
|
|
|
if not new_upstream then
|
|
|
|
ngx.log(ngx.WARN, string.format("failed to get new upstream; using upstream %s", new_upstream))
|
|
|
|
elseif should_set_cookie(self) then
|
2019-08-30 09:40:29 +00:00
|
|
|
self:set_routing_key(key)
|
2018-05-18 21:36:43 +00:00
|
|
|
end
|
|
|
|
|
2019-04-29 13:20:30 +00:00
|
|
|
return new_upstream
|
2018-05-18 21:36:43 +00:00
|
|
|
end
|
|
|
|
|
2019-02-19 22:27:08 +00:00
|
|
|
function _M.sync(self, backend)
|
2019-08-30 09:40:29 +00:00
|
|
|
local changed = false
|
|
|
|
|
|
|
|
-- check and reinit affinity mode before syncing the balancer which will reinit the nodes
|
|
|
|
if self.affinity_mode ~= backend.sessionAffinityConfig.mode then
|
|
|
|
changed = true
|
|
|
|
init_affinity_mode(self, backend)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- reload balancer nodes
|
2019-02-19 22:27:08 +00:00
|
|
|
balancer_resty.sync(self, backend)
|
|
|
|
|
|
|
|
-- Reload the balancer if any of the annotations have changed.
|
2019-08-30 09:40:29 +00:00
|
|
|
changed = changed or not util.deep_compare(
|
2019-02-19 22:27:08 +00:00
|
|
|
self.cookie_session_affinity,
|
|
|
|
backend.sessionAffinityConfig.cookieSessionAffinity
|
|
|
|
)
|
2019-08-30 09:40:29 +00:00
|
|
|
|
2019-02-19 22:27:08 +00:00
|
|
|
if not changed then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-05-26 03:50:18 +00:00
|
|
|
ngx_log(INFO, string_format("[%s] nodes have changed for backend %s", self.name, backend.name))
|
|
|
|
|
2019-02-19 22:27:08 +00:00
|
|
|
self.cookie_session_affinity = backend.sessionAffinityConfig.cookieSessionAffinity
|
|
|
|
end
|
|
|
|
|
2018-05-18 21:36:43 +00:00
|
|
|
return _M
|