Skip to content

Commit

Permalink
Fail fast if cgo is disabled and compression is ZSTD (#1284)
Browse files Browse the repository at this point in the history
As seen in issue hypermodeinc/dgraph#4995, if
badger is built without CGO and compression is set to ZSTD, badger
would crash when level 0 is compacted. This could take from a few
seconds to a few minutes for the crash to show up.

With this commit, `badger.Open()` call will return an error if
compression is set to ZSTD and badger was built without CGO.
  • Loading branch information
Ibrahim Jarif authored Apr 8, 2020
1 parent 6e032c0 commit 2e708d9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
5 changes: 5 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ func Open(opt Options) (db *DB, err error) {
return nil, ErrInvalidLoadingMode
}

// Return error if badger is built without cgo and compression is set to ZSTD.
if opt.Compression == options.ZSTD && !y.CgoEnabled {
return nil, y.ErrZstdCgo
}

// Compact L0 on close if either it is set or if KeepL0InMemory is set. When
// keepL0InMemory is set we need to compact L0 on close otherwise we might lose data.
opt.CompactL0OnClose = opt.CompactL0OnClose || opt.KeepL0InMemory
Expand Down
13 changes: 10 additions & 3 deletions y/y.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ import (
"github.com/pkg/errors"
)

// ErrEOF indicates an end of file when trying to read from a memory mapped file
// and encountering the end of slice.
var ErrEOF = errors.New("End of mapped region")
var (
// ErrEOF indicates an end of file when trying to read from a memory mapped file
// and encountering the end of slice.
ErrEOF = errors.New("End of mapped region")

// ErrZstdCgo indicates that badger was built without cgo but ZSTD
// compression algorithm is being used for compression. ZSTD cannot work
// without CGO.
ErrZstdCgo = errors.New("zstd compression requires building badger with cgo enabled")
)

const (
// Sync indicates that O_DSYNC should be set on the underlying file,
Expand Down
10 changes: 2 additions & 8 deletions y/zstd_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,15 @@

package y

import (
"errors"
)

var errZstdCgo = errors.New("zstd compression requires building badger with cgo enabled")

// CgoEnabled is used to check if CGO is enabled while building badger.
const CgoEnabled = false

// ZSTDDecompress decompresses a block using ZSTD algorithm.
func ZSTDDecompress(dst, src []byte) ([]byte, error) {
return nil, errZstdCgo
return nil, ErrZstdCgo
}

// ZSTDCompress compresses a block using ZSTD algorithm.
func ZSTDCompress(dst, src []byte, compressionLevel int) ([]byte, error) {
return nil, errZstdCgo
return nil, ErrZstdCgo
}

0 comments on commit 2e708d9

Please sign in to comment.