Skip to content

Commit 906dba3

Browse files
committed
config: move role start and stop to _post_apply()
Roles are now started and stopped at the "post_apply" stage rather than at the "apply" stage. This allows require('config'):get() to correctly return the configuration that is being applied. Closes tarantool#9649 NO_DOC=bugfix
1 parent 9e9ea6d commit 906dba3

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## bugfix/config
2+
3+
* Calling `require('config'):get()` in role code now returns the configuration
4+
that is currently applied (gh-9649).

src/box/lua/config/applier/roles.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ local log = require('internal.config.utils.log')
33
local last_loaded = {}
44
local last_roles_ordered = {}
55

6+
local function apply(_config)
7+
log.verbose('roles.apply: do nothing')
8+
end
9+
610
local function stop_roles(roles_to_skip)
711
local roles_to_stop = {}
812
for id = #last_roles_ordered, 1, -1 do
@@ -44,7 +48,7 @@ local function stop_roles(roles_to_skip)
4448
end
4549
end
4650
for _, role_name in ipairs(roles_to_stop) do
47-
log.verbose('roles.apply: stop role ' .. role_name)
51+
log.verbose('roles.post_apply: stop role ' .. role_name)
4852
local ok, err = pcall(last_loaded[role_name].stop)
4953
if not ok then
5054
error(('Error stopping role %s: %s'):format(role_name, err), 0)
@@ -106,7 +110,7 @@ local function resort_roles(original_order, roles)
106110
return ordered
107111
end
108112

109-
local function apply(config)
113+
local function post_apply(config)
110114
local configdata = config._configdata
111115
local role_names = configdata:get('roles', {use_default = true})
112116
if role_names == nil or next(role_names) == nil then
@@ -132,7 +136,7 @@ local function apply(config)
132136
for _, role_name in ipairs(roles_ordered) do
133137
local role = last_loaded[role_name]
134138
if not role then
135-
log.verbose('roles.apply: load role ' .. role_name)
139+
log.verbose('roles.post_apply: load role ' .. role_name)
136140
role = require(role_name)
137141
local funcs = {'validate', 'apply', 'stop'}
138142
for _, func_name in pairs(funcs) do
@@ -164,7 +168,7 @@ local function apply(config)
164168

165169
-- Apply configs for all roles.
166170
for _, role_name in ipairs(roles_ordered) do
167-
log.verbose('roles.apply: apply config for role ' .. role_name)
171+
log.verbose('roles.post_apply: apply config for role ' .. role_name)
168172
local ok, err = pcall(loaded[role_name].apply, roles_cfg[role_name])
169173
if not ok then
170174
error(('Error applying role %s: %s'):format(role_name, err), 0)
@@ -178,4 +182,5 @@ end
178182
return {
179183
name = 'roles',
180184
apply = apply,
185+
post_apply = post_apply,
181186
}

test/config-luatest/roles_test.lua

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,47 @@ g.test_role_dependencies_stop_required_role = function(g)
572572
'"one", "two" depend on it'
573573
})
574574
end
575+
576+
-- Make sure that role was started after config was fully loaded.
577+
g.test_role_started_and_stopped_after_config_loaded = function(g)
578+
local one = string.dump(function()
579+
local function apply(cfg)
580+
local cfg = require('config')
581+
local state = rawget(_G, 'state')
582+
table.insert(state, {'start', cfg:info().status, cfg:get('roles')})
583+
end
584+
585+
local function stop(cfg)
586+
local cfg = require('config')
587+
local state = rawget(_G, 'state')
588+
table.insert(state, {'stop', cfg:info().status, cfg:get('roles')})
589+
end
590+
591+
rawset(_G, 'state', {})
592+
593+
return {
594+
validate = function() end,
595+
apply = apply,
596+
stop = stop,
597+
}
598+
end)
599+
local verify = function()
600+
local exp = {{'start', 'ready', {'one'}}}
601+
t.assert_equals(rawget(_G, 'state'), exp)
602+
end
603+
604+
local verify_2 = function()
605+
local exp = {{'start', 'ready', {'one'}}, {'stop', 'ready'}}
606+
t.assert_equals(rawget(_G, 'state'), exp)
607+
end
608+
609+
helpers.reload_success_case(g, {
610+
roles = {one = one},
611+
options = {
612+
['roles'] = {'one'}
613+
},
614+
options_2 = {},
615+
verify = verify,
616+
verify_2 = verify_2,
617+
})
618+
end

0 commit comments

Comments
 (0)