Skip to content

Commit

Permalink
*: check whether region is nil (tikv#7263)
Browse files Browse the repository at this point in the history
close tikv#7261

Signed-off-by: lhy1024 <admin@liudos.us>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and rleungx committed Dec 1, 2023
1 parent 91a198b commit 60f3364
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
30 changes: 23 additions & 7 deletions pkg/schedule/schedulers/hot_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ func (h *hotScheduler) dispatch(typ utils.RWType, cluster sche.SchedulerCluster)
}
return nil
}

func (h *hotScheduler) tryAddPendingInfluence(op *operator.Operator, srcStore, dstStore uint64, infl statistics.Influence, maxZombieDur time.Duration) bool {
func (h *hotScheduler) tryAddPendingInfluence(op *operator.Operator, srcStore []uint64, dstStore uint64, infl statistics.Influence, maxZombieDur time.Duration) bool {
regionID := op.RegionID()
_, ok := h.regionPendings[regionID]
if ok {
Expand Down Expand Up @@ -723,24 +722,41 @@ func (bs *balanceSolver) tryAddPendingInfluence() bool {
if bs.best == nil || len(bs.ops) == 0 {
return false
}
if bs.best.srcStore.IsTiFlash() != bs.best.dstStore.IsTiFlash() {
isSplit := bs.ops[0].Kind() == operator.OpSplit
if !isSplit && bs.best.srcStore.IsTiFlash() != bs.best.dstStore.IsTiFlash() {
hotSchedulerNotSameEngineCounter.Inc()
return false
}
maxZombieDur := bs.calcMaxZombieDur()

// TODO: Process operators atomically.
// main peer
srcStoreID := bs.best.srcStore.GetID()
dstStoreID := bs.best.dstStore.GetID()

srcStoreIDs := make([]uint64, 0)
dstStoreID := uint64(0)
if isSplit {
region := bs.GetRegion(bs.ops[0].RegionID())
if region == nil {
return false
}
for id := range region.GetStoreIDs() {
srcStoreIDs = append(srcStoreIDs, id)
}
} else {
srcStoreIDs = append(srcStoreIDs, bs.best.srcStore.GetID())
dstStoreID = bs.best.dstStore.GetID()
}
infl := bs.collectPendingInfluence(bs.best.mainPeerStat)
if !bs.sche.tryAddPendingInfluence(bs.ops[0], srcStoreID, dstStoreID, infl, maxZombieDur) {
if !bs.sche.tryAddPendingInfluence(bs.ops[0], srcStoreIDs, dstStoreID, infl, maxZombieDur) {
return false
}
if isSplit {
return true
}
// revert peers
if bs.best.revertPeerStat != nil && len(bs.ops) > 1 {
infl := bs.collectPendingInfluence(bs.best.revertPeerStat)
if !bs.sche.tryAddPendingInfluence(bs.ops[1], dstStoreID, srcStoreID, infl, maxZombieDur) {
if !bs.sche.tryAddPendingInfluence(bs.ops[1], srcStoreIDs, dstStoreID, infl, maxZombieDur) {
return false
}
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/schedule/schedulers/split_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,17 @@ func (s *splitBucketScheduler) splitBucket(plan *splitBucketPlan) []*operator.Op
}
if splitBucket != nil {
region := plan.cluster.GetRegion(splitBucket.RegionID)
if region == nil {
return nil
}
splitKey := make([][]byte, 0)
if bytes.Compare(region.GetStartKey(), splitBucket.StartKey) < 0 {
splitKey = append(splitKey, splitBucket.StartKey)
}
if bytes.Compare(region.GetEndKey(), splitBucket.EndKey) > 0 {
splitKey = append(splitKey, splitBucket.EndKey)
}
op, err := operator.CreateSplitRegionOperator(SplitBucketType, plan.cluster.GetRegion(splitBucket.RegionID), operator.OpSplit,
op, err := operator.CreateSplitRegionOperator(SplitBucketType, region, operator.OpSplit,
pdpb.CheckPolicy_USEKEY, splitKey)
if err != nil {
splitBucketCreateOpeartorFailCounter.Inc()
Expand Down
7 changes: 4 additions & 3 deletions pkg/schedule/schedulers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,16 @@ func getKeyRanges(args []string) ([]core.KeyRange, error) {

type pendingInfluence struct {
op *operator.Operator
from, to uint64
froms []uint64
to uint64
origin statistics.Influence
maxZombieDuration time.Duration
}

func newPendingInfluence(op *operator.Operator, from, to uint64, infl statistics.Influence, maxZombieDur time.Duration) *pendingInfluence {
func newPendingInfluence(op *operator.Operator, froms []uint64, to uint64, infl statistics.Influence, maxZombieDur time.Duration) *pendingInfluence {
return &pendingInfluence{
op: op,
from: from,
froms: froms,
to: to,
origin: infl,
maxZombieDuration: maxZombieDur,
Expand Down
6 changes: 5 additions & 1 deletion pkg/statistics/region_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ func (r *RegionStatistics) GetRegionStatsByType(typ RegionStatisticType) []*core
defer r.RUnlock()
res := make([]*core.RegionInfo, 0, len(r.stats[typ]))
for regionID := range r.stats[typ] {
res = append(res, r.rip.GetRegion(regionID).Clone())
region := r.rip.GetRegion(regionID)
if region == nil {
continue
}
res = append(res, region.Clone())
}
return res
}
Expand Down

0 comments on commit 60f3364

Please sign in to comment.