Skip to content

Commit

Permalink
placement: close pingcap#31271
Browse files Browse the repository at this point in the history
Signed-off-by: xhe <[email protected]>
  • Loading branch information
xhebox committed Jan 5, 2022
1 parent ba59021 commit e415f53
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
36 changes: 19 additions & 17 deletions ddl/placement/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,20 @@ func NewBundleFromSugarOptions(options *model.PlacementSettings) (*Bundle, error
}
schedule := options.Schedule

primaryIndex := 0
// regions must include the primary
// but we don't see empty primaryRegion and regions as an error
if primaryRegion != "" || len(regions) > 0 {
sort.Strings(regions)
primaryIndex = sort.SearchStrings(regions, primaryRegion)
if primaryIndex >= len(regions) || regions[primaryIndex] != primaryRegion {
return nil, fmt.Errorf("%w: primary region must be included in regions", ErrInvalidPlacementOptions)
}
var Rules []*Rule

// in case empty primaryRegion and regions, just return an empty bundle
if primaryRegion == "" && len(regions) == 0 {
Rules = append(Rules, NewRule(Voter, followers+1, NewConstraintsDirect()))
return &Bundle{Rules: Rules}, nil
}

var Rules []*Rule
// regions must include the primary
sort.Strings(regions)
primaryIndex := sort.SearchStrings(regions, primaryRegion)
if primaryIndex >= len(regions) || regions[primaryIndex] != primaryRegion {
return nil, fmt.Errorf("%w: primary region must be included in regions", ErrInvalidPlacementOptions)
}

// primaryCount only makes sense when len(regions) > 0
// but we will compute it here anyway for reusing code
Expand All @@ -179,15 +181,15 @@ func NewBundleFromSugarOptions(options *model.PlacementSettings) (*Bundle, error
return nil, fmt.Errorf("%w: unsupported schedule %s", ErrInvalidPlacementOptions, schedule)
}

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

if followers+1 > primaryCount {
// 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...))))
} else {
Rules = append(Rules, NewRule(Follower, followers+1-primaryCount, NewConstraintsDirect()))
}
}

Expand Down
15 changes: 15 additions & 0 deletions ddl/placement/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,21 @@ func (s *testBundleSuite) TestNewBundleFromOptions(c *C) {
},
})

tests = append(tests, TestCase{
name: "sugar syntax: normal case 2",
input: &model.PlacementSettings{
PrimaryRegion: "us",
Regions: "us",
Schedule: "majority_in_primary",
},
output: []*Rule{
NewRule(Voter, 2, NewConstraintsDirect(
NewConstraintDirect("region", In, "us"),
)),
NewRule(Follower, 1, NewConstraintsDirect()),
},
})

tests = append(tests, TestCase{
name: "sugar syntax: few followers",
input: &model.PlacementSettings{
Expand Down

0 comments on commit e415f53

Please sign in to comment.