Skip to content

Commit 720eee3

Browse files
Ibrahim Jarifehsannm
andcommitted
Add BypassDirLock option (#1243)
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]> (cherry picked from commit 1bcbefc)
1 parent 6a21a40 commit 720eee3

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

db.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -217,34 +217,36 @@ func Open(opt Options) (db *DB, err error) {
217217
}
218218
}
219219
}
220-
absDir, err := filepath.Abs(opt.Dir)
221-
if err != nil {
222-
return nil, err
223-
}
224-
absValueDir, err := filepath.Abs(opt.ValueDir)
225-
if err != nil {
226-
return nil, err
227-
}
228220
var dirLockGuard, valueDirLockGuard *directoryLockGuard
229-
dirLockGuard, err = acquireDirectoryLock(opt.Dir, lockFile, opt.ReadOnly)
230-
if err != nil {
231-
return nil, err
232-
}
233-
defer func() {
234-
if dirLockGuard != nil {
235-
_ = dirLockGuard.release()
221+
if !opt.BypassLockGuard {
222+
absDir, err := filepath.Abs(opt.Dir)
223+
if err != nil {
224+
return nil, err
236225
}
237-
}()
238-
if absValueDir != absDir {
239-
valueDirLockGuard, err = acquireDirectoryLock(opt.ValueDir, lockFile, opt.ReadOnly)
226+
absValueDir, err := filepath.Abs(opt.ValueDir)
227+
if err != nil {
228+
return nil, err
229+
}
230+
dirLockGuard, err = acquireDirectoryLock(opt.Dir, lockFile, opt.ReadOnly)
240231
if err != nil {
241232
return nil, err
242233
}
243234
defer func() {
244-
if valueDirLockGuard != nil {
245-
_ = valueDirLockGuard.release()
235+
if dirLockGuard != nil {
236+
_ = dirLockGuard.release()
246237
}
247238
}()
239+
if absValueDir != absDir {
240+
valueDirLockGuard, err = acquireDirectoryLock(opt.ValueDir, lockFile, opt.ReadOnly)
241+
if err != nil {
242+
return nil, err
243+
}
244+
defer func() {
245+
if valueDirLockGuard != nil {
246+
_ = valueDirLockGuard.release()
247+
}
248+
}()
249+
}
248250
}
249251
if !(opt.ValueLogFileSize <= 2<<30 && opt.ValueLogFileSize >= 1<<20) {
250252
return nil, ErrValueLogSize

options.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ type Options struct {
6767
// When set, checksum will be validated for each entry read from the value log file.
6868
VerifyValueChecksum bool
6969

70+
// BypassLockGaurd will bypass the lock guard on badger. Bypassing lock
71+
// guard can cause data corruption if multiple badger instances are using
72+
// the same directory. Use this options with caution.
73+
BypassLockGuard bool
74+
7075
// Transaction start and commit timestamps are managed by end-user.
7176
// This is only useful for databases built on top of Badger (like Dgraph).
7277
// Not recommended for most users.
@@ -400,3 +405,16 @@ func (opt Options) WithVerifyValueChecksum(val bool) Options {
400405
opt.VerifyValueChecksum = val
401406
return opt
402407
}
408+
409+
// WithBypassLockGuard returns a new Options value with BypassLockGuard
410+
// set to the given value.
411+
//
412+
// When BypassLockGuard option is set, badger will not acquire a lock on the
413+
// directory. This could lead to data corruption if multiple badger instances
414+
// write to the same data directory. Use this option with caution.
415+
//
416+
// The default value of BypassLockGuard is false.
417+
func (opt Options) WithBypassLockGuard(b bool) Options {
418+
opt.BypassLockGuard = b
419+
return opt
420+
}

0 commit comments

Comments
 (0)