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 Oct 31, 2022
1 parent 80f0d8c commit 756858c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
55 changes: 28 additions & 27 deletions server/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,9 @@ func GenerateRegionGuideFunc(enableLog bool) RegionGuideFunc {
logutil.ZapRedactStringer("meta-region", RegionToHexMeta(region.GetMeta())))
saveKV, saveCache, isNew = true, true, true
} else {
if !origin.IsFromHeartbeat() || origin.GetLeader().GetId() == 0 {
isNew = true
}
r := region.GetRegionEpoch()
o := origin.GetRegionEpoch()
if r.GetVersion() > o.GetVersion() {
Expand All @@ -625,60 +628,58 @@ func GenerateRegionGuideFunc(enableLog bool) RegionGuideFunc {
if r.GetConfVer() > o.GetConfVer() {
info("region ConfVer changed",
zap.Uint64("region-id", region.GetID()),
zap.String("detail", DiffRegionPeersInfo(origin, region)),
logutil.ZapRedactString("detail", DiffRegionPeersInfo(origin, region)),
zap.Uint64("old-confver", o.GetConfVer()),
zap.Uint64("new-confver", r.GetConfVer()),
)
saveKV, saveCache = true, true
}
if region.GetLeader().GetId() != origin.GetLeader().GetId() {
if origin.GetLeader().GetId() == 0 {
isNew = true
} else {
info("leader changed",
zap.Uint64("region-id", region.GetID()),
zap.Uint64("from", origin.GetLeader().GetStoreId()),
zap.Uint64("to", region.GetLeader().GetStoreId()),
)
}
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()))
info("leader changed",
zap.Uint64("region-id", region.GetID()),
zap.Uint64("from", origin.GetLeader().GetStoreId()),
zap.Uint64("to", region.GetLeader().GetStoreId()),
)
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 @@ -686,3 +686,34 @@ func BenchmarkAddRegion(b *testing.B) {
regions.SetRegion(items[i])
}
}

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 756858c

Please sign in to comment.