repair: improve error message

This change adds the directory path to the error message in case the
repair process fails.

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Simon Pasquier 2018-04-25 10:37:25 +02:00
parent def6e5a574
commit eab78875fe
1 changed files with 16 additions and 20 deletions

View File

@ -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 %s", 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: %s", 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