From c5a832b39440547f681dc08f06343c79d5ba7f45 Mon Sep 17 00:00:00 2001 From: Arthur Silva Sens Date: Wed, 21 Oct 2020 12:08:28 -0300 Subject: [PATCH] Close resources after failing to startup TSDB (#8031) * Close resources after failing to startup TSDB Signed-off-by: arthursens * Return close error instead of logging Signed-off-by: arthursens * Change named return's name Signed-off-by: arthursens --- tsdb/db.go | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/tsdb/db.go b/tsdb/db.go index 213a44122..5be60b670 100644 --- a/tsdb/db.go +++ b/tsdb/db.go @@ -553,7 +553,7 @@ func validateOpts(opts *Options, rngs []int64) (*Options, []int64) { return opts, rngs } -func open(dir string, l log.Logger, r prometheus.Registerer, opts *Options, rngs []int64) (db *DB, err error) { +func open(dir string, l log.Logger, r prometheus.Registerer, opts *Options, rngs []int64) (db *DB, returnedErr error) { if err := os.MkdirAll(dir, 0777); err != nil { return nil, err } @@ -595,6 +595,19 @@ func open(dir string, l log.Logger, r prometheus.Registerer, opts *Options, rngs chunkPool: chunkenc.NewPool(), blocksToDelete: opts.BlocksToDelete, } + defer func() { + // Close files if startup fails somewhere. + if returnedErr == nil { + return + } + + var merr tsdb_errors.MultiError + merr.Add(returnedErr) + merr.Add(errors.Wrap(db.Close(), "close DB after failed startup")) + + returnedErr = merr.Err() + }() + if db.blocksToDelete == nil { db.blocksToDelete = DefaultBlocksToDelete(db) } @@ -612,10 +625,10 @@ func open(dir string, l log.Logger, r prometheus.Registerer, opts *Options, rngs } ctx, cancel := context.WithCancel(context.Background()) - db.compactor, err = NewLeveledCompactor(ctx, r, l, rngs, db.chunkPool) - if err != nil { + db.compactor, returnedErr = NewLeveledCompactor(ctx, r, l, rngs, db.chunkPool) + if returnedErr != nil { cancel() - return nil, errors.Wrap(err, "create leveled compactor") + return nil, errors.Wrap(returnedErr, "create leveled compactor") } db.compactCancel = cancel @@ -627,15 +640,15 @@ func open(dir string, l log.Logger, r prometheus.Registerer, opts *Options, rngs if opts.WALSegmentSize > 0 { segmentSize = opts.WALSegmentSize } - wlog, err = wal.NewSize(l, r, walDir, segmentSize, opts.WALCompression) - if err != nil { - return nil, err + wlog, returnedErr = wal.NewSize(l, r, walDir, segmentSize, opts.WALCompression) + if returnedErr != nil { + return nil, returnedErr } } - db.head, err = NewHead(r, l, wlog, rngs[0], dir, db.chunkPool, opts.StripeSize, opts.SeriesLifecycleCallback) - if err != nil { - return nil, err + db.head, returnedErr = NewHead(r, l, wlog, rngs[0], dir, db.chunkPool, opts.StripeSize, opts.SeriesLifecycleCallback) + if returnedErr != nil { + return nil, returnedErr } // Register metrics after assigning the head block. @@ -1333,7 +1346,9 @@ func (db *DB) Close() error { if db.lockf != nil { merr.Add(db.lockf.Release()) } - merr.Add(db.head.Close()) + if db.head != nil { + merr.Add(db.head.Close()) + } return merr.Err() }