Skip to content

fix: add exptime to ewma shared dict items#12557

Merged
nic-6443 merged 1 commit into
apache:masterfrom
nic-6443:nic/ewma-sharedict
Sep 1, 2025
Merged

fix: add exptime to ewma shared dict items#12557
nic-6443 merged 1 commit into
apache:masterfrom
nic-6443:nic/ewma-sharedict

Conversation

@nic-6443

@nic-6443 nic-6443 commented Aug 28, 2025

Copy link
Copy Markdown
Member

Description

Because the current ewma load balancer uses two shared memory and has no recycling mechanism, when the user's upstream IP address changes frequently, the shared memory will be quickly consumed, resulting in the following error:

2024/11/19 19:29:04 [error] 42#42: *7149052434 [lua] ewma.lua:63: store_stats(): shm_last_touched_at:set failed: no memory while logging request...

This PR sets expiration times for the two shared memory items used by ewma to record upstream latency data, so that when executing the set and get functions of sharedict, openresty will attempt to reclaim 1~2 expired entries.
https://github.com/openresty/lua-nginx-module/blob/c47084b5d719ce507d2419d8660f39544a9d1fea/src/ngx_http_lua_shdict.c#L1595-L1599
https://github.com/openresty/lua-nginx-module/blob/c47084b5d719ce507d2419d8660f39544a9d1fea/src/ngx_http_lua_shdict.c#L1368
https://github.com/openresty/lua-nginx-module/blob/c47084b5d719ce507d2419d8660f39544a9d1fea/src/ngx_http_lua_shdict.c#L250-L254
This ensures that the shared memory space is released.

The choice of TTL
According to the current ewma algorithm parameters:

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 DECAY_TIME = 10 -- this value is in seconds

set ttl to 60s is more appropriate because when the current time exceeds 60s from the last_touched_at, the weight of that historical score is already close to 0. Therefore, whether or not this historical score exists does not matter.
image

This error can only be triggered when there are frequent and continuous changes in the upstream nodes. so it is not able to provide an effective integration test.

Which issue(s) this PR fixes:

Fixes #

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. bug Something isn't working labels Aug 28, 2025
@nic-6443
nic-6443 merged commit 9ae2403 into apache:master Sep 1, 2025
29 checks passed
@nic-6443
nic-6443 deleted the nic/ewma-sharedict branch September 1, 2025 03:31
jizhuozhi pushed a commit to jizhuozhi/apisix that referenced this pull request Oct 18, 2025
shreemaan-abhishek pushed a commit to shreemaan-abhishek/apisix that referenced this pull request Jan 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants