[v11.0.x] Alerting: Fix scheduler to sort rules before evaluation #88021
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backport 05d6813 from #88006
What is this feature?
This PR fixes Grafnaa Alerting scheduler to sort the rules that are scheduled for the current tick before spreading them out.
Why do we need this feature?
This is needed to guarantee that rule is consistently evaluated every X interval. The order of items scheduled for evaluation is important because the index of element is used to calculate a delay. That delay can be up to 10 second (base interval of tick). Therefore, one time rule can be evaluated at time X + 5 second, and the next time, for example, Y = X+eval interval, at Y + 2 seconds and therefore the rule is evaluated 3 seconds earlier than it is supposed to.
This is especially important in HA mode where we rely on the fact that rules are evaluated at the same time.
Who is this feature for?
Alerting users
Special notes for your reviewer:
One possible downside of this change is that now all instances in HA mode will send the same query at pretty much the same time, which may cause some contention of the data source side.
Please check that: