Skip to content

Commit

Permalink
Add BypassDirLock option (#1243)
Browse files Browse the repository at this point in the history
Fixes #988
Based on #1046

This PR adds `BypassDirLock` option which allows badger to work without
acquiring a lock on the data directory. This option could lead to data
corruption if used with multiple badger instances trying to write to
the same badger directory.

Co-authored-by: Ehsan Noureddin Moosa <[email protected]>
  • Loading branch information
Ibrahim Jarif and ehsannm authored Mar 13, 2020
1 parent c6c1e5e commit 1bcbefc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
44 changes: 23 additions & 21 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,33 +234,35 @@ func Open(opt Options) (db *DB, err error) {
if err := createDirs(opt); err != nil {
return nil, err
}
dirLockGuard, err = acquireDirectoryLock(opt.Dir, lockFile, opt.ReadOnly)
if err != nil {
return nil, err
}
defer func() {
if dirLockGuard != nil {
_ = dirLockGuard.release()
}
}()
absDir, err := filepath.Abs(opt.Dir)
if err != nil {
return nil, err
}
absValueDir, err := filepath.Abs(opt.ValueDir)
if err != nil {
return nil, err
}
if absValueDir != absDir {
valueDirLockGuard, err = acquireDirectoryLock(opt.ValueDir, lockFile, opt.ReadOnly)
if !opt.BypassLockGuard {
dirLockGuard, err = acquireDirectoryLock(opt.Dir, lockFile, opt.ReadOnly)
if err != nil {
return nil, err
}
defer func() {
if valueDirLockGuard != nil {
_ = valueDirLockGuard.release()
if dirLockGuard != nil {
_ = dirLockGuard.release()
}
}()
absDir, err := filepath.Abs(opt.Dir)
if err != nil {
return nil, err
}
absValueDir, err := filepath.Abs(opt.ValueDir)
if err != nil {
return nil, err
}
if absValueDir != absDir {
valueDirLockGuard, err = acquireDirectoryLock(opt.ValueDir, lockFile, opt.ReadOnly)
if err != nil {
return nil, err
}
defer func() {
if valueDirLockGuard != nil {
_ = valueDirLockGuard.release()
}
}()
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ type Options struct {
EncryptionKey []byte // encryption key
EncryptionKeyRotationDuration time.Duration // key rotation duration

// BypassLockGaurd will bypass the lock guard on badger. Bypassing lock
// guard can cause data corruption if multiple badger instances are using
// the same directory. Use this options with caution.
BypassLockGuard bool

// ChecksumVerificationMode decides when db should verify checksums for SSTable blocks.
ChecksumVerificationMode options.ChecksumVerificationMode

Expand Down Expand Up @@ -575,3 +580,16 @@ func (opt Options) WithZSTDCompressionLevel(cLevel int) Options {
opt.ZSTDCompressionLevel = cLevel
return opt
}

// WithBypassLockGuard returns a new Options value with BypassLockGuard
// set to the given value.
//
// When BypassLockGuard option is set, badger will not acquire a lock on the
// directory. This could lead to data corruption if multiple badger instances
// write to the same data directory. Use this option with caution.
//
// The default value of BypassLockGuard is false.
func (opt Options) WithBypassLockGuard(b bool) Options {
opt.BypassLockGuard = b
return opt
}

0 comments on commit 1bcbefc

Please sign in to comment.