Skip to content

Commit b0207a6

Browse files
authored
Revert "statistics: fix empty region count when resuming (#7009) (#70… (#7150)
ref #7148 Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
1 parent 5b491e2 commit b0207a6

File tree

8 files changed

+53
-31
lines changed

8 files changed

+53
-31
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: 350,
140+
StorageSize: 351,
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: 200,
153+
StorageSize: 201,
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: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -843,9 +843,7 @@ func (c *RaftCluster) processRegionHeartbeat(region *core.RegionInfo) error {
843843
if err != nil {
844844
return err
845845
}
846-
if c.GetStoreConfig().IsEnableRegionBucket() {
847-
region.InheritBuckets(origin)
848-
}
846+
region.Inherit(origin, c.storeConfigManager.GetStoreConfig().IsEnableRegionBucket())
849847

850848
c.hotStat.CheckWriteAsync(statistics.NewCheckExpiredItemTask(region))
851849
c.hotStat.CheckReadAsync(statistics.NewCheckExpiredItemTask(region))

server/core/region.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,8 @@ 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.
147148
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
150149
if heartbeat.GetApproximateSize() > 0 && regionSize < EmptyRegionApproximateSize {
151150
regionSize = EmptyRegionApproximateSize
152151
}
@@ -189,9 +188,19 @@ func RegionFromHeartbeat(heartbeat *pdpb.RegionHeartbeatRequest, opts ...RegionC
189188
return region
190189
}
191190

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 {
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 {
195204
r.buckets = origin.buckets
196205
}
197206
}
@@ -469,13 +478,6 @@ func (r *RegionInfo) GetApproximateSize() int64 {
469478
return r.approximateSize
470479
}
471480

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-
479481
// GetApproximateKeys returns the approximate keys of the region.
480482
func (r *RegionInfo) GetApproximateKeys() int64 {
481483
return r.approximateKeys

server/core/region_test.go

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

189-
func TestInheritBuckets(t *testing.T) {
189+
func TestInherit(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+
}
191216

217+
// bucket
192218
data := []struct {
193219
originBuckets *metapb.Buckets
194220
buckets *metapb.Buckets
@@ -201,11 +227,12 @@ func TestInheritBuckets(t *testing.T) {
201227
for _, d := range data {
202228
origin := NewRegionInfo(&metapb.Region{Id: 100}, nil, SetBuckets(d.originBuckets))
203229
r := NewRegionInfo(&metapb.Region{Id: 100}, nil)
204-
r.InheritBuckets(origin)
230+
r.Inherit(origin, true)
205231
re.Equal(d.originBuckets, r.GetBuckets())
206232
// region will not inherit bucket keys.
207233
if origin.GetBuckets() != nil {
208234
newRegion := NewRegionInfo(&metapb.Region{Id: 100}, nil)
235+
newRegion.Inherit(origin, false)
209236
re.NotEqual(d.originBuckets, newRegion.GetBuckets())
210237
}
211238
}

server/statistics/region.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,10 @@ 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-
if !r.IsEmptyRegion() {
64-
s.StorageSize += approximateSize
65-
}
63+
s.StorageSize += approximateSize
6664
s.StorageKeys += approximateKeys
6765
leader := r.GetLeader()
6866
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.IsEmptyRegion(),
201+
EmptyRegion: region.GetApproximateSize() <= core.EmptyRegionApproximateSize,
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-
) && region.GetApproximateSize() >= core.EmptyRegionApproximateSize,
209+
),
210210
}
211211

212212
for typ, c := range conditions {

server/statistics/region_collection_test.go

Lines changed: 2 additions & 3 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], core.SetApproximateSize(1))
66+
region1 := core.NewRegionInfo(r1, peers[0])
6767
region2 := core.NewRegionInfo(r2, peers[0])
6868
regionStats := NewRegionStatistics(opt, manager, nil)
6969
regionStats.Observe(region1, stores)
@@ -103,8 +103,7 @@ 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], 0)
107-
re.Len(regionStats.stats[EmptyRegion], 0)
106+
re.Len(regionStats.stats[UndersizedRegion], 1)
108107
re.Len(regionStats.offlineStats[ExtraPeer], 1)
109108
re.Empty(regionStats.offlineStats[MissPeer])
110109
re.Len(regionStats.offlineStats[DownPeer], 1)

tests/pdctl/scheduler/scheduler_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ 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"
2827
"github.com/tikv/pd/server/versioninfo"
2928
"github.com/tikv/pd/tests"
3029
"github.com/tikv/pd/tests/pdctl"
@@ -129,8 +128,7 @@ func TestScheduler(t *testing.T) {
129128
pdctl.MustPutStore(re, leaderServer.GetServer(), store)
130129
}
131130

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))
131+
pdctl.MustPutRegion(re, cluster, 1, 1, []byte("a"), []byte("b"))
134132
time.Sleep(3 * time.Second)
135133

136134
// scheduler show command

0 commit comments

Comments
 (0)