Improvements after review.

This commit is contained in:
beorn7 2015-03-03 18:59:39 +01:00
parent a18cb29fa8
commit 0167083da6
1 changed files with 8 additions and 14 deletions

View File

@ -18,6 +18,7 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -38,8 +39,8 @@ import (
) )
const ( const (
// Version of the storage, as it can be found in the version file. // Version of the storage as it can be found in the version file.
// Increment to protect against icompatible changes. // Increment to protect against incompatible changes.
Version = 1 Version = 1
versionFileName = "VERSION" versionFileName = "VERSION"
@ -124,15 +125,9 @@ func newPersistence(basePath string, chunkLen int, dirty bool) (*persistence, er
dirtyPath := filepath.Join(basePath, dirtyFileName) dirtyPath := filepath.Join(basePath, dirtyFileName)
versionPath := filepath.Join(basePath, versionFileName) versionPath := filepath.Join(basePath, versionFileName)
if file, err := os.Open(versionPath); err == nil { if versionData, err := ioutil.ReadFile(versionPath); err == nil {
defer file.Close() if persistedVersion, err := strconv.Atoi(strings.TrimSpace(string(versionData))); err != nil {
data := make([]byte, 8) return nil, fmt.Errorf("cannot parse content of %s: %s", versionPath, versionData)
n, err := file.Read(data)
if err != nil {
return nil, err
}
if persistedVersion, err := strconv.Atoi(strings.TrimSpace(string(data[:n]))); err != nil {
return nil, fmt.Errorf("cannot parse content of %s: %s", versionPath, data[:n])
} else if persistedVersion != Version { } else if persistedVersion != Version {
return nil, fmt.Errorf("found storage version %d on disk, need version %d - please wipe storage or run a version of Prometheus compatible with storage version %d", persistedVersion, Version, persistedVersion) return nil, fmt.Errorf("found storage version %d on disk, need version %d - please wipe storage or run a version of Prometheus compatible with storage version %d", persistedVersion, Version, persistedVersion)
} }
@ -144,12 +139,11 @@ func newPersistence(basePath string, chunkLen int, dirty bool) (*persistence, er
if err := os.MkdirAll(basePath, 0700); err != nil { if err := os.MkdirAll(basePath, 0700); err != nil {
return nil, err return nil, err
} }
d, err := os.Open(basePath) fis, err := ioutil.ReadDir(basePath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer d.Close() if len(fis) > 0 {
if fis, _ := d.Readdir(1); len(fis) > 0 {
return nil, fmt.Errorf("could not detect storage version on disk, assuming version 0, need version %d - please wipe storage or run a version of Prometheus compatible with storage version 0", Version) return nil, fmt.Errorf("could not detect storage version on disk, assuming version 0, need version %d - please wipe storage or run a version of Prometheus compatible with storage version 0", Version)
} }
// Finally we can write our own version into a new version file. // Finally we can write our own version into a new version file.