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

linter: find inappropriate key in call to context.WithValue #40769

Merged
merged 9 commits into from
Jan 28, 2023
1 change: 1 addition & 0 deletions build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ STATICHECK_ANALYZERS = [
"S1039",
"S1040",
"SA1019",
"SA1029",
"SA2000",
"SA2001",
"SA2003",
Expand Down
7 changes: 7 additions & 0 deletions build/nogo_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,13 @@
"meta": "meta code"
}
},
"SA1029": {
"exclude_files": {
"/external/": "no need to vet third party code",
".*_generated\\.go$": "ignore generated code",
".*_test\\.go$": "ignore test code"
}
},
"SA2000": {
"exclude_files": {
"external/": "no need to vet third party code",
Expand Down
3 changes: 2 additions & 1 deletion executor/memtable_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/fn"
"github.com/pingcap/sysutil"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/util/pdapi"
pmodel "github.com/prometheus/common/model"
Expand Down Expand Up @@ -56,7 +57,7 @@ func TestMetricTableData(t *testing.T) {
}
matrix = append(matrix, &pmodel.SampleStream{Metric: metric, Values: []pmodel.SamplePair{v1}})

ctx := context.WithValue(context.Background(), "__mockMetricsPromData", matrix)
ctx := context.WithValue(context.Background(), executor.MockMetricsPromDataKey{}, matrix)
ctx = failpoint.WithHook(ctx, func(ctx context.Context, fpname string) bool {
return fpname == fpName
})
Expand Down
5 changes: 4 additions & 1 deletion executor/metrics_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,12 @@ func (e *MetricRetriever) retrieve(ctx context.Context, sctx sessionctx.Context)
return totalRows, nil
}

// MockMetricsPromDataKey is for test
type MockMetricsPromDataKey struct{}

func (e *MetricRetriever) queryMetric(ctx context.Context, sctx sessionctx.Context, queryRange promv1.Range, quantile float64) (result pmodel.Value, err error) {
failpoint.InjectContext(ctx, "mockMetricsPromData", func() {
failpoint.Return(ctx.Value("__mockMetricsPromData").(pmodel.Matrix), nil)
failpoint.Return(ctx.Value(MockMetricsPromDataKey{}).(pmodel.Matrix), nil)
})

// Add retry to avoid network error.
Expand Down
4 changes: 3 additions & 1 deletion executor/slow_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ import (
"golang.org/x/exp/slices"
)

type signalsKey struct{}

// ParseSlowLogBatchSize is the batch size of slow-log lines for a worker to parse, exported for testing.
var ParseSlowLogBatchSize = 64

Expand Down Expand Up @@ -474,7 +476,7 @@ func (e *slowQueryRetriever) parseSlowLog(ctx context.Context, sctx sessionctx.C
}
failpoint.Inject("mockReadSlowLogSlow", func(val failpoint.Value) {
if val.(bool) {
signals := ctx.Value("signals").([]chan int)
signals := ctx.Value(signalsKey{}).([]chan int)
signals[0] <- 1
<-signals[1]
}
Expand Down
2 changes: 1 addition & 1 deletion executor/slow_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ select * from t;`
retriever, err := newSlowQueryRetriever()
require.NoError(t, err)
var signal1, signal2 = make(chan int, 1), make(chan int, 1)
ctx := context.WithValue(context.Background(), "signals", []chan int{signal1, signal2})
ctx := context.WithValue(context.Background(), signalsKey{}, []chan int{signal1, signal2})
ctx, cancel := context.WithCancel(ctx)
err = failpoint.Enable("github.com/pingcap/tidb/executor/mockReadSlowLogSlow", "return(true)")
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions store/gcworker/gc_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,7 @@ func (w *GCWorker) resolveLocksForRange(
failpoint.Inject("setGcResolveMaxBackoff", func(v failpoint.Value) {
sleep := v.(int)
// cooperate with github.com/tikv/client-go/v2/locate/invalidCacheAndRetry
//nolint: SA1029
ctx = context.WithValue(ctx, "injectedBackoff", struct{}{})
bo = tikv.NewBackofferWithVars(ctx, sleep, nil)
})
Expand Down
6 changes: 3 additions & 3 deletions ttl/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,16 @@ func (t *PhaseTracer) EndPhase() {
t.EnterPhase("")
}

const ttlPhaseTraceKey = "ttlPhaseTraceKey"
type ttlPhaseTraceKey struct{}

// CtxWithPhaseTracer create a new context with tracer
func CtxWithPhaseTracer(ctx context.Context, tracer *PhaseTracer) context.Context {
return context.WithValue(ctx, ttlPhaseTraceKey, tracer)
return context.WithValue(ctx, ttlPhaseTraceKey{}, tracer)
}

// PhaseTracerFromCtx returns a tracer from a given context
func PhaseTracerFromCtx(ctx context.Context) *PhaseTracer {
if tracer, ok := ctx.Value(ttlPhaseTraceKey).(*PhaseTracer); ok {
if tracer, ok := ctx.Value(ttlPhaseTraceKey{}).(*PhaseTracer); ok {
return tracer
}
return nil
Expand Down