Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/pingcap/kvproto => github.com/liyishuai/kvproto v0.0.0-20260510081044-bfe73946ca6d
4 changes: 2 additions & 2 deletions client/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3x
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/liyishuai/kvproto v0.0.0-20260510081044-bfe73946ca6d h1:XAIUcuLO83ycbBL2/V2nyBH9YCfr7nsgGiDsMGtM7LY=
github.com/liyishuai/kvproto v0.0.0-20260510081044-bfe73946ca6d/go.mod h1:z6+aAHB7dBkA+LyinEX+48/ImRJ3jag0Hg0c7wkhEvE=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
Expand All @@ -53,8 +55,6 @@ github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c h1:xpW9bvK+HuuTm
github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c/go.mod h1:X2r9ueLEUZgtx2cIogM0v4Zj5uvvzhuuiu7Pn8HzMPg=
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 h1:tdMsjOqUR7YXHoBitzdebTvOjs/swniBTOLy5XiMtuE=
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86/go.mod h1:exzhVYca3WRtd6gclGNErRWb1qEgff3LYta0LvRmON4=
github.com/pingcap/kvproto v0.0.0-20260326084500-678ff92b1edd h1:FA2DzGly3tuBWFjktkJxmqeOVEqgrsUvKMQXAw9xvWE=
github.com/pingcap/kvproto v0.0.0-20260326084500-678ff92b1edd/go.mod h1:z6+aAHB7dBkA+LyinEX+48/ImRJ3jag0Hg0c7wkhEvE=
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 h1:HR/ylkkLmGdSSDaD8IDP+SZrdhV1Kibl9KrHxJ9eciw=
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,5 @@ require (
// which will cause several different tests to fail. So this is a temporary workaround to use the old version of `testify`.
// TODO: fix those flasky tests introduced by the behavior change of `Eventually` and `EventuallyWithT` assertions.
replace github.com/stretchr/testify => github.com/stretchr/testify v1.10.0

replace github.com/pingcap/kvproto => github.com/liyishuai/kvproto v0.0.0-20260510081044-bfe73946ca6d
1,749 changes: 1,740 additions & 9 deletions go.sum

Large diffs are not rendered by default.

66 changes: 42 additions & 24 deletions pkg/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -222,37 +223,37 @@ type RegionHeartbeatRequest interface {
GetInterval() *pdpb.TimeInterval
GetQueryStats() *pdpb.QueryStats
GetApproximateSize() uint64
GetApproximateSizeKb() uint64
GetApproximateKeys() uint64
GetBucketMeta() *metapb.BucketMeta
}
Comment on lines 225 to 229
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Locate the kvproto version actually referenced and inspect for GetApproximateSizeKb.
rg -nP 'pingcap/kvproto' go.mod go.sum 2>/dev/null | head -50
echo "---"
fd -t f 'pdpb.pb.go' --exec rg -nP 'GetApproximateSizeKb|ApproximateSizeKb' {}
echo "---"
# If a replace directive points to a local path, surface it
rg -nP '^replace\s+.*kvproto' go.mod

Repository: tikv/pd

Length of output: 392


🏁 Script executed:

# First, let's see the full interface definition around lines 224-228
sed -n '200,240p' pkg/core/region.go

Repository: tikv/pd

Length of output: 1459


🏁 Script executed:

# Search for all implementations of RegionHeartbeatRequest interface
rg -n 'type.*RegionHeartbeatRequest' --glob '*.go'

Repository: tikv/pd

Length of output: 114


🏁 Script executed:

# Check if GetApproximateSizeKb exists elsewhere in the codebase
rg -n 'GetApproximateSizeKb' --glob '*.go'

Repository: tikv/pd

Length of output: 399


🏁 Script executed:

# Verify the pinned kvproto version's actual capabilities by checking imports and usage
rg -n 'pdpb\.' pkg/core/region.go | head -20

Repository: tikv/pd

Length of output: 1155


🏁 Script executed:

# Check if pdpb.RegionHeartbeatRequest is used and if it has the method
rg -n 'RegionHeartbeatRequest' pkg/core/region.go -A 3 -B 3

Repository: tikv/pd

Length of output: 1030


🏁 Script executed:

# Look for GetApproximateSizeKb in vendor or other locations
find . -name '*.go' -type f -exec grep -l 'GetApproximateSizeKb' {} \;

Repository: tikv/pd

Length of output: 72


🏁 Script executed:

# Check what type satisfies the RegionHeartbeatRequest interface
rg -n 'func.*GetApproximateSizeKb' --glob '*.go'

Repository: tikv/pd

Length of output: 33


🏁 Script executed:

# Check the actual pdpb imports and see what version is being used
grep -n 'github.com/pingcap/kvproto' go.mod | head -5

Repository: tikv/pd

Length of output: 285


🏁 Script executed:

# Search for any file that implements the RegionHeartbeatRequest interface
rg -n 'GetApproximateSize\(' --glob '*.go' -B 5 -A 2 | head -40

Repository: tikv/pd

Length of output: 3323


🏁 Script executed:

# Check if there's a wrapper or adapter that implements GetApproximateSizeKb
rg -n 'type.*struct' pkg/core/region.go | grep -i heartbeat

Repository: tikv/pd

Length of output: 33


🏁 Script executed:

# Look at what methods pdpb.RegionHeartbeatRequest actually has
rg -n 'GetApproximate' --glob '*.go'

Repository: tikv/pd

Length of output: 9543


🏁 Script executed:

# Check if RegionInfo implements GetApproximateSizeKb
sed -n '700,750p' pkg/core/region.go

Repository: tikv/pd

Length of output: 1689


🏁 Script executed:

# Look for the actual implementation of GetApproximateSizeKb
rg -n 'func.*GetApproximateSizeKb' --glob '*.go' -A 3

Repository: tikv/pd

Length of output: 33


🏁 Script executed:

# Check the response/region.go file to see how ApproximateSizeKb is set
sed -n '170,195p' pkg/response/region.go

Repository: tikv/pd

Length of output: 1019


🏁 Script executed:

# Confirm RegionInfo is supposed to implement RegionHeartbeatRequest
grep -n 'var.*RegionHeartbeatRequest' pkg/core/region.go

Repository: tikv/pd

Length of output: 33


🏁 Script executed:

# Check if there's a type assertion or conversion that might implement the interface
rg -n 'RegionHeartbeatRequest' server/ --glob '*.go' | head -20

Repository: tikv/pd

Length of output: 492


🏁 Script executed:

# Check if the kvproto version actually has GetApproximateSizeKb by looking at vendored protobuf
find . -path '*/vendor/*kvproto*' -name '*.pb.go' -type f | grep pdpb

Repository: tikv/pd

Length of output: 33


🏁 Script executed:

# Try to see what methods pdpb.RegionHeartbeatRequest actually has from vendor
fd -t f 'pdpb.pb.go' --path '*/vendor/*' -exec grep -n 'func.*RegionHeartbeatRequest' {} + 2>/dev/null | head -30

Repository: tikv/pd

Length of output: 33


🏁 Script executed:

# Check all Go files for implementations of RegionHeartbeatRequest interface
rg -n 'func.*\(\s*.*\)\s+GetApproximateSizeKb' --glob '*.go'

Repository: tikv/pd

Length of output: 33


🏁 Script executed:

# Look at pdpb package directly to see what's available
rg -n 'type RegionHeartbeatRequest' --glob '*.go' -A 50 | head -100

Repository: tikv/pd

Length of output: 3164


🏁 Script executed:

# Check if there's a mock or test implementation
rg -n 'GetApproximateSizeKb' --glob '*.go' -B 5 | head -50

Repository: tikv/pd

Length of output: 1223


🏁 Script executed:

# Check if there are any mock implementations or test doubles of RegionHeartbeatRequest
rg -n 'type.*struct.*implement' pkg/ --glob '*.go' -i heartbeat

Repository: tikv/pd

Length of output: 101


🏁 Script executed:

# Verify: does the code even compile with GetApproximateSizeKb in the interface?
# Check if there are compilation guard comments or build tags
rg -n 'build.*tag|// +build' pkg/core/region.go

Repository: tikv/pd

Length of output: 33


🏁 Script executed:

# Final check: look at git history or comments around line 225 to understand the intent
sed -n '210,230p' pkg/core/region.go

Repository: tikv/pd

Length of output: 663


Confirm kvproto coordination before merging: GetApproximateSizeKb() added to interface but not implemented.

Adding GetApproximateSizeKb() to the RegionHeartbeatRequest interface breaks compatibility. The pinned kvproto version (v0.0.0-20260326084500-678ff92b1edd) does not provide this method on pdpb.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 in go.mod before merging.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/core/region.go` around lines 224 - 228, The new interface method
GetApproximateSizeKb() was added but pdpb.RegionHeartbeatRequest in the pinned
kvproto does not implement it, causing runtime breakage; either revert/remove
GetApproximateSizeKb() from the interface or update dependencies and
implementations: confirm the coordinated kvproto change is released, update
go.mod to the kvproto version that adds GetApproximateSizeKb, and implement the
method on any types adhering to the interface (e.g., ensure
pdpb.RegionHeartbeatRequest has the corresponding accessor) or add a
compatibility shim/guard around calls
(RegionHeartbeatRequest.GetApproximateSizeKb) until the new kvproto is pinned.


// 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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Include KB-size delta in cache-update trigger logic.

After adding approximateSizeKb, region update equivalence still only checks GetApproximateSize() later in GenerateRegionGuideFunc (Line 1006). Sub-MiB size changes won’t trigger saveCache, so KB-based empty/small-region logic can stay stale.

🔧 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
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/core/region.go` around lines 236 - 257, The region cache-update logic
fails to consider the new approximateSizeKb field so sub-MiB changes won't
trigger saveCache; update the equivalence check inside GenerateRegionGuideFunc
to compare region.approximateSizeKb (or heartbeat.GetApproximateSizeKb()) in
addition to the existing GetApproximateSize() comparison so any KB-level delta
triggers a cache update; locate the comparison code in GenerateRegionGuideFunc
and include approximateSizeKb in the equality/dirty determination (using the
same fallback conversion you already set when constructing RegionInfo) so small
size changes will cause saveCache to run.


// scheduling service doesn't need the following fields.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 17 additions & 1 deletion pkg/core/region_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,19 @@ func AddQueryStats(v *pdpb.QueryStats) RegionCreateOption {
}
}

// SetApproximateSize sets the approximate size for the region.
// SetApproximateSize sets the approximate size (in MB) for the region.
func SetApproximateSize(v int64) RegionCreateOption {
return func(region *RegionInfo) {
region.approximateSize = v
region.approximateSizeKb = v * 1024
}
}

// SetApproximateSizeKb sets the approximate size (in KB) for the region.
func SetApproximateSizeKb(v int64) RegionCreateOption {
return func(region *RegionInfo) {
region.approximateSize = v / 1024
region.approximateSizeKb = v
}
}

Expand All @@ -308,6 +317,13 @@ func SetApproximateKvSize(v int64) RegionCreateOption {
}
}

// SetApproximateColumnarKvSize sets the approximate size for the region.
func SetApproximateColumnarKvSize(v int64) RegionCreateOption {
return func(region *RegionInfo) {
region.approximateColumnarKvSize = v
}
}

// SetApproximateKeys sets the approximate keys for the region.
func SetApproximateKeys(v int64) RegionCreateOption {
return func(region *RegionInfo) {
Expand Down
66 changes: 66 additions & 0 deletions pkg/core/region_size_test.go
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())
}
7 changes: 5 additions & 2 deletions pkg/core/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestInherit(t *testing.T) {
size uint64
expect uint64
}{
{false, 0, 0, 1},
{false, 0, 0, 0},
{false, 0, 2, 2},
{true, 0, 2, 2},
{true, 1, 2, 2},
Expand All @@ -221,9 +221,12 @@ func TestInherit(t *testing.T) {
}
r := NewRegionInfo(&metapb.Region{Id: 100}, nil)
r.approximateSize = int64(testCase.size)
r.approximateKeys = 1
r.approximateKeys = 0
r.Inherit(origin, false)
re.Equal(int64(testCase.expect), r.approximateSize)
if testCase.expect == 0 && testCase.size == 0 && !testCase.originExists {
re.Equal(int64(1), r.GetApproximateSizeKb())
}
}

// case for approximateKeys
Expand Down
10 changes: 10 additions & 0 deletions pkg/core/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Get*Size() and Get*SizeKb() currently expose the same value despite different unit contracts.

GetLeaderSize/GetLeaderSizeKb and GetRegionSize/GetRegionSizeKb all return the same backing fields unchanged. That makes MiB and KiB outputs indistinguishable and will misreport at least one API field.

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 size_kb == size * 1024 for non-zero values.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/core/store.go` around lines 408 - 421, The getters currently return
identical values for different unit contracts; pick a canonical internal unit
(e.g., store leaderSize and regionSize in KiB or in MiB) and update the
complementary accessors to convert units accordingly: adjust either
GetLeaderSize/GetRegionSize to return the canonical unit (e.g., bytes or KiB)
and change GetLeaderSizeKb/GetRegionSizeKb to return the converted value (or
vice‑versa) so GetLeaderSizeKb returns GetLeaderSize()*1024 (or
GetLeaderSize()/1024 depending on your canonical choice); update the StoreInfo
fields usage accordingly and add a regression test asserting for non‑zero values
that size_kb == size * 1024 (use the same functions
GetLeaderSize/GetLeaderSizeKb and GetRegionSize/GetRegionSizeKb in the test).


// GetPendingPeerCount returns the pending peer count of the store.
func (s *StoreInfo) GetPendingPeerCount() int {
return s.pendingPeerCount
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/storelimit/store_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

const (
// SmallRegionThreshold is used to represent a region which can be regarded as a small region once the size is small than it.
SmallRegionThreshold int64 = 20
SmallRegionThreshold int64 = 20 * 1024
// Unlimited is used to control the store limit. Here uses a big enough number to represent unlimited.
Unlimited = float64(100000000)
// influence is the influence of a normal region.
Expand Down
2 changes: 2 additions & 0 deletions pkg/response/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ type RegionInfo struct {
WrittenKeys uint64 `json:"written_keys"`
ReadKeys uint64 `json:"read_keys"`
ApproximateSize int64 `json:"approximate_size"`
ApproximateSizeKb int64 `json:"approximate_size_kb"`
ApproximateKeys int64 `json:"approximate_keys"`
ApproximateKvSize int64 `json:"approximate_kv_size"`
ApproximateColumnarKvSize int64 `json:"approximate_columnar_kv_size"`
Expand Down Expand Up @@ -177,6 +178,7 @@ func InitRegion(r *core.RegionInfo, s *RegionInfo) *RegionInfo {
s.ReadBytes = r.GetBytesRead()
s.ReadKeys = r.GetKeysRead()
s.ApproximateSize = r.GetApproximateSize()
s.ApproximateSizeKb = r.GetApproximateSizeKb()
s.ApproximateKeys = r.GetApproximateKeys()
s.ApproximateKvSize = r.GetApproximateKvSize()
s.ApproximateColumnarKvSize = r.GetApproximateColumnarKvSize()
Expand Down
11 changes: 11 additions & 0 deletions pkg/response/region_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading