forked from prometheus-junkyard/tsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wal_test.go
551 lines (451 loc) · 15.7 KB
/
wal_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
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// Copyright 2017 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !windows
package tsdb
// func TestSegmentWAL_cut(t *testing.T) {
// tmpdir, err := ioutil.TempDir("", "test_wal_cut")
// testutil.Ok(t, err)
// defer func() {
// testutil.Ok(t, os.RemoveAll(tmpdir))
// }()
// // This calls cut() implicitly the first time without a previous tail.
// w, err := OpenSegmentWAL(tmpdir, nil, 0, nil)
// testutil.Ok(t, err)
// testutil.Ok(t, w.write(WALEntrySeries, 1, []byte("Hello World!!")))
// testutil.Ok(t, w.cut())
// // Cutting creates a new file.
// testutil.Equals(t, 2, len(w.files))
// testutil.Ok(t, w.write(WALEntrySeries, 1, []byte("Hello World!!")))
// testutil.Ok(t, w.Close())
// for _, of := range w.files {
// f, err := os.Open(of.Name())
// testutil.Ok(t, err)
// // Verify header data.
// metab := make([]byte, 8)
// _, err = f.Read(metab)
// testutil.Ok(t, err)
// testutil.Equals(t, WALMagic, binary.BigEndian.Uint32(metab[:4]))
// testutil.Equals(t, WALFormatDefault, metab[4])
// // We cannot actually check for correct pre-allocation as it is
// // optional per filesystem and handled transparently.
// et, flag, b, err := newWALReader(nil, nil).entry(f)
// testutil.Ok(t, err)
// testutil.Equals(t, WALEntrySeries, et)
// testutil.Equals(t, byte(walSeriesSimple), flag)
// testutil.Equals(t, []byte("Hello World!!"), b)
// }
// }
// func TestSegmentWAL_Truncate(t *testing.T) {
// const (
// numMetrics = 20000
// batch = 100
// )
// series, err := labels.ReadLabels(filepath.Join("testdata", "20kseries.json"), numMetrics)
// testutil.Ok(t, err)
// dir, err := ioutil.TempDir("", "test_wal_log_truncate")
// testutil.Ok(t, err)
// defer func() {
// testutil.Ok(t, os.RemoveAll(dir))
// }()
// w, err := OpenSegmentWAL(dir, nil, 0, nil)
// testutil.Ok(t, err)
// w.segmentSize = 10000
// for i := 0; i < numMetrics; i += batch {
// var rs []RefSeries
// for j, s := range series[i : i+batch] {
// rs = append(rs, RefSeries{Labels: s, Ref: uint64(i+j) + 1})
// }
// err := w.LogSeries(rs)
// testutil.Ok(t, err)
// }
// // We mark the 2nd half of the files with a min timestamp that should discard
// // them from the selection of compactable files.
// for i, f := range w.files[len(w.files)/2:] {
// f.maxTime = int64(1000 + i)
// }
// // All series in those files must be preserved regarding of the provided postings list.
// boundarySeries := w.files[len(w.files)/2].minSeries
// // We truncate while keeping every 2nd series.
// keep := map[uint64]struct{}{}
// for i := 1; i <= numMetrics; i += 2 {
// keep[uint64(i)] = struct{}{}
// }
// keepf := func(id uint64) bool {
// _, ok := keep[id]
// return ok
// }
// err = w.Truncate(1000, keepf)
// testutil.Ok(t, err)
// var expected []RefSeries
// for i := 1; i <= numMetrics; i++ {
// if i%2 == 1 || uint64(i) >= boundarySeries {
// expected = append(expected, RefSeries{Ref: uint64(i), Labels: series[i-1]})
// }
// }
// // Call Truncate once again to see whether we can read the written file without
// // creating a new WAL.
// err = w.Truncate(1000, keepf)
// testutil.Ok(t, err)
// testutil.Ok(t, w.Close())
// // The same again with a new WAL.
// w, err = OpenSegmentWAL(dir, nil, 0, nil)
// testutil.Ok(t, err)
// var readSeries []RefSeries
// r := w.Reader()
// testutil.Ok(t, r.Read(func(s []RefSeries) {
// readSeries = append(readSeries, s...)
// }, nil, nil))
// testutil.Equals(t, expected, readSeries)
// }
// Symmetrical test of reading and writing to the WAL via its main interface.
// func TestSegmentWAL_Log_Restore(t *testing.T) {
// const (
// numMetrics = 50
// iterations = 5
// stepSize = 5
// )
// // Generate testing data. It does not make semantical sense but
// // for the purpose of this test.
// series, err := labels.ReadLabels(filepath.Join("testdata", "20kseries.json"), numMetrics)
// testutil.Ok(t, err)
// dir, err := ioutil.TempDir("", "test_wal_log_restore")
// testutil.Ok(t, err)
// defer func() {
// testutil.Ok(t, os.RemoveAll(dir))
// }()
// var (
// recordedSeries [][]RefSeries
// recordedSamples [][]RefSample
// recordedDeletes [][]Stone
// )
// var totalSamples int
// // Open WAL a bunch of times, validate all previous data can be read,
// // write more data to it, close it.
// for k := 0; k < numMetrics; k += numMetrics / iterations {
// w, err := OpenSegmentWAL(dir, nil, 0, nil)
// testutil.Ok(t, err)
// // Set smaller segment size so we can actually write several files.
// w.segmentSize = 1000 * 1000
// r := w.Reader()
// var (
// resultSeries [][]RefSeries
// resultSamples [][]RefSample
// resultDeletes [][]Stone
// )
// serf := func(series []RefSeries) {
// if len(series) > 0 {
// clsets := make([]RefSeries, len(series))
// copy(clsets, series)
// resultSeries = append(resultSeries, clsets)
// }
// }
// smplf := func(smpls []RefSample) {
// if len(smpls) > 0 {
// csmpls := make([]RefSample, len(smpls))
// copy(csmpls, smpls)
// resultSamples = append(resultSamples, csmpls)
// }
// }
// delf := func(stones []Stone) {
// if len(stones) > 0 {
// cst := make([]Stone, len(stones))
// copy(cst, stones)
// resultDeletes = append(resultDeletes, cst)
// }
// }
// testutil.Ok(t, r.Read(serf, smplf, delf))
// testutil.Equals(t, recordedSamples, resultSamples)
// testutil.Equals(t, recordedSeries, resultSeries)
// testutil.Equals(t, recordedDeletes, resultDeletes)
// series := series[k : k+(numMetrics/iterations)]
// val := make([]byte, 4)
// // Insert in batches and generate different amounts of samples for each.
// for i := 0; i < len(series); i += stepSize {
// var samples []RefSample
// var stones []Stone
// for j := 0; j < i*10; j++ {
// rand.Read(val)
// samples = append(samples, RefSample{
// Ref: uint64(j % 10000),
// T: int64(j * 2),
// V: val,
// })
// }
// for j := 0; j < i*20; j++ {
// ts := rand.Int63()
// stones = append(stones, Stone{rand.Uint64(), Intervals{{ts, ts + rand.Int63n(10000)}}})
// }
// lbls := series[i : i+stepSize]
// series := make([]RefSeries, 0, len(series))
// for j, l := range lbls {
// series = append(series, RefSeries{
// Ref: uint64(i + j),
// Labels: l,
// })
// }
// testutil.Ok(t, w.LogSeries(series))
// testutil.Ok(t, w.LogSamples(samples))
// testutil.Ok(t, w.LogDeletes(stones))
// if len(lbls) > 0 {
// recordedSeries = append(recordedSeries, series)
// }
// if len(samples) > 0 {
// recordedSamples = append(recordedSamples, samples)
// totalSamples += len(samples)
// }
// if len(stones) > 0 {
// recordedDeletes = append(recordedDeletes, stones)
// }
// }
// testutil.Ok(t, w.Close())
// }
// }
// func TestWALRestoreCorrupted_invalidSegment(t *testing.T) {
// dir, err := ioutil.TempDir("", "test_wal_log_restore")
// testutil.Ok(t, err)
// defer func() {
// testutil.Ok(t, os.RemoveAll(dir))
// }()
// wal, err := OpenSegmentWAL(dir, nil, 0, nil)
// testutil.Ok(t, err)
// _, err = wal.createSegmentFile(filepath.Join(dir, "000000"))
// testutil.Ok(t, err)
// f, err := wal.createSegmentFile(filepath.Join(dir, "000001"))
// testutil.Ok(t, err)
// f2, err := wal.createSegmentFile(filepath.Join(dir, "000002"))
// testutil.Ok(t, err)
// testutil.Ok(t, f2.Close())
// // Make header of second segment invalid.
// _, err = f.WriteAt([]byte{1, 2, 3, 4}, 0)
// testutil.Ok(t, err)
// testutil.Ok(t, f.Close())
// testutil.Ok(t, wal.Close())
// _, err = OpenSegmentWAL(dir, log.NewLogfmtLogger(os.Stderr), 0, nil)
// testutil.Ok(t, err)
// fns, err := fileutil.ReadDir(dir)
// testutil.Ok(t, err)
// testutil.Equals(t, []string{"000000"}, fns)
// }
// Test reading from a WAL that has been corrupted through various means.
// func TestWALRestoreCorrupted(t *testing.T) {
// cases := []struct {
// name string
// f func(*testing.T, *SegmentWAL)
// }{
// {
// name: "truncate_checksum",
// f: func(t *testing.T, w *SegmentWAL) {
// f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666)
// testutil.Ok(t, err)
// defer f.Close()
// off, err := f.Seek(0, io.SeekEnd)
// testutil.Ok(t, err)
// testutil.Ok(t, f.Truncate(off-1))
// },
// },
// {
// name: "truncate_body",
// f: func(t *testing.T, w *SegmentWAL) {
// f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666)
// testutil.Ok(t, err)
// defer f.Close()
// off, err := f.Seek(0, io.SeekEnd)
// testutil.Ok(t, err)
// testutil.Ok(t, f.Truncate(off-8))
// },
// },
// {
// name: "body_content",
// f: func(t *testing.T, w *SegmentWAL) {
// f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666)
// testutil.Ok(t, err)
// defer f.Close()
// off, err := f.Seek(0, io.SeekEnd)
// testutil.Ok(t, err)
// // Write junk before checksum starts.
// _, err = f.WriteAt([]byte{1, 2, 3, 4}, off-8)
// testutil.Ok(t, err)
// },
// },
// {
// name: "checksum",
// f: func(t *testing.T, w *SegmentWAL) {
// f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666)
// testutil.Ok(t, err)
// defer f.Close()
// off, err := f.Seek(0, io.SeekEnd)
// testutil.Ok(t, err)
// // Write junk into checksum
// _, err = f.WriteAt([]byte{1, 2, 3, 4}, off-4)
// testutil.Ok(t, err)
// },
// },
// }
// for _, c := range cases {
// t.Run(c.name, func(t *testing.T) {
// // Generate testing data. It does not make semantical sense but
// // for the purpose of this test.
// dir, err := ioutil.TempDir("", "test_corrupted")
// testutil.Ok(t, err)
// defer func() {
// testutil.Ok(t, os.RemoveAll(dir))
// }()
// w, err := OpenSegmentWAL(dir, nil, 0, nil)
// testutil.Ok(t, err)
// testutil.Ok(t, w.LogSamples([]RefSample{{T: 1, V: []byte("2")}}))
// testutil.Ok(t, w.LogSamples([]RefSample{{T: 2, V: []byte("3")}}))
// testutil.Ok(t, w.cut())
// // Sleep 2 seconds to avoid error where cut and test "cases" function may write or
// // truncate the file out of orders as "cases" are not synchronized with cut.
// // Hopefully cut will complete by 2 seconds.
// time.Sleep(2 * time.Second)
// testutil.Ok(t, w.LogSamples([]RefSample{{T: 3, V: []byte("4")}}))
// testutil.Ok(t, w.LogSamples([]RefSample{{T: 5, V: []byte("6")}}))
// testutil.Ok(t, w.Close())
// // cut() truncates and fsyncs the first segment async. If it happens after
// // the corruption we apply below, the corruption will be overwritten again.
// // Fire and forget a sync to avoid flakyness.
// w.files[0].Sync()
// // Corrupt the second entry in the first file.
// // After re-opening we must be able to read the first entry
// // and the rest, including the second file, must be truncated for clean further
// // writes.
// c.f(t, w)
// logger := log.NewLogfmtLogger(os.Stderr)
// w2, err := OpenSegmentWAL(dir, logger, 0, nil)
// testutil.Ok(t, err)
// r := w2.Reader()
// serf := func(l []RefSeries) {
// testutil.Equals(t, 0, len(l))
// }
// // Weird hack to check order of reads.
// i := 0
// samplf := func(s []RefSample) {
// if i == 0 {
// testutil.Equals(t, []RefSample{{T: 1, V: []byte("2")}}, s)
// i++
// } else {
// testutil.Equals(t, []RefSample{{T: 99, V: []byte("100")}}, s)
// }
// }
// testutil.Ok(t, r.Read(serf, samplf, nil))
// testutil.Ok(t, w2.LogSamples([]RefSample{{T: 99, V: []byte("100")}}))
// testutil.Ok(t, w2.Close())
// // We should see the first valid entry and the new one, everything after
// // is truncated.
// w3, err := OpenSegmentWAL(dir, logger, 0, nil)
// testutil.Ok(t, err)
// r = w3.Reader()
// i = 0
// testutil.Ok(t, r.Read(serf, samplf, nil))
// })
// }
// }
// func TestMigrateWAL_Empty(t *testing.T) {
// // The migration proecedure must properly deal with a zero-length segment,
// // which is valid in the new format.
// dir, err := ioutil.TempDir("", "walmigrate")
// testutil.Ok(t, err)
// defer func() {
// testutil.Ok(t, os.RemoveAll(dir))
// }()
// wdir := path.Join(dir, "wal")
// // Initialize empty WAL.
// w, err := wal.New(nil, nil, wdir)
// testutil.Ok(t, err)
// testutil.Ok(t, w.Close())
// testutil.Ok(t, MigrateWAL(nil, wdir))
// }
// func TestMigrateWAL_Fuzz(t *testing.T) {
// dir, err := ioutil.TempDir("", "walmigrate")
// testutil.Ok(t, err)
// defer func() {
// testutil.Ok(t, os.RemoveAll(dir))
// }()
// wdir := path.Join(dir, "wal")
// // Should pass if no WAL exists yet.
// testutil.Ok(t, MigrateWAL(nil, wdir))
// oldWAL, err := OpenSegmentWAL(wdir, nil, time.Minute, nil)
// testutil.Ok(t, err)
// // Write some data.
// testutil.Ok(t, oldWAL.LogSeries([]RefSeries{
// {Ref: 100, Labels: labels.FromStrings("abc", "def", "123", "456")},
// {Ref: 1, Labels: labels.FromStrings("abc", "def2", "1234", "4567")},
// }))
// testutil.Ok(t, oldWAL.LogSamples([]RefSample{
// {Ref: 1, T: 100, V: []byte("200")},
// {Ref: 2, T: 300, V: []byte("400")},
// }))
// testutil.Ok(t, oldWAL.LogSeries([]RefSeries{
// {Ref: 200, Labels: labels.FromStrings("xyz", "def", "foo", "bar")},
// }))
// testutil.Ok(t, oldWAL.LogSamples([]RefSample{
// {Ref: 3, T: 100, V: []byte("200")},
// {Ref: 4, T: 300, V: []byte("400")},
// }))
// testutil.Ok(t, oldWAL.LogDeletes([]Stone{
// {ref: 1, intervals: []Interval{{100, 200}}},
// }))
// testutil.Ok(t, oldWAL.Close())
// // Perform migration.
// testutil.Ok(t, MigrateWAL(nil, wdir))
// w, err := wal.New(nil, nil, wdir)
// testutil.Ok(t, err)
// // We can properly write some new data after migration.
// var enc RecordEncoder
// testutil.Ok(t, w.Log(enc.Samples([]RefSample{
// {Ref: 500, T: 1, V: []byte("1")},
// }, nil)))
// testutil.Ok(t, w.Close())
// // Read back all data.
// sr, err := wal.NewSegmentsReader(wdir)
// testutil.Ok(t, err)
// r := wal.NewReader(sr)
// var res []interface{}
// var dec RecordDecoder
// for r.Next() {
// rec := r.Record()
// switch dec.Type(rec) {
// case RecordSeries:
// s, err := dec.Series(rec, nil)
// testutil.Ok(t, err)
// res = append(res, s)
// case RecordSamples:
// s, err := dec.Samples(rec, nil)
// testutil.Ok(t, err)
// res = append(res, s)
// case RecordTombstones:
// s, err := dec.Tombstones(rec, nil)
// testutil.Ok(t, err)
// res = append(res, s)
// default:
// t.Fatalf("unknown record type %d", dec.Type(rec))
// }
// }
// testutil.Ok(t, r.Err())
// testutil.Equals(t, []interface{}{
// []RefSeries{
// {Ref: 100, Labels: labels.FromStrings("abc", "def", "123", "456")},
// {Ref: 1, Labels: labels.FromStrings("abc", "def2", "1234", "4567")},
// },
// []RefSample{{Ref: 1, T: 100, V: []byte("200")}, {Ref: 2, T: 300, V: []byte("400")}},
// []RefSeries{
// {Ref: 200, Labels: labels.FromStrings("xyz", "def", "foo", "bar")},
// },
// []RefSample{{Ref: 3, T: 100, V: []byte("200")}, {Ref: 4, T: 300, V: []byte("400")}},
// []Stone{{ref: 1, intervals: []Interval{{100, 200}}}},
// []RefSample{{Ref: 500, T: 1, V: []byte("1")}},
// }, res)
// // Migrating an already migrated WAL shouldn't do anything.
// testutil.Ok(t, MigrateWAL(nil, wdir))
// }