Alerting: Delete state from the database on reset (#53919)

* make ResetStatesByRuleUID return states
* delete rule states when reset
* rule eval routine to clean up the state only when rule is deleted
This commit is contained in:
Yuriy Tseretyan
2022-08-25 14:12:22 -04:00
committed by GitHub
parent 5c0bf2ba39
commit 03e746d9df
8 changed files with 101 additions and 44 deletions

View File

@@ -180,11 +180,20 @@ func (c *cache) getStatesForRuleUID(orgID int64, alertRuleUID string) []*State {
return ruleStates
}
// removeByRuleUID deletes all entries in the state cache that match the given UID.
func (c *cache) removeByRuleUID(orgID int64, uid string) {
// removeByRuleUID deletes all entries in the state cache that match the given UID. Returns removed states
func (c *cache) removeByRuleUID(orgID int64, uid string) []*State {
c.mtxStates.Lock()
defer c.mtxStates.Unlock()
statesMap := c.states[orgID][uid]
delete(c.states[orgID], uid)
if statesMap == nil {
return nil
}
states := make([]*State, 0, len(statesMap))
for _, state := range statesMap {
states = append(states, state)
}
return states
}
func (c *cache) reset() {