Skip to content

Commit

Permalink
*: enable errcheck for schedulers (#8322)
Browse files Browse the repository at this point in the history
ref #1919

Signed-off-by: Ryan Leung <[email protected]>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
rleungx and ti-chi-bot[bot] authored Jun 25, 2024
1 parent 6fbe737 commit 1578f29
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,6 @@ issues:
- path: (pd-analysis|pd-api-bench|pd-backup|pd-ctl|pd-heartbeat-bench|pd-recover|pd-simulator|pd-tso-bench|pd-ut|regions-dump|stores-dump)
linters:
- errcheck
- path: (pkg/tso/admin.go|pkg/schedule/schedulers/split_bucket.go|server/api/plugin_disable.go|server/api/plugin_disable.go|server/api/operator.go|server/api/region.go|pkg/schedule/schedulers/balance_leader.go|server/api/.*\.go|pkg/replication/replication_mode.go|pkg/storage/endpoint/gc_safe_point.go|server/.*\.go|pkg/schedule/schedulers/.*\.go|pkg/syncer/server.go)
- path: (pkg/tso/admin.go|pkg/schedule/schedulers/split_bucket.go|server/api/plugin_disable.go|server/api/plugin_disable.go|server/api/operator.go|server/api/region.go|pkg/schedule/schedulers/balance_leader.go|pkg/replication/replication_mode.go|pkg/storage/endpoint/gc_safe_point.go|server/.*\.go|pkg/syncer/server.go)
linters:
- errcheck
4 changes: 2 additions & 2 deletions pkg/schedule/schedulers/balance_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ func (handler *balanceLeaderHandler) UpdateConfig(w http.ResponseWriter, r *http
data, _ := io.ReadAll(r.Body)
r.Body.Close()
httpCode, v := handler.config.Update(data)
handler.rd.JSON(w, httpCode, v)
_ = handler.rd.JSON(w, httpCode, v)
}

func (handler *balanceLeaderHandler) ListConfig(w http.ResponseWriter, _ *http.Request) {
conf := handler.config.Clone()
handler.rd.JSON(w, http.StatusOK, conf)
_ = handler.rd.JSON(w, http.StatusOK, conf)
}

type balanceLeaderScheduler struct {
Expand Down
12 changes: 8 additions & 4 deletions pkg/schedule/schedulers/balance_witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ func (conf *balanceWitnessSchedulerConfig) Update(data []byte) (int, any) {
newc, _ := json.Marshal(conf)
if !bytes.Equal(oldc, newc) {
if !conf.validateLocked() {
json.Unmarshal(oldc, conf)
if err := json.Unmarshal(oldc, conf); err != nil {
return http.StatusInternalServerError, err.Error()
}
return http.StatusBadRequest, "invalid batch size which should be an integer between 1 and 10"
}
conf.persistLocked()
if err := conf.persistLocked(); 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))
return http.StatusOK, "Config is updated."
}
Expand Down Expand Up @@ -147,12 +151,12 @@ func (handler *balanceWitnessHandler) UpdateConfig(w http.ResponseWriter, r *htt
data, _ := io.ReadAll(r.Body)
r.Body.Close()
httpCode, v := handler.config.Update(data)
handler.rd.JSON(w, httpCode, v)
_ = handler.rd.JSON(w, httpCode, v)
}

func (handler *balanceWitnessHandler) ListConfig(w http.ResponseWriter, _ *http.Request) {
conf := handler.config.Clone()
handler.rd.JSON(w, http.StatusOK, conf)
_ = handler.rd.JSON(w, http.StatusOK, conf)
}

type balanceWitnessScheduler struct {
Expand Down
33 changes: 20 additions & 13 deletions pkg/schedule/schedulers/evict_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/pkg/utils/syncutil"
"github.com/unrolled/render"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -150,7 +151,9 @@ func (conf *evictLeaderSchedulerConfig) removeStore(id uint64) (succ bool, last
func (conf *evictLeaderSchedulerConfig) resetStore(id uint64, keyRange []core.KeyRange) {
conf.Lock()
defer conf.Unlock()
conf.cluster.PauseLeaderTransfer(id)
if err := conf.cluster.PauseLeaderTransfer(id); err != nil {
log.Error("pause leader transfer failed", zap.Uint64("store-id", id), errs.ZapError(err))
}
conf.StoreIDWithRanges[id] = keyRange
}

Expand Down Expand Up @@ -370,7 +373,7 @@ func (handler *evictLeaderHandler) UpdateConfig(w http.ResponseWriter, r *http.R
if _, exists = handler.config.StoreIDWithRanges[id]; !exists {
if err := handler.config.cluster.PauseLeaderTransfer(id); err != nil {
handler.config.RUnlock()
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
_ = handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
}
Expand All @@ -385,26 +388,30 @@ func (handler *evictLeaderHandler) UpdateConfig(w http.ResponseWriter, r *http.R
args = append(args, handler.config.getRanges(id)...)
}

handler.config.BuildWithArgs(args)
err := handler.config.Persist()
err := handler.config.BuildWithArgs(args)
if err != nil {
_ = handler.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
err = handler.config.Persist()
if err != nil {
handler.config.removeStore(id)
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
_ = handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
handler.rd.JSON(w, http.StatusOK, "The scheduler has been applied to the store.")
_ = handler.rd.JSON(w, http.StatusOK, "The scheduler has been applied to the store.")
}

func (handler *evictLeaderHandler) ListConfig(w http.ResponseWriter, _ *http.Request) {
conf := handler.config.Clone()
handler.rd.JSON(w, http.StatusOK, conf)
_ = handler.rd.JSON(w, http.StatusOK, conf)
}

func (handler *evictLeaderHandler) DeleteConfig(w http.ResponseWriter, r *http.Request) {
idStr := mux.Vars(r)["store_id"]
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
handler.rd.JSON(w, http.StatusBadRequest, err.Error())
_ = handler.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}

Expand All @@ -415,26 +422,26 @@ func (handler *evictLeaderHandler) DeleteConfig(w http.ResponseWriter, r *http.R
err = handler.config.Persist()
if err != nil {
handler.config.resetStore(id, keyRanges)
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
_ = handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
if last {
if err := handler.config.removeSchedulerCb(EvictLeaderName); err != nil {
if errors.ErrorEqual(err, errs.ErrSchedulerNotFound.FastGenByArgs()) {
handler.rd.JSON(w, http.StatusNotFound, err.Error())
_ = handler.rd.JSON(w, http.StatusNotFound, err.Error())
} else {
handler.config.resetStore(id, keyRanges)
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
_ = handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
}
return
}
resp = lastStoreDeleteInfo
}
handler.rd.JSON(w, http.StatusOK, resp)
_ = handler.rd.JSON(w, http.StatusOK, resp)
return
}

handler.rd.JSON(w, http.StatusNotFound, errs.ErrScheduleConfigNotExist.FastGenByArgs().Error())
_ = handler.rd.JSON(w, http.StatusNotFound, errs.ErrScheduleConfigNotExist.FastGenByArgs().Error())
}

func newEvictLeaderHandler(config *evictLeaderSchedulerConfig) http.Handler {
Expand Down
8 changes: 4 additions & 4 deletions pkg/schedule/schedulers/evict_slow_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (handler *evictSlowStoreHandler) UpdateConfig(w http.ResponseWriter, r *htt
}
recoveryDurationGapFloat, ok := input["recovery-duration"].(float64)
if !ok {
handler.rd.JSON(w, http.StatusInternalServerError, errors.New("invalid argument for 'recovery-duration'").Error())
_ = handler.rd.JSON(w, http.StatusInternalServerError, errors.New("invalid argument for 'recovery-duration'").Error())
return
}
handler.config.Lock()
Expand All @@ -169,17 +169,17 @@ func (handler *evictSlowStoreHandler) UpdateConfig(w http.ResponseWriter, r *htt
recoveryDurationGap := uint64(recoveryDurationGapFloat)
handler.config.RecoveryDurationGap = recoveryDurationGap
if err := handler.config.persistLocked(); err != nil {
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
_ = handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
handler.config.RecoveryDurationGap = prevRecoveryDurationGap
return
}
log.Info("evict-slow-store-scheduler update 'recovery-duration' - unit: s", zap.Uint64("prev", prevRecoveryDurationGap), zap.Uint64("cur", recoveryDurationGap))
handler.rd.JSON(w, http.StatusOK, "Config updated.")
_ = handler.rd.JSON(w, http.StatusOK, "Config updated.")
}

func (handler *evictSlowStoreHandler) ListConfig(w http.ResponseWriter, _ *http.Request) {
conf := handler.config.Clone()
handler.rd.JSON(w, http.StatusOK, conf)
_ = handler.rd.JSON(w, http.StatusOK, conf)
}

type evictSlowStoreScheduler struct {
Expand Down
8 changes: 4 additions & 4 deletions pkg/schedule/schedulers/evict_slow_trend.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (handler *evictSlowTrendHandler) UpdateConfig(w http.ResponseWriter, r *htt
}
recoveryDurationGapFloat, ok := input["recovery-duration"].(float64)
if !ok {
handler.rd.JSON(w, http.StatusInternalServerError, errors.New("invalid argument for 'recovery-duration'").Error())
_ = handler.rd.JSON(w, http.StatusInternalServerError, errors.New("invalid argument for 'recovery-duration'").Error())
return
}
handler.config.Lock()
Expand All @@ -255,17 +255,17 @@ func (handler *evictSlowTrendHandler) UpdateConfig(w http.ResponseWriter, r *htt
recoveryDurationGap := uint64(recoveryDurationGapFloat)
handler.config.RecoveryDurationGap = recoveryDurationGap
if err := handler.config.persistLocked(); err != nil {
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
_ = handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
handler.config.RecoveryDurationGap = prevRecoveryDurationGap
return
}
log.Info("evict-slow-trend-scheduler update 'recovery-duration' - unit: s", zap.Uint64("prev", prevRecoveryDurationGap), zap.Uint64("cur", recoveryDurationGap))
handler.rd.JSON(w, http.StatusOK, "Config updated.")
_ = handler.rd.JSON(w, http.StatusOK, "Config updated.")
}

func (handler *evictSlowTrendHandler) ListConfig(w http.ResponseWriter, _ *http.Request) {
conf := handler.config.Clone()
handler.rd.JSON(w, http.StatusOK, conf)
_ = handler.rd.JSON(w, http.StatusOK, conf)
}

type evictSlowTrendScheduler struct {
Expand Down
33 changes: 20 additions & 13 deletions pkg/schedule/schedulers/grant_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/pkg/utils/syncutil"
"github.com/unrolled/render"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -130,7 +131,9 @@ func (conf *grantLeaderSchedulerConfig) removeStore(id uint64) (succ bool, last
func (conf *grantLeaderSchedulerConfig) resetStore(id uint64, keyRange []core.KeyRange) {
conf.Lock()
defer conf.Unlock()
conf.cluster.PauseLeaderTransfer(id)
if err := conf.cluster.PauseLeaderTransfer(id); err != nil {
log.Error("pause leader transfer failed", zap.Uint64("store-id", id), errs.ZapError(err))
}
conf.StoreIDWithRanges[id] = keyRange
}

Expand Down Expand Up @@ -281,7 +284,7 @@ func (handler *grantLeaderHandler) UpdateConfig(w http.ResponseWriter, r *http.R
if _, exists = handler.config.StoreIDWithRanges[id]; !exists {
if err := handler.config.cluster.PauseLeaderTransfer(id); err != nil {
handler.config.RUnlock()
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
_ = handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
}
Expand All @@ -296,26 +299,30 @@ func (handler *grantLeaderHandler) UpdateConfig(w http.ResponseWriter, r *http.R
args = append(args, handler.config.getRanges(id)...)
}

handler.config.BuildWithArgs(args)
err := handler.config.Persist()
err := handler.config.BuildWithArgs(args)
if err != nil {
_ = handler.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
err = handler.config.Persist()
if err != nil {
handler.config.removeStore(id)
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
_ = handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
handler.rd.JSON(w, http.StatusOK, "The scheduler has been applied to the store.")
_ = handler.rd.JSON(w, http.StatusOK, "The scheduler has been applied to the store.")
}

func (handler *grantLeaderHandler) ListConfig(w http.ResponseWriter, _ *http.Request) {
conf := handler.config.Clone()
handler.rd.JSON(w, http.StatusOK, conf)
_ = handler.rd.JSON(w, http.StatusOK, conf)
}

func (handler *grantLeaderHandler) DeleteConfig(w http.ResponseWriter, r *http.Request) {
idStr := mux.Vars(r)["store_id"]
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
handler.rd.JSON(w, http.StatusBadRequest, err.Error())
_ = handler.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}

Expand All @@ -326,26 +333,26 @@ func (handler *grantLeaderHandler) DeleteConfig(w http.ResponseWriter, r *http.R
err = handler.config.Persist()
if err != nil {
handler.config.resetStore(id, keyRanges)
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
_ = handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
if last {
if err := handler.config.removeSchedulerCb(GrantLeaderName); err != nil {
if errors.ErrorEqual(err, errs.ErrSchedulerNotFound.FastGenByArgs()) {
handler.rd.JSON(w, http.StatusNotFound, err.Error())
_ = handler.rd.JSON(w, http.StatusNotFound, err.Error())
} else {
handler.config.resetStore(id, keyRanges)
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
_ = handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
}
return
}
resp = lastStoreDeleteInfo
}
handler.rd.JSON(w, http.StatusOK, resp)
_ = handler.rd.JSON(w, http.StatusOK, resp)
return
}

handler.rd.JSON(w, http.StatusNotFound, errs.ErrScheduleConfigNotExist.FastGenByArgs().Error())
_ = handler.rd.JSON(w, http.StatusNotFound, errs.ErrScheduleConfigNotExist.FastGenByArgs().Error())
}

func newGrantLeaderHandler(config *grantLeaderSchedulerConfig) http.Handler {
Expand Down
22 changes: 12 additions & 10 deletions pkg/schedule/schedulers/hot_region_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func (conf *hotRegionSchedulerConfig) handleGetConfig(w http.ResponseWriter, _ *
conf.RLock()
defer conf.RUnlock()
rd := render.New(render.Options{IndentJSON: true})
rd.JSON(w, http.StatusOK, conf.getValidConf())
_ = rd.JSON(w, http.StatusOK, conf.getValidConf())
}

func isPriorityValid(priorities []string) (map[string]bool, error) {
Expand Down Expand Up @@ -434,43 +434,45 @@ func (conf *hotRegionSchedulerConfig) handleSetConfig(w http.ResponseWriter, r *
data, err := io.ReadAll(r.Body)
r.Body.Close()
if err != nil {
rd.JSON(w, http.StatusInternalServerError, err.Error())
_ = rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}

if err := json.Unmarshal(data, conf); err != nil {
rd.JSON(w, http.StatusInternalServerError, err.Error())
_ = rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
if err := conf.validateLocked(); err != nil {
// revert to old version
if err2 := json.Unmarshal(oldc, conf); err2 != nil {
rd.JSON(w, http.StatusInternalServerError, err2.Error())
_ = rd.JSON(w, http.StatusInternalServerError, err2.Error())
} else {
rd.JSON(w, http.StatusBadRequest, err.Error())
_ = rd.JSON(w, http.StatusBadRequest, err.Error())
}
return
}
newc, _ := json.Marshal(conf)
if !bytes.Equal(oldc, newc) {
conf.persistLocked()
if err := conf.persistLocked(); 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)))
rd.Text(w, http.StatusOK, "Config is updated.")
_ = rd.Text(w, http.StatusOK, "Config is updated.")
return
}

m := make(map[string]any)
if err := json.Unmarshal(data, &m); err != nil {
rd.JSON(w, http.StatusInternalServerError, err.Error())
_ = rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
ok := reflectutil.FindSameFieldByJSON(conf, m)
if ok {
rd.Text(w, http.StatusOK, "Config is the same with origin, so do nothing.")
_ = rd.Text(w, http.StatusOK, "Config is the same with origin, so do nothing.")
return
}

rd.Text(w, http.StatusBadRequest, "Config item is not found.")
_ = rd.Text(w, http.StatusBadRequest, "Config item is not found.")
}

func (conf *hotRegionSchedulerConfig) persistLocked() error {
Expand Down
Loading

0 comments on commit 1578f29

Please sign in to comment.