Skip to content

Commit

Permalink
[query] Fix Graphite asPercent function to handle nodes and multiple …
Browse files Browse the repository at this point in the history
…total divisor series (#3142)
  • Loading branch information
robskillington authored Mar 18, 2021
1 parent 8df132f commit be8f4f6
Show file tree
Hide file tree
Showing 20 changed files with 638 additions and 329 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package convert_test

import (
stdctx "context"
"errors"
"fmt"
"testing"
"time"
Expand All @@ -33,7 +34,6 @@ import (
"github.com/m3db/m3/src/dbnode/storage/limits"
"github.com/m3db/m3/src/dbnode/x/xpool"
"github.com/m3db/m3/src/m3ninx/idx"
"github.com/m3db/m3/src/query/graphite/errors"
xerrors "github.com/m3db/m3/src/x/errors"
"github.com/m3db/m3/src/x/ident"
"github.com/m3db/m3/src/x/pool"
Expand Down
2 changes: 1 addition & 1 deletion src/query/api/v1/handler/graphite/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ import (
"github.com/m3db/m3/src/query/api/v1/options"
"github.com/m3db/m3/src/query/block"
"github.com/m3db/m3/src/query/graphite/common"
"github.com/m3db/m3/src/query/graphite/errors"
"github.com/m3db/m3/src/query/graphite/native"
graphite "github.com/m3db/m3/src/query/graphite/storage"
"github.com/m3db/m3/src/query/graphite/ts"
"github.com/m3db/m3/src/query/models"
"github.com/m3db/m3/src/x/errors"
"github.com/m3db/m3/src/x/headers"
xhttp "github.com/m3db/m3/src/x/net/http"
)
Expand Down
21 changes: 11 additions & 10 deletions src/query/api/v1/handler/graphite/render_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package graphite

import (
"context"
"errors"
"fmt"
"io"
"math"
Expand All @@ -31,11 +32,11 @@ import (

"github.com/m3db/m3/src/query/api/v1/handler/graphite/pickle"
"github.com/m3db/m3/src/query/api/v1/options"
"github.com/m3db/m3/src/query/graphite/errors"
"github.com/m3db/m3/src/query/graphite/graphite"
"github.com/m3db/m3/src/query/graphite/ts"
"github.com/m3db/m3/src/query/storage"
"github.com/m3db/m3/src/query/util/json"
xerrors "github.com/m3db/m3/src/x/errors"
xhttp "github.com/m3db/m3/src/x/net/http"
)

Expand All @@ -47,8 +48,8 @@ const (
)

var (
errNoTarget = errors.NewInvalidParamsError(errors.New("no 'target' specified"))
errFromNotBeforeUntil = errors.NewInvalidParamsError(errors.New("'from' must come before 'until'"))
errNoTarget = xerrors.NewInvalidParamsError(errors.New("no 'target' specified"))
errFromNotBeforeUntil = xerrors.NewInvalidParamsError(errors.New("'from' must come before 'until'"))
)

// WriteRenderResponse writes the response to a render request
Expand Down Expand Up @@ -124,15 +125,15 @@ func ParseRenderRequest(
now,
tzOffsetForAbsoluteTime,
); err != nil {
return nil, p, nil, errors.NewInvalidParamsError(fmt.Errorf("invalid 'from': %s", fromString))
return nil, p, nil, xerrors.NewInvalidParamsError(fmt.Errorf("invalid 'from': %s", fromString))
}

if p.Until, err = graphite.ParseTime(
untilString,
now,
tzOffsetForAbsoluteTime,
); err != nil {
return nil, p, nil, errors.NewInvalidParamsError(fmt.Errorf("invalid 'until': %s", untilString))
return nil, p, nil, xerrors.NewInvalidParamsError(fmt.Errorf("invalid 'until': %s", untilString))
}

if !p.From.Before(p.Until) {
Expand All @@ -156,8 +157,8 @@ func ParseRenderRequest(
if len(offset) > 0 {
dur, err := graphite.ParseDuration(offset)
if err != nil {
err = errors.NewInvalidParamsError(err)
return nil, p, nil, errors.NewRenamedError(err, fmt.Errorf("invalid 'offset': %w", err))
err = xerrors.NewInvalidParamsError(err)
return nil, p, nil, xerrors.NewRenamedError(err, fmt.Errorf("invalid 'offset': %w", err))
}

p.Until = p.Until.Add(dur)
Expand All @@ -169,7 +170,7 @@ func ParseRenderRequest(
p.MaxDataPoints, err = strconv.ParseInt(maxDataPointsString, 10, 64)

if err != nil || p.MaxDataPoints < 1 {
return nil, p, nil, errors.NewInvalidParamsError(fmt.Errorf("invalid 'maxDataPoints': %s", maxDataPointsString))
return nil, p, nil, xerrors.NewInvalidParamsError(fmt.Errorf("invalid 'maxDataPoints': %s", maxDataPointsString))
}
} else {
p.MaxDataPoints = math.MaxInt64
Expand All @@ -182,9 +183,9 @@ func ParseRenderRequest(
p.From,
tzOffsetForAbsoluteTime,
); err != nil && len(compareString) != 0 {
return nil, p, nil, errors.NewInvalidParamsError(fmt.Errorf("invalid 'compare': %s", compareString))
return nil, p, nil, xerrors.NewInvalidParamsError(fmt.Errorf("invalid 'compare': %s", compareString))
} else if p.From.Before(compareFrom) {
return nil, p, nil, errors.NewInvalidParamsError(fmt.Errorf("'compare' must be in the past"))
return nil, p, nil, xerrors.NewInvalidParamsError(fmt.Errorf("'compare' must be in the past"))
} else {
p.Compare = compareFrom.Sub(p.From)
}
Expand Down
7 changes: 3 additions & 4 deletions src/query/api/v1/handler/prom/prom.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@
package prom

import (
"errors"
"net/http"
"time"

"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/promql/parser"
promstorage "github.com/prometheus/prometheus/storage"

"github.com/m3db/m3/src/query/api/v1/options"
"github.com/m3db/m3/src/query/block"
"github.com/m3db/m3/src/query/graphite/errors"
"github.com/m3db/m3/src/query/storage/prometheus"

"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/promql/parser"
)

// NB: since Prometheus engine is not brought up in the usual fashion,
Expand Down
2 changes: 1 addition & 1 deletion src/query/graphite/common/aliasing.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"strconv"
"strings"

"github.com/m3db/m3/src/query/graphite/errors"
"github.com/m3db/m3/src/query/graphite/ts"
"github.com/m3db/m3/src/x/errors"
)

var (
Expand Down
18 changes: 10 additions & 8 deletions src/query/graphite/common/basic_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@
package common

import (
"errors"
"fmt"
"math"
"regexp"
"strconv"
"strings"
"time"

"github.com/m3db/m3/src/query/graphite/errors"
"github.com/m3db/m3/src/query/graphite/ts"
xerrors "github.com/m3db/m3/src/x/errors"
)

var (
// ErrNegativeCount occurs when the request count is < 0.
ErrNegativeCount = errors.NewInvalidParamsError(errors.New("n must be positive"))
ErrNegativeCount = xerrors.NewInvalidParamsError(errors.New("n must be positive"))
// ErrEmptySeriesList occurs when a function requires a series as input
ErrEmptySeriesList = errors.NewInvalidParamsError(errors.New("empty series list"))
ErrEmptySeriesList = xerrors.NewInvalidParamsError(errors.New("empty series list"))
// ErrInvalidIntervalFormat occurs when invalid interval string encountered
ErrInvalidIntervalFormat = errors.NewInvalidParamsError(errors.New("invalid format"))
ErrInvalidIntervalFormat = xerrors.NewInvalidParamsError(errors.New("invalid format"))

reInterval *regexp.Regexp

Expand Down Expand Up @@ -115,7 +116,7 @@ func Identity(ctx *Context, name string) (ts.SeriesList, error) {
func Normalize(ctx *Context, input ts.SeriesList) (ts.SeriesList, time.Time, time.Time, int, error) {
numSeries := input.Len()
if numSeries == 0 {
return ts.NewSeriesList(), ctx.StartTime, ctx.EndTime, -1, errors.NewInvalidParamsError(ErrEmptySeriesList)
return ts.NewSeriesList(), ctx.StartTime, ctx.EndTime, -1, xerrors.NewInvalidParamsError(ErrEmptySeriesList)
}
if numSeries == 1 {
return input, input.Values[0].StartTime(), input.Values[0].EndTime(), input.Values[0].MillisPerStep(), nil
Expand Down Expand Up @@ -186,14 +187,15 @@ func ParseInterval(fullInterval string) (time.Duration, error) {
allIntervals := reInterval.FindAllString(fullInterval, -1)
output := time.Duration(0)
if allIntervals == nil {
return 0, errors.NewInvalidParamsError(fmt.Errorf("Unrecognized interval string: %s", fullInterval))
return 0, xerrors.NewInvalidParamsError(
fmt.Errorf("unrecognized interval string: %s", fullInterval))
}

for _, interval := range allIntervals {
if m := reInterval.FindStringSubmatch(strings.TrimSpace(interval)); len(m) != 0 {
amount, err := strconv.ParseInt(m[1], 10, 32)
if err != nil {
return 0, errors.NewInvalidParamsError(err)
return 0, xerrors.NewInvalidParamsError(err)
}

interval := intervals[strings.ToLower(m[2])]
Expand Down
10 changes: 5 additions & 5 deletions src/query/graphite/common/basic_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"testing"
"time"

"github.com/m3db/m3/src/query/graphite/errors"
"github.com/m3db/m3/src/query/graphite/ts"
"github.com/m3db/m3/src/x/errors"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -110,10 +110,10 @@ func TestNormalize(t *testing.T) {
require.Equal(t, expectedEnd, end)
require.Equal(t, expectedStep, step)
expected := []TestSeries{
TestSeries{Name: "a", Data: []float64{nan, nan, nan, 10, 10, 10, 10, 10, 10, nan, nan, nan}},
TestSeries{Name: "b", Data: []float64{15, 15, 15, 15, 15, 15, nan, nan, nan, nan, nan, nan}},
TestSeries{Name: "c", Data: []float64{nan, nan, nan, nan, nan, nan, 17, 17, 17, 17, 17, 17}},
TestSeries{Name: "d", Data: []float64{nan, nan, nan, 3, 3, 3, 3, 3, 3, nan, nan, nan}},
{Name: "a", Data: []float64{nan, nan, nan, 10, 10, 10, 10, 10, 10, nan, nan, nan}},
{Name: "b", Data: []float64{15, 15, 15, 15, 15, 15, nan, nan, nan, nan, nan, nan}},
{Name: "c", Data: []float64{nan, nan, nan, nan, nan, nan, 17, 17, 17, 17, 17, 17}},
{Name: "d", Data: []float64{nan, nan, nan, 3, 3, 3, 3, 3, 3, nan, nan, nan}},
}

CompareOutputsAndExpected(t, expectedStep, expectedStart, expected, normalized.Values)
Expand Down
2 changes: 1 addition & 1 deletion src/query/graphite/common/percentiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"math"
"sort"

"github.com/m3db/m3/src/query/graphite/errors"
"github.com/m3db/m3/src/query/graphite/ts"
"github.com/m3db/m3/src/x/errors"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion src/query/graphite/common/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"fmt"
"math"

"github.com/m3db/m3/src/query/graphite/errors"
"github.com/m3db/m3/src/query/graphite/ts"
"github.com/m3db/m3/src/x/errors"
)

// TransformFunc is used by Transform to apply a function
Expand Down
99 changes: 0 additions & 99 deletions src/query/graphite/errors/errors.go

This file was deleted.

39 changes: 0 additions & 39 deletions src/query/graphite/errors/errors_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion src/query/graphite/graphite/glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"fmt"
"strings"

"github.com/m3db/m3/src/query/graphite/errors"
"github.com/m3db/m3/src/x/errors"
)

const (
Expand Down
Loading

0 comments on commit be8f4f6

Please sign in to comment.