Skip to content

feat(ai): add dynamic route cache key#8113

Merged
spacewander merged 11 commits into
apache:masterfrom
kingluo:dynamic_route_cache_key
Oct 19, 2022
Merged

feat(ai): add dynamic route cache key#8113
spacewander merged 11 commits into
apache:masterfrom
kingluo:dynamic_route_cache_key

Conversation

@kingluo

@kingluo kingluo commented Oct 18, 2022

Copy link
Copy Markdown
Contributor

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)

Comment thread apisix/core/ai.lua Outdated
Comment thread apisix/core/ai.lua Outdated
Comment thread apisix/core/ai.lua Outdated
router.match = ai_match

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

Copy link
Copy Markdown
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?

@kingluo kingluo Oct 18, 2022

Copy link
Copy Markdown
Contributor Author

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.

Comment thread apisix/core/ai.lua Outdated
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
Copy Markdown
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

@membphis membphis left a comment

Copy link
Copy Markdown
Member

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

Comment thread apisix/core/ai.lua Outdated
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
Copy Markdown
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

Comment thread apisix/core/ai.lua Outdated
core.log.info("use ai plane to match route")
router.match = ai_match

local err = gen_get_cache_key_func(route_flags)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ditto

Comment thread apisix/core/ai.lua Outdated
return false, err
else
local ok
ok, get_cache_key_func = pcall(func)

Copy link
Copy Markdown
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

Comment thread apisix/core/ai.lua Outdated

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

Copy link
Copy Markdown
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.

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

-- enable
do

Copy link
Copy Markdown
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
Comment thread apisix/core/ai.lua
-- 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))

@membphis membphis Oct 19, 2022

Copy link
Copy Markdown
Member

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.

Comment thread apisix/core/ai.lua
local var = ctx.var
return var.uri
{% if route_flags["methods"] then %}
.. "\0" .. var.method

Copy link
Copy Markdown
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