mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-24 05:04:05 -08:00
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>
This commit is contained in:
parent
fdf1c29224
commit
c5a832b394
37
tsdb/db.go
37
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()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue