app/vmselect/promql: consistently return the same set of time series from limitk() function

This is the expected behaviour by most users.
This commit is contained in:
Aliaksandr Valialkin 2021-10-08 19:53:50 +03:00
parent 001750c239
commit 92b92d4d2c
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
4 changed files with 10 additions and 3 deletions

View file

@ -1010,6 +1010,11 @@ func aggrFuncLimitK(afa *aggrFuncArg) ([]*timeseries, error) {
}
}
afe := func(tss []*timeseries, modifier *metricsql.ModifierExpr) []*timeseries {
// Sort series by metricName in order to get consistent set of output series
// across multiple calls to limitk() function.
sort.Slice(tss, func(i, j int) bool {
return metricNameLess(&tss[i].MetricName, &tss[j].MetricName)
})
if len(tss) > maxK {
tss = tss[:maxK]
}

View file

@ -4993,7 +4993,7 @@ func TestExecSuccess(t *testing.T) {
})
t.Run(`limitk(1)`, func(t *testing.T) {
t.Parallel()
q := `limitk(1, label_set(10, "foo", "bar") or label_set(time()/150, "baz", "sss"))`
q := `limitk(1, label_set(10, "foo", "bar") or label_set(time()/150, "xbaz", "sss"))`
r1 := netstorage.Result{
MetricName: metricNameExpected,
Values: []float64{10, 10, 10, 10, 10, 10},

View file

@ -6,7 +6,9 @@ sort: 15
## tip
BUGFIX: vmstorage: fix `unaligned 64-bit atomic operation` panic on 32-bit architectures (arm and 386). The panic has been introduced in v1.67.0
* FEATURE: consistently return the same set of time series from [limitk](https://docs.victoriametrics.com/MetricsQL.html#limitk) function. This improves the usability of periodically refreshed graphs.
* BUGFIX: vmstorage: fix `unaligned 64-bit atomic operation` panic on 32-bit architectures (arm and 386). The panic has been introduced in v1.67.0
## [v1.67.0](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.67.0)

View file

@ -762,7 +762,7 @@ See also [implicit query conversions](#implicit-query-conversions).
#### limitk
`limitk(k, q) by (group_labels)` returns up to `k` time series per each `group_labels` out of time series returned by `q`. The returned set of time series can change with each call.
`limitk(k, q) by (group_labels)` returns up to `k` time series per each `group_labels` out of time series returned by `q`. The returned set of time series remain the same across calls.
#### mad