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(ext-plugin): send the idempotent key when preparing conf #4736

Merged
merged 1 commit into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 15 additions & 3 deletions apisix/core/lrucache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,16 @@ end
global_lru_fun = new_lru_fun()


local plugin_ctx
local plugin_ctx, plugin_ctx_id
do
local key_buf = {
nil,
nil,
nil,
nil,
}

function plugin_ctx(lrucache, api_ctx, extra_key, create_obj_func, ...)
local function plugin_ctx_key_and_ver(api_ctx, extra_key)
key_buf[1] = api_ctx.conf_type
key_buf[2] = api_ctx.conf_id

Expand All @@ -159,7 +160,17 @@ do
key = concat(key_buf, "#", 1, 2)
end

return lrucache(key, api_ctx.conf_version, create_obj_func, ...)
return key, api_ctx.conf_version
end

function plugin_ctx(lrucache, api_ctx, extra_key, create_obj_func, ...)
local key, ver = plugin_ctx_key_and_ver(api_ctx, extra_key)
return lrucache(key, ver, create_obj_func, ...)
end

function plugin_ctx_id(api_ctx, extra_key)
local key, ver = plugin_ctx_key_and_ver(api_ctx, extra_key)
return key .. "#" .. ver
end
end

Expand All @@ -169,6 +180,7 @@ local _M = {
new = new_lru_fun,
global = global_lru_fun,
plugin_ctx = plugin_ctx,
plugin_ctx_id = plugin_ctx_id,
}


Expand Down
9 changes: 7 additions & 2 deletions apisix/plugins/ext-plugin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,11 @@ end
local rpc_call
local rpc_handlers = {
nil,
function (conf, ctx, sock)
function (conf, ctx, sock, unique_key)
builder:Clear()

local key = builder:CreateString(unique_key)

local conf_vec
if conf.conf then
local len = #conf.conf
Expand All @@ -278,6 +280,7 @@ local rpc_handlers = {
end

prepare_conf_req.Start(builder)
prepare_conf_req.AddKey(builder, key)
if conf_vec then
prepare_conf_req.AddConf(builder, conf_vec)
end
Expand Down Expand Up @@ -306,8 +309,10 @@ local rpc_handlers = {
return token
end,
function (conf, ctx, sock, entry)
local lrucache_id = core.lrucache.plugin_ctx_id(ctx, entry)
local token, err = core.lrucache.plugin_ctx(lrucache, ctx, entry, rpc_call,
constants.RPC_PREPARE_CONF, conf, ctx)
constants.RPC_PREPARE_CONF, conf, ctx,
lrucache_id)
if not token then
return nil, err
end
Expand Down
2 changes: 1 addition & 1 deletion rockspec/apisix-master-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ dependencies = {
"luasec = 0.9-1",
"lua-resty-consul = 0.3-2",
"penlight = 1.9.2-1",
"ext-plugin-proto = 0.1.1",
"ext-plugin-proto = 0.2.1",
}

build = {
Expand Down
7 changes: 7 additions & 0 deletions t/lib/ext-plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ function _M.go(case)
assert(pc:ConfLength() == 0)
end

if case.expect_key_pattern then
local m = ngx.re.find(pc:Key(), case.expect_key_pattern, "jo")
assert(m ~= nil, pc:Key())
else
assert(pc:Key() ~= "")
end

prepare_conf_resp.Start(builder)
prepare_conf_resp.AddConfToken(builder, 233)
local req = prepare_conf_req.End(builder)
Expand Down
96 changes: 95 additions & 1 deletion t/plugin/ext-plugin/sanity.t
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ hello world

content_by_lua_block {
local ext = require("lib.ext-plugin")
ext.go({with_conf = true})
ext.go({with_conf = true, expect_key_pattern = [[^route#1#ext-plugin-pre-req#]]})
}
}
--- error_log eval
Expand Down Expand Up @@ -475,3 +475,97 @@ MY_ENV_VAR foo
location /t {
return 200;
}



=== TEST 16: prepare conf with global rule
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")

local code, message, res = t.test('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/hello",
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
}
}]]
)

if code >= 300 then
ngx.say(message)
return
end

local code, message, res = t.test('/apisix/admin/global_rules/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"ext-plugin-post-req": {
"conf": [
{"name":"foo", "value":"bar"},
{"name":"cat", "value":"dog"}
]
}
}
}]]
)

if code >= 300 then
ngx.status = code
ngx.say(message)
return
end

ngx.say(message)
}
}
--- response_body
passed



=== TEST 17: hit
--- request
GET /hello
--- response_body
hello world
--- extra_stream_config
server {
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock;

content_by_lua_block {
local ext = require("lib.ext-plugin")
ext.go({with_conf = true, expect_key_pattern = [[^global_rule#1#ext-plugin-post-req#]]})
}
}
--- error_log eval
qr/get conf token: 233 conf: \[(\{"value":"bar","name":"foo"\}|\{"name":"foo","value":"bar"\}),(\{"value":"dog","name":"cat"\}|\{"name":"cat","value":"dog"\})\]/
--- no_error_log
[error]



=== TEST 18: clean global rule
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")

local code, message, res = t.test('/apisix/admin/global_rules/1',
ngx.HTTP_DELETE)

if code >= 300 then
ngx.status = code
ngx.say(message)
return
end
}
}