Merge pull request #319 from simonpasquier/improve-repair-err
repair: improve error message
This commit is contained in:
commit
eeb3a7435a
36
repair.go
36
repair.go
|
@ -5,12 +5,10 @@ import (
|
|||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/go-kit/kit/log"
|
||||
"github.com/go-kit/kit/log/level"
|
||||
"github.com/oklog/ulid"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prometheus/tsdb/fileutil"
|
||||
)
|
||||
|
@ -20,20 +18,18 @@ import (
|
|||
func repairBadIndexVersion(logger log.Logger, dir string) error {
|
||||
// All blocks written by Prometheus 2.1 with a meta.json version of 2 are affected.
|
||||
// We must actually set the index file version to 2 and revert the meta.json version back to 1.
|
||||
subdirs, err := fileutil.ReadDir(dir)
|
||||
dirs, err := blockDirs(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.Wrapf(err, "list block dirs in %q", dir)
|
||||
}
|
||||
for _, d := range subdirs {
|
||||
// Skip non-block dirs.
|
||||
if _, err := ulid.Parse(d); err != nil {
|
||||
continue
|
||||
}
|
||||
d = path.Join(dir, d)
|
||||
|
||||
wrapErr := func(err error, d string) error {
|
||||
return errors.Wrapf(err, "block dir: %q", d)
|
||||
}
|
||||
for _, d := range dirs {
|
||||
meta, err := readBogusMetaFile(d)
|
||||
if err != nil {
|
||||
return err
|
||||
return wrapErr(err, d)
|
||||
}
|
||||
if meta.Version == 1 {
|
||||
level.Info(logger).Log(
|
||||
|
@ -53,35 +49,35 @@ func repairBadIndexVersion(logger log.Logger, dir string) error {
|
|||
|
||||
repl, err := os.Create(filepath.Join(d, "index.repaired"))
|
||||
if err != nil {
|
||||
return err
|
||||
return wrapErr(err, d)
|
||||
}
|
||||
broken, err := os.Open(filepath.Join(d, "index"))
|
||||
if err != nil {
|
||||
return err
|
||||
return wrapErr(err, d)
|
||||
}
|
||||
if _, err := io.Copy(repl, broken); err != nil {
|
||||
return err
|
||||
return wrapErr(err, d)
|
||||
}
|
||||
// Set the 5th byte to 2 to indiciate the correct file format version.
|
||||
if _, err := repl.WriteAt([]byte{2}, 4); err != nil {
|
||||
return err
|
||||
return wrapErr(err, d)
|
||||
}
|
||||
if err := fileutil.Fsync(repl); err != nil {
|
||||
return err
|
||||
return wrapErr(err, d)
|
||||
}
|
||||
if err := repl.Close(); err != nil {
|
||||
return err
|
||||
return wrapErr(err, d)
|
||||
}
|
||||
if err := broken.Close(); err != nil {
|
||||
return err
|
||||
return wrapErr(err, d)
|
||||
}
|
||||
if err := renameFile(repl.Name(), broken.Name()); err != nil {
|
||||
return err
|
||||
return wrapErr(err, d)
|
||||
}
|
||||
// Reset version of meta.json to 1.
|
||||
meta.Version = 1
|
||||
if err := writeMetaFile(d, meta); err != nil {
|
||||
return err
|
||||
return wrapErr(err, d)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
Loading…
Reference in New Issue