forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal_test.go
372 lines (314 loc) · 11 KB
/
internal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package influxdb
// This file is run within the "influxdb" package and allows for internal unit tests.
import (
"bytes"
"fmt"
"reflect"
"testing"
"time"
"github.com/influxdb/influxdb/influxql"
)
// Ensure a measurement can return a set of unique tag values specified by an expression.
func TestMeasurement_uniqueTagValues(t *testing.T) {
// Create a measurement to run against.
m := NewMeasurement("cpu")
m.createFieldIfNotExists("value", influxql.Float)
for i, tt := range []struct {
expr string
values map[string][]string
}{
{expr: `1`, values: map[string][]string{}},
{expr: `foo = 'bar'`, values: map[string][]string{"foo": {"bar"}}},
{expr: `(region = 'us-west' AND value > 10) OR ('us-east' = region AND value > 20) OR (host = 'serverA' AND value > 30)`, values: map[string][]string{"region": {"us-east", "us-west"}, "host": {"serverA"}}},
} {
// Extract unique tag values from the expression.
values := m.uniqueTagValues(MustParseExpr(tt.expr))
if !reflect.DeepEqual(tt.values, values) {
t.Errorf("%d. %s: mismatch: exp=%+v, got=%+v", i, tt.expr, tt.values, values)
}
}
}
// Ensure a measurement can expand an expression for all possible tag values used.
func TestMeasurement_expandExpr(t *testing.T) {
m := NewMeasurement("cpu")
m.createFieldIfNotExists("value", influxql.Float)
type tagSetExprString struct {
tagExpr []tagExpr
expr string
}
for i, tt := range []struct {
expr string
exprs []tagSetExprString
}{
// Single tag key, single value.
{
expr: `region = 'us-east' AND value > 10`,
exprs: []tagSetExprString{
{tagExpr: []tagExpr{{"region", []string{"us-east"}, influxql.EQ}}, expr: `value > 10.000`},
},
},
// Single tag key, multiple values.
{
expr: `(region = 'us-east' AND value > 10) OR (region = 'us-west' AND value > 20)`,
exprs: []tagSetExprString{
{tagExpr: []tagExpr{{"region", []string{"us-east"}, influxql.EQ}}, expr: `value > 10.000`},
{tagExpr: []tagExpr{{"region", []string{"us-west"}, influxql.EQ}}, expr: `value > 20.000`},
},
},
// Multiple tag keys, multiple values.
{
expr: `(region = 'us-east' AND value > 10) OR ((host = 'serverA' OR host = 'serverB') AND value > 20)`,
exprs: []tagSetExprString{
{tagExpr: []tagExpr{{key: "host", values: []string{"serverA"}, op: influxql.EQ}, {key: "region", values: []string{"us-east"}, op: influxql.EQ}}, expr: "(value > 10.000) OR (value > 20.000)"},
{tagExpr: []tagExpr{{key: "host", values: []string{"serverA"}, op: influxql.EQ}, {key: "region", values: []string{"us-east"}, op: influxql.NEQ}}, expr: "value > 20.000"},
{tagExpr: []tagExpr{{key: "host", values: []string{"serverB"}, op: influxql.EQ}, {key: "region", values: []string{"us-east"}, op: influxql.EQ}}, expr: "(value > 10.000) OR (value > 20.000)"},
{tagExpr: []tagExpr{{key: "host", values: []string{"serverB"}, op: influxql.EQ}, {key: "region", values: []string{"us-east"}, op: influxql.NEQ}}, expr: "value > 20.000"},
{tagExpr: []tagExpr{{key: "host", values: []string{"serverA", "serverB"}, op: influxql.NEQ}, {key: "region", values: []string{"us-east"}, op: influxql.EQ}}, expr: "value > 10.000"},
},
},
} {
// Expand out an expression to all possible expressions based on tag values.
tagExprs := m.expandExpr(MustParseExpr(tt.expr))
// Convert to intermediate representation.
var a []tagSetExprString
for _, tagExpr := range tagExprs {
a = append(a, tagSetExprString{tagExpr: tagExpr.values, expr: tagExpr.expr.String()})
}
// Validate that the expanded expressions are what we expect.
if !reflect.DeepEqual(tt.exprs, a) {
t.Errorf("%d. %s: mismatch:\n\nexp=%#v\n\ngot=%#v\n\ns", i, tt.expr, tt.exprs, a)
}
}
}
// Ensure the createMeasurementsIfNotExistsCommand operates correctly.
func TestCreateMeasurementsCommand(t *testing.T) {
var err error
var n int
c := newCreateMeasurementsIfNotExistsCommand("foo")
if c == nil {
t.Fatal("createMeasurementsIfNotExistsCommand is nil")
}
// Add Measurement twice, to make sure nothing blows up.
c.addMeasurementIfNotExists("bar")
c.addMeasurementIfNotExists("bar")
n = len(c.Measurements)
if n != 1 {
t.Fatalf("wrong number of measurements, expected 1, got %d", n)
}
// Add Series, no tags.
c.addSeriesIfNotExists("bar", nil)
// Add Series, some tags.
tags := map[string]string{"host": "server01"}
c.addSeriesIfNotExists("bar", tags)
// Add Series, same tags again.
c.addSeriesIfNotExists("bar", tags)
for _, m := range c.Measurements {
if m.Name == "bar" {
if len(m.Tags) != 2 {
t.Fatalf("measurement has wrong number of tags, expected 2, got %d", n)
}
}
}
// Add a field.
err = c.addFieldIfNotExists("bar", "value", influxql.Integer)
if err != nil {
t.Fatal("error adding field \"value\"")
}
// Add same field again.
err = c.addFieldIfNotExists("bar", "value", influxql.Integer)
if err != nil {
t.Fatal("error re-adding field \"value\"")
}
// Add another field.
err = c.addFieldIfNotExists("bar", "value2", influxql.String)
if err != nil {
t.Fatal("error re-adding field \"value2\"")
}
for _, m := range c.Measurements {
if m.Name == "bar" {
if len(m.Fields) != 2 {
t.Fatalf("measurement has wrong number of fields, expected 2, got %d", n)
}
}
}
}
// Ensure the createMeasurementsIfNotExistsCommand returns expected errors.
func TestCreateMeasurementsCommand_Errors(t *testing.T) {
var err error
c := newCreateMeasurementsIfNotExistsCommand("foo")
if c == nil {
t.Fatal("createMeasurementsIfNotExistsCommand is nil")
}
// Ensure fields can be added to non-existent Measurements. The
// Measurements should be created automatically.
c.addSeriesIfNotExists("bar", nil)
err = c.addFieldIfNotExists("bar", "value", influxql.Float)
if err != nil {
t.Fatalf("unexpected error got %s", err.Error())
}
// Add Measurement. Adding it now should be OK.
c.addMeasurementIfNotExists("bar")
// Test type conflicts
err = c.addFieldIfNotExists("bar", "value", influxql.Float)
if err != nil {
t.Fatal("error adding field \"value\"")
}
err = c.addFieldIfNotExists("bar", "value", influxql.String)
if err != ErrFieldTypeConflict {
t.Fatalf("expected ErrFieldTypeConflict got %s", err.Error())
}
}
// Test comparing seriesIDs for equality.
func Test_seriesIDs_equals(t *testing.T) {
ids1 := seriesIDs{1, 2, 3}
ids2 := seriesIDs{1, 2, 3}
ids3 := seriesIDs{4, 5, 6}
if !ids1.equals(ids2) {
t.Fatal("expected ids1 == ids2")
} else if ids1.equals(ids3) {
t.Fatal("expected ids1 != ids3")
}
}
// Test intersecting sets of seriesIDs.
func Test_seriesIDs_intersect(t *testing.T) {
// Test swaping l & r, all branches of if-else, and exit loop when 'j < len(r)'
ids1 := seriesIDs{1, 3, 4, 5, 6}
ids2 := seriesIDs{1, 2, 3, 7}
exp := seriesIDs{1, 3}
got := ids1.intersect(ids2)
if !exp.equals(got) {
t.Fatalf("exp=%v, got=%v", exp, got)
}
// Test exit for loop when 'i < len(l)'
ids1 = seriesIDs{1}
ids2 = seriesIDs{1, 2}
exp = seriesIDs{1}
got = ids1.intersect(ids2)
if !exp.equals(got) {
t.Fatalf("exp=%v, got=%v", exp, got)
}
}
// Test union sets of seriesIDs.
func Test_seriesIDs_union(t *testing.T) {
// Test all branches of if-else, exit loop because of 'j < len(r)', and append remainder from left.
ids1 := seriesIDs{1, 2, 3, 7}
ids2 := seriesIDs{1, 3, 4, 5, 6}
exp := seriesIDs{1, 2, 3, 4, 5, 6, 7}
got := ids1.union(ids2)
if !exp.equals(got) {
t.Fatalf("exp=%v, got=%v", exp, got)
}
// Test exit because of 'i < len(l)' and append remainder from right.
ids1 = seriesIDs{1}
ids2 = seriesIDs{1, 2}
exp = seriesIDs{1, 2}
got = ids1.union(ids2)
if !exp.equals(got) {
t.Fatalf("exp=%v, got=%v", exp, got)
}
}
// Test removing one set of seriesIDs from another.
func Test_seriesIDs_reject(t *testing.T) {
// Test all branches of if-else, exit loop because of 'j < len(r)', and append remainder from left.
ids1 := seriesIDs{1, 2, 3, 7}
ids2 := seriesIDs{1, 3, 4, 5, 6}
exp := seriesIDs{2, 7}
got := ids1.reject(ids2)
if !exp.equals(got) {
t.Fatalf("exp=%v, got=%v", exp, got)
}
// Test exit because of 'i < len(l)'.
ids1 = seriesIDs{1}
ids2 = seriesIDs{1, 2}
exp = seriesIDs{}
got = ids1.reject(ids2)
if !exp.equals(got) {
t.Fatalf("exp=%v, got=%v", exp, got)
}
}
// Test shard group selection.
func TestShardGroup_Contains(t *testing.T) {
// Make a shard group 1 hour in duration
tm, _ := time.Parse(time.RFC3339, "2000-01-01T00:00:00Z")
g := newShardGroup(tm, time.Hour)
if !g.Contains(g.StartTime.Add(-time.Minute), g.EndTime) {
t.Fatal("shard group not selected when min before start time")
}
if !g.Contains(g.StartTime, g.EndTime.Add(time.Minute)) {
t.Fatal("shard group not selected when max after after end time")
}
if !g.Contains(g.StartTime.Add(-time.Minute), g.EndTime.Add(time.Minute)) {
t.Fatal("shard group not selected when min before start time and when max after end time")
}
if !g.Contains(g.StartTime.Add(time.Minute), g.EndTime.Add(-time.Minute)) {
t.Fatal("shard group not selected when min after start time and when max before end time")
}
if !g.Contains(g.StartTime, g.EndTime) {
t.Fatal("shard group not selected when min at start time and when max at end time")
}
if !g.Contains(g.StartTime, g.StartTime) {
t.Fatal("shard group not selected when min and max set to start time")
}
if !g.Contains(g.EndTime, g.EndTime) {
t.Fatal("shard group not selected when min and max set to end time")
}
if g.Contains(g.StartTime.Add(-10*time.Hour), g.EndTime.Add(-9*time.Hour)) {
t.Fatal("shard group selected when both min and max before shard times")
}
if g.Contains(g.StartTime.Add(24*time.Hour), g.EndTime.Add(25*time.Hour)) {
t.Fatal("shard group selected when both min and max after shard times")
}
}
// Ensure tags can be marshaled into a byte slice.
func TestMarshalTags(t *testing.T) {
for i, tt := range []struct {
tags map[string]string
result []byte
}{
{
tags: nil,
result: nil,
},
{
tags: map[string]string{"foo": "bar"},
result: []byte(`foo|bar`),
},
{
tags: map[string]string{"foo": "bar", "baz": "battttt"},
result: []byte(`baz|foo|battttt|bar`),
},
} {
result := marshalTags(tt.tags)
if !bytes.Equal(result, tt.result) {
t.Fatalf("%d. unexpected result: exp=%s, got=%s", i, tt.result, result)
}
}
}
func BenchmarkMarshalTags_KeyN1(b *testing.B) { benchmarkMarshalTags(b, 1) }
func BenchmarkMarshalTags_KeyN3(b *testing.B) { benchmarkMarshalTags(b, 3) }
func BenchmarkMarshalTags_KeyN5(b *testing.B) { benchmarkMarshalTags(b, 5) }
func BenchmarkMarshalTags_KeyN10(b *testing.B) { benchmarkMarshalTags(b, 10) }
func benchmarkMarshalTags(b *testing.B, keyN int) {
const keySize, valueSize = 8, 15
// Generate tag map.
tags := make(map[string]string)
for i := 0; i < keyN; i++ {
tags[fmt.Sprintf("%0*d", keySize, i)] = fmt.Sprintf("%0*d", valueSize, i)
}
// Unmarshal map into byte slice.
b.ReportAllocs()
for i := 0; i < b.N; i++ {
marshalTags(tags)
}
}
// MustParseExpr parses an expression string and returns its AST representation.
func MustParseExpr(s string) influxql.Expr {
expr, err := influxql.ParseExpr(s)
if err != nil {
panic(err.Error())
}
return expr
}
func strref(s string) *string {
return &s
}