Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
Signed-off-by: bufferflies <1045931706@qq.com>
  • Loading branch information
bufferflies committed May 19, 2022
1 parent ce6bc97 commit c51a2af
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
2 changes: 2 additions & 0 deletions pkg/keyutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ func BuildKeyRangeKey(startKey, endKey []byte) string {
return fmt.Sprintf("%s-%s", hex.EncodeToString(startKey), hex.EncodeToString(endKey))
}

// MaxKey return the bigger key for the given keys.
func MaxKey(a, b []byte) []byte {
if bytes.Compare(a, b) > 0 {
return a
}
return b
}

// MinKey returns the smaller key for the given keys.
func MinKey(a, b []byte) []byte {
if bytes.Compare(a, b) > 0 {
return b
Expand Down
29 changes: 16 additions & 13 deletions server/statistics/buckets/bucket_stat_informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ package buckets
import (
"bytes"
"fmt"
"github.com/tikv/pd/pkg/keyutil"

"github.com/tikv/pd/pkg/btree"
"github.com/tikv/pd/pkg/keyutil"
"github.com/tikv/pd/pkg/slice"
"github.com/tikv/pd/server/core"
"github.com/tikv/pd/server/statistics"
Expand All @@ -46,7 +46,7 @@ type BucketStat struct {
EndKey []byte
HotDegree int
Interval uint64
// see statistics.RegionStatKind
// the order should see statistics.RegionStatKind
Loads []uint64
}

Expand Down Expand Up @@ -100,8 +100,8 @@ func (b *BucketTreeItem) Less(than btree.Item) bool {
return bytes.Compare(b.startKey, than.(*BucketTreeItem).startKey) < 0
}

// compareKeyRange returns whether the key range is overlaps with the item.
func (b *BucketTreeItem) compareKeyRange(origin *BucketTreeItem) bool {
// equals returns whether the key range is overlaps with the item.
func (b *BucketTreeItem) equals(origin *BucketTreeItem) bool {
if origin == nil {
return false
}
Expand Down Expand Up @@ -153,20 +153,22 @@ func (b *BucketTreeItem) inherit(origins []*BucketTreeItem) {
for _, bucketTree := range origins {
oldItems = append(oldItems, bucketTree.stats...)
}
// details: https://leetcode.cn/problems/interval-list-intersections/solution/jiu-pa-ni-bu-dong-shuang-zhi-zhen-by-hyj8/
// given two list of closed intervals like newItems and oldItems, where items[i]=[start-key,end-key],
// and each item are disjoint and sorted order.
// It should calculate the value if some item has intersection.
for p1, p2 := 0, 0; p1 < len(newItems) && p2 < len(oldItems); {
newItem, oldItem := newItems[p1], oldItems[p2]
left := keyutil.MaxKey(newItem.StartKey, oldItems[p2].StartKey)
right := keyutil.MinKey(newItem.EndKey, oldItems[p2].EndKey)
left := keyutil.MaxKey(newItem.StartKey, oldItem.StartKey)
right := keyutil.MinKey(newItem.EndKey, oldItem.EndKey)

// bucket should inherit the old bucket hot degree if they have some intersection.
// skip if the left is equal to the right key, such as [10 20] [20 30].
// new bucket: |10 ---- 20 |
// old bucket: | 5 ---------15|
// they has one intersection |10-----15|.
// new bucket: |10 ---- 20 |
// old bucket: | 5 ---------15|
// they has one intersection |10--15|.
if bytes.Compare(left, right) < 0 {
oldDegree := oldItems[p2].HotDegree
newDegree := newItems[p1].HotDegree
oldDegree := oldItem.HotDegree
newDegree := newItem.HotDegree
// new bucket should interim old if the hot degree of the new bucket is less than zero.
if oldDegree < 0 && newDegree <= 0 && oldDegree < newDegree {
newItem.HotDegree = oldDegree
Expand All @@ -187,7 +189,8 @@ func (b *BucketTreeItem) inherit(origins []*BucketTreeItem) {

func (b *BucketTreeItem) calculateHotDegree() {
for _, stat := range b.stats {
// todo: qps should be considered, tikv will report this in next sprint
// todo: qps should be considered, tikv will report this in next sprint
// the order: read [bytes keys qps] and write[bytes keys qps]
readLoads := stat.Loads[:2]
readHot := slice.AllOf(readLoads, func(i int) bool {
return readLoads[i] > minHotThresholds[i]
Expand Down
14 changes: 7 additions & 7 deletions server/statistics/buckets/hot_bucket_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ package buckets
import (
"bytes"
"context"
"github.com/tikv/pd/pkg/keyutil"

"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/keyutil"
"github.com/tikv/pd/pkg/logutil"
"github.com/tikv/pd/pkg/rangetree"
"github.com/tikv/pd/server/core"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -52,7 +52,7 @@ type HotBucketCache struct {
ctx context.Context
}

// bucketDebrisFactory returns the debris.
// bucketDebrisFactory returns the debris if the key range of the item is bigger than the given key range.
// like bucket tree item: | 001------------------------200|
// the split key range: |050---150|
// returns debris: |001-----050| |150------200|
Expand Down Expand Up @@ -94,16 +94,16 @@ func NewBucketsCache(ctx context.Context) *HotBucketCache {
// putItem puts the item into the cache.
func (h *HotBucketCache) putItem(item *BucketTreeItem, overlaps []*BucketTreeItem) {
// only update origin if the key range is same.
if origin := h.bucketsOfRegion[item.regionID]; item.compareKeyRange(origin) {
if origin := h.bucketsOfRegion[item.regionID]; item.equals(origin) {
*origin = *item
return
}
for _, overlap := range overlaps {
if overlap.status == alive {
log.Debug("delete buckets from cache",
zap.Uint64("region-id", overlap.regionID),
zap.String("start-key", core.HexRegionKeyStr(overlap.GetStartKey())),
zap.String("end-key", core.HexRegionKeyStr(overlap.GetEndKey())))
logutil.ZapRedactByteString("start-key", overlap.GetStartKey()),
logutil.ZapRedactByteString("end-key", overlap.GetEndKey()))
delete(h.bucketsOfRegion, overlap.regionID)
}
}
Expand Down Expand Up @@ -139,7 +139,7 @@ func (h *HotBucketCache) schedule() {
func (h *HotBucketCache) checkBucketsFlow(buckets *metapb.Buckets) (newItem *BucketTreeItem, overlaps []*BucketTreeItem) {
newItem = convertToBucketTreeItem(buckets)
// origin is existed and the version is same.
if origin := h.bucketsOfRegion[buckets.GetRegionId()]; newItem.compareKeyRange(origin) {
if origin := h.bucketsOfRegion[buckets.GetRegionId()]; newItem.equals(origin) {
overlaps = []*BucketTreeItem{origin}
} else {
overlaps = h.getBucketsByKeyRange(newItem.startKey, newItem.endKey)
Expand Down

0 comments on commit c51a2af

Please sign in to comment.