mirror of
https://github.com/taigrr/bitcask
synced 2025-01-18 04:03:17 -08:00
* add failing test case to highlight the race condition on bug note : the test "TestLock" is non deterministic, its outcome depends on the sequence of instructions yielded by the go scheduler on each run. There are two values, "goroutines" and "succesfulLockCount", which can be edited to see how the test performs. With the committed value, resp "20" and "50", I had a 100% failure on my local machine, running linux (Ubuntu 20.04). Sample test output : $ go test . -run TestLock --- FAIL: TestLock (0.17s) lock_test.go:91: [runner 14] lockCounter was > 1 on 5 occasions, max seen value was 2 lock_test.go:91: [runner 03] lockCounter was > 1 on 2 occasions, max seen value was 3 lock_test.go:91: [runner 02] lockCounter was > 1 on 3 occasions, max seen value was 3 lock_test.go:91: [runner 00] lockCounter was > 1 on 1 occasions, max seen value was 2 lock_test.go:91: [runner 12] lockCounter was > 1 on 7 occasions, max seen value was 3 lock_test.go:91: [runner 01] lockCounter was > 1 on 8 occasions, max seen value was 2 lock_test.go:91: [runner 04] lockCounter was > 1 on 6 occasions, max seen value was 4 lock_test.go:91: [runner 13] lockCounter was > 1 on 1 occasions, max seen value was 2 lock_test.go:91: [runner 17] lockCounter was > 1 on 4 occasions, max seen value was 2 lock_test.go:91: [runner 10] lockCounter was > 1 on 3 occasions, max seen value was 2 lock_test.go:91: [runner 08] lockCounter was > 1 on 6 occasions, max seen value was 2 lock_test.go:91: [runner 09] lockCounter was > 1 on 4 occasions, max seen value was 2 lock_test.go:91: [runner 05] lockCounter was > 1 on 1 occasions, max seen value was 2 lock_test.go:91: [runner 19] lockCounter was > 1 on 3 occasions, max seen value was 3 lock_test.go:91: [runner 07] lockCounter was > 1 on 4 occasions, max seen value was 3 lock_test.go:91: [runner 11] lockCounter was > 1 on 9 occasions, max seen value was 2 lock_test.go:91: [runner 15] lockCounter was > 1 on 1 occasions, max seen value was 3 lock_test.go:91: [runner 16] lockCounter was > 1 on 1 occasions, max seen value was 3 FAIL FAIL github.com/prologic/bitcask 0.176s FAIL * flock: create a wrapper module, local to bitcask, around gofrs.Flock the racy TestLock has been moved to bitcask/flock * flock: add test for expected regular locking behavior * flock: replace gofrs/flock with local implementation * update go.sum * Add build constraint for flock_unix.go Co-authored-by: James Mills <prologic@shortcircuit.net.au>
80 lines
1.6 KiB
Go
80 lines
1.6 KiB
Go
// +build !aix,!windows
|
|
|
|
package flock
|
|
|
|
import (
|
|
"os"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func lock_sys(path string, nonBlocking bool) (_ *os.File, err error) {
|
|
var fh *os.File
|
|
|
|
fh, err = os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer func() {
|
|
if err != nil {
|
|
fh.Close()
|
|
}
|
|
}()
|
|
|
|
flag := unix.LOCK_EX
|
|
if nonBlocking {
|
|
flag |= unix.LOCK_NB
|
|
}
|
|
|
|
err = unix.Flock(int(fh.Fd()), flag)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !sameInodes(fh, path) {
|
|
return nil, ErrInodeChangedAtPath
|
|
}
|
|
|
|
return fh, nil
|
|
}
|
|
|
|
func rm_if_match(fh *os.File, path string) error {
|
|
// Sanity check :
|
|
// before running "rm", check that the file pointed at by the
|
|
// filehandle has the same inode as the path in the filesystem
|
|
//
|
|
// If this sanity check doesn't pass, store a "ErrInodeChangedAtPath" error,
|
|
// if the check passes, run os.Remove, and store the error if any.
|
|
//
|
|
// note : this sanity check is in no way atomic, but :
|
|
// - as long as only cooperative processes are involved, it will work as intended
|
|
// - it allows to avoid 99.9% the major pitfall case: "root user forcefully removed the lockfile"
|
|
|
|
if !sameInodes(fh, path) {
|
|
return ErrInodeChangedAtPath
|
|
}
|
|
|
|
return os.Remove(path)
|
|
}
|
|
|
|
func sameInodes(f *os.File, path string) bool {
|
|
// get inode from opened file f:
|
|
var fstat unix.Stat_t
|
|
err := unix.Fstat(int(f.Fd()), &fstat)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
fileIno := fstat.Ino
|
|
|
|
// get inode for path on disk:
|
|
var dstat unix.Stat_t
|
|
err = unix.Stat(path, &dstat)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
pathIno := dstat.Ino
|
|
|
|
return pathIno == fileIno
|
|
}
|