Skip to content

Commit

Permalink
config: introduce droppable alerts
Browse files Browse the repository at this point in the history
For now it's impossible to drop created alert in any way except
manual searching for _alerts table. However, we need to drop alerts
on missing names, when the names are set.

Let's introduce simple key-value alerts in order to easily drop them
by key.

Needed for tarantool#8978

NO_DOC=internal
NO_TEST=following commit
NO_CHANGELOG=internal
  • Loading branch information
Serpentian committed Oct 26, 2023
1 parent 9c675f6 commit c869fd0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/box/lua/config/applier/box_cfg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ local function apply(config)
if v ~= box.cfg[k] then
local warning = 'box_cfg.apply: non-dynamic option '..k..
' will not be set until the instance is restarted'
config:_alert({type = 'warn', message = warning})
config:_alert('box_cfg_apply_non_dynamic',
{type = 'warn', message = warning})
box_cfg[k] = nil
end
end
Expand Down
16 changes: 10 additions & 6 deletions src/box/lua/config/applier/credentials.lua
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,15 @@ local privileges_action_f = function(grant_or_revoke, role_or_user, name, privs,
err = ('credentials.apply: box.schema.%s.%s(%q, %q, %q, %q) failed: %s')
:format(role_or_user, grant_or_revoke, name, privs, obj_type,
obj_name, err)
config:_alert({type = 'error', message = err})
config:_alert('credentials_grant_or_revoke_err',
{type = 'error', message = err})
else
local msg = "credentials.apply: %s %q hasn't been created yet, " ..
"'box.schema.%s.%s(%q, %q, %q, %q)' will be applied later"
msg = msg:format(obj_type, obj_name, role_or_user, grant_or_revoke,
name, privs, obj_type, obj_name)
config:_alert({type = 'warn', message = msg})
config:_alert('credentials_grant_or_revoke_warn',
{type = 'warn', message = msg})
end
end

Expand Down Expand Up @@ -621,9 +623,10 @@ local function set_password(user_name, password)
end

if user_name == 'guest' then
config:_alert({type = 'error',
message = 'credentials.apply: setting a password for ' ..
'the guest user is not allowed'})
local message = 'credentials.apply: setting a password for ' ..
'the guest user is not allowed'
config:_alert('credentials_apply_password',
{type = 'error', message = message})
end

local auth_def = box.space._user.index.name:get({user_name})[5]
Expand Down Expand Up @@ -729,7 +732,8 @@ local function sync_credentials_worker()
local msg = 'credentials.apply: Tarantool is in Read Only ' ..
'mode, so credentials will be set up in the ' ..
'background when it is switched to Read Write mode'
config:_alert({type = 'warn', message = msg})
config:_alert('credentials_apply_ro',
{type = 'warn', message = msg})
box.ctl.wait_rw()
end

Expand Down
24 changes: 20 additions & 4 deletions src/box/lua/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,22 @@ local function broadcast(self)
box.broadcast('config.info', self:info())
end

function methods._alert(self, alert)
function methods._alert(self, key, alert)
assert(alert.type == 'error' or alert.type == 'warn')
if alert.type == 'error' then
log.error(alert.message)
else
log.warn(alert.message)
end
alert.timestamp = datetime.now()
table.insert(self._alerts, alert)
self._alerts[key] = alert
end

function methods._alert_drop(self, key)
self._alerts[key] = nil
if self._status == 'check_warnings' and table.equals(self._alerts, {}) then
self._status = 'ready'
end
end

function methods._meta(self, source_name, key, value)
Expand Down Expand Up @@ -446,7 +453,7 @@ function methods._reload_noexc(self, opts)

assert(not ok or err == nil)
if not ok then
self:_alert({type = 'error', message = err})
self:_alert('reload_error', {type = 'error', message = err})
end

self:_set_status_based_on_alerts()
Expand All @@ -464,8 +471,17 @@ end

function methods.info(self)
selfcheck(self, 'info')
-- Don't return alert keys from config:info().
local alerts = {}
for _, alert in pairs(self._alerts) do
table.insert(alerts, alert)
end
table.sort(alerts, function(a, b)
return a.timestamp < b.timestamp
end)

return {
alerts = self._alerts,
alerts = alerts,
meta = self._metadata,
status = self._status,
}
Expand Down

0 comments on commit c869fd0

Please sign in to comment.