Skip to content

Commit

Permalink
*: fix data race in TestTiFlashGroupIndexWhenStartup (#37371)
Browse files Browse the repository at this point in the history
close #37370
  • Loading branch information
lcwangchao authored Aug 25, 2022
1 parent 29b57e3 commit a43bc8c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
9 changes: 5 additions & 4 deletions ddl/ddl_tiflash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -998,18 +998,19 @@ func TestTiFlashBatchUnsupported(t *testing.T) {

func TestTiFlashGroupIndexWhenStartup(t *testing.T) {
s, teardown := createTiFlashContext(t)
tiflash := s.tiflash
defer teardown()
_ = testkit.NewTestKit(t, s.store)
timeout := time.Now().Add(10 * time.Second)
errMsg := "time out"
for time.Now().Before(timeout) {
time.Sleep(100 * time.Millisecond)
if s.tiflash.GroupIndex != 0 {
if tiflash.GetRuleGroupIndex() != 0 {
errMsg = "invalid group index"
break
}
}
require.Equal(t, placement.RuleIndexTiFlash, s.tiflash.GroupIndex, errMsg)
require.Greater(t, s.tiflash.GroupIndex, placement.RuleIndexTable)
require.Greater(t, s.tiflash.GroupIndex, placement.RuleIndexPartition)
require.Equal(t, placement.RuleIndexTiFlash, tiflash.GetRuleGroupIndex(), errMsg)
require.Greater(t, tiflash.GetRuleGroupIndex(), placement.RuleIndexTable)
require.Greater(t, tiflash.GetRuleGroupIndex(), placement.RuleIndexPartition)
}
20 changes: 17 additions & 3 deletions domain/infosync/tiflash_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func (m *mockTiFlashTableInfo) String() string {
// MockTiFlash mocks a TiFlash, with necessary Pd support.
type MockTiFlash struct {
sync.Mutex
GroupIndex int
groupIndex int
StatusAddr string
StatusServer *httptest.Server
SyncStatus map[int]mockTiFlashTableInfo
Expand Down Expand Up @@ -373,7 +373,7 @@ func NewMockTiFlash() *MockTiFlash {
func (tiflash *MockTiFlash) HandleSetPlacementRule(rule placement.TiFlashRule) error {
tiflash.Lock()
defer tiflash.Unlock()
tiflash.GroupIndex = placement.RuleIndexTiFlash
tiflash.groupIndex = placement.RuleIndexTiFlash
if !tiflash.PdEnabled {
logutil.BgLogger().Info("pd server is manually disabled, just quit")
return nil
Expand Down Expand Up @@ -492,6 +492,20 @@ func (tiflash *MockTiFlash) HandleGetStoresStat() *helper.StoresStat {
}
}

// SetRuleGroupIndex sets the group index of tiflash
func (tiflash *MockTiFlash) SetRuleGroupIndex(groupIndex int) {
tiflash.Lock()
defer tiflash.Unlock()
tiflash.groupIndex = groupIndex
}

// GetRuleGroupIndex gets the group index of tiflash
func (tiflash *MockTiFlash) GetRuleGroupIndex() int {
tiflash.Lock()
defer tiflash.Unlock()
return tiflash.groupIndex
}

// Compare supposed rule, and we actually get from TableInfo
func isRuleMatch(rule placement.TiFlashRule, startKey string, endKey string, count int, labels []string) bool {
// Compute startKey
Expand Down Expand Up @@ -598,7 +612,7 @@ func (m *mockTiFlashPlacementManager) SetTiFlashGroupConfig(_ context.Context) e
if m.tiflash == nil {
return nil
}
m.tiflash.GroupIndex = placement.RuleIndexTiFlash
m.tiflash.SetRuleGroupIndex(placement.RuleIndexTiFlash)
return nil
}

Expand Down

0 comments on commit a43bc8c

Please sign in to comment.