aboutsummaryrefslogtreecommitdiff
path: root/weed/wdclient/vid_map_test.go
diff options
context:
space:
mode:
authorLHHDZ <changlin.shi@ly.com>2022-09-06 11:00:16 +0800
committerGitHub <noreply@github.com>2022-09-05 20:00:16 -0700
commitbc629665de418ce71c85f83ae618b8f7e5ac42bc (patch)
treeead15a555e3656db5ed0a2fc5de7ec8545c37e01 /weed/wdclient/vid_map_test.go
parent7c277f36ec0cbe8fe0bfa41c8971ef0d5d49245b (diff)
downloadseaweedfs-bc629665de418ce71c85f83ae618b8f7e5ac42bc.tar.xz
seaweedfs-bc629665de418ce71c85f83ae618b8f7e5ac42bc.zip
fix bug due to data racing on VidMap (#3606)
Diffstat (limited to 'weed/wdclient/vid_map_test.go')
-rw-r--r--weed/wdclient/vid_map_test.go41
1 files changed, 40 insertions, 1 deletions
diff --git a/weed/wdclient/vid_map_test.go b/weed/wdclient/vid_map_test.go
index eae456c9f..980e5bd8c 100644
--- a/weed/wdclient/vid_map_test.go
+++ b/weed/wdclient/vid_map_test.go
@@ -1,15 +1,17 @@
package wdclient
import (
+ "context"
"fmt"
"google.golang.org/grpc"
"strconv"
"sync"
"testing"
+ "time"
)
func TestLocationIndex(t *testing.T) {
- vm := vidMap{}
+ vm := &vidMap{}
// test must be failed
mustFailed := func(length int) {
_, err := vm.getLocationIndex(length)
@@ -132,6 +134,43 @@ func TestLookupFileId(t *testing.T) {
wg.Wait()
}
+func TestConcurrentGetLocations(t *testing.T) {
+ mc := NewMasterClient(grpc.EmptyDialOption{}, "", "", "", "", "", nil)
+ location := Location{Url: "TestDataRacing"}
+ mc.addLocation(1, location)
+
+ ctx, cancel := context.WithCancel(context.Background())
+ wg := sync.WaitGroup{}
+ for i := 0; i < 50; i++ {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ for {
+ select {
+ case <-ctx.Done():
+ return
+ default:
+ _, found := mc.GetLocations(1)
+ if !found {
+ cancel()
+ t.Error("vid map invalid due to data racing. ")
+ return
+ }
+ }
+ }
+ }()
+ }
+
+ //Simulate vidmap reset with cache when leader changes
+ for i := 0; i < 100; i++ {
+ mc.resetVidMap()
+ mc.addLocation(1, location)
+ time.Sleep(1 * time.Microsecond)
+ }
+ cancel()
+ wg.Wait()
+}
+
func BenchmarkLocationIndex(b *testing.B) {
b.SetParallelism(8)
vm := vidMap{