From af174e6401299e0d2ac8ef72a5f4beb48be535e2 Mon Sep 17 00:00:00 2001 From: tomato <38561029+qidi1@users.noreply.github.com> Date: Mon, 20 Dec 2021 17:53:46 +0800 Subject: [PATCH] core:fix style of hot history region info of startkey and endkey,fix bug isleader always false (#4450) bugfix:change the style of data store in levledb bugfix:remove omit empty ref #4449 Signed-off-by: qidi1 <1083369179@qq.com> Co-authored-by: Ti Chi Robot --- conf/config.toml | 2 +- server/core/hot_region_storage.go | 43 +++++++++++++------------- server/core/hot_region_storage_test.go | 22 +++++++++++-- server/handler.go | 11 ++++--- 4 files changed, 47 insertions(+), 31 deletions(-) diff --git a/conf/config.toml b/conf/config.toml index 9cf246eb5a2..252d5a2938e 100644 --- a/conf/config.toml +++ b/conf/config.toml @@ -119,7 +119,7 @@ ## Controls the time interval between write hot regions info into leveldb # hot-regions-write-interval= "10m" ## The day of hot regions data to be reserved. 0 means close. -# hot-regions-reserved-days= "7" +# hot-regions-reserved-days= 7 ## The number of Leader scheduling tasks performed at the same time. # leader-schedule-limit = 4 ## The number of Region scheduling tasks performed at the same time. diff --git a/server/core/hot_region_storage.go b/server/core/hot_region_storage.go index 0e4289fd432..4800275eb28 100644 --- a/server/core/hot_region_storage.go +++ b/server/core/hot_region_storage.go @@ -62,19 +62,19 @@ type HistoryHotRegions struct { // HistoryHotRegion wraps hot region info // it is storage format of hot_region_storage type HistoryHotRegion struct { - UpdateTime int64 `json:"update_time,omitempty"` - RegionID uint64 `json:"region_id,omitempty"` - PeerID uint64 `json:"peer_id,omitempty"` - StoreID uint64 `json:"store_id,omitempty"` - IsLeader bool `json:"is_leader,omitempty"` - IsLearner bool `json:"is_learner,omitempty"` - HotRegionType string `json:"hot_region_type,omitempty"` - HotDegree int64 `json:"hot_degree,omitempty"` - FlowBytes float64 `json:"flow_bytes,omitempty"` - KeyRate float64 `json:"key_rate,omitempty"` - QueryRate float64 `json:"query_rate,omitempty"` - StartKey []byte `json:"start_key,omitempty"` - EndKey []byte `json:"end_key,omitempty"` + UpdateTime int64 `json:"update_time"` + RegionID uint64 `json:"region_id"` + PeerID uint64 `json:"peer_id"` + StoreID uint64 `json:"store_id"` + IsLeader bool `json:"is_leader"` + IsLearner bool `json:"is_learner"` + HotRegionType string `json:"hot_region_type"` + HotDegree int64 `json:"hot_degree"` + FlowBytes float64 `json:"flow_bytes"` + KeyRate float64 `json:"key_rate"` + QueryRate float64 `json:"query_rate"` + StartKey string `json:"start_key"` + EndKey string `json:"end_key"` // Encryption metadata for start_key and end_key. encryption_meta.iv is IV for start_key. // IV for end_key is calculated from (encryption_meta.iv + len(start_key)). // The field is only used by PD and should be ignored otherwise. @@ -254,16 +254,16 @@ func (h *HotRegionStorage) packHistoryHotRegions(historyHotRegions []HistoryHotR for i := range historyHotRegions { region := &metapb.Region{ Id: historyHotRegions[i].RegionID, - StartKey: historyHotRegions[i].StartKey, - EndKey: historyHotRegions[i].EndKey, + StartKey: HexRegionKey([]byte(historyHotRegions[i].StartKey)), + EndKey: HexRegionKey([]byte(historyHotRegions[i].EndKey)), EncryptionMeta: historyHotRegions[i].EncryptionMeta, } region, err := encryption.EncryptRegion(region, h.encryptionKeyManager) if err != nil { return err } - historyHotRegions[i].StartKey = region.StartKey - historyHotRegions[i].EndKey = region.EndKey + historyHotRegions[i].StartKey = String(region.StartKey) + historyHotRegions[i].EndKey = String(region.EndKey) key := HotRegionStorePath(hotRegionType, historyHotRegions[i].UpdateTime, historyHotRegions[i].RegionID) h.batchHotInfo[key] = &historyHotRegions[i] } @@ -338,21 +338,20 @@ func (it *HotRegionStorageIterator) Next() (*HistoryHotRegion, error) { } region := &metapb.Region{ Id: message.RegionID, - StartKey: message.StartKey, - EndKey: message.EndKey, + StartKey: []byte(message.StartKey), + EndKey: []byte(message.EndKey), EncryptionMeta: message.EncryptionMeta, } if err := encryption.DecryptRegion(region, it.encryptionKeyManager); err != nil { return nil, err } - message.StartKey = region.StartKey - message.EndKey = region.EndKey + message.StartKey = String(region.StartKey) + message.EndKey = String(region.EndKey) message.EncryptionMeta = nil return &message, nil } // HotRegionStorePath generate hot region store key for HotRegionStorage. -// TODO:find a better place to put this function. func HotRegionStorePath(hotRegionType string, updateTime int64, regionID uint64) string { return path.Join( "schedule", diff --git a/server/core/hot_region_storage_test.go b/server/core/hot_region_storage_test.go index 11dd7c641f3..62859c47881 100644 --- a/server/core/hot_region_storage_test.go +++ b/server/core/hot_region_storage_test.go @@ -15,6 +15,7 @@ package core import ( "context" + "encoding/json" "fmt" "log" "math/rand" @@ -63,8 +64,8 @@ func (m *MockPackHotRegionInfo) GenHistoryHotRegions(num int, updateTime time.Ti FlowBytes: rand.Float64() * 100, KeyRate: rand.Float64() * 100, QueryRate: rand.Float64() * 100, - StartKey: []byte(fmt.Sprintf("%20d", i)), - EndKey: []byte(fmt.Sprintf("%20d", i)), + StartKey: fmt.Sprintf("%20d", i), + EndKey: fmt.Sprintf("%20d", i), } if i%2 == 1 { m.historyHotWrites = append(m.historyHotWrites, historyHotRegion) @@ -103,20 +104,33 @@ func (t *testHotRegionStorage) TestHotRegionWrite(c *C) { RegionID: 1, StoreID: 1, HotRegionType: ReadType.String(), + StartKey: string([]byte{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0x15, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0xff, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0xfa}), + EndKey: string([]byte{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0x15, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0xff, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0xfa}), }, { UpdateTime: now.Add(10*time.Second).UnixNano() / int64(time.Millisecond), RegionID: 2, StoreID: 1, HotRegionType: ReadType.String(), + StartKey: string([]byte{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0x15, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0xff, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0xfa}), + EndKey: string([]byte{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0x15, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0xff, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0xfa}), }, { UpdateTime: now.Add(20*time.Second).UnixNano() / int64(time.Millisecond), RegionID: 3, StoreID: 1, HotRegionType: ReadType.String(), + StartKey: string([]byte{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0x83, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0xff, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0xfa}), + EndKey: string([]byte{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0x83, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0xff, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0xfa}), }, } + var copyHotRegionStorages []HistoryHotRegion + data, _ := json.Marshal(hotRegionStorages) + json.Unmarshal(data, ©HotRegionStorages) + for i, region := range hotRegionStorages { + copyHotRegionStorages[i].StartKey = region.StartKey + copyHotRegionStorages[i].EndKey = region.EndKey + } packHotRegionInfo.historyHotReads = hotRegionStorages packHotRegionInfo.historyHotWrites = []HistoryHotRegion{ { @@ -133,7 +147,9 @@ func (t *testHotRegionStorage) TestHotRegionWrite(c *C) { now.Add(40*time.Second).UnixNano()/int64(time.Millisecond)) index := 0 for next, err := iter.Next(); next != nil && err == nil; next, err = iter.Next() { - c.Assert(reflect.DeepEqual(&hotRegionStorages[index], next), IsTrue) + copyHotRegionStorages[index].StartKey = HexRegionKeyStr([]byte(copyHotRegionStorages[index].StartKey)) + copyHotRegionStorages[index].EndKey = HexRegionKeyStr([]byte(copyHotRegionStorages[index].EndKey)) + c.Assert(reflect.DeepEqual(©HotRegionStorages[index], next), IsTrue) index++ } c.Assert(err, IsNil) diff --git a/server/handler.go b/server/handler.go index 8125f26b454..df3285120b6 100644 --- a/server/handler.go +++ b/server/handler.go @@ -994,7 +994,8 @@ func (h *Handler) packHotRegions(hotPeersStat statistics.StoreHotPeersStat, hotR for _, peer := range meta.Peers { if peer.StoreId == hotPeerStat.StoreID { peerID = peer.Id - isLearner = peer.Role == metapb.PeerRole_Learner + isLearner = core.IsLearner(peer) + break } } stat := core.HistoryHotRegion{ @@ -1003,15 +1004,15 @@ func (h *Handler) packHotRegions(hotPeersStat statistics.StoreHotPeersStat, hotR RegionID: hotPeerStat.RegionID, StoreID: hotPeerStat.StoreID, PeerID: peerID, - IsLeader: meta.Id == region.GetLeader().Id, + IsLeader: peerID == region.GetLeader().Id, IsLearner: isLearner, HotDegree: int64(hotPeerStat.HotDegree), FlowBytes: hotPeerStat.ByteRate, KeyRate: hotPeerStat.KeyRate, QueryRate: hotPeerStat.QueryRate, - StartKey: meta.StartKey, - EndKey: meta.EndKey, - EncryptionMeta: meta.EncryptionMeta, + StartKey: string(region.GetStartKey()), + EndKey: string(region.GetEndKey()), + EncryptionMeta: meta.GetEncryptionMeta(), HotRegionType: hotRegionType, } historyHotRegions = append(historyHotRegions, stat)