From b8a7e80f9b93311ecc6eb4d83bed48d1cda54e3b Mon Sep 17 00:00:00 2001 From: Ganesh Vernekar Date: Wed, 15 Jul 2020 11:18:48 +0530 Subject: [PATCH] Fix review comments Signed-off-by: Ganesh Vernekar --- tsdb/chunks/chunks.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tsdb/chunks/chunks.go b/tsdb/chunks/chunks.go index ffd3e5302..5a824d965 100644 --- a/tsdb/chunks/chunks.go +++ b/tsdb/chunks/chunks.go @@ -217,7 +217,7 @@ func (w *Writer) cut() error { return nil } -func cutSegmentFile(dirFile *os.File, magicNumber uint32, chunksFormat byte, allocSize int64) (headerSize int, newFile *os.File, seq int, err error) { +func cutSegmentFile(dirFile *os.File, magicNumber uint32, chunksFormat byte, allocSize int64) (headerSize int, newFile *os.File, seq int, returnErr error) { p, seq, err := nextSequenceFile(dirFile.Name()) if err != nil { return 0, nil, 0, errors.Wrap(err, "next sequence file") @@ -227,6 +227,18 @@ func cutSegmentFile(dirFile *os.File, magicNumber uint32, chunksFormat byte, all if err != nil { return 0, nil, 0, errors.Wrap(err, "open temp file") } + defer func() { + if returnErr != nil { + var merr tsdb_errors.MultiError + merr.Add(returnErr) + if f != nil { + merr.Add(f.Close()) + } + // Calling RemoveAll on a non-existent file does not return error. + merr.Add(os.RemoveAll(ptmp)) + returnErr = merr.Err() + } + }() if allocSize > 0 { if err = fileutil.Preallocate(f, allocSize, true); err != nil { return 0, nil, 0, errors.Wrap(err, "preallocate") @@ -248,6 +260,7 @@ func cutSegmentFile(dirFile *os.File, magicNumber uint32, chunksFormat byte, all if err := f.Close(); err != nil { return 0, nil, 0, errors.Wrap(err, "close temp file") } + f = nil if err := fileutil.Rename(ptmp, p); err != nil { return 0, nil, 0, errors.Wrap(err, "replace file") @@ -257,7 +270,7 @@ func cutSegmentFile(dirFile *os.File, magicNumber uint32, chunksFormat byte, all if err != nil { return 0, nil, 0, errors.Wrap(err, "open final file") } - // Skip the header for the further writes. + // Skip header for further writes. if _, err := f.Seek(int64(n), 0); err != nil { return 0, nil, 0, errors.Wrap(err, "seek in final file") }