From 9c3edfa0af66727a47b2c416b80f2041529abd41 Mon Sep 17 00:00:00 2001 From: Wei Fu Date: Thu, 21 Sep 2023 10:26:52 +0800 Subject: [PATCH 1/4] *: fix staticcheck lint Changed TraceKey/StartTimeKey/TokenFieldNameGRPCKey to struct{} to follow the correct usage of context. Similar patch to #8901. Signed-off-by: Wei Fu --- api/v3rpc/rpctypes/metadatafields.go | 3 ++ .../v3/experimental/recipes/double_barrier.go | 7 ++-- client/v3/lease.go | 2 +- client/v3/maintenance.go | 2 +- pkg/traceutil/trace.go | 11 ++--- pkg/traceutil/trace_test.go | 4 +- scripts/test.sh | 40 +++++++++++++++++-- server/auth/store_test.go | 4 +- server/etcdserver/txn/txn.go | 8 ++-- server/etcdserver/v3_server.go | 10 ++--- server/proxy/grpcproxy/util.go | 4 +- tests/framework/e2e/etcd_process.go | 4 +- .../clientv3/lease/leasing_test.go | 2 +- tests/integration/lazy_cluster.go | 5 ++- tests/robustness/report/client.go | 1 - 15 files changed, 74 insertions(+), 33 deletions(-) diff --git a/api/v3rpc/rpctypes/metadatafields.go b/api/v3rpc/rpctypes/metadatafields.go index 8f8ac60ff22..e5770afb2e8 100644 --- a/api/v3rpc/rpctypes/metadatafields.go +++ b/api/v3rpc/rpctypes/metadatafields.go @@ -18,3 +18,6 @@ var ( TokenFieldNameGRPC = "token" TokenFieldNameSwagger = "authorization" ) + +// TokenFieldNameGRPCKey is used as a key of context to store token. +type TokenFieldNameGRPCKey struct{} diff --git a/client/v3/experimental/recipes/double_barrier.go b/client/v3/experimental/recipes/double_barrier.go index cc2416db23b..ab6231ad7a6 100644 --- a/client/v3/experimental/recipes/double_barrier.go +++ b/client/v3/experimental/recipes/double_barrier.go @@ -71,10 +71,9 @@ func (b *DoubleBarrier) Enter() error { // delete itself now, otherwise other processes may need to wait // until these keys are automatically deleted when the related // lease expires. - if err = b.myKey.Delete(); err != nil { - // Nothing to do here. We have to wait for the key to be - // deleted when the lease expires. - } + b.myKey.Delete() + // Nothing to do here even if we run into error. + // We have to wait for the key to be deleted when the lease expires. return ErrTooManyClients } diff --git a/client/v3/lease.go b/client/v3/lease.go index e13fa674295..e51e7b0d6dc 100644 --- a/client/v3/lease.go +++ b/client/v3/lease.go @@ -198,12 +198,12 @@ func NewLeaseFromLeaseClient(remote pb.LeaseClient, c *Client, keepAliveTimeout keepAlives: make(map[LeaseID]*keepAlive), remote: remote, firstKeepAliveTimeout: keepAliveTimeout, - lg: c.lg, } if l.firstKeepAliveTimeout == time.Second { l.firstKeepAliveTimeout = defaultTTL } if c != nil { + l.lg = c.lg l.callOpts = c.callOpts } reqLeaderCtx := WithRequireLeader(context.Background()) diff --git a/client/v3/maintenance.go b/client/v3/maintenance.go index 7af1f07cc78..d8cd137179f 100644 --- a/client/v3/maintenance.go +++ b/client/v3/maintenance.go @@ -133,7 +133,6 @@ func NewMaintenance(c *Client) Maintenance { func NewMaintenanceFromMaintenanceClient(remote pb.MaintenanceClient, c *Client) Maintenance { api := &maintenance{ - lg: c.lg, dial: func(string) (pb.MaintenanceClient, func(), error) { return remote, func() {}, nil }, @@ -141,6 +140,7 @@ func NewMaintenanceFromMaintenanceClient(remote pb.MaintenanceClient, c *Client) } if c != nil { api.callOpts = c.callOpts + api.lg = c.lg } return api } diff --git a/pkg/traceutil/trace.go b/pkg/traceutil/trace.go index 7b83956b87b..0f36323bd19 100644 --- a/pkg/traceutil/trace.go +++ b/pkg/traceutil/trace.go @@ -25,10 +25,11 @@ import ( "go.uber.org/zap" ) -const ( - TraceKey = "trace" - StartTimeKey = "startTime" -) +// TraceKey is used as a key of context for Trace. +type TraceKey struct{} + +// StartTimeKey is used as a key of context for start time of operation. +type StartTimeKey struct{} // Field is a kv pair to record additional details of the trace. type Field struct { @@ -81,7 +82,7 @@ func TODO() *Trace { } func Get(ctx context.Context) *Trace { - if trace, ok := ctx.Value(TraceKey).(*Trace); ok && trace != nil { + if trace, ok := ctx.Value(TraceKey{}).(*Trace); ok && trace != nil { return trace } return TODO() diff --git a/pkg/traceutil/trace_test.go b/pkg/traceutil/trace_test.go index d56fe295306..8d3b10e07fd 100644 --- a/pkg/traceutil/trace_test.go +++ b/pkg/traceutil/trace_test.go @@ -40,7 +40,7 @@ func TestGet(t *testing.T) { }, { name: "When the context has trace", - inputCtx: context.WithValue(context.Background(), TraceKey, traceForTest), + inputCtx: context.WithValue(context.Background(), TraceKey{}, traceForTest), outputTrace: traceForTest, }, } @@ -51,7 +51,7 @@ func TestGet(t *testing.T) { if trace == nil { t.Errorf("Expected %v; Got nil", tt.outputTrace) } - if trace.operation != tt.outputTrace.operation { + if tt.outputTrace == nil || trace.operation != tt.outputTrace.operation { t.Errorf("Expected %v; Got %v", tt.outputTrace, trace) } }) diff --git a/scripts/test.sh b/scripts/test.sh index 6f9b89f732c..83347fdf41d 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -426,10 +426,44 @@ function unparam_pass { } function staticcheck_pass { - # TODO: we should upgrade pb or ignore the pb package + local tmp_lintyaml + tmp_lintyaml=$(mktemp -t 'tmp_golangcilint_staticcheckXXX.yaml') + + # shellcheck disable=SC2064 + trap "rm ${tmp_lintyaml}; trap - RETURN" RETURN + + # TODO: The golangci-lint commandline doesn't support listener-settings. And + # staticcheck command[1] not just verifies the staticcheck, but also includes + # stylecheck and unused. So copy the tools/.golangci.yaml and just run the + # staticcheck rule. It should be removed when closes #16610 # - # versionpb/version.pb.go:69:15: proto.RegisterFile is deprecated: Use protoregistry.GlobalFiles.RegisterFile instead. (SA1019) - run_for_modules generic_checker run_go_tool "honnef.co/go/tools/cmd/staticcheck" + # REF: + # [1] https://github.com/dominikh/go-tools/blob/v0.4.5/cmd/staticcheck/staticcheck.go#L29 + cat > "${tmp_lintyaml}" < Date: Thu, 21 Sep 2023 12:30:23 +0800 Subject: [PATCH 2/4] *: Use golangcilint_pass to run staticcheck and ineffassign Copy the tools/.golangci.yaml and run the linters for which we have already fixed. The temp .golangci.yaml will be removed when we fixes all the linters' issues. Signed-off-by: Wei Fu --- Makefile | 8 ++++---- scripts/test.sh | 25 +++++++++++-------------- tools/mod/go.mod | 1 - tools/mod/go.sum | 8 -------- tools/mod/tools.go | 1 - 5 files changed, 15 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index 0e8f90b7970..249fad34fbb 100644 --- a/Makefile +++ b/Makefile @@ -91,7 +91,7 @@ verify-dep: # still depends on legacy {ineffassign,nakedret,unparam,...}_pass. These X_pass # will be removed when the golangci-lint covers all the sub modules. .PHONY: verify-lint -verify-lint: verify-ineffassign +verify-lint: verify-golangcilint golangci-lint run --config tools/.golangci.yaml .PHONY: fix-lint @@ -150,9 +150,9 @@ verify-yamllint: verify-govet-shadow: PASSES="govet_shadow" ./scripts/test.sh -.PHONY: verify-ineffassign -verify-ineffassign: - PASSES="ineffassign" ./scripts/test.sh +.PHONY: verify-golangcilint +verify-golangcilint: + PASSES="golangcilint" ./scripts/test.sh YAMLFMT_VERSION = $(shell cd tools/mod && go list -m -f '{{.Version}}' github.com/google/yamlfmt) diff --git a/scripts/test.sh b/scripts/test.sh index 83347fdf41d..f79268f9724 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -425,20 +425,15 @@ function unparam_pass { run_for_modules generic_checker run_go_tool "mvdan.cc/unparam" } -function staticcheck_pass { +function golangcilint_pass { local tmp_lintyaml - tmp_lintyaml=$(mktemp -t 'tmp_golangcilint_staticcheckXXX.yaml') + tmp_lintyaml=$(mktemp -t 'tmp_golangcilint_XXX.yaml') # shellcheck disable=SC2064 trap "rm ${tmp_lintyaml}; trap - RETURN" RETURN - # TODO: The golangci-lint commandline doesn't support listener-settings. And - # staticcheck command[1] not just verifies the staticcheck, but also includes - # stylecheck and unused. So copy the tools/.golangci.yaml and just run the - # staticcheck rule. It should be removed when closes #16610 - # - # REF: - # [1] https://github.com/dominikh/go-tools/blob/v0.4.5/cmd/staticcheck/staticcheck.go#L29 + # TODO: Copy the tools/.golangci.yaml and run the linters for which we have + # already fixed. It should be removed when closes #16610. cat > "${tmp_lintyaml}" < Date: Thu, 21 Sep 2023 15:08:42 +0800 Subject: [PATCH 3/4] *: lint_pass should use global golangci.yaml Disable failed linters and enable it by #16610. Signed-off-by: Wei Fu --- Makefile | 13 ++----------- scripts/test.sh | 42 ++---------------------------------------- tools/.golangci.yaml | 10 +++++----- 3 files changed, 9 insertions(+), 56 deletions(-) diff --git a/Makefile b/Makefile index 249fad34fbb..902214e3fbd 100644 --- a/Makefile +++ b/Makefile @@ -85,14 +85,9 @@ fix-bom: verify-dep: PASSES="dep" ./scripts/test.sh -# TODO: https://github.com/etcd-io/etcd/issues/16610 -# -# The golangci-lint doesn't verify sub modules. Before #16610 fixed, verify-lint -# still depends on legacy {ineffassign,nakedret,unparam,...}_pass. These X_pass -# will be removed when the golangci-lint covers all the sub modules. .PHONY: verify-lint -verify-lint: verify-golangcilint - golangci-lint run --config tools/.golangci.yaml +verify-lint: + PASSES="lint" ./scripts/test.sh .PHONY: fix-lint fix-lint: @@ -150,10 +145,6 @@ verify-yamllint: verify-govet-shadow: PASSES="govet_shadow" ./scripts/test.sh -.PHONY: verify-golangcilint -verify-golangcilint: - PASSES="golangcilint" ./scripts/test.sh - YAMLFMT_VERSION = $(shell cd tools/mod && go list -m -f '{{.Version}}' github.com/google/yamlfmt) .PHONY: fix-yamllint diff --git a/scripts/test.sh b/scripts/test.sh index f79268f9724..ded0d818479 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -425,46 +425,8 @@ function unparam_pass { run_for_modules generic_checker run_go_tool "mvdan.cc/unparam" } -function golangcilint_pass { - local tmp_lintyaml - tmp_lintyaml=$(mktemp -t 'tmp_golangcilint_XXX.yaml') - - # shellcheck disable=SC2064 - trap "rm ${tmp_lintyaml}; trap - RETURN" RETURN - - # TODO: Copy the tools/.golangci.yaml and run the linters for which we have - # already fixed. It should be removed when closes #16610. - cat > "${tmp_lintyaml}" < Date: Thu, 21 Sep 2023 17:34:12 +0800 Subject: [PATCH 4/4] disable staticcheck for DoubleBarrier.Enter Signed-off-by: Wei Fu --- client/v3/experimental/recipes/double_barrier.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/client/v3/experimental/recipes/double_barrier.go b/client/v3/experimental/recipes/double_barrier.go index ab6231ad7a6..e0d00247f1c 100644 --- a/client/v3/experimental/recipes/double_barrier.go +++ b/client/v3/experimental/recipes/double_barrier.go @@ -71,9 +71,11 @@ func (b *DoubleBarrier) Enter() error { // delete itself now, otherwise other processes may need to wait // until these keys are automatically deleted when the related // lease expires. - b.myKey.Delete() - // Nothing to do here even if we run into error. - // We have to wait for the key to be deleted when the lease expires. + //nolint:staticcheck // SA9003 disable empty branch checker to keep the comment for why we ignore error + if err = b.myKey.Delete(); err != nil { + // Nothing to do here. We have to wait for the key to be + // deleted when the lease expires. + } return ErrTooManyClients }