Skip to content

Commit

Permalink
checker: fix the conflict between tiflash learner and location labels (
Browse files Browse the repository at this point in the history
…#6660)

close #6662

Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx committed Jul 10, 2023
1 parent 9954576 commit 1f0f401
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/schedule/checker/rule_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (c *RuleChecker) allowLeader(fit *placement.RegionFit, peer *metapb.Peer) b
}

func (c *RuleChecker) fixBetterLocation(region *core.RegionInfo, rf *placement.RuleFit) (*operator.Operator, error) {
if len(rf.Rule.LocationLabels) == 0 || rf.Rule.Count <= 1 {
if len(rf.Rule.LocationLabels) == 0 {
return nil, nil
}

Expand All @@ -403,7 +403,15 @@ func (c *RuleChecker) fixBetterLocation(region *core.RegionInfo, rf *placement.R
if oldStore == 0 {
return nil, nil
}
newStore, filterByTempState := strategy.SelectStoreToImprove(ruleStores, oldStore)
var coLocationStores []*core.StoreInfo
regionStores := c.cluster.GetRegionStores(region)
for _, s := range regionStores {
if placement.MatchLabelConstraints(s, rf.Rule.LabelConstraints) {
coLocationStores = append(coLocationStores, s)
}
}

newStore, filterByTempState := strategy.SelectStoreToImprove(coLocationStores, oldStore)
if newStore == 0 {
log.Debug("no replacement store", zap.Uint64("region-id", region.GetID()))
c.handleFilterState(region, filterByTempState)
Expand Down
91 changes: 91 additions & 0 deletions pkg/schedule/checker/rule_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1332,3 +1332,94 @@ func (suite *ruleCheckerTestSuite) TestPendingList() {
_, exist = suite.rc.pendingList.Get(1)
suite.False(exist)
}

func (suite *ruleCheckerTestSuite) TestLocationLabels() {
suite.cluster.AddLabelsStore(1, 1, map[string]string{"zone": "z1", "rack": "r1", "host": "h1"})
suite.cluster.AddLabelsStore(2, 1, map[string]string{"zone": "z1", "rack": "r1", "host": "h1"})
suite.cluster.AddLabelsStore(3, 1, map[string]string{"zone": "z1", "rack": "r2", "host": "h1"})
suite.cluster.AddLabelsStore(4, 1, map[string]string{"zone": "z1", "rack": "r2", "host": "h1"})
suite.cluster.AddLabelsStore(5, 1, map[string]string{"zone": "z2", "rack": "r3", "host": "h2"})
suite.cluster.AddLabelsStore(6, 1, map[string]string{"zone": "z2", "rack": "r3", "host": "h2"})
suite.cluster.AddLeaderRegionWithRange(1, "", "", 1, 2, 5)
rule1 := &placement.Rule{
GroupID: "pd",
ID: "test1",
Role: placement.Leader,
Count: 1,
LabelConstraints: []placement.LabelConstraint{
{
Key: "zone",
Op: placement.In,
Values: []string{"z1"},
},
},
LocationLabels: []string{"rack"},
}
rule2 := &placement.Rule{
GroupID: "pd",
ID: "test2",
Role: placement.Voter,
Count: 1,
LabelConstraints: []placement.LabelConstraint{
{
Key: "zone",
Op: placement.In,
Values: []string{"z1"},
},
},
LocationLabels: []string{"rack"},
}
rule3 := &placement.Rule{
GroupID: "pd",
ID: "test3",
Role: placement.Voter,
Count: 1,
LabelConstraints: []placement.LabelConstraint{
{
Key: "zone",
Op: placement.In,
Values: []string{"z2"},
},
},
LocationLabels: []string{"rack"},
}
suite.ruleManager.SetRule(rule1)
suite.ruleManager.SetRule(rule2)
suite.ruleManager.SetRule(rule3)
suite.ruleManager.DeleteRule("pd", "default")
op := suite.rc.Check(suite.cluster.GetRegion(1))
suite.NotNil(op)
suite.Equal("move-to-better-location", op.Desc())
}

func (suite *ruleCheckerTestSuite) TestTiFlashLocationLabels() {
suite.cluster.SetEnableUseJointConsensus(true)
suite.cluster.AddLabelsStore(1, 1, map[string]string{"zone": "z1", "rack": "r1", "host": "h1"})
suite.cluster.AddLabelsStore(2, 1, map[string]string{"zone": "z1", "rack": "r1", "host": "h1"})
suite.cluster.AddLabelsStore(3, 1, map[string]string{"zone": "z1", "rack": "r2", "host": "h1"})
suite.cluster.AddLabelsStore(4, 1, map[string]string{"zone": "z1", "rack": "r2", "host": "h1"})
suite.cluster.AddLabelsStore(5, 1, map[string]string{"zone": "z2", "rack": "r3", "host": "h2"})
suite.cluster.AddLabelsStore(6, 1, map[string]string{"zone": "z2", "rack": "r3", "host": "h2"})
suite.cluster.AddLabelsStore(7, 1, map[string]string{"engine": "tiflash"})
suite.cluster.AddRegionWithLearner(1, 1, []uint64{3, 5}, []uint64{7})

rule1 := &placement.Rule{
GroupID: "tiflash",
ID: "test1",
Role: placement.Learner,
Count: 1,
LabelConstraints: []placement.LabelConstraint{
{
Key: "engine",
Op: placement.In,
Values: []string{"tiflash"},
},
},
}
suite.ruleManager.SetRule(rule1)
rule := suite.ruleManager.GetRule("pd", "default")
rule.LocationLabels = []string{"zone", "rack", "host"}
suite.ruleManager.SetRule(rule)
op := suite.rc.Check(suite.cluster.GetRegion(1))
suite.Nil(op)
}

0 comments on commit 1f0f401

Please sign in to comment.