From 2109a94d3f0fb95ca7119656bd9348920c2cc761 Mon Sep 17 00:00:00 2001 From: Mergen Imeev Date: Mon, 5 Feb 2024 16:29:11 +0300 Subject: [PATCH] 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 #9649 NO_DOC=bugfix (cherry picked from commit ebb170cb8cf2b9c4634bcf0178665909f578c335) --- .../unreleased/gh-9649-config-get-in-roles.md | 4 ++ src/box/lua/config/applier/roles.lua | 13 ++++-- test/config-luatest/roles_test.lua | 44 +++++++++++++++++++ 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 changelogs/unreleased/gh-9649-config-get-in-roles.md diff --git a/changelogs/unreleased/gh-9649-config-get-in-roles.md b/changelogs/unreleased/gh-9649-config-get-in-roles.md new file mode 100644 index 000000000000..406f2610eed1 --- /dev/null +++ b/changelogs/unreleased/gh-9649-config-get-in-roles.md @@ -0,0 +1,4 @@ +## bugfix/config + +* Calling `require('config'):get()` in role code now returns the configuration + that is currently applied (gh-9649). diff --git a/src/box/lua/config/applier/roles.lua b/src/box/lua/config/applier/roles.lua index d965c129b464..93b863d1d023 100644 --- a/src/box/lua/config/applier/roles.lua +++ b/src/box/lua/config/applier/roles.lua @@ -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 @@ -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) @@ -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 @@ -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 @@ -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) @@ -178,4 +182,5 @@ end return { name = 'roles', apply = apply, + post_apply = post_apply, } diff --git a/test/config-luatest/roles_test.lua b/test/config-luatest/roles_test.lua index 0695103b5866..ace7a3443e94 100644 --- a/test/config-luatest/roles_test.lua +++ b/test/config-luatest/roles_test.lua @@ -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