Skip to content

Commit

Permalink
fix(plugin-servers): harden seq number generation
Browse files Browse the repository at this point in the history
Also, `get_instance_id` uses plugin cache key to fetch instance id.
  • Loading branch information
gszr authored and jschmid1 committed Nov 7, 2023
1 parent 349d36e commit 9ca82dd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
4 changes: 1 addition & 3 deletions kong/runloop/plugin_servers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ function get_instance_id(plugin_name, conf)

if instance_info
and instance_info.id
and instance_info.seq == conf.__seq__
and instance_info.conf and instance_info.conf.__key__ == key
then
-- exact match, return it
return instance_info.id
Expand All @@ -224,7 +224,6 @@ function get_instance_id(plugin_name, conf)
-- we're the first, put something to claim
instance_info = {
conf = conf,
seq = conf.__seq__,
}
running_instances[key] = instance_info
else
Expand All @@ -247,7 +246,6 @@ function get_instance_id(plugin_name, conf)
instance_info.id = new_instance_info.id
instance_info.plugin_name = plugin_name
instance_info.conf = new_instance_info.conf
instance_info.seq = new_instance_info.seq
instance_info.Config = new_instance_info.Config
instance_info.rpc = new_instance_info.rpc

Expand Down
3 changes: 3 additions & 0 deletions kong/runloop/plugin_servers/pb_rpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ function Rpc:call_start_instance(plugin_name, conf)
return nil, err
end

kong.log.debug("started plugin server: seq ", conf.__seq__, ", worker ", ngx.worker.id(), ", instance id ",
status.instance_status.instance_id)

return {
id = status.instance_status.instance_id,
conf = conf,
Expand Down
10 changes: 7 additions & 3 deletions kong/runloop/plugins_iterator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ do
end


local NEXT_SEQ = 0
local PLUGINS_NS = "plugins." .. subsystem
local ENABLED_PLUGINS
local LOADED_PLUGINS
Expand Down Expand Up @@ -170,8 +169,13 @@ local function get_plugin_config(plugin, name, ws_id)
-- TODO: deprecate usage of __key__ as id of plugin
if not cfg.__key__ then
cfg.__key__ = key
cfg.__seq__ = NEXT_SEQ
NEXT_SEQ = NEXT_SEQ + 1
-- generate a unique sequence across workers
-- with a seq 0, plugin server generates an unused random instance id
local next_seq, err = ngx.shared.kong:incr("plugins_iterator:__seq__", 1, 0, 0)
if err then
next_seq = 0
end
cfg.__seq__ = next_seq
end

return cfg
Expand Down

1 comment on commit 9ca82dd

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

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

Bazel Build

Docker image available kong/kong:9ca82ddb46f5b766c9df1982444697f0b3c0b496
Artifacts available https://github.com/Kong/kong/actions/runs/6782268735

Please sign in to comment.