Solve IP-Hash issue 95/34395/1
authorHuabingZhao <zhao.huabing@zte.com.cn>
Wed, 7 Mar 2018 02:43:42 +0000 (10:43 +0800)
committerHuabingZhao <zhao.huabing@zte.com.cn>
Wed, 7 Mar 2018 02:43:47 +0000 (10:43 +0800)
Use deepcopy to avoid the side effect, which cause the client request is forward to wrong service instance occassionally.

Issue-ID: MSB-154
Change-Id: I51e04fb273c2ca13c43b1e66fab44424e6af837e
Signed-off-by: HuabingZhao <zhao.huabing@zte.com.cn>
openresty-ext/src/assembly/resources/openresty/nginx/luaext/loadbalance/policy/consistent_hash.lua

index b3cd46e..344b261 100644 (file)
@@ -31,7 +31,7 @@ local LUCKY_NUM = 13
 
 local tbl_util  = require('lib.utils.table_util')
 local tbl_isempty = tbl_util.isempty
-local tbl_isequal = require('pl.tablex')
+local tablex = require('pl.tablex')
 local peerwatcher = require "core.peerwatcher"
 local ngx_var = ngx.var
 local hash_data = {}
@@ -73,10 +73,10 @@ end
 local function update_consistent_hash_state(hash_data,servers,svckey)
     -- compare servers in ctx with servers in cache
     -- update the hash data if changes occur
-    local serverscache = hash_data[svckey].servers
+    local serverscache = tablex.deepcopy(hash_data[svckey].servers)
     tab_sort(serverscache, function(a, b) return a.ip < b.ip end)
     tab_sort(servers, function(a, b) return a.ip < b.ip end)
-    if  not tbl_isequal.deepcompare(serverscache, servers, false) then
+    if  not tablex.deepcompare(serverscache, servers, false) then
         local tmp_chash = init_consistent_hash_state(servers)
         hash_data[svckey].servers =servers
         hash_data[svckey].chash = tmp_chash