Skip to content

Commit

Permalink
fix(clustering) conditional rebuilding broke stream reconfigure (#8952)
Browse files Browse the repository at this point in the history
### Summary

The reconfigure event needs to pass hashes and not only the default
workspace. This broke when PR #8519 was merged and then `LMDB` PR
#8224 got merged after it.
  • Loading branch information
bungle authored Jun 14, 2022
1 parent 2cf1d33 commit ef58cdd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
19 changes: 14 additions & 5 deletions kong/db/declarative/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local constants = require "kong.constants"
local txn = require "resty.lmdb.transaction"
local lmdb = require "resty.lmdb"


local setmetatable = setmetatable
local loadstring = loadstring
local tostring = tostring
Expand Down Expand Up @@ -957,6 +958,7 @@ do
return nil, "exiting"
end

local reconfigure_data
local worker_events = kong.worker_events

local ok, err, default_ws = declarative.load_into_cache(entities, meta, hash)
Expand All @@ -980,12 +982,14 @@ do
end
end

ok, err = worker_events.post("declarative", "reconfigure", {
reconfigure_data = {
default_ws,
router_hash,
plugins_hash,
balancer_hash
})
}

ok, err = worker_events.post("declarative", "reconfigure", reconfigure_data)
if ok ~= "done" then
return nil, "failed to broadcast reconfigure event: " .. (err or ok)
end
Expand All @@ -1000,22 +1004,27 @@ do
if SUBSYS == "http" and #kong.configuration.stream_listeners > 0 then
-- update stream if necessary

local json, err = cjson.encode(reconfigure_data)
if not json then
return nil, err
end

local sock = ngx_socket_tcp()
ok, err = sock:connect("unix:" .. PREFIX .. "/stream_config.sock")
if not ok then
return nil, err
end

local bytes
bytes, err = sock:send(default_ws)
bytes, err = sock:send(json)
sock:close()

if not bytes then
return nil, err
end

assert(bytes == #default_ws,
"incomplete default workspace id sent to the stream subsystem")
assert(bytes == #json,
"incomplete reconfigure data sent to the stream subsystem")
end


Expand Down
15 changes: 10 additions & 5 deletions kong/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,8 @@ end


do
local cjson = require "cjson.safe"

function Kong.stream_config_listener()
local sock, err = ngx.req.socket()
if not sock then
Expand All @@ -1568,16 +1570,19 @@ do

local data, err = sock:receive("*a")
if not data then
ngx_log(ngx_CRIT, "unable to receive new config: ", err)
ngx_log(ngx_CRIT, "unable to receive reconfigure data: ", err)
return
end

kong.core_cache:purge()
kong.cache:purge()
local reconfigure_data, err = cjson.decode(data)
if not reconfigure_data then
ngx_log(ngx_ERR, "failed to json decode reconfigure data: ", err)
return
end

local ok, err = kong.worker_events.post("declarative", "reconfigure", data)
local ok, err = kong.worker_events.post("declarative", "reconfigure", reconfigure_data)
if ok ~= "done" then
ngx_log(ngx_ERR, "failed to reboadcast reconfigure event in stream: ", err or ok)
ngx_log(ngx_ERR, "failed to rebroadcast reconfigure event in stream: ", err or ok)
end
end
end
Expand Down

0 comments on commit ef58cdd

Please sign in to comment.