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: rerun rewrite phase for newly added plugins in consumer #6502

Merged
merged 9 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 3 additions & 8 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -454,15 +454,10 @@ function _M.http_access_phase()

if changed then
api_ctx.matched_route = route
local runned_plugins = core.table.deepcopy(api_ctx.plugins)
core.table.clear(api_ctx.plugins)
local unrunn_plugins
api_ctx.plugins, unrunn_plugins = plugin.filter(api_ctx, route,
api_ctx.plugins, nil, runned_plugins)
if unrunn_plugins then
-- rerun rewrite phase for newly added plugins in consumer
plugin.run_plugin("rewrite", unrunn_plugins, api_ctx)
end
api_ctx.plugins = plugin.filter(api_ctx, route, api_ctx.plugins)
-- rerun rewrite phase for newly added plugins in consumer
plugin.rerun_plugins_of_consumer(api_ctx.plugins, api_ctx)
tokers marked this conversation as resolved.
Show resolved Hide resolved
end
end
plugin.run_plugin("access", plugins, api_ctx)
Expand Down
57 changes: 36 additions & 21 deletions apisix/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ local function trace_plugins_info_for_debug(ctx, plugins)
end


function _M.filter(ctx, conf, plugins, route_conf, runned_plugins)
function _M.filter(ctx, conf, plugins, route_conf)
local user_plugin_conf = conf.value.plugins
if user_plugin_conf == nil or
core.table.nkeys(user_plugin_conf) == 0 then
Expand All @@ -365,17 +365,7 @@ function _M.filter(ctx, conf, plugins, route_conf, runned_plugins)
return plugins or core.tablepool.fetch("plugins", 0, 0)
end

local unrunn_plugins
local runned_plugins_names
if runned_plugins and type(runned_plugins) == "table" then
runned_plugins_names = core.table.new(#runned_plugins / 2, 0)
for i = 1, #runned_plugins, 2 do
core.table.insert(runned_plugins_names, runned_plugins[i].name)
end
unrunn_plugins = core.table.new(4, 0)
end

local route_plugin_conf = route_conf and route_conf.value and route_conf.value.plugins
local route_plugin_conf = route_conf and route_conf.value.plugins
shuaijinchao marked this conversation as resolved.
Show resolved Hide resolved
plugins = plugins or core.tablepool.fetch("plugins", 32, 0)
for _, plugin_obj in ipairs(local_plugins) do
local name = plugin_obj.name
Expand All @@ -389,14 +379,6 @@ function _M.filter(ctx, conf, plugins, route_conf, runned_plugins)
end
end

if runned_plugins_names and not core.table.array_find(runned_plugins_names, name) then
-- no need to rerun the auth plugins
if plugin_obj.type ~= 'auth' then
core.table.insert(unrunn_plugins, plugin_obj)
core.table.insert(unrunn_plugins, plugin_conf)
end
end

core.table.insert(plugins, plugin_obj)
core.table.insert(plugins, plugin_conf)

Expand All @@ -406,7 +388,7 @@ function _M.filter(ctx, conf, plugins, route_conf, runned_plugins)

trace_plugins_info_for_debug(ctx, plugins)

return plugins, unrunn_plugins
return plugins
end


Expand Down Expand Up @@ -521,6 +503,11 @@ local function merge_consumer_route(route_conf, consumer_conf)
end

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 use if new_route_conf.value.plugins[name] == nil then conf._from_consumer = true?

Copy link
Member Author

Choose a reason for hiding this comment

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

tried, Test 2 will fail. We need to merge the `new_route_conf.value.plugins["proxy-rewrite"], it will not be nil.

Copy link
Member

Choose a reason for hiding this comment

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

What about this change?

diff --git apisix/plugin.lua apisix/plugin.lua
index d66ec045..190b0833 100644
--- apisix/plugin.lua
+++ apisix/plugin.lua
@@ -502,12 +502,11 @@ local function merge_consumer_route(route_conf, consumer_conf)
             new_route_conf.value.plugins = {}
         end

-        new_route_conf.value.plugins[name] = conf
-
-        if (route_conf and route_conf.value and route_conf.value.plugins)
-                and not route_conf.value.plugins[name] then
-            new_route_conf.value.plugins[name]["_from_consumer"] = true
+        if new_route_conf.value.plugins[name] == nil then
+            conf._from_consumer = true
         end
+
+        new_route_conf.value.plugins[name] = conf
     end

     core.log.info("merged conf : ", core.json.delay_encode(new_route_conf))

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, update

new_route_conf.value.plugins[name] = conf

if (route_conf and route_conf.value and route_conf.value.plugins)
and not route_conf.value.plugins[name] then
new_route_conf.value.plugins[name]["_un_running"] = true
end
end

core.log.info("merged conf : ", core.json.delay_encode(new_route_conf))
Expand Down Expand Up @@ -823,4 +810,32 @@ function _M.run_global_rules(api_ctx, global_rules, phase_name)
end


function _M.rerun_plugins_of_consumer(plugins, api_ctx)
for i = 1, #plugins, 2 do
-- no need to rerun the auth plugins
if plugins[i + 1]["_un_running"] and plugins[i].type ~= "auth" then
local phase_func = plugins[i]["rewrite"]
if phase_func then
local code, body = phase_func(plugins[i + 1], api_ctx)
if code or body then
if is_http then
if code >= 400 then
core.log.warn(plugins[i].name, " exits with http status code ", code)
end

core.response.exit(code, body)
else
if code >= 400 then
core.log.warn(plugins[i].name, " exits with status code ", code)
end

ngx_exit(1)
end
end
end
end
end
return api_ctx
end

return _M