@@ -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,117 @@ func TestWALRestoreCorrupted(t *testing.T) {
431433 })
432434 }
433435}
436+
437+ func TestMigrateWAL_Empty (t * testing.T ) {
438+ // The migration proecedure must properly deal with a zero-length segment,
439+ // which is valid in the new format.
440+ dir , err := ioutil .TempDir ("" , "walmigrate" )
441+ testutil .Ok (t , err )
442+ defer os .RemoveAll (dir )
443+
444+ wdir := path .Join (dir , "wal" )
445+
446+ // Initialize empty WAL.
447+ w , err := wal .New (nil , nil , wdir )
448+ testutil .Ok (t , err )
449+ testutil .Ok (t , w .Close ())
450+
451+ testutil .Ok (t , MigrateWAL (nil , wdir ))
452+ }
453+
454+ func TestMigrateWAL_Fuzz (t * testing.T ) {
455+ dir , err := ioutil .TempDir ("" , "walmigrate" )
456+ testutil .Ok (t , err )
457+ defer os .RemoveAll (dir )
458+
459+ wdir := path .Join (dir , "wal" )
460+
461+ // Should pass if no WAL exists yet.
462+ testutil .Ok (t , MigrateWAL (nil , wdir ))
463+
464+ oldWAL , err := OpenSegmentWAL (wdir , nil , time .Minute , nil )
465+ testutil .Ok (t , err )
466+
467+ // Write some data.
468+ testutil .Ok (t , oldWAL .LogSeries ([]RefSeries {
469+ {Ref : 100 , Labels : labels .FromStrings ("abc" , "def" , "123" , "456" )},
470+ {Ref : 1 , Labels : labels .FromStrings ("abc" , "def2" , "1234" , "4567" )},
471+ }))
472+ testutil .Ok (t , oldWAL .LogSamples ([]RefSample {
473+ {Ref : 1 , T : 100 , V : 200 },
474+ {Ref : 2 , T : 300 , V : 400 },
475+ }))
476+ testutil .Ok (t , oldWAL .LogSeries ([]RefSeries {
477+ {Ref : 200 , Labels : labels .FromStrings ("xyz" , "def" , "foo" , "bar" )},
478+ }))
479+ testutil .Ok (t , oldWAL .LogSamples ([]RefSample {
480+ {Ref : 3 , T : 100 , V : 200 },
481+ {Ref : 4 , T : 300 , V : 400 },
482+ }))
483+ testutil .Ok (t , oldWAL .LogDeletes ([]Stone {
484+ {ref : 1 , intervals : []Interval {{100 , 200 }}},
485+ }))
486+
487+ testutil .Ok (t , oldWAL .Close ())
488+
489+ // Perform migration.
490+ testutil .Ok (t , MigrateWAL (nil , wdir ))
491+
492+ w , err := wal .New (nil , nil , wdir )
493+ testutil .Ok (t , err )
494+
495+ // We can properly write some new data after migration.
496+ var enc RecordEncoder
497+ testutil .Ok (t , w .Log (enc .Samples ([]RefSample {
498+ {Ref : 500 , T : 1 , V : 1 },
499+ }, nil )))
500+
501+ testutil .Ok (t , w .Close ())
502+
503+ // Read back all data.
504+ sr , err := wal .NewSegmentsReader (wdir )
505+ testutil .Ok (t , err )
506+
507+ r := wal .NewReader (sr )
508+ var res []interface {}
509+ var dec RecordDecoder
510+
511+ for r .Next () {
512+ rec := r .Record ()
513+
514+ switch dec .Type (rec ) {
515+ case RecordSeries :
516+ s , err := dec .Series (rec , nil )
517+ testutil .Ok (t , err )
518+ res = append (res , s )
519+ case RecordSamples :
520+ s , err := dec .Samples (rec , nil )
521+ testutil .Ok (t , err )
522+ res = append (res , s )
523+ case RecordTombstones :
524+ s , err := dec .Tombstones (rec , nil )
525+ testutil .Ok (t , err )
526+ res = append (res , s )
527+ default :
528+ t .Fatalf ("unknown record type %d" , dec .Type (rec ))
529+ }
530+ }
531+ testutil .Ok (t , r .Err ())
532+
533+ testutil .Equals (t , []interface {}{
534+ []RefSeries {
535+ {Ref : 100 , Labels : labels .FromStrings ("abc" , "def" , "123" , "456" )},
536+ {Ref : 1 , Labels : labels .FromStrings ("abc" , "def2" , "1234" , "4567" )},
537+ },
538+ []RefSample {{Ref : 1 , T : 100 , V : 200 }, {Ref : 2 , T : 300 , V : 400 }},
539+ []RefSeries {
540+ {Ref : 200 , Labels : labels .FromStrings ("xyz" , "def" , "foo" , "bar" )},
541+ },
542+ []RefSample {{Ref : 3 , T : 100 , V : 200 }, {Ref : 4 , T : 300 , V : 400 }},
543+ []Stone {{ref : 1 , intervals : []Interval {{100 , 200 }}}},
544+ []RefSample {{Ref : 500 , T : 1 , V : 1 }},
545+ }, res )
546+
547+ // Migrating an already migrated WAL shouldn't do anything.
548+ testutil .Ok (t , MigrateWAL (nil , wdir ))
549+ }
0 commit comments