Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate previous value in derivative calls #4993

Merged
merged 1 commit into from
Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

### Bugfixes
- [#4984](https://github.com/influxdb/influxdb/pull/4984): Allow math on fields, fixes regression. Thanks @mengjinglei
- [#4666](https://github.com/influxdb/influxdb/issues/4666): Fix panic in derivative with invalid values.
- [#4858](https://github.com/influxdb/influxdb/pull/4858): Validate nested aggregations in queries. Thanks @viru
- [#4921](https://github.com/influxdb/influxdb/pull/4921): Error responses should be JSON-formatted. Thanks @pires
- [#4974](https://github.com/influxdb/influxdb/issues/4974) Fix Data Race in TSDB when setting measurement field name
Expand Down
16 changes: 12 additions & 4 deletions tsdb/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,15 +702,23 @@ func ProcessAggregateDerivative(results [][]interface{}, isNonNegative bool, int

// Check the value's type to ensure it's an numeric, if not, return a nil result. We only check the first value
// because derivatives cannot be combined with other aggregates currently.
validType := false
curValidType := false
switch cur[1].(type) {
case int64:
validType = true
curValidType = true
case float64:
validType = true
curValidType = true
}

if !validType {
prevValidType := false
switch prev[1].(type) {
case int64:
prevValidType = true
case float64:
prevValidType = true
}

if !curValidType || !prevValidType {
derivatives = append(derivatives, []interface{}{
cur[0], nil,
})
Expand Down
34 changes: 34 additions & 0 deletions tsdb/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,40 @@ func TestProcessAggregateDerivative_Bool(t *testing.T) {
}
}

func TestProcessAggregateDerivative_FirstInvalid(t *testing.T) {
results := tsdb.ProcessAggregateDerivative([][]interface{}{
[]interface{}{time.Unix(0, 0), "1.0"},
[]interface{}{time.Unix(0, 0).Add(24 * time.Hour), 2.0},
[]interface{}{time.Unix(0, 0).Add(48 * time.Hour), 3.0},
[]interface{}{time.Unix(0, 0).Add(72 * time.Hour), 4.0},
}, false, 24*time.Hour)

if !reflect.DeepEqual(results, [][]interface{}{
[]interface{}{time.Unix(0, 0).Add(24 * time.Hour), nil},
[]interface{}{time.Unix(0, 0).Add(48 * time.Hour), 1.0},
[]interface{}{time.Unix(0, 0).Add(72 * time.Hour), 1.0},
}) {
t.Fatalf("unexpected results: %s", spew.Sdump(results))
}
}

func TestProcessAggregateDerivative_RandomInvalid(t *testing.T) {
results := tsdb.ProcessAggregateDerivative([][]interface{}{
[]interface{}{time.Unix(0, 0), 1.0},
[]interface{}{time.Unix(0, 0).Add(24 * time.Hour), 2.0},
[]interface{}{time.Unix(0, 0).Add(48 * time.Hour), "3.0"},
[]interface{}{time.Unix(0, 0).Add(72 * time.Hour), 4.0},
}, false, 24*time.Hour)

if !reflect.DeepEqual(results, [][]interface{}{
[]interface{}{time.Unix(0, 0).Add(24 * time.Hour), 1.0},
[]interface{}{time.Unix(0, 0).Add(48 * time.Hour), nil},
[]interface{}{time.Unix(0, 0).Add(72 * time.Hour), nil},
}) {
t.Fatalf("unexpected results: %s", spew.Sdump(results))
}
}

func TestRawQueryDerivative_Process_Empty(t *testing.T) {
p := tsdb.RawQueryDerivativeProcessor{
IsNonNegative: false,
Expand Down