Skip to content

Commit

Permalink
*: fix data races in from MockTiFlash (pingcap#31836)
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvinNeo authored Jan 25, 2022
1 parent f868024 commit 3c797fd
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 65 deletions.
80 changes: 20 additions & 60 deletions ddl/ddl_tiflash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,50 +96,15 @@ func (s *tiflashDDLTestSuite) SetUpSuite(c *C) {
}

func (s *tiflashDDLTestSuite) TearDownSuite(c *C) {
s.tiflash.Lock()
s.tiflash.StatusServer.Close()
s.tiflash.Unlock()
s.dom.Close()
err := s.store.Close()
c.Assert(err, IsNil)
ddl.PollTiFlashInterval = 2 * time.Second
}

// 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
if rule.StartKeyHex == startKey && rule.EndKeyHex == endKey {
ok := false
for _, c := range rule.Constraints {
if c.Key == "engine" && len(c.Values) == 1 && c.Values[0] == "tiflash" && c.Op == placement.In {
ok = true
break
}
}
if !ok {
return false
}

if len(rule.LocationLabels) == len(labels) {
for i, lb := range labels {
if lb != rule.LocationLabels[i] {
return false
}
}
} else {
return false
}

if rule.Count != count {
return false
}
if rule.Role != placement.Learner {
return false
}
} else {
return false
}
return true
}

func ChangeGCSafePoint(tk *testkit.TestKit, t time.Time, enable string, lifeTime string) {
gcTimeFormat := "20060102-15:04:05 -0700 MST"
lastSafePoint := t.Format(gcTimeFormat)
Expand All @@ -161,12 +126,7 @@ func ChangeGCSafePoint(tk *testkit.TestKit, t time.Time, enable string, lifeTime
}

func (s *tiflashDDLTestSuite) CheckPlacementRule(rule placement.TiFlashRule) bool {
for _, r := range s.tiflash.GlobalTiFlashPlacementRules {
if isRuleMatch(rule, r.StartKeyHex, r.EndKeyHex, r.Count, r.LocationLabels) {
return true
}
}
return false
return s.tiflash.CheckPlacementRule(rule)
}

func (s *tiflashDDLTestSuite) CheckFlashback(tk *testkit.TestKit, c *C) {
Expand All @@ -190,12 +150,12 @@ func (s *tiflashDDLTestSuite) CheckFlashback(tk *testkit.TestKit, c *C) {
if tb.Meta().Partition != nil {
for _, e := range tb.Meta().Partition.Definitions {
ruleName := fmt.Sprintf("table-%v-r", e.ID)
_, ok := s.tiflash.GlobalTiFlashPlacementRules[ruleName]
_, ok := s.tiflash.GetPlacementRule(ruleName)
c.Assert(ok, Equals, true)
}
} else {
ruleName := fmt.Sprintf("table-%v-r", tb.Meta().ID)
_, ok := s.tiflash.GlobalTiFlashPlacementRules[ruleName]
_, ok := s.tiflash.GetPlacementRule(ruleName)
c.Assert(ok, Equals, true)
}
}
Expand Down Expand Up @@ -247,64 +207,64 @@ func (s *tiflashDDLTestSuite) TestTiFlashNoRedundantPDRules(c *C) {
defer fCancelPD()

// Clean all rules
s.tiflash.GlobalTiFlashPlacementRules = make(map[string]placement.TiFlashRule)
s.tiflash.CleanPlacementRules()
tk.MustExec("use test")
tk.MustExec("drop table if exists ddltiflash")
tk.MustExec("drop table if exists ddltiflashp")
tk.MustExec("create table ddltiflash(z int)")
tk.MustExec("create table ddltiflashp(z int) PARTITION BY RANGE(z) (PARTITION p0 VALUES LESS THAN (10),PARTITION p1 VALUES LESS THAN (20), PARTITION p2 VALUES LESS THAN (30))")

total := 0
c.Assert(len(s.tiflash.GlobalTiFlashPlacementRules), Equals, total)
c.Assert(s.tiflash.PlacementRulesLen(), Equals, total)

tk.MustExec("alter table ddltiflash set tiflash replica 1")
total += 1
time.Sleep(ddl.PollTiFlashInterval * RoundToBeAvailable)
c.Assert(len(s.tiflash.GlobalTiFlashPlacementRules), Equals, total)
c.Assert(s.tiflash.PlacementRulesLen(), Equals, total)

tk.MustExec("alter table ddltiflashp set tiflash replica 1")
total += 3
time.Sleep(ddl.PollTiFlashInterval * RoundToBeAvailablePartitionTable)
c.Assert(len(s.tiflash.GlobalTiFlashPlacementRules), Equals, total)
c.Assert(s.tiflash.PlacementRulesLen(), Equals, total)

lessThan := 40
tk.MustExec(fmt.Sprintf("ALTER TABLE ddltiflashp ADD PARTITION (PARTITION pn VALUES LESS THAN (%v))", lessThan))
total += 1
time.Sleep(ddl.PollTiFlashInterval * RoundToBeAvailablePartitionTable)
c.Assert(len(s.tiflash.GlobalTiFlashPlacementRules), Equals, total)
c.Assert(s.tiflash.PlacementRulesLen(), Equals, total)

tk.MustExec("alter table ddltiflashp truncate partition p1")
total += 1
time.Sleep(ddl.PollTiFlashInterval * RoundToBeAvailablePartitionTable)
c.Assert(len(s.tiflash.GlobalTiFlashPlacementRules), Equals, total)
c.Assert(s.tiflash.PlacementRulesLen(), Equals, total)

// Now gc will trigger, and will remove dropped partition.
c.Assert(gcWorker.DeleteRanges(context.TODO(), math.MaxInt64), IsNil)
total -= 1
time.Sleep(ddl.PollTiFlashInterval * RoundToBeAvailablePartitionTable)
c.Assert(len(s.tiflash.GlobalTiFlashPlacementRules), Equals, total)
c.Assert(s.tiflash.PlacementRulesLen(), Equals, total)

tk.MustExec("alter table ddltiflashp drop partition p2")
c.Assert(gcWorker.DeleteRanges(context.TODO(), math.MaxInt64), IsNil)
total -= 1
time.Sleep(ddl.PollTiFlashInterval * RoundToBeAvailablePartitionTable)
c.Assert(len(s.tiflash.GlobalTiFlashPlacementRules), Equals, total)
c.Assert(s.tiflash.PlacementRulesLen(), Equals, total)

tk.MustExec("truncate table ddltiflash")
total += 1
time.Sleep(ddl.PollTiFlashInterval * RoundToBeAvailablePartitionTable)
c.Assert(len(s.tiflash.GlobalTiFlashPlacementRules), Equals, total)
c.Assert(s.tiflash.PlacementRulesLen(), Equals, total)

c.Assert(gcWorker.DeleteRanges(context.TODO(), math.MaxInt64), IsNil)
total -= 1
time.Sleep(ddl.PollTiFlashInterval * RoundToBeAvailablePartitionTable)
c.Assert(len(s.tiflash.GlobalTiFlashPlacementRules), Equals, total)
c.Assert(s.tiflash.PlacementRulesLen(), Equals, total)

tk.MustExec("drop table ddltiflash")
total -= 1
time.Sleep(ddl.PollTiFlashInterval * RoundToBeAvailablePartitionTable)
c.Assert(gcWorker.DeleteRanges(context.TODO(), math.MaxInt64), IsNil)
c.Assert(len(s.tiflash.GlobalTiFlashPlacementRules), Equals, total)
c.Assert(s.tiflash.PlacementRulesLen(), Equals, total)
}

func (s *tiflashDDLTestSuite) TestTiFlashReplicaPartitionTableNormal(c *C) {
Expand Down Expand Up @@ -335,7 +295,7 @@ func (s *tiflashDDLTestSuite) TestTiFlashReplicaPartitionTableNormal(c *C) {
for _, p := range pi.Definitions {
c.Assert(tb2.Meta().TiFlashReplica.IsPartitionAvailable(p.ID), Equals, true)
if len(p.LessThan) == 1 && p.LessThan[0] == lessThan {
table, ok := s.tiflash.SyncStatus[int(p.ID)]
table, ok := s.tiflash.GetTableSyncStatus(int(p.ID))
c.Assert(ok, Equals, true)
c.Assert(table.Accel, Equals, true)
}
Expand Down Expand Up @@ -373,7 +333,7 @@ func (s *tiflashDDLTestSuite) TestTiFlashReplicaPartitionTableBlock(c *C) {
for _, p := range pi.Definitions {
c.Assert(tb.Meta().TiFlashReplica.IsPartitionAvailable(p.ID), Equals, true)
if len(p.LessThan) == 1 && p.LessThan[0] == lessThan {
table, ok := s.tiflash.SyncStatus[int(p.ID)]
table, ok := s.tiflash.GetTableSyncStatus(int(p.ID))
c.Assert(ok, Equals, true)
c.Assert(table.Accel, Equals, true)
}
Expand Down Expand Up @@ -589,9 +549,9 @@ func (s *tiflashDDLTestSuite) TestSetPlacementRuleFail(c *C) {
tk.MustExec("use test")
tk.MustExec("drop table if exists ddltiflash")
tk.MustExec("create table ddltiflash(z int)")
s.tiflash.PdEnabled = false
s.tiflash.PdSwitch(false)
defer func() {
s.tiflash.PdEnabled = true
s.tiflash.PdSwitch(true)
}()
tk.MustExec("alter table ddltiflash set tiflash replica 1")
tb, err := s.dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("ddltiflash"))
Expand Down
Loading

0 comments on commit 3c797fd

Please sign in to comment.