Write meta.json file atomically

pull/5805/head
Fabian Reinartz 2017-03-01 17:19:57 +01:00
parent 6c4217276f
commit 2c3b56350a
3 changed files with 8 additions and 5 deletions

View File

@ -94,7 +94,11 @@ func readMetaFile(dir string) (*BlockMeta, error) {
}
func writeMetaFile(dir string, meta *BlockMeta) error {
f, err := os.Create(filepath.Join(dir, metaFilename))
// Make any changes to the file appear atomic.
path := filepath.Join(dir, metaFilename)
tmp := path + ".tmp"
f, err := os.Create(tmp)
if err != nil {
return err
}
@ -108,8 +112,7 @@ func writeMetaFile(dir string, meta *BlockMeta) error {
if err := f.Close(); err != nil {
return err
}
return nil
return renameFile(tmp, path)
}
func newPersistedBlock(dir string) (*persistedBlock, error) {

View File

@ -406,7 +406,7 @@ func (c *compactionMerger) At() (labels.Labels, []ChunkMeta) {
return c.l, c.c
}
func renameDir(from, to string) error {
func renameFile(from, to string) error {
if err := os.RemoveAll(to); err != nil {
return err
}

2
db.go
View File

@ -300,7 +300,7 @@ func (db *DB) compact(i, j int) error {
}
}
if err := renameDir(tmpdir, dir); err != nil {
if err := renameFile(tmpdir, dir); err != nil {
return errors.Wrap(err, "rename dir")
}
pb.dir = dir