Skip to content

Commit

Permalink
Merge branch 'master' into region-path
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Oct 27, 2022
2 parents b983009 + 7aba282 commit 7072371
Show file tree
Hide file tree
Showing 29 changed files with 1,003 additions and 524 deletions.
3 changes: 2 additions & 1 deletion pkg/rangetree/range_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion plugin/scheduler_example/evict_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (s *evictLeaderScheduler) Schedule(cluster schedule.Cluster, dryRun bool) (
continue
}
target := filter.NewCandidates(cluster.GetFollowerStores(region)).
FilterTarget(cluster.GetOpts(), nil, &filter.StoreStateFilter{ActionScope: EvictLeaderName, TransferLeader: true}).
FilterTarget(cluster.GetOpts(), nil, nil, &filter.StoreStateFilter{ActionScope: EvictLeaderName, TransferLeader: true}).
RandomPick()
if target == nil {
continue
Expand Down
4 changes: 2 additions & 2 deletions server/api/trend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestTrend(t *testing.T) {
newPeerID := op.Step(0).(operator.AddLearner).PeerID
region5 = region5.Clone(core.WithAddPeer(&metapb.Peer{Id: newPeerID, StoreId: 3, Role: metapb.PeerRole_Learner}), core.WithIncConfVer())
mustRegionHeartbeat(re, svr, region5)
region5 = region5.Clone(core.WithPromoteLearner(newPeerID), core.WithRemoveStorePeer(2), core.WithIncConfVer())
region5 = region5.Clone(core.WithRole(newPeerID, metapb.PeerRole_Voter), core.WithRemoveStorePeer(2), core.WithIncConfVer())
mustRegionHeartbeat(re, svr, region5)

op, err = svr.GetHandler().GetOperator(6)
Expand All @@ -71,7 +71,7 @@ func TestTrend(t *testing.T) {
newPeerID = op.Step(0).(operator.AddLearner).PeerID
region6 = region6.Clone(core.WithAddPeer(&metapb.Peer{Id: newPeerID, StoreId: 3, Role: metapb.PeerRole_Learner}), core.WithIncConfVer())
mustRegionHeartbeat(re, svr, region6)
region6 = region6.Clone(core.WithPromoteLearner(newPeerID), core.WithLeader(region6.GetStorePeer(2)), core.WithRemoveStorePeer(1), core.WithIncConfVer())
region6 = region6.Clone(core.WithRole(newPeerID, metapb.PeerRole_Voter), core.WithLeader(region6.GetStorePeer(2)), core.WithRemoveStorePeer(1), core.WithIncConfVer())
mustRegionHeartbeat(re, svr, region6)

var trend Trend
Expand Down
26 changes: 13 additions & 13 deletions server/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := &regionItem{region: region}
item := &regionItem{RegionInfo: region}
rm[region.GetID()] = item
return item
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -1110,15 +1110,15 @@ 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
}

// 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
}
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions server/core/region_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,12 @@ func WithAddPeer(peer *metapb.Peer) RegionCreateOption {
}
}

// WithPromoteLearner promotes the learner.
func WithPromoteLearner(peerID uint64) RegionCreateOption {
// WithRole changes the role.
func WithRole(peerID uint64, role metapb.PeerRole) RegionCreateOption {
return func(region *RegionInfo) {
for _, p := range region.GetPeers() {
if p.GetId() == peerID {
p.Role = metapb.PeerRole_Voter
p.Role = role
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/core/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
}
Expand Down
52 changes: 26 additions & 26 deletions server/core/region_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ import (
var _ rangetree.RangeItem = &regionItem{}

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)
}

Expand Down Expand Up @@ -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 := &regionItem{region: region}
item := &regionItem{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
}
Expand All @@ -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
Expand All @@ -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()),
Expand All @@ -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()
Expand All @@ -145,14 +145,14 @@ func (t *regionTree) remove(region *RegionInfo) {
if t.length() == 0 {
return
}
item := &regionItem{region: region}
item := &regionItem{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)
Expand All @@ -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.
Expand All @@ -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(&regionItem{region: region})
item := t.tree.Find(&regionItem{RegionInfo: region})
if item == nil {
return nil
}
Expand All @@ -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(&regionItem{region: region}, fn)
t.tree.ScanRange(&regionItem{RegionInfo: region}, fn)
}

func (t *regionTree) scanRanges() []*RegionInfo {
Expand All @@ -223,7 +223,7 @@ func (t *regionTree) scanRanges() []*RegionInfo {
}

func (t *regionTree) getAdjacentRegions(region *RegionInfo) (*regionItem, *regionItem) {
item := &regionItem{region: &RegionInfo{meta: &metapb.Region{StartKey: region.GetStartKey()}}}
item := &regionItem{RegionInfo: &RegionInfo{meta: &metapb.Region{StartKey: region.GetStartKey()}}}
prevItem, nextItem := t.tree.GetAdjacentItem(item)
var prev, next *regionItem
if prevItem != nil {
Expand All @@ -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(&regionItem{region: &RegionInfo{meta: &metapb.Region{StartKey: startKey}}})
startRegion, startIndex := t.tree.GetWithIndex(&regionItem{RegionInfo: &RegionInfo{meta: &metapb.Region{StartKey: startKey}}})

if len(endKey) != 0 {
_, endIndex = t.tree.GetWithIndex(&regionItem{region: &RegionInfo{meta: &metapb.Region{StartKey: endKey}}})
_, endIndex = t.tree.GetWithIndex(&regionItem{RegionInfo: &RegionInfo{meta: &metapb.Region{StartKey: endKey}}})
} else {
endIndex = t.tree.Len()
}
Expand All @@ -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
}
Expand Down
Loading

0 comments on commit 7072371

Please sign in to comment.