Browse Source

Close resources after failing to startup TSDB (#8031)

* Close resources after failing to startup TSDB

Signed-off-by: arthursens <arthursens2005@gmail.com>

* Return close error instead of logging

Signed-off-by: arthursens <arthursens2005@gmail.com>

* Change named return's name

Signed-off-by: arthursens <arthursens2005@gmail.com>
pull/8087/head
Arthur Silva Sens 4 years ago committed by GitHub
parent
commit
c5a832b394
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 35
      tsdb/db.go

35
tsdb/db.go

@ -553,7 +553,7 @@ func validateOpts(opts *Options, rngs []int64) (*Options, []int64) {
return opts, rngs 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 { if err := os.MkdirAll(dir, 0777); err != nil {
return nil, err return nil, err
} }
@ -595,6 +595,19 @@ func open(dir string, l log.Logger, r prometheus.Registerer, opts *Options, rngs
chunkPool: chunkenc.NewPool(), chunkPool: chunkenc.NewPool(),
blocksToDelete: opts.BlocksToDelete, 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 { if db.blocksToDelete == nil {
db.blocksToDelete = DefaultBlocksToDelete(db) 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()) ctx, cancel := context.WithCancel(context.Background())
db.compactor, err = NewLeveledCompactor(ctx, r, l, rngs, db.chunkPool) db.compactor, returnedErr = NewLeveledCompactor(ctx, r, l, rngs, db.chunkPool)
if err != nil { if returnedErr != nil {
cancel() cancel()
return nil, errors.Wrap(err, "create leveled compactor") return nil, errors.Wrap(returnedErr, "create leveled compactor")
} }
db.compactCancel = cancel db.compactCancel = cancel
@ -627,15 +640,15 @@ func open(dir string, l log.Logger, r prometheus.Registerer, opts *Options, rngs
if opts.WALSegmentSize > 0 { if opts.WALSegmentSize > 0 {
segmentSize = opts.WALSegmentSize segmentSize = opts.WALSegmentSize
} }
wlog, err = wal.NewSize(l, r, walDir, segmentSize, opts.WALCompression) wlog, returnedErr = wal.NewSize(l, r, walDir, segmentSize, opts.WALCompression)
if err != nil { if returnedErr != nil {
return nil, err return nil, returnedErr
} }
} }
db.head, err = NewHead(r, l, wlog, rngs[0], dir, db.chunkPool, opts.StripeSize, opts.SeriesLifecycleCallback) db.head, returnedErr = NewHead(r, l, wlog, rngs[0], dir, db.chunkPool, opts.StripeSize, opts.SeriesLifecycleCallback)
if err != nil { if returnedErr != nil {
return nil, err return nil, returnedErr
} }
// Register metrics after assigning the head block. // Register metrics after assigning the head block.
@ -1333,7 +1346,9 @@ func (db *DB) Close() error {
if db.lockf != nil { if db.lockf != nil {
merr.Add(db.lockf.Release()) merr.Add(db.lockf.Release())
} }
if db.head != nil {
merr.Add(db.head.Close()) merr.Add(db.head.Close())
}
return merr.Err() return merr.Err()
} }

Loading…
Cancel
Save