Skip to content

Commit

Permalink
core: remove compaction for hot region history (#4236)
Browse files Browse the repository at this point in the history
* core:remove compaction for hot region history ref #pingcap/tidb/pull/25281

Signed-off-by: qidi1 <1083369179@qq.com>

* ref #4020

Signed-off-by: nolouch <nolouch@gmail.com>

Co-authored-by: nolouch <nolouch@gmail.com>
Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
  • Loading branch information
3 people authored Oct 27, 2021
1 parent e68eee4 commit ef9eb0c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 84 deletions.
14 changes: 0 additions & 14 deletions server/core/hot_region_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ type HotRegionStorage struct {
batchHotInfo map[string]*HistoryHotRegion
remianedDays int64
pullInterval time.Duration
compactionCountdown int
hotRegionInfoCtx context.Context
hotRegionInfoCancel context.CancelFunc
hotRegionStorageHandler HotRegionStorageHandler
Expand Down Expand Up @@ -94,8 +93,6 @@ type HotRegionStorageHandler interface {
}

const (
// leveldb will run compaction after 30 times delete.
defaultCompactionTime = 30
// delete will run at this o`clock.
defaultDeleteTime = 4
)
Expand Down Expand Up @@ -146,7 +143,6 @@ func NewHotRegionsStorage(
batchHotInfo: make(map[string]*HistoryHotRegion),
remianedDays: remianedDays,
pullInterval: pullInterval,
compactionCountdown: defaultCompactionTime,
hotRegionInfoCtx: hotRegionInfoCtx,
hotRegionInfoCancel: hotRegionInfoCancle,
hotRegionStorageHandler: hotRegionStorageHandler,
Expand Down Expand Up @@ -310,16 +306,6 @@ func (h *HotRegionStorage) delete() error {
if err := db.Write(batch, nil); err != nil {
return errs.ErrLevelDBWrite.Wrap(err).GenWithStackByCause()
}
h.compactionCountdown--
if h.compactionCountdown == 0 {
h.compactionCountdown = defaultCompactionTime
for _, hotRegionType := range HotRegionTypes {
startKey := HotRegionStorePath(hotRegionType, 0, 0)
endTime := time.Now().AddDate(0, 0, 0-int(h.remianedDays)).Unix()
endKey := HotRegionStorePath(hotRegionType, endTime, math.MaxInt64)
db.CompactRange(util.Range{Start: []byte(startKey), Limit: []byte(endKey)})
}
}
return nil
}

Expand Down
81 changes: 28 additions & 53 deletions server/core/hot_region_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,44 +141,35 @@ func (t *testHotRegionStorage) TestHotRegionWrite(c *C) {
}

func (t *testHotRegionStorage) TestHotRegionDelete(c *C) {
deleteDate := time.Now().AddDate(0, 0, -30)
defaultReaminDay := 7
defaultDelteData := 30
deleteDate := time.Now().AddDate(0, 0, 0)
packHotRegionInfo := &MockPackHotRegionInfo{}
store, clean, err := newTestHotRegionStorage(10*time.Minute, 1, packHotRegionInfo)
store, clean, err := newTestHotRegionStorage(10*time.Minute, int64(defaultReaminDay), packHotRegionInfo)
c.Assert(err, IsNil)
defer clean()
hotRegionStorages := []HistoryHotRegion{
{
historyHotRegions := make([]HistoryHotRegion, 0)
for i := 0; i < defaultDelteData; i++ {
historyHotRegion := HistoryHotRegion{
UpdateTime: deleteDate.UnixNano() / int64(time.Millisecond),
RegionID: 1,
StoreID: 1,
HotRegionType: ReadType.String(),
},
{
UpdateTime: deleteDate.Add(10*time.Second).UnixNano() / int64(time.Millisecond),
RegionID: 2,
StoreID: 1,
HotRegionType: ReadType.String(),
},
{
UpdateTime: time.Now().UnixNano() / int64(time.Millisecond),
RegionID: 3,
StoreID: 1,
HotRegionType: ReadType.String(),
},
}
historyHotRegions = append(historyHotRegions, historyHotRegion)
deleteDate = deleteDate.AddDate(0, 0, -1)
}
packHotRegionInfo.historyHotReads = hotRegionStorages
packHotRegionInfo.historyHotReads = historyHotRegions
store.pullHotRegionInfo()
store.flush()
store.delete()
iter := store.NewIterator(HotRegionTypes,
deleteDate.UnixNano()/int64(time.Millisecond),
time.Now().UnixNano()/int64(time.Millisecond))
next, err := iter.Next()
c.Assert(err, IsNil)
c.Assert(reflect.DeepEqual(&hotRegionStorages[2], next), IsTrue)
next, err = iter.Next()
c.Assert(next, IsNil)
c.Assert(err, IsNil)
num := 0
for next, err := iter.Next(); next != nil && err == nil; next, err = iter.Next() {
num++
c.Assert(reflect.DeepEqual(next, &historyHotRegions[defaultReaminDay-num]), IsTrue)
}
}

func BenchmarkInsert(b *testing.B) {
Expand All @@ -196,13 +187,14 @@ func BenchmarkInsert(b *testing.B) {
}

func BenchmarkInsertAfterMonth(b *testing.B) {
defaultInsertDay := 30
packHotRegionInfo := &MockPackHotRegionInfo{}
regionStorage, clear, err := newTestHotRegionStorage(10*time.Hour, 7, packHotRegionInfo)
regionStorage, clear, err := newTestHotRegionStorage(10*time.Hour, int64(defaultInsertDay), packHotRegionInfo)
defer clear()
if err != nil {
b.Fatal(err)
}
nextTime := newTestHotRegions(regionStorage, *packHotRegionInfo, 4464, 1000, time.Now())
nextTime := newTestHotRegions(regionStorage, packHotRegionInfo, 144*defaultInsertDay, 1000, time.Now())
packHotRegionInfo.GenHistoryHotRegions(1000, nextTime)
b.ResetTimer()
regionStorage.pullHotRegionInfo()
Expand All @@ -211,29 +203,31 @@ func BenchmarkInsertAfterMonth(b *testing.B) {
}

func BenchmarkDelete(b *testing.B) {
defaultInsertDay := 7
defaultReaminDay := 7
packHotRegionInfo := &MockPackHotRegionInfo{}
regionStorage, clear, err := newTestHotRegionStorage(10*time.Hour, 7, packHotRegionInfo)
regionStorage, clear, err := newTestHotRegionStorage(10*time.Hour, int64(defaultReaminDay), packHotRegionInfo)
defer clear()
if err != nil {
b.Fatal(err)
}
deleteTime := time.Now().AddDate(0, 0, -14)
newTestHotRegions(regionStorage, *packHotRegionInfo, 144*7, 1000, deleteTime)
newTestHotRegions(regionStorage, packHotRegionInfo, 144*defaultInsertDay, 1000, deleteTime)
b.ResetTimer()
regionStorage.delete()
b.StopTimer()
}

func BenchmarkRead(b *testing.B) {
packHotRegionInfo := &MockPackHotRegionInfo{}
regionStorage, clear, err := newTestHotRegionStorage(10*time.Hour, 30, packHotRegionInfo)
regionStorage, clear, err := newTestHotRegionStorage(10*time.Hour, 7, packHotRegionInfo)
if err != nil {
b.Fatal(err)
}
defer clear()
endTime := time.Now()
startTime := endTime
endTime = newTestHotRegions(regionStorage, *packHotRegionInfo, 144*7, 1000, endTime)
endTime = newTestHotRegions(regionStorage, packHotRegionInfo, 144*7, 1000, endTime)
b.ResetTimer()
iter := regionStorage.NewIterator(HotRegionTypes, startTime.UnixNano()/int64(time.Millisecond),
endTime.AddDate(0, 1, 0).UnixNano()/int64(time.Millisecond))
Expand All @@ -247,32 +241,13 @@ func BenchmarkRead(b *testing.B) {
b.StopTimer()
}

func BenchmarkCompaction(b *testing.B) {
packHotRegionInfo := &MockPackHotRegionInfo{}
regionStorage, clear, err := newTestHotRegionStorage(10*time.Hour, 30, packHotRegionInfo)
if err != nil {
b.Fatal(err)
}
defer clear()
endTime := time.Now().AddDate(0, 0, 2*defaultCompactionTime)
for i := 0; i < 2*defaultDeleteTime-1; i++ {
endTime = newTestHotRegions(regionStorage, *packHotRegionInfo, 144, 1000, endTime)
regionStorage.flush()
regionStorage.delete()
}
newTestHotRegions(regionStorage, *packHotRegionInfo, 144, 1000, endTime)
regionStorage.flush()
b.ResetTimer()
regionStorage.delete()
b.StopTimer()
}

func newTestHotRegions(storage *HotRegionStorage, mock MockPackHotRegionInfo, cycleTimes, num int, updateTime time.Time) time.Time {
func newTestHotRegions(storage *HotRegionStorage, mock *MockPackHotRegionInfo, cycleTimes, num int, updateTime time.Time) time.Time {
for i := 0; i < cycleTimes; i++ {
mock.GenHistoryHotRegions(num, updateTime)
storage.pullHotRegionInfo()
storage.flush()
updateTime.Add(10 * time.Minute)
updateTime = updateTime.Add(10 * time.Minute)
mock.ClearHotRegion()
}
return updateTime
}
Expand Down
24 changes: 7 additions & 17 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -938,34 +938,24 @@ func (h *Handler) IsLeader() bool {
}

// PackHistoryHotReadRegions get read hot region info in HistoryHotRegion form.
func (h *Handler) PackHistoryHotReadRegions() (historyHotRegions []core.HistoryHotRegion, err error) {
func (h *Handler) PackHistoryHotReadRegions() ([]core.HistoryHotRegion, error) {
hotReadRegions := h.GetHotReadRegions()
if hotReadRegions == nil {
return
return nil, nil
}
hotReadPeerRegions := hotReadRegions.AsPeer
historyPeerHotRegions, err := h.packHotRegions(hotReadPeerRegions, core.ReadType.String())
if err != nil {
return
}
historyHotRegions = append(historyHotRegions, historyPeerHotRegions...)
return
return h.packHotRegions(hotReadPeerRegions, core.ReadType.String())

}

// PackHistoryHotWriteRegions get write hot region info in HistoryHotRegion from
func (h *Handler) PackHistoryHotWriteRegions() (historyHotRegions []core.HistoryHotRegion, err error) {
func (h *Handler) PackHistoryHotWriteRegions() ([]core.HistoryHotRegion, error) {
hotWriteRegions := h.GetHotWriteRegions()
if hotWriteRegions == nil {
return
return nil, nil
}
hotWritePeerRegions := hotWriteRegions.AsPeer
historyPeerHotRegions, err := h.packHotRegions(hotWritePeerRegions, core.WriteType.String())
if err != nil {
return
}

historyHotRegions = append(historyHotRegions, historyPeerHotRegions...)
return
return h.packHotRegions(hotWritePeerRegions, core.WriteType.String())
}

func (h *Handler) packHotRegions(hotPeersStat statistics.StoreHotPeersStat, hotRegionType string) (historyHotRegions []core.HistoryHotRegion, err error) {
Expand Down

0 comments on commit ef9eb0c

Please sign in to comment.