Skip to content

Commit

Permalink
add more linters
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx committed Aug 15, 2024
1 parent cc2900c commit d5a8fff
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ coverage
go.work*
embedded_assets_handler.go
*.log
*.bin
11 changes: 11 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion client/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions client/http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand All @@ -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)
}

Expand Down
3 changes: 1 addition & 2 deletions client/resource_group/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
3 changes: 1 addition & 2 deletions pkg/gctuner/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ package gctuner

import (
"runtime"

"go.uber.org/atomic"
"sync/atomic"
)

type finalizerCallback func()
Expand Down
16 changes: 6 additions & 10 deletions pkg/gctuner/memory_limit_tuner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ package gctuner
import (
"math"
"runtime/debug"
"sync/atomic"
"time"

"github.com/pingcap/failpoint"
"github.com/pingcap/log"
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"
)

Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}
}

Expand All @@ -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.
Expand Down
13 changes: 2 additions & 11 deletions pkg/memory/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
1 change: 1 addition & 0 deletions tools/pd-simulator/simulator/cases/hot_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions tools/pd-simulator/simulator/simutil/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d5a8fff

Please sign in to comment.