Skip to content

Commit

Permalink
fix(pluginservers): workers invalidate instances
Browse files Browse the repository at this point in the history
Ensure that if a pluginserver is restarted all workers invalidate their
running instances.

Fixes FTI-5238
  • Loading branch information
gszr authored and locao committed Aug 9, 2023
1 parent d0ee961 commit 5205655
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
10 changes: 9 additions & 1 deletion kong/runloop/plugin_servers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function get_instance_id(plugin_name, conf)
-- some other thread is already starting an instance
-- prevent busy-waiting
ngx_sleep(SLEEP_STEP)

-- to prevent a potential dead loop when someone failed to release the ID
wait_count = wait_count + 1
if wait_count > MAX_WAIT_STEPS then
Expand Down Expand Up @@ -386,6 +386,7 @@ end


function plugin_servers.start()
-- only worker 0 managers the plugin server
if worker_id() == 0 then
local pluginserver_timer = proc_mgmt.pluginserver_timer

Expand All @@ -396,13 +397,20 @@ function plugin_servers.start()
end
end

-- workers != 0 still need to get plugin servers definitions
--
local connection_check_timer = proc_mgmt.connection_check_timer

for _, server_def in ipairs(proc_mgmt.get_server_defs()) do
if server_def.start_command then
native_timer_at(0, connection_check_timer, server_def)
end
end

-- in case plugin server restarts, all workers need to update their defs
kong.worker_events.register(function (data)
reset_instance(data.plugin_name, data.conf)
end, "plugin_server", "reset_instances")
end

function plugin_servers.stop()
Expand Down
26 changes: 17 additions & 9 deletions kong/runloop/plugin_servers/pb_rpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -400,15 +400,23 @@ function Rpc:handle_event(plugin_name, conf, phase)
end

if not res or res == "" then
if err == "not ready" then
self.reset_instance(plugin_name, conf)
return handle_not_ready(plugin_name)
end
if err and (str_find(err:lower(), "no plugin instance", 1, true)
or str_find(err:lower(), "closed", 1, true)) then
kong.log.warn(err)
self.reset_instance(plugin_name, conf)
return self:handle_event(plugin_name, conf, phase)
if err then
local ok, err = kong.worker_events.post("plugin_server", "reset_instances",
{ plugin_name = plugin_name, conf = conf })
if not ok then
kong.log.err("failed to post plugin_server reset_instances event: ", err)
end

if err == "not ready" then
self.reset_instance(plugin_name, conf)
return handle_not_ready(plugin_name)
end
if err and (str_find(err:lower(), "no plugin instance", 1, true)
or str_find(err:lower(), "closed", 1, true)) then
kong.log.warn(err)
self.reset_instance(plugin_name, conf)
return self:handle_event(plugin_name, conf, phase)
end
end
kong.log.err(err)
end
Expand Down

0 comments on commit 5205655

Please sign in to comment.