-
Notifications
You must be signed in to change notification settings - Fork 2.6k
add ewma balancer #2001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add ewma balancer #2001
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
42ab53f
Merge pull request #1 from apache/master
redynasc 802a4e4
add ewma balancer
f3f80e6
debug ewma balancer
2237b82
add doc
73c10da
fix review error
616e929
trailing whitespace
c400878
remove luacheck warnings
f830491
fix ci errors
9ce4986
fix ci errors2
d5707b0
fix review errors 2
8d4923d
fix ewma.t
dbd27a0
fix ewma.t
3d879d2
remove redundant space
358b878
remove lock; aadd lrucache for _trans_format
23db4e8
include required license header
f9ea6ad
fix the LICENSE when copy file from another open source project
8d2ee8a
add test case
a4c5c53
keep node 1980 hot
84b46df
must keep the node 1980 hot
0aceded
change test params,may be CI ENV latency is high
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
-- | ||
-- 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 core = require("apisix.core") | ||
local ngx = ngx | ||
local ngx_shared = ngx.shared | ||
local ngx_now = ngx.now | ||
local math = math | ||
local pairs = pairs | ||
local next = next | ||
local tonumber = tonumber | ||
|
||
local _M = {} | ||
local DECAY_TIME = 10 -- this value is in seconds | ||
|
||
local shm_ewma = ngx_shared.balancer_ewma | ||
local shm_last_touched_at= ngx_shared.balancer_ewma_last_touched_at | ||
|
||
local lrucache_addr = core.lrucache.new({ | ||
ttl = 300, count = 1024 | ||
}) | ||
local lrucache_trans_format = core.lrucache.new({ | ||
ttl = 300, count = 256 | ||
}) | ||
|
||
|
||
membphis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
local function decay_ewma(ewma, last_touched_at, rtt, now) | ||
local td = now - last_touched_at | ||
td = math.max(td, 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 = shm_last_touched_at:set(upstream, now) | ||
if not success then | ||
core.log.error("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 = shm_ewma:set(upstream, ewma) | ||
if not success then | ||
core.log.error("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 ewma = shm_ewma:get(upstream) or 0 | ||
local now = ngx_now() | ||
local last_touched_at = shm_last_touched_at:get(upstream) or 0 | ||
ewma = decay_ewma(ewma, last_touched_at, rtt, now) | ||
|
||
if not update then | ||
return ewma | ||
redynasc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
store_stats(upstream, ewma, now) | ||
|
||
return ewma | ||
end | ||
|
||
|
||
local function score(upstream) | ||
-- Original implementation used names | ||
-- Endpoints don't have names, so passing in host:Port as key instead | ||
local upstream_name = upstream.host .. ":" .. upstream.port | ||
return get_or_update_ewma(upstream_name, 0, false) | ||
end | ||
|
||
|
||
local function pick_and_score(peers) | ||
local lowest_score_index = 1 | ||
local lowest_score = score(peers[lowest_score_index]) | ||
redynasc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for i = 2, #peers do | ||
local new_score = score(peers[i]) | ||
if new_score < lowest_score then | ||
lowest_score_index, lowest_score = i, new_score | ||
end | ||
end | ||
|
||
return peers[lowest_score_index], lowest_score | ||
end | ||
|
||
|
||
local function parse_addr(addr) | ||
local host, port, err = core.utils.parse_addr(addr) | ||
return {host = host, port = port}, err | ||
end | ||
|
||
|
||
local function _trans_format(up_nodes) | ||
-- trans | ||
--{"1.2.3.4:80":100,"5.6.7.8:8080":100} | ||
-- into | ||
-- [{"host":"1.2.3.4","port":"80"},{"host":"5.6.7.8","port":"8080"}] | ||
local peers = {} | ||
local res, err | ||
|
||
for addr, _ in pairs(up_nodes) do | ||
res, err = lrucache_addr(addr, nil, parse_addr, addr) | ||
if not err then | ||
core.table.insert(peers, res) | ||
else | ||
core.log.error('parse_addr error: ', addr, err) | ||
end | ||
end | ||
|
||
return next(peers) and peers or nil | ||
end | ||
|
||
|
||
local function _ewma_find(ctx, up_nodes) | ||
local peers | ||
local endpoint | ||
|
||
if not up_nodes | ||
or core.table.nkeys(up_nodes) == 0 then | ||
return nil, 'up_nodes empty' | ||
end | ||
|
||
peers = lrucache_trans_format(ctx.upstream_key, ctx.upstream_version, | ||
_trans_format, up_nodes) | ||
if not peers then | ||
return nil, 'up_nodes trans error' | ||
end | ||
|
||
if #peers > 1 then | ||
endpoint = pick_and_score(peers) | ||
else | ||
endpoint = peers[1] | ||
end | ||
|
||
return endpoint.host .. ":" .. endpoint.port | ||
end | ||
|
||
|
||
local function _ewma_after_balance(ctx) | ||
local response_time = tonumber(ctx.var.upstream_response_time) or 0 | ||
local connect_time = tonumber(ctx.var.upstream_connect_time) or 0 | ||
local rtt = connect_time + response_time | ||
local upstream = ctx.var.upstream_addr | ||
|
||
if not upstream then | ||
return nil, "no upstream addr found" | ||
end | ||
|
||
return get_or_update_ewma(upstream, rtt, true) | ||
end | ||
|
||
|
||
function _M.new(up_nodes, upstream) | ||
if not shm_ewma | ||
or not shm_last_touched_at then | ||
return nil, "dictionary not find" | ||
end | ||
|
||
return { | ||
upstream = upstream, | ||
get = function (ctx) | ||
return _ewma_find(ctx, up_nodes) | ||
end, | ||
after_balance = _ewma_after_balance | ||
} | ||
end | ||
|
||
|
||
return _M |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.