Skip to content

roles: fix bugs after config reload #213

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

Merged
merged 2 commits into from
Jun 27, 2025
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Fixed

- Removing the server if it is not in use (#212).
- Changing server address if it was edited (#209).

## [1.7.0] - 2024-11-15

The release introduces a TLS support.
Expand Down
16 changes: 16 additions & 0 deletions roles/httpd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ local function apply_http(name, node)
httpd = httpd,
routes = {},
}
elseif servers[name].host ~= host or servers[name].port ~= port then
servers[name].httpd:stop()
servers[name].httpd = http_server.new(host, port, parse_params(node))
servers[name].httpd:start()
end
end

Expand Down Expand Up @@ -130,9 +134,21 @@ M.apply = function(conf)
-- a meaningful error if something goes wrong.
M.validate(conf)

local actual_servers = {}

for name, node in pairs(conf or {}) do
actual_servers[name] = true

apply_http(name, node)
end

-- Stop server if it is not used anymore.
for name, server in pairs(servers) do
if actual_servers[name] == nil then
server.httpd:stop()
servers[name] = nil
end
end
end

M.stop = function()
Expand Down
8 changes: 8 additions & 0 deletions test/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,12 @@ helpers.update_lua_env_variables = function(server)
ROOT .. '/.rocks/lib/tarantool/?/?.so;'
end

helpers.tcp_connection_exists = function(host, port)
local tcp = socket.tcp()
tcp:settimeout(0.3)
local ok, _ = tcp:connect(host, port)
tcp:close()
return ok
end

return helpers
31 changes: 31 additions & 0 deletions test/integration/httpd_role_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,34 @@ g.test_httpd_role_usage = function(cg)
'return require("test.mocks.mock_role").get_server_port(2)'
), 13001)
end

g.test_stop_server_after_remove = function(cg)
local resp = http_client:get('http://localhost:13001/ping')
t.assert_equals(resp.status, 200, 'response not 200')
t.assert_equals(resp.body, 'pong')

local cfg = table.deepcopy(config)
cfg.groups['group-001'].replicasets['replicaset-001'].roles_cfg['roles.httpd'].additional = nil
treegen.write_file(cg.server.chdir, 'config.yaml', yaml.encode(cfg))
local _, err = cg.server:eval("require('config'):reload()")
t.assert_not(err)

t.assert_not(helpers.tcp_connection_exists('localhost', 13001))
end

g.test_change_server_addr_on_the_run = function(cg)
local resp = http_client:get('http://0.0.0.0:13001/ping')
t.assert_equals(resp.status, 200, 'response not 200')
t.assert_equals(resp.body, 'pong')

local cfg = table.deepcopy(config)
cfg.groups['group-001'].replicasets['replicaset-001'].roles_cfg['roles.httpd'].additional.listen = 'localhost:13001'
treegen.write_file(cg.server.chdir, 'config.yaml', yaml.encode(cfg))
local _, err = cg.server:eval("require('config'):reload()")
t.assert_not(err)

t.assert_not(helpers.tcp_connection_exists('0.0.0.0', 13001))
resp = http_client:get('http://localhost:13001/ping')
t.assert_equals(resp.status, 200, 'response not 200')
t.assert_equals(resp.body, 'pong')
end
12 changes: 7 additions & 5 deletions test/mocks/mock_role.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ M.apply = function(conf)
for _, server in pairs(conf) do
servers[server.id] = require('roles.httpd').get_server(server.name)

servers[server.id]:route({
path = '/ping',
}, function(tx)
return tx:render({text = 'pong'})
end)
if servers[server.id] ~= nil then
servers[server.id]:route({
path = '/ping',
}, function(tx)
return tx:render({text = 'pong'})
end)
end
end
end

Expand Down
43 changes: 43 additions & 0 deletions test/unit/httpd_role_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,46 @@ g['test_get_server_bad_type'] = function()
t.assert_not(ok)
t.assert_str_contains(res, '?string expected, got table')
end

g.test_stop_unused_servers = function()
local cfg = {
[httpd_role.DEFAULT_SERVER_NAME] = {
listen = 13001,
},
additional = {
listen = 13002,
},
}

httpd_role.apply(cfg)
for name, body in pairs(cfg) do
local res = httpd_role.get_server(name)
t.assert(res)
t.assert_equals(res.port, body.listen)
end

cfg.additional = nil
httpd_role.apply(cfg)
t.assert(httpd_role.get_server())
t.assert_equals(httpd_role.get_server('additional'))
end

g.test_edit_server_address = function()
local cfg = {
[httpd_role.DEFAULT_SERVER_NAME] = {
listen = 13001,
},
}

httpd_role.apply(cfg)
local result = httpd_role.get_server()
t.assert(result)
t.assert_equals(result.port, cfg[httpd_role.DEFAULT_SERVER_NAME].listen)

cfg[httpd_role.DEFAULT_SERVER_NAME].listen = 13002

httpd_role.apply(cfg)
result = httpd_role.get_server()
t.assert(result)
t.assert_equals(result.port, cfg[httpd_role.DEFAULT_SERVER_NAME].listen)
end
Loading