Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <jinhao.hu@pingcap.com>
  • Loading branch information
HuSharp committed Sep 7, 2023
1 parent d161acb commit 6be6f17
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
40 changes: 25 additions & 15 deletions pkg/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ type RegionInfo struct {
source RegionSource
}

// RegionSource is the source of region.
type RegionSource uint32

const (
// FromStorage means this region's meta info might be stale.
FromStorage RegionSource = iota
// FromSync means this region's meta info might be stale.
FromSync
// FromHeartbeat means this region's meta info is relatively fresher.
FromHeartbeat
)

// SourceStale means this region's meta info might be stale.
func (r *RegionInfo) SourceStale() bool {
return r.source == FromStorage || r.source == FromSync
}

// SourceFresh means this region's meta info is relatively fresher.
func (r *RegionInfo) SourceFresh() bool {
return r.source == FromHeartbeat
}

// GetRegionSource returns the region source.
func (r *RegionInfo) GetRegionSource() RegionSource {
return r.source
Expand Down Expand Up @@ -685,7 +707,7 @@ func GenerateRegionGuideFunc(enableLog bool) RegionGuideFunc {
}
saveKV, saveCache, isNew = true, true, true
} else {
if origin.source == FromSync || origin.source == FromStorage {
if origin.SourceStale() {
isNew = true
}
r := region.GetRegionEpoch()
Expand Down Expand Up @@ -795,18 +817,6 @@ type RegionsInfo struct {
pendingPeers map[uint64]*regionTree // storeID -> sub regionTree
}

// RegionSource is the source of region.
type RegionSource uint32

const (
// FromStorage means region is stale.
FromStorage RegionSource = iota
// FromSync means region is stale.
FromSync
// FromHeartbeat means region is fresh.
FromHeartbeat
)

// NewRegionsInfo creates RegionsInfo with tree, regions, leaders and followers
func NewRegionsInfo() *RegionsInfo {
return &RegionsInfo{
Expand Down Expand Up @@ -854,7 +864,7 @@ func (r *RegionsInfo) CheckAndPutRegion(region *RegionInfo) []*RegionInfo {
origin, overlaps, rangeChanged := r.setRegionLocked(region, true, ols...)
r.t.Unlock()
r.UpdateSubTree(region, origin, overlaps, rangeChanged)
// FromStorage means region is stale.
// FromStorage means this region's meta info might be stale.
r.AtomicAddStaleRegionCnt()
return overlaps
}
Expand Down Expand Up @@ -902,7 +912,7 @@ func (r *RegionsInfo) AtomicCheckAndPutRegion(region *RegionInfo) ([]*RegionInfo
return nil, err
}
// If origin is stale, need to sub the stale region count.
if origin != nil && origin.source != FromHeartbeat && region.source == FromHeartbeat {
if origin != nil && origin.SourceStale() && region.SourceFresh() {
r.tree.AtomicSubStaleRegionCnt()
}
origin, overlaps, rangeChanged := r.setRegionLocked(region, true, ols...)
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/region_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type regionTree struct {
totalSize int64
totalWriteBytesRate float64
totalWriteKeysRate float64

// count the stale meta regions
staleRegionCnt int64
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/syncer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ func (s *RegionSyncer) StartSyncWithLeader(addr string) {
log.Debug("region is stale", zap.Stringer("origin", origin.GetMeta()), errs.ZapError(err))
continue
}
// FromSync means region is stale.
if origin == nil || (origin != nil && origin.GetRegionSource() == core.FromHeartbeat) {
// FromSync means this region's meta info might be stale.
if origin == nil || (origin != nil && origin.SourceFresh()) {
bc.RegionsInfo.AtomicAddStaleRegionCnt()
}
_, saveKV, _, _ := regionGuide(region, origin)
Expand Down

0 comments on commit 6be6f17

Please sign in to comment.