Skip to content
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

feat: renew route lrucache when the routes change #8157

Merged
merged 6 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apisix/http/router/radixtree_host_uri.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ function _M.match(api_ctx)
cached_service_version = service_version
end

return _M.matching(api_ctx)
end


function _M.matching(api_ctx)
core.table.clear(match_opts)
match_opts.method = api_ctx.var.request_method
match_opts.remote_addr = api_ctx.var.remote_addr
Expand Down
5 changes: 5 additions & 0 deletions apisix/http/router/radixtree_uri.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ function _M.match(api_ctx)
return true
end

return _M.matching(api_ctx)
end


function _M.matching(api_ctx)
return base_router.match_uri(uri_router, match_opts, api_ctx)
end

Expand Down
5 changes: 5 additions & 0 deletions apisix/http/router/radixtree_uri_with_parameter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ function _M.match(api_ctx)
return true
end

return _M.matching(api_ctx)
end


function _M.matching(api_ctx)
return base_router.match_uri(uri_router, match_opts, api_ctx)
end

Expand Down
40 changes: 21 additions & 19 deletions apisix/plugins/ai.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ return function(ctx)
end
]]

local route_lrucache = core.lrucache.new({
-- TODO: we need to set the cache size by count of routes
-- if we have done this feature, we need to release the origin lrucache
count = 512
})
local route_lrucache

local schema = {}

Expand All @@ -64,27 +60,26 @@ local _M = {
scope = "global",
}

local orig_router_match
local orig_router_http_matching
local orig_handle_upstream = apisix.handle_upstream
local orig_http_balancer_phase = apisix.http_balancer_phase

local default_keepalive_pool = {}

local function match_route(ctx)
orig_router_match(ctx)
return ctx.matched_route or false
local function create_router_matching_cache(api_ctx)
orig_router_http_matching(api_ctx)
return api_ctx.matched_route or false
end


local function ai_match(ctx)
local key = get_cache_key_func(ctx)
local function ai_router_http_matching(api_ctx)
local key = get_cache_key_func(api_ctx)
core.log.info("route cache key: ", key)
local ver = router.router_http.user_routes.conf_version
local route_cache = route_lrucache(key, ver,
match_route, ctx)
local route_cache = route_lrucache(key, nil,
create_router_matching_cache, api_ctx)
-- if the version has not changed, use the cached route
if route_cache then
ctx.matched_route = route_cache
api_ctx.matched_route = route_cache
end
end

Expand Down Expand Up @@ -224,15 +219,21 @@ local function routes_analyze(routes)
or route_flags["service_id"]
or route_flags["plugin_config_id"]
or global_rules_flag then
router.router_http.match = orig_router_match
router.router_http.matching = orig_router_http_matching
else
core.log.info("use ai plane to match route")
router.router_http.match = ai_match
router.router_http.matching = ai_router_http_matching

local count = #routes + 3000
tzssangglass marked this conversation as resolved.
Show resolved Hide resolved
core.log.info("renew route cache: count=", count)
route_lrucache = core.lrucache.new({
count = count
})

local ok, err = gen_get_cache_key_func(route_flags)
if not ok then
core.log.error("generate get_cache_key_func failed:", err)
router.router_http.match = orig_router_match
router.router_http.matching = orig_router_http_matching
end
end

Expand Down Expand Up @@ -280,7 +281,8 @@ end


function _M.init_worker()
orig_router_match = router.router_http.match
orig_router_http_matching = router.router_http.matching
end


return _M
54 changes: 54 additions & 0 deletions t/plugin/ai.t
Original file line number Diff line number Diff line change
Expand Up @@ -875,3 +875,57 @@ done
qr/enable sample upstream/
--- grep_error_log_out
enable sample upstream



=== TEST 14: renew route cache
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local http = require "resty.http"
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
for k = 1, 2 do
local code, body = t('/apisix/admin/routes/' .. k,
ngx.HTTP_PUT,
[[{
"host": "127.0.0.1",
"methods": ["GET"],
"plugins": {
"proxy-rewrite": {
"uri": "/hello"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello]] .. k .. [["
}]]
)
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end
ngx.sleep(1)
for i = 1, 2 do
local httpc = http.new()
local res, err = httpc:request_uri(uri .. k)
assert(res.status == 200)
if not res then
ngx.log(ngx.ERR, err)
return
end
end
end
ngx.say("done")
}
}
--- response_body
done
--- error_log
renew route cache: count=3001
renew route cache: count=3002