Skip to content

Commit

Permalink
fix comment: update save(conf) to save()
Browse files Browse the repository at this point in the history
Signed-off-by: okJiang <819421878@qq.com>
  • Loading branch information
okJiang committed Aug 13, 2024
1 parent 5832835 commit 96ef87e
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 42 deletions.
2 changes: 1 addition & 1 deletion pkg/schedule/schedulers/balance_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (conf *balanceLeaderSchedulerConfig) update(data []byte) (int, any) {
}
return http.StatusBadRequest, "invalid batch size which should be an integer between 1 and 10"
}
if err := conf.save(conf); err != nil {
if err := conf.save(); err != nil {
log.Warn("failed to save balance-leader-scheduler config", errs.ZapError(err))
}
log.Info("balance-leader-scheduler config is updated", zap.ByteString("old", oldConfig), zap.ByteString("new", newConfig))
Expand Down
2 changes: 1 addition & 1 deletion pkg/schedule/schedulers/balance_witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (conf *balanceWitnessSchedulerConfig) update(data []byte) (int, any) {
}
return http.StatusBadRequest, "invalid batch size which should be an integer between 1 and 10"
}
if err := conf.save(conf); err != nil {
if err := conf.save(); err != nil {
log.Warn("failed to persist config", zap.Error(err))
}
log.Info("balance-witness-scheduler config is updated", zap.ByteString("old", oldc), zap.ByteString("new", newc))
Expand Down
14 changes: 9 additions & 5 deletions pkg/schedule/schedulers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,27 @@ import (
)

type schedulerConfig interface {
save(any) error
save() error
load(any) error
init(name string, storage endpoint.ConfigStorage)
init(name string, storage endpoint.ConfigStorage, data any)
}

type baseSchedulerConfig struct {
name string
storage endpoint.ConfigStorage

// data is the config of the scheduler.
data any
}

func (b *baseSchedulerConfig) init(name string, storage endpoint.ConfigStorage) {
func (b *baseSchedulerConfig) init(name string, storage endpoint.ConfigStorage, data any) {
b.name = name
b.storage = storage
b.data = data
}

func (b *baseSchedulerConfig) save(v any) error {
data, err := EncodeConfig(v)
func (b *baseSchedulerConfig) save() error {
data, err := EncodeConfig(b.data)
failpoint.Inject("persistFail", func() {
err = errors.New("fail to persist")
})
Expand Down
20 changes: 12 additions & 8 deletions pkg/schedule/schedulers/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,28 @@ import (

func TestSchedulerConfig(t *testing.T) {
s := storage.NewStorageWithMemoryBackend()
cfg := &baseSchedulerConfig{}
cfg.init("test", s)

type testConfig struct {
schedulerConfig
Value string `json:"value"`
}

tc := &testConfig{
Value: "test",
cfg := &testConfig{
schedulerConfig: &baseSchedulerConfig{},
}
require.NoError(t, cfg.save(tc))
cfg.init("test", s, cfg)

cfg.Value = "test"
require.NoError(t, cfg.save())
newTc := &testConfig{}
require.NoError(t, cfg.load(newTc))
require.Equal(t, tc.Value, newTc.Value)
require.Equal(t, cfg.Value, newTc.Value)

// config with another name cannot loaded the previous config
cfg2 := &baseSchedulerConfig{}
cfg2.init("test2", s)
cfg2 := &testConfig{
schedulerConfig: &baseSchedulerConfig{},
}
cfg2.init("test2", s, cfg2)
// report error because the config is empty and cannot be decoded
require.Error(t, cfg2.load(newTc))
}
4 changes: 2 additions & 2 deletions pkg/schedule/schedulers/evict_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (conf *evictLeaderSchedulerConfig) update(id uint64, newRanges []core.KeyRa
conf.StoreIDWithRanges[id] = newRanges
}
conf.Batch = batch
err := conf.save(conf)
err := conf.save()
if err != nil && id != 0 {
_, _ = conf.removeStoreLocked(id)
}
Expand All @@ -201,7 +201,7 @@ func (conf *evictLeaderSchedulerConfig) delete(id uint64) (any, error) {
}

keyRanges := conf.StoreIDWithRanges[id]
err = conf.save(conf)
err = conf.save()
if err != nil {
conf.resetStoreLocked(id, keyRanges)
conf.Unlock()
Expand Down
6 changes: 3 additions & 3 deletions pkg/schedule/schedulers/evict_slow_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (conf *evictSlowStoreSchedulerConfig) setStoreAndPersist(id uint64) error {
defer conf.Unlock()
conf.EvictedStores = []uint64{id}
conf.lastSlowStoreCaptureTS = time.Now()
return conf.save(conf)
return conf.save()
}

func (conf *evictSlowStoreSchedulerConfig) clearAndPersist() (oldID uint64, err error) {
Expand All @@ -120,7 +120,7 @@ func (conf *evictSlowStoreSchedulerConfig) clearAndPersist() (oldID uint64, err
if oldID > 0 {
conf.EvictedStores = []uint64{}
conf.lastSlowStoreCaptureTS = time.Time{}
err = conf.save(conf)
err = conf.save()
}
return
}
Expand Down Expand Up @@ -158,7 +158,7 @@ func (handler *evictSlowStoreHandler) updateConfig(w http.ResponseWriter, r *htt
prevRecoveryDurationGap := conf.RecoveryDurationGap
recoveryDurationGap := uint64(recoveryDurationGapFloat)
conf.RecoveryDurationGap = recoveryDurationGap
if err := conf.save(conf); err != nil {
if err := conf.save(); err != nil {
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
conf.RecoveryDurationGap = prevRecoveryDurationGap
return
Expand Down
6 changes: 3 additions & 3 deletions pkg/schedule/schedulers/evict_slow_trend.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (conf *evictSlowTrendSchedulerConfig) setStoreAndPersist(id uint64) error {
conf.Lock()
defer conf.Unlock()
conf.EvictedStores = []uint64{id}
return conf.save(conf)
return conf.save()
}

func (conf *evictSlowTrendSchedulerConfig) clearAndPersist(cluster sche.SchedulerCluster) (oldID uint64, err error) {
Expand All @@ -211,7 +211,7 @@ func (conf *evictSlowTrendSchedulerConfig) clearAndPersist(cluster sche.Schedule
conf.Lock()
defer conf.Unlock()
conf.EvictedStores = []uint64{}
return oldID, conf.save(conf)
return oldID, conf.save()
}

type evictSlowTrendHandler struct {
Expand Down Expand Up @@ -246,7 +246,7 @@ func (handler *evictSlowTrendHandler) updateConfig(w http.ResponseWriter, r *htt
prevRecoveryDurationGap := conf.RecoveryDurationGap
recoveryDurationGap := uint64(recoveryDurationGapFloat)
conf.RecoveryDurationGap = recoveryDurationGap
if err := conf.save(conf); err != nil {
if err := conf.save(); err != nil {
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
conf.RecoveryDurationGap = prevRecoveryDurationGap
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/schedule/schedulers/grant_hot_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (conf *grantHotRegionSchedulerConfig) clone() *grantHotRegionSchedulerConfi
func (conf *grantHotRegionSchedulerConfig) persist() error {
conf.RLock()
defer conf.RUnlock()
return conf.save(conf)
return conf.save()
}

func (conf *grantHotRegionSchedulerConfig) has(storeID uint64) bool {
Expand Down
2 changes: 1 addition & 1 deletion pkg/schedule/schedulers/grant_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (conf *grantLeaderSchedulerConfig) clone() *grantLeaderSchedulerConfig {
func (conf *grantLeaderSchedulerConfig) persist() error {
conf.RLock()
defer conf.RUnlock()
return conf.save(conf)
return conf.save()
}

func (conf *grantLeaderSchedulerConfig) getRanges(id uint64) []string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/schedule/schedulers/hot_region_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func (conf *hotRegionSchedulerConfig) handleSetConfig(w http.ResponseWriter, r *
}
newc, _ := json.Marshal(conf)
if !bytes.Equal(oldc, newc) {
if err := conf.save(conf); err != nil {
if err := conf.save(); err != nil {
log.Warn("failed to persist config", zap.Error(err))
}
log.Info("hot-region-scheduler config is updated", zap.String("old", string(oldc)), zap.String("new", string(newc)))
Expand Down
24 changes: 12 additions & 12 deletions pkg/schedule/schedulers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func schedulersRegister() {
conf.Batch = BalanceLeaderBatchSize
}
sche := newBalanceLeaderScheduler(opController, conf)
conf.init(sche.GetName(), storage)
conf.init(sche.GetName(), storage, conf)
return sche, nil
})

Expand Down Expand Up @@ -123,7 +123,7 @@ func schedulersRegister() {
conf.Batch = balanceWitnessBatchSize
}
sche := newBalanceWitnessScheduler(opController, conf)
conf.init(sche.GetName(), storage)
conf.init(sche.GetName(), storage, conf)
return sche, nil
})

Expand Down Expand Up @@ -165,7 +165,7 @@ func schedulersRegister() {
conf.cluster = opController.GetCluster()
conf.removeSchedulerCb = removeSchedulerCb[0]
sche := newEvictLeaderScheduler(opController, conf)
conf.init(sche.GetName(), storage)
conf.init(sche.GetName(), storage, conf)
return sche, nil
})

Expand All @@ -184,7 +184,7 @@ func schedulersRegister() {
}
conf.cluster = opController.GetCluster()
sche := newEvictSlowStoreScheduler(opController, conf)
conf.init(sche.GetName(), storage)
conf.init(sche.GetName(), storage, conf)
return sche, nil
})

Expand Down Expand Up @@ -230,7 +230,7 @@ func schedulersRegister() {
return nil, err
}
sche := newGrantHotRegionScheduler(opController, conf)
conf.init(sche.GetName(), storage)
conf.init(sche.GetName(), storage, conf)
return sche, nil
})

Expand Down Expand Up @@ -259,7 +259,7 @@ func schedulersRegister() {
}
}
sche := newHotScheduler(opController, conf)
conf.init(sche.GetName(), storage)
conf.init(sche.GetName(), storage, conf)
return sche, nil
})

Expand Down Expand Up @@ -300,7 +300,7 @@ func schedulersRegister() {
return nil, err
}
sche := newGrantLeaderScheduler(opController, conf)
conf.init(sche.GetName(), storage)
conf.init(sche.GetName(), storage, conf)
return sche, nil
})

Expand Down Expand Up @@ -388,7 +388,7 @@ func schedulersRegister() {
return nil, errs.ErrSchedulerConfig.FastGenByArgs("range name")
}
sche := newScatterRangeScheduler(opController, conf)
conf.init(sche.GetName(), storage)
conf.init(sche.GetName(), storage, conf)
return sche, nil
})

Expand Down Expand Up @@ -421,7 +421,7 @@ func schedulersRegister() {
return nil, err
}
sche := newShuffleHotRegionScheduler(opController, conf)
conf.init(sche.GetName(), storage)
conf.init(sche.GetName(), storage, conf)
return sche, nil
})

Expand Down Expand Up @@ -476,7 +476,7 @@ func schedulersRegister() {
return nil, err
}
sche := newShuffleRegionScheduler(opController, conf)
conf.init(sche.GetName(), storage)
conf.init(sche.GetName(), storage, conf)
return sche, nil
})

Expand All @@ -494,7 +494,7 @@ func schedulersRegister() {
return nil, err
}
sche := newSplitBucketScheduler(opController, conf)
conf.init(sche.GetName(), storage)
conf.init(sche.GetName(), storage, conf)
return sche, nil
})

Expand Down Expand Up @@ -525,7 +525,7 @@ func schedulersRegister() {
}

sche := newEvictSlowTrendScheduler(opController, conf)
conf.init(sche.GetName(), storage)
conf.init(sche.GetName(), storage, conf)
return sche, nil
})
}
2 changes: 1 addition & 1 deletion pkg/schedule/schedulers/scatter_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (conf *scatterRangeSchedulerConfig) clone() *scatterRangeSchedulerConfig {
func (conf *scatterRangeSchedulerConfig) persist() error {
conf.RLock()
defer conf.RUnlock()
return conf.save(conf)
return conf.save()
}

func (conf *scatterRangeSchedulerConfig) getRangeName() string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/schedule/schedulers/shuffle_hot_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (handler *shuffleHotRegionHandler) updateConfig(w http.ResponseWriter, r *h
defer conf.Unlock()
previous := conf.Limit
conf.Limit = uint64(limit)
err := conf.save(conf)
err := conf.save()
if err != nil {
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
conf.Limit = previous
Expand Down
2 changes: 1 addition & 1 deletion pkg/schedule/schedulers/shuffle_region_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (conf *shuffleRegionSchedulerConfig) handleSetRoles(w http.ResponseWriter,
defer conf.Unlock()
old := conf.Roles
conf.Roles = roles
if err := conf.save(conf); err != nil {
if err := conf.save(); err != nil {
conf.Roles = old // revert
rd.Text(w, http.StatusInternalServerError, err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/schedule/schedulers/split_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (h *splitBucketHandler) updateConfig(w http.ResponseWriter, r *http.Request
}
newc, _ := json.Marshal(h.conf)
if !bytes.Equal(oldc, newc) {
if err := h.conf.save(h.conf); err != nil {
if err := h.conf.save(); err != nil {
log.Warn("failed to save config", errs.ZapError(err))
}
rd.Text(w, http.StatusOK, "Config is updated.")
Expand Down

0 comments on commit 96ef87e

Please sign in to comment.