Skip to content

Commit

Permalink
6.6: add approixmate kv size (tikv#18) (tikv#26)
Browse files Browse the repository at this point in the history
Signed-off-by: zeminzhou <zhouzemin@pingcap.com>
Signed-off-by: disksing <i@disksing.com>
  • Loading branch information
disksing authored and rleungx committed Jul 28, 2023
1 parent 816c7fc commit 12fc611
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type RegionInfo struct {
readBytes uint64
readKeys uint64
approximateSize int64
approximateKvSize int64
approximateKeys int64
interval *pdpb.TimeInterval
replicationStatus *replication_modepb.RegionReplicationStatus
Expand Down Expand Up @@ -151,6 +152,7 @@ func RegionFromHeartbeat(heartbeat *pdpb.RegionHeartbeatRequest, opts ...RegionC
if heartbeat.GetApproximateSize() > 0 && regionSize < EmptyRegionApproximateSize {
regionSize = EmptyRegionApproximateSize
}
regionKvSize := heartbeat.GetApproximateKvSize() / units.MiB

region := &RegionInfo{
term: heartbeat.GetTerm(),
Expand All @@ -164,6 +166,7 @@ func RegionFromHeartbeat(heartbeat *pdpb.RegionHeartbeatRequest, opts ...RegionC
readBytes: heartbeat.GetBytesRead(),
readKeys: heartbeat.GetKeysRead(),
approximateSize: int64(regionSize),
approximateKvSize: int64(regionKvSize),
approximateKeys: int64(heartbeat.GetApproximateKeys()),
interval: heartbeat.GetInterval(),
replicationStatus: heartbeat.GetReplicationStatus(),
Expand Down Expand Up @@ -230,6 +233,7 @@ func (r *RegionInfo) Clone(opts ...RegionCreateOption) *RegionInfo {
readBytes: r.readBytes,
readKeys: r.readKeys,
approximateSize: r.approximateSize,
approximateKvSize: r.approximateKvSize,
approximateKeys: r.approximateKeys,
interval: typeutil.DeepClone(r.interval, TimeIntervalFactory),
replicationStatus: r.replicationStatus,
Expand Down Expand Up @@ -520,6 +524,11 @@ func (r *RegionInfo) GetStorePeerApproximateKeys(storeID uint64) int64 {
return r.approximateKeys
}

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

// GetApproximateKeys returns the approximate keys of the region.
func (r *RegionInfo) GetApproximateKeys() int64 {
return r.approximateKeys
Expand Down
7 changes: 7 additions & 0 deletions pkg/core/region_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ func SetApproximateSize(v int64) RegionCreateOption {
}
}

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

// SetApproximateKeys sets the approximate keys for the region.
func SetApproximateKeys(v int64) RegionCreateOption {
return func(region *RegionInfo) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/statistics/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type RegionStats struct {
Count int `json:"count"`
EmptyCount int `json:"empty_count"`
StorageSize int64 `json:"storage_size"`
UserStorageSize int64 `json:"user_storage_size"`
StorageKeys int64 `json:"storage_keys"`
StoreLeaderCount map[uint64]int `json:"store_leader_count"`
StorePeerCount map[uint64]int `json:"store_peer_count"`
Expand Down Expand Up @@ -57,10 +58,12 @@ func (s *RegionStats) Observe(r *core.RegionInfo) {
s.Count++
approximateKeys := r.GetApproximateKeys()
approximateSize := r.GetApproximateSize()
approximateKvSize := r.GetApproximateKvSize()
if approximateSize <= core.EmptyRegionApproximateSize {
s.EmptyCount++
}
s.StorageSize += approximateSize
s.UserStorageSize += approximateKvSize
s.StorageKeys += approximateKeys
leader := r.GetLeader()
if leader != nil {
Expand Down
6 changes: 6 additions & 0 deletions server/api/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (suite *statsTestSuite) TestRegionStats() {
},
&metapb.Peer{Id: 101, StoreId: 1},
core.SetApproximateSize(100),
core.SetApproximateKvSize(80),
core.SetApproximateKeys(50),
),
core.NewRegionInfo(
Expand All @@ -90,6 +91,7 @@ func (suite *statsTestSuite) TestRegionStats() {
},
&metapb.Peer{Id: 105, StoreId: 4},
core.SetApproximateSize(200),
core.SetApproximateKvSize(180),
core.SetApproximateKeys(150),
),
core.NewRegionInfo(
Expand All @@ -105,6 +107,7 @@ func (suite *statsTestSuite) TestRegionStats() {
},
&metapb.Peer{Id: 107, StoreId: 5},
core.SetApproximateSize(1),
core.SetApproximateKvSize(1),
core.SetApproximateKeys(1),
),
core.NewRegionInfo(
Expand All @@ -119,6 +122,7 @@ func (suite *statsTestSuite) TestRegionStats() {
},
&metapb.Peer{Id: 108, StoreId: 4},
core.SetApproximateSize(50),
core.SetApproximateKvSize(30),
core.SetApproximateKeys(20),
),
}
Expand All @@ -139,6 +143,7 @@ func (suite *statsTestSuite) TestRegionStats() {
Count: 4,
EmptyCount: 1,
StorageSize: 351,
UserStorageSize: 291,
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 @@ -152,6 +157,7 @@ func (suite *statsTestSuite) TestRegionStats() {
Count: 2,
EmptyCount: 1,
StorageSize: 201,
UserStorageSize: 181,
StorageKeys: 151,
StoreLeaderCount: map[uint64]int{4: 1, 5: 1},
StorePeerCount: map[uint64]int{1: 2, 4: 1, 5: 2},
Expand Down

0 comments on commit 12fc611

Please sign in to comment.