Skip to content

Commit

Permalink
placement: refine sugar syntax rules generation (#37565)
Browse files Browse the repository at this point in the history
ref #37251
  • Loading branch information
xhebox authored Sep 7, 2022
1 parent 62480cf commit 8670731
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
12 changes: 7 additions & 5 deletions ddl/placement/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,17 @@ func NewBundleFromSugarOptions(options *model.PlacementSettings) (*Bundle, error
return nil, fmt.Errorf("%w: unsupported schedule %s", ErrInvalidPlacementOptions, schedule)
}

rules = append(rules, NewRule(Voter, primaryCount, NewConstraintsDirect(NewConstraintDirect("region", In, primaryRegion))))
if followers+1 > primaryCount {
rules = append(rules, NewRule(Leader, 1, NewConstraintsDirect(NewConstraintDirect("region", In, primaryRegion))))
if primaryCount > 1 {
rules = append(rules, NewRule(Voter, primaryCount-1, NewConstraintsDirect(NewConstraintDirect("region", In, primaryRegion))))
}
if cnt := followers + 1 - primaryCount; cnt > 0 {
// delete primary from regions
regions = regions[:primaryIndex+copy(regions[primaryIndex:], regions[primaryIndex+1:])]

if len(regions) > 0 {
rules = append(rules, NewRule(Follower, followers+1-primaryCount, NewConstraintsDirect(NewConstraintDirect("region", In, regions...))))
rules = append(rules, NewRule(Voter, cnt, NewConstraintsDirect(NewConstraintDirect("region", In, regions...))))
} else {
rules = append(rules, NewRule(Follower, followers+1-primaryCount, NewConstraintsDirect()))
rules = append(rules, NewRule(Voter, cnt, NewConstraintsDirect()))
}
}

Expand Down
30 changes: 21 additions & 9 deletions ddl/placement/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,10 @@ func TestNewBundleFromOptions(t *testing.T) {
Regions: "us",
},
output: []*Rule{
NewRule(Voter, 3, NewConstraintsDirect(
NewRule(Leader, 1, NewConstraintsDirect(
NewConstraintDirect("region", In, "us"),
)),
NewRule(Voter, 2, NewConstraintsDirect(
NewConstraintDirect("region", In, "us"),
)),
},
Expand All @@ -419,10 +422,13 @@ func TestNewBundleFromOptions(t *testing.T) {
Schedule: "majority_in_primary",
},
output: []*Rule{
NewRule(Voter, 2, NewConstraintsDirect(
NewRule(Leader, 1, NewConstraintsDirect(
NewConstraintDirect("region", In, "us"),
)),
NewRule(Follower, 1, NewConstraintsDirect()),
NewRule(Voter, 1, NewConstraintsDirect(
NewConstraintDirect("region", In, "us"),
)),
NewRule(Voter, 1, NewConstraintsDirect()),
},
})

Expand All @@ -434,10 +440,10 @@ func TestNewBundleFromOptions(t *testing.T) {
Followers: 1,
},
output: []*Rule{
NewRule(Voter, 1, NewConstraintsDirect(
NewRule(Leader, 1, NewConstraintsDirect(
NewConstraintDirect("region", In, "us"),
)),
NewRule(Follower, 1, NewConstraintsDirect(
NewRule(Voter, 1, NewConstraintsDirect(
NewConstraintDirect("region", In, "bj", "sh"),
)),
},
Expand Down Expand Up @@ -510,10 +516,13 @@ func TestNewBundleFromOptions(t *testing.T) {
Followers: 5,
},
output: []*Rule{
NewRule(Voter, 3, NewConstraintsDirect(
NewRule(Leader, 1, NewConstraintsDirect(
NewConstraintDirect("region", In, "us"),
)),
NewRule(Voter, 2, NewConstraintsDirect(
NewConstraintDirect("region", In, "us"),
)),
NewRule(Follower, 3, NewConstraintsDirect(
NewRule(Voter, 3, NewConstraintsDirect(
NewConstraintDirect("region", In, "sh"),
)),
},
Expand All @@ -531,10 +540,13 @@ func TestNewBundleFromOptions(t *testing.T) {
Schedule: "majority_in_primary",
},
output: []*Rule{
NewRule(Voter, 3, NewConstraintsDirect(
NewRule(Leader, 1, NewConstraintsDirect(
NewConstraintDirect("region", In, "sh"),
)),
NewRule(Voter, 2, NewConstraintsDirect(
NewConstraintDirect("region", In, "sh"),
)),
NewRule(Follower, 2, NewConstraintsDirect(
NewRule(Voter, 2, NewConstraintsDirect(
NewConstraintDirect("region", In, "bj"),
)),
},
Expand Down

0 comments on commit 8670731

Please sign in to comment.