Skip to content

Commit

Permalink
revert: boltdbshipper: fix crash of "timeout" panic when boltdbshippe…
Browse files Browse the repository at this point in the history
…r do init (#3369)

This appears to have introduced a different problem, still investigating.
  • Loading branch information
slim-bean committed Feb 27, 2021
1 parent cf8be4a commit 09a9357
Showing 1 changed file with 2 additions and 24 deletions.
26 changes: 2 additions & 24 deletions pkg/storage/stores/shipper/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
"os"
"runtime/debug"
"strings"
"time"

"github.com/cortexproject/cortex/pkg/chunk"
"github.com/cortexproject/cortex/pkg/chunk/local"
util_log "github.com/cortexproject/cortex/pkg/util/log"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
"go.etcd.io/bbolt"

"github.com/grafana/loki/pkg/chunkenc"
Expand Down Expand Up @@ -143,41 +142,20 @@ func safeOpenBoltDbFile(path string, ret chan *result) {
debug.SetPanicOnFault(true)
res := &result{}

var file *os.File
defer func() {
if r := recover(); r != nil {
res.err = fmt.Errorf("recovered from panic opening boltdb file: %v", r)

// A panic when opening the boltdb file may have left a lingering flock
// at the OS level, attempt to clear the flock that may exist
attemptCleanFlock(file)
}

// Return the result object on the channel to unblock the calling thread
ret <- res
}()

b, err := openBoltdbFile(path, func(s string, i int, mode os.FileMode) (f *os.File, err error) {
file, err = os.OpenFile(s, i, mode)
return file, err
})
b, err := local.OpenBoltdbFile(path)
res.boltdb = b
res.err = err
}

func openBoltdbFile(path string, f func(string, int, os.FileMode) (*os.File, error)) (*bbolt.DB, error) {
return bbolt.Open(path, 0666, &bbolt.Options{Timeout: 5 * time.Second, OpenFile: f})
}

// open a dummy db with previous opened file
// intentionally throw an error using the provided OpenFile function
// the error will trigger the flock cleanup within boltdb and should free all resources
func attemptCleanFlock(f *os.File) {
_, _ = openBoltdbFile("", func(string, int, os.FileMode) (*os.File, error) {
return f, errors.New("error for cleanup")
})
}

// RemoveDirectories will return a new slice with any StorageObjects identified as directories removed.
func RemoveDirectories(incoming []chunk.StorageObject) []chunk.StorageObject {
outgoing := make([]chunk.StorageObject, 0, len(incoming))
Expand Down

0 comments on commit 09a9357

Please sign in to comment.