Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loki: handle faults when opening boltdb files #2988

Merged
merged 4 commits into from
Nov 25, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions pkg/storage/stores/shipper/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"runtime/debug"
"strings"

"github.com/cortexproject/cortex/pkg/chunk/local"
Expand Down Expand Up @@ -120,12 +121,37 @@ func CompressFile(src, dest string) error {
return compressedFile.Sync()
}

type result struct {
boltdb *bbolt.DB
err error
}

// SafeOpenBoltdbFile will recover from a panic opening a DB file, and return the panic message in the err return object.
func SafeOpenBoltdbFile(path string) (boltdb *bbolt.DB, err error) {
func SafeOpenBoltdbFile(path string) (*bbolt.DB, error) {
result := make(chan *result)
// Open the file in a separate goroutine because we want to change
// the behavior of a Fault for just this operation and not for the
// calling goroutine
go safeOpenBoltDbFile(path, result)
res := <-result
return res.boltdb, res.err
}

func safeOpenBoltDbFile(path string, ret chan *result) {
// boltdb can throw faults which are not caught be recover unless we turn them into panics
slim-bean marked this conversation as resolved.
Show resolved Hide resolved
debug.SetPanicOnFault(true)
res := &result{}

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

// Return the result object on the channel to unblock the calling thread
ret <- res
}()
return local.OpenBoltdbFile(path)

b, err := local.OpenBoltdbFile(path)
res.boltdb = b
res.err = err
}