Add comments on EvalTimestamp

Signed-off-by: Dimitar Dimitrov <dimitar.dimitrov@grafana.com>
This commit is contained in:
Dimitar Dimitrov 2022-10-12 14:12:03 +02:00
parent 3fb881af26
commit 03ab8dcca0
No known key found for this signature in database
GPG Key ID: 4541B04E6C90EBC3
1 changed files with 21 additions and 2 deletions

View File

@ -532,9 +532,28 @@ func (g *Group) setLastEvaluation(ts time.Time) {
// EvalTimestamp returns the immediately preceding consistently slotted evaluation time.
func (g *Group) EvalTimestamp(startTime int64) time.Time {
offset := (uint64(startTime) - g.hash()) % uint64(g.interval)
var (
offset = int64(g.hash() % uint64(g.interval))
return time.Unix(0, startTime-int64(offset)).UTC()
// This group's evaluation times differ from the perfect time intervals by `offset` nanoseconds.
// But we can only use `% interval` to align with the interval. And `% interval` will always
// align with the perfect time intervals, instead of this group's. Because of this we add
// `offset` _after_ aligning with the perfect time interval.
//
// There can be cases where adding `offset` to the perfect evaluation time can yield a
// timestamp in the future, which is not what EvalTimestamp should do.
// So we subtract one `offset` to make sure that `now - (now % interval) + offset` gives an
// evaluation time in the past.
adjNow = startTime - offset
// Adjust to perfect evaluation intervals.
base = adjNow - (adjNow % int64(g.interval))
// Add one offset to randomize the evaluation times of this group.
next = base + offset
)
return time.Unix(0, next).UTC()
}
func nameAndLabels(rule Rule) string {