Skip to content

Commit 047fafb

Browse files
committed
api: fix address already in use error
There was a bug when user wants to change server's `listen` address and reload config on the fly - error `address already in use` occures. This patch reworks server's address handling by applying roles' config. Closes #34
1 parent 3396bc0 commit 047fafb

File tree

3 files changed

+129
-20
lines changed

3 files changed

+129
-20
lines changed

CHANGELOG.md

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

1212
### Fixed
1313

14+
- `Address already in use` error on change role's config on the fly (#34).
15+
1416
### Changed
1517

1618
- Unclear error message when `roles.httpd` config is not applied yet (#33).

roles/metrics-export.lua

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ local function apply_http(conf)
329329
local host, port, target
330330
if node.server ~= nil then
331331
target = {
332-
value = node.server,
333-
is_httpd_role = true,
332+
value = 'httpd_' .. node.server,
333+
httpd_name = node.server,
334334
}
335335
elseif node.listen ~= nil then
336336
local err
@@ -339,23 +339,38 @@ local function apply_http(conf)
339339
error("failed to parse URI: " .. err, 2)
340340
end
341341
target = {
342-
value = node.listen,
343-
is_httpd_role = false,
342+
value = 'listen_' .. host .. ':' .. tostring(port),
344343
}
345344
else
346345
target = {
347-
value = httpd_role.DEFAULT_SERVER_NAME,
348-
is_httpd_role = true,
346+
value = 'httpd_' .. httpd_role.DEFAULT_SERVER_NAME,
347+
httpd_name = httpd_role.DEFAULT_SERVER_NAME,
349348
}
350349
end
351350

352351
http_servers = http_servers or {}
352+
353353
-- Since the 'listen' and 'server' names of other servers in the config may be
354-
-- the same, we create a unique string concatenating the key name and information
355-
-- about whether it is an httpd key or not.
356-
enabled[tostring(target.value) .. tostring(target.is_httpd_role)] = true
354+
-- the same, we create a unique string concatenating the key (which is listen or server)
355+
-- and its value.
356+
enabled[target.value] = true
357+
358+
if http_servers[target.value] == nil then
359+
if target.httpd_name == nil then
360+
if host == '0.0.0.0' then
361+
for k in pairs(http_servers) do
362+
if string.find(k, ':' .. port) ~= nil then
363+
http_servers[k].httpd:stop()
364+
enabled[k] = false
365+
end
366+
end
367+
elseif http_servers['listen_0.0.0.0:' .. port] ~= nil then
368+
local key = 'listen_0.0.0.0:' .. port
369+
http_servers[key].httpd:stop()
370+
enabled[key] = false
371+
end
372+
end
357373

358-
if http_servers[tostring(target.value) .. tostring(target.is_httpd_role)] == nil then
359374
local httpd
360375
if node.listen ~= nil then
361376
httpd = http_server.new(host, port, {
@@ -368,21 +383,21 @@ local function apply_http(conf)
368383
})
369384
httpd:start()
370385
else
371-
httpd = httpd_role.get_server(target.value)
386+
httpd = httpd_role.get_server(target.httpd_name)
372387
if httpd == nil then
373388
error(('failed to get server by name %q, check that roles.httpd was' ..
374-
' already applied'):format(target.value))
389+
' already applied'):format(target.httpd_name))
375390
end
376391
end
377392

378-
http_servers[tostring(target.value) .. tostring(target.is_httpd_role)] = {
393+
http_servers[target.value] = {
379394
httpd = httpd,
380395
routes = {},
381-
is_httpd_role = target.is_httpd_role,
396+
httpd_name = target.httpd_name,
382397
}
383398
end
384399

385-
local server = http_servers[tostring(target.value) .. tostring(target.is_httpd_role)]
400+
local server = http_servers[target.value]
386401
local httpd = server.httpd
387402
local old_routes = server.routes
388403

@@ -422,14 +437,16 @@ local function apply_http(conf)
422437
end
423438
end
424439

425-
for target, server in pairs(http_servers) do
440+
for target, server in pairs(http_servers or {}) do
426441
if not enabled[target] then
427-
if server.is_httpd_role then
442+
if server.httpd_name ~= nil then
428443
for path, _ in pairs(server.routes) do
429444
server.httpd:delete(path)
430445
end
431446
else
432-
server.httpd:stop()
447+
if server.httpd.is_run == true then
448+
server.httpd:stop()
449+
end
433450
end
434451
http_servers[target] = nil
435452
end
@@ -438,12 +455,14 @@ end
438455

439456
local function stop_http()
440457
for _, server in pairs(http_servers or {}) do
441-
if server.is_httpd_role then
458+
if server.httpd_name ~= nil then
442459
for path, _ in pairs(server.routes) do
443460
server.httpd:delete(path)
444461
end
445462
else
446-
server.httpd:stop()
463+
if server.httpd.is_run == true then
464+
server.httpd:stop()
465+
end
447466
end
448467
end
449468
http_servers = nil
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
local fio = require('fio')
2+
local yaml = require('yaml')
3+
local socket = require('socket')
4+
local helpers = require('test.helpers')
5+
local server = require('test.helpers.server')
6+
7+
local t = require('luatest')
8+
local g = t.group()
9+
10+
g.before_all(function()
11+
helpers.skip_if_unsupported()
12+
end)
13+
14+
g.before_each(function(cg)
15+
cg.workdir = fio.tempdir()
16+
fio.mktree(cg.workdir)
17+
18+
fio.copytree(".rocks", fio.pathjoin(cg.workdir, ".rocks"))
19+
fio.copytree("roles", fio.pathjoin(cg.workdir, "roles"))
20+
fio.copytree(fio.pathjoin("test", "ssl_data"), fio.pathjoin(cg.workdir, "ssl_data"))
21+
fio.copyfile(fio.pathjoin('test', 'entrypoint', 'config.yaml'), cg.workdir)
22+
end)
23+
24+
g.after_each(function(cg)
25+
cg.server:stop()
26+
fio.rmtree(cg.workdir)
27+
end)
28+
29+
local function is_tcp_connect(host, port)
30+
local tcp = socket.tcp()
31+
tcp:settimeout(0.3)
32+
local ok, _ = tcp:connect(host, port)
33+
tcp:close()
34+
35+
return ok
36+
end
37+
38+
local function change_listen_target_in_config(cg, old_addr, new_addr)
39+
local file = fio.open(fio.pathjoin(cg.workdir, 'config.yaml'), {'O_RDONLY'})
40+
t.assert(file ~= nil)
41+
42+
local cfg = file:read()
43+
file:close()
44+
45+
cfg = yaml.decode(cfg)
46+
local export_instances = cfg.groups['group-001'].replicasets['replicaset-001'].
47+
instances.master.roles_cfg['roles.metrics-export'].http
48+
49+
for i, v in pairs(export_instances) do
50+
if v.listen ~= nil and v.listen == old_addr then
51+
export_instances[i].listen = new_addr
52+
end
53+
end
54+
55+
file = fio.open(fio.pathjoin(cg.workdir, 'config.yaml'), {'O_CREAT', 'O_WRONLY', 'O_TRUNC'}, tonumber('644', 8))
56+
file:write(yaml.encode(cfg))
57+
file:close()
58+
end
59+
60+
g.test_reload_config = function(cg)
61+
cg.server = server:new({
62+
config_file = fio.pathjoin(cg.workdir, 'config.yaml'),
63+
chdir = cg.workdir,
64+
alias = 'master',
65+
workdir = cg.workdir,
66+
})
67+
68+
cg.server:start({wait_until_ready = true})
69+
70+
local file = fio.open(fio.pathjoin(cg.workdir, 'config.yaml'), {'O_RDONLY'})
71+
t.assert(file ~= nil)
72+
73+
t.assert(is_tcp_connect('127.0.0.1', 8082))
74+
t.assert_not(is_tcp_connect('127.0.0.2', 8082))
75+
76+
change_listen_target_in_config(cg, '127.0.0.1:8082', '0.0.0.0:8082')
77+
cg.server:eval("require('config'):reload()")
78+
79+
t.assert(is_tcp_connect('127.0.0.1', 8082))
80+
t.assert(is_tcp_connect('127.0.0.2', 8082))
81+
t.assert(is_tcp_connect('127.1.2.3', 8082))
82+
83+
change_listen_target_in_config(cg, '0.0.0.0:8082', '127.0.0.1:8082')
84+
cg.server:eval("require('config'):reload()")
85+
86+
t.assert_not(is_tcp_connect('127.0.0.2', 8082))
87+
t.assert(is_tcp_connect('127.0.0.1', 8082))
88+
end

0 commit comments

Comments
 (0)