Skip to content

Commit

Permalink
fix: revert sync
Browse files Browse the repository at this point in the history
Signed-off-by: Mohammad Shehar Yaar Tausif <[email protected]>
  • Loading branch information
sheharyaar committed Feb 18, 2024
1 parent 643cd6d commit f3934ee
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 355 deletions.
77 changes: 76 additions & 1 deletion apisix/admin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ local reload_event = "/apisix/admin/plugins/reload"
local ipairs = ipairs
local error = error
local type = type
local sync_local_conf_to_etcd = require("apisix.utils.config_etcd").sync_local_conf_to_etcd


local events
Expand Down Expand Up @@ -291,6 +290,82 @@ local function post_reload_plugins()
end


local function plugins_eq(old, new)
local old_set = {}
for _, p in ipairs(old) do
old_set[p.name] = p
end

local new_set = {}
for _, p in ipairs(new) do
new_set[p.name] = p
end

return core.table.set_eq(old_set, new_set)
end


local function sync_local_conf_to_etcd(reset)
local local_conf = core.config.local_conf()

local plugins = {}
for _, name in ipairs(local_conf.plugins) do
core.table.insert(plugins, {
name = name,
})
end

for _, name in ipairs(local_conf.stream_plugins) do
core.table.insert(plugins, {
name = name,
stream = true,
})
end

if reset then
local res, err = core.etcd.get("/plugins")
if not res then
core.log.error("failed to get current plugins: ", err)
return
end

if res.status == 404 then
-- nothing need to be reset
return
end

if res.status ~= 200 then
core.log.error("failed to get current plugins, status: ", res.status)
return
end

local stored_plugins = res.body.node.value
local revision = res.body.node.modifiedIndex
if plugins_eq(stored_plugins, plugins) then
core.log.info("plugins not changed, don't need to reset")
return
end

core.log.warn("sync local conf to etcd")

local res, err = core.etcd.atomic_set("/plugins", plugins, nil, revision)
if not res then
core.log.error("failed to set plugins: ", err)
end

return
end

core.log.warn("sync local conf to etcd")

-- need to store all plugins name into one key so that it can be updated atomically
local res, err = core.etcd.set("/plugins", plugins)
if not res then
core.log.error("failed to set plugins: ", err)
end
end


local function reload_plugins(data, event, source, pid)
core.log.info("start to hot reload plugins")
plugin.load()
Expand Down
26 changes: 0 additions & 26 deletions apisix/control/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ local pairs = pairs
local type = type
local ngx = ngx
local get_method = ngx.req.get_method
local ngx_worker_id = ngx.worker.id
local events = require("apisix.events")
local sync_local_conf_to_etcd = require("apisix.utils.config_etcd").sync_local_conf_to_etcd

local _M = {}

Expand All @@ -48,7 +45,6 @@ local function format_dismod_uri(mod_name, uri)
return core.table.concat(tmp, "")
end


-- we do not hardcode the discovery module's control api uri
local function format_dismod_control_api_uris(mod_name, api_route)
if not api_route or #api_route == 0 then
Expand Down Expand Up @@ -201,26 +197,4 @@ end

end -- do

local function reload_plugins()
core.log.info("start to hot reload plugins")
plugin_mod.load()

local local_conf = core.config.local_conf()
local deployment_role = core.table.try_read_attr(
local_conf, "deployment", "role")
if deployment_role ~= "data_plane" then
-- data_plane should not write to etcd
if ngx_worker_id() == 0 then
sync_local_conf_to_etcd()
end
end

end


function _M.init_worker()
-- register reload plugin handler
events:register(reload_plugins, builtin_v1_routes.reload_event, "PUT")
end

return _M
13 changes: 5 additions & 8 deletions apisix/control/v1.lua
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,11 @@ function _M.dump_plugin_metadata()
return 200, metadata.value
end

function _M.post_reload_plugins()
local success, err = events:post(_M.RELOAD_EVENT, ngx.req.get_method(), ngx.time())
if not success then
core.response.exit(503, err)
end
function _M.reload_plugins()
core.log.info("start to hot reload plugins")
plugin.load()

core.response.exit(200, "done")
return 200, "done"
end

return {
Expand Down Expand Up @@ -489,8 +487,7 @@ return {
{
methods = {"PUT"},
uris = {"/plugins/reload"},
handler = _M.post_reload_plugins,
handler = _M.reload_plugins,
},
get_health_checkers = _get_health_checkers,
reload_event = _M.RELOAD_EVENT,
}
1 change: 0 additions & 1 deletion apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ function _M.http_init_worker()
apisix_upstream.init_worker()
require("apisix.plugins.ext-plugin.init").init_worker()

control_api_router.init_worker()
local_conf = core.config.local_conf()

if local_conf.apisix and local_conf.apisix.enable_server_tokens == false then
Expand Down
97 changes: 0 additions & 97 deletions apisix/utils/config_etcd.lua

This file was deleted.

Loading

0 comments on commit f3934ee

Please sign in to comment.