diff --git a/ddl/placement/bundle.go b/ddl/placement/bundle.go index 83e2eff7b91e8..d8f24f08583f5 100644 --- a/ddl/placement/bundle.go +++ b/ddl/placement/bundle.go @@ -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())) } } diff --git a/ddl/placement/bundle_test.go b/ddl/placement/bundle_test.go index b59f349609f87..a219fc0589789 100644 --- a/ddl/placement/bundle_test.go +++ b/ddl/placement/bundle_test.go @@ -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"), )), }, @@ -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()), }, }) @@ -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"), )), }, @@ -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"), )), }, @@ -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"), )), },