|
| 1 | +-- |
| 2 | +-- Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +-- contributor license agreements. See the NOTICE file distributed with |
| 4 | +-- this work for additional information regarding copyright ownership. |
| 5 | +-- The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +-- (the "License"); you may not use this file except in compliance with |
| 7 | +-- the License. You may obtain a copy of the License at |
| 8 | +-- |
| 9 | +-- http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +-- |
| 11 | +-- Unless required by applicable law or agreed to in writing, software |
| 12 | +-- distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +-- See the License for the specific language governing permissions and |
| 15 | +-- limitations under the License. |
| 16 | +-- |
| 17 | + |
| 18 | + |
| 19 | +local core = require("apisix.core") |
| 20 | +local ngx = ngx |
| 21 | +local ngx_shared = ngx.shared |
| 22 | +local ngx_now = ngx.now |
| 23 | +local math = math |
| 24 | +local pairs = pairs |
| 25 | +local next = next |
| 26 | +local tonumber = tonumber |
| 27 | + |
| 28 | +local _M = {} |
| 29 | +local DECAY_TIME = 10 -- this value is in seconds |
| 30 | + |
| 31 | +local shm_ewma = ngx_shared.balancer_ewma |
| 32 | +local shm_last_touched_at= ngx_shared.balancer_ewma_last_touched_at |
| 33 | + |
| 34 | +local lrucache_addr = core.lrucache.new({ |
| 35 | + ttl = 300, count = 1024 |
| 36 | +}) |
| 37 | +local lrucache_trans_format = core.lrucache.new({ |
| 38 | + ttl = 300, count = 256 |
| 39 | +}) |
| 40 | + |
| 41 | + |
| 42 | +local function decay_ewma(ewma, last_touched_at, rtt, now) |
| 43 | + local td = now - last_touched_at |
| 44 | + td = math.max(td, 0) |
| 45 | + local weight = math.exp(-td / DECAY_TIME) |
| 46 | + |
| 47 | + ewma = ewma * weight + rtt * (1.0 - weight) |
| 48 | + return ewma |
| 49 | +end |
| 50 | + |
| 51 | + |
| 52 | +local function store_stats(upstream, ewma, now) |
| 53 | + local success, err, forcible = shm_last_touched_at:set(upstream, now) |
| 54 | + if not success then |
| 55 | + core.log.error("balancer_ewma_last_touched_at:set failed ", err) |
| 56 | + end |
| 57 | + if forcible then |
| 58 | + core.log.warn("balancer_ewma_last_touched_at:set valid items forcibly overwritten") |
| 59 | + end |
| 60 | + |
| 61 | + success, err, forcible = shm_ewma:set(upstream, ewma) |
| 62 | + if not success then |
| 63 | + core.log.error("balancer_ewma:set failed ", err) |
| 64 | + end |
| 65 | + if forcible then |
| 66 | + core.log.warn("balancer_ewma:set valid items forcibly overwritten") |
| 67 | + end |
| 68 | +end |
| 69 | + |
| 70 | + |
| 71 | +local function get_or_update_ewma(upstream, rtt, update) |
| 72 | + local ewma = shm_ewma:get(upstream) or 0 |
| 73 | + local now = ngx_now() |
| 74 | + local last_touched_at = shm_last_touched_at:get(upstream) or 0 |
| 75 | + ewma = decay_ewma(ewma, last_touched_at, rtt, now) |
| 76 | + |
| 77 | + if not update then |
| 78 | + return ewma |
| 79 | + end |
| 80 | + |
| 81 | + store_stats(upstream, ewma, now) |
| 82 | + |
| 83 | + return ewma |
| 84 | +end |
| 85 | + |
| 86 | + |
| 87 | +local function score(upstream) |
| 88 | + -- Original implementation used names |
| 89 | + -- Endpoints don't have names, so passing in host:Port as key instead |
| 90 | + local upstream_name = upstream.host .. ":" .. upstream.port |
| 91 | + return get_or_update_ewma(upstream_name, 0, false) |
| 92 | +end |
| 93 | + |
| 94 | + |
| 95 | +local function pick_and_score(peers) |
| 96 | + local lowest_score_index = 1 |
| 97 | + local lowest_score = score(peers[lowest_score_index]) |
| 98 | + for i = 2, #peers do |
| 99 | + local new_score = score(peers[i]) |
| 100 | + if new_score < lowest_score then |
| 101 | + lowest_score_index, lowest_score = i, new_score |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + return peers[lowest_score_index], lowest_score |
| 106 | +end |
| 107 | + |
| 108 | + |
| 109 | +local function parse_addr(addr) |
| 110 | + local host, port, err = core.utils.parse_addr(addr) |
| 111 | + return {host = host, port = port}, err |
| 112 | +end |
| 113 | + |
| 114 | + |
| 115 | +local function _trans_format(up_nodes) |
| 116 | + -- trans |
| 117 | + --{"1.2.3.4:80":100,"5.6.7.8:8080":100} |
| 118 | + -- into |
| 119 | + -- [{"host":"1.2.3.4","port":"80"},{"host":"5.6.7.8","port":"8080"}] |
| 120 | + local peers = {} |
| 121 | + local res, err |
| 122 | + |
| 123 | + for addr, _ in pairs(up_nodes) do |
| 124 | + res, err = lrucache_addr(addr, nil, parse_addr, addr) |
| 125 | + if not err then |
| 126 | + core.table.insert(peers, res) |
| 127 | + else |
| 128 | + core.log.error('parse_addr error: ', addr, err) |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + return next(peers) and peers or nil |
| 133 | +end |
| 134 | + |
| 135 | + |
| 136 | +local function _ewma_find(ctx, up_nodes) |
| 137 | + local peers |
| 138 | + local endpoint |
| 139 | + |
| 140 | + if not up_nodes |
| 141 | + or core.table.nkeys(up_nodes) == 0 then |
| 142 | + return nil, 'up_nodes empty' |
| 143 | + end |
| 144 | + |
| 145 | + peers = lrucache_trans_format(ctx.upstream_key, ctx.upstream_version, |
| 146 | + _trans_format, up_nodes) |
| 147 | + if not peers then |
| 148 | + return nil, 'up_nodes trans error' |
| 149 | + end |
| 150 | + |
| 151 | + if #peers > 1 then |
| 152 | + endpoint = pick_and_score(peers) |
| 153 | + else |
| 154 | + endpoint = peers[1] |
| 155 | + end |
| 156 | + |
| 157 | + return endpoint.host .. ":" .. endpoint.port |
| 158 | +end |
| 159 | + |
| 160 | + |
| 161 | +local function _ewma_after_balance(ctx) |
| 162 | + local response_time = tonumber(ctx.var.upstream_response_time) or 0 |
| 163 | + local connect_time = tonumber(ctx.var.upstream_connect_time) or 0 |
| 164 | + local rtt = connect_time + response_time |
| 165 | + local upstream = ctx.var.upstream_addr |
| 166 | + |
| 167 | + if not upstream then |
| 168 | + return nil, "no upstream addr found" |
| 169 | + end |
| 170 | + |
| 171 | + return get_or_update_ewma(upstream, rtt, true) |
| 172 | +end |
| 173 | + |
| 174 | + |
| 175 | +function _M.new(up_nodes, upstream) |
| 176 | + if not shm_ewma |
| 177 | + or not shm_last_touched_at then |
| 178 | + return nil, "dictionary not find" |
| 179 | + end |
| 180 | + |
| 181 | + return { |
| 182 | + upstream = upstream, |
| 183 | + get = function (ctx) |
| 184 | + return _ewma_find(ctx, up_nodes) |
| 185 | + end, |
| 186 | + after_balance = _ewma_after_balance |
| 187 | + } |
| 188 | +end |
| 189 | + |
| 190 | + |
| 191 | +return _M |
0 commit comments