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

client/http: fix the panic when merging empty RegionsInfo #7970

Merged
merged 1 commit into from
Mar 22, 2024
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
15 changes: 13 additions & 2 deletions client/http/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,22 @@ type RegionsInfo struct {
Regions []RegionInfo `json:"regions"`
}

func newRegionsInfo(count int64) *RegionsInfo {
return &RegionsInfo{
Count: count,
Regions: make([]RegionInfo, 0, count),
}
}

// Merge merges two RegionsInfo together and returns a new one.
func (ri *RegionsInfo) Merge(other *RegionsInfo) *RegionsInfo {
newRegionsInfo := &RegionsInfo{
Regions: make([]RegionInfo, 0, ri.Count+other.Count),
if ri == nil {
ri = newRegionsInfo(0)
}
if other == nil {
other = newRegionsInfo(0)
}
newRegionsInfo := newRegionsInfo(ri.Count + other.Count)
m := make(map[int64]RegionInfo, ri.Count+other.Count)
for _, region := range ri.Regions {
m[region.ID] = region
Expand Down
148 changes: 129 additions & 19 deletions client/http/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,140 @@ import (

func TestMergeRegionsInfo(t *testing.T) {
re := require.New(t)
regionsInfo1 := &RegionsInfo{
Count: 1,
Regions: []RegionInfo{
{
ID: 1,
StartKey: "",
EndKey: "a",
testCases := []struct {
source *RegionsInfo
target *RegionsInfo
}{
// Different regions.
{
source: &RegionsInfo{
Count: 1,
Regions: []RegionInfo{
{
ID: 1,
StartKey: "",
EndKey: "a",
},
},
},
target: &RegionsInfo{
Count: 1,
Regions: []RegionInfo{
{
ID: 2,
StartKey: "a",
EndKey: "",
},
},
},
},
}
regionsInfo2 := &RegionsInfo{
Count: 1,
Regions: []RegionInfo{
{
ID: 2,
StartKey: "a",
EndKey: "",
// Same region.
{
source: &RegionsInfo{
Count: 1,
Regions: []RegionInfo{
{
ID: 1,
StartKey: "",
EndKey: "a",
},
},
},
target: &RegionsInfo{
Count: 1,
Regions: []RegionInfo{
{
ID: 1,
StartKey: "",
EndKey: "a",
},
},
},
},
{
source: &RegionsInfo{
Count: 1,
Regions: []RegionInfo{
{
ID: 1,
StartKey: "",
EndKey: "a",
},
},
},
target: nil,
},
{
source: nil,
target: &RegionsInfo{
Count: 1,
Regions: []RegionInfo{
{
ID: 2,
StartKey: "a",
EndKey: "",
},
},
},
},
{
source: nil,
target: nil,
},
{
source: &RegionsInfo{
Count: 1,
Regions: []RegionInfo{
{
ID: 1,
StartKey: "",
EndKey: "a",
},
},
},
target: newRegionsInfo(0),
},
{
source: newRegionsInfo(0),
target: &RegionsInfo{
Count: 1,
Regions: []RegionInfo{
{
ID: 2,
StartKey: "a",
EndKey: "",
},
},
},
},
{
source: newRegionsInfo(0),
target: newRegionsInfo(0),
},
}
for idx, tc := range testCases {
regionsInfo := tc.source.Merge(tc.target)
if tc.source == nil {
tc.source = newRegionsInfo(0)
}
if tc.target == nil {
tc.target = newRegionsInfo(0)
}
m := make(map[int64]RegionInfo, tc.source.Count+tc.target.Count)
for _, region := range tc.source.Regions {
m[region.ID] = region
}
for _, region := range tc.target.Regions {
m[region.ID] = region
}
mergedCount := len(m)
re.Equal(int64(mergedCount), regionsInfo.Count, "case %d", idx)
re.Len(regionsInfo.Regions, mergedCount, "case %d", idx)
// All regions in source and target should be in the merged result.
for _, region := range append(tc.source.Regions, tc.target.Regions...) {
re.Contains(regionsInfo.Regions, region, "case %d", idx)
}
}
regionsInfo := regionsInfo1.Merge(regionsInfo2)
re.Equal(int64(2), regionsInfo.Count)
re.Len(regionsInfo.Regions, 2)
re.Subset(regionsInfo.Regions, append(regionsInfo1.Regions, regionsInfo2.Regions...))
}

func TestRuleStartEndKey(t *testing.T) {
Expand Down
Loading