-
Notifications
You must be signed in to change notification settings - Fork 728
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
*: fix region stats check #7748
Changes from 4 commits
62c5424
2d055ed
c545ac8
77dfa24
dfa0a64
73f62c4
cf03d8c
43cc64d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,12 @@ func (r *RegionInfo) LoadedFromStorage() bool { | |
return r.source == Storage | ||
} | ||
|
||
// LoadedFromSync means this region's meta info loaded from region syncer. | ||
// Only used for test. | ||
func (r *RegionInfo) LoadedFromSync() bool { | ||
return r.source == Sync | ||
} | ||
|
||
// NewRegionInfo creates RegionInfo with region's meta and leader peer. | ||
func NewRegionInfo(region *metapb.Region, leader *metapb.Peer, opts ...RegionCreateOption) *RegionInfo { | ||
regionInfo := &RegionInfo{ | ||
|
@@ -705,7 +711,7 @@ func (r *RegionInfo) isRegionRecreated() bool { | |
|
||
// RegionGuideFunc is a function that determines which follow-up operations need to be performed based on the origin | ||
// and new region information. | ||
type RegionGuideFunc func(region, origin *RegionInfo) (isNew, saveKV, saveCache, needSync bool) | ||
type RegionGuideFunc func(region, origin *RegionInfo) (saveKV, saveCache, needSync bool) | ||
|
||
// GenerateRegionGuideFunc is used to generate a RegionGuideFunc. Control the log output by specifying the log function. | ||
// nil means do not print the log. | ||
|
@@ -718,19 +724,16 @@ func GenerateRegionGuideFunc(enableLog bool) RegionGuideFunc { | |
} | ||
// Save to storage if meta is updated. | ||
// Save to cache if meta or leader is updated, or contains any down/pending peer. | ||
// Mark isNew if the region in cache does not have leader. | ||
return func(region, origin *RegionInfo) (isNew, saveKV, saveCache, needSync bool) { | ||
// Now, no need to hold `isNew` because `saveCache` must be true if the region in cache does not have leader. | ||
return func(region, origin *RegionInfo) (saveKV, saveCache, needSync bool) { | ||
if origin == nil { | ||
if log.GetLevel() <= zap.DebugLevel { | ||
debug("insert new region", | ||
zap.Uint64("region-id", region.GetID()), | ||
logutil.ZapRedactStringer("meta-region", RegionToHexMeta(region.GetMeta()))) | ||
} | ||
saveKV, saveCache, isNew = true, true, true | ||
saveKV, saveCache = true, true | ||
} else { | ||
if origin.LoadedFromStorage() { | ||
isNew = true | ||
} | ||
r := region.GetRegionEpoch() | ||
o := origin.GetRegionEpoch() | ||
if r.GetVersion() > o.GetVersion() { | ||
|
@@ -756,9 +759,7 @@ func GenerateRegionGuideFunc(enableLog bool) RegionGuideFunc { | |
saveKV, saveCache = true, true | ||
} | ||
if region.GetLeader().GetId() != origin.GetLeader().GetId() { | ||
if origin.GetLeader().GetId() == 0 { | ||
isNew = true | ||
} else if log.GetLevel() <= zap.InfoLevel { | ||
if origin.GetLeader().GetId() != 0 && log.GetLevel() <= zap.InfoLevel { | ||
info("leader changed", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will it print leader 0? |
||
zap.Uint64("region-id", region.GetID()), | ||
zap.Uint64("from", origin.GetLeader().GetStoreId()), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,6 +148,9 @@ func (r *RegionStatistics) deleteEntry(deleteIndex RegionStatisticType, regionID | |
// due to some special state types. | ||
func (r *RegionStatistics) RegionStatsNeedUpdate(region *core.RegionInfo) bool { | ||
regionID := region.GetID() | ||
if !r.isObserved(regionID) { | ||
return true | ||
} | ||
if r.IsRegionStatsType(regionID, OversizedRegion) != | ||
region.IsOversized(int64(r.conf.GetRegionMaxSize()), int64(r.conf.GetRegionMaxKeys())) { | ||
return true | ||
|
@@ -156,6 +159,14 @@ func (r *RegionStatistics) RegionStatsNeedUpdate(region *core.RegionInfo) bool { | |
region.NeedMerge(int64(r.conf.GetMaxMergeRegionSize()), int64(r.conf.GetMaxMergeRegionKeys())) | ||
} | ||
|
||
// isObserved returns whether the region is observed. And it also shows whether PD received heartbeat of this region. | ||
func (r *RegionStatistics) isObserved(id uint64) bool { | ||
r.RLock() | ||
defer r.RUnlock() | ||
_, ok := r.index[id] | ||
return ok | ||
} | ||
|
||
// Observe records the current regions' status. | ||
func (r *RegionStatistics) Observe(region *core.RegionInfo, stores []*core.StoreInfo) { | ||
r.Lock() | ||
|
@@ -164,7 +175,6 @@ func (r *RegionStatistics) Observe(region *core.RegionInfo, stores []*core.Store | |
desiredReplicas = r.conf.GetMaxReplicas() | ||
desiredVoters = desiredReplicas | ||
peerTypeIndex RegionStatisticType | ||
deleteIndex RegionStatisticType | ||
) | ||
// Check if the region meets count requirements of its rules. | ||
if r.conf.IsPlacementRulesEnabled() { | ||
|
@@ -240,10 +250,10 @@ func (r *RegionStatistics) Observe(region *core.RegionInfo, stores []*core.Store | |
} | ||
} | ||
// Remove the info if any of the conditions are not met any more. | ||
if oldIndex, ok := r.index[regionID]; ok { | ||
deleteIndex = oldIndex &^ peerTypeIndex | ||
if oldIndex, ok := r.index[regionID]; ok && oldIndex > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for 0 for region state feels a bit weird, do you think we need to add a state name to 0? Like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For bit operations. |
||
deleteIndex := oldIndex &^ peerTypeIndex | ||
r.deleteEntry(deleteIndex, regionID) | ||
} | ||
r.deleteEntry(deleteIndex, regionID) | ||
r.index[regionID] = peerTypeIndex | ||
} | ||
|
||
|
@@ -252,7 +262,10 @@ func (r *RegionStatistics) ClearDefunctRegion(regionID uint64) { | |
r.Lock() | ||
defer r.Unlock() | ||
if oldIndex, ok := r.index[regionID]; ok { | ||
r.deleteEntry(oldIndex, regionID) | ||
delete(r.index, regionID) | ||
if oldIndex > 0 { | ||
r.deleteEntry(oldIndex, regionID) | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe can move this statement to pr description
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. But I think we can keep this comment in code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh 😯 I mean, after your code merged,
isNew
is a word which doesn't exist and could be confused others.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK