Alerting: remove State cache entries on Ruler Delete (#33638)

for https://github.com/grafana/alerting-squad/issues/133
This commit is contained in:
Kyle Brandt
2021-05-03 14:01:33 -04:00
committed by GitHub
parent 0609b80fdc
commit 48358efc13
5 changed files with 61 additions and 12 deletions

View File

@@ -113,6 +113,17 @@ func (c *cache) getStatesByRuleUID() map[string][]*State {
return ruleMap
}
// removeByRuleUID deletes all entries in the state cache that match the given UID.
func (c *cache) removeByRuleUID(orgID int64, uid string) {
c.mtxStates.Lock()
defer c.mtxStates.Unlock()
for k, state := range c.states {
if state.AlertRuleUID == uid && state.OrgID == orgID {
delete(c.states, k)
}
}
}
func (c *cache) reset() {
c.mtxStates.Lock()
defer c.mtxStates.Unlock()

View File

@@ -45,11 +45,16 @@ func (st *Manager) Get(id string) (*State, error) {
return st.cache.get(id)
}
//Used to ensure a clean cache on startup
// ResetCache is used to ensure a clean cache on startup.
func (st *Manager) ResetCache() {
st.cache.reset()
}
// RemoveByRuleUID deletes all entries in the state manager that match the given rule UID.
func (st *Manager) RemoveByRuleUID(orgID int64, ruleUID string) {
st.cache.removeByRuleUID(orgID, ruleUID)
}
func (st *Manager) ProcessEvalResults(alertRule *ngModels.AlertRule, results eval.Results) []*State {
st.Log.Debug("state manager processing evaluation results", "uid", alertRule.UID, "resultCount", len(results))
var states []*State