forked from tarantool/tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch introduces initial support for roles. Roles are programs that run when a configuration is loaded or reloaded. Roles can be defined for each instance. On first run, the role is initialized with the init() method. On each run, the role calls validate_config() and then apply_config() if validate_config() returns true. If the roles were removed from the instance on reboot, the role is stopped using the stop() method. If a role was added to an instance, then removed from the instance, and then added again, init() is still not called. If a role is defined more than once on an instance, it will still only run once. Roles must have four methods: init(), validate_config(), apply_config() and stop(). Each assigned role must be described in the roles_cfg file. Dependencies are not currently supported for roles. Part of tarantool#9078 NO_DOC=will be added later.
- Loading branch information
Showing
10 changed files
with
645 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## feature/config | ||
|
||
* Added initial support of roles (gh-9078). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
local log = require('internal.config.utils.log') | ||
|
||
local last_loaded = {} | ||
|
||
local function apply(_config) | ||
log.verbose('roles.apply: do nothing') | ||
end | ||
|
||
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 | ||
return | ||
end | ||
|
||
-- Remove duplicates. | ||
local roles = {} | ||
local roles_dup = {} | ||
local roles_check = {} | ||
for _, role_name in pairs(role_names) do | ||
table.insert(roles_dup, roles_check[role_name] ~= nil) | ||
roles_check[role_name] = true | ||
end | ||
for id, role_name in pairs(role_names) do | ||
if not roles_dup[id] then | ||
table.insert(roles, role_name) | ||
end | ||
end | ||
|
||
-- Run roles. | ||
local roles_cfg = configdata:get('roles_cfg', {use_default = true}) or {} | ||
local loaded = {} | ||
for _, role_name in pairs(roles) do | ||
local role_cfg = roles_cfg[role_name] | ||
-- All roles must be described in roles_cfg, even if the role does not | ||
-- require a cfg. | ||
if role_cfg == nil then | ||
error('No config for role ' .. role_name, 0) | ||
end | ||
local role = last_loaded[role_name] | ||
if not role then | ||
role = require(role_name) | ||
local methods = {'init', 'validate_config', 'apply_config', 'stop'} | ||
for _, method_name in pairs(methods) do | ||
if type(role[method_name]) ~= 'function' then | ||
local err = 'Role %s does not contain method %s' | ||
error(err:format(role_name, method_name), 0) | ||
end | ||
end | ||
log.verbose('roles.post_apply: initialize role ' .. role_name) | ||
role.init() | ||
end | ||
loaded[role_name] = role | ||
if not role.validate_config(role_cfg) then | ||
error('Wrong config for role ' .. role_name, 0) | ||
end | ||
log.verbose('roles.post_apply: apply config for role ' .. role_name) | ||
role.apply_config(role_cfg) | ||
end | ||
|
||
-- Stop removed roles. | ||
for role_name, role in pairs(last_loaded) do | ||
if not roles_check[role_name] then | ||
log.verbose('roles.post_apply: stop role ' .. role_name) | ||
role.stop() | ||
end | ||
end | ||
last_loaded = loaded | ||
end | ||
|
||
return { | ||
name = 'roles', | ||
apply = apply, | ||
post_apply = post_apply, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.