Skip to content

Commit

Permalink
Only keep the integration test
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <ghzpotato@gmail.com>
  • Loading branch information
JmPotato committed Oct 17, 2023
1 parent 159ff37 commit 11dbbb2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 175 deletions.
7 changes: 7 additions & 0 deletions pkg/mcs/scheduling/server/rule/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ func (rw *Watcher) initializeGroupWatcher() error {
if err != nil {
return err
}
// Add all rule key ranges within the group to the suspect key ranges.
for _, rule := range rm.GetRulesByGroup(ruleGroup.ID) {
rw.getCheckerController().AddSuspectKeyRange(rule.StartKey, rule.EndKey)
}
return rm.SetRuleGroup(ruleGroup)
}
deleteFn := func(kv *mvccpb.KeyValue) error {
Expand All @@ -183,6 +187,9 @@ func (rw *Watcher) initializeGroupWatcher() error {
if rm == nil {
return rw.ruleStorage.DeleteRuleGroup(trimmedKey)
}
for _, rule := range rm.GetRulesByGroup(trimmedKey) {
rw.getCheckerController().AddSuspectKeyRange(rule.StartKey, rule.EndKey)
}
return rm.DeleteRuleGroup(trimmedKey)
}
// Trigger the rule manager to reload the rule groups.
Expand Down
175 changes: 0 additions & 175 deletions tests/integrations/mcs/scheduling/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ import (
"sort"
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/tikv/pd/pkg/keyspace"
"github.com/tikv/pd/pkg/mcs/scheduling/server/rule"
"github.com/tikv/pd/pkg/mcs/utils"
"github.com/tikv/pd/pkg/schedule/labeler"
"github.com/tikv/pd/pkg/schedule/placement"
"github.com/tikv/pd/pkg/storage/endpoint"
"github.com/tikv/pd/pkg/storage/kv"
"github.com/tikv/pd/pkg/utils/testutil"
"github.com/tikv/pd/tests"
)
Expand Down Expand Up @@ -69,178 +63,9 @@ func (suite *ruleTestSuite) TearDownSuite() {
suite.cluster.Destroy()
}

func loadRules(re *require.Assertions, ruleStorage endpoint.RuleStorage) (rules []*placement.Rule) {
err := ruleStorage.LoadRules(func(_, v string) {
r, err := placement.NewRuleFromJSON([]byte(v))
re.NoError(err)
rules = append(rules, r)
})
re.NoError(err)
return
}

func loadRuleGroups(re *require.Assertions, ruleStorage endpoint.RuleStorage) (groups []*placement.RuleGroup) {
err := ruleStorage.LoadRuleGroups(func(_, v string) {
rg, err := placement.NewRuleGroupFromJSON([]byte(v))
re.NoError(err)
groups = append(groups, rg)
})
re.NoError(err)
return
}

func loadRegionRules(re *require.Assertions, ruleStorage endpoint.RuleStorage) (rules []*labeler.LabelRule) {
err := ruleStorage.LoadRegionRules(func(_, v string) {
lr, err := labeler.NewLabelRuleFromJSON([]byte(v))
re.NoError(err)
rules = append(rules, lr)
})
re.NoError(err)
return
}

func (suite *ruleTestSuite) TestRuleWatch() {
re := suite.Require()

ruleStorage := endpoint.NewStorageEndpoint(kv.NewMemoryKV(), nil)
// Create a rule watcher.
_, err := rule.NewWatcher(
suite.ctx,
suite.pdLeaderServer.GetEtcdClient(),
suite.cluster.GetCluster().GetId(),
ruleStorage,
)
re.NoError(err)
// Check the default rule.
rules := loadRules(re, ruleStorage)
re.Len(rules, 1)
re.Equal("pd", rules[0].GroupID)
re.Equal("default", rules[0].ID)
re.Equal(0, rules[0].Index)
re.Empty(rules[0].StartKey)
re.Empty(rules[0].EndKey)
re.Equal(placement.Voter, rules[0].Role)
re.Empty(rules[0].LocationLabels)
// Check the empty rule group.
ruleGroups := loadRuleGroups(re, ruleStorage)
re.NoError(err)
re.Empty(ruleGroups)
// Set a new rule via the PD API server.
ruleManager := suite.pdLeaderServer.GetRaftCluster().GetRuleManager()
rule := &placement.Rule{
GroupID: "2",
ID: "3",
Role: "voter",
Count: 1,
StartKeyHex: "22",
EndKeyHex: "dd",
}
err = ruleManager.SetRule(rule)
re.NoError(err)
testutil.Eventually(re, func() bool {
rules = loadRules(re, ruleStorage)
return len(rules) == 2
})
sort.Slice(rules, func(i, j int) bool {
return rules[i].ID > rules[j].ID
})
re.Len(rules, 2)
re.Equal(rule.GroupID, rules[1].GroupID)
re.Equal(rule.ID, rules[1].ID)
re.Equal(rule.Role, rules[1].Role)
re.Equal(rule.Count, rules[1].Count)
re.Equal(rule.StartKeyHex, rules[1].StartKeyHex)
re.Equal(rule.EndKeyHex, rules[1].EndKeyHex)
// Delete the rule.
err = ruleManager.DeleteRule(rule.GroupID, rule.ID)
re.NoError(err)
testutil.Eventually(re, func() bool {
rules = loadRules(re, ruleStorage)
return len(rules) == 1
})
re.Len(rules, 1)
re.Equal("pd", rules[0].GroupID)
// Create a new rule group.
ruleGroup := &placement.RuleGroup{
ID: "2",
Index: 100,
Override: true,
}
err = ruleManager.SetRuleGroup(ruleGroup)
re.NoError(err)
testutil.Eventually(re, func() bool {
ruleGroups = loadRuleGroups(re, ruleStorage)
return len(ruleGroups) == 1
})
re.Len(ruleGroups, 1)
re.Equal(ruleGroup.ID, ruleGroups[0].ID)
re.Equal(ruleGroup.Index, ruleGroups[0].Index)
re.Equal(ruleGroup.Override, ruleGroups[0].Override)
// Delete the rule group.
err = ruleManager.DeleteRuleGroup(ruleGroup.ID)
re.NoError(err)
testutil.Eventually(re, func() bool {
ruleGroups = loadRuleGroups(re, ruleStorage)
return len(ruleGroups) == 0
})
re.Empty(ruleGroups)

// Test the region label rule watch.
labelRules := loadRegionRules(re, ruleStorage)
re.Len(labelRules, 1)
defaultKeyspaceRule := keyspace.MakeLabelRule(utils.DefaultKeyspaceID)
re.Equal(defaultKeyspaceRule, labelRules[0])
// Set a new region label rule.
labelRule := &labeler.LabelRule{
ID: "rule1",
Labels: []labeler.RegionLabel{{Key: "k1", Value: "v1"}},
RuleType: "key-range",
Data: labeler.MakeKeyRanges("1234", "5678"),
}
regionLabeler := suite.pdLeaderServer.GetRaftCluster().GetRegionLabeler()
err = regionLabeler.SetLabelRule(labelRule)
re.NoError(err)
testutil.Eventually(re, func() bool {
labelRules = loadRegionRules(re, ruleStorage)
return len(labelRules) == 2
})
sort.Slice(labelRules, func(i, j int) bool {
return labelRules[i].ID < labelRules[j].ID
})
re.Len(labelRules, 2)
re.Equal(labelRule.ID, labelRules[1].ID)
re.Equal(labelRule.Labels, labelRules[1].Labels)
re.Equal(labelRule.RuleType, labelRules[1].RuleType)
// Patch the region label rule.
labelRule = &labeler.LabelRule{
ID: "rule2",
Labels: []labeler.RegionLabel{{Key: "k2", Value: "v2"}},
RuleType: "key-range",
Data: labeler.MakeKeyRanges("ab12", "cd12"),
}
patch := labeler.LabelRulePatch{
SetRules: []*labeler.LabelRule{labelRule},
DeleteRules: []string{"rule1"},
}
err = regionLabeler.Patch(patch)
re.NoError(err)
testutil.Eventually(re, func() bool {
labelRules = loadRegionRules(re, ruleStorage)
return len(labelRules) == 2
})
sort.Slice(labelRules, func(i, j int) bool {
return labelRules[i].ID < labelRules[j].ID
})
re.Len(labelRules, 2)
re.Equal(defaultKeyspaceRule, labelRules[0])
re.Equal(labelRule.ID, labelRules[1].ID)
re.Equal(labelRule.Labels, labelRules[1].Labels)
re.Equal(labelRule.RuleType, labelRules[1].RuleType)
}

func (suite *ruleTestSuite) TestRuleWatchWithManager() {
re := suite.Require()

tc, err := tests.NewTestSchedulingCluster(suite.ctx, 1, suite.backendEndpoint)
re.NoError(err)
defer tc.Destroy()
Expand Down

0 comments on commit 11dbbb2

Please sign in to comment.