2016-06-06 03:31:21 -05:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
import (
|
2016-09-05 07:26:08 -05:00
|
|
|
"math"
|
2016-06-06 03:31:21 -05:00
|
|
|
"time"
|
|
|
|
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2016-10-10 06:29:10 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2020-01-14 03:13:34 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2016-06-06 03:31:21 -05:00
|
|
|
)
|
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
type schedulerImpl struct {
|
2016-07-27 09:29:28 -05:00
|
|
|
jobs map[int64]*Job
|
2016-06-10 03:00:00 -05:00
|
|
|
log log.Logger
|
2016-06-06 03:31:21 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
func newScheduler() scheduler {
|
|
|
|
return &schedulerImpl{
|
2018-04-16 13:22:12 -05:00
|
|
|
jobs: make(map[int64]*Job),
|
2016-06-10 03:00:00 -05:00
|
|
|
log: log.New("alerting.scheduler"),
|
2016-06-06 03:31:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
func (s *schedulerImpl) Update(rules []*Rule) {
|
2016-08-01 03:07:00 -05:00
|
|
|
s.log.Debug("Scheduling update", "ruleCount", len(rules))
|
2016-06-06 03:31:21 -05:00
|
|
|
|
2018-04-16 13:22:12 -05:00
|
|
|
jobs := make(map[int64]*Job)
|
2016-06-06 03:31:21 -05:00
|
|
|
|
2016-07-27 09:29:28 -05:00
|
|
|
for i, rule := range rules {
|
|
|
|
var job *Job
|
2019-06-03 03:25:58 -05:00
|
|
|
if s.jobs[rule.ID] != nil {
|
|
|
|
job = s.jobs[rule.ID]
|
2016-06-06 03:31:21 -05:00
|
|
|
} else {
|
2019-09-03 08:14:28 -05:00
|
|
|
job = &Job{}
|
|
|
|
job.SetRunning(false)
|
2016-06-06 03:31:21 -05:00
|
|
|
}
|
|
|
|
|
2016-06-06 07:24:14 -05:00
|
|
|
job.Rule = rule
|
2016-09-16 07:58:10 -05:00
|
|
|
|
|
|
|
offset := ((rule.Frequency * 1000) / int64(len(rules))) * int64(i)
|
|
|
|
job.Offset = int64(math.Floor(float64(offset) / 1000))
|
2020-09-22 09:22:19 -05:00
|
|
|
if job.Offset == 0 { // zero offset causes division with 0 panics.
|
2016-11-17 08:20:14 -06:00
|
|
|
job.Offset = 1
|
|
|
|
}
|
2019-06-03 03:25:58 -05:00
|
|
|
jobs[rule.ID] = job
|
2016-06-06 03:31:21 -05:00
|
|
|
}
|
|
|
|
|
2016-06-06 06:50:47 -05:00
|
|
|
s.jobs = jobs
|
2016-06-06 03:31:21 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
func (s *schedulerImpl) Tick(tickTime time.Time, execQueue chan *Job) {
|
2016-06-06 04:56:58 -05:00
|
|
|
now := tickTime.Unix()
|
|
|
|
|
2016-06-06 06:50:47 -05:00
|
|
|
for _, job := range s.jobs {
|
2019-09-03 08:14:28 -05:00
|
|
|
if job.GetRunning() || job.Rule.State == models.AlertStatePaused {
|
2016-09-05 07:26:08 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if job.OffsetWait && now%job.Offset == 0 {
|
|
|
|
job.OffsetWait = false
|
2018-04-13 11:40:14 -05:00
|
|
|
s.enqueue(job, execQueue)
|
2016-09-05 07:26:08 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-14 03:13:34 -06:00
|
|
|
// Check the job frequency against the minimum interval required
|
|
|
|
interval := job.Rule.Frequency
|
|
|
|
if interval < setting.AlertingMinInterval {
|
|
|
|
interval = setting.AlertingMinInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
if now%interval == 0 {
|
2016-09-05 07:26:08 -05:00
|
|
|
if job.Offset > 0 {
|
|
|
|
job.OffsetWait = true
|
|
|
|
} else {
|
2018-04-13 11:40:14 -05:00
|
|
|
s.enqueue(job, execQueue)
|
2016-09-05 07:26:08 -05:00
|
|
|
}
|
2016-06-06 03:31:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-05 07:26:08 -05:00
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
func (s *schedulerImpl) enqueue(job *Job, execQueue chan *Job) {
|
2019-06-03 03:25:58 -05:00
|
|
|
s.log.Debug("Scheduler: Putting job on to exec queue", "name", job.Rule.Name, "id", job.Rule.ID)
|
2016-09-05 07:26:08 -05:00
|
|
|
execQueue <- job
|
|
|
|
}
|