Skip to content

Commit

Permalink
config: move role start and stop to _post_apply()
Browse files Browse the repository at this point in the history
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

(cherry picked from commit ebb170c)
  • Loading branch information
ImeevMA authored and Totktonada committed Feb 7, 2024
1 parent 19c06ac commit 2109a94
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
4 changes: 4 additions & 0 deletions changelogs/unreleased/gh-9649-config-get-in-roles.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## bugfix/config

* Calling `require('config'):get()` in role code now returns the configuration
that is currently applied (gh-9649).
13 changes: 9 additions & 4 deletions src/box/lua/config/applier/roles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ local log = require('internal.config.utils.log')
local last_loaded = {}
local last_roles_ordered = {}

local function apply(_config)
log.verbose('roles.apply: do nothing')
end

local function stop_roles(roles_to_skip)
local roles_to_stop = {}
for id = #last_roles_ordered, 1, -1 do
Expand Down Expand Up @@ -44,7 +48,7 @@ local function stop_roles(roles_to_skip)
end
end
for _, role_name in ipairs(roles_to_stop) do
log.verbose('roles.apply: stop role ' .. role_name)
log.verbose('roles.post_apply: stop role ' .. role_name)
local ok, err = pcall(last_loaded[role_name].stop)
if not ok then
error(('Error stopping role %s: %s'):format(role_name, err), 0)
Expand Down Expand Up @@ -106,7 +110,7 @@ local function resort_roles(original_order, roles)
return ordered
end

local function apply(config)
local function post_apply(config)
local configdata = config._configdata
local role_names = configdata:get('roles', {use_default = true})
if role_names == nil or next(role_names) == nil then
Expand All @@ -132,7 +136,7 @@ local function apply(config)
for _, role_name in ipairs(roles_ordered) do
local role = last_loaded[role_name]
if not role then
log.verbose('roles.apply: load role ' .. role_name)
log.verbose('roles.post_apply: load role ' .. role_name)
role = require(role_name)
local funcs = {'validate', 'apply', 'stop'}
for _, func_name in pairs(funcs) do
Expand Down Expand Up @@ -164,7 +168,7 @@ local function apply(config)

-- Apply configs for all roles.
for _, role_name in ipairs(roles_ordered) do
log.verbose('roles.apply: apply config for role ' .. role_name)
log.verbose('roles.post_apply: apply config for role ' .. role_name)
local ok, err = pcall(loaded[role_name].apply, roles_cfg[role_name])
if not ok then
error(('Error applying role %s: %s'):format(role_name, err), 0)
Expand All @@ -178,4 +182,5 @@ end
return {
name = 'roles',
apply = apply,
post_apply = post_apply,
}
44 changes: 44 additions & 0 deletions test/config-luatest/roles_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,47 @@ g.test_role_dependencies_stop_required_role = function(g)
'"one", "two" depend on it'
})
end

-- Make sure that role was started after config was fully loaded.
g.test_role_started_and_stopped_after_config_loaded = function(g)
local one = string.dump(function()
local function apply()
local cfg = require('config')
local state = rawget(_G, 'state')
table.insert(state, {'start', cfg:info().status, cfg:get('roles')})
end

local function stop()
local cfg = require('config')
local state = rawget(_G, 'state')
table.insert(state, {'stop', cfg:info().status, cfg:get('roles')})
end

rawset(_G, 'state', {})

return {
validate = function() end,
apply = apply,
stop = stop,
}
end)
local verify = function()
local exp = {{'start', 'ready', {'one'}}}
t.assert_equals(rawget(_G, 'state'), exp)
end

local verify_2 = function()
local exp = {{'start', 'ready', {'one'}}, {'stop', 'ready'}}
t.assert_equals(rawget(_G, 'state'), exp)
end

helpers.reload_success_case(g, {
roles = {one = one},
options = {
['roles'] = {'one'}
},
options_2 = {},
verify = verify,
verify_2 = verify_2,
})
end

0 comments on commit 2109a94

Please sign in to comment.