Skip to content

Commit

Permalink
fix(plugin/bk-cache-fallback): bk-cache-fallback use the separate sha… (
Browse files Browse the repository at this point in the history
#46)

* fix(plugin/bk-cache-fallback): bk-cache-fallback use the separate shared_dict for lock

* fix(plugin/bk-cache-fallback): add resty_lock options, set timeout and exptime

* docs(plugin/bk-cache-fallback): add comment

* docs(plugin/bk-cache-fallback): add comment
  • Loading branch information
wklken authored Sep 21, 2023
1 parent ca182a3 commit 0ac963a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/apisix/plugins/bk-cache-fallback/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ local ngx_shared = ngx.shared

local fallback_missing_err = "create_obj_funcs failed and got no data in the shared_dict for fallback"

-- NOTE: here we use the same shared_dict as apisix lrucache-lock
-- if this become a problem in the future, we should apply and use our own bk-lrucache-lock
local lock_shdict_name = "lrucache-lock"
-- NOTE: change to separate shared_dict, avoid use the name with `lurcache-lock`
local lock_shdict_name = "plugin-bk-cache-fallback-lock"
if ngx.config.subsystem == "stream" then
lock_shdict_name = lock_shdict_name .. "-" .. ngx.config.subsystem
end
Expand Down Expand Up @@ -123,14 +122,22 @@ function _M.get_with_fallback(self, ctx, key, version, create_obj_func, ...)
-- 1.2 lrucache miss

-- 2. retrieve the lock
local lock, create_lock_err = resty_lock:new(lock_shdict_name)
-- NOTE: while the bk-components http timeout is 5s, here the lock timeout should be bigger than 5s
-- and at the same time, set the exptime shorter, the lock will be released if the worker is crashed
-- so: http timeout < lock timeout < lock exptime
-- https://github.com/openresty/lua-resty-lock#new
local lock, create_lock_err = resty_lock:new(lock_shdict_name, {timeout = 6, exptime = 7})
if not lock then
return nil, "failed to create lock, err: " .. create_lock_err
end

local key_s = cache_key
log.info("try to lock with key ", key_s)

-- FIXME: possible problem here, if high concurrent, all requests may wait here except one
-- and at that time, process one by one after the retrieve finished
-- maybe some requests will timeout?
-- check the sentry and error log after this version online
local elapsed, lock_err = lock:lock(key_s)
if not elapsed then
return nil, "failed to acquire the bk-cache-fallback lock, err: " .. lock_err
Expand Down
1 change: 1 addition & 0 deletions src/apisix/plugins/bk-components/bk-apigateway-core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ local string_format = string.format

local QUERY_PERMISSION_URL = "/api/v1/micro-gateway/%s/permissions/"
local QUERY_PUBLIC_KEY_URL = "/api/v1/micro-gateway/%s/public_keys/"
-- NOTE: important, if you change the timeout here, you should reset the timeout/exptime in bk-cache-fallback lock
local BKCORE_TIMEOUT_MS = 5 * 1000

local _M = {
Expand Down
1 change: 1 addition & 0 deletions src/apisix/tests/conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ lua_shared_dict plugin-limit-conn 1m;
lua_shared_dict prometheus-metrics 1m;
lua_shared_dict plugin-bk-permission 1m;
lua_shared_dict plugin-bk-cache-fallback 1m;
lua_shared_dict plugin-bk-cache-fallback-lock 1m;
# for unittest, bk-cache-fallback/init.lua, case ok/fail/ok->fail
lua_shared_dict plugin-bk-cache-fallback-ok 1m;
lua_shared_dict plugin-bk-cache-fallback-fail 1m;
Expand Down

0 comments on commit 0ac963a

Please sign in to comment.