membphis commented on a change in pull request #2001:
URL: https://github.com/apache/apisix/pull/2001#discussion_r465842892



##########
File path: apisix/balancer/ewma.lua
##########
@@ -0,0 +1,237 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local resty_lock = require("resty.lock")
+local core = require("apisix.core")
+
+local _M = {}
+local DECAY_TIME = 10 -- this value is in seconds
+local LOCK_KEY = ":ewma_key"
+local PICK_SET_SIZE = 2
+local ewma_lock, ewma_lock_err 
+
+
+local function lock(upstream)
+       local _, err = ewma_lock:lock(upstream .. LOCK_KEY)
+       if err then
+               if err ~= "timeout" then
+                       core.log.error(string.format("EWMA Balancer failed to 
lock: %s", tostring(err)))
+               end
+       end
+
+       return err

Review comment:
       `return true` or `return false, err` is easier to read

##########
File path: apisix/balancer/ewma.lua
##########
@@ -0,0 +1,237 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local resty_lock = require("resty.lock")
+local core = require("apisix.core")
+
+local _M = {}
+local DECAY_TIME = 10 -- this value is in seconds
+local LOCK_KEY = ":ewma_key"
+local PICK_SET_SIZE = 2
+local ewma_lock, ewma_lock_err 
+
+
+local function lock(upstream)
+       local _, err = ewma_lock:lock(upstream .. LOCK_KEY)
+       if err then
+               if err ~= "timeout" then
+                       core.log.error(string.format("EWMA Balancer failed to 
lock: %s", tostring(err)))
+               end
+       end
+
+       return err
+end
+
+local function unlock()
+       local ok, err = ewma_lock:unlock()
+       if not ok then
+               core.log.error(string.format("EWMA Balancer failed to unlock: 
%s", tostring(err)))
+       end
+
+       return err
+end
+
+local function decay_ewma(ewma, last_touched_at, rtt, now)
+       local td = now - last_touched_at
+       td = (td > 0) and td or 0
+       local weight = math.exp(-td/DECAY_TIME)
+
+       ewma = ewma * weight + rtt * (1.0 - weight)
+       return ewma
+end
+
+local function store_stats(upstream, ewma, now)
+       local success, err, forcible = 
ngx.shared.balancer_ewma_last_touched_at:set(upstream, now)

Review comment:
       local cache `balancer_ewma_last_touched_at` to top level

##########
File path: apisix/balancer/ewma.lua
##########
@@ -0,0 +1,237 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local resty_lock = require("resty.lock")
+local core = require("apisix.core")
+
+local _M = {}
+local DECAY_TIME = 10 -- this value is in seconds
+local LOCK_KEY = ":ewma_key"
+local PICK_SET_SIZE = 2
+local ewma_lock, ewma_lock_err 
+
+
+local function lock(upstream)
+       local _, err = ewma_lock:lock(upstream .. LOCK_KEY)

Review comment:
       Replace the "tab" key with 4 spaces

##########
File path: apisix/balancer/ewma.lua
##########
@@ -0,0 +1,237 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local resty_lock = require("resty.lock")
+local core = require("apisix.core")
+
+local _M = {}
+local DECAY_TIME = 10 -- this value is in seconds
+local LOCK_KEY = ":ewma_key"
+local PICK_SET_SIZE = 2
+local ewma_lock, ewma_lock_err 
+
+
+local function lock(upstream)
+       local _, err = ewma_lock:lock(upstream .. LOCK_KEY)
+       if err then
+               if err ~= "timeout" then
+                       core.log.error(string.format("EWMA Balancer failed to 
lock: %s", tostring(err)))
+               end
+       end
+
+       return err
+end
+
+local function unlock()
+       local ok, err = ewma_lock:unlock()
+       if not ok then
+               core.log.error(string.format("EWMA Balancer failed to unlock: 
%s", tostring(err)))
+       end
+
+       return err
+end
+

Review comment:
       use two blank lines between different functions.

##########
File path: apisix/balancer/ewma.lua
##########
@@ -0,0 +1,237 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local resty_lock = require("resty.lock")
+local core = require("apisix.core")
+
+local _M = {}
+local DECAY_TIME = 10 -- this value is in seconds
+local LOCK_KEY = ":ewma_key"
+local PICK_SET_SIZE = 2
+local ewma_lock, ewma_lock_err 
+
+
+local function lock(upstream)
+       local _, err = ewma_lock:lock(upstream .. LOCK_KEY)
+       if err then
+               if err ~= "timeout" then
+                       core.log.error(string.format("EWMA Balancer failed to 
lock: %s", tostring(err)))
+               end
+       end
+
+       return err
+end
+
+local function unlock()
+       local ok, err = ewma_lock:unlock()
+       if not ok then
+               core.log.error(string.format("EWMA Balancer failed to unlock: 
%s", tostring(err)))
+       end
+
+       return err
+end
+
+local function decay_ewma(ewma, last_touched_at, rtt, now)
+       local td = now - last_touched_at
+       td = (td > 0) and td or 0
+       local weight = math.exp(-td/DECAY_TIME)
+
+       ewma = ewma * weight + rtt * (1.0 - weight)
+       return ewma
+end
+
+local function store_stats(upstream, ewma, now)
+       local success, err, forcible = 
ngx.shared.balancer_ewma_last_touched_at:set(upstream, now)
+       if not success then
+               core.log.warn("balancer_ewma_last_touched_at:set failed " .. 
err)
+       end
+       if forcible then
+               core.log.warn("balancer_ewma_last_touched_at:set valid items 
forcibly overwritten")
+       end
+
+       success, err, forcible = ngx.shared.balancer_ewma:set(upstream, ewma)
+       if not success then
+               core.log.warn("balancer_ewma:set failed " .. err)
+       end
+       if forcible then
+               core.log.warn("balancer_ewma:set valid items forcibly 
overwritten")
+       end
+end
+
+local function get_or_update_ewma(upstream, rtt, update)
+       local lock_err = nil
+       if update then
+               lock_err = lock(upstream)
+       end
+       local ewma = ngx.shared.balancer_ewma:get(upstream) or 0
+       if lock_err ~= nil then
+               return ewma, lock_err
+       end
+
+       local now = ngx.now()
+       local last_touched_at = 
ngx.shared.balancer_ewma_last_touched_at:get(upstream) or 0
+       ewma = decay_ewma(ewma, last_touched_at, rtt, now)
+
+       if not update then
+               return ewma, nil

Review comment:
       `return ewma` is enough.

##########
File path: bin/apisix
##########
@@ -181,7 +181,10 @@ http {
     lua_shared_dict worker-events        10m;
     lua_shared_dict lrucache-lock        10m;
     lua_shared_dict skywalking-tracing-buffer    100m;
-
+    lua_shared_dict balancer_ewma 10m;
+    lua_shared_dict balancer_ewma_last_touched_at 10m;
+    lua_shared_dict balancer_ewma_locks 10m;

Review comment:
       need to align
   
   
![image](https://user-images.githubusercontent.com/6814606/89438009-dbb0cb00-d77a-11ea-93ca-316bead5ddad.png)
   

##########
File path: apisix/balancer/ewma.lua
##########
@@ -0,0 +1,237 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local resty_lock = require("resty.lock")
+local core = require("apisix.core")
+
+local _M = {}
+local DECAY_TIME = 10 -- this value is in seconds
+local LOCK_KEY = ":ewma_key"
+local PICK_SET_SIZE = 2
+local ewma_lock, ewma_lock_err 
+
+
+local function lock(upstream)
+       local _, err = ewma_lock:lock(upstream .. LOCK_KEY)
+       if err then
+               if err ~= "timeout" then
+                       core.log.error(string.format("EWMA Balancer failed to 
lock: %s", tostring(err)))
+               end
+       end
+
+       return err
+end
+
+local function unlock()
+       local ok, err = ewma_lock:unlock()
+       if not ok then
+               core.log.error(string.format("EWMA Balancer failed to unlock: 
%s", tostring(err)))

Review comment:
       ditto

##########
File path: apisix/balancer/ewma.lua
##########
@@ -0,0 +1,237 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local resty_lock = require("resty.lock")
+local core = require("apisix.core")
+
+local _M = {}
+local DECAY_TIME = 10 -- this value is in seconds
+local LOCK_KEY = ":ewma_key"
+local PICK_SET_SIZE = 2
+local ewma_lock, ewma_lock_err 
+
+
+local function lock(upstream)
+       local _, err = ewma_lock:lock(upstream .. LOCK_KEY)
+       if err then
+               if err ~= "timeout" then
+                       core.log.error(string.format("EWMA Balancer failed to 
lock: %s", tostring(err)))

Review comment:
       bad performance style.
   
   right style: `core.log.error("xxxx: ", err)`

##########
File path: apisix/balancer/ewma.lua
##########
@@ -0,0 +1,237 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local resty_lock = require("resty.lock")
+local core = require("apisix.core")
+
+local _M = {}
+local DECAY_TIME = 10 -- this value is in seconds
+local LOCK_KEY = ":ewma_key"
+local PICK_SET_SIZE = 2
+local ewma_lock, ewma_lock_err 
+
+
+local function lock(upstream)
+       local _, err = ewma_lock:lock(upstream .. LOCK_KEY)
+       if err then
+               if err ~= "timeout" then
+                       core.log.error(string.format("EWMA Balancer failed to 
lock: %s", tostring(err)))
+               end
+       end
+
+       return err
+end
+
+local function unlock()
+       local ok, err = ewma_lock:unlock()
+       if not ok then
+               core.log.error(string.format("EWMA Balancer failed to unlock: 
%s", tostring(err)))
+       end
+
+       return err
+end
+
+local function decay_ewma(ewma, last_touched_at, rtt, now)
+       local td = now - last_touched_at
+       td = (td > 0) and td or 0
+       local weight = math.exp(-td/DECAY_TIME)

Review comment:
       need a space: `-td/DECAY_TIME` -> `-td / DECAY_TIME`

##########
File path: apisix/balancer/ewma.lua
##########
@@ -0,0 +1,237 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local resty_lock = require("resty.lock")
+local core = require("apisix.core")
+
+local _M = {}
+local DECAY_TIME = 10 -- this value is in seconds
+local LOCK_KEY = ":ewma_key"
+local PICK_SET_SIZE = 2
+local ewma_lock, ewma_lock_err 
+
+
+local function lock(upstream)
+       local _, err = ewma_lock:lock(upstream .. LOCK_KEY)
+       if err then

Review comment:
       bad style:
   
   ```lua
   if err and err ~= "timeout" then
       -- ...
   end
   ```

##########
File path: apisix/balancer/ewma.lua
##########
@@ -0,0 +1,237 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local resty_lock = require("resty.lock")
+local core = require("apisix.core")
+
+local _M = {}
+local DECAY_TIME = 10 -- this value is in seconds
+local LOCK_KEY = ":ewma_key"
+local PICK_SET_SIZE = 2
+local ewma_lock, ewma_lock_err 
+
+
+local function lock(upstream)
+       local _, err = ewma_lock:lock(upstream .. LOCK_KEY)
+       if err then
+               if err ~= "timeout" then
+                       core.log.error(string.format("EWMA Balancer failed to 
lock: %s", tostring(err)))
+               end
+       end
+
+       return err
+end
+
+local function unlock()
+       local ok, err = ewma_lock:unlock()
+       if not ok then
+               core.log.error(string.format("EWMA Balancer failed to unlock: 
%s", tostring(err)))
+       end
+
+       return err
+end
+
+local function decay_ewma(ewma, last_touched_at, rtt, now)
+       local td = now - last_touched_at
+       td = (td > 0) and td or 0
+       local weight = math.exp(-td/DECAY_TIME)
+
+       ewma = ewma * weight + rtt * (1.0 - weight)
+       return ewma
+end
+
+local function store_stats(upstream, ewma, now)
+       local success, err, forcible = 
ngx.shared.balancer_ewma_last_touched_at:set(upstream, now)
+       if not success then
+               core.log.warn("balancer_ewma_last_touched_at:set failed " .. 
err)
+       end
+       if forcible then
+               core.log.warn("balancer_ewma_last_touched_at:set valid items 
forcibly overwritten")
+       end
+
+       success, err, forcible = ngx.shared.balancer_ewma:set(upstream, ewma)
+       if not success then
+               core.log.warn("balancer_ewma:set failed " .. err)
+       end
+       if forcible then
+               core.log.warn("balancer_ewma:set valid items forcibly 
overwritten")
+       end
+end
+
+local function get_or_update_ewma(upstream, rtt, update)
+       local lock_err = nil
+       if update then
+               lock_err = lock(upstream)
+       end
+       local ewma = ngx.shared.balancer_ewma:get(upstream) or 0
+       if lock_err ~= nil then
+               return ewma, lock_err
+       end
+
+       local now = ngx.now()
+       local last_touched_at = 
ngx.shared.balancer_ewma_last_touched_at:get(upstream) or 0
+       ewma = decay_ewma(ewma, last_touched_at, rtt, now)
+
+       if not update then
+               return ewma, nil
+       end
+
+       store_stats(upstream, ewma, now)
+
+       unlock()
+
+       return ewma, nil
+end
+
+local function score(upstream)
+       -- Original implementation used names
+       -- Endpoints don't have names, so passing in IP:Port as key instead
+       local upstream_name = upstream.address .. ":" .. upstream.port
+       return get_or_update_ewma(upstream_name, 0, false)
+end
+
+-- implementation similar to 
https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
+-- or https://en.wikipedia.org/wiki/Random_permutation
+-- loop from 1 .. k
+-- pick a random value r from the remaining set of unpicked values (i .. n)
+-- swap the value at position i with the value at position r
+local function shuffle_peers(peers, k)
+       for i=1, k do

Review comment:
       `i=1` -> `i = 1`

##########
File path: apisix/balancer/ewma.lua
##########
@@ -0,0 +1,237 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local resty_lock = require("resty.lock")
+local core = require("apisix.core")
+
+local _M = {}
+local DECAY_TIME = 10 -- this value is in seconds
+local LOCK_KEY = ":ewma_key"
+local PICK_SET_SIZE = 2
+local ewma_lock, ewma_lock_err 
+
+
+local function lock(upstream)
+       local _, err = ewma_lock:lock(upstream .. LOCK_KEY)
+       if err then
+               if err ~= "timeout" then
+                       core.log.error(string.format("EWMA Balancer failed to 
lock: %s", tostring(err)))
+               end
+       end
+
+       return err
+end
+
+local function unlock()
+       local ok, err = ewma_lock:unlock()
+       if not ok then
+               core.log.error(string.format("EWMA Balancer failed to unlock: 
%s", tostring(err)))
+       end
+
+       return err
+end
+
+local function decay_ewma(ewma, last_touched_at, rtt, now)
+       local td = now - last_touched_at
+       td = (td > 0) and td or 0
+       local weight = math.exp(-td/DECAY_TIME)
+
+       ewma = ewma * weight + rtt * (1.0 - weight)
+       return ewma
+end
+
+local function store_stats(upstream, ewma, now)
+       local success, err, forcible = 
ngx.shared.balancer_ewma_last_touched_at:set(upstream, now)
+       if not success then
+               core.log.warn("balancer_ewma_last_touched_at:set failed " .. 
err)

Review comment:
       bad performance: avoid to create new string object, this way will make 
GC work hard.

##########
File path: apisix/balancer/ewma.lua
##########
@@ -0,0 +1,237 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local resty_lock = require("resty.lock")
+local core = require("apisix.core")
+
+local _M = {}
+local DECAY_TIME = 10 -- this value is in seconds
+local LOCK_KEY = ":ewma_key"
+local PICK_SET_SIZE = 2
+local ewma_lock, ewma_lock_err 
+
+
+local function lock(upstream)
+       local _, err = ewma_lock:lock(upstream .. LOCK_KEY)
+       if err then
+               if err ~= "timeout" then
+                       core.log.error(string.format("EWMA Balancer failed to 
lock: %s", tostring(err)))
+               end
+       end
+
+       return err
+end
+
+local function unlock()
+       local ok, err = ewma_lock:unlock()
+       if not ok then
+               core.log.error(string.format("EWMA Balancer failed to unlock: 
%s", tostring(err)))
+       end
+
+       return err
+end
+
+local function decay_ewma(ewma, last_touched_at, rtt, now)
+       local td = now - last_touched_at
+       td = (td > 0) and td or 0
+       local weight = math.exp(-td/DECAY_TIME)
+
+       ewma = ewma * weight + rtt * (1.0 - weight)
+       return ewma
+end
+
+local function store_stats(upstream, ewma, now)
+       local success, err, forcible = 
ngx.shared.balancer_ewma_last_touched_at:set(upstream, now)

Review comment:
       throw an error if there is no `balancer_ewma_last_touched_at` defined

##########
File path: apisix/balancer/ewma.lua
##########
@@ -0,0 +1,237 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local resty_lock = require("resty.lock")
+local core = require("apisix.core")
+
+local _M = {}
+local DECAY_TIME = 10 -- this value is in seconds
+local LOCK_KEY = ":ewma_key"
+local PICK_SET_SIZE = 2
+local ewma_lock, ewma_lock_err 
+
+
+local function lock(upstream)
+       local _, err = ewma_lock:lock(upstream .. LOCK_KEY)
+       if err then
+               if err ~= "timeout" then
+                       core.log.error(string.format("EWMA Balancer failed to 
lock: %s", tostring(err)))
+               end
+       end
+
+       return err
+end
+
+local function unlock()
+       local ok, err = ewma_lock:unlock()
+       if not ok then
+               core.log.error(string.format("EWMA Balancer failed to unlock: 
%s", tostring(err)))
+       end
+
+       return err
+end
+
+local function decay_ewma(ewma, last_touched_at, rtt, now)
+       local td = now - last_touched_at
+       td = (td > 0) and td or 0
+       local weight = math.exp(-td/DECAY_TIME)
+
+       ewma = ewma * weight + rtt * (1.0 - weight)
+       return ewma
+end
+
+local function store_stats(upstream, ewma, now)
+       local success, err, forcible = 
ngx.shared.balancer_ewma_last_touched_at:set(upstream, now)
+       if not success then
+               core.log.warn("balancer_ewma_last_touched_at:set failed " .. 
err)
+       end
+       if forcible then
+               core.log.warn("balancer_ewma_last_touched_at:set valid items 
forcibly overwritten")
+       end
+
+       success, err, forcible = ngx.shared.balancer_ewma:set(upstream, ewma)
+       if not success then
+               core.log.warn("balancer_ewma:set failed " .. err)
+       end
+       if forcible then
+               core.log.warn("balancer_ewma:set valid items forcibly 
overwritten")
+       end
+end
+
+local function get_or_update_ewma(upstream, rtt, update)
+       local lock_err = nil
+       if update then
+               lock_err = lock(upstream)
+       end
+       local ewma = ngx.shared.balancer_ewma:get(upstream) or 0
+       if lock_err ~= nil then
+               return ewma, lock_err
+       end
+
+       local now = ngx.now()
+       local last_touched_at = 
ngx.shared.balancer_ewma_last_touched_at:get(upstream) or 0
+       ewma = decay_ewma(ewma, last_touched_at, rtt, now)
+
+       if not update then
+               return ewma, nil
+       end
+
+       store_stats(upstream, ewma, now)
+
+       unlock()
+
+       return ewma, nil
+end
+
+local function score(upstream)
+       -- Original implementation used names
+       -- Endpoints don't have names, so passing in IP:Port as key instead
+       local upstream_name = upstream.address .. ":" .. upstream.port
+       return get_or_update_ewma(upstream_name, 0, false)
+end
+
+-- implementation similar to 
https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
+-- or https://en.wikipedia.org/wiki/Random_permutation
+-- loop from 1 .. k
+-- pick a random value r from the remaining set of unpicked values (i .. n)
+-- swap the value at position i with the value at position r
+local function shuffle_peers(peers, k)
+       for i=1, k do
+               local rand_index = math.random(i,#peers)

Review comment:
       `math.random(i,#peers)` -> `math.random(i, #peers)`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to