make aggregations deterministic (#9459)

* Add deterministic test for aggregations

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>

* Make aggregations deterministic

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>

* Increase testing

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
pull/9528/head
Julien Pivotto 3 years ago committed by GitHub
parent 94a9156cc4
commit a18224d02d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2138,6 +2138,7 @@ type groupedAggregation struct {
func (ev *evaluator) aggregation(op parser.ItemType, grouping []string, without bool, param interface{}, vec Vector, seriesHelper []EvalSeriesHelper, enh *EvalNodeHelper) Vector { func (ev *evaluator) aggregation(op parser.ItemType, grouping []string, without bool, param interface{}, vec Vector, seriesHelper []EvalSeriesHelper, enh *EvalNodeHelper) Vector {
result := map[uint64]*groupedAggregation{} result := map[uint64]*groupedAggregation{}
orderedResult := []*groupedAggregation{}
var k int64 var k int64
if op == parser.TOPK || op == parser.BOTTOMK { if op == parser.TOPK || op == parser.BOTTOMK {
f := param.(float64) f := param.(float64)
@ -2206,12 +2207,16 @@ func (ev *evaluator) aggregation(op parser.ItemType, grouping []string, without
} else { } else {
m = metric.WithLabels(grouping...) m = metric.WithLabels(grouping...)
} }
result[groupingKey] = &groupedAggregation{ newAgg := &groupedAggregation{
labels: m, labels: m,
value: s.V, value: s.V,
mean: s.V, mean: s.V,
groupCount: 1, groupCount: 1,
} }
result[groupingKey] = newAgg
orderedResult = append(orderedResult, newAgg)
inputVecLen := int64(len(vec)) inputVecLen := int64(len(vec))
resultSize := k resultSize := k
if k > inputVecLen { if k > inputVecLen {
@ -2333,7 +2338,7 @@ func (ev *evaluator) aggregation(op parser.ItemType, grouping []string, without
} }
// Construct the result Vector from the aggregated groups. // Construct the result Vector from the aggregated groups.
for _, aggr := range result { for _, aggr := range orderedResult {
switch op { switch op {
case parser.AVG: case parser.AVG:
aggr.value = aggr.mean aggr.value = aggr.mean

@ -497,3 +497,13 @@ eval instant at 1m avg(data{test="-big"})
eval instant at 1m avg(data{test="bigzero"}) eval instant at 1m avg(data{test="bigzero"})
{} 0 {} 0
clear
# Test that aggregations are deterministic.
load 10s
up{job="prometheus"} 1
up{job="prometheus2"} 1
eval instant at 1m count(topk(1,max(up) without()) == topk(1,max(up) without()) == topk(1,max(up) without()) == topk(1,max(up) without()) == topk(1,max(up) without()))
{} 1

Loading…
Cancel
Save