@@ -18,6 +18,7 @@ import (
1818 "math"
1919 "math/rand"
2020 "os"
21+ "path/filepath"
2122 "sort"
2223 "testing"
2324
@@ -808,6 +809,48 @@ func TestDB_Retention(t *testing.T) {
808809 testutil.Equals(t, int64(100), db.blocks[0].meta.MaxTime) // To verify its the right block.
809810}
810811
812+ // TestDBMissingMeta assures that the db can be opened even when a folder is missing the meta file.
813+ // Also ensures that the folder with the missing meta is deleted when reloading the db.
814+ func TestDBMissingMeta(t *testing.T) {
815+ tmpdir, _ := ioutil.TempDir("", "test")
816+ defer os.RemoveAll(tmpdir)
817+
818+ db, err := Open(tmpdir, nil, nil, nil)
819+ testutil.Ok(t, err)
820+
821+ lbls := labels.Labels{labels.Label{Name: "labelname", Value: "labelvalue"}}
822+
823+ app := db.Appender()
824+ _, err = app.Add(lbls, 0, 1)
825+ testutil.Ok(t, err)
826+ testutil.Ok(t, app.Commit())
827+
828+ // create snapshot to make it create a block.
829+ snap, err := ioutil.TempDir("", "snap")
830+ testutil.Ok(t, err)
831+ testutil.Ok(t, db.Snapshot(snap))
832+
833+ testutil.Ok(t, db.Close())
834+ defer os.RemoveAll(snap)
835+
836+ var deleteable string
837+ testutil.Ok(t, filepath.Walk(snap, func(path string, f os.FileInfo, err error) error {
838+ if f.Name() == metaFilename {
839+ deleteable = filepath.Dir(path)
840+ return nil
841+ }
842+ return nil
843+ }))
844+
845+ // reopen DB from snapshot. This should succeed even though the meta file is missing.
846+ db, err = Open(snap, nil, nil, nil)
847+ testutil.Ok(t, err)
848+
849+ db.reload()
850+ _, err = os.Stat(deleteable)
851+ testutil.Assert(t, os.IsNotExist(err), "unexpected error when checking that the folder with the missing meta is deleted", err)
852+ }
853+
811854func TestNotMatcherSelectsLabelsUnsetSeries(t *testing.T) {
812855 tmpdir, _ := ioutil.TempDir("", "test")
813856 defer os.RemoveAll(tmpdir)
0 commit comments