From 7149cb9afe3e531c6805dfd977109f754ad05fb5 Mon Sep 17 00:00:00 2001 From: James Mills <1290234+prologic@users.noreply.github.com> Date: Fri, 22 Mar 2019 17:59:07 +1000 Subject: [PATCH] Fixed concurrency bug with reopening datafiles when maxDatafileSize is exceeded --- bitcask.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bitcask.go b/bitcask.go index e43c238..80c3ca5 100644 --- a/bitcask.go +++ b/bitcask.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "strings" + "sync" "github.com/gofrs/flock" "github.com/prologic/trie" @@ -40,6 +41,8 @@ var ( // and in-memory hash of key/value pairs as per the Bitcask paper and seen // in the Riak database. type Bitcask struct { + mu sync.RWMutex + *flock.Flock config *config @@ -175,6 +178,9 @@ func (b *Bitcask) Fold(f func(key string) error) error { } func (b *Bitcask) put(key string, value []byte) (int64, error) { + b.mu.Lock() + defer b.mu.Unlock() + size := b.curr.Size() if size >= int64(b.config.maxDatafileSize) { err := b.curr.Close()