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

refactor(clustering/rpc): rework on rpc initial connection #13824

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 31 additions & 7 deletions kong/clustering/rpc/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ end


function _M:_add_socket(socket, capabilities_list)
local sockets = self.clients[socket.node_id]
local node_id = socket.node_id

local sockets = self.clients[node_id]
if not sockets then
assert(self.concentrator:_enqueue_subscribe(socket.node_id))
assert(self.concentrator:_enqueue_subscribe(node_id))
sockets = setmetatable({}, { __mode = "k", })
self.clients[socket.node_id] = sockets
self.clients[node_id] = sockets
end

self.client_capabilities[socket.node_id] = {
self.client_capabilities[node_id] = {
set = pl_tablex_makeset(capabilities_list),
list = capabilities_list,
}
Expand Down Expand Up @@ -274,6 +276,30 @@ function _M:handle_websocket()
end


function _M:try_connect(reconnection_delay)
ngx.timer.at(reconnection_delay or 0, function(premature)
self:connect(premature,
"control_plane", -- node_id
self.conf.cluster_control_plane, -- host
"/v2/outlet", -- path
self.cluster_cert.cdata,
self.cluster_cert_key)
end)
end


function _M:init_worker()
if self.conf.role == "data_plane" then
-- data_plane will try to connect to cp
self:try_connect()

else
-- control_plane
self.concentrator:start()
end
end


function _M:connect(premature, node_id, host, path, cert, key)
if premature then
return
Expand Down Expand Up @@ -350,9 +376,7 @@ function _M:connect(premature, node_id, host, path, cert, key)
::err::

if not exiting() then
ngx.timer.at(reconnection_delay, function(premature)
self:connect(premature, node_id, host, path, cert, key)
end)
self:try_connect(reconnection_delay)
end
end

Expand Down
38 changes: 13 additions & 25 deletions kong/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -886,15 +886,15 @@ function Kong.init_worker()

if kong.clustering then
-- full sync dp

local is_dp_full_sync_agent = process.type() == "privileged agent" and not kong.sync

if is_control_plane(kong.configuration) or -- CP needs to support both full and incremental sync
is_dp_full_sync_agent -- full sync is only enabled for DP if incremental sync is disabled
then
kong.clustering:init_worker()
end

-- DP full sync agent skips the rest of the init_worker
if is_dp_full_sync_agent then
return
Expand Down Expand Up @@ -995,30 +995,18 @@ function Kong.init_worker()
plugin_servers.start()
end

if kong.clustering then
-- rpc and incremental sync
if kong.rpc and is_http_module then

-- only available in http subsystem
local cluster_tls = require("kong.clustering.tls")

if is_data_plane(kong.configuration) then
ngx.timer.at(0, function(premature)
kong.rpc:connect(premature,
"control_plane", kong.configuration.cluster_control_plane,
"/v2/outlet",
cluster_tls.get_cluster_cert(kong.configuration).cdata,
cluster_tls.get_cluster_cert_key(kong.configuration))
end)

else -- control_plane
kong.rpc.concentrator:start()
end
-- rpc and incremental sync
if is_http_module then

-- init incremental sync
if kong.sync then
kong.sync:init_worker()
end
-- init rpc connection
if kong.rpc then
kong.rpc:init_worker()
end

-- init incremental sync
-- should run after rpc init successfully
if kong.sync then
kong.sync:init_worker()
end
end

Expand Down
Loading