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

fix(storage): array cursor iterator should return stats of all observed cursors #15731

Merged
merged 4 commits into from
Nov 5, 2019
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v2.0.0-alpha.20 [unreleased]

### Bug Fixes

1. [15731](https://github.com/influxdata/influxdb/pull/15731): Ensure array cursor iterator stats accumulate all cursor stats

## v2.0.0-alpha.19 [2019-10-30]

### Features
Expand Down
24 changes: 16 additions & 8 deletions tsdb/tsm1/array_cursor_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,32 @@ func (q *arrayCursorIterator) Stats() cursors.CursorStats {
var stats cursors.CursorStats
if cur := q.asc.Float; cur != nil {
stats.Add(cur.Stats())
} else if cur := q.asc.Integer; cur != nil {
}
if cur := q.asc.Integer; cur != nil {
stats.Add(cur.Stats())
} else if cur := q.asc.Unsigned; cur != nil {
}
if cur := q.asc.Unsigned; cur != nil {
stats.Add(cur.Stats())
} else if cur := q.asc.Boolean; cur != nil {
}
if cur := q.asc.Boolean; cur != nil {
stats.Add(cur.Stats())
} else if cur := q.asc.String; cur != nil {
}
if cur := q.asc.String; cur != nil {
stats.Add(cur.Stats())
}
if cur := q.desc.Float; cur != nil {
stats.Add(cur.Stats())
} else if cur := q.desc.Integer; cur != nil {
}
if cur := q.desc.Integer; cur != nil {
stats.Add(cur.Stats())
} else if cur := q.desc.Unsigned; cur != nil {
}
if cur := q.desc.Unsigned; cur != nil {
stats.Add(cur.Stats())
} else if cur := q.desc.Boolean; cur != nil {
}
if cur := q.desc.Boolean; cur != nil {
stats.Add(cur.Stats())
} else if cur := q.desc.String; cur != nil {
}
if cur := q.desc.String; cur != nil {
stats.Add(cur.Stats())
}
return stats
Expand Down
117 changes: 117 additions & 0 deletions tsdb/tsm1/engine_cursor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package tsm1_test

import (
"context"
"testing"
"time"

"github.com/influxdata/influxdb/models"
"github.com/influxdata/influxdb/tsdb"
"github.com/influxdata/influxdb/tsdb/cursors"
)

func TestEngine_CursorIterator_Stats(t *testing.T) {
e := MustOpenEngine()
defer e.Close()

points := []models.Point{
models.MustNewPoint("cpu",
models.Tags{
{Key: []byte("a"), Value: []byte("b")},
},
models.Fields{"value": 4.6},
time.Now().UTC(),
),
models.MustNewPoint("cpu",
models.Tags{
{Key: []byte("a"), Value: []byte("b")},
},
models.Fields{"value": 3.2},
time.Now().UTC(),
),
models.MustNewPoint("mem",
models.Tags{
{Key: []byte("b"), Value: []byte("c")},
},
models.Fields{"value": int64(3)},
time.Now().UTC(),
),
}

// Write into the index.
collection := tsdb.NewSeriesCollection(points)
if err := e.index.CreateSeriesListIfNotExists(collection); err != nil {
t.Fatal(err)
}

if err := e.WritePoints(points); err != nil {
t.Fatal(err)
}

e.MustWriteSnapshot()

ctx := context.Background()
cursorIterator, err := e.CreateCursorIterator(ctx)
if err != nil {
t.Fatal(err)
}

cur, err := cursorIterator.Next(ctx, &tsdb.CursorRequest{
Name: []byte("cpu"),
Tags: []models.Tag{{Key: []byte("a"), Value: []byte("b")}},
Field: "value",
EndTime: time.Now().UTC().UnixNano(),
Ascending: true,
})

if err != nil {
t.Fatal(err)
}

if cur == nil {
t.Fatal("expected cursor to be present")
}

fc, ok := cur.(cursors.FloatArrayCursor)
if !ok {
t.Fatalf("unexpected cursor type: expected FloatArrayCursor, got %#v", cur)
}

// drain the cursor
for a := fc.Next(); a.Len() > 0; a = fc.Next() {
}

cur.Close()

cur, err = cursorIterator.Next(ctx, &tsdb.CursorRequest{
Name: []byte("mem"),
Tags: []models.Tag{{Key: []byte("b"), Value: []byte("c")}},
Field: "value",
EndTime: time.Now().UTC().UnixNano(),
Ascending: true,
})

if err != nil {
t.Fatal(err)
}

if cur == nil {
t.Fatal("expected cursor to be present")
}

defer cur.Close()

ic, ok := cur.(cursors.IntegerArrayCursor)
if !ok {
t.Fatalf("unexpected cursor type: expected FloatArrayCursor, got %#v", cur)
}

// drain the cursor
for a := ic.Next(); a.Len() > 0; a = ic.Next() {
}

// iterator should report integer array stats
if got, exp := cursorIterator.Stats(), (cursors.CursorStats{ScannedValues: 3, ScannedBytes: 24}); exp != got {
t.Fatalf("expected %v, got %v", exp, got)
}
}