diff --git a/block.go b/block.go index ad369a4a1..9bb5d6ebb 100644 --- a/block.go +++ b/block.go @@ -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) { diff --git a/compact.go b/compact.go index 9ae18f198..b58acba43 100644 --- a/compact.go +++ b/compact.go @@ -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 } diff --git a/db.go b/db.go index fb7dedacf..f967b044c 100644 --- a/db.go +++ b/db.go @@ -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