From d5a8fff19bf7ee112c640846db75132b604c7bdb Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Thu, 15 Aug 2024 13:57:13 +0800 Subject: [PATCH] add more linters Signed-off-by: Ryan Leung --- .gitignore | 1 + .golangci.yml | 11 +++++++++++ client/go.mod | 2 +- client/http/client_test.go | 7 ++++--- client/resource_group/controller/controller.go | 3 +-- go.mod | 2 +- go.sum | 3 +-- pkg/gctuner/finalizer.go | 3 +-- pkg/gctuner/memory_limit_tuner.go | 16 ++++++---------- pkg/memory/var.go | 13 ++----------- tools/pd-simulator/simulator/cases/hot_write.go | 1 + tools/pd-simulator/simulator/simutil/key_test.go | 4 ++-- 12 files changed, 32 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index dc2efa5004f..a9d942fe706 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,4 @@ coverage go.work* embedded_assets_handler.go *.log +*.bin diff --git a/.golangci.yml b/.golangci.yml index bc1ba393f39..e09011c45be 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -15,6 +15,9 @@ linters: - gofmt - revive - errcheck + - exportloopref + - goimports + - depguard linters-settings: gocritic: # Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty @@ -205,6 +208,14 @@ linters-settings: - (net/http.ResponseWriter).Write - github.com/pingcap/log.Sync - (github.com/tikv/pd/pkg/ratelimit.Runner).RunTask + depguard: + rules: + denied-deps: + deny: + - pkg: go.uber.org/atomic + desc: "Use 'sync/atomic' instead of 'go.uber.org/atomic'" + - pkg: github.com/pkg/errors + desc: "Use 'github.com/pingcap/errors' instead of 'github.com/pkg/errors'" issues: exclude-rules: - path: (_test\.go|pkg/mock/.*\.go|tests/.*\.go) diff --git a/client/go.mod b/client/go.mod index 8dc706a4540..eb537f93ec8 100644 --- a/client/go.mod +++ b/client/go.mod @@ -14,7 +14,6 @@ require ( github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 github.com/prometheus/client_golang v1.18.0 github.com/stretchr/testify v1.8.2 - go.uber.org/atomic v1.10.0 go.uber.org/goleak v1.1.11 go.uber.org/zap v1.24.0 golang.org/x/exp v0.0.0-20230711005742-c3f37128e5a4 @@ -34,6 +33,7 @@ require ( github.com/prometheus/common v0.46.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect github.com/stretchr/objx v0.5.0 // indirect + go.uber.org/atomic v1.10.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/net v0.23.0 // indirect golang.org/x/sys v0.18.0 // indirect diff --git a/client/http/client_test.go b/client/http/client_test.go index 8769fa53f9a..5d63a34df54 100644 --- a/client/http/client_test.go +++ b/client/http/client_test.go @@ -18,13 +18,13 @@ import ( "context" "net/http" "strings" + "sync/atomic" "testing" "time" "github.com/stretchr/testify/require" "github.com/tikv/pd/client/errs" "github.com/tikv/pd/client/retry" - "go.uber.org/atomic" ) func TestPDAllowFollowerHandleHeader(t *testing.T) { @@ -53,7 +53,8 @@ func TestPDAllowFollowerHandleHeader(t *testing.T) { func TestWithCallerID(t *testing.T) { re := require.New(t) checked := 0 - expectedVal := atomic.NewString(defaultCallerID) + var expectedVal atomic.Value + expectedVal.Store(defaultCallerID) httpClient := NewHTTPClientWithRequestChecker(func(req *http.Request) error { val := req.Header.Get(xCallerIDKey) // Exclude the request sent by the inner client. @@ -68,7 +69,7 @@ func TestWithCallerID(t *testing.T) { defer c.Close() c.GetRegions(context.Background()) expectedVal.Store("test") - c.WithCallerID(expectedVal.Load()).GetRegions(context.Background()) + c.WithCallerID(expectedVal.Load().(string)).GetRegions(context.Background()) re.Equal(2, checked) } diff --git a/client/resource_group/controller/controller.go b/client/resource_group/controller/controller.go index 8e32b156a61..e5ad1f81fa5 100644 --- a/client/resource_group/controller/controller.go +++ b/client/resource_group/controller/controller.go @@ -32,7 +32,6 @@ import ( "github.com/prometheus/client_golang/prometheus" pd "github.com/tikv/pd/client" "github.com/tikv/pd/client/errs" - atomicutil "go.uber.org/atomic" "go.uber.org/zap" "golang.org/x/exp/slices" ) @@ -57,7 +56,7 @@ const ( lowToken selectType = 1 ) -var enableControllerTraceLog = atomicutil.NewBool(false) +var enableControllerTraceLog atomic.Bool func logControllerTrace(msg string, fields ...zap.Field) { if enableControllerTraceLog.Load() { diff --git a/go.mod b/go.mod index 9dc5fa9f249..79db4f3940c 100644 --- a/go.mod +++ b/go.mod @@ -56,7 +56,6 @@ require ( go.etcd.io/etcd/client/pkg/v3 v3.5.15 go.etcd.io/etcd/client/v3 v3.5.15 go.etcd.io/etcd/server/v3 v3.5.15 - go.uber.org/atomic v1.10.0 go.uber.org/goleak v1.3.0 go.uber.org/zap v1.27.0 golang.org/x/exp v0.0.0-20230711005742-c3f37128e5a4 @@ -187,6 +186,7 @@ require ( go.opentelemetry.io/otel/sdk v1.20.0 // indirect go.opentelemetry.io/otel/trace v1.20.0 // indirect go.opentelemetry.io/proto/otlp v1.0.0 // indirect + go.uber.org/atomic v1.9.0 // indirect go.uber.org/dig v1.9.0 // indirect go.uber.org/fx v1.12.0 // indirect go.uber.org/multierr v1.11.0 diff --git a/go.sum b/go.sum index e3bc1ed175a..e57fdcbce9c 100644 --- a/go.sum +++ b/go.sum @@ -566,9 +566,8 @@ go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= -go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/dig v1.9.0 h1:pJTDXKEhRqBI8W7rU7kwT5EgyRZuSMVSFcZolOvKK9U= go.uber.org/dig v1.9.0/go.mod h1:X34SnWGr8Fyla9zQNO2GSO2D+TIuqB14OS8JhYocIyw= go.uber.org/fx v1.12.0 h1:+1+3Cz9M0dFMPy9SW9XUIUHye8bnPUm7q7DroNGWYG4= diff --git a/pkg/gctuner/finalizer.go b/pkg/gctuner/finalizer.go index b9a73fa7ce5..a1424980b66 100644 --- a/pkg/gctuner/finalizer.go +++ b/pkg/gctuner/finalizer.go @@ -16,8 +16,7 @@ package gctuner import ( "runtime" - - "go.uber.org/atomic" + "sync/atomic" ) type finalizerCallback func() diff --git a/pkg/gctuner/memory_limit_tuner.go b/pkg/gctuner/memory_limit_tuner.go index 77a8f9db4c6..8a852b191d8 100644 --- a/pkg/gctuner/memory_limit_tuner.go +++ b/pkg/gctuner/memory_limit_tuner.go @@ -17,6 +17,7 @@ package gctuner import ( "math" "runtime/debug" + "sync/atomic" "time" "github.com/pingcap/failpoint" @@ -24,7 +25,6 @@ import ( util "github.com/tikv/pd/pkg/gogc" "github.com/tikv/pd/pkg/memory" "github.com/tikv/pd/pkg/utils/logutil" - atomicutil "go.uber.org/atomic" "go.uber.org/zap" ) @@ -35,10 +35,10 @@ var GlobalMemoryLimitTuner = &memoryLimitTuner{} // So we can change memory limit dynamically to avoid frequent GC when memory usage is greater than the limit. type memoryLimitTuner struct { finalizer *finalizer - isTuning atomicutil.Bool - percentage atomicutil.Float64 - waitingReset atomicutil.Bool - nextGCTriggeredByMemoryLimit atomicutil.Bool + isTuning atomic.Bool + percentage atomic.Value + waitingReset atomic.Bool + nextGCTriggeredByMemoryLimit atomic.Bool } // fallbackPercentage indicates the fallback memory limit percentage when turning. @@ -74,8 +74,6 @@ func (t *memoryLimitTuner) tuning() { if t.nextGCTriggeredByMemoryLimit.Load() && t.waitingReset.CompareAndSwap(false, true) { go func() { defer logutil.LogPanic() - memory.MemoryLimitGCLast.Store(time.Now()) - memory.MemoryLimitGCTotal.Add(1) setMemoryLimit(t.calcMemoryLimit(fallbackPercentage)) resetInterval := 1 * time.Minute // Wait 1 minute and set back, to avoid frequent GC failpoint.Inject("testMemoryLimitTuner", func(val failpoint.Value) { @@ -89,12 +87,10 @@ func (t *memoryLimitTuner) tuning() { continue } }() - memory.TriggerMemoryLimitGC.Store(true) } t.nextGCTriggeredByMemoryLimit.Store(true) } else { t.nextGCTriggeredByMemoryLimit.Store(false) - memory.TriggerMemoryLimitGC.Store(false) } } @@ -117,7 +113,7 @@ func (t *memoryLimitTuner) SetPercentage(percentage float64) { // GetPercentage get the percentage from memory limit tuner. func (t *memoryLimitTuner) GetPercentage() float64 { - return t.percentage.Load() + return t.percentage.Load().(float64) } // UpdateMemoryLimit updates the memory limit. diff --git a/pkg/memory/var.go b/pkg/memory/var.go index fd218183937..1ecc0aa3238 100644 --- a/pkg/memory/var.go +++ b/pkg/memory/var.go @@ -15,19 +15,10 @@ package memory import ( - "time" - - atomicutil "go.uber.org/atomic" + atomic "sync/atomic" ) // Process global variables for memory limit. var ( - ServerMemoryLimitOriginText = atomicutil.NewString("0") - ServerMemoryLimit = atomicutil.NewUint64(0) - ServerMemoryLimitSessMinSize = atomicutil.NewUint64(128 << 20) - - QueryForceDisk = atomicutil.NewInt64(0) - TriggerMemoryLimitGC = atomicutil.NewBool(false) - MemoryLimitGCLast = atomicutil.NewTime(time.Time{}) - MemoryLimitGCTotal = atomicutil.NewInt64(0) + ServerMemoryLimit atomic.Uint64 ) diff --git a/tools/pd-simulator/simulator/cases/hot_write.go b/tools/pd-simulator/simulator/cases/hot_write.go index 8f08264590d..54e944e540d 100644 --- a/tools/pd-simulator/simulator/cases/hot_write.go +++ b/tools/pd-simulator/simulator/cases/hot_write.go @@ -16,6 +16,7 @@ package cases import ( "fmt" + "github.com/docker/go-units" "github.com/pingcap/kvproto/pkg/metapb" "github.com/tikv/pd/pkg/core" diff --git a/tools/pd-simulator/simulator/simutil/key_test.go b/tools/pd-simulator/simulator/simutil/key_test.go index ae00887afa8..9db7c597751 100644 --- a/tools/pd-simulator/simulator/simutil/key_test.go +++ b/tools/pd-simulator/simulator/simutil/key_test.go @@ -15,12 +15,12 @@ package simutil import ( - "github.com/pingcap/kvproto/pkg/metapb" - "github.com/tikv/pd/pkg/core" "testing" + "github.com/pingcap/kvproto/pkg/metapb" "github.com/stretchr/testify/require" "github.com/tikv/pd/pkg/codec" + "github.com/tikv/pd/pkg/core" ) func TestGenerateTableKeys(t *testing.T) {