Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: set errcheck exclude func #8346

Merged
merged 6 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ linters-settings:
severity: warning
disabled: false
exclude: [""]
errcheck:
exclude-functions:
- (*github.com/unrolled/render.Render).JSON
- (*github.com/unrolled/render.Render).Data
- (*github.com/unrolled/render.Render).Text
- (net/http.ResponseWriter).Write
- github.com/pingcap/log.Sync
- (github.com/tikv/pd/pkg/ratelimit.Runner).RunTask
issues:
exclude-rules:
- path: (_test\.go|pkg/mock/.*\.go|tests/.*\.go)
Expand All @@ -207,6 +215,3 @@ 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/schedule/schedulers/split_bucket.go|server/api/.*\.go|pkg/schedule/schedulers/balance_leader.go)
linters:
- errcheck
6 changes: 2 additions & 4 deletions cmd/pd-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,7 @@ func start(cmd *cobra.Command, args []string, services ...string) {
log.Fatal("initialize logger error", errs.ZapError(err))
}
// Flushing any buffered log entries
defer func() {
_ = log.Sync()
}()
defer log.Sync()
memory.InitMemoryHook()
if len(services) != 0 {
versioninfo.Log(server.APIServiceMode)
Expand Down Expand Up @@ -301,6 +299,6 @@ func start(cmd *cobra.Command, args []string, services ...string) {
}

func exit(code int) {
_ = log.Sync()
log.Sync()
os.Exit(code)
}
8 changes: 4 additions & 4 deletions pkg/autoscaling/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ func NewHTTPHandler(svr *server.Server, rd *render.Render) *HTTPHandler {
func (h *HTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rc := h.svr.GetRaftCluster()
if rc == nil {
_ = h.rd.JSON(w, http.StatusInternalServerError, errs.ErrNotBootstrapped.FastGenByArgs().Error())
h.rd.JSON(w, http.StatusInternalServerError, errs.ErrNotBootstrapped.FastGenByArgs().Error())
return
}
data, err := io.ReadAll(r.Body)
r.Body.Close()
if err != nil {
_ = h.rd.JSON(w, http.StatusInternalServerError, err.Error())
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}

strategy := Strategy{}
if err := json.Unmarshal(data, &strategy); err != nil {
_ = h.rd.JSON(w, http.StatusBadRequest, err.Error())
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}

plan := calculate(rc, h.svr.GetPDServerConfig(), &strategy)
_ = h.rd.JSON(w, http.StatusOK, plan)
h.rd.JSON(w, http.StatusOK, plan)
}
4 changes: 2 additions & 2 deletions pkg/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ func GenerateRegionGuideFunc(enableLog bool) RegionGuideFunc {
regionID := region.GetID()
if logRunner != nil {
debug = func(msg string, fields ...zap.Field) {
_ = logRunner.RunTask(
logRunner.RunTask(
regionID,
"DebugLog",
func() {
Expand All @@ -756,7 +756,7 @@ func GenerateRegionGuideFunc(enableLog bool) RegionGuideFunc {
)
}
info = func(msg string, fields ...zap.Field) {
_ = logRunner.RunTask(
logRunner.RunTask(
regionID,
"InfoLog",
func() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/mcs/metastorage/server/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type dummyRestService struct{}

func (dummyRestService) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
_, _ = w.Write([]byte("not implemented"))
w.Write([]byte("not implemented"))
}

// Service is the gRPC service for meta storage.
Expand Down
2 changes: 1 addition & 1 deletion pkg/mcs/resourcemanager/server/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type dummyRestService struct{}

func (dummyRestService) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
_, _ = w.Write([]byte("not implemented"))
w.Write([]byte("not implemented"))
}

// Service is the gRPC service for resource manager.
Expand Down
5 changes: 1 addition & 4 deletions pkg/mcs/resourcemanager/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,7 @@ func CreateServerWrapper(cmd *cobra.Command, args []string) {
log.Fatal("initialize logger error", errs.ZapError(err))
}
// Flushing any buffered log entries
defer func() {
_ = log.Sync()
}()

log.Sync()
versioninfo.Log(serviceName)
log.Info("resource manager config", zap.Reflect("config", cfg))

Expand Down
4 changes: 1 addition & 3 deletions pkg/mcs/resourcemanager/server/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ func NewTestServer(ctx context.Context, re *require.Assertions, cfg *Config) (*S
re.NoError(err)
log.ReplaceGlobals(cfg.Logger, cfg.LogProps)
// Flushing any buffered log entries
defer func() {
_ = log.Sync()
}()
log.Sync()

s := CreateServer(ctx, cfg)
if err = s.Run(); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions pkg/mcs/scheduling/server/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ func (c *Cluster) processRegionHeartbeat(ctx *core.MetaProcessContext, region *c
// Due to some config changes need to update the region stats as well,
// so we do some extra checks here.
if hasRegionStats && c.regionStats.RegionStatsNeedUpdate(region) {
_ = ctx.TaskRunner.RunTask(
ctx.TaskRunner.RunTask(
regionID,
ratelimit.ObserveRegionStatsAsync,
func() {
Expand All @@ -636,7 +636,7 @@ func (c *Cluster) processRegionHeartbeat(ctx *core.MetaProcessContext, region *c
}
// region is not updated to the subtree.
if origin.GetRef() < 2 {
_ = ctx.TaskRunner.RunTask(
ctx.TaskRunner.RunTask(
regionID,
ratelimit.UpdateSubTree,
func() {
Expand All @@ -660,7 +660,7 @@ func (c *Cluster) processRegionHeartbeat(ctx *core.MetaProcessContext, region *c
tracer.OnSaveCacheFinished()
return err
}
_ = ctx.TaskRunner.RunTask(
ctx.TaskRunner.RunTask(
regionID,
ratelimit.UpdateSubTree,
func() {
Expand All @@ -669,7 +669,7 @@ func (c *Cluster) processRegionHeartbeat(ctx *core.MetaProcessContext, region *c
ratelimit.WithRetained(retained),
)
tracer.OnUpdateSubTreeFinished()
_ = ctx.TaskRunner.RunTask(
ctx.TaskRunner.RunTask(
regionID,
ratelimit.HandleOverlaps,
func() {
Expand All @@ -679,7 +679,7 @@ func (c *Cluster) processRegionHeartbeat(ctx *core.MetaProcessContext, region *c
}
tracer.OnSaveCacheFinished()
// handle region stats
_ = ctx.TaskRunner.RunTask(
ctx.TaskRunner.RunTask(
regionID,
ratelimit.CollectRegionStatsAsync,
func() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/mcs/scheduling/server/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type dummyRestService struct{}

func (dummyRestService) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
_, _ = w.Write([]byte("not implemented"))
w.Write([]byte("not implemented"))
}

// ConfigProvider is used to get scheduling config from the given
Expand Down
4 changes: 1 addition & 3 deletions pkg/mcs/scheduling/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,7 @@ func CreateServerWrapper(cmd *cobra.Command, args []string) {
log.Fatal("initialize logger error", errs.ZapError(err))
}
// Flushing any buffered log entries
defer func() {
_ = log.Sync()
}()
log.Sync()

versioninfo.Log(serviceName)
log.Info("scheduling service config", zap.Reflect("config", cfg))
Expand Down
5 changes: 1 addition & 4 deletions pkg/mcs/scheduling/server/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ func NewTestServer(ctx context.Context, re *require.Assertions, cfg *config.Conf
re.NoError(err)
log.ReplaceGlobals(cfg.Logger, cfg.LogProps)
// Flushing any buffered log entries
defer func() {
_ = log.Sync()
}()

log.Sync()
s := CreateServer(ctx, cfg)
if err = s.Run(); err != nil {
return nil, nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/mcs/tso/server/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type dummyRestService struct{}

func (dummyRestService) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
_, _ = w.Write([]byte("not implemented"))
w.Write([]byte("not implemented"))
}

// ConfigProvider is used to get tso config from the given
Expand Down
4 changes: 1 addition & 3 deletions pkg/mcs/tso/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,7 @@ func CreateServerWrapper(cmd *cobra.Command, args []string) {
log.Fatal("initialize logger error", errs.ZapError(err))
}
// Flushing any buffered log entries
defer func() {
_ = log.Sync()
}()
log.Sync()

versioninfo.Log(serviceName)
log.Info("TSO service config", zap.Reflect("config", cfg))
Expand Down
2 changes: 1 addition & 1 deletion pkg/mcs/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,6 @@ func StopGRPCServer(s server) {

// Exit exits the program with the given code.
func Exit(code int) {
_ = log.Sync()
log.Sync()
os.Exit(code)
}
12 changes: 8 additions & 4 deletions pkg/schedule/schedulers/balance_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,14 @@ func (conf *balanceLeaderSchedulerConfig) Update(data []byte) (int, any) {
newConfig, _ := json.Marshal(conf)
if !bytes.Equal(oldConfig, newConfig) {
if !conf.validateLocked() {
json.Unmarshal(oldConfig, conf)
if err := json.Unmarshal(oldConfig, 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 save balance-leader-scheduler config", errs.ZapError(err))
}
log.Info("balance-leader-scheduler config is updated", zap.ByteString("old", oldConfig), zap.ByteString("new", newConfig))
return http.StatusOK, "Config is updated."
}
Expand Down Expand Up @@ -161,12 +165,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
4 changes: 2 additions & 2 deletions pkg/schedule/schedulers/balance_witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,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
22 changes: 11 additions & 11 deletions pkg/schedule/schedulers/evict_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,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 @@ -390,28 +390,28 @@ func (handler *evictLeaderHandler) UpdateConfig(w http.ResponseWriter, r *http.R

err := handler.config.BuildWithArgs(args)
if err != nil {
_ = handler.rd.JSON(w, http.StatusBadRequest, err.Error())
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 @@ -422,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
Loading
Loading