@@ -19,13 +19,15 @@ import (
1919 "io/ioutil"
2020 "math/rand"
2121 "os"
22+ "path"
2223 "testing"
2324 "time"
2425
2526 "github.com/go-kit/kit/log"
2627 "github.com/prometheus/tsdb/fileutil"
2728 "github.com/prometheus/tsdb/labels"
2829 "github.com/prometheus/tsdb/testutil"
30+ "github.com/prometheus/tsdb/wal"
2931)
3032
3133func TestSegmentWAL_cut (t * testing.T ) {
@@ -431,3 +433,100 @@ func TestWALRestoreCorrupted(t *testing.T) {
431433 })
432434 }
433435}
436+
437+ func TestMigrateWAL_Fuzz (t * testing.T ) {
438+ dir , err := ioutil .TempDir ("" , "walmigrate" )
439+ testutil .Ok (t , err )
440+ defer os .RemoveAll (dir )
441+
442+ wdir := path .Join (dir , "wal" )
443+
444+ // Should pass if no WAL exists yet.
445+ testutil .Ok (t , MigrateWAL (nil , wdir ))
446+
447+ oldWAL , err := OpenSegmentWAL (wdir , nil , time .Minute , nil )
448+ testutil .Ok (t , err )
449+
450+ // Write some data.
451+ testutil .Ok (t , oldWAL .LogSeries ([]RefSeries {
452+ {Ref : 100 , Labels : labels .FromStrings ("abc" , "def" , "123" , "456" )},
453+ {Ref : 1 , Labels : labels .FromStrings ("abc" , "def2" , "1234" , "4567" )},
454+ }))
455+ testutil .Ok (t , oldWAL .LogSamples ([]RefSample {
456+ {Ref : 1 , T : 100 , V : 200 },
457+ {Ref : 2 , T : 300 , V : 400 },
458+ }))
459+ testutil .Ok (t , oldWAL .LogSeries ([]RefSeries {
460+ {Ref : 200 , Labels : labels .FromStrings ("xyz" , "def" , "foo" , "bar" )},
461+ }))
462+ testutil .Ok (t , oldWAL .LogSamples ([]RefSample {
463+ {Ref : 3 , T : 100 , V : 200 },
464+ {Ref : 4 , T : 300 , V : 400 },
465+ }))
466+ testutil .Ok (t , oldWAL .LogDeletes ([]Stone {
467+ {ref : 1 , intervals : []Interval {{100 , 200 }}},
468+ }))
469+
470+ testutil .Ok (t , oldWAL .Close ())
471+
472+ // Perform migration.
473+ testutil .Ok (t , MigrateWAL (nil , wdir ))
474+
475+ w , err := wal .New (nil , nil , wdir )
476+ testutil .Ok (t , err )
477+
478+ // We can properly write some new data after migration.
479+ var enc RecordEncoder
480+ testutil .Ok (t , w .Log (enc .Samples ([]RefSample {
481+ {Ref : 500 , T : 1 , V : 1 },
482+ }, nil )))
483+
484+ testutil .Ok (t , w .Close ())
485+
486+ // Read back all data.
487+ sr , err := wal .NewSegmentsReader (wdir )
488+ testutil .Ok (t , err )
489+
490+ r := wal .NewReader (sr )
491+ var res []interface {}
492+ var dec RecordDecoder
493+
494+ for r .Next () {
495+ rec := r .Record ()
496+
497+ switch dec .Type (rec ) {
498+ case RecordSeries :
499+ s , err := dec .Series (rec , nil )
500+ testutil .Ok (t , err )
501+ res = append (res , s )
502+ case RecordSamples :
503+ s , err := dec .Samples (rec , nil )
504+ testutil .Ok (t , err )
505+ res = append (res , s )
506+ case RecordTombstones :
507+ s , err := dec .Tombstones (rec , nil )
508+ testutil .Ok (t , err )
509+ res = append (res , s )
510+ default :
511+ t .Fatalf ("unknown record type %d" , dec .Type (rec ))
512+ }
513+ }
514+ testutil .Ok (t , r .Err ())
515+
516+ testutil .Equals (t , []interface {}{
517+ []RefSeries {
518+ {Ref : 100 , Labels : labels .FromStrings ("abc" , "def" , "123" , "456" )},
519+ {Ref : 1 , Labels : labels .FromStrings ("abc" , "def2" , "1234" , "4567" )},
520+ },
521+ []RefSample {{Ref : 1 , T : 100 , V : 200 }, {Ref : 2 , T : 300 , V : 400 }},
522+ []RefSeries {
523+ {Ref : 200 , Labels : labels .FromStrings ("xyz" , "def" , "foo" , "bar" )},
524+ },
525+ []RefSample {{Ref : 3 , T : 100 , V : 200 }, {Ref : 4 , T : 300 , V : 400 }},
526+ []Stone {{ref : 1 , intervals : []Interval {{100 , 200 }}}},
527+ []RefSample {{Ref : 500 , T : 1 , V : 1 }},
528+ }, res )
529+
530+ // Migrating an already migrated WAL shouldn't do anything.
531+ testutil .Ok (t , MigrateWAL (nil , wdir ))
532+ }
0 commit comments