From e64a1499845783cbb7136fe9f02bdf2349655486 Mon Sep 17 00:00:00 2001 From: Ganesh Vernekar <15064823+codesome@users.noreply.github.com> Date: Mon, 23 Mar 2020 14:49:44 +0530 Subject: [PATCH] Close Head in DBReadOnly.FlushWAL (#7022) Signed-off-by: Ganesh Vernekar --- tsdb/db.go | 8 +++++++- tsdb/wal/wal.go | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tsdb/db.go b/tsdb/db.go index 072acb9bf..3cd75bae3 100644 --- a/tsdb/db.go +++ b/tsdb/db.go @@ -294,7 +294,7 @@ func OpenDBReadOnly(dir string, l log.Logger) (*DBReadOnly, error) { // Samples that are in existing blocks will not be written to the new block. // Note that if the read only database is running concurrently with a // writable database then writing the WAL to the database directory can race. -func (db *DBReadOnly) FlushWAL(dir string) error { +func (db *DBReadOnly) FlushWAL(dir string) (returnErr error) { blockReaders, err := db.Blocks() if err != nil { return errors.Wrap(err, "read blocks") @@ -311,6 +311,12 @@ func (db *DBReadOnly) FlushWAL(dir string) error { if err != nil { return err } + defer func() { + var merr tsdb_errors.MultiError + merr.Add(returnErr) + merr.Add(errors.Wrap(head.Close(), "closing Head")) + returnErr = merr.Err() + }() // Set the min valid time for the ingested wal samples // to be no lower than the maxt of the last block. if err := head.Init(maxBlockTime); err != nil { diff --git a/tsdb/wal/wal.go b/tsdb/wal/wal.go index 05cc02f66..e4c198f68 100644 --- a/tsdb/wal/wal.go +++ b/tsdb/wal/wal.go @@ -730,6 +730,11 @@ func (w *WAL) Close() (err error) { return errors.New("wal already closed") } + if w.segment == nil { + w.closed = true + return nil + } + // Flush the last page and zero out all its remaining size. // We must not flush an empty page as it would falsely signal // the segment is done if we start writing to it again after opening.