Skip to content

Commit

Permalink
cherry pick #37606 to release-5.3
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
xhebox authored and ti-srebot committed Sep 6, 2022
1 parent 473cae5 commit 447a04f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
28 changes: 17 additions & 11 deletions parser/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,51 +1184,57 @@ func writeSettingItemToBuilder(sb *strings.Builder, item string) {
}
sb.WriteString(item)
}
func writeSettingStringToBuilder(sb *strings.Builder, item string, value string) {
writeSettingItemToBuilder(sb, fmt.Sprintf("%s=\"%s\"", item, strings.ReplaceAll(value, "\"", "\\\"")))
}
func writeSettingIntegerToBuilder(sb *strings.Builder, item string, value uint64) {
writeSettingItemToBuilder(sb, fmt.Sprintf("%s=%d", item, value))
}

func (p *PlacementSettings) String() string {
sb := new(strings.Builder)
if len(p.PrimaryRegion) > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("PRIMARY_REGION=\"%s\"", p.PrimaryRegion))
writeSettingStringToBuilder(sb, "PRIMARY_REGION", p.PrimaryRegion)
}

if len(p.Regions) > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("REGIONS=\"%s\"", p.Regions))
writeSettingStringToBuilder(sb, "REGIONS", p.Regions)
}

if len(p.Schedule) > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("SCHEDULE=\"%s\"", p.Schedule))
writeSettingStringToBuilder(sb, "SCHEDULE", p.Schedule)
}

if len(p.Constraints) > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("CONSTRAINTS=\"%s\"", p.Constraints))
writeSettingStringToBuilder(sb, "CONSTRAINTS", p.Constraints)
}

if len(p.LeaderConstraints) > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("LEADER_CONSTRAINTS=\"%s\"", p.LeaderConstraints))
writeSettingStringToBuilder(sb, "LEADER_CONSTRAINTS", p.LeaderConstraints)
}

if p.Voters > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("VOTERS=%d", p.Voters))
writeSettingIntegerToBuilder(sb, "VOTERS", p.Voters)
}

if len(p.VoterConstraints) > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("VOTER_CONSTRAINTS=\"%s\"", p.VoterConstraints))
writeSettingStringToBuilder(sb, "VOTER_CONSTRAINTS", p.VoterConstraints)
}

if p.Followers > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("FOLLOWERS=%d", p.Followers))
writeSettingIntegerToBuilder(sb, "FOLLOWERS", p.Followers)
}

if len(p.FollowerConstraints) > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("FOLLOWER_CONSTRAINTS=\"%s\"", p.FollowerConstraints))
writeSettingStringToBuilder(sb, "FOLLOWER_CONSTRAINTS", p.FollowerConstraints)
}

if p.Learners > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("LEARNERS=%d", p.Learners))
writeSettingIntegerToBuilder(sb, "LEARNERS", p.Learners)
}

if len(p.LearnerConstraints) > 0 {
writeSettingItemToBuilder(sb, fmt.Sprintf("LEARNER_CONSTRAINTS=\"%s\"", p.LearnerConstraints))
writeSettingStringToBuilder(sb, "LEARNER_CONSTRAINTS", p.LearnerConstraints)
}

return sb.String()
Expand Down
4 changes: 2 additions & 2 deletions parser/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func TestPlacementSettingsString(t *testing.T) {
Voters: 3,
Followers: 2,
Learners: 1,
Constraints: "{+us-east-1:1,+us-east-2:1}",
Constraints: "{\"+us-east-1\":1,+us-east-2:1}",
}
require.Equal(t, "CONSTRAINTS=\"{+us-east-1:1,+us-east-2:1}\" VOTERS=3 FOLLOWERS=2 LEARNERS=1", settings.String())
require.Equal(t, "CONSTRAINTS=\"{\\\"+us-east-1\\\":1,+us-east-2:1}\" VOTERS=3 FOLLOWERS=2 LEARNERS=1", settings.String())
}

0 comments on commit 447a04f

Please sign in to comment.