Skip to content

Commit

Permalink
This is an automated cherry-pick of tikv#7009
Browse files Browse the repository at this point in the history
close tikv#7008

Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
CabinfeverB authored and ti-chi-bot committed Sep 7, 2023
1 parent 41a070b commit 2ce73d5
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 16 deletions.
10 changes: 10 additions & 0 deletions server/api/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ func (s *testStatsSuite) TestRegionStats(c *C) {
statsAll := &statistics.RegionStats{
Count: 4,
EmptyCount: 1,
<<<<<<< HEAD
StorageSize: 351,
=======
StorageSize: 350,
UserStorageSize: 291,
>>>>>>> 74ead5cbd (statistics: fix empty region count when resuming (#7009))
StorageKeys: 221,
StoreLeaderCount: map[uint64]int{1: 1, 4: 2, 5: 1},
StorePeerCount: map[uint64]int{1: 3, 2: 1, 3: 1, 4: 2, 5: 2},
Expand All @@ -144,7 +149,12 @@ func (s *testStatsSuite) TestRegionStats(c *C) {
stats23 := &statistics.RegionStats{
Count: 2,
EmptyCount: 1,
<<<<<<< HEAD
StorageSize: 201,
=======
StorageSize: 200,
UserStorageSize: 181,
>>>>>>> 74ead5cbd (statistics: fix empty region count when resuming (#7009))
StorageKeys: 151,
StoreLeaderCount: map[uint64]int{4: 1, 5: 1},
StorePeerCount: map[uint64]int{1: 2, 4: 1, 5: 2},
Expand Down
6 changes: 6 additions & 0 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,13 @@ func (c *RaftCluster) processRegionHeartbeat(region *core.RegionInfo) error {
if err != nil {
return err
}
<<<<<<< HEAD
region.Inherit(origin, c.storeConfigManager.GetStoreConfig().IsEnableRegionBucket())
=======
if c.GetStoreConfig().IsEnableRegionBucket() {
region.InheritBuckets(origin)
}
>>>>>>> 74ead5cbd (statistics: fix empty region count when resuming (#7009))

c.hotStat.CheckWriteAsync(statistics.NewCheckExpiredItemTask(region))
c.hotStat.CheckReadAsync(statistics.NewCheckExpiredItemTask(region))
Expand Down
46 changes: 33 additions & 13 deletions server/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,14 @@ const (
func RegionFromHeartbeat(heartbeat *pdpb.RegionHeartbeatRequest, opts ...RegionCreateOption) *RegionInfo {
// Convert unit to MB.
// If region isn't empty and less than 1MB, use 1MB instead.
<<<<<<< HEAD:server/core/region.go

Check failure on line 134 in server/core/region.go

View workflow job for this annotation

GitHub Actions / statics

syntax error: unexpected <<, expecting }
// The size of empty region will be correct by the previous RegionInfo.
regionSize := heartbeat.GetApproximateSize() / (1 << 20)
=======
regionSize := heartbeat.GetApproximateSize() / units.MiB
// Due to https://github.com/tikv/tikv/pull/11170, if region size is not initialized,
// approximate size will be zero, and region size is zero not EmptyRegionApproximateSize
>>>>>>> 74ead5cbd (statistics: fix empty region count when resuming (#7009)):pkg/core/region.go

Check failure on line 141 in server/core/region.go

View workflow job for this annotation

GitHub Actions / statics

exponent has no digits

Check failure on line 141 in server/core/region.go

View workflow job for this annotation

GitHub Actions / statics

invalid character U+0023 '#'
if heartbeat.GetApproximateSize() > 0 && regionSize < EmptyRegionApproximateSize {
regionSize = EmptyRegionApproximateSize
}
Expand Down Expand Up @@ -174,19 +180,9 @@ func RegionFromHeartbeat(heartbeat *pdpb.RegionHeartbeatRequest, opts ...RegionC
return region
}

// Inherit inherits the buckets and region size from the parent region if bucket enabled.
// correct approximate size and buckets by the previous size if here exists a reported RegionInfo.
// See https://github.com/tikv/tikv/issues/11114
func (r *RegionInfo) Inherit(origin *RegionInfo, bucketEnable bool) {
// regionSize should not be zero if region is not empty.
if r.GetApproximateSize() == 0 {
if origin != nil {
r.approximateSize = origin.approximateSize
} else {
r.approximateSize = EmptyRegionApproximateSize
}
}
if bucketEnable && origin != nil && r.buckets == nil {
// InheritBuckets inherits the buckets from the parent region if bucket enabled.
func (r *RegionInfo) InheritBuckets(origin *RegionInfo) {
if origin != nil && r.buckets == nil {
r.buckets = origin.buckets
}
}
Expand Down Expand Up @@ -457,6 +453,30 @@ func (r *RegionInfo) GetApproximateSize() int64 {
return r.approximateSize
}

<<<<<<< HEAD:server/core/region.go

Check failure on line 456 in server/core/region.go

View workflow job for this annotation

GitHub Actions / statics

syntax error: non-declaration statement outside function body
=======
// IsEmptyRegion returns whether the region is empty.
func (r *RegionInfo) IsEmptyRegion() bool {
// When cluster resumes, the region size may be not initialized, but region heartbeat is send.
// So use `==` here.
return r.approximateSize == EmptyRegionApproximateSize
}

// GetStorePeerApproximateKeys returns the approximate keys of the peer on the specified store.
func (r *RegionInfo) GetStorePeerApproximateKeys(storeID uint64) int64 {
peer := r.GetStorePeer(storeID)
if storeID != 0 && peer != nil && peer.IsWitness {
return 0
}
return r.approximateKeys
}

// GetApproximateKvSize returns the approximate kv size of the region.
func (r *RegionInfo) GetApproximateKvSize() int64 {
return r.approximateKvSize
}

>>>>>>> 74ead5cbd (statistics: fix empty region count when resuming (#7009)):pkg/core/region.go

Check failure on line 479 in server/core/region.go

View workflow job for this annotation

GitHub Actions / statics

syntax error: non-declaration statement outside function body

Check failure on line 479 in server/core/region.go

View workflow job for this annotation

GitHub Actions / statics

exponent has no digits

Check failure on line 479 in server/core/region.go

View workflow job for this annotation

GitHub Actions / statics

invalid character U+0023 '#'
// GetApproximateKeys returns the approximate keys of the region.
func (r *RegionInfo) GetApproximateKeys() int64 {
return r.approximateKeys
Expand Down
15 changes: 14 additions & 1 deletion server/core/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func (s *testRegionInfoSuite) TestSortedEqual(c *C) {
}
}

<<<<<<< HEAD:server/core/region_test.go
func (s *testRegionInfoSuite) TestInherit(c *C) {
// size in MB
// case for approximateSize
Expand All @@ -219,8 +220,11 @@ func (s *testRegionInfoSuite) TestInherit(c *C) {
r.Inherit(origin, false)
c.Assert(r.approximateSize, Equals, int64(t.expect))
}
=======
func TestInheritBuckets(t *testing.T) {
re := require.New(t)
>>>>>>> 74ead5cbd (statistics: fix empty region count when resuming (#7009)):pkg/core/region_test.go

// bucket
data := []struct {
originBuckets *metapb.Buckets
buckets *metapb.Buckets
Expand All @@ -233,13 +237,22 @@ func (s *testRegionInfoSuite) TestInherit(c *C) {
for _, d := range data {
origin := NewRegionInfo(&metapb.Region{Id: 100}, nil, SetBuckets(d.originBuckets))
r := NewRegionInfo(&metapb.Region{Id: 100}, nil)
<<<<<<< HEAD:server/core/region_test.go
r.Inherit(origin, true)
c.Assert(r.GetBuckets(), DeepEquals, d.originBuckets)
// region will not inherit bucket keys.
if origin.GetBuckets() != nil {
newRegion := NewRegionInfo(&metapb.Region{Id: 100}, nil)
newRegion.Inherit(origin, false)
c.Assert(newRegion.GetBuckets(), Not(DeepEquals), d.originBuckets)
=======
r.InheritBuckets(origin)
re.Equal(d.originBuckets, r.GetBuckets())
// region will not inherit bucket keys.
if origin.GetBuckets() != nil {
newRegion := NewRegionInfo(&metapb.Region{Id: 100}, nil)
re.NotEqual(d.originBuckets, newRegion.GetBuckets())
>>>>>>> 74ead5cbd (statistics: fix empty region count when resuming (#7009)):pkg/core/region_test.go
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions server/statistics/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,21 @@ func (s *RegionStats) Observe(r *core.RegionInfo) {
s.Count++
approximateKeys := r.GetApproximateKeys()
approximateSize := r.GetApproximateSize()
<<<<<<< HEAD:server/statistics/region.go
if approximateSize <= core.EmptyRegionApproximateSize {
s.EmptyCount++
}
s.StorageSize += approximateSize
=======
approximateKvSize := r.GetApproximateKvSize()
if approximateSize == core.EmptyRegionApproximateSize {
s.EmptyCount++
}
if !r.IsEmptyRegion() {
s.StorageSize += approximateSize
}
s.UserStorageSize += approximateKvSize
>>>>>>> 74ead5cbd (statistics: fix empty region count when resuming (#7009)):pkg/statistics/region.go
s.StorageKeys += approximateKeys
leader := r.GetLeader()
if leader != nil {
Expand Down
9 changes: 8 additions & 1 deletion server/statistics/region_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,22 @@ func (r *RegionStatistics) Observe(region *core.RegionInfo, stores []*core.Store
DownPeer: len(region.GetDownPeers()) > 0,
PendingPeer: len(region.GetPendingPeers()) > 0,
LearnerPeer: len(region.GetLearners()) > 0,
EmptyRegion: region.GetApproximateSize() <= core.EmptyRegionApproximateSize,
EmptyRegion: region.IsEmptyRegion(),
OversizedRegion: region.IsOversized(
int64(r.storeConfigManager.GetStoreConfig().GetRegionMaxSize()),
int64(r.storeConfigManager.GetStoreConfig().GetRegionMaxKeys()),
),
UndersizedRegion: region.NeedMerge(
<<<<<<< HEAD:server/statistics/region_collection.go
int64(r.opt.GetMaxMergeRegionSize()),
int64(r.opt.GetMaxMergeRegionKeys()),
),
=======
int64(r.conf.GetMaxMergeRegionSize()),
int64(r.conf.GetMaxMergeRegionKeys()),
) && region.GetApproximateSize() >= core.EmptyRegionApproximateSize,
WitnessLeader: region.GetLeader().GetIsWitness(),
>>>>>>> 74ead5cbd (statistics: fix empty region count when resuming (#7009)):pkg/statistics/region_collection.go
}

for typ, c := range conditions {
Expand Down
14 changes: 13 additions & 1 deletion server/statistics/region_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (t *testRegionStatisticsSuite) TestRegionStatistics(c *C) {
stores[3] = store3
r1 := &metapb.Region{Id: 1, Peers: peers, StartKey: []byte("aa"), EndKey: []byte("bb")}
r2 := &metapb.Region{Id: 2, Peers: peers[0:2], StartKey: []byte("cc"), EndKey: []byte("dd")}
region1 := core.NewRegionInfo(r1, peers[0])
region1 := core.NewRegionInfo(r1, peers[0], core.SetApproximateSize(1))
region2 := core.NewRegionInfo(r2, peers[0])
regionStats := NewRegionStatistics(opt, t.manager, nil)
regionStats.Observe(region1, stores)
Expand Down Expand Up @@ -112,6 +112,7 @@ func (t *testRegionStatisticsSuite) TestRegionStatistics(c *C) {

region2 = region2.Clone(core.WithDownPeers(downPeers[0:1]))
regionStats.Observe(region2, stores[0:2])
<<<<<<< HEAD:server/statistics/region_collection_test.go
c.Assert(regionStats.stats[ExtraPeer], HasLen, 1)
c.Assert(regionStats.stats[MissPeer], HasLen, 1)
c.Assert(regionStats.stats[DownPeer], HasLen, 2)
Expand All @@ -125,6 +126,17 @@ func (t *testRegionStatisticsSuite) TestRegionStatistics(c *C) {
c.Assert(regionStats.offlineStats[PendingPeer], HasLen, 1)
c.Assert(regionStats.offlineStats[LearnerPeer], HasLen, 1)
c.Assert(regionStats.offlineStats[OfflinePeer], HasLen, 1)
=======
re.Len(regionStats.stats[ExtraPeer], 1)
re.Len(regionStats.stats[MissPeer], 1)
re.Len(regionStats.stats[DownPeer], 2)
re.Len(regionStats.stats[PendingPeer], 1)
re.Len(regionStats.stats[LearnerPeer], 1)
re.Len(regionStats.stats[OversizedRegion], 1)
re.Len(regionStats.stats[UndersizedRegion], 0)
re.Len(regionStats.stats[EmptyRegion], 0)
re.Len(regionStats.stats[OfflinePeer], 1)
>>>>>>> 74ead5cbd (statistics: fix empty region count when resuming (#7009)):pkg/statistics/region_collection_test.go

region1 = region1.Clone(core.WithRemoveStorePeer(7))
regionStats.Observe(region1, stores[0:3])
Expand Down

0 comments on commit 2ce73d5

Please sign in to comment.