tikv / pd

Placement driver for TiKV
Apache License 2.0
1.03k stars 714 forks source link

statistics: meet data race when check `IsRegionHot` #8335

Closed lhy1024 closed 4 days ago

lhy1024 commented 4 days ago

Bug Report

What did you do?

go test -timeout 120s -run ^TestSpecialUseHotRegion$ github.com/tikv/pd/pkg/schedule/schedulers -race

What did you expect to see?

no data race

What did you see instead?

==================
WARNING: DATA RACE
Write at 0x00c00003b2f0 by goroutine 210:
  runtime.mapassign_fast64()
      /usr/lib/go/src/runtime/map_fast64.go:93 +0x0
  github.com/tikv/pd/pkg/statistics.(*HotPeerCache).putItem()
      /home/lhy1024/pd/pkg/statistics/hot_peer_cache.go:521 +0x14e
  github.com/tikv/pd/pkg/statistics.(*HotPeerCache).UpdateStat()
      /home/lhy1024/pd/pkg/statistics/hot_peer_cache.go:111 +0x64
  github.com/tikv/pd/pkg/statistics.(*HotCache).Update()
      /home/lhy1024/pd/pkg/statistics/hot_cache.go:202 +0x8c
  github.com/tikv/pd/pkg/mock/mockcluster.(*Cluster).AddLeaderRegionWithWriteInfo()
      /home/lhy1024/pd/pkg/mock/mockcluster/mockcluster.go:552 +0x464
  github.com/tikv/pd/pkg/schedule/schedulers.TestSpecialUseHotRegion()
      /home/lhy1024/pd/pkg/schedule/schedulers/scheduler_test.go:350 +0xa3b
  testing.tRunner()
      /usr/lib/go/src/testing/testing.go:1689 +0x21e
  testing.(*T).Run.gowrap1()
      /usr/lib/go/src/testing/testing.go:1742 +0x44

Previous read at 0x00c00003b2f0 by goroutine 214:
  runtime.mapaccess2_fast64()
      /usr/lib/go/src/runtime/map_fast64.go:53 +0x0
  github.com/tikv/pd/pkg/statistics.(*HotPeerCache).getHotPeerStat()
      /home/lhy1024/pd/pkg/statistics/hot_peer_cache.go:429 +0x57
  github.com/tikv/pd/pkg/statistics.(*HotPeerCache).isRegionHotWithPeer()
      /home/lhy1024/pd/pkg/statistics/hot_peer_cache.go:422 +0x9c
  github.com/tikv/pd/pkg/statistics.(*HotPeerCache).isRegionHotWithAnyPeers()
      /home/lhy1024/pd/pkg/statistics/hot_peer_cache.go:411 +0xd0
  github.com/tikv/pd/pkg/statistics.(*HotCache).IsRegionHot.func1()
      /home/lhy1024/pd/pkg/statistics/hot_cache.go:108 +0x4b
  github.com/tikv/pd/pkg/statistics.(*HotCache).runWriteTask()
      /home/lhy1024/pd/pkg/statistics/hot_cache.go:192 +0x5e
  github.com/tikv/pd/pkg/statistics.(*HotCache).runWriteTask-fm()
      <autogenerated>:1 +0x3d
  github.com/tikv/pd/pkg/statistics.(*HotCache).updateItems()
      /home/lhy1024/pd/pkg/statistics/hot_cache.go:176 +0x87
  github.com/tikv/pd/pkg/statistics.NewHotCache.gowrap2()
      /home/lhy1024/pd/pkg/statistics/hot_cache.go:49 +0x4f

Goroutine 210 (running) created at:
  testing.(*T).Run()
      /usr/lib/go/src/testing/testing.go:1742 +0x825
  testing.runTests.func1()
      /usr/lib/go/src/testing/testing.go:2161 +0x85
  testing.tRunner()
      /usr/lib/go/src/testing/testing.go:1689 +0x21e
  testing.runTests()
      /usr/lib/go/src/testing/testing.go:2159 +0x8be
  testing.(*M).Run()
      /usr/lib/go/src/testing/testing.go:2027 +0xf17
  main.main()
      _testmain.go:227 +0x2bd

Goroutine 214 (running) created at:
  github.com/tikv/pd/pkg/statistics.NewHotCache()
      /home/lhy1024/pd/pkg/statistics/hot_cache.go:49 +0x444
  github.com/tikv/pd/pkg/statistics.NewHotStat()
      /home/lhy1024/pd/pkg/statistics/hot_stat.go:33 +0x34
  github.com/tikv/pd/pkg/mock/mockcluster.NewCluster()
      /home/lhy1024/pd/pkg/mock/mockcluster/mockcluster.go:70 +0x1a8
  github.com/tikv/pd/pkg/schedule/schedulers.prepareSchedulersTest()
      /home/lhy1024/pd/pkg/schedule/schedulers/scheduler_test.go:42 +0x91
  github.com/tikv/pd/pkg/schedule/schedulers.TestSpecialUseHotRegion()
      /home/lhy1024/pd/pkg/schedule/schedulers/scheduler_test.go:313 +0x73
  testing.tRunner()
      /usr/lib/go/src/testing/testing.go:1689 +0x21e
  testing.(*T).Run.gowrap1()
      /usr/lib/go/src/testing/testing.go:1742 +0x44
==================
--- FAIL: TestSpecialUseHotRegion (0.01s)
    testing.go:1398: race detected during execution of test
FAIL

What version of PD are you using (pd-server -V)?

lhy1024 commented 4 days ago

it is introduced by #8164