File tree 2 files changed +25
-0
lines changed
2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -96,6 +96,7 @@ type Table struct {
96
96
97
97
fd * os.File // Own fd.
98
98
tableSize int // Initialized in OpenTable, using fd.Stat().
99
+ bfLock sync.Mutex
99
100
100
101
blockIndex []* pb.BlockOffset
101
102
ref int32 // For file garbage collection. Atomic.
@@ -381,7 +382,9 @@ func (t *Table) readIndex() error {
381
382
t .blockIndex = index .Offsets
382
383
383
384
if t .opt .LoadBloomsOnOpen {
385
+ t .bfLock .Lock ()
384
386
t .bf , _ = t .readBloomFilter ()
387
+ t .bfLock .Unlock ()
385
388
}
386
389
387
390
return nil
@@ -511,11 +514,13 @@ func (t *Table) DoesNotHave(hash uint64) bool {
511
514
512
515
// Return fast if the cache is absent.
513
516
if t .opt .BfCache == nil {
517
+ t .bfLock .Lock ()
514
518
// Load bloomfilter into memory if the cache is absent.
515
519
if t .bf == nil {
516
520
y .AssertTrue (! t .opt .LoadBloomsOnOpen )
517
521
t .bf , _ = t .readBloomFilter ()
518
522
}
523
+ t .bfLock .Unlock ()
519
524
return ! t .bf .Has (hash )
520
525
}
521
526
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ import (
25
25
"os"
26
26
"sort"
27
27
"strings"
28
+ "sync"
28
29
"testing"
29
30
"time"
30
31
@@ -932,3 +933,22 @@ func TestOpenKVSize(t *testing.T) {
932
933
var entrySize uint64 = 15 /* DiffKey len */ + 4 /* Header Size */ + 4 /* Encoded vp */
933
934
require .Equal (t , entrySize , table .EstimatedSize ())
934
935
}
936
+
937
+ // Run this test with command "go test -race -run TestDoesNotHaveRace"
938
+ func TestDoesNotHaveRace (t * testing.T ) {
939
+ opts := getTestTableOptions ()
940
+ f := buildTestTable (t , "key" , 10000 , opts )
941
+ table , err := OpenTable (f , opts )
942
+ require .NoError (t , err )
943
+ defer table .DecrRef ()
944
+
945
+ var wg sync.WaitGroup
946
+ wg .Add (5 )
947
+ for i := 0 ; i < 5 ; i ++ {
948
+ go func () {
949
+ require .True (t , table .DoesNotHave (uint64 (1237882 )))
950
+ wg .Done ()
951
+ }()
952
+ }
953
+ wg .Wait ()
954
+ }
You can’t perform that action at this time.
0 commit comments