Skip to content

Commit 8c5859e

Browse files
statistics: fix empty region count when resuming (tikv#7009) (tikv#7055)
close tikv#7008 Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io> Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> Co-authored-by: Yongbo Jiang <cabinfeveroier@gmail.com> Co-authored-by: Cabinfever_B <cabinfeveroier@gmail.com>
1 parent 11f71ee commit 8c5859e

File tree

8 files changed

+31
-53
lines changed

8 files changed

+31
-53
lines changed

server/api/stats_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (suite *statsTestSuite) TestRegionStats() {
137137
statsAll := &statistics.RegionStats{
138138
Count: 4,
139139
EmptyCount: 1,
140-
StorageSize: 351,
140+
StorageSize: 350,
141141
StorageKeys: 221,
142142
StoreLeaderCount: map[uint64]int{1: 1, 4: 2, 5: 1},
143143
StorePeerCount: map[uint64]int{1: 3, 2: 1, 3: 1, 4: 2, 5: 2},
@@ -150,7 +150,7 @@ func (suite *statsTestSuite) TestRegionStats() {
150150
stats23 := &statistics.RegionStats{
151151
Count: 2,
152152
EmptyCount: 1,
153-
StorageSize: 201,
153+
StorageSize: 200,
154154
StorageKeys: 151,
155155
StoreLeaderCount: map[uint64]int{4: 1, 5: 1},
156156
StorePeerCount: map[uint64]int{1: 2, 4: 1, 5: 2},

server/cluster/cluster.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,9 @@ func (c *RaftCluster) processRegionHeartbeat(region *core.RegionInfo) error {
843843
if err != nil {
844844
return err
845845
}
846-
region.Inherit(origin, c.storeConfigManager.GetStoreConfig().IsEnableRegionBucket())
846+
if c.GetStoreConfig().IsEnableRegionBucket() {
847+
region.InheritBuckets(origin)
848+
}
847849

848850
c.hotStat.CheckWriteAsync(statistics.NewCheckExpiredItemTask(region))
849851
c.hotStat.CheckReadAsync(statistics.NewCheckExpiredItemTask(region))

server/core/region.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ const (
144144
func RegionFromHeartbeat(heartbeat *pdpb.RegionHeartbeatRequest, opts ...RegionCreateOption) *RegionInfo {
145145
// Convert unit to MB.
146146
// If region isn't empty and less than 1MB, use 1MB instead.
147-
// The size of empty region will be correct by the previous RegionInfo.
148147
regionSize := heartbeat.GetApproximateSize() / units.MiB
148+
// Due to https://github.com/tikv/tikv/pull/11170, if region size is not initialized,
149+
// approximate size will be zero, and region size is zero not EmptyRegionApproximateSize
149150
if heartbeat.GetApproximateSize() > 0 && regionSize < EmptyRegionApproximateSize {
150151
regionSize = EmptyRegionApproximateSize
151152
}
@@ -188,19 +189,9 @@ func RegionFromHeartbeat(heartbeat *pdpb.RegionHeartbeatRequest, opts ...RegionC
188189
return region
189190
}
190191

191-
// Inherit inherits the buckets and region size from the parent region if bucket enabled.
192-
// correct approximate size and buckets by the previous size if here exists a reported RegionInfo.
193-
// See https://github.com/tikv/tikv/issues/11114
194-
func (r *RegionInfo) Inherit(origin *RegionInfo, bucketEnable bool) {
195-
// regionSize should not be zero if region is not empty.
196-
if r.GetApproximateSize() == 0 {
197-
if origin != nil {
198-
r.approximateSize = origin.approximateSize
199-
} else {
200-
r.approximateSize = EmptyRegionApproximateSize
201-
}
202-
}
203-
if bucketEnable && origin != nil && r.buckets == nil {
192+
// InheritBuckets inherits the buckets from the parent region if bucket enabled.
193+
func (r *RegionInfo) InheritBuckets(origin *RegionInfo) {
194+
if origin != nil && r.buckets == nil {
204195
r.buckets = origin.buckets
205196
}
206197
}
@@ -478,6 +469,13 @@ func (r *RegionInfo) GetApproximateSize() int64 {
478469
return r.approximateSize
479470
}
480471

472+
// IsEmptyRegion returns whether the region is empty.
473+
func (r *RegionInfo) IsEmptyRegion() bool {
474+
// When cluster resumes, the region size may be not initialized, but region heartbeat is send.
475+
// So use `==` here.
476+
return r.approximateSize == EmptyRegionApproximateSize
477+
}
478+
481479
// GetApproximateKeys returns the approximate keys of the region.
482480
func (r *RegionInfo) GetApproximateKeys() int64 {
483481
return r.approximateKeys

server/core/region_test.go

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -186,35 +186,9 @@ func TestSortedEqual(t *testing.T) {
186186
}
187187
}
188188

189-
func TestInherit(t *testing.T) {
189+
func TestInheritBuckets(t *testing.T) {
190190
re := require.New(t)
191-
// size in MB
192-
// case for approximateSize
193-
testCases := []struct {
194-
originExists bool
195-
originSize uint64
196-
size uint64
197-
expect uint64
198-
}{
199-
{false, 0, 0, 1},
200-
{false, 0, 2, 2},
201-
{true, 0, 2, 2},
202-
{true, 1, 2, 2},
203-
{true, 2, 0, 2},
204-
}
205-
for _, testCase := range testCases {
206-
var origin *RegionInfo
207-
if testCase.originExists {
208-
origin = NewRegionInfo(&metapb.Region{Id: 100}, nil)
209-
origin.approximateSize = int64(testCase.originSize)
210-
}
211-
r := NewRegionInfo(&metapb.Region{Id: 100}, nil)
212-
r.approximateSize = int64(testCase.size)
213-
r.Inherit(origin, false)
214-
re.Equal(int64(testCase.expect), r.approximateSize)
215-
}
216191

217-
// bucket
218192
data := []struct {
219193
originBuckets *metapb.Buckets
220194
buckets *metapb.Buckets
@@ -227,12 +201,11 @@ func TestInherit(t *testing.T) {
227201
for _, d := range data {
228202
origin := NewRegionInfo(&metapb.Region{Id: 100}, nil, SetBuckets(d.originBuckets))
229203
r := NewRegionInfo(&metapb.Region{Id: 100}, nil)
230-
r.Inherit(origin, true)
204+
r.InheritBuckets(origin)
231205
re.Equal(d.originBuckets, r.GetBuckets())
232206
// region will not inherit bucket keys.
233207
if origin.GetBuckets() != nil {
234208
newRegion := NewRegionInfo(&metapb.Region{Id: 100}, nil)
235-
newRegion.Inherit(origin, false)
236209
re.NotEqual(d.originBuckets, newRegion.GetBuckets())
237210
}
238211
}

server/statistics/region.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ func (s *RegionStats) Observe(r *core.RegionInfo) {
5757
s.Count++
5858
approximateKeys := r.GetApproximateKeys()
5959
approximateSize := r.GetApproximateSize()
60-
if approximateSize <= core.EmptyRegionApproximateSize {
60+
if approximateSize == core.EmptyRegionApproximateSize {
6161
s.EmptyCount++
6262
}
63-
s.StorageSize += approximateSize
63+
if !r.IsEmptyRegion() {
64+
s.StorageSize += approximateSize
65+
}
6466
s.StorageKeys += approximateKeys
6567
leader := r.GetLeader()
6668
if leader != nil {

server/statistics/region_collection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,15 @@ func (r *RegionStatistics) Observe(region *core.RegionInfo, stores []*core.Store
198198
DownPeer: len(region.GetDownPeers()) > 0,
199199
PendingPeer: len(region.GetPendingPeers()) > 0,
200200
LearnerPeer: len(region.GetLearners()) > 0,
201-
EmptyRegion: region.GetApproximateSize() <= core.EmptyRegionApproximateSize,
201+
EmptyRegion: region.IsEmptyRegion(),
202202
OversizedRegion: region.IsOversized(
203203
int64(r.storeConfigManager.GetStoreConfig().GetRegionMaxSize()),
204204
int64(r.storeConfigManager.GetStoreConfig().GetRegionMaxKeys()),
205205
),
206206
UndersizedRegion: region.NeedMerge(
207207
int64(r.opt.GetMaxMergeRegionSize()),
208208
int64(r.opt.GetMaxMergeRegionKeys()),
209-
),
209+
) && region.GetApproximateSize() >= core.EmptyRegionApproximateSize,
210210
}
211211

212212
for typ, c := range conditions {

server/statistics/region_collection_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestRegionStatistics(t *testing.T) {
6363
stores[3] = store3
6464
r1 := &metapb.Region{Id: 1, Peers: peers, StartKey: []byte("aa"), EndKey: []byte("bb")}
6565
r2 := &metapb.Region{Id: 2, Peers: peers[0:2], StartKey: []byte("cc"), EndKey: []byte("dd")}
66-
region1 := core.NewRegionInfo(r1, peers[0])
66+
region1 := core.NewRegionInfo(r1, peers[0], core.SetApproximateSize(1))
6767
region2 := core.NewRegionInfo(r2, peers[0])
6868
regionStats := NewRegionStatistics(opt, manager, nil)
6969
regionStats.Observe(region1, stores)
@@ -103,7 +103,8 @@ func TestRegionStatistics(t *testing.T) {
103103
re.Len(regionStats.stats[PendingPeer], 1)
104104
re.Len(regionStats.stats[LearnerPeer], 1)
105105
re.Len(regionStats.stats[OversizedRegion], 1)
106-
re.Len(regionStats.stats[UndersizedRegion], 1)
106+
re.Len(regionStats.stats[UndersizedRegion], 0)
107+
re.Len(regionStats.stats[EmptyRegion], 0)
107108
re.Len(regionStats.offlineStats[ExtraPeer], 1)
108109
re.Empty(regionStats.offlineStats[MissPeer])
109110
re.Len(regionStats.offlineStats[DownPeer], 1)

tests/pdctl/scheduler/scheduler_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/stretchr/testify/require"
2525
"github.com/tikv/pd/pkg/testutil"
2626
"github.com/tikv/pd/server/config"
27+
"github.com/tikv/pd/server/core"
2728
"github.com/tikv/pd/server/versioninfo"
2829
"github.com/tikv/pd/tests"
2930
"github.com/tikv/pd/tests/pdctl"
@@ -128,7 +129,8 @@ func TestScheduler(t *testing.T) {
128129
pdctl.MustPutStore(re, leaderServer.GetServer(), store)
129130
}
130131

131-
pdctl.MustPutRegion(re, cluster, 1, 1, []byte("a"), []byte("b"))
132+
// note: because pdqsort is a unstable sort algorithm, set ApproximateSize for this region.
133+
pdctl.MustPutRegion(re, cluster, 1, 1, []byte("a"), []byte("b"), core.SetApproximateSize(10))
132134
time.Sleep(3 * time.Second)
133135

134136
// scheduler show command

0 commit comments

Comments
 (0)