@ -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 , r eturnedE rr 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 , r eturnedE rr = NewLeveledCompactor ( ctx , r , l , rngs , db . chunkPool )
if r eturnedE rr != nil {
cancel ( )
return nil , errors . Wrap ( err, "create leveled compactor" )
return nil , errors . Wrap ( r eturnedE rr, "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 , r eturnedE rr = wal . NewSize ( l , r , walDir , segmentSize , opts . WALCompression )
if r eturnedE rr != nil {
return nil , r eturnedE rr
}
}
db . head , err = NewHead ( r , l , wlog , rngs [ 0 ] , dir , db . chunkPool , opts . StripeSize , opts . SeriesLifecycleCallback )
if err != nil {
return nil , err
db . head , r eturnedE rr = NewHead ( r , l , wlog , rngs [ 0 ] , dir , db . chunkPool , opts . StripeSize , opts . SeriesLifecycleCallback )
if r eturnedE rr != nil {
return nil , r eturnedE rr
}
// 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 ( )
}