Skip to content

Commit

Permalink
optimize the flush function by deleting alerts based on their id (#1054)
Browse files Browse the repository at this point in the history
  • Loading branch information
blotus authored Nov 17, 2021
1 parent 0652e9e commit dd03d07
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions pkg/database/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,34 +850,35 @@ func (c *Client) FlushAlerts(MaxAge string, MaxItems int) error {
deletedByAge = nbDeleted
}
if MaxItems > 0 {
if totalAlerts > MaxItems {
nbToDelete := totalAlerts - MaxItems
batchSize := 500
if batchSize > nbToDelete {
batchSize = nbToDelete
}
deleted := 0
for deleted < nbToDelete {
c.Log.Debugf("FlushAlerts (before query with filter) to delete: %d", nbToDelete)
alerts, err := c.QueryAlertWithFilter(map[string][]string{
"sort": {"ASC"},
"limit": {strconv.Itoa(batchSize)},
}) // we want to delete older alerts if we reach the max number of items
if err != nil {
c.Log.Warningf("FlushAlerts (max items query) : %s", err)
return errors.Wrap(err, "unable to get all alerts")
}
deletedAlerts, err := c.DeleteAlertGraphBatch(alerts)
//We get the highest id for the alerts
//We substract MaxItems to avoid deleting alerts that are not old enough
//This gives us the oldest alert that we want to keep
//We then delete all the alerts with an id lower than this one
//We can do this because the id is auto-increment, and the database won't reuse the same id twice
lastAlert, err := c.QueryAlertWithFilter(map[string][]string{
"sort": {"DESC"},
"limit": {"1"},
})
c.Log.Debugf("FlushAlerts (last alert): %+v", lastAlert)
if err != nil {
c.Log.Errorf("FlushAlerts: could not get last alert: %s", err)
return errors.Wrap(err, "could not get last alert")
}

if len(lastAlert) != 0 {
maxid := lastAlert[0].ID - MaxItems

c.Log.Debugf("FlushAlerts (max id): %d", maxid)

if maxid > 0 {
//This may lead to orphan alerts (at least on MySQL), but the next time the flush job will run, they will be deleted
deletedByNbItem, err = c.Ent.Alert.Delete().Where(alert.IDLT(maxid)).Exec(c.CTX)

if err != nil {
c.Log.Warningf("FlushAlerts (max items query) : %s", err)
return errors.Wrap(err, "unable to delete alerts")
}
deleted += deletedAlerts
if nbToDelete-deleted < batchSize {
batchSize = nbToDelete - deleted
c.Log.Errorf("FlushAlerts: Could not delete alerts : %s", err)
return errors.Wrap(err, "could not delete alerts")
}
}
deletedByNbItem = deleted
}
}
if deletedByNbItem > 0 {
Expand Down

0 comments on commit dd03d07

Please sign in to comment.