2018-03-18 13:13:41 +00:00
|
|
|
local ngx_balancer = require("ngx.balancer")
|
|
|
|
local json = require("cjson")
|
|
|
|
local configuration = require("configuration")
|
2018-05-18 21:36:43 +00:00
|
|
|
local round_robin = require("balancer.round_robin")
|
|
|
|
local chash = require("balancer.chash")
|
|
|
|
local sticky = require("balancer.sticky")
|
2018-03-23 15:06:21 +00:00
|
|
|
local ewma = require("balancer.ewma")
|
2018-03-18 13:13:41 +00:00
|
|
|
|
|
|
|
-- measured in seconds
|
2018-03-18 16:31:49 +00:00
|
|
|
-- for an Nginx worker to pick up the new list of upstream peers
|
2018-03-18 13:13:41 +00:00
|
|
|
-- it will take <the delay until controller POSTed the backend object to the Nginx endpoint> + BACKENDS_SYNC_INTERVAL
|
|
|
|
local BACKENDS_SYNC_INTERVAL = 1
|
|
|
|
|
2018-03-23 15:06:21 +00:00
|
|
|
local DEFAULT_LB_ALG = "round_robin"
|
2018-05-18 21:36:43 +00:00
|
|
|
local IMPLEMENTATIONS = {
|
|
|
|
round_robin = round_robin,
|
|
|
|
chash = chash,
|
|
|
|
sticky = sticky,
|
|
|
|
ewma = ewma,
|
|
|
|
}
|
2018-03-18 13:13:41 +00:00
|
|
|
|
|
|
|
local _M = {}
|
2018-05-18 21:36:43 +00:00
|
|
|
local balancers = {}
|
2018-03-18 13:13:41 +00:00
|
|
|
|
2018-05-18 21:36:43 +00:00
|
|
|
local function get_implementation(backend)
|
|
|
|
local name = backend["load-balance"] or DEFAULT_LB_ALG
|
2018-03-30 17:19:33 +00:00
|
|
|
|
2018-05-18 21:36:43 +00:00
|
|
|
if backend["sessionAffinityConfig"] and backend["sessionAffinityConfig"]["name"] == "cookie" then
|
|
|
|
name = "sticky"
|
|
|
|
elseif backend["upstream-hash-by"] then
|
|
|
|
name = "chash"
|
2018-04-08 20:37:13 +00:00
|
|
|
end
|
|
|
|
|
2018-05-18 21:36:43 +00:00
|
|
|
local implementation = IMPLEMENTATIONS[name]
|
|
|
|
if not implementation then
|
|
|
|
ngx.log(ngx.WARN, string.format("%s is not supported, falling back to %s", backend["load-balance"], DEFAULT_LB_ALG))
|
|
|
|
implementation = IMPLEMENTATIONS[DEFAULT_LB_ALG]
|
2018-05-09 15:26:04 +00:00
|
|
|
end
|
|
|
|
|
2018-05-18 21:36:43 +00:00
|
|
|
return implementation
|
2018-03-18 13:13:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function sync_backend(backend)
|
2018-05-18 21:36:43 +00:00
|
|
|
local implementation = get_implementation(backend)
|
|
|
|
local balancer = balancers[backend.name]
|
2018-03-18 13:13:41 +00:00
|
|
|
|
2018-05-09 15:26:04 +00:00
|
|
|
if not balancer then
|
2018-05-18 21:36:43 +00:00
|
|
|
balancers[backend.name] = implementation:new(backend)
|
2018-05-09 15:26:04 +00:00
|
|
|
return
|
2018-03-23 15:06:21 +00:00
|
|
|
end
|
2018-05-18 21:36:43 +00:00
|
|
|
|
|
|
|
if getmetatable(balancer) ~= implementation then
|
|
|
|
ngx.log(ngx.INFO,
|
|
|
|
string.format("LB algorithm changed from %s to %s, resetting the instance", balancer.name, implementation.name))
|
|
|
|
balancers[backend.name] = implementation:new(backend)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
balancer:sync(backend)
|
2018-03-18 13:13:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function sync_backends()
|
|
|
|
local backends_data = configuration.get_backends_data()
|
|
|
|
if not backends_data then
|
2018-05-26 01:25:41 +00:00
|
|
|
balancers = {}
|
2018-03-18 13:13:41 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local ok, new_backends = pcall(json.decode, backends_data)
|
|
|
|
if not ok then
|
|
|
|
ngx.log(ngx.ERR, "could not parse backends data: " .. tostring(new_backends))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-05-26 01:25:41 +00:00
|
|
|
local balancers_to_keep = {}
|
|
|
|
for _, new_backend in ipairs(new_backends) do
|
2018-05-18 21:36:43 +00:00
|
|
|
sync_backend(new_backend)
|
2018-05-26 01:25:41 +00:00
|
|
|
balancers_to_keep[new_backend.name] = balancers[new_backend.name]
|
2018-03-23 15:06:21 +00:00
|
|
|
end
|
2018-05-26 01:25:41 +00:00
|
|
|
|
|
|
|
for backend_name, _ in pairs(balancers) do
|
|
|
|
if not balancers_to_keep[backend_name] then
|
|
|
|
balancers[backend_name] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_balancer()
|
|
|
|
local backend_name = ngx.var.proxy_upstream_name
|
|
|
|
return balancers[backend_name]
|
2018-03-23 15:06:21 +00:00
|
|
|
end
|
|
|
|
|
2018-03-18 13:13:41 +00:00
|
|
|
function _M.init_worker()
|
2018-05-14 21:02:09 +00:00
|
|
|
sync_backends() -- when worker starts, sync backends without delay
|
2018-05-09 15:26:04 +00:00
|
|
|
local _, err = ngx.timer.every(BACKENDS_SYNC_INTERVAL, sync_backends)
|
2018-03-18 13:13:41 +00:00
|
|
|
if err then
|
2018-05-04 21:39:57 +00:00
|
|
|
ngx.log(ngx.ERR, string.format("error when setting up timer.every for sync_backends: %s", tostring(err)))
|
2018-03-18 13:13:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-26 01:25:41 +00:00
|
|
|
function _M.rewrite()
|
|
|
|
local balancer = get_balancer()
|
2018-05-18 21:36:43 +00:00
|
|
|
if not balancer then
|
|
|
|
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE
|
|
|
|
return ngx.exit(ngx.status)
|
2018-03-23 15:06:21 +00:00
|
|
|
end
|
2018-05-26 01:25:41 +00:00
|
|
|
end
|
2018-03-23 15:06:21 +00:00
|
|
|
|
2018-05-26 01:25:41 +00:00
|
|
|
function _M.balance()
|
|
|
|
local balancer = get_balancer()
|
|
|
|
if not balancer then
|
2018-05-18 21:36:43 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local host, port = balancer:balance()
|
2018-04-24 16:10:57 +00:00
|
|
|
if not host then
|
2018-05-26 01:25:41 +00:00
|
|
|
ngx.log(ngx.WARN, "no host returned, balancer: " .. balancer.name)
|
|
|
|
return
|
2018-04-24 16:10:57 +00:00
|
|
|
end
|
2018-03-18 13:13:41 +00:00
|
|
|
|
2018-05-14 21:02:09 +00:00
|
|
|
ngx_balancer.set_more_tries(1)
|
|
|
|
|
2018-05-09 15:26:04 +00:00
|
|
|
local ok, err = ngx_balancer.set_current_peer(host, port)
|
|
|
|
if not ok then
|
2018-05-18 21:36:43 +00:00
|
|
|
ngx.log(ngx.ERR, "error while setting current upstream peer to " .. tostring(err))
|
2018-03-18 13:13:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-26 01:25:41 +00:00
|
|
|
function _M.log()
|
|
|
|
local balancer = get_balancer()
|
|
|
|
if not balancer then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
balancer:after_balance()
|
|
|
|
end
|
|
|
|
|
2018-05-18 21:36:43 +00:00
|
|
|
if _TEST then
|
|
|
|
_M.get_implementation = get_implementation
|
|
|
|
_M.sync_backend = sync_backend
|
|
|
|
end
|
|
|
|
|
2018-03-18 13:13:41 +00:00
|
|
|
return _M
|