Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

labeler: add index to support rule override #4032

Merged
merged 1 commit into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions server/schedule/labeler/labeler.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,37 +243,56 @@ func (l *RegionLabeler) Patch(patch LabelRulePatch) error {
}

// GetRegionLabel returns the label of the region for a key.
// If there are multiple rules that match the key, the one with max rule index will be returned.
func (l *RegionLabeler) GetRegionLabel(region *core.RegionInfo, key string) string {
l.RLock()
defer l.RUnlock()
value, index := "", -1
// search ranges
if i, data := l.rangeList.GetData(region.GetStartKey(), region.GetEndKey()); i != -1 {
for _, rule := range data {
for _, l := range rule.(*LabelRule).Labels {
r := rule.(*LabelRule)
if r.Index <= index && value != "" {
continue
}
for _, l := range r.Labels {
if l.Key == key {
return l.Value
value, index = l.Value, r.Index
}
}
}
}
return ""
return value
}

// GetRegionLabels returns the labels of the region.
// For each key, the label with max rule index will be returned.
func (l *RegionLabeler) GetRegionLabels(region *core.RegionInfo) []*RegionLabel {
l.RLock()
defer l.RUnlock()
var result []*RegionLabel
type valueIndex struct {
value string
index int
}
labels := make(map[string]valueIndex)

// search ranges
if i, data := l.rangeList.GetData(region.GetStartKey(), region.GetEndKey()); i != -1 {
for _, rule := range data {
for _, l := range rule.(*LabelRule).Labels {
result = append(result, &RegionLabel{
Key: l.Key,
Value: l.Value,
})
r := rule.(*LabelRule)
for _, l := range r.Labels {
if old, ok := labels[l.Key]; !ok || old.index < r.Index {
labels[l.Key] = valueIndex{l.Value, r.Index}
}
}
}
}
result := make([]*RegionLabel, 0, len(labels))
for k, l := range labels {
result = append(result, &RegionLabel{
Key: k,
Value: l.value,
})
}
return result
}
40 changes: 39 additions & 1 deletion server/schedule/labeler/labeler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type testLabelerSuite struct {
labeler *RegionLabeler
}

func (s *testLabelerSuite) SetUpSuite(c *C) {
func (s *testLabelerSuite) SetUpTest(c *C) {
s.store = core.NewStorage(kv.NewMemoryKV())
var err error
s.labeler, err = NewRegionLabeler(s.store)
Expand Down Expand Up @@ -137,6 +137,44 @@ func (s *testLabelerSuite) TestGetSetRule(c *C) {
c.Assert(allRules, DeepEquals, rules[1:])
}

func (s *testLabelerSuite) TestIndex(c *C) {
rules := []*LabelRule{
{ID: "rule0", Labels: []RegionLabel{{Key: "k1", Value: "v0"}}, RuleType: "key-range", Rule: map[string]interface{}{"start_key": "", "end_key": ""}},
{ID: "rule1", Index: 1, Labels: []RegionLabel{{Key: "k1", Value: "v1"}}, RuleType: "key-range", Rule: map[string]interface{}{"start_key": "1234", "end_key": "5678"}},
{ID: "rule2", Index: 2, Labels: []RegionLabel{{Key: "k2", Value: "v2"}}, RuleType: "key-range", Rule: map[string]interface{}{"start_key": "ab12", "end_key": "cd12"}},
{ID: "rule3", Index: 1, Labels: []RegionLabel{{Key: "k2", Value: "v3"}}, RuleType: "key-range", Rule: map[string]interface{}{"start_key": "abcd", "end_key": "efef"}},
}
for _, r := range rules {
err := s.labeler.SetLabelRule(r)
c.Assert(err, IsNil)
}

type testCase struct {
start, end string
labels map[string]string
}
testCases := []testCase{
{"", "1234", map[string]string{"k1": "v0"}},
{"1234", "5678", map[string]string{"k1": "v1"}},
{"ab12", "abcd", map[string]string{"k1": "v0", "k2": "v2"}},
{"abcd", "cd12", map[string]string{"k1": "v0", "k2": "v2"}},
{"cdef", "efef", map[string]string{"k1": "v0", "k2": "v3"}},
}
for _, tc := range testCases {
start, _ := hex.DecodeString(tc.start)
end, _ := hex.DecodeString(tc.end)
region := core.NewTestRegionInfo(start, end)
labels := s.labeler.GetRegionLabels(region)
c.Assert(labels, HasLen, len(tc.labels))
for _, l := range labels {
c.Assert(l.Value, Equals, tc.labels[l.Key])
}
for _, k := range []string{"k1", "k2"} {
c.Assert(s.labeler.GetRegionLabel(region, k), Equals, tc.labels[k])
}
}
}

func (s *testLabelerSuite) TestSaveLoadRule(c *C) {
rules := []*LabelRule{
{ID: "rule1", Labels: []RegionLabel{{Key: "k1", Value: "v1"}}, RuleType: "key-range", Rule: map[string]interface{}{"start_key": "1234", "end_key": "5678"}},
Expand Down
1 change: 1 addition & 0 deletions server/schedule/labeler/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type RegionLabel struct {
// LabelRule is the rule to assign labels to a region.
type LabelRule struct {
ID string `json:"id"`
Index int `json:"index"`
Labels []RegionLabel `json:"labels"`
RuleType string `json:"rule_type"`
Rule interface{} `json:"rule"`
Expand Down