From cf104f58ec5232d3c2869141ca2e8f0c72d9803f Mon Sep 17 00:00:00 2001 From: spacewander Date: Thu, 24 Dec 2020 09:07:12 +0800 Subject: [PATCH] Update for review --- rootfs/etc/nginx/lua/balancer/ewma.lua | 5 ++--- rootfs/etc/nginx/lua/test/balancer/ewma_test.lua | 9 +++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/rootfs/etc/nginx/lua/balancer/ewma.lua b/rootfs/etc/nginx/lua/balancer/ewma.lua index f7cbadf3a..ae65ccc73 100644 --- a/rootfs/etc/nginx/lua/balancer/ewma.lua +++ b/rootfs/etc/nginx/lua/balancer/ewma.lua @@ -176,7 +176,6 @@ function _M.balance(self) if #peers > 1 then local k = (#peers < PICK_SET_SIZE) and #peers or PICK_SET_SIZE - local peer_copy = util.deepcopy(peers) local tried_endpoints if not ngx.ctx.balancer_ewma_tried_endpoints then @@ -187,7 +186,7 @@ function _M.balance(self) end local filtered_peers - for _, peer in ipairs(peer_copy) do + for _, peer in ipairs(peers) do if not tried_endpoints[get_upstream_name(peer)] then if not filtered_peers then filtered_peers = {} @@ -198,7 +197,7 @@ function _M.balance(self) if not filtered_peers then ngx.log(ngx.WARN, "all endpoints have been retried") - filtered_peers = peer_copy + filtered_peers = util.deepcopy(peers) end if #filtered_peers > 1 then diff --git a/rootfs/etc/nginx/lua/test/balancer/ewma_test.lua b/rootfs/etc/nginx/lua/test/balancer/ewma_test.lua index a54f6214a..6af588396 100644 --- a/rootfs/etc/nginx/lua/test/balancer/ewma_test.lua +++ b/rootfs/etc/nginx/lua/test/balancer/ewma_test.lua @@ -106,6 +106,7 @@ describe("Balancer ewma", function() -- even though 10.10.10.1:8080 has a lower ewma score -- algorithm picks 10.10.10.3:8080 because its decayed score is even lower assert.equal("10.10.10.3:8080", peer) + assert.equal(true, ngx.ctx.balancer_ewma_tried_endpoints["10.10.10.3:8080"]) assert.are.equals(0.16240233988393523723, ngx.var.balancer_ewma_score) end) @@ -114,8 +115,12 @@ describe("Balancer ewma", function() table.remove(two_endpoints_backend.endpoints, 2) local two_endpoints_instance = balancer_ewma:new(two_endpoints_backend) + ngx.ctx.balancer_ewma_tried_endpoints = { + ["10.10.10.3:8080"] = true, + } local peer = two_endpoints_instance:balance() assert.equal("10.10.10.1:8080", peer) + assert.equal(true, ngx.ctx.balancer_ewma_tried_endpoints["10.10.10.1:8080"]) end) it("all the endpoints are tried, pick the one with lowest score", function() @@ -123,6 +128,10 @@ describe("Balancer ewma", function() table.remove(two_endpoints_backend.endpoints, 2) local two_endpoints_instance = balancer_ewma:new(two_endpoints_backend) + ngx.ctx.balancer_ewma_tried_endpoints = { + ["10.10.10.1:8080"] = true, + ["10.10.10.3:8080"] = true, + } local peer = two_endpoints_instance:balance() assert.equal("10.10.10.3:8080", peer) end)