Skip to content

Commit

Permalink
reorder the check
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx committed Dec 2, 2022
1 parent 5f33430 commit 12a94d4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
39 changes: 22 additions & 17 deletions server/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,9 @@ func GenerateRegionGuideFunc(enableLog bool) RegionGuideFunc {
logutil.ZapRedactStringer("meta-region", RegionToHexMeta(region.GetMeta())))
saveKV, saveCache, isNew = true, true, true
} else {
if !origin.IsFromHeartbeat() {
isNew = true
}
r := region.GetRegionEpoch()
o := origin.GetRegionEpoch()
if r.GetVersion() > o.GetVersion() {
Expand Down Expand Up @@ -656,43 +659,45 @@ func GenerateRegionGuideFunc(enableLog bool) RegionGuideFunc {
)
}
saveCache, needSync = true, true
}
if !SortedPeersStatsEqual(region.GetDownPeers(), origin.GetDownPeers()) {
debug("down-peers changed", zap.Uint64("region-id", region.GetID()))
saveCache, needSync = true, true
}
if !SortedPeersEqual(region.GetPendingPeers(), origin.GetPendingPeers()) {
debug("pending-peers changed", zap.Uint64("region-id", region.GetID()))
saveCache, needSync = true, true
return
}
if len(region.GetPeers()) != len(origin.GetPeers()) {
saveKV, saveCache = true, true
return
}
if len(region.GetBuckets().GetKeys()) != len(origin.GetBuckets().GetKeys()) {
debug("bucket key changed", zap.Uint64("region-id", region.GetID()))
saveKV, saveCache = true, true
}

if region.GetApproximateSize() != origin.GetApproximateSize() ||
region.GetApproximateKeys() != origin.GetApproximateKeys() {
saveCache = true
return
}
// Once flow has changed, will update the cache.
// Because keys and bytes are strongly related, only bytes are judged.
if region.GetRoundBytesWritten() != origin.GetRoundBytesWritten() ||
region.GetRoundBytesRead() != origin.GetRoundBytesRead() ||
region.flowRoundDivisor < origin.flowRoundDivisor {
saveCache, needSync = true, true
return
}
if !SortedPeersStatsEqual(region.GetDownPeers(), origin.GetDownPeers()) {
debug("down-peers changed", zap.Uint64("region-id", region.GetID()))
saveCache, needSync = true, true
return
}
if !SortedPeersEqual(region.GetPendingPeers(), origin.GetPendingPeers()) {
debug("pending-peers changed", zap.Uint64("region-id", region.GetID()))
saveCache, needSync = true, true
return
}
if region.GetApproximateSize() != origin.GetApproximateSize() ||
region.GetApproximateKeys() != origin.GetApproximateKeys() {
saveCache = true
return
}

if region.GetReplicationStatus().GetState() != replication_modepb.RegionReplicationState_UNKNOWN &&
(region.GetReplicationStatus().GetState() != origin.GetReplicationStatus().GetState() ||
region.GetReplicationStatus().GetStateId() != origin.GetReplicationStatus().GetStateId()) {
saveCache = true
}
if !origin.IsFromHeartbeat() {
isNew = true
}
}
return
}
Expand Down
31 changes: 31 additions & 0 deletions server/core/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,3 +694,34 @@ func BenchmarkAddRegion(b *testing.B) {
regions.UpdateSubTree(items[i], origin, overlaps, rangeChanged)
}
}

func BenchmarkRegionFromHeartbeat(b *testing.B) {
peers := make([]*metapb.Peer, 0, 3)
for i := uint64(1); i <= 3; i++ {
peers = append(peers, &metapb.Peer{
Id: i,
StoreId: i,
})
}
regionReq := &pdpb.RegionHeartbeatRequest{
Region: &metapb.Region{
Id: 1,
Peers: peers,
StartKey: []byte{byte(2)},
EndKey: []byte{byte(3)},
RegionEpoch: &metapb.RegionEpoch{
ConfVer: 2,
Version: 1,
},
},
Leader: peers[0],
Term: 5,
ApproximateSize: 10,
PendingPeers: []*metapb.Peer{peers[1]},
DownPeers: []*pdpb.PeerStats{{Peer: peers[2], DownSeconds: 100}},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
RegionFromHeartbeat(regionReq)
}
}

0 comments on commit 12a94d4

Please sign in to comment.