diff --git a/rootfs/etc/nginx/lua/balancer/sticky.lua b/rootfs/etc/nginx/lua/balancer/sticky.lua index b088ff860..73498d8a3 100644 --- a/rootfs/etc/nginx/lua/balancer/sticky.lua +++ b/rootfs/etc/nginx/lua/balancer/sticky.lua @@ -1,5 +1,4 @@ local balancer_resty = require("balancer.resty") -local util = require("util") local ck = require("resty.cookie") local ngx_balancer = require("ngx.balancer") local split = require("util.split") @@ -87,7 +86,6 @@ local function get_failed_upstreams() end local function should_set_cookie(self) - if self.cookie_session_affinity.locations and ngx.var.host then local locs = self.cookie_session_affinity.locations[ngx.var.host] if locs == nil then @@ -115,7 +113,7 @@ end function _M.balance(self) local upstream_from_cookie - local key = self:get_routing_key() + local key = self:get_cookie() if key then upstream_from_cookie = self.instance:find(key) end @@ -134,7 +132,7 @@ function _M.balance(self) 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 - self:set_routing_key(key) + self:set_cookie(key) end return new_upstream @@ -144,18 +142,6 @@ function _M.sync(self, backend) -- reload balancer nodes 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 - - ngx_log(INFO, string_format("[%s] nodes have changed for backend %s", self.name, backend.name)) - self.traffic_shaping_policy = backend.trafficShapingPolicy self.alternative_backends = backend.alternativeBackends self.cookie_session_affinity = backend.sessionAffinityConfig.cookieSessionAffinity diff --git a/rootfs/etc/nginx/lua/balancer/sticky_balanced.lua b/rootfs/etc/nginx/lua/balancer/sticky_balanced.lua index e2132dc08..ce0018158 100644 --- a/rootfs/etc/nginx/lua/balancer/sticky_balanced.lua +++ b/rootfs/etc/nginx/lua/balancer/sticky_balanced.lua @@ -5,9 +5,9 @@ -- pods. -- local balancer_sticky = require("balancer.sticky") -local math = require("math") +local math_random = require("math").random local resty_chash = require("resty.chash") -local util = require("util") +local util_get_nodes = require("util").get_nodes local _M = balancer_sticky:new() @@ -18,7 +18,7 @@ local _M = balancer_sticky:new() local MAX_UPSTREAM_CHECKS_COUNT = 20 function _M.new(self, backend) - local nodes = util.get_nodes(backend.endpoints) + local nodes = util_get_nodes(backend.endpoints) local o = { name = "sticky_balanced", @@ -33,17 +33,9 @@ function _M.new(self, backend) return o end -function _M.get_routing_key(self) - return self:get_cookie(), nil -end - -function _M.set_routing_key(self, key) - self:set_cookie(key) -end - function _M.pick_new_upstream(self, failed_upstreams) 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 key = string.format("%s.%s.%s", ngx.now() + i, ngx.worker.pid(), math_random(999999)) local new_upstream = self.instance:find(key) if not failed_upstreams[new_upstream] then diff --git a/rootfs/etc/nginx/lua/balancer/sticky_persistent.lua b/rootfs/etc/nginx/lua/balancer/sticky_persistent.lua index 993eb8c8c..a4a6f0da2 100644 --- a/rootfs/etc/nginx/lua/balancer/sticky_persistent.lua +++ b/rootfs/etc/nginx/lua/balancer/sticky_persistent.lua @@ -4,13 +4,13 @@ -- be rebalanced. -- local balancer_sticky = require("balancer.sticky") -local util = require("util") +local util_get_nodes = require("util").get_nodes local util_nodemap = require("util.nodemap") local _M = balancer_sticky:new() function _M.new(self, backend) - local nodes = util.get_nodes(backend.endpoints) + local nodes = util_get_nodes(backend.endpoints) local hash_salt = backend["name"] local o = { @@ -26,29 +26,6 @@ function _M.new(self, backend) return o end -function _M.get_routing_key(self) - local cookie_value = self:get_cookie() - - if cookie_value then - -- format .. - local routing_key = string.match(cookie_value, '[^\\.]+$') - - if routing_key == nil then - local err = string.format("Failed to extract routing key from cookie '%s'!", cookie_value) - return nil, err - end - - return routing_key, nil - end - - return nil, nil -end - -function _M.set_routing_key(self, key) - local value = string.format("%s.%s.%s", ngx.now(), ngx.worker.pid(), key) - self:set_cookie(value); -end - function _M.pick_new_upstream(self, failed_upstreams) return self.instance:random_except(failed_upstreams) end diff --git a/rootfs/etc/nginx/lua/util/nodemap.lua b/rootfs/etc/nginx/lua/util/nodemap.lua index b4b4c9738..04d034023 100644 --- a/rootfs/etc/nginx/lua/util/nodemap.lua +++ b/rootfs/etc/nginx/lua/util/nodemap.lua @@ -1,5 +1,5 @@ -local math = require("math") -local util = require("util") +local math_random = require("math").random +local util_tablelength = require("util").tablelength local _M = {} @@ -24,13 +24,13 @@ end -- @tparam {[string], ...} map A key to node hash table. -- @treturn string,string The node and its key local function get_random_node(map) - local size = util.tablelength(map) + local size = util_tablelength(map) if size < 1 then return nil, nil end - local index = math.random(1, size) + local index = math_random(1, size) local count = 1 for key, endpoint in pairs(map) do @@ -58,7 +58,6 @@ end -- @tparam {[string]=number} endpoints A table with the node endpoint as a key and its weight as a value. -- @tparam[opt] string hash_salt A optional hash salt that will be used to obfuscate the hash key. function _M.new(self, endpoints, hash_salt) - if hash_salt == nil then hash_salt = '' end @@ -103,7 +102,7 @@ function _M.random_except(self, ignore_nodes) local valid_nodes = {} -- avoid generating the map if no ignores where provided - if ignore_nodes == nil or util.tablelength(ignore_nodes) == 0 then + if ignore_nodes == nil or util_tablelength(ignore_nodes) == 0 then return get_random_node(self.map) end