diff --git a/pkg/rangetree/range_tree.go b/pkg/rangetree/range_tree.go index 47d7e960a0e..174e338f775 100644 --- a/pkg/rangetree/range_tree.go +++ b/pkg/rangetree/range_tree.go @@ -76,9 +76,10 @@ func (r *RangeTree) GetOverlaps(item RangeItem) []RangeItem { } var overlaps []RangeItem + endKey := item.GetEndKey() r.tree.AscendGreaterOrEqual(result, func(i btree.Item) bool { over := i.(RangeItem) - if len(item.GetEndKey()) > 0 && bytes.Compare(item.GetEndKey(), over.GetStartKey()) <= 0 { + if len(endKey) > 0 && bytes.Compare(endKey, over.GetStartKey()) <= 0 { return false } overlaps = append(overlaps, over) diff --git a/server/core/region.go b/server/core/region.go index d041ede2cb9..cdf961749c4 100644 --- a/server/core/region.go +++ b/server/core/region.go @@ -702,7 +702,7 @@ func (rm regionMap) Get(id uint64) *regionItem { // If the regionItem already exists, it will be overwritten. // Note: Do not use this function when you only need to update the RegionInfo and do not need a new regionItem. func (rm regionMap) AddNew(region *RegionInfo) *regionItem { - item := ®ionItem{region: region} + item := ®ionItem{RegionInfo: region} rm[region.GetID()] = item return item } @@ -738,7 +738,7 @@ func NewRegionsInfo() *RegionsInfo { // GetRegion returns the RegionInfo with regionID func (r *RegionsInfo) GetRegion(regionID uint64) *RegionInfo { if item := r.regions.Get(regionID); item != nil { - return item.region + return item.RegionInfo } return nil } @@ -750,7 +750,7 @@ func (r *RegionsInfo) SetRegion(region *RegionInfo) (overlaps []*RegionInfo) { rangeChanged := true // This Region is new, or its range has changed. if item = r.regions.Get(region.GetID()); item != nil { // If this ID already exists, use the existing regionItem and pick out the origin. - origin := item.region + origin := item.RegionInfo rangeChanged = !origin.rangeEqualsTo(region) if rangeChanged { // Delete itself in regionTree so that overlaps will not contain itself. @@ -765,14 +765,14 @@ func (r *RegionsInfo) SetRegion(region *RegionInfo) (overlaps []*RegionInfo) { // If the peers are not changed, only the statistical on the sub regionTree needs to be updated. r.updateSubTreeStat(origin, region) // Update the RegionInfo in the regionItem. - item.region = region + item.RegionInfo = region return } // If the range or peers have changed, the sub regionTree needs to be cleaned up. // TODO: Improve performance by deleting only the different peers. r.removeRegionFromSubTree(origin) // Update the RegionInfo in the regionItem. - item.region = region + item.RegionInfo = region } else { // If this ID does not exist, generate a new regionItem and save it in the regionMap. item = r.regions.AddNew(region) @@ -963,7 +963,7 @@ func (r *RegionsInfo) GetPrevRegionByKey(regionKey []byte) *RegionInfo { func (r *RegionsInfo) GetRegions() []*RegionInfo { regions := make([]*RegionInfo, 0, r.regions.Len()) for _, item := range r.regions { - regions = append(regions, item.region) + regions = append(regions, item.RegionInfo) } return regions } @@ -1027,7 +1027,7 @@ func (r *RegionsInfo) GetStoreWriteRate(storeID uint64) (bytesRate, keysRate flo func (r *RegionsInfo) GetMetaRegions() []*metapb.Region { regions := make([]*metapb.Region, 0, r.regions.Len()) for _, item := range r.regions { - regions = append(regions, typeutil.DeepClone(item.region.meta, RegionFactory)) + regions = append(regions, typeutil.DeepClone(item.meta, RegionFactory)) } return regions } @@ -1110,7 +1110,7 @@ func (r *RegionsInfo) RandLearnerRegions(storeID uint64, ranges []KeyRange, n in // GetLeader returns leader RegionInfo by storeID and regionID (now only used in test) func (r *RegionsInfo) GetLeader(storeID uint64, region *RegionInfo) *RegionInfo { if leaders, ok := r.leaders[storeID]; ok { - return leaders.find(region).region + return leaders.find(region).RegionInfo } return nil } @@ -1118,7 +1118,7 @@ func (r *RegionsInfo) GetLeader(storeID uint64, region *RegionInfo) *RegionInfo // GetFollower returns follower RegionInfo by storeID and regionID (now only used in test) func (r *RegionsInfo) GetFollower(storeID uint64, region *RegionInfo) *RegionInfo { if followers, ok := r.followers[storeID]; ok { - return followers.find(region).region + return followers.find(region).RegionInfo } return nil } @@ -1215,11 +1215,11 @@ func (r *RegionsInfo) GetAdjacentRegions(region *RegionInfo) (*RegionInfo, *Regi p, n := r.tree.getAdjacentRegions(region) var prev, next *RegionInfo // check key to avoid key range hole - if p != nil && bytes.Equal(p.region.GetEndKey(), region.GetStartKey()) { - prev = r.GetRegion(p.region.GetID()) + if p != nil && bytes.Equal(p.GetEndKey(), region.GetStartKey()) { + prev = r.GetRegion(p.GetID()) } - if n != nil && bytes.Equal(region.GetEndKey(), n.region.GetStartKey()) { - next = r.GetRegion(n.region.GetID()) + if n != nil && bytes.Equal(region.GetEndKey(), n.GetStartKey()) { + next = r.GetRegion(n.GetID()) } return prev, next } diff --git a/server/core/region_test.go b/server/core/region_test.go index f3f0c917a04..93367f8dbbc 100644 --- a/server/core/region_test.go +++ b/server/core/region_test.go @@ -401,7 +401,7 @@ func regionInfo(id uint64) *RegionInfo { func check(re *require.Assertions, rm regionMap, ids ...uint64) { // Check Get. for _, id := range ids { - re.Equal(id, rm.Get(id).region.GetID()) + re.Equal(id, rm.Get(id).GetID()) } // Check Len. re.Equal(len(ids), rm.Len()) @@ -412,7 +412,7 @@ func check(re *require.Assertions, rm regionMap, ids ...uint64) { } set1 := make(map[uint64]struct{}) for _, r := range rm { - set1[r.region.GetID()] = struct{}{} + set1[r.GetID()] = struct{}{} } re.Equal(expect, set1) } diff --git a/server/core/region_tree.go b/server/core/region_tree.go index a5d717a93e9..69056675837 100644 --- a/server/core/region_tree.go +++ b/server/core/region_tree.go @@ -30,28 +30,28 @@ import ( var _ rangetree.RangeItem = ®ionItem{} type regionItem struct { - region *RegionInfo + *RegionInfo } // GetStartKey returns the start key of the region. func (r *regionItem) GetStartKey() []byte { - return r.region.GetStartKey() + return r.meta.StartKey } // GetEndKey returns the end key of the region. func (r *regionItem) GetEndKey() []byte { - return r.region.GetEndKey() + return r.meta.EndKey } // Less returns true if the region start key is less than the other. func (r *regionItem) Less(other btree.Item) bool { - left := r.region.GetStartKey() - right := other.(rangetree.RangeItem).GetStartKey() + left := r.meta.StartKey + right := other.(*regionItem).meta.StartKey return bytes.Compare(left, right) < 0 } func (r *regionItem) Contains(key []byte) bool { - start, end := r.region.GetStartKey(), r.region.GetEndKey() + start, end := r.GetStartKey(), r.GetEndKey() return bytes.Compare(key, start) >= 0 && (len(end) == 0 || bytes.Compare(key, end) < 0) } @@ -88,11 +88,11 @@ func (t *regionTree) length() int { // getOverlaps gets the regions which are overlapped with the specified region range. func (t *regionTree) getOverlaps(region *RegionInfo) []*RegionInfo { - item := ®ionItem{region: region} + item := ®ionItem{RegionInfo: region} result := t.tree.GetOverlaps(item) overlaps := make([]*RegionInfo, len(result)) for i, r := range result { - overlaps[i] = r.(*regionItem).region + overlaps[i] = r.(*regionItem).RegionInfo } return overlaps } @@ -101,7 +101,7 @@ func (t *regionTree) getOverlaps(region *RegionInfo) []*RegionInfo { // It finds and deletes all the overlapped regions first, and then // insert the region. func (t *regionTree) update(item *regionItem) []*RegionInfo { - region := item.region + region := item.RegionInfo t.totalSize += region.approximateSize regionWriteBytesRate, regionWriteKeysRate := region.GetWriteRate() t.totalWriteBytesRate += regionWriteBytesRate @@ -110,7 +110,7 @@ func (t *regionTree) update(item *regionItem) []*RegionInfo { overlaps := t.tree.Update(item) result := make([]*RegionInfo, len(overlaps)) for i, overlap := range overlaps { - old := overlap.(*regionItem).region + old := overlap.(*regionItem).RegionInfo result[i] = old log.Debug("overlapping region", zap.Uint64("region-id", old.GetID()), @@ -125,7 +125,7 @@ func (t *regionTree) update(item *regionItem) []*RegionInfo { return result } -// updateStat is used to update statistics when regionItem.region is directly replaced. +// updateStat is used to update statistics when regionItem.RegionInfo is directly replaced. func (t *regionTree) updateStat(origin *RegionInfo, region *RegionInfo) { t.totalSize += region.approximateSize regionWriteBytesRate, regionWriteKeysRate := region.GetWriteRate() @@ -145,14 +145,14 @@ func (t *regionTree) remove(region *RegionInfo) { if t.length() == 0 { return } - item := ®ionItem{region: region} + item := ®ionItem{RegionInfo: region} result := t.tree.Find(item) - if result == nil || result.(*regionItem).region.GetID() != region.GetID() { + if result == nil || result.(*regionItem).GetID() != region.GetID() { return } - t.totalSize -= result.(*regionItem).region.GetApproximateSize() - regionWriteBytesRate, regionWriteKeysRate := result.(*regionItem).region.GetWriteRate() + t.totalSize -= result.(*regionItem).GetApproximateSize() + regionWriteBytesRate, regionWriteKeysRate := result.(*regionItem).GetWriteRate() t.totalWriteBytesRate -= regionWriteBytesRate t.totalWriteKeysRate -= regionWriteKeysRate t.tree.Remove(result) @@ -165,7 +165,7 @@ func (t *regionTree) search(regionKey []byte) *RegionInfo { if result == nil { return nil } - return result.region + return result.RegionInfo } // searchPrev returns the previous region of the region where the regionKey is located. @@ -175,20 +175,20 @@ func (t *regionTree) searchPrev(regionKey []byte) *RegionInfo { if curRegionItem == nil { return nil } - prevRegionItem, _ := t.getAdjacentRegions(curRegionItem.region) + prevRegionItem, _ := t.getAdjacentRegions(curRegionItem.RegionInfo) if prevRegionItem == nil { return nil } - if !bytes.Equal(prevRegionItem.region.GetEndKey(), curRegionItem.region.GetStartKey()) { + if !bytes.Equal(prevRegionItem.GetEndKey(), curRegionItem.GetStartKey()) { return nil } - return prevRegionItem.region + return prevRegionItem.RegionInfo } // find is a helper function to find an item that contains the regions start // key. func (t *regionTree) find(region *RegionInfo) *regionItem { - item := t.tree.Find(®ionItem{region: region}) + item := t.tree.Find(®ionItem{RegionInfo: region}) if item == nil { return nil } @@ -205,9 +205,9 @@ func (t *regionTree) scanRange(startKey []byte, f func(*RegionInfo) bool) { // find if there is a region with key range [s, d), s < startKey < d fn := func(item rangetree.RangeItem) bool { r := item.(*regionItem) - return f(r.region) + return f(r.RegionInfo) } - t.tree.ScanRange(®ionItem{region: region}, fn) + t.tree.ScanRange(®ionItem{RegionInfo: region}, fn) } func (t *regionTree) scanRanges() []*RegionInfo { @@ -223,7 +223,7 @@ func (t *regionTree) scanRanges() []*RegionInfo { } func (t *regionTree) getAdjacentRegions(region *RegionInfo) (*regionItem, *regionItem) { - item := ®ionItem{region: &RegionInfo{meta: &metapb.Region{StartKey: region.GetStartKey()}}} + item := ®ionItem{RegionInfo: &RegionInfo{meta: &metapb.Region{StartKey: region.GetStartKey()}}} prevItem, nextItem := t.tree.GetAdjacentItem(item) var prev, next *regionItem if prevItem != nil { @@ -248,10 +248,10 @@ func (t *regionTree) RandomRegion(ranges []KeyRange) *RegionInfo { for _, i := range rand.Perm(len(ranges)) { var endIndex int startKey, endKey := ranges[i].StartKey, ranges[i].EndKey - startRegion, startIndex := t.tree.GetWithIndex(®ionItem{region: &RegionInfo{meta: &metapb.Region{StartKey: startKey}}}) + startRegion, startIndex := t.tree.GetWithIndex(®ionItem{RegionInfo: &RegionInfo{meta: &metapb.Region{StartKey: startKey}}}) if len(endKey) != 0 { - _, endIndex = t.tree.GetWithIndex(®ionItem{region: &RegionInfo{meta: &metapb.Region{StartKey: endKey}}}) + _, endIndex = t.tree.GetWithIndex(®ionItem{RegionInfo: &RegionInfo{meta: &metapb.Region{StartKey: endKey}}}) } else { endIndex = t.tree.Len() } @@ -272,7 +272,7 @@ func (t *regionTree) RandomRegion(ranges []KeyRange) *RegionInfo { continue } index := rand.Intn(endIndex-startIndex) + startIndex - region := t.tree.GetAt(index).(*regionItem).region + region := t.tree.GetAt(index).(*regionItem).RegionInfo if region.isInvolved(startKey, endKey) { return region } diff --git a/server/core/region_tree_test.go b/server/core/region_tree_test.go index a8a8d6a3ba7..3237a5daf09 100644 --- a/server/core/region_tree_test.go +++ b/server/core/region_tree_test.go @@ -185,28 +185,28 @@ func TestRegionTree(t *testing.T) { // check get adjacent regions prev, next := tree.getAdjacentRegions(regionA) re.Nil(prev) - re.Equal(regionB, next.region) + re.Equal(regionB, next.RegionInfo) prev, next = tree.getAdjacentRegions(regionB) - re.Equal(regionA, prev.region) - re.Equal(regionD, next.region) + re.Equal(regionA, prev.RegionInfo) + re.Equal(regionD, next.RegionInfo) prev, next = tree.getAdjacentRegions(regionC) - re.Equal(regionB, prev.region) - re.Equal(regionD, next.region) + re.Equal(regionB, prev.RegionInfo) + re.Equal(regionD, next.RegionInfo) prev, next = tree.getAdjacentRegions(regionD) - re.Equal(regionB, prev.region) + re.Equal(regionB, prev.RegionInfo) re.Nil(next) // region with the same range and different region id will not be delete. - region0 := newRegionItem([]byte{}, []byte("a")).region + region0 := newRegionItem([]byte{}, []byte("a")).RegionInfo updateNewItem(tree, region0) re.Equal(region0, tree.search([]byte{})) - anotherRegion0 := newRegionItem([]byte{}, []byte("a")).region + anotherRegion0 := newRegionItem([]byte{}, []byte("a")).RegionInfo anotherRegion0.meta.Id = 123 tree.remove(anotherRegion0) re.Equal(region0, tree.search([]byte{})) // overlaps with 0, A, B, C. - region0D := newRegionItem([]byte(""), []byte("d")).region + region0D := newRegionItem([]byte(""), []byte("d")).RegionInfo updateNewItem(tree, region0D) re.Equal(region0D, tree.search([]byte{})) re.Equal(region0D, tree.search([]byte("a"))) @@ -215,7 +215,7 @@ func TestRegionTree(t *testing.T) { re.Equal(regionD, tree.search([]byte("d"))) // overlaps with D. - regionE := newRegionItem([]byte("e"), []byte{}).region + regionE := newRegionItem([]byte("e"), []byte{}).RegionInfo updateNewItem(tree, regionE) re.Equal(region0D, tree.search([]byte{})) re.Equal(region0D, tree.search([]byte("a"))) @@ -240,7 +240,7 @@ func updateRegions(re *require.Assertions, tree *regionTree, regions []*RegionIn func TestRegionTreeSplitAndMerge(t *testing.T) { re := require.New(t) tree := newRegionTree() - regions := []*RegionInfo{newRegionItem([]byte{}, []byte{}).region} + regions := []*RegionInfo{newRegionItem([]byte{}, []byte{}).RegionInfo} // Byte will underflow/overflow if n > 7. n := 7 @@ -355,7 +355,7 @@ func TestRandomRegionDiscontinuous(t *testing.T) { } func updateNewItem(tree *regionTree, region *RegionInfo) { - item := ®ionItem{region: region} + item := ®ionItem{RegionInfo: region} tree.update(item) } @@ -379,7 +379,7 @@ func checkRandomRegion(re *require.Assertions, tree *regionTree, regions []*Regi } func newRegionItem(start, end []byte) *regionItem { - return ®ionItem{region: NewTestRegionInfo(start, end)} + return ®ionItem{RegionInfo: NewTestRegionInfo(start, end)} } type mockRegionTreeData struct {