Skip to content

Commit

Permalink
core: use previous size if heartbeat's size is zero
Browse files Browse the repository at this point in the history
ref tikv/tikv#11114

Signed-off-by: p4tr1ck <patrick.li@pingcap.com>
  • Loading branch information
w41ter committed Oct 28, 2021
1 parent 433d4f2 commit fc7f774
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
2 changes: 2 additions & 0 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,8 @@ func (c *RaftCluster) processRegionHeartbeat(region *core.RegionInfo) error {
if err != nil {
return err
}
region.CorrectApproximateSize(origin)

hotStat.CheckWriteAsync(statistics.NewCheckExpiredItemTask(region))
hotStat.CheckReadAsync(statistics.NewCheckExpiredItemTask(region))
reportInterval := region.GetInterval()
Expand Down
25 changes: 17 additions & 8 deletions server/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,6 @@ const (

// RegionFromHeartbeat constructs a Region from region heartbeat.
func RegionFromHeartbeat(heartbeat *pdpb.RegionHeartbeatRequest, opts ...RegionCreateOption) *RegionInfo {
// Convert unit to MB.
// If region is empty or less than 1MB, use 1MB instead.
regionSize := heartbeat.GetApproximateSize() / (1 << 20)
if regionSize < EmptyRegionApproximateSize {
regionSize = EmptyRegionApproximateSize
}

region := &RegionInfo{
term: heartbeat.GetTerm(),
meta: heartbeat.GetRegion(),
Expand All @@ -123,7 +116,7 @@ func RegionFromHeartbeat(heartbeat *pdpb.RegionHeartbeatRequest, opts ...RegionC
writtenKeys: heartbeat.GetKeysWritten(),
readBytes: heartbeat.GetBytesRead(),
readKeys: heartbeat.GetKeysRead(),
approximateSize: int64(regionSize),
approximateSize: int64(heartbeat.GetApproximateSize()),
approximateKeys: int64(heartbeat.GetApproximateKeys()),
interval: heartbeat.GetInterval(),
replicationStatus: heartbeat.GetReplicationStatus(),
Expand All @@ -150,6 +143,22 @@ func RegionFromHeartbeat(heartbeat *pdpb.RegionHeartbeatRequest, opts ...RegionC
return region
}

// AdjustApproximateSize convert approximate size unit to MB, and
// - if region is empty, and there exists an reported RegionInfo, use the previous size;
// - otherwise if region size less than 1MB, use 1MB instead.
//
// See https://github.com/tikv/tikv/issues/11114
func (r *RegionInfo) CorrectApproximateSize(origin *RegionInfo) {
if r.approximateSize == 0 && origin != nil {
r.approximateSize = origin.approximateSize
} else {
r.approximateSize = r.approximateSize / (1 << 20)
if r.approximateSize < EmptyRegionApproximateSize {
r.approximateSize = EmptyRegionApproximateSize
}
}
}

// Clone returns a copy of current regionInfo.
func (r *RegionInfo) Clone(opts ...RegionCreateOption) *RegionInfo {
downPeers := make([]*pdpb.PeerStats, 0, len(r.downPeers))
Expand Down
29 changes: 29 additions & 0 deletions server/core/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,35 @@ func (s *testRegionInfoSuite) TestSortedEqual(c *C) {
}
}

func (s *testRegionInfoSuite) TestCorrectRegionApproximateSize(c *C) {
// size in MB
testcases := []struct {
originExists bool
originSize uint64
size uint64
expect uint64
}{
{false, 0, 0, 1},
{false, 0, 2, 2},
{true, 0, 2, 2},
{true, 1, 2, 2},
{true, 2, 0, 2},
}
for _, t := range testcases {
var origin *RegionInfo
if t.originExists {
origin = NewRegionInfo(&metapb.Region{Id: 100}, nil)
origin.approximateSize = int64(t.originSize)
}
r := NewRegionInfo(&metapb.Region{Id: 100}, nil)
r.approximateSize = int64(t.size << 20)
fmt.Printf("before %d\n", r.approximateSize)
r.CorrectApproximateSize(origin)
fmt.Printf("after %d\n", r.approximateSize)
c.Assert(r.approximateSize, Equals, int64(t.expect))
}
}

func (s *testRegionInfoSuite) TestRegionRoundingFlow(c *C) {
testcases := []struct {
flow uint64
Expand Down

0 comments on commit fc7f774

Please sign in to comment.