Skip to content

Commit

Permalink
config:make config can be change
Browse files Browse the repository at this point in the history
ref #pingcap/tidb/issues/25281

Signed-off-by: qidi1 <1083369179@qq.com>
  • Loading branch information
qidi1 committed Jan 4, 2022
1 parent a939ad4 commit 4ce3370
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 29 deletions.
9 changes: 3 additions & 6 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ type ScheduleConfig struct {
HotRegionsWriteInterval typeutil.Duration `toml:"hot-regions-write-interval" json:"hot-regions-write-interval"`

// The day of hot regions data to be reserved. 0 means close.
HotRegionsReservedDays int64 `toml:"hot-regions-reserved-days" json:"hot-regions-reserved-days"`
HotRegionsReservedDays uint64 `toml:"hot-regions-reserved-days" json:"hot-regions-reserved-days"`
}

// Clone returns a cloned scheduling configuration.
Expand Down Expand Up @@ -805,6 +805,7 @@ func (c *ScheduleConfig) adjust(meta *configMetaData, reloading bool) error {
adjustDuration(&c.SplitMergeInterval, defaultSplitMergeInterval)
adjustDuration(&c.PatrolRegionInterval, defaultPatrolRegionInterval)
adjustDuration(&c.MaxStoreDownTime, defaultMaxStoreDownTime)
adjustDuration(&c.HotRegionsWriteInterval, defaultHotRegionsWriteInterval)
if !meta.IsDefined("leader-schedule-limit") {
adjustUint64(&c.LeaderScheduleLimit, defaultLeaderScheduleLimit)
}
Expand Down Expand Up @@ -868,12 +869,8 @@ func (c *ScheduleConfig) adjust(meta *configMetaData, reloading bool) error {
c.StoreLimit = make(map[uint64]StoreLimitConfig)
}

if !meta.IsDefined("hot-regions-write-interval") {
adjustDuration(&c.HotRegionsWriteInterval, defaultHotRegionsWriteInterval)
}

if !meta.IsDefined("hot-regions-reserved-days") {
adjustInt64(&c.HotRegionsReservedDays, defaultHotRegionsReservedDays)
adjustUint64(&c.HotRegionsReservedDays, defaultHotRegionsReservedDays)
}

return c.Validate()
Expand Down
10 changes: 10 additions & 0 deletions server/config/persist_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,16 @@ func (o *PersistOptions) GetSchedulers() SchedulerConfigs {
return o.GetScheduleConfig().Schedulers
}

// GetHotRegionIntervalConfig gets interval for PD to store Hot Region information..
func (o *PersistOptions) GetHotRegionIntervalConfig() time.Duration {
return o.GetScheduleConfig().HotRegionsWriteInterval.Duration
}

// GetHotRegionsReservedDaysConfig gets days hot region information is kept.
func (o *PersistOptions) GetHotRegionsReservedDaysConfig() int {
return int(o.GetScheduleConfig().HotRegionsReservedDays)
}

// AddSchedulerCfg adds the scheduler configurations.
func (o *PersistOptions) AddSchedulerCfg(tp string, args []string) {
v := o.GetScheduleConfig().Clone()
Expand Down
37 changes: 24 additions & 13 deletions server/core/hot_region_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/server/encryptionkm"
"github.com/tikv/pd/server/kv"
"go.uber.org/zap"
)

// HotRegionStorage is used to storage hot region info,
Expand All @@ -46,8 +47,6 @@ type HotRegionStorage struct {
mu sync.RWMutex
hotRegionLoopWg sync.WaitGroup
batchHotInfo map[string]*HistoryHotRegion
remianedDays int64
pullInterval time.Duration
hotRegionInfoCtx context.Context
hotRegionInfoCancel context.CancelFunc
hotRegionStorageHandler HotRegionStorageHandler
Expand Down Expand Up @@ -90,6 +89,10 @@ type HotRegionStorageHandler interface {
PackHistoryHotWriteRegions() ([]HistoryHotRegion, error)
// IsLeader return true means this server is leader.
IsLeader() bool
// GetHotRegionIntervalConfig gets interval for PD to store Hot Region information..
GetHotRegionIntervalConfig() time.Duration
// GetHotRegionsReservedDaysConfig gets days hot region information is kept.
GetHotRegionsReservedDaysConfig() int
}

const (
Expand Down Expand Up @@ -129,8 +132,6 @@ func NewHotRegionsStorage(
path string,
encryptionKeyManager *encryptionkm.KeyManager,
hotRegionStorageHandler HotRegionStorageHandler,
remianedDays int64,
pullInterval time.Duration,
) (*HotRegionStorage, error) {
levelDB, err := kv.NewLeveldbKV(path)
if err != nil {
Expand All @@ -141,17 +142,13 @@ func NewHotRegionsStorage(
LeveldbKV: levelDB,
encryptionKeyManager: encryptionKeyManager,
batchHotInfo: make(map[string]*HistoryHotRegion),
remianedDays: remianedDays,
pullInterval: pullInterval,
hotRegionInfoCtx: hotRegionInfoCtx,
hotRegionInfoCancel: hotRegionInfoCancle,
hotRegionStorageHandler: hotRegionStorageHandler,
}
if remianedDays > 0 {
h.hotRegionLoopWg.Add(2)
go h.backgroundFlush()
go h.backgroundDelete()
}
h.hotRegionLoopWg.Add(2)
go h.backgroundFlush()
go h.backgroundDelete()
return &h, nil
}

Expand Down Expand Up @@ -186,14 +183,25 @@ func (h *HotRegionStorage) backgroundDelete() {

// Write hot_region info into db in the background.
func (h *HotRegionStorage) backgroundFlush() {
ticker := time.NewTicker(h.pullInterval)
interval := h.hotRegionStorageHandler.GetHotRegionIntervalConfig()
ticker := time.NewTicker(interval)
defer func() {
ticker.Stop()
h.hotRegionLoopWg.Done()
}()
for {
select {
case <-ticker.C:
ticker.Reset(h.hotRegionStorageHandler.GetHotRegionIntervalConfig())
log.Info("HotRegionStorage", zap.Uint64("Reset Ticker",
uint64(h.hotRegionStorageHandler.GetHotRegionIntervalConfig())))
if h.hotRegionStorageHandler.GetHotRegionsReservedDaysConfig() == 0 {
log.Info("HotRegionStorage",
zap.Int("Not Flush", h.hotRegionStorageHandler.GetHotRegionsReservedDaysConfig()))
continue
}
log.Info("HotRegionStorage",
zap.Int("IS Flush", h.hotRegionStorageHandler.GetHotRegionsReservedDaysConfig()))
if h.hotRegionStorageHandler.IsLeader() {
if err := h.pullHotRegionInfo(); err != nil {
log.Error("get hot_region stat meet error", errs.ZapError(err))
Expand Down Expand Up @@ -294,8 +302,11 @@ func (h *HotRegionStorage) delete() error {
db := h.LeveldbKV
batch := new(leveldb.Batch)
for _, hotRegionType := range HotRegionTypes {
if h.hotRegionStorageHandler.GetHotRegionsReservedDaysConfig() == 0 {
continue
}
startKey := HotRegionStorePath(hotRegionType, 0, 0)
endTime := time.Now().AddDate(0, 0, 0-int(h.remianedDays)).UnixNano() / int64(time.Millisecond)
endTime := time.Now().AddDate(0, 0, 0-h.hotRegionStorageHandler.GetHotRegionsReservedDaysConfig()).UnixNano() / int64(time.Millisecond)
endKey := HotRegionStorePath(hotRegionType, endTime, math.MaxInt64)
iter := db.NewIterator(&util.Range{
Start: []byte(startKey), Limit: []byte(endKey)}, nil)
Expand Down
34 changes: 27 additions & 7 deletions server/core/hot_region_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type MockPackHotRegionInfo struct {
isLeader bool
historyHotReads []HistoryHotRegion
historyHotWrites []HistoryHotRegion
reservedDays int
pullInterval time.Duration
}

// PackHistoryHotWriteRegions get read hot region info in HistoryHotRegion from.
Expand Down Expand Up @@ -75,6 +77,22 @@ func (m *MockPackHotRegionInfo) GenHistoryHotRegions(num int, updateTime time.Ti
}
}

func (m *MockPackHotRegionInfo) GetHotRegionsReservedDaysConfig() int {
return m.reservedDays
}

func (m *MockPackHotRegionInfo) SetHotRegionsReservedDaysConfig(reservedDays int) {
m.reservedDays = reservedDays
}

func (m *MockPackHotRegionInfo) GetHotRegionIntervalConfig() time.Duration {
return m.pullInterval
}

func (m *MockPackHotRegionInfo) SetHotRegionIntervalConfig(interval time.Duration) {
m.pullInterval = interval
}

// ClearHotRegion delete all region cached.
func (m *MockPackHotRegionInfo) ClearHotRegion() {
m.historyHotReads = make([]HistoryHotRegion, 0)
Expand Down Expand Up @@ -161,7 +179,7 @@ func (t *testHotRegionStorage) TestHotRegionDelete(c *C) {
defaultDelteData := 30
deleteDate := time.Now().AddDate(0, 0, 0)
packHotRegionInfo := &MockPackHotRegionInfo{}
store, clean, err := newTestHotRegionStorage(10*time.Minute, int64(defaultReaminDay), packHotRegionInfo)
store, clean, err := newTestHotRegionStorage(10*time.Minute, defaultReaminDay, packHotRegionInfo)
c.Assert(err, IsNil)
defer clean()
historyHotRegions := make([]HistoryHotRegion, 0)
Expand Down Expand Up @@ -202,10 +220,10 @@ func BenchmarkInsert(b *testing.B) {
b.StopTimer()
}

func BenchmarkInsertAfterMonth(b *testing.B) {
func BenchmarkInsertAfterManyDays(b *testing.B) {
defaultInsertDay := 30
packHotRegionInfo := &MockPackHotRegionInfo{}
regionStorage, clear, err := newTestHotRegionStorage(10*time.Hour, int64(defaultInsertDay), packHotRegionInfo)
regionStorage, clear, err := newTestHotRegionStorage(10*time.Hour, defaultInsertDay, packHotRegionInfo)
defer clear()
if err != nil {
b.Fatal(err)
Expand All @@ -222,7 +240,7 @@ func BenchmarkDelete(b *testing.B) {
defaultInsertDay := 7
defaultReaminDay := 7
packHotRegionInfo := &MockPackHotRegionInfo{}
regionStorage, clear, err := newTestHotRegionStorage(10*time.Hour, int64(defaultReaminDay), packHotRegionInfo)
regionStorage, clear, err := newTestHotRegionStorage(10*time.Hour, defaultReaminDay, packHotRegionInfo)
defer clear()
if err != nil {
b.Fatal(err)
Expand Down Expand Up @@ -269,18 +287,20 @@ func newTestHotRegions(storage *HotRegionStorage, mock *MockPackHotRegionInfo, c
}

func newTestHotRegionStorage(pullInterval time.Duration,
remianedDays int64,
packHotRegionInfo HotRegionStorageHandler) (
reservedDays int,
packHotRegionInfo *MockPackHotRegionInfo) (
hotRegionStorage *HotRegionStorage,
clear func(), err error) {
writePath := "./tmp"
ctx := context.Background()
if err != nil {
return nil, nil, err
}
packHotRegionInfo.pullInterval = pullInterval
packHotRegionInfo.reservedDays = reservedDays
// delete data in between today and tomrrow
hotRegionStorage, err = NewHotRegionsStorage(ctx,
writePath, nil, packHotRegionInfo, remianedDays, pullInterval)
writePath, nil, packHotRegionInfo)
if err != nil {
return nil, nil, err
}
Expand Down
10 changes: 10 additions & 0 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ func (h *Handler) GetHotReadRegions() *statistics.StoreHotPeersInfos {
return c.GetHotReadRegions()
}

// GetHotRegionIntervalConfig gets interval for PD to store Hot Region information..
func (h *Handler) GetHotRegionIntervalConfig() time.Duration {
return h.opt.GetHotRegionIntervalConfig()
}

// GetHotRegionsReservedDaysConfig gets days hot region information is kept.
func (h *Handler) GetHotRegionsReservedDaysConfig() int {
return h.opt.GetHotRegionsReservedDaysConfig()
}

// GetStoresLoads gets all hot write stores stats.
func (h *Handler) GetStoresLoads() map[uint64][]float64 {
rc := h.s.GetRaftCluster()
Expand Down
4 changes: 1 addition & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,7 @@ func (s *Server) startServer(ctx context.Context) error {
// initial hot_region_storage in here.
hotRegionPath := filepath.Join(s.cfg.DataDir, "hot-region")
s.hotRegionStorage, err = core.NewHotRegionsStorage(
ctx, hotRegionPath, encryptionKeyManager, s.handler,
s.cfg.Schedule.HotRegionsReservedDays,
s.cfg.Schedule.HotRegionsWriteInterval.Duration)
ctx, hotRegionPath, encryptionKeyManager, s.handler)
if err != nil {
return err
}
Expand Down

0 comments on commit 4ce3370

Please sign in to comment.