From 1b23d62e3fea8284f3bfecc5923bd1420d45ced6 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Mon, 19 Dec 2016 22:37:03 +0100 Subject: [PATCH] Properly close files before reopening --- db.go | 20 ++------------------ head.go | 28 ++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/db.go b/db.go index d9dd1cd1f..70f4400a6 100644 --- a/db.go +++ b/db.go @@ -351,27 +351,11 @@ func (s *Shard) persist() error { return err } - sf, err := os.Create(chunksFileName(p)) + n, err := head.persist(p) if err != nil { return err } - xf, err := os.Create(indexFileName(p)) - if err != nil { - return err - } - - iw := newIndexWriter(xf) - sw := newSeriesWriter(sf, iw, s.head.stats.MinTime) - - defer sw.Close() - defer iw.Close() - - if err := head.persist(sw, iw); err != nil { - return err - } - - sz := fmt.Sprintf("%.2fMiB", float64(sw.Size()+iw.Size())/1024/1024) - + sz := fmt.Sprintf("%.2fMiB", float64(n)/1024/1024) s.logger.Log("size", sz, "samples", head.stats.SampleCount, "chunks", head.stats.ChunkCount, "msg", "persisted head") // Reopen block as persisted block for querying. diff --git a/head.go b/head.go index d3c7e4e9e..dbeee982e 100644 --- a/head.go +++ b/head.go @@ -2,6 +2,7 @@ package tsdb import ( "math" + "os" "sort" "sync" @@ -135,15 +136,30 @@ func (h *HeadBlock) append(hash uint64, lset Labels, ts int64, v float64) error return nil } -func (h *HeadBlock) persist(sw SeriesWriter, iw IndexWriter) error { +func (h *HeadBlock) persist(p string) (int64, error) { + sf, err := os.Create(chunksFileName(p)) + if err != nil { + return 0, err + } + xf, err := os.Create(indexFileName(p)) + if err != nil { + return 0, err + } + + iw := newIndexWriter(xf) + sw := newSeriesWriter(sf, iw, h.stats.MinTime) + + defer sw.Close() + defer iw.Close() + for ref, cd := range h.index.forward { if err := sw.WriteSeries(ref, cd.lset, []*chunkDesc{cd}); err != nil { - return err + return 0, err } } if err := iw.WriteStats(h.stats); err != nil { - return err + return 0, err } for n, v := range h.index.values { s := make([]string, 0, len(v)) @@ -152,15 +168,15 @@ func (h *HeadBlock) persist(sw SeriesWriter, iw IndexWriter) error { } if err := iw.WriteLabelIndex([]string{n}, s); err != nil { - return err + return 0, err } } for t := range h.index.postings.m { if err := iw.WritePostings(t.name, t.value, h.index.postings.get(t)); err != nil { - return err + return 0, err } } - return nil + return iw.Size() + sw.Size(), nil }