Skip to content

Commit 0d63047

Browse files
committed
roles: delete server if it is not in use
There was a but if the server is not used (for example, it was removed from the configuration), it will still be running after the configuration is reloaded. This patch fixes it. Closes #212
1 parent ca1db4d commit 0d63047

File tree

6 files changed

+66
-5
lines changed

6 files changed

+66
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212

1313
### Fixed
1414

15+
- Removing the server if it is not in use (#212).
16+
1517
## [1.7.0] - 2024-11-15
1618

1719
The release introduces a TLS support.

roles/httpd.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,21 @@ M.apply = function(conf)
130130
-- a meaningful error if something goes wrong.
131131
M.validate(conf)
132132

133+
local actual_servers = {}
134+
133135
for name, node in pairs(conf or {}) do
136+
actual_servers[name] = true
137+
134138
apply_http(name, node)
135139
end
140+
141+
-- Stop server if it is not using anymore.
142+
for name, server in pairs(servers) do
143+
if actual_servers[name] == nil then
144+
server.httpd:stop()
145+
servers[name] = nil
146+
end
147+
end
136148
end
137149

138150
M.stop = function()

test/helpers.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,12 @@ helpers.update_lua_env_variables = function(server)
130130
ROOT .. '/.rocks/lib/tarantool/?/?.so;'
131131
end
132132

133+
helpers.tcp_connection_exists = function(host, port)
134+
local tcp = socket.tcp()
135+
tcp:settimeout(0.3)
136+
local ok, _ = tcp:connect(host, port)
137+
tcp:close()
138+
return ok
139+
end
140+
133141
return helpers

test/integration/httpd_role_test.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,17 @@ g.test_httpd_role_usage = function(cg)
115115
'return require("test.mocks.mock_role").get_server_port(2)'
116116
), 13001)
117117
end
118+
119+
g.test_stop_server_after_remove = function(cg)
120+
local resp = http_client:get('http://localhost:13001/ping')
121+
t.assert_equals(resp.status, 200, 'response not 200')
122+
t.assert_equals(resp.body, 'pong')
123+
124+
local cfg = table.deepcopy(config)
125+
cfg.groups['group-001'].replicasets['replicaset-001'].roles_cfg['roles.httpd'].additional = nil
126+
treegen.write_file(cg.server.chdir, 'config.yaml', yaml.encode(cfg))
127+
local _, err = cg.server:eval("require('config'):reload()")
128+
t.assert_not(err)
129+
130+
t.assert_not(helpers.tcp_connection_exists('localhost', 13001))
131+
end

test/mocks/mock_role.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ M.apply = function(conf)
88
for _, server in pairs(conf) do
99
servers[server.id] = require('roles.httpd').get_server(server.name)
1010

11-
servers[server.id]:route({
12-
path = '/ping',
13-
}, function(tx)
14-
return tx:render({text = 'pong'})
15-
end)
11+
if servers[server.id] ~= nil then
12+
servers[server.id]:route({
13+
path = '/ping',
14+
}, function(tx)
15+
return tx:render({text = 'pong'})
16+
end)
17+
end
1618
end
1719
end
1820

test/unit/httpd_role_test.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,26 @@ g['test_get_server_bad_type'] = function()
268268
t.assert_not(ok)
269269
t.assert_str_contains(res, '?string expected, got table')
270270
end
271+
272+
g.test_stop_unusable_servers = function()
273+
local cfg = {
274+
[httpd_role.DEFAULT_SERVER_NAME] = {
275+
listen = 13001,
276+
},
277+
additional = {
278+
listen = 13002,
279+
},
280+
}
281+
282+
httpd_role.apply(cfg)
283+
for name, body in pairs(cfg) do
284+
local res = httpd_role.get_server(name)
285+
t.assert(res)
286+
t.assert_equals(res.port, body.listen)
287+
end
288+
289+
cfg.additional = nil
290+
httpd_role.apply(cfg)
291+
t.assert(httpd_role.get_server())
292+
t.assert_equals(httpd_role.get_server('additional'))
293+
end

0 commit comments

Comments
 (0)