From 1a9510b7151284debf6f24e450f13d0bab95f78f Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Wed, 7 Sep 2022 15:34:57 +0800 Subject: [PATCH] parser: correctly escape placement options (#37606) (#37644) close pingcap/tidb#37526 --- parser/model/model.go | 28 +++++++++++++++++----------- parser/model/model_test.go | 4 ++-- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/parser/model/model.go b/parser/model/model.go index 8e8f9cd860add..9da3e260264ab 100644 --- a/parser/model/model.go +++ b/parser/model/model.go @@ -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() diff --git a/parser/model/model_test.go b/parser/model/model_test.go index bbefb05b4c38e..7cbcd63a2f5f1 100644 --- a/parser/model/model_test.go +++ b/parser/model/model_test.go @@ -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()) }