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(ai): add dynamic route cache key #8113

Merged
merged 11 commits into from
Oct 19, 2022

Conversation

kingluo
Copy link
Contributor

@kingluo kingluo commented Oct 18, 2022

Description

add dynamic route cache key

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)

apisix/core/ai.lua Outdated Show resolved Hide resolved
apisix/core/ai.lua Outdated Show resolved Hide resolved
@@ -71,6 +97,26 @@ function _M.routes_analyze(routes)
else
core.log.info("use ai plane to match route")
router.match = ai_match

if get_cache_key_func_def_render == nil then
local template = require("resty.template")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do this template at top level?

Copy link
Contributor Author

@kingluo kingluo Oct 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resty.template depends on nginx.location, but ai.lua would be called in init_by_lua, where no ngx.location, so for the top level case, the require would fail.

get_cache_key_func_def_render = template.compile(get_cache_key_func_def)
end

local str = get_cache_key_func_def_render({route_flags = route_flags})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we move this logic to one function?

then we can check the return value, and do different thing

@membphis membphis changed the title fix: add dynamic route cache key fix(ai): add dynamic route cache key Oct 18, 2022
Copy link
Member

@membphis membphis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls add some test cases to check it works fine

local str = get_cache_key_func_def_render({route_flags = route_flags})
local func, err = loadstring(str)
if func == nil then
return err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh my god, this is not a good design.

we should use return false, err, the error message can be ignore in some case

@@ -71,6 +118,12 @@ function _M.routes_analyze(routes)
else
core.log.info("use ai plane to match route")
router.match = ai_match

local err = gen_get_cache_key_func(route_flags)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

return false, err
else
local ok
ok, get_cache_key_func = pcall(func)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use a temp var instead of hijacking a module var to hold the potential err message


local get_cache_key_func_def = [[
return function(ctx)
return ctx.var.uri
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make it a little faster, by using local var = ctx.var to reduce table lookup.

t/core/ai.t Outdated
ngx.sleep(1)

-- enable
do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's exact this common part into a function, instead of repeating it three times.

@spacewander spacewander changed the title fix(ai): add dynamic route cache key feat(ai): add dynamic route cache key Oct 19, 2022
@spacewander spacewander merged commit 490e0ba into apache:master Oct 19, 2022
-- TODO: we need to generate cache key dynamically
local key = ctx.var.uri .. "-" .. ctx.var.method .. "-" .. ctx.var.host
local key = get_cache_key_func(ctx)
core.log.info("route cache key: ", core.log.delay_exec(encode_base64, key))
Copy link
Member

@membphis membphis Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kingluo after my benchmark, we have to remove this line.

so we have to find a new way of testing.

local var = ctx.var
return var.uri
{% if route_flags["methods"] then %}
.. "\0" .. var.method
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the uri, host and method, they should generally not contain #

Liu-Junlin pushed a commit to Liu-Junlin/apisix that referenced this pull request Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants