forked from grafana-cold-storage/metrictank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_engine_test.go
More file actions
530 lines (498 loc) · 16.4 KB
/
Copy pathquery_engine_test.go
File metadata and controls
530 lines (498 loc) · 16.4 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package api
import (
"regexp"
"testing"
"github.com/grafana/metrictank/api/models"
"github.com/grafana/metrictank/conf"
"github.com/grafana/metrictank/consolidation"
"github.com/grafana/metrictank/mdata"
"github.com/grafana/metrictank/test"
)
// testAlign verifies the aligment of the given requests, given the retentions (one or more patterns, one or more retentions each)
func testAlign(reqs []models.Req, retentions [][]conf.Retention, outReqs []models.Req, outErr error, now uint32, t *testing.T) {
var schemas []conf.Schema
oriMaxPointsPerReqSoft := maxPointsPerReqSoft
for _, ret := range retentions {
schemas = append(schemas, conf.Schema{
Pattern: regexp.MustCompile(".*"),
Retentions: conf.Retentions(ret),
})
// make sure maxPointsPerReqSoft is high enough
points := (int(reqs[0].To-reqs[0].From) / ret[0].SecondsPerPoint) * len(reqs)
if points > maxPointsPerReqSoft {
maxPointsPerReqSoft = points
}
}
mdata.Schemas = conf.NewSchemas(schemas)
out, _, _, err := alignRequests(now, reqs[0].From, reqs[0].To, reqs)
if err != outErr {
t.Errorf("different err value expected: %v, got: %v", outErr, err)
}
if len(out) != len(outReqs) {
t.Errorf("different number of requests expected: %v, got: %v", len(outReqs), len(out))
} else {
for r, exp := range outReqs {
if !exp.Equals(out[r]) {
t.Errorf("request %d:\nexpected: %v\n got: %v", r, exp.DebugString(), out[r].DebugString())
}
}
}
maxPointsPerReqSoft = oriMaxPointsPerReqSoft
}
// 2 series requested with equal raw intervals. req 0-30. now 1200. one archive of ttl=1200 does it
func TestAlignRequestsBasic(t *testing.T) {
testAlign([]models.Req{
reqRaw(test.GetMKey(1), 0, 30, 800, 60, consolidation.Avg, 0, 0),
reqRaw(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 0, 0),
},
[][]conf.Retention{
{
conf.NewRetentionMT(60, 1200, 0, 0, true),
},
},
[]models.Req{
reqOut(test.GetMKey(1), 0, 30, 800, 60, consolidation.Avg, 0, 0, 0, 60, 1200, 60, 1),
reqOut(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 0, 0, 0, 60, 1200, 60, 1),
},
nil,
1200,
t,
)
}
// 2 series requested with equal raw intervals from different schemas. req 0-30. now 1200. their archives of ttl=1200 do it
func TestAlignRequestsBasicDiff(t *testing.T) {
testAlign([]models.Req{
reqRaw(test.GetMKey(1), 0, 30, 800, 60, consolidation.Avg, 0, 0),
reqRaw(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 1, 0),
},
[][]conf.Retention{
{
conf.NewRetentionMT(60, 1200, 0, 0, true),
},
{
conf.NewRetentionMT(60, 1200, 0, 0, true),
},
},
[]models.Req{
reqOut(test.GetMKey(1), 0, 30, 800, 60, consolidation.Avg, 0, 0, 0, 60, 1200, 60, 1),
reqOut(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 1, 0, 0, 60, 1200, 60, 1),
},
nil,
1200,
t,
)
}
// 2 series requested with different raw intervals from different schemas. req 0-30. now 1200. their archives of ttl=1200 do it, but needs normalizing
// (real example seen with alerting queries)
func TestAlignRequestsAlerting(t *testing.T) {
testAlign([]models.Req{
reqRaw(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0),
reqRaw(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 1, 0),
},
[][]conf.Retention{{
conf.NewRetentionMT(10, 1200, 0, 0, true),
}, {
conf.NewRetentionMT(60, 1200, 0, 0, true),
},
},
[]models.Req{
reqOut(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0, 0, 10, 1200, 60, 6),
reqOut(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 1, 0, 0, 60, 1200, 60, 1),
},
nil,
1200,
t,
)
}
// 2 series requested with different raw intervals from different schemas. req 0-30. now 1200. neither has long enough archive. no rollups, so best effort from raw
func TestAlignRequestsBasicBestEffort(t *testing.T) {
testAlign([]models.Req{
reqRaw(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0),
reqRaw(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 1, 0),
},
[][]conf.Retention{
{
conf.NewRetentionMT(10, 800, 0, 0, true),
}, {
conf.NewRetentionMT(60, 1100, 0, 0, true),
},
},
[]models.Req{
reqOut(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0, 0, 10, 800, 60, 6),
reqOut(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 1, 0, 0, 60, 1100, 60, 1),
},
nil,
1200,
t,
)
}
// 2 series requested with different raw intervals from the same schemas. Both requests should use the 60second rollups
func TestAlignRequestsMultipleIntervalsPerSchema(t *testing.T) {
testAlign([]models.Req{
reqRaw(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0),
reqRaw(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 1, 0),
},
[][]conf.Retention{
{
conf.NewRetentionMT(1, 800, 0, 0, true),
conf.NewRetentionMT(60, 1100, 0, 0, true),
},
},
[]models.Req{
reqOut(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0, 1, 60, 800, 60, 1),
reqOut(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 1, 0, 0, 60, 1100, 60, 1),
},
nil,
1200,
t,
)
}
// 2 series requested with different raw intervals from the same schemas. Both requests should use raw
func TestAlignRequestsMultiIntervalsWithRuntimeConsolidation(t *testing.T) {
testAlign([]models.Req{
reqRaw(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0),
reqRaw(test.GetMKey(2), 0, 30, 800, 30, consolidation.Avg, 0, 0),
},
[][]conf.Retention{
{
conf.NewRetentionMT(10, 800, 0, 0, true),
conf.NewRetentionMT(60, 1200, 0, 0, true),
},
},
[]models.Req{
reqOut(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0, 0, 10, 800, 30, 3),
reqOut(test.GetMKey(2), 0, 30, 800, 30, consolidation.Avg, 0, 0, 0, 30, 800, 30, 1),
},
nil,
800,
t,
)
}
// 2 series requested with different raw intervals from different schemas. req 0-30. now 1200. one has short raw. other has short raw + good rollup
func TestAlignRequestsHalfGood(t *testing.T) {
testAlign([]models.Req{
reqRaw(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0),
reqRaw(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 1, 0),
},
[][]conf.Retention{
{
conf.NewRetentionMT(10, 800, 0, 0, true),
}, {
conf.NewRetentionMT(60, 1100, 0, 0, true),
conf.NewRetentionMT(120, 1200, 0, 0, true),
},
},
[]models.Req{
reqOut(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0, 0, 10, 800, 120, 12),
reqOut(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 1, 0, 1, 120, 1200, 120, 1),
},
nil,
1200,
t,
)
}
// 2 series requested with different raw intervals from different schemas. req 0-30. now 1200. both have short raw + good rollup
func TestAlignRequestsGoodRollup(t *testing.T) {
testAlign([]models.Req{
reqRaw(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0),
reqRaw(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 2, 0),
},
[][]conf.Retention{
{
conf.NewRetentionMT(10, 1199, 0, 0, true), // just not long enough
conf.NewRetentionMT(120, 1200, 600, 2, true),
},
{
conf.NewRetentionMT(60, 1199, 0, 0, true), // just not long enough
conf.NewRetentionMT(120, 1200, 600, 2, true),
},
},
[]models.Req{
reqOut(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0, 1, 120, 1200, 120, 1),
reqOut(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 2, 0, 1, 120, 1200, 120, 1),
},
nil,
1200,
t,
)
}
// 2 series requested with different raw intervals, and rollup intervals from different schemas. req 0-30. now 1200. both have short raw + good rollup
func TestAlignRequestsDiffGoodRollup(t *testing.T) {
testAlign([]models.Req{
reqRaw(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0),
reqRaw(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 2, 0),
},
[][]conf.Retention{
{
conf.NewRetentionMT(10, 1199, 0, 0, true), // just not long enough
conf.NewRetentionMT(100, 1200, 600, 2, true),
},
{
conf.NewRetentionMT(60, 1199, 0, 0, true), // just not long enough
conf.NewRetentionMT(600, 1200, 600, 2, true),
},
},
[]models.Req{
reqOut(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0, 1, 100, 1200, 600, 6),
reqOut(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 2, 0, 1, 600, 1200, 600, 1),
},
nil,
1200,
t,
)
}
// now raw is short and we have a rollup we can use instead, at same interval as one of the raws
func TestAlignRequestsWeird(t *testing.T) {
testAlign([]models.Req{
reqRaw(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0),
reqRaw(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 2, 0),
},
[][]conf.Retention{
{
conf.NewRetentionMT(10, 1199, 0, 0, true),
conf.NewRetentionMT(60, 1200, 600, 2, true),
},
{
conf.NewRetentionMT(60, 1200, 0, 0, true),
},
},
[]models.Req{
reqOut(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0, 1, 60, 1200, 60, 1),
reqOut(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 2, 0, 0, 60, 1200, 60, 1),
},
nil,
1200,
t,
)
}
// now TTL of first rollup is *just* enough
func TestAlignRequestsWeird2(t *testing.T) {
testAlign([]models.Req{
reqRaw(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0),
reqRaw(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 2, 0),
},
[][]conf.Retention{
{
conf.NewRetentionMT(10, 1100, 0, 0, true), // just not long enough
conf.NewRetentionMT(120, 1200, 600, 2, true),
},
{
conf.NewRetentionMT(60, 1100, 0, 0, true), // just not long enough
conf.NewRetentionMT(120, 1200, 600, 2, true),
},
},
[]models.Req{
reqOut(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0, 1, 120, 1200, 120, 1),
reqOut(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 2, 0, 1, 120, 1200, 120, 1),
},
nil,
1200,
t,
)
}
// now TTL of first rollup is not enough but we have no other choice but to use it
func TestAlignRequestsNoOtherChoice(t *testing.T) {
testAlign([]models.Req{
reqRaw(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0),
reqRaw(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 2, 0),
},
[][]conf.Retention{
{
conf.NewRetentionMT(10, 1100, 0, 0, true),
conf.NewRetentionMT(120, 1199, 600, 2, true),
},
{
conf.NewRetentionMT(60, 1100, 0, 0, true),
conf.NewRetentionMT(120, 1199, 600, 2, true),
},
},
[]models.Req{
reqOut(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0, 1, 120, 1199, 120, 1),
reqOut(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 2, 0, 1, 120, 1199, 120, 1),
},
nil,
1200,
t,
)
}
// now TTL of first rollup is not enough and we have a 3rd band to use
func TestAlignRequests3rdBand(t *testing.T) {
testAlign([]models.Req{
reqRaw(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0),
reqRaw(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 3, 0),
},
[][]conf.Retention{
{
conf.NewRetentionMT(1, 1100, 0, 0, true),
conf.NewRetentionMT(120, 1199, 600, 2, true),
conf.NewRetentionMT(240, 1200, 600, 2, true),
},
{
conf.NewRetentionMT(60, 1100, 0, 0, true),
conf.NewRetentionMT(240, 1200, 600, 2, true),
},
},
[]models.Req{
reqOut(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0, 2, 240, 1200, 240, 1),
reqOut(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 3, 0, 1, 240, 1200, 240, 1),
},
nil,
1200,
t,
)
}
// now TTL of raw/first rollup is not enough but the two rollups are disabled, so must use raw
func TestAlignRequests2RollupsDisabled(t *testing.T) {
testAlign([]models.Req{
reqRaw(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0),
reqRaw(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 3, 0),
},
[][]conf.Retention{
{
conf.NewRetentionMT(10, 1100, 0, 0, true), // just not long enough
conf.NewRetentionMT(120, 1199, 600, 2, false),
conf.NewRetentionMT(240, 1200, 600, 2, false),
},
{
conf.NewRetentionMT(60, 1100, 0, 0, true), // just not long enough
conf.NewRetentionMT(240, 1200, 600, 2, false),
},
},
[]models.Req{
reqOut(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0, 0, 10, 1100, 60, 6),
reqOut(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 3, 0, 0, 60, 1100, 60, 1),
},
nil,
1200,
t,
)
}
func TestAlignRequestsHuh(t *testing.T) {
testAlign([]models.Req{
reqRaw(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0),
reqRaw(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 3, 0),
},
[][]conf.Retention{
{
conf.NewRetentionMT(1, 1000, 0, 0, true),
conf.NewRetentionMT(120, 1080, 600, 2, true),
conf.NewRetentionMT(240, 1200, 600, 2, false),
},
{
conf.NewRetentionMT(60, 1100, 0, 0, true),
conf.NewRetentionMT(240, 1200, 600, 2, false),
},
},
[]models.Req{
reqOut(test.GetMKey(1), 0, 30, 800, 10, consolidation.Avg, 0, 0, 1, 120, 1080, 120, 1),
reqOut(test.GetMKey(2), 0, 30, 800, 60, consolidation.Avg, 3, 0, 0, 60, 1100, 120, 2),
},
nil,
1200,
t,
)
}
var hour uint32 = 60 * 60
var day uint32 = 24 * hour
func testMaxPointsPerReq(maxPointsSoft, maxPointsHard int, reqs []models.Req, t *testing.T) ([]models.Req, error) {
origMaxPointsPerReqSoft := maxPointsPerReqSoft
origMaxPointsPerReqHard := maxPointsPerReqHard
maxPointsPerReqSoft = maxPointsSoft
maxPointsPerReqHard = maxPointsHard
mdata.Schemas = conf.NewSchemas([]conf.Schema{{
Pattern: regexp.MustCompile(".*"),
Retentions: conf.Retentions([]conf.Retention{
conf.NewRetentionMT(1, 2*day, 600, 2, true),
conf.NewRetentionMT(60, 7*day, 600, 2, true),
conf.NewRetentionMT(3600, 30*day, 600, 2, true),
}),
}})
out, _, _, err := alignRequests(30*day, reqs[0].From, reqs[0].To, reqs)
maxPointsPerReqSoft = origMaxPointsPerReqSoft
maxPointsPerReqHard = origMaxPointsPerReqHard
return out, err
}
func TestGettingOneNextBiggerAgg(t *testing.T) {
reqs := []models.Req{
reqOut(test.GetMKey(1), 29*day, 30*day, 30*day, 1, consolidation.Avg, 0, 0, 0, 1, hour, 1, 1),
}
// without maxPointsPerReqSoft = 23*hour we'd get archive 0 for this request,
// with it we expect the aggregation to get bumped to the next one
out, err := testMaxPointsPerReq(int(23*hour), 0, reqs, t)
if err != nil {
t.Fatalf("expected to get no error")
}
if out[0].Archive != 1 {
t.Errorf("expected archive %d, but got archive %d", 1, out[0].Archive)
}
}
func TestGettingTwoNextBiggerAgg(t *testing.T) {
reqs := []models.Req{
reqOut(test.GetMKey(1), 29*day, 30*day, 30*day, 1, consolidation.Avg, 0, 0, 0, 1, hour, 1, 1),
}
// maxPointsPerReqSoft only allows 24 points, so the aggregation 2 with
// 3600 SecondsPerPoint should be chosen for our request of 1 day
out, err := testMaxPointsPerReq(24, 0, reqs, t)
if err != nil {
t.Fatalf("expected to get no error")
}
if out[0].Archive != 2 {
t.Errorf("expected archive %d, but got archive %d", 2, out[0].Archive)
}
}
func TestMaxPointsPerReqHardLimit(t *testing.T) {
reqs := []models.Req{
reqOut(test.GetMKey(1), 29*day, 30*day, 30*day, 1, consolidation.Avg, 0, 0, 0, 1, hour, 1, 1),
}
// we're requesting one day and the lowest resolution aggregation has 3600 seconds per point,
// so there should be an error because we only allow max 23 points per request
_, err := testMaxPointsPerReq(22, 23, reqs, t)
if err != errMaxPointsPerReq {
t.Fatalf("expected to get an error")
}
}
var result []models.Req
func BenchmarkAlignRequests(b *testing.B) {
var res []models.Req
reqs := []models.Req{
reqRaw(test.GetMKey(1), 0, 3600*24*7, 1000, 10, consolidation.Avg, 0, 0),
reqRaw(test.GetMKey(2), 0, 3600*24*7, 1000, 30, consolidation.Avg, 4, 0),
reqRaw(test.GetMKey(3), 0, 3600*24*7, 1000, 60, consolidation.Avg, 8, 0),
}
mdata.Schemas = conf.NewSchemas([]conf.Schema{
{
Pattern: regexp.MustCompile("a"),
Retentions: conf.Retentions(
[]conf.Retention{
conf.NewRetentionMT(10, 35*24*3600, 0, 0, true),
conf.NewRetentionMT(600, 60*24*3600, 0, 0, true),
conf.NewRetentionMT(7200, 180*24*3600, 0, 0, true),
conf.NewRetentionMT(21600, 2*365*24*3600, 0, 0, true),
}),
},
{
Pattern: regexp.MustCompile("b"),
Retentions: conf.Retentions(
[]conf.Retention{
conf.NewRetentionMT(30, 35*24*3600, 0, 0, true),
conf.NewRetentionMT(600, 60*24*3600, 0, 0, true),
conf.NewRetentionMT(7200, 180*24*3600, 0, 0, true),
conf.NewRetentionMT(21600, 2*365*24*3600, 0, 0, true),
}),
},
{
Pattern: regexp.MustCompile(".*"),
Retentions: conf.Retentions(
[]conf.Retention{
conf.NewRetentionMT(60, 35*24*3600, 0, 0, true),
conf.NewRetentionMT(600, 60*24*3600, 0, 0, true),
conf.NewRetentionMT(7200, 180*24*3600, 0, 0, true),
conf.NewRetentionMT(21600, 2*365*24*3600, 0, 0, true),
}),
},
})
for n := 0; n < b.N; n++ {
res, _, _, _ = alignRequests(14*24*3600, 0, 3600*24*7, reqs)
}
result = res
}