Skip to content

Commit

Permalink
Use consts more
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <ghzpotato@gmail.com>
  • Loading branch information
JmPotato committed Nov 17, 2023
1 parent 91a633c commit 6cef656
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 92 deletions.
70 changes: 35 additions & 35 deletions pkg/schedule/placement/rule_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ func TestAdjustRule(t *testing.T) {
re := require.New(t)
_, manager := newTestManager(t, false)
rules := []Rule{
{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: "voter", Count: 3},
{GroupID: "", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: "voter", Count: 3},
{GroupID: "group", ID: "", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: "voter", Count: 3},
{GroupID: "group", ID: "id", StartKeyHex: "123ab", EndKeyHex: "123abf", Role: "voter", Count: 3},
{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "1123abf", Role: "voter", Count: 3},
{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123aaa", Role: "voter", Count: 3},
{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: Voter, Count: 3},
{GroupID: "", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: Voter, Count: 3},
{GroupID: "group", ID: "", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: Voter, Count: 3},
{GroupID: "group", ID: "id", StartKeyHex: "123ab", EndKeyHex: "123abf", Role: Voter, Count: 3},
{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "1123abf", Role: Voter, Count: 3},
{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123aaa", Role: Voter, Count: 3},
{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: "master", Count: 3},
{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: "voter", Count: 0},
{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: "voter", Count: -1},
{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: "voter", Count: 3, LabelConstraints: []LabelConstraint{{Op: "foo"}}},
{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: Voter, Count: 0},
{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: Voter, Count: -1},
{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: Voter, Count: 3, LabelConstraints: []LabelConstraint{{Op: "foo"}}},
}
re.NoError(manager.adjustRule(&rules[0], "group"))

Expand All @@ -101,17 +101,17 @@ func TestAdjustRule(t *testing.T) {
}

manager.SetKeyType(constant.Table.String())
re.Error(manager.adjustRule(&Rule{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: "voter", Count: 3}, "group"))
re.Error(manager.adjustRule(&Rule{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: Voter, Count: 3}, "group"))

manager.SetKeyType(constant.Txn.String())
re.Error(manager.adjustRule(&Rule{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: "voter", Count: 3}, "group"))
re.Error(manager.adjustRule(&Rule{GroupID: "group", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: Voter, Count: 3}, "group"))

re.Error(manager.adjustRule(&Rule{
GroupID: "group",
ID: "id",
StartKeyHex: hex.EncodeToString(codec.EncodeBytes([]byte{0})),
EndKeyHex: "123abf",
Role: "voter",
Role: Voter,
Count: 3,
}, "group"))

Expand All @@ -120,7 +120,7 @@ func TestAdjustRule(t *testing.T) {
ID: "id",
StartKeyHex: hex.EncodeToString(codec.EncodeBytes([]byte{0})),
EndKeyHex: hex.EncodeToString(codec.EncodeBytes([]byte{1})),
Role: "learner",
Role: Learner,
Count: 1,
IsWitness: true,
LabelConstraints: []LabelConstraint{{Key: "engine", Op: "in", Values: []string{"tiflash"}}},
Expand All @@ -130,15 +130,15 @@ func TestAdjustRule(t *testing.T) {
func TestLeaderCheck(t *testing.T) {
re := require.New(t)
_, manager := newTestManager(t, false)
re.Regexp(".*needs at least one leader or voter.*", manager.SetRule(&Rule{GroupID: DefaultGroupID, ID: DefaultRuleID, Role: "learner", Count: 3}).Error())
re.Regexp(".*define multiple leaders by count 2.*", manager.SetRule(&Rule{GroupID: "g2", ID: "33", Role: "leader", Count: 2}).Error())
re.Regexp(".*needs at least one leader or voter.*", manager.SetRule(&Rule{GroupID: DefaultGroupID, ID: DefaultRuleID, Role: Learner, Count: 3}).Error())
re.Regexp(".*define multiple leaders by count 2.*", manager.SetRule(&Rule{GroupID: "g2", ID: "33", Role: Leader, Count: 2}).Error())
re.Regexp(".*multiple leader replicas.*", manager.Batch([]RuleOp{
{
Rule: &Rule{GroupID: "g2", ID: "foo1", Role: "leader", Count: 1},
Rule: &Rule{GroupID: "g2", ID: "foo1", Role: Leader, Count: 1},
Action: RuleOpAdd,
},
{
Rule: &Rule{GroupID: "g2", ID: "foo2", Role: "leader", Count: 1},
Rule: &Rule{GroupID: "g2", ID: "foo2", Role: Leader, Count: 1},
Action: RuleOpAdd,
},
}).Error())
Expand All @@ -148,9 +148,9 @@ func TestSaveLoad(t *testing.T) {
re := require.New(t)
store, manager := newTestManager(t, false)
rules := []*Rule{
{GroupID: DefaultGroupID, ID: DefaultRuleID, Role: "voter", Count: 5},
{GroupID: "foo", ID: "baz", StartKeyHex: "", EndKeyHex: "abcd", Role: "voter", Count: 1},
{GroupID: "foo", ID: "bar", Role: "learner", Count: 1},
{GroupID: DefaultGroupID, ID: DefaultRuleID, Role: Voter, Count: 5},
{GroupID: "foo", ID: "baz", StartKeyHex: "", EndKeyHex: "abcd", Role: Voter, Count: 1},
{GroupID: "foo", ID: "bar", Role: Learner, Count: 1},
}
for _, r := range rules {
re.NoError(manager.SetRule(r.Clone()))
Expand Down Expand Up @@ -193,9 +193,9 @@ func TestKeys(t *testing.T) {
re := require.New(t)
_, manager := newTestManager(t, false)
rules := []*Rule{
{GroupID: "1", ID: "1", Role: "voter", Count: 1, StartKeyHex: "", EndKeyHex: ""},
{GroupID: "2", ID: "2", Role: "voter", Count: 1, StartKeyHex: "11", EndKeyHex: "ff"},
{GroupID: "2", ID: "3", Role: "voter", Count: 1, StartKeyHex: "22", EndKeyHex: "dd"},
{GroupID: "1", ID: "1", Role: Voter, Count: 1, StartKeyHex: "", EndKeyHex: ""},
{GroupID: "2", ID: "2", Role: Voter, Count: 1, StartKeyHex: "11", EndKeyHex: "ff"},
{GroupID: "2", ID: "3", Role: Voter, Count: 1, StartKeyHex: "22", EndKeyHex: "dd"},
}

toDelete := []RuleOp{}
Expand All @@ -211,8 +211,8 @@ func TestKeys(t *testing.T) {
manager.Batch(toDelete)
checkRules(t, manager.GetAllRules(), [][2]string{{DefaultGroupID, DefaultRuleID}})

rules = append(rules, &Rule{GroupID: "3", ID: "4", Role: "voter", Count: 1, StartKeyHex: "44", EndKeyHex: "ee"},
&Rule{GroupID: "3", ID: "5", Role: "voter", Count: 1, StartKeyHex: "44", EndKeyHex: "dd"})
rules = append(rules, &Rule{GroupID: "3", ID: "4", Role: Voter, Count: 1, StartKeyHex: "44", EndKeyHex: "ee"},
&Rule{GroupID: "3", ID: "5", Role: Voter, Count: 1, StartKeyHex: "44", EndKeyHex: "dd"})
manager.SetRules(rules)
checkRules(t, manager.GetAllRules(), [][2]string{{"1", "1"}, {"2", "2"}, {"2", "3"}, {"3", "4"}, {"3", "5"}, {DefaultGroupID, DefaultRuleID}})

Expand Down Expand Up @@ -282,10 +282,10 @@ func TestKeys(t *testing.T) {
func TestDeleteByIDPrefix(t *testing.T) {
_, manager := newTestManager(t, false)
manager.SetRules([]*Rule{
{GroupID: "g1", ID: "foo1", Role: "voter", Count: 1},
{GroupID: "g2", ID: "foo1", Role: "voter", Count: 1},
{GroupID: "g2", ID: "foobar", Role: "voter", Count: 1},
{GroupID: "g2", ID: "baz2", Role: "voter", Count: 1},
{GroupID: "g1", ID: "foo1", Role: Voter, Count: 1},
{GroupID: "g2", ID: "foo1", Role: Voter, Count: 1},
{GroupID: "g2", ID: "foobar", Role: Voter, Count: 1},
{GroupID: "g2", ID: "baz2", Role: Voter, Count: 1},
})
manager.DeleteRule(DefaultGroupID, DefaultRuleID)
checkRules(t, manager.GetAllRules(), [][2]string{{"g1", "foo1"}, {"g2", "baz2"}, {"g2", "foo1"}, {"g2", "foobar"}})
Expand All @@ -304,20 +304,20 @@ func TestRangeGap(t *testing.T) {
err := manager.DeleteRule(DefaultGroupID, DefaultRuleID)
re.Error(err)

err = manager.SetRule(&Rule{GroupID: DefaultGroupID, ID: "foo", StartKeyHex: "", EndKeyHex: "abcd", Role: "voter", Count: 1})
err = manager.SetRule(&Rule{GroupID: DefaultGroupID, ID: "foo", StartKeyHex: "", EndKeyHex: "abcd", Role: Voter, Count: 1})
re.NoError(err)
// |-- default --|
// |-- foo --|
// still cannot delete default since it will cause ("abcd", "") has no rules inside.
err = manager.DeleteRule(DefaultGroupID, DefaultRuleID)
re.Error(err)
err = manager.SetRule(&Rule{GroupID: DefaultGroupID, ID: "bar", StartKeyHex: "abcd", EndKeyHex: "", Role: "voter", Count: 1})
err = manager.SetRule(&Rule{GroupID: DefaultGroupID, ID: "bar", StartKeyHex: "abcd", EndKeyHex: "", Role: Voter, Count: 1})
re.NoError(err)
// now default can be deleted.
err = manager.DeleteRule(DefaultGroupID, DefaultRuleID)
re.NoError(err)
// cannot change range since it will cause ("abaa", "abcd") has no rules inside.
err = manager.SetRule(&Rule{GroupID: DefaultGroupID, ID: "foo", StartKeyHex: "", EndKeyHex: "abaa", Role: "voter", Count: 1})
err = manager.SetRule(&Rule{GroupID: DefaultGroupID, ID: "foo", StartKeyHex: "", EndKeyHex: "abaa", Role: Voter, Count: 1})
re.Error(err)
}

Expand All @@ -334,7 +334,7 @@ func TestGroupConfig(t *testing.T) {
re.Equal(pd2, manager.GetRuleGroup(DefaultGroupID))

// new group g without config
err = manager.SetRule(&Rule{GroupID: "g", ID: "1", Role: "voter", Count: 1})
err = manager.SetRule(&Rule{GroupID: "g", ID: "1", Role: Voter, Count: 1})
re.NoError(err)
g1 := &RuleGroup{ID: "g"}
re.Equal(g1, manager.GetRuleGroup("g"))
Expand Down Expand Up @@ -363,13 +363,13 @@ func TestRuleVersion(t *testing.T) {
rule1 := manager.GetRule(DefaultGroupID, DefaultRuleID)
re.Equal(uint64(0), rule1.Version)
// create new rule
newRule := &Rule{GroupID: "g1", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: "voter", Count: 3}
newRule := &Rule{GroupID: "g1", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: Voter, Count: 3}
err := manager.SetRule(newRule)
re.NoError(err)
newRule = manager.GetRule("g1", "id")
re.Equal(uint64(0), newRule.Version)
// update rule
newRule = &Rule{GroupID: "g1", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: "voter", Count: 2}
newRule = &Rule{GroupID: "g1", ID: "id", StartKeyHex: "123abc", EndKeyHex: "123abf", Role: Voter, Count: 2}
err = manager.SetRule(newRule)
re.NoError(err)
newRule = manager.GetRule("g1", "id")
Expand Down
4 changes: 2 additions & 2 deletions pkg/schedule/placement/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestBuildRuleList(t *testing.T) {
defaultRule := &Rule{
GroupID: DefaultGroupID,
ID: DefaultRuleID,
Role: "voter",
Role: Voter,
StartKey: []byte{},
EndKey: []byte{},
Count: 3,
Expand All @@ -128,7 +128,7 @@ func TestBuildRuleList(t *testing.T) {
Override: true,
StartKey: byteStart,
EndKey: byteEnd,
Role: "voter",
Role: Voter,
Count: 5,
}

Expand Down
6 changes: 3 additions & 3 deletions server/api/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ func (suite *regionsReplicatedTestSuite) TestCheckRegionsReplicated() {
Index: 5,
Rules: []*placement.Rule{
{
ID: "foo", Index: 1, Role: "voter", Count: 1,
ID: "foo", Index: 1, Role: placement.Voter, Count: 1,
},
},
},
Expand Down Expand Up @@ -738,7 +738,7 @@ func (suite *regionsReplicatedTestSuite) TestCheckRegionsReplicated() {
mustRegionHeartbeat(re, suite.svr, r1)

bundle[0].Rules = append(bundle[0].Rules, &placement.Rule{
ID: "bar", Index: 1, Role: "voter", Count: 1,
ID: "bar", Index: 1, Role: placement.Voter, Count: 1,
})
data, err = json.Marshal(bundle)
suite.NoError(err)
Expand All @@ -755,7 +755,7 @@ func (suite *regionsReplicatedTestSuite) TestCheckRegionsReplicated() {
Index: 6,
Rules: []*placement.Rule{
{
ID: "foo", Index: 1, Role: "voter", Count: 2,
ID: "foo", Index: 1, Role: placement.Voter, Count: 2,
},
},
})
Expand Down
12 changes: 6 additions & 6 deletions server/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1669,23 +1669,23 @@ func TestCalculateStoreSize1(t *testing.T) {
}

cluster.ruleManager.SetRule(
&placement.Rule{GroupID: placement.DefaultGroupID, ID: "zone1", StartKey: []byte(""), EndKey: []byte(""), Role: "voter", Count: 2,
&placement.Rule{GroupID: placement.DefaultGroupID, ID: "zone1", StartKey: []byte(""), EndKey: []byte(""), Role: placement.Voter, Count: 2,
LabelConstraints: []placement.LabelConstraint{
{Key: "zone", Op: "in", Values: []string{"zone1"}},
},
LocationLabels: []string{"rack", "host"}},
)

cluster.ruleManager.SetRule(
&placement.Rule{GroupID: placement.DefaultGroupID, ID: "zone2", StartKey: []byte(""), EndKey: []byte(""), Role: "voter", Count: 2,
&placement.Rule{GroupID: placement.DefaultGroupID, ID: "zone2", StartKey: []byte(""), EndKey: []byte(""), Role: placement.Voter, Count: 2,
LabelConstraints: []placement.LabelConstraint{
{Key: "zone", Op: "in", Values: []string{"zone2"}},
},
LocationLabels: []string{"rack", "host"}},
)

cluster.ruleManager.SetRule(
&placement.Rule{GroupID: placement.DefaultGroupID, ID: "zone3", StartKey: []byte(""), EndKey: []byte(""), Role: "follower", Count: 1,
&placement.Rule{GroupID: placement.DefaultGroupID, ID: "zone3", StartKey: []byte(""), EndKey: []byte(""), Role: placement.Follower, Count: 1,
LabelConstraints: []placement.LabelConstraint{
{Key: "zone", Op: "in", Values: []string{"zone3"}},
},
Expand Down Expand Up @@ -1753,23 +1753,23 @@ func TestCalculateStoreSize2(t *testing.T) {
}

cluster.ruleManager.SetRule(
&placement.Rule{GroupID: placement.DefaultGroupID, ID: "dc1", StartKey: []byte(""), EndKey: []byte(""), Role: "voter", Count: 2,
&placement.Rule{GroupID: placement.DefaultGroupID, ID: "dc1", StartKey: []byte(""), EndKey: []byte(""), Role: placement.Voter, Count: 2,
LabelConstraints: []placement.LabelConstraint{
{Key: "dc", Op: "in", Values: []string{"dc1"}},
},
LocationLabels: []string{"dc", "logic", "rack", "host"}},
)

cluster.ruleManager.SetRule(
&placement.Rule{GroupID: placement.DefaultGroupID, ID: "logic3", StartKey: []byte(""), EndKey: []byte(""), Role: "voter", Count: 1,
&placement.Rule{GroupID: placement.DefaultGroupID, ID: "logic3", StartKey: []byte(""), EndKey: []byte(""), Role: placement.Voter, Count: 1,
LabelConstraints: []placement.LabelConstraint{
{Key: "logic", Op: "in", Values: []string{"logic3"}},
},
LocationLabels: []string{"dc", "logic", "rack", "host"}},
)

cluster.ruleManager.SetRule(
&placement.Rule{GroupID: placement.DefaultGroupID, ID: "logic4", StartKey: []byte(""), EndKey: []byte(""), Role: "learner", Count: 1,
&placement.Rule{GroupID: placement.DefaultGroupID, ID: "logic4", StartKey: []byte(""), EndKey: []byte(""), Role: placement.Learner, Count: 1,
LabelConstraints: []placement.LabelConstraint{
{Key: "logic", Op: "in", Values: []string{"logic4"}},
},
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/mcs/scheduling/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (suite *apiTestSuite) TestAPIForward() {
{
GroupID: placement.DefaultGroupID,
ID: placement.DefaultRuleID,
Role: "voter",
Role: placement.Voter,
Count: 3,
LocationLabels: []string{},
},
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/mcs/scheduling/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (suite *ruleTestSuite) TestRuleWatch() {
rule := &placement.Rule{
GroupID: "2",
ID: "3",
Role: "voter",
Role: placement.Voter,
Count: 1,
StartKeyHex: "22",
EndKeyHex: "dd",
Expand Down
Loading

0 comments on commit 6cef656

Please sign in to comment.