Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition in DoesNotHave #1287

Merged
merged 4 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ type Table struct {

fd *os.File // Own fd.
tableSize int // Initialized in OpenTable, using fd.Stat().
bfLock sync.Mutex

blockIndex []*pb.BlockOffset
ref int32 // For file garbage collection. Atomic.
Expand Down Expand Up @@ -381,6 +382,7 @@ func (t *Table) readIndex() error {
t.blockIndex = index.Offsets

if t.opt.LoadBloomsOnOpen {
// We don't need to acquire lock here because this will be called in sequential manner.
t.bf, _ = t.readBloomFilter()
}

Expand Down Expand Up @@ -511,11 +513,13 @@ func (t *Table) DoesNotHave(hash uint64) bool {

// Return fast if the cache is absent.
if t.opt.BfCache == nil {
t.bfLock.Lock()
// Load bloomfilter into memory if the cache is absent.
if t.bf == nil {
y.AssertTrue(!t.opt.LoadBloomsOnOpen)
t.bf, _ = t.readBloomFilter()
}
t.bfLock.Unlock()
return !t.bf.Has(hash)
}

Expand Down
20 changes: 20 additions & 0 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"sort"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -932,3 +933,22 @@ func TestOpenKVSize(t *testing.T) {
var entrySize uint64 = 15 /* DiffKey len */ + 4 /* Header Size */ + 4 /* Encoded vp */
require.Equal(t, entrySize, table.EstimatedSize())
}

// Run this test with command "go test -race -run TestDoesNotHaveRace"
func TestDoesNotHaveRace(t *testing.T) {
opts := getTestTableOptions()
f := buildTestTable(t, "key", 10000, opts)
table, err := OpenTable(f, opts)
require.NoError(t, err)
defer table.DecrRef()

var wg sync.WaitGroup
wg.Add(5)
for i := 0; i < 5; i++ {
go func() {
require.True(t, table.DoesNotHave(uint64(1237882)))
wg.Done()
}()
}
wg.Wait()
}