-
Notifications
You must be signed in to change notification settings - Fork 764
scheduler: use KB-level region size granularity for improved empty region detection #10657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,7 @@ type RegionInfo struct { | |
| readBytes uint64 | ||
| readKeys uint64 | ||
| approximateSize int64 | ||
| approximateSizeKb int64 | ||
| approximateKvSize int64 // Unit: MiB | ||
| approximateColumnarKvSize int64 // Unit: MiB | ||
| approximateKeys int64 | ||
|
|
@@ -188,7 +189,7 @@ func (r *RegionInfo) rangeEqualsTo(region *RegionInfo) bool { | |
|
|
||
| const ( | ||
| // EmptyRegionApproximateSize is the region approximate size of an empty region | ||
| // (heartbeat size <= 1MB). | ||
| // (heartbeat size <= 1KB). | ||
| EmptyRegionApproximateSize = 1 | ||
| // ImpossibleFlowSize is an impossible flow size (such as written_bytes, read_keys, etc.) | ||
| // It may be caused by overflow, refer to https://github.com/tikv/pd/issues/3379. | ||
|
|
@@ -222,37 +223,37 @@ type RegionHeartbeatRequest interface { | |
| GetInterval() *pdpb.TimeInterval | ||
| GetQueryStats() *pdpb.QueryStats | ||
| GetApproximateSize() uint64 | ||
| GetApproximateSizeKb() uint64 | ||
| GetApproximateKeys() uint64 | ||
| GetBucketMeta() *metapb.BucketMeta | ||
| } | ||
|
|
||
| // RegionFromHeartbeat constructs a Region from region heartbeat. | ||
| func RegionFromHeartbeat(heartbeat RegionHeartbeatRequest, flowRoundDivisor uint64) *RegionInfo { | ||
| // Convert unit to MB. | ||
| // If region isn't empty and less than 1MB, use 1MB instead. | ||
| // The size and keys of empty region will be corrected by the previous RegionInfo. | ||
| regionSize := heartbeat.GetApproximateSize() / units.MiB | ||
| if heartbeat.GetApproximateSize() > 0 && regionSize < EmptyRegionApproximateSize { | ||
| regionSize = EmptyRegionApproximateSize | ||
| } | ||
|
|
||
| region := &RegionInfo{ | ||
| term: heartbeat.GetTerm(), | ||
| meta: heartbeat.GetRegion(), | ||
| leader: heartbeat.GetLeader(), | ||
| downPeers: heartbeat.GetDownPeers(), | ||
| pendingPeers: heartbeat.GetPendingPeers(), | ||
| writtenBytes: heartbeat.GetBytesWritten(), | ||
| writtenKeys: heartbeat.GetKeysWritten(), | ||
| readBytes: heartbeat.GetBytesRead(), | ||
| readKeys: heartbeat.GetKeysRead(), | ||
| approximateSize: int64(regionSize), | ||
| approximateKeys: int64(heartbeat.GetApproximateKeys()), | ||
| interval: heartbeat.GetInterval(), | ||
| queryStats: heartbeat.GetQueryStats(), | ||
| source: Heartbeat, | ||
| flowRoundDivisor: flowRoundDivisor, | ||
| bucketMeta: heartbeat.GetBucketMeta(), | ||
| term: heartbeat.GetTerm(), | ||
| meta: heartbeat.GetRegion(), | ||
| leader: heartbeat.GetLeader(), | ||
| downPeers: heartbeat.GetDownPeers(), | ||
| pendingPeers: heartbeat.GetPendingPeers(), | ||
| writtenBytes: heartbeat.GetBytesWritten(), | ||
| writtenKeys: heartbeat.GetKeysWritten(), | ||
| readBytes: heartbeat.GetBytesRead(), | ||
| readKeys: heartbeat.GetKeysRead(), | ||
| approximateSize: int64(regionSize), | ||
| approximateSizeKb: int64(heartbeat.GetApproximateSizeKb()), | ||
| approximateKeys: int64(heartbeat.GetApproximateKeys()), | ||
| interval: heartbeat.GetInterval(), | ||
| queryStats: heartbeat.GetQueryStats(), | ||
| source: Heartbeat, | ||
| flowRoundDivisor: flowRoundDivisor, | ||
| bucketMeta: heartbeat.GetBucketMeta(), | ||
| } | ||
| if region.approximateSizeKb == 0 { | ||
| region.approximateSizeKb = int64((heartbeat.GetApproximateSize() + 1023) / 1024) | ||
| } | ||
|
Comment on lines
236
to
257
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include KB-size delta in cache-update trigger logic. After adding 🔧 Minimal fix- if region.GetApproximateSize() != origin.GetApproximateSize() ||
+ if region.GetApproximateSize() != origin.GetApproximateSize() ||
+ region.GetApproximateSizeKb() != origin.GetApproximateSizeKb() ||
region.GetApproximateKeys() != origin.GetApproximateKeys() {
saveCache = true
return
}🤖 Prompt for AI Agents |
||
|
|
||
| // scheduling service doesn't need the following fields. | ||
|
|
@@ -296,12 +297,14 @@ func (r *RegionInfo) Inherit(origin *RegionInfo, bucketEnable bool) { | |
| // - size=1, keys=0: Truly empty region | ||
| // - size>1, keys>0: Region has data | ||
| // Ref: https://github.com/tikv/tikv/pull/19181 | ||
| if r.GetApproximateSize() == 0 { | ||
| if r.approximateSize == 0 && r.approximateSizeKb == 0 && r.GetApproximateKeys() == 0 { | ||
| if origin != nil { | ||
| r.approximateSize = origin.approximateSize | ||
| r.approximateSizeKb = origin.approximateSizeKb | ||
| r.approximateKeys = origin.approximateKeys | ||
| } else { | ||
| r.approximateSize = EmptyRegionApproximateSize | ||
| r.approximateSize = 0 | ||
| r.approximateSizeKb = EmptyRegionApproximateSize | ||
| } | ||
| } | ||
| if bucketEnable && origin != nil && atomic.LoadPointer(&r.reportBuckets) == nil { | ||
|
|
@@ -334,6 +337,7 @@ func (r *RegionInfo) Clone(opts ...RegionCreateOption) *RegionInfo { | |
| readBytes: r.readBytes, | ||
| readKeys: r.readKeys, | ||
| approximateSize: r.approximateSize, | ||
| approximateSizeKb: r.approximateSizeKb, | ||
| approximateKvSize: r.approximateKvSize, | ||
| approximateColumnarKvSize: r.approximateColumnarKvSize, | ||
| approximateKeys: r.approximateKeys, | ||
|
|
@@ -698,11 +702,25 @@ func (r *RegionInfo) GetStorePeerApproximateSize(storeID uint64) int64 { | |
| return r.approximateSize | ||
| } | ||
|
|
||
| // GetStorePeerApproximateSizeKb returns the approximate size (in KB) of the peer on the specified store. | ||
| func (r *RegionInfo) GetStorePeerApproximateSizeKb(storeID uint64) int64 { | ||
| peer := r.GetStorePeer(storeID) | ||
| if storeID != 0 && peer != nil && peer.IsWitness { | ||
| return 0 | ||
| } | ||
| return r.approximateSizeKb | ||
| } | ||
|
|
||
| // GetApproximateSize returns the approximate size of the region. | ||
| func (r *RegionInfo) GetApproximateSize() int64 { | ||
| return r.approximateSize | ||
| } | ||
|
|
||
| // GetApproximateSizeKb returns the approximate size (in KB) of the region. | ||
| func (r *RegionInfo) GetApproximateSizeKb() int64 { | ||
| return r.approximateSizeKb | ||
| } | ||
|
|
||
| // GetStorePeerApproximateKeys returns the approximate keys of the peer on the specified store. | ||
| func (r *RegionInfo) GetStorePeerApproximateKeys(storeID uint64) int64 { | ||
| peer := r.GetStorePeer(storeID) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Copyright 2026 TiKV Project Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package core | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/pingcap/kvproto/pkg/metapb" | ||
| "github.com/pingcap/kvproto/pkg/pdpb" | ||
| ) | ||
|
|
||
| func TestRegionSizeKB(t *testing.T) { | ||
| re := require.New(t) | ||
|
|
||
| // Test 1: Old heartbeat with bytes only (approximate_size = 512KB) | ||
| heartbeat := &pdpb.RegionHeartbeatRequest{ | ||
| Region: &metapb.Region{Id: 1}, | ||
| ApproximateSize: 512 * 1024, | ||
| } | ||
| region := RegionFromHeartbeat(heartbeat, 0) | ||
| // approximateSize (MiB) should be 0 | ||
| re.Equal(int64(0), region.GetApproximateSize()) | ||
| // approximateSizeKb (KiB) should be 512 | ||
| re.Equal(int64(512), region.GetApproximateSizeKb()) | ||
|
|
||
| // Test 2: New heartbeat with approximate_size_kb = 512 | ||
| heartbeat = &pdpb.RegionHeartbeatRequest{ | ||
| Region: &metapb.Region{Id: 1}, | ||
| ApproximateSizeKb: 512, | ||
| } | ||
| region = RegionFromHeartbeat(heartbeat, 0) | ||
| re.Equal(int64(0), region.GetApproximateSize()) | ||
| re.Equal(int64(512), region.GetApproximateSizeKb()) | ||
|
|
||
| // Test 3: Region size < 1 KiB should be floored to 1 KiB (EmptyRegionApproximateSize) | ||
| heartbeat = &pdpb.RegionHeartbeatRequest{ | ||
| Region: &metapb.Region{Id: 1}, | ||
| ApproximateSize: 100, // 100 bytes | ||
| } | ||
| region = RegionFromHeartbeat(heartbeat, 0) | ||
| re.Equal(int64(0), region.GetApproximateSize()) | ||
| re.Equal(int64(1), region.GetApproximateSizeKb()) | ||
|
|
||
| // Test 4: Truly empty region (size=0) should stay 0 | ||
| heartbeat = &pdpb.RegionHeartbeatRequest{ | ||
| Region: &metapb.Region{Id: 1}, | ||
| ApproximateSize: 0, | ||
| } | ||
| region = RegionFromHeartbeat(heartbeat, 0) | ||
| re.Equal(int64(0), region.GetApproximateSize()) | ||
| re.Equal(int64(0), region.GetApproximateSizeKb()) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -405,11 +405,21 @@ func (s *StoreInfo) GetLeaderSize() int64 { | |
| return s.leaderSize | ||
| } | ||
|
|
||
| // GetLeaderSizeKb returns the leader size (in KB) of the store. | ||
| func (s *StoreInfo) GetLeaderSizeKb() int64 { | ||
| return s.leaderSize | ||
| } | ||
|
|
||
| // GetRegionSize returns the Region size of the store. | ||
| func (s *StoreInfo) GetRegionSize() int64 { | ||
| return s.regionSize | ||
| } | ||
|
|
||
| // GetRegionSizeKb returns the Region size (in KB) of the store. | ||
| func (s *StoreInfo) GetRegionSizeKb() int64 { | ||
| return s.regionSize | ||
| } | ||
|
Comment on lines
+408
to
+421
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Please make one unit canonical internally and convert in the other accessor family (or convert at the API boundary), then add a regression test asserting 🤖 Prompt for AI Agents |
||
|
|
||
| // GetPendingPeerCount returns the pending peer count of the store. | ||
| func (s *StoreInfo) GetPendingPeerCount() int { | ||
| return s.pendingPeerCount | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: tikv/pd
Length of output: 392
🏁 Script executed:
Repository: tikv/pd
Length of output: 1459
🏁 Script executed:
Repository: tikv/pd
Length of output: 114
🏁 Script executed:
Repository: tikv/pd
Length of output: 399
🏁 Script executed:
Repository: tikv/pd
Length of output: 1155
🏁 Script executed:
Repository: tikv/pd
Length of output: 1030
🏁 Script executed:
Repository: tikv/pd
Length of output: 72
🏁 Script executed:
Repository: tikv/pd
Length of output: 33
🏁 Script executed:
Repository: tikv/pd
Length of output: 285
🏁 Script executed:
Repository: tikv/pd
Length of output: 3323
🏁 Script executed:
Repository: tikv/pd
Length of output: 33
🏁 Script executed:
Repository: tikv/pd
Length of output: 9543
🏁 Script executed:
Repository: tikv/pd
Length of output: 1689
🏁 Script executed:
Repository: tikv/pd
Length of output: 33
🏁 Script executed:
Repository: tikv/pd
Length of output: 1019
🏁 Script executed:
Repository: tikv/pd
Length of output: 33
🏁 Script executed:
Repository: tikv/pd
Length of output: 492
🏁 Script executed:
Repository: tikv/pd
Length of output: 33
🏁 Script executed:
Repository: tikv/pd
Length of output: 33
🏁 Script executed:
Repository: tikv/pd
Length of output: 33
🏁 Script executed:
Repository: tikv/pd
Length of output: 3164
🏁 Script executed:
Repository: tikv/pd
Length of output: 1223
🏁 Script executed:
Repository: tikv/pd
Length of output: 101
🏁 Script executed:
Repository: tikv/pd
Length of output: 33
🏁 Script executed:
Repository: tikv/pd
Length of output: 663
Confirm kvproto coordination before merging:
GetApproximateSizeKb()added to interface but not implemented.Adding
GetApproximateSizeKb()to theRegionHeartbeatRequestinterface breaks compatibility. The pinned kvproto version (v0.0.0-20260326084500-678ff92b1edd) does not provide this method onpdpb.RegionHeartbeatRequest. The code at lines 236–241 calls this method, which will fail at runtime with the current kvproto version. Verify that the coordinated kvproto change has been released and pin the updated version ingo.modbefore merging.🤖 Prompt for AI Agents