2015-05-11 06:56:35 -07:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package promql
|
|
|
|
|
|
|
|
import (
|
2017-10-24 21:21:42 -07:00
|
|
|
"context"
|
2015-05-11 06:56:35 -07:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-03-25 16:01:12 -07:00
|
|
|
"github.com/pkg/errors"
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2020-10-22 02:00:08 -07:00
|
|
|
|
2021-05-06 13:53:52 -07:00
|
|
|
"github.com/prometheus/prometheus/pkg/exemplar"
|
2016-12-25 02:34:22 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2021-01-20 02:57:39 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/timestamp"
|
2020-02-03 08:32:23 -08:00
|
|
|
"github.com/prometheus/prometheus/promql/parser"
|
2015-05-11 06:56:35 -07:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2020-04-29 09:16:14 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb"
|
2019-08-08 18:35:39 -07:00
|
|
|
"github.com/prometheus/prometheus/util/teststorage"
|
2015-05-29 04:30:30 -07:00
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2015-05-11 06:56:35 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
minNormal = math.Float64frombits(0x0010000000000000) // The smallest positive normal value of type float64.
|
|
|
|
|
|
|
|
patSpace = regexp.MustCompile("[\t ]+")
|
|
|
|
patLoad = regexp.MustCompile(`^load\s+(.+?)$`)
|
|
|
|
patEvalInstant = regexp.MustCompile(`^eval(?:_(fail|ordered))?\s+instant\s+(?:at\s+(.+?))?\s+(.+)$`)
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-12-23 04:51:59 -08:00
|
|
|
epsilon = 0.000001 // Relative error allowed for sample values.
|
2015-05-11 06:56:35 -07:00
|
|
|
)
|
|
|
|
|
2020-03-29 09:35:39 -07:00
|
|
|
var testStartTime = time.Unix(0, 0).UTC()
|
2016-12-23 04:51:59 -08:00
|
|
|
|
2015-05-11 06:56:35 -07:00
|
|
|
// Test is a sequence of read and write commands that are run
|
|
|
|
// against a test storage.
|
|
|
|
type Test struct {
|
2015-05-23 03:23:33 -07:00
|
|
|
testutil.T
|
2015-05-11 06:56:35 -07:00
|
|
|
|
|
|
|
cmds []testCommand
|
|
|
|
|
2020-04-29 09:16:14 -07:00
|
|
|
storage *teststorage.TestStorage
|
2016-12-25 02:34:22 -08:00
|
|
|
|
|
|
|
queryEngine *Engine
|
|
|
|
context context.Context
|
|
|
|
cancelCtx context.CancelFunc
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTest returns an initialized empty Test.
|
2015-05-23 03:23:33 -07:00
|
|
|
func NewTest(t testutil.T, input string) (*Test, error) {
|
2015-05-11 06:56:35 -07:00
|
|
|
test := &Test{
|
|
|
|
T: t,
|
|
|
|
cmds: []testCommand{},
|
|
|
|
}
|
|
|
|
err := test.parse(input)
|
|
|
|
test.clear()
|
|
|
|
|
|
|
|
return test, err
|
|
|
|
}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
func newTestFromFile(t testutil.T, filename string) (*Test, error) {
|
2015-05-11 06:56:35 -07:00
|
|
|
content, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewTest(t, string(content))
|
|
|
|
}
|
|
|
|
|
2015-06-04 09:09:20 -07:00
|
|
|
// QueryEngine returns the test's query engine.
|
|
|
|
func (t *Test) QueryEngine() *Engine {
|
|
|
|
return t.queryEngine
|
|
|
|
}
|
|
|
|
|
2018-01-09 08:44:23 -08:00
|
|
|
// Queryable allows querying the test data.
|
|
|
|
func (t *Test) Queryable() storage.Queryable {
|
|
|
|
return t.storage
|
|
|
|
}
|
|
|
|
|
2016-09-15 15:58:06 -07:00
|
|
|
// Context returns the test's context.
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 04:52:50 -07:00
|
|
|
func (t *Test) Context() context.Context {
|
2016-09-15 15:58:06 -07:00
|
|
|
return t.context
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 04:52:50 -07:00
|
|
|
}
|
|
|
|
|
2015-06-04 09:09:20 -07:00
|
|
|
// Storage returns the test's storage.
|
2016-12-25 02:34:22 -08:00
|
|
|
func (t *Test) Storage() storage.Storage {
|
2015-06-04 09:09:20 -07:00
|
|
|
return t.storage
|
|
|
|
}
|
|
|
|
|
2020-04-29 09:16:14 -07:00
|
|
|
// TSDB returns test's TSDB.
|
|
|
|
func (t *Test) TSDB() *tsdb.DB {
|
|
|
|
return t.storage.DB
|
|
|
|
}
|
|
|
|
|
2021-03-16 02:47:45 -07:00
|
|
|
// ExemplarStorage returns the test's exemplar storage.
|
|
|
|
func (t *Test) ExemplarStorage() storage.ExemplarStorage {
|
|
|
|
return t.storage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Test) ExemplarQueryable() storage.ExemplarQueryable {
|
|
|
|
return t.storage.ExemplarQueryable()
|
|
|
|
}
|
|
|
|
|
2015-05-11 06:56:35 -07:00
|
|
|
func raise(line int, format string, v ...interface{}) error {
|
2020-02-03 09:00:41 -08:00
|
|
|
return &parser.ParseErr{
|
2020-02-03 09:48:27 -08:00
|
|
|
LineOffset: line,
|
2020-01-15 12:01:49 -08:00
|
|
|
Err: errors.Errorf(format, v...),
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-22 00:51:38 -08:00
|
|
|
func parseLoad(lines []string, i int) (int, *loadCmd, error) {
|
2015-05-11 06:56:35 -07:00
|
|
|
if !patLoad.MatchString(lines[i]) {
|
|
|
|
return i, nil, raise(i, "invalid load command. (load <step:duration>)")
|
|
|
|
}
|
|
|
|
parts := patLoad.FindStringSubmatch(lines[i])
|
|
|
|
|
2020-02-03 09:48:27 -08:00
|
|
|
gap, err := model.ParseDuration(parts[1])
|
2015-05-11 06:56:35 -07:00
|
|
|
if err != nil {
|
|
|
|
return i, nil, raise(i, "invalid step definition %q: %s", parts[1], err)
|
|
|
|
}
|
2016-01-29 06:23:11 -08:00
|
|
|
cmd := newLoadCmd(time.Duration(gap))
|
2015-05-11 06:56:35 -07:00
|
|
|
for i+1 < len(lines) {
|
|
|
|
i++
|
|
|
|
defLine := lines[i]
|
|
|
|
if len(defLine) == 0 {
|
|
|
|
i--
|
|
|
|
break
|
|
|
|
}
|
2020-02-03 09:48:27 -08:00
|
|
|
metric, vals, err := parser.ParseSeriesDesc(defLine)
|
2015-05-11 06:56:35 -07:00
|
|
|
if err != nil {
|
2020-02-03 09:00:41 -08:00
|
|
|
if perr, ok := err.(*parser.ParseErr); ok {
|
2020-02-03 09:48:27 -08:00
|
|
|
perr.LineOffset = i
|
2015-08-02 15:26:21 -07:00
|
|
|
}
|
2015-05-11 06:56:35 -07:00
|
|
|
return i, nil, err
|
|
|
|
}
|
|
|
|
cmd.set(metric, vals...)
|
|
|
|
}
|
|
|
|
return i, cmd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Test) parseEval(lines []string, i int) (int, *evalCmd, error) {
|
|
|
|
if !patEvalInstant.MatchString(lines[i]) {
|
|
|
|
return i, nil, raise(i, "invalid evaluation command. (eval[_fail|_ordered] instant [at <offset:duration>] <query>")
|
|
|
|
}
|
|
|
|
parts := patEvalInstant.FindStringSubmatch(lines[i])
|
|
|
|
var (
|
2018-03-08 08:38:56 -08:00
|
|
|
mod = parts[1]
|
|
|
|
at = parts[2]
|
|
|
|
expr = parts[3]
|
2015-05-11 06:56:35 -07:00
|
|
|
)
|
2020-02-03 09:00:41 -08:00
|
|
|
_, err := parser.ParseExpr(expr)
|
2015-05-11 06:56:35 -07:00
|
|
|
if err != nil {
|
2020-02-03 09:00:41 -08:00
|
|
|
if perr, ok := err.(*parser.ParseErr); ok {
|
2020-02-03 09:48:27 -08:00
|
|
|
perr.LineOffset = i
|
|
|
|
posOffset := parser.Pos(strings.Index(lines[i], expr))
|
2020-01-15 12:01:49 -08:00
|
|
|
perr.PositionRange.Start += posOffset
|
|
|
|
perr.PositionRange.End += posOffset
|
|
|
|
perr.Query = lines[i]
|
2015-08-02 15:26:21 -07:00
|
|
|
}
|
|
|
|
return i, nil, err
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
|
2020-02-03 09:48:27 -08:00
|
|
|
offset, err := model.ParseDuration(at)
|
2015-05-11 06:56:35 -07:00
|
|
|
if err != nil {
|
|
|
|
return i, nil, raise(i, "invalid step definition %q: %s", parts[1], err)
|
|
|
|
}
|
2016-01-29 06:23:11 -08:00
|
|
|
ts := testStartTime.Add(time.Duration(offset))
|
2015-05-11 06:56:35 -07:00
|
|
|
|
2018-06-14 07:18:16 -07:00
|
|
|
cmd := newEvalCmd(expr, ts, i+1)
|
2015-05-11 06:56:35 -07:00
|
|
|
switch mod {
|
|
|
|
case "ordered":
|
|
|
|
cmd.ordered = true
|
|
|
|
case "fail":
|
|
|
|
cmd.fail = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := 1; i+1 < len(lines); j++ {
|
|
|
|
i++
|
|
|
|
defLine := lines[i]
|
|
|
|
if len(defLine) == 0 {
|
|
|
|
i--
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if f, err := parseNumber(defLine); err == nil {
|
2020-02-03 09:48:27 -08:00
|
|
|
cmd.expect(0, nil, parser.SequenceValue{Value: f})
|
2015-05-11 06:56:35 -07:00
|
|
|
break
|
|
|
|
}
|
2020-02-03 09:48:27 -08:00
|
|
|
metric, vals, err := parser.ParseSeriesDesc(defLine)
|
2015-05-11 06:56:35 -07:00
|
|
|
if err != nil {
|
2020-02-03 09:00:41 -08:00
|
|
|
if perr, ok := err.(*parser.ParseErr); ok {
|
2020-02-03 09:48:27 -08:00
|
|
|
perr.LineOffset = i
|
2015-08-02 15:26:21 -07:00
|
|
|
}
|
2015-05-11 06:56:35 -07:00
|
|
|
return i, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently, we are not expecting any matrices.
|
|
|
|
if len(vals) > 1 {
|
|
|
|
return i, nil, raise(i, "expecting multiple values in instant evaluation not allowed")
|
|
|
|
}
|
|
|
|
cmd.expect(j, metric, vals...)
|
|
|
|
}
|
|
|
|
return i, cmd, nil
|
|
|
|
}
|
|
|
|
|
2018-11-22 00:51:38 -08:00
|
|
|
// getLines returns trimmed lines after removing the comments.
|
|
|
|
func getLines(input string) []string {
|
2015-05-11 06:56:35 -07:00
|
|
|
lines := strings.Split(input, "\n")
|
|
|
|
for i, l := range lines {
|
|
|
|
l = strings.TrimSpace(l)
|
|
|
|
if strings.HasPrefix(l, "#") {
|
|
|
|
l = ""
|
|
|
|
}
|
|
|
|
lines[i] = l
|
|
|
|
}
|
2018-11-22 00:51:38 -08:00
|
|
|
return lines
|
|
|
|
}
|
2015-05-11 06:56:35 -07:00
|
|
|
|
2018-11-22 00:51:38 -08:00
|
|
|
// parse the given command sequence and appends it to the test.
|
|
|
|
func (t *Test) parse(input string) error {
|
|
|
|
lines := getLines(input)
|
|
|
|
var err error
|
2015-05-11 06:56:35 -07:00
|
|
|
// Scan for steps line by line.
|
|
|
|
for i := 0; i < len(lines); i++ {
|
|
|
|
l := lines[i]
|
|
|
|
if len(l) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var cmd testCommand
|
|
|
|
|
|
|
|
switch c := strings.ToLower(patSpace.Split(l, 2)[0]); {
|
|
|
|
case c == "clear":
|
|
|
|
cmd = &clearCmd{}
|
|
|
|
case c == "load":
|
2018-11-22 00:51:38 -08:00
|
|
|
i, cmd, err = parseLoad(lines, i)
|
2015-05-11 06:56:35 -07:00
|
|
|
case strings.HasPrefix(c, "eval"):
|
|
|
|
i, cmd, err = t.parseEval(lines, i)
|
|
|
|
default:
|
|
|
|
return raise(i, "invalid command %q", l)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.cmds = append(t.cmds, cmd)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// testCommand is an interface that ensures that only the package internal
|
|
|
|
// types can be a valid command for a test.
|
|
|
|
type testCommand interface {
|
|
|
|
testCmd()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*clearCmd) testCmd() {}
|
|
|
|
func (*loadCmd) testCmd() {}
|
|
|
|
func (*evalCmd) testCmd() {}
|
|
|
|
|
|
|
|
// loadCmd is a command that loads sequences of sample values for specific
|
|
|
|
// metrics into the storage.
|
|
|
|
type loadCmd struct {
|
2021-05-06 13:53:52 -07:00
|
|
|
gap time.Duration
|
|
|
|
metrics map[uint64]labels.Labels
|
|
|
|
defs map[uint64][]Point
|
|
|
|
exemplars map[uint64][]exemplar.Exemplar
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func newLoadCmd(gap time.Duration) *loadCmd {
|
|
|
|
return &loadCmd{
|
2021-05-06 13:53:52 -07:00
|
|
|
gap: gap,
|
|
|
|
metrics: map[uint64]labels.Labels{},
|
|
|
|
defs: map[uint64][]Point{},
|
|
|
|
exemplars: map[uint64][]exemplar.Exemplar{},
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd loadCmd) String() string {
|
|
|
|
return "load"
|
|
|
|
}
|
|
|
|
|
|
|
|
// set a sequence of sample values for the given metric.
|
2020-02-03 08:45:41 -08:00
|
|
|
func (cmd *loadCmd) set(m labels.Labels, vals ...parser.SequenceValue) {
|
2016-12-23 04:51:59 -08:00
|
|
|
h := m.Hash()
|
2015-05-11 06:56:35 -07:00
|
|
|
|
2016-12-24 02:23:06 -08:00
|
|
|
samples := make([]Point, 0, len(vals))
|
2015-05-11 06:56:35 -07:00
|
|
|
ts := testStartTime
|
|
|
|
for _, v := range vals {
|
2020-02-03 09:48:27 -08:00
|
|
|
if !v.Omitted {
|
2016-12-24 02:23:06 -08:00
|
|
|
samples = append(samples, Point{
|
|
|
|
T: ts.UnixNano() / int64(time.Millisecond/time.Nanosecond),
|
2020-02-03 09:48:27 -08:00
|
|
|
V: v.Value,
|
2015-05-11 06:56:35 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
ts = ts.Add(cmd.gap)
|
|
|
|
}
|
2016-12-23 04:51:59 -08:00
|
|
|
cmd.defs[h] = samples
|
|
|
|
cmd.metrics[h] = m
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// append the defined time series to the storage.
|
2017-01-13 05:48:01 -08:00
|
|
|
func (cmd *loadCmd) append(a storage.Appender) error {
|
2016-12-28 00:16:48 -08:00
|
|
|
for h, smpls := range cmd.defs {
|
|
|
|
m := cmd.metrics[h]
|
|
|
|
|
|
|
|
for _, s := range smpls {
|
2021-02-18 04:07:00 -08:00
|
|
|
if _, err := a.Append(0, m, s.T, s.V); err != nil {
|
2017-01-13 05:48:01 -08:00
|
|
|
return err
|
|
|
|
}
|
2016-12-28 00:16:48 -08:00
|
|
|
}
|
|
|
|
}
|
2017-01-13 05:48:01 -08:00
|
|
|
return nil
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// evalCmd is a command that evaluates an expression for the given time (range)
|
|
|
|
// and expects a specific result.
|
|
|
|
type evalCmd struct {
|
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
|
|
|
expr string
|
|
|
|
start time.Time
|
2018-06-14 07:18:16 -07:00
|
|
|
line int
|
2015-05-11 06:56:35 -07:00
|
|
|
|
|
|
|
fail, ordered bool
|
|
|
|
|
2016-12-23 04:51:59 -08:00
|
|
|
metrics map[uint64]labels.Labels
|
|
|
|
expected map[uint64]entry
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type entry struct {
|
|
|
|
pos int
|
2020-02-03 08:45:41 -08:00
|
|
|
vals []parser.SequenceValue
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e entry) String() string {
|
|
|
|
return fmt.Sprintf("%d: %s", e.pos, e.vals)
|
|
|
|
}
|
|
|
|
|
2018-06-14 07:18:16 -07:00
|
|
|
func newEvalCmd(expr string, start time.Time, line int) *evalCmd {
|
2015-05-11 06:56:35 -07:00
|
|
|
return &evalCmd{
|
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
|
|
|
expr: expr,
|
|
|
|
start: start,
|
2018-06-14 07:18:16 -07:00
|
|
|
line: line,
|
2015-05-11 06:56:35 -07:00
|
|
|
|
2016-12-23 04:51:59 -08:00
|
|
|
metrics: map[uint64]labels.Labels{},
|
|
|
|
expected: map[uint64]entry{},
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *evalCmd) String() string {
|
|
|
|
return "eval"
|
|
|
|
}
|
|
|
|
|
|
|
|
// expect adds a new metric with a sequence of values to the set of expected
|
|
|
|
// results for the query.
|
2020-02-03 08:45:41 -08:00
|
|
|
func (ev *evalCmd) expect(pos int, m labels.Labels, vals ...parser.SequenceValue) {
|
2015-05-11 06:56:35 -07:00
|
|
|
if m == nil {
|
|
|
|
ev.expected[0] = entry{pos: pos, vals: vals}
|
|
|
|
return
|
|
|
|
}
|
2016-12-23 04:51:59 -08:00
|
|
|
h := m.Hash()
|
|
|
|
ev.metrics[h] = m
|
|
|
|
ev.expected[h] = entry{pos: pos, vals: vals}
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// compareResult compares the result value with the defined expectation.
|
2020-02-03 08:32:23 -08:00
|
|
|
func (ev *evalCmd) compareResult(result parser.Value) error {
|
2015-05-11 06:56:35 -07:00
|
|
|
switch val := result.(type) {
|
2016-12-24 01:42:54 -08:00
|
|
|
case Matrix:
|
2019-03-25 16:01:12 -07:00
|
|
|
return errors.New("received range result on instant evaluation")
|
2015-05-11 06:56:35 -07:00
|
|
|
|
2016-12-24 01:40:09 -08:00
|
|
|
case Vector:
|
2016-12-23 04:51:59 -08:00
|
|
|
seen := map[uint64]bool{}
|
2015-05-11 06:56:35 -07:00
|
|
|
for pos, v := range val {
|
2016-12-23 04:51:59 -08:00
|
|
|
fp := v.Metric.Hash()
|
2015-05-11 06:56:35 -07:00
|
|
|
if _, ok := ev.metrics[fp]; !ok {
|
2019-03-25 16:01:12 -07:00
|
|
|
return errors.Errorf("unexpected metric %s in result", v.Metric)
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
exp := ev.expected[fp]
|
|
|
|
if ev.ordered && exp.pos != pos+1 {
|
2019-03-25 16:01:12 -07:00
|
|
|
return errors.Errorf("expected metric %s with %v at position %d but was at %d", v.Metric, exp.vals, exp.pos, pos+1)
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
2020-02-03 09:48:27 -08:00
|
|
|
if !almostEqual(exp.vals[0].Value, v.V) {
|
|
|
|
return errors.Errorf("expected %v for %s but got %v", exp.vals[0].Value, v.Metric, v.V)
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
seen[fp] = true
|
|
|
|
}
|
|
|
|
for fp, expVals := range ev.expected {
|
|
|
|
if !seen[fp] {
|
2018-03-08 08:39:46 -08:00
|
|
|
fmt.Println("vector result", len(val), ev.expr)
|
|
|
|
for _, ss := range val {
|
|
|
|
fmt.Println(" ", ss.Metric, ss.Point)
|
|
|
|
}
|
2019-03-25 16:01:12 -07:00
|
|
|
return errors.Errorf("expected metric %s with %v not found", ev.metrics[fp], expVals)
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-24 01:44:04 -08:00
|
|
|
case Scalar:
|
2020-02-03 09:48:27 -08:00
|
|
|
if !almostEqual(ev.expected[0].vals[0].Value, val.V) {
|
|
|
|
return errors.Errorf("expected Scalar %v but got %v", val.V, ev.expected[0].vals[0].Value)
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2019-03-25 16:01:12 -07:00
|
|
|
panic(errors.Errorf("promql.Test.compareResult: unexpected result type %T", result))
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// clearCmd is a command that wipes the test's storage state.
|
|
|
|
type clearCmd struct{}
|
|
|
|
|
|
|
|
func (cmd clearCmd) String() string {
|
|
|
|
return "clear"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes the command sequence of the test. Until the maximum error number
|
|
|
|
// is reached, evaluation errors do not terminate execution.
|
|
|
|
func (t *Test) Run() error {
|
|
|
|
for _, cmd := range t.cmds {
|
|
|
|
// TODO(fabxc): aggregate command errors, yield diffs for result
|
|
|
|
// comparison errors.
|
2020-02-10 04:46:10 -08:00
|
|
|
if err := t.exec(cmd); err != nil {
|
2015-05-11 06:56:35 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-20 02:57:39 -08:00
|
|
|
type atModifierTestCase struct {
|
|
|
|
expr string
|
|
|
|
evalTime time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func atModifierTestCases(exprStr string, evalTime time.Time) ([]atModifierTestCase, error) {
|
|
|
|
expr, err := parser.ParseExpr(exprStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ts := timestamp.FromTime(evalTime)
|
|
|
|
|
|
|
|
containsNonStepInvariant := false
|
|
|
|
// Setting the @ timestamp for all selectors to be evalTime.
|
|
|
|
// If there is a subquery, then the selectors inside it don't get the @ timestamp.
|
|
|
|
// If any selector already has the @ timestamp set, then it is untouched.
|
|
|
|
parser.Inspect(expr, func(node parser.Node, path []parser.Node) error {
|
|
|
|
_, _, subqTs := subqueryTimes(path)
|
|
|
|
if subqTs != nil {
|
|
|
|
// There is a subquery with timestamp in the path,
|
|
|
|
// hence don't change any timestamps further.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *parser.VectorSelector:
|
|
|
|
if n.Timestamp == nil {
|
|
|
|
n.Timestamp = makeInt64Pointer(ts)
|
|
|
|
}
|
|
|
|
|
|
|
|
case *parser.MatrixSelector:
|
|
|
|
if vs := n.VectorSelector.(*parser.VectorSelector); vs.Timestamp == nil {
|
|
|
|
vs.Timestamp = makeInt64Pointer(ts)
|
|
|
|
}
|
|
|
|
|
|
|
|
case *parser.SubqueryExpr:
|
|
|
|
if n.Timestamp == nil {
|
|
|
|
n.Timestamp = makeInt64Pointer(ts)
|
|
|
|
}
|
|
|
|
|
|
|
|
case *parser.Call:
|
|
|
|
_, ok := AtModifierUnsafeFunctions[n.Func.Name]
|
|
|
|
containsNonStepInvariant = containsNonStepInvariant || ok
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if containsNonStepInvariant {
|
|
|
|
// Since there is a step invariant function, we cannot automatically
|
|
|
|
// generate step invariant test cases for it sanely.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
newExpr := expr.String() // With all the @ evalTime set.
|
|
|
|
additionalEvalTimes := []int64{-10 * ts, 0, ts / 5, ts, 10 * ts}
|
|
|
|
if ts == 0 {
|
|
|
|
additionalEvalTimes = []int64{-1000, -ts, 1000}
|
|
|
|
}
|
|
|
|
testCases := make([]atModifierTestCase, 0, len(additionalEvalTimes))
|
|
|
|
for _, et := range additionalEvalTimes {
|
|
|
|
testCases = append(testCases, atModifierTestCase{
|
|
|
|
expr: newExpr,
|
|
|
|
evalTime: timestamp.Time(et),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return testCases, nil
|
|
|
|
}
|
|
|
|
|
2016-03-02 00:16:56 -08:00
|
|
|
// exec processes a single step of the test.
|
2015-05-11 06:56:35 -07:00
|
|
|
func (t *Test) exec(tc testCommand) error {
|
|
|
|
switch cmd := tc.(type) {
|
|
|
|
case *clearCmd:
|
|
|
|
t.clear()
|
|
|
|
|
|
|
|
case *loadCmd:
|
2020-07-24 07:10:51 -07:00
|
|
|
app := t.storage.Appender(t.context)
|
2017-01-13 05:48:01 -08:00
|
|
|
if err := cmd.append(app); err != nil {
|
|
|
|
app.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
2016-12-25 02:34:22 -08:00
|
|
|
|
|
|
|
if err := app.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-11 06:56:35 -07:00
|
|
|
|
|
|
|
case *evalCmd:
|
2021-01-20 02:57:39 -08:00
|
|
|
queries, err := atModifierTestCases(cmd.expr, cmd.start)
|
2018-08-17 08:24:35 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-20 02:57:39 -08:00
|
|
|
queries = append([]atModifierTestCase{{expr: cmd.expr, evalTime: cmd.start}}, queries...)
|
|
|
|
for _, iq := range queries {
|
|
|
|
q, err := t.QueryEngine().NewInstantQuery(t.storage, iq.expr, iq.evalTime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer q.Close()
|
|
|
|
res := q.Exec(t.context)
|
|
|
|
if res.Err != nil {
|
|
|
|
if cmd.fail {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return errors.Wrapf(res.Err, "error evaluating query %q (line %d)", iq.expr, cmd.line)
|
|
|
|
}
|
|
|
|
if res.Err == nil && cmd.fail {
|
|
|
|
return errors.Errorf("expected error evaluating query %q (line %d) but got none", iq.expr, cmd.line)
|
|
|
|
}
|
|
|
|
err = cmd.compareResult(res.Value)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error in %s %s", cmd, iq.expr)
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 02:57:39 -08:00
|
|
|
// Check query returns same result in range mode,
|
|
|
|
// by checking against the middle step.
|
|
|
|
q, err = t.queryEngine.NewRangeQuery(t.storage, iq.expr, iq.evalTime.Add(-time.Minute), iq.evalTime.Add(time.Minute), time.Minute)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rangeRes := q.Exec(t.context)
|
|
|
|
if rangeRes.Err != nil {
|
|
|
|
return errors.Wrapf(rangeRes.Err, "error evaluating query %q (line %d) in range mode", iq.expr, cmd.line)
|
|
|
|
}
|
|
|
|
defer q.Close()
|
|
|
|
if cmd.ordered {
|
|
|
|
// Ordering isn't defined for range queries.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mat := rangeRes.Value.(Matrix)
|
|
|
|
vec := make(Vector, 0, len(mat))
|
|
|
|
for _, series := range mat {
|
|
|
|
for _, point := range series.Points {
|
|
|
|
if point.T == timeMilliseconds(iq.evalTime) {
|
|
|
|
vec = append(vec, Sample{Metric: series.Metric, Point: point})
|
|
|
|
break
|
|
|
|
}
|
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
|
|
|
}
|
|
|
|
}
|
2021-01-20 02:57:39 -08:00
|
|
|
if _, ok := res.Value.(Scalar); ok {
|
|
|
|
err = cmd.compareResult(Scalar{V: vec[0].Point.V})
|
|
|
|
} else {
|
|
|
|
err = cmd.compareResult(vec)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error in %s %s (line %d) rande mode", cmd, iq.expr, cmd.line)
|
|
|
|
}
|
|
|
|
|
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
|
|
|
}
|
|
|
|
|
2015-05-11 06:56:35 -07:00
|
|
|
default:
|
|
|
|
panic("promql.Test.exec: unknown test command type")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear the current test storage of all inserted samples.
|
|
|
|
func (t *Test) clear() {
|
2016-12-25 02:34:22 -08:00
|
|
|
if t.storage != nil {
|
|
|
|
if err := t.storage.Close(); err != nil {
|
|
|
|
t.T.Fatalf("closing test storage: %s", err)
|
|
|
|
}
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
2016-09-15 15:58:06 -07:00
|
|
|
if t.cancelCtx != nil {
|
|
|
|
t.cancelCtx()
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
2019-08-08 18:35:39 -07:00
|
|
|
t.storage = teststorage.New(t)
|
2015-05-11 06:56:35 -07:00
|
|
|
|
2018-10-02 04:59:19 -07:00
|
|
|
opts := EngineOpts{
|
2020-07-22 06:39:51 -07:00
|
|
|
Logger: nil,
|
|
|
|
Reg: nil,
|
|
|
|
MaxSamples: 10000,
|
|
|
|
Timeout: 100 * time.Second,
|
|
|
|
NoStepSubqueryIntervalFn: func(int64) int64 { return durationMilliseconds(1 * time.Minute) },
|
2021-01-20 02:57:39 -08:00
|
|
|
EnableAtModifier: true,
|
2018-10-02 04:59:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
t.queryEngine = NewEngine(opts)
|
2016-09-15 15:58:06 -07:00
|
|
|
t.context, t.cancelCtx = context.WithCancel(context.Background())
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
// Close closes resources associated with the Test.
|
2015-05-11 06:56:35 -07:00
|
|
|
func (t *Test) Close() {
|
2016-09-15 15:58:06 -07:00
|
|
|
t.cancelCtx()
|
2016-12-25 02:34:22 -08:00
|
|
|
|
|
|
|
if err := t.storage.Close(); err != nil {
|
|
|
|
t.T.Fatalf("closing test storage: %s", err)
|
|
|
|
}
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// samplesAlmostEqual returns true if the two sample lines only differ by a
|
|
|
|
// small relative error in their sample value.
|
|
|
|
func almostEqual(a, b float64) bool {
|
|
|
|
// NaN has no equality but for testing we still want to know whether both values
|
|
|
|
// are NaN.
|
|
|
|
if math.IsNaN(a) && math.IsNaN(b) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cf. http://floating-point-gui.de/errors/comparison/
|
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
diff := math.Abs(a - b)
|
|
|
|
|
|
|
|
if a == 0 || b == 0 || diff < minNormal {
|
|
|
|
return diff < epsilon*minNormal
|
|
|
|
}
|
|
|
|
return diff/(math.Abs(a)+math.Abs(b)) < epsilon
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseNumber(s string) (float64, error) {
|
2020-02-03 09:48:27 -08:00
|
|
|
n, err := strconv.ParseInt(s, 0, 64)
|
2015-05-11 06:56:35 -07:00
|
|
|
f := float64(n)
|
|
|
|
if err != nil {
|
2020-02-03 09:48:27 -08:00
|
|
|
f, err = strconv.ParseFloat(s, 64)
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
if err != nil {
|
2019-03-25 16:01:12 -07:00
|
|
|
return 0, errors.Wrap(err, "error parsing number")
|
2015-05-11 06:56:35 -07:00
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
2018-11-22 00:51:38 -08:00
|
|
|
|
|
|
|
// LazyLoader lazily loads samples into storage.
|
|
|
|
// This is specifically implemented for unit testing of rules.
|
|
|
|
type LazyLoader struct {
|
|
|
|
testutil.T
|
|
|
|
|
|
|
|
loadCmd *loadCmd
|
|
|
|
|
2021-03-07 00:19:33 -08:00
|
|
|
storage storage.Storage
|
|
|
|
SubqueryInterval time.Duration
|
2018-11-22 00:51:38 -08:00
|
|
|
|
|
|
|
queryEngine *Engine
|
|
|
|
context context.Context
|
|
|
|
cancelCtx context.CancelFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLazyLoader returns an initialized empty LazyLoader.
|
|
|
|
func NewLazyLoader(t testutil.T, input string) (*LazyLoader, error) {
|
|
|
|
ll := &LazyLoader{
|
|
|
|
T: t,
|
|
|
|
}
|
|
|
|
err := ll.parse(input)
|
|
|
|
ll.clear()
|
|
|
|
return ll, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the given load command.
|
|
|
|
func (ll *LazyLoader) parse(input string) error {
|
|
|
|
lines := getLines(input)
|
|
|
|
// Accepts only 'load' command.
|
|
|
|
for i := 0; i < len(lines); i++ {
|
|
|
|
l := lines[i]
|
|
|
|
if len(l) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.ToLower(patSpace.Split(l, 2)[0]) == "load" {
|
|
|
|
_, cmd, err := parseLoad(lines, i)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ll.loadCmd = cmd
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-08 10:26:02 -08:00
|
|
|
|
|
|
|
return raise(i, "invalid command %q", l)
|
2018-11-22 00:51:38 -08:00
|
|
|
}
|
|
|
|
return errors.New("no \"load\" command found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear the current test storage of all inserted samples.
|
|
|
|
func (ll *LazyLoader) clear() {
|
|
|
|
if ll.storage != nil {
|
|
|
|
if err := ll.storage.Close(); err != nil {
|
|
|
|
ll.T.Fatalf("closing test storage: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ll.cancelCtx != nil {
|
|
|
|
ll.cancelCtx()
|
|
|
|
}
|
2019-08-08 18:35:39 -07:00
|
|
|
ll.storage = teststorage.New(ll)
|
2018-11-22 00:51:38 -08:00
|
|
|
|
|
|
|
opts := EngineOpts{
|
2021-03-07 00:19:33 -08:00
|
|
|
Logger: nil,
|
|
|
|
Reg: nil,
|
|
|
|
MaxSamples: 10000,
|
|
|
|
Timeout: 100 * time.Second,
|
|
|
|
NoStepSubqueryIntervalFn: func(int64) int64 { return durationMilliseconds(ll.SubqueryInterval) },
|
|
|
|
EnableAtModifier: true,
|
2018-11-22 00:51:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
ll.queryEngine = NewEngine(opts)
|
|
|
|
ll.context, ll.cancelCtx = context.WithCancel(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
// appendTill appends the defined time series to the storage till the given timestamp (in milliseconds).
|
|
|
|
func (ll *LazyLoader) appendTill(ts int64) error {
|
2020-07-24 07:10:51 -07:00
|
|
|
app := ll.storage.Appender(ll.Context())
|
2018-11-22 00:51:38 -08:00
|
|
|
for h, smpls := range ll.loadCmd.defs {
|
|
|
|
m := ll.loadCmd.metrics[h]
|
|
|
|
for i, s := range smpls {
|
|
|
|
if s.T > ts {
|
|
|
|
// Removing the already added samples.
|
|
|
|
ll.loadCmd.defs[h] = smpls[i:]
|
|
|
|
break
|
|
|
|
}
|
2021-02-18 04:07:00 -08:00
|
|
|
if _, err := app.Append(0, m, s.T, s.V); err != nil {
|
2018-11-22 00:51:38 -08:00
|
|
|
return err
|
|
|
|
}
|
2019-01-07 05:37:27 -08:00
|
|
|
if i == len(smpls)-1 {
|
|
|
|
ll.loadCmd.defs[h] = nil
|
|
|
|
}
|
2018-11-22 00:51:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return app.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSamplesTill loads the samples till given timestamp and executes the given function.
|
|
|
|
func (ll *LazyLoader) WithSamplesTill(ts time.Time, fn func(error)) {
|
2020-03-29 09:35:39 -07:00
|
|
|
tsMilli := ts.Sub(time.Unix(0, 0).UTC()) / time.Millisecond
|
2018-11-22 00:51:38 -08:00
|
|
|
fn(ll.appendTill(int64(tsMilli)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryEngine returns the LazyLoader's query engine.
|
|
|
|
func (ll *LazyLoader) QueryEngine() *Engine {
|
|
|
|
return ll.queryEngine
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queryable allows querying the LazyLoader's data.
|
|
|
|
// Note: only the samples till the max timestamp used
|
|
|
|
// in `WithSamplesTill` can be queried.
|
|
|
|
func (ll *LazyLoader) Queryable() storage.Queryable {
|
|
|
|
return ll.storage
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context returns the LazyLoader's context.
|
|
|
|
func (ll *LazyLoader) Context() context.Context {
|
|
|
|
return ll.context
|
|
|
|
}
|
|
|
|
|
|
|
|
// Storage returns the LazyLoader's storage.
|
|
|
|
func (ll *LazyLoader) Storage() storage.Storage {
|
|
|
|
return ll.storage
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes resources associated with the LazyLoader.
|
|
|
|
func (ll *LazyLoader) Close() {
|
|
|
|
ll.cancelCtx()
|
|
|
|
|
|
|
|
if err := ll.storage.Close(); err != nil {
|
|
|
|
ll.T.Fatalf("closing test storage: %s", err)
|
|
|
|
}
|
|
|
|
}
|