fix(tsdb): Fix existing Prometheus tests based on batch cursors

pull/10007/head
Stuart Carnie 2018-07-13 14:13:04 -07:00
parent 497fc42779
commit d977c0ac24
5 changed files with 157 additions and 25 deletions

132
internal/array_cursors.go Normal file
View File

@ -0,0 +1,132 @@
package internal
import "github.com/influxdata/influxdb/tsdb"
var (
_ tsdb.IntegerArrayCursor = NewIntegerArrayCursorMock()
_ tsdb.FloatArrayCursor = NewFloatArrayCursorMock()
_ tsdb.UnsignedArrayCursor = NewUnsignedArrayCursorMock()
_ tsdb.StringArrayCursor = NewStringArrayCursorMock()
_ tsdb.BooleanArrayCursor = NewBooleanArrayCursorMock()
)
// ArrayCursorMock provides a mock base implementation for batch cursors.
type ArrayCursorMock struct {
CloseFn func()
ErrFn func() error
}
// NewArrayCursorMock returns an initialised ArrayCursorMock, which
// returns the zero value for all methods.
func NewArrayCursorMock() *ArrayCursorMock {
return &ArrayCursorMock{
CloseFn: func() {},
ErrFn: func() error { return nil },
}
}
// Close closes the cursor.
func (c *ArrayCursorMock) Close() { c.CloseFn() }
// Err returns the latest error, if any.
func (c *ArrayCursorMock) Err() error { return c.ErrFn() }
// IntegerArrayCursorMock provides a mock implementation of an IntegerArrayCursorMock.
type IntegerArrayCursorMock struct {
*ArrayCursorMock
NextFn func() *tsdb.IntegerArray
}
// NewIntegerArrayCursorMock returns an initialised IntegerArrayCursorMock, which
// returns the zero value for all methods.
func NewIntegerArrayCursorMock() *IntegerArrayCursorMock {
return &IntegerArrayCursorMock{
ArrayCursorMock: NewArrayCursorMock(),
NextFn: func() *tsdb.IntegerArray { return tsdb.NewIntegerArrayLen(0) },
}
}
// Next returns the next set of keys and values.
func (c *IntegerArrayCursorMock) Next() *tsdb.IntegerArray {
return c.NextFn()
}
// FloatArrayCursorMock provides a mock implementation of a FloatArrayCursor.
type FloatArrayCursorMock struct {
*ArrayCursorMock
NextFn func() *tsdb.FloatArray
}
// NewFloatArrayCursorMock returns an initialised FloatArrayCursorMock, which
// returns the zero value for all methods.
func NewFloatArrayCursorMock() *FloatArrayCursorMock {
return &FloatArrayCursorMock{
ArrayCursorMock: NewArrayCursorMock(),
NextFn: func() *tsdb.FloatArray { return tsdb.NewFloatArrayLen(0) },
}
}
// Next returns the next set of keys and values.
func (c *FloatArrayCursorMock) Next() *tsdb.FloatArray {
return c.NextFn()
}
// UnsignedArrayCursorMock provides a mock implementation of an UnsignedArrayCursorMock.
type UnsignedArrayCursorMock struct {
*ArrayCursorMock
NextFn func() *tsdb.UnsignedArray
}
// NewUnsignedArrayCursorMock returns an initialised UnsignedArrayCursorMock, which
// returns the zero value for all methods.
func NewUnsignedArrayCursorMock() *UnsignedArrayCursorMock {
return &UnsignedArrayCursorMock{
ArrayCursorMock: NewArrayCursorMock(),
NextFn: func() *tsdb.UnsignedArray { return tsdb.NewUnsignedArrayLen(0) },
}
}
// Next returns the next set of keys and values.
func (c *UnsignedArrayCursorMock) Next() *tsdb.UnsignedArray {
return c.NextFn()
}
// StringArrayCursorMock provides a mock implementation of a StringArrayCursor.
type StringArrayCursorMock struct {
*ArrayCursorMock
NextFn func() *tsdb.StringArray
}
// NewStringArrayCursorMock returns an initialised StringArrayCursorMock, which
// returns the zero value for all methods.
func NewStringArrayCursorMock() *StringArrayCursorMock {
return &StringArrayCursorMock{
ArrayCursorMock: NewArrayCursorMock(),
NextFn: func() *tsdb.StringArray { return tsdb.NewStringArrayLen(0) },
}
}
// Next returns the next set of keys and values.
func (c *StringArrayCursorMock) Next() *tsdb.StringArray {
return c.NextFn()
}
// BooleanArrayCursorMock provides a mock implementation of a BooleanArrayCursor.
type BooleanArrayCursorMock struct {
*ArrayCursorMock
NextFn func() *tsdb.BooleanArray
}
// NewBooleanArrayCursorMock returns an initialised BooleanArrayCursorMock, which
// returns the zero value for all methods.
func NewBooleanArrayCursorMock() *BooleanArrayCursorMock {
return &BooleanArrayCursorMock{
ArrayCursorMock: NewArrayCursorMock(),
NextFn: func() *tsdb.BooleanArray { return tsdb.NewBooleanArrayLen(0) },
}
}
// Next returns the next set of keys and values.
func (c *BooleanArrayCursorMock) Next() *tsdb.BooleanArray {
return c.NextFn()
}

View File

@ -1024,11 +1024,11 @@ func (h *Handler) servePromRead(w http.ResponseWriter, r *http.Request, user met
tags := prometheus.RemoveInfluxSystemTags(rs.Tags())
var unsupportedCursor string
switch cur := cur.(type) {
case tsdb.FloatBatchCursor:
case tsdb.FloatArrayCursor:
var series *remote.TimeSeries
for {
ts, vs := cur.Next()
if len(ts) == 0 {
a := cur.Next()
if a.Len() == 0 {
break
}
@ -1039,10 +1039,10 @@ func (h *Handler) servePromRead(w http.ResponseWriter, r *http.Request, user met
}
}
for i, ts := range ts {
for i, ts := range a.Timestamps {
series.Samples = append(series.Samples, &remote.Sample{
TimestampMs: ts / int64(time.Millisecond),
Value: vs[i],
Value: a.Values[i],
})
}
}
@ -1051,13 +1051,13 @@ func (h *Handler) servePromRead(w http.ResponseWriter, r *http.Request, user met
if series != nil {
resp.Results[0].Timeseries = append(resp.Results[0].Timeseries, series)
}
case tsdb.IntegerBatchCursor:
case tsdb.IntegerArrayCursor:
unsupportedCursor = "int64"
case tsdb.UnsignedBatchCursor:
case tsdb.UnsignedArrayCursor:
unsupportedCursor = "uint"
case tsdb.BooleanBatchCursor:
case tsdb.BooleanArrayCursor:
unsupportedCursor = "bool"
case tsdb.StringBatchCursor:
case tsdb.StringArrayCursor:
unsupportedCursor = "string"
default:
panic(fmt.Sprintf("unreachable: %T", cur))

View File

@ -630,17 +630,17 @@ func TestHandler_PromRead(t *testing.T) {
// data for each cursor.
h.Store.ResultSet.CursorFn = func() tsdb.Cursor {
cursor := internal.NewFloatBatchCursorMock()
cursor := internal.NewFloatArrayCursorMock()
var i int64
cursor.NextFn = func() ([]int64, []float64) {
cursor.NextFn = func() *tsdb.FloatArray {
i++
ts := []int64{22000000 * i, 10000000000 * i}
vs := []float64{2.3, 2992.33}
if i > 2 {
ts, vs = nil, nil
}
return ts, vs
return &tsdb.FloatArray{Timestamps: ts, Values: vs}
}
return cursor
@ -758,10 +758,10 @@ func TestHandler_PromRead_UnsupportedCursors(t *testing.T) {
compressed := snappy.Encode(nil, data)
unsupported := []tsdb.Cursor{
internal.NewIntegerBatchCursorMock(),
internal.NewBooleanBatchCursorMock(),
internal.NewUnsignedBatchCursorMock(),
internal.NewStringBatchCursorMock(),
internal.NewIntegerArrayCursorMock(),
internal.NewBooleanArrayCursorMock(),
internal.NewUnsignedArrayCursorMock(),
internal.NewStringArrayCursorMock(),
}
for _, cursor := range unsupported {

View File

@ -1794,12 +1794,12 @@ func TestEngine_CreateCursor_Ascending(t *testing.T) {
}
defer cur.Close()
fcur := cur.(tsdb.FloatBatchCursor)
ts, vs := fcur.Next()
if !cmp.Equal([]int64{2, 3, 10, 11}, ts) {
fcur := cur.(tsdb.FloatArrayCursor)
a := fcur.Next()
if !cmp.Equal([]int64{2, 3, 10, 11}, a.Timestamps) {
t.Fatal("unexpect timestamps")
}
if !cmp.Equal([]float64{1.2, 1.3, 10.1, 11.2}, vs) {
if !cmp.Equal([]float64{1.2, 1.3, 10.1, 11.2}, a.Values) {
t.Fatal("unexpect timestamps")
}
})
@ -1854,12 +1854,12 @@ func TestEngine_CreateCursor_Descending(t *testing.T) {
}
defer cur.Close()
fcur := cur.(tsdb.FloatBatchCursor)
ts, vs := fcur.Next()
if !cmp.Equal([]int64{11, 10, 3, 2}, ts) {
fcur := cur.(tsdb.FloatArrayCursor)
a := fcur.Next()
if !cmp.Equal([]int64{11, 10, 3, 2}, a.Timestamps) {
t.Fatal("unexpect timestamps")
}
if !cmp.Equal([]float64{11.2, 10.1, 1.3, 1.2}, vs) {
if !cmp.Equal([]float64{11.2, 10.1, 1.3, 1.2}, a.Values) {
t.Fatal("unexpect timestamps")
}
})

View File

@ -355,7 +355,7 @@ func TestFileStore_Array(t *testing.T) {
}
if len(read) == 0 {
exp = nil
exp = tsdb.NewFloatArrayLen(0)
}
if !cmp.Equal(values, exp) {