Skip to content
This repository was archived by the owner on Aug 13, 2019. It is now read-only.

Commit c57bd62

Browse files
committed
cleanup tmp file if file renaming failed
Signed-off-by: zhulongcheng <[email protected]>
1 parent 520b1d8 commit c57bd62

File tree

5 files changed

+34
-28
lines changed

5 files changed

+34
-28
lines changed

block.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/prometheus/tsdb/chunkenc"
3030
"github.com/prometheus/tsdb/chunks"
3131
tsdb_errors "github.com/prometheus/tsdb/errors"
32+
"github.com/prometheus/tsdb/fileutil"
3233
"github.com/prometheus/tsdb/index"
3334
"github.com/prometheus/tsdb/labels"
3435
)
@@ -259,7 +260,14 @@ func writeMetaFile(dir string, meta *BlockMeta) error {
259260
if err := f.Close(); err != nil {
260261
return err
261262
}
262-
return renameFile(tmp, path)
263+
264+
if err := fileutil.Rename(tmp, path); err != nil {
265+
merr.Add(err)
266+
merr.Add(os.RemoveAll(tmp))
267+
return merr.Err()
268+
}
269+
270+
return nil
263271
}
264272

265273
// Block represents a directory of time series data covering a continuous time range.

compact.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ func (c *LeveledCompactor) write(dest string, meta *BlockMeta, blocks ...BlockRe
637637
df = nil
638638

639639
// Block successfully written, make visible and remove old ones.
640-
if err := renameFile(tmp, dir); err != nil {
640+
if err := fileutil.Rename(tmp, dir); err != nil {
641641
return errors.Wrap(err, "rename block dir")
642642
}
643643

@@ -1011,24 +1011,3 @@ func (c *compactionMerger) Err() error {
10111011
func (c *compactionMerger) At() (labels.Labels, []chunks.Meta, Intervals) {
10121012
return c.l, c.c, c.intervals
10131013
}
1014-
1015-
func renameFile(from, to string) error {
1016-
if err := os.RemoveAll(to); err != nil {
1017-
return err
1018-
}
1019-
if err := os.Rename(from, to); err != nil {
1020-
return err
1021-
}
1022-
1023-
// Directory was renamed; sync parent dir to persist rename.
1024-
pdir, err := fileutil.OpenDir(filepath.Dir(to))
1025-
if err != nil {
1026-
return err
1027-
}
1028-
1029-
if err = pdir.Sync(); err != nil {
1030-
pdir.Close()
1031-
return err
1032-
}
1033-
return pdir.Close()
1034-
}

repair.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"github.com/go-kit/kit/log"
2424
"github.com/go-kit/kit/log/level"
2525
"github.com/pkg/errors"
26+
tsdb_errors "github.com/prometheus/tsdb/errors"
27+
"github.com/prometheus/tsdb/fileutil"
2628
)
2729

2830
// repairBadIndexVersion repairs an issue in index and meta.json persistence introduced in
@@ -83,8 +85,11 @@ func repairBadIndexVersion(logger log.Logger, dir string) error {
8385
if err := broken.Close(); err != nil {
8486
return wrapErr(err, d)
8587
}
86-
if err := renameFile(repl.Name(), broken.Name()); err != nil {
87-
return wrapErr(err, d)
88+
if err := fileutil.Rename(repl.Name(), broken.Name()); err != nil {
89+
var merr tsdb_errors.MultiError
90+
merr.Add(wrapErr(err, d))
91+
merr.Add(os.RemoveAll(repl.Name()))
92+
return merr.Err()
8893
}
8994
// Reset version of meta.json to 1.
9095
meta.Version = 1

tombstones.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424

2525
"github.com/pkg/errors"
2626
"github.com/prometheus/tsdb/encoding"
27+
tsdb_errors "github.com/prometheus/tsdb/errors"
28+
"github.com/prometheus/tsdb/fileutil"
2729
)
2830

2931
const tombstoneFilename = "tombstones"
@@ -104,7 +106,14 @@ func writeTombstoneFile(dir string, tr TombstoneReader) error {
104106
return err
105107
}
106108
f = nil
107-
return renameFile(tmp, path)
109+
110+
if err := fileutil.Rename(tmp, path); err != nil {
111+
var merr tsdb_errors.MultiError
112+
merr.Add(err)
113+
merr.Add(os.RemoveAll(tmp))
114+
return merr.Err()
115+
}
116+
return nil
108117
}
109118

110119
// Stone holds the information on the posting and time-range

wal.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/pkg/errors"
3333
"github.com/prometheus/client_golang/prometheus"
3434
"github.com/prometheus/tsdb/encoding"
35+
tsdb_errors "github.com/prometheus/tsdb/errors"
3536
"github.com/prometheus/tsdb/fileutil"
3637
"github.com/prometheus/tsdb/labels"
3738
"github.com/prometheus/tsdb/wal"
@@ -389,9 +390,13 @@ func (w *SegmentWAL) Truncate(mint int64, keep func(uint64) bool) error {
389390
csf.Close()
390391

391392
candidates[0].Close() // need close before remove on platform windows
392-
if err := renameFile(csf.Name(), candidates[0].Name()); err != nil {
393-
return errors.Wrap(err, "rename compaction segment")
393+
if err := fileutil.Rename(csf.Name(), candidates[0].Name()); err != nil {
394+
var merr tsdb_errors.MultiError
395+
merr.Add(errors.Wrap(err, "rename compaction segment"))
396+
merr.Add(os.RemoveAll(csf.Name()))
397+
return merr.Err()
394398
}
399+
395400
for _, f := range candidates[1:] {
396401
f.Close() // need close before remove on platform windows
397402
if err := os.RemoveAll(f.Name()); err != nil {

0 commit comments

Comments
 (0)