2018-05-18 21:36:43 +00:00
|
|
|
local balancer_resty = require("balancer.resty")
|
|
|
|
local resty_chash = require("resty.chash")
|
|
|
|
local util = require("util")
|
|
|
|
local ck = require("resty.cookie")
|
2019-03-04 15:34:48 +00:00
|
|
|
local math = require("math")
|
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
|
|
|
|
|
2018-05-18 21:36:43 +00:00
|
|
|
local _M = balancer_resty:new({ factory = resty_chash, 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
|
|
|
-- Consider the situation of N upstreams one of which is failing.
|
|
|
|
-- Then the probability to obtain failing upstream after M iterations would be close to (1/N)**M.
|
|
|
|
-- For the worst case (2 upstreams; 20 iterations) it would be ~10**(-6)
|
|
|
|
-- which is much better then ~10**(-3) for 10 iterations.
|
|
|
|
local MAX_UPSTREAM_CHECKS_COUNT = 20
|
|
|
|
|
2019-02-19 22:27:08 +00:00
|
|
|
function _M.cookie_name(self)
|
|
|
|
return self.cookie_session_affinity.name or DEFAULT_COOKIE_NAME
|
|
|
|
end
|
|
|
|
|
|
|
|
function _M.new(self, backend)
|
|
|
|
local nodes = util.get_nodes(backend.endpoints)
|
2018-05-18 21:36:43 +00:00
|
|
|
|
|
|
|
local o = {
|
|
|
|
instance = self.factory:new(nodes),
|
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
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
|
|
|
local function set_cookie(self, value)
|
|
|
|
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-06-06 20:40:59 +00:00
|
|
|
local function pick_new_upstream(self)
|
|
|
|
local failed_upstreams = get_failed_upstreams()
|
|
|
|
|
2019-06-06 16:51:38 +00:00
|
|
|
for i = 1, MAX_UPSTREAM_CHECKS_COUNT do
|
|
|
|
local key = string.format("%s.%s.%s", ngx.now() + i, ngx.worker.pid(), math.random(999999))
|
|
|
|
|
|
|
|
local new_upstream = self.instance:find(key)
|
|
|
|
|
2019-06-06 20:40:59 +00:00
|
|
|
if not failed_upstreams[new_upstream] then
|
|
|
|
return new_upstream, key
|
2019-06-06 16:51:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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)
|
|
|
|
if self.cookie_session_affinity.locations then
|
|
|
|
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
|
|
|
|
local wildcardHostname, _, err = ngx.re.sub(ngx.var.host, "^[^\\.]+\\.", "*.", "jo")
|
|
|
|
if err then
|
|
|
|
ngx.log(ngx.ERR, "error: ", err);
|
|
|
|
elseif wildcardHostname then
|
|
|
|
locs = self.cookie_session_affinity.locations[wildcardHostname]
|
|
|
|
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)
|
2018-05-18 21:36:43 +00:00
|
|
|
local cookie, err = ck:new()
|
|
|
|
if not cookie then
|
2018-10-30 12:23:08 +00:00
|
|
|
ngx.log(ngx.ERR, "error while initializing cookie: " .. tostring(err))
|
|
|
|
return
|
2018-05-18 21:36:43 +00:00
|
|
|
end
|
|
|
|
|
2019-06-06 16:51:38 +00:00
|
|
|
local upstream_from_cookie
|
2019-04-29 13:20:30 +00:00
|
|
|
|
2019-02-19 22:27:08 +00:00
|
|
|
local key = cookie:get(self:cookie_name())
|
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
|
|
|
|
|
|
|
|
new_upstream, key = pick_new_upstream(self)
|
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
|
|
|
|
set_cookie(self, 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)
|
|
|
|
balancer_resty.sync(self, backend)
|
|
|
|
|
|
|
|
-- Reload the balancer if any of the annotations have changed.
|
|
|
|
local changed = not util.deep_compare(
|
|
|
|
self.cookie_session_affinity,
|
|
|
|
backend.sessionAffinityConfig.cookieSessionAffinity
|
|
|
|
)
|
|
|
|
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
|