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: check AutoSyncInterval for etcd config (#44081) #44090

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ nogo(
"@org_golang_x_tools//go/analysis/passes/unreachable:go_default_library",
"@org_golang_x_tools//go/analysis/passes/unsafeptr:go_default_library",
"@org_golang_x_tools//go/analysis/passes/unusedresult:go_default_library",
<<<<<<< HEAD
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lance6716 Please fix it.

"//build/linter/asciicheck:asciicheck",
"//build/linter/bodyclose:bodyclose",
"//build/linter/durationcheck:durationcheck",
Expand All @@ -136,6 +137,26 @@ nogo(
"//build/linter/predeclared:predeclared",
"//build/linter/unconvert:unconvert",
"//build/linter/rowserrcheck:rowserrcheck",
=======
"//build/linter/asciicheck",
"//build/linter/bodyclose",
"//build/linter/durationcheck",
"//build/linter/etcdconfig",
"//build/linter/exportloopref",
"//build/linter/forcetypeassert",
"//build/linter/gofmt",
"//build/linter/gci",
"//build/linter/gosec",
"//build/linter/ineffassign",
"//build/linter/makezero",
"//build/linter/mirror",
"//build/linter/misspell",
"//build/linter/noloopclosure",
"//build/linter/prealloc",
"//build/linter/predeclared",
"//build/linter/unconvert",
"//build/linter/rowserrcheck",
>>>>>>> 8c1fae88e5a (linter: check AutoSyncInterval for etcd config (#44081))
] + staticcheck_analyzers(STATICHECK_ANALYZERS) +
select({
"//build:with_nogo": [
Expand Down
12 changes: 12 additions & 0 deletions build/linter/etcdconfig/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "etcdconfig",
srcs = ["analyzer.go"],
importpath = "github.com/pingcap/tidb/build/linter/etcdconfig",
visibility = ["//visibility:public"],
deps = [
"@org_golang_x_tools//go/analysis",
"@org_golang_x_tools//go/analysis/passes/inspect",
],
)
100 changes: 100 additions & 0 deletions build/linter/etcdconfig/analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package etcdconfig

import (
"go/ast"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
)

// Analyzer is the analyzer struct of unconvert.
var Analyzer = &analysis.Analyzer{
Name: "etcdconfig",
Doc: "Check necessary fields of etcd config",
Requires: []*analysis.Analyzer{inspect.Analyzer},
Run: run,
}

const (
configPackagePath = "go.etcd.io/etcd/client/v3"
configPackageName = "clientv3"
configStructName = "Config"
)

// Adapted from https://github.com/mdempsky/unconvert/blob/beb68d938016d2dec1d1b078054f4d3db25f97be/unconvert.go#L371-L414.
func run(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
packageName := ""
for _, spec := range file.Imports {
if spec.Path.Value != "\""+configPackagePath+"\"" {
continue
}
if spec.Name != nil {
packageName = spec.Name.Name
} else {
packageName = configPackageName
}
}
if packageName == "" {
continue
}

for _, decl := range file.Decls {
ast.Inspect(decl, func(n ast.Node) bool {
lit, ok := n.(*ast.CompositeLit)
if !ok {
return true
}
tp, ok := lit.Type.(*ast.SelectorExpr)
if !ok {
return true
}
litPackage, ok := tp.X.(*ast.Ident)
if !ok {
return true
}
if litPackage.Name != packageName {
return true
}
if tp.Sel.Name != configStructName {
return true
}

found := false
for _, field := range lit.Elts {
kv, ok := field.(*ast.KeyValueExpr)
if !ok {
continue
}
key, ok := kv.Key.(*ast.Ident)
if !ok {
continue
}
if key.Name == "AutoSyncInterval" {
found = true
break
}
}
if !found {
pass.Reportf(lit.Pos(), "missing field AutoSyncInterval")
}
return true
})
}
}
return nil, nil
}
7 changes: 7 additions & 0 deletions build/nogo_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -916,5 +916,12 @@
"/external/": "no need to vet third party code",
".*_generated\\.go$": "ignore generated code"
}
},
"etcdconfig": {
"exclude_files": {
"parser/parser.go": "parser/parser.go code",
".*_test.go": "ignore test code",
"external/": "no need to vet third party code"
}
}
}
5 changes: 3 additions & 2 deletions dumpling/export/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ func getPdDDLIDs(pCtx context.Context, cli *clientv3.Client) ([]string, error) {

func checkSameCluster(tctx *tcontext.Context, db *sql.DB, pdAddrs []string) (bool, error) {
cli, err := clientv3.New(clientv3.Config{
Endpoints: pdAddrs,
DialTimeout: defaultEtcdDialTimeOut,
Endpoints: pdAddrs,
DialTimeout: defaultEtcdDialTimeOut,
AutoSyncInterval: 30 * time.Second,
})
if err != nil {
return false, errors.Trace(err)
Expand Down
7 changes: 4 additions & 3 deletions util/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ func NewClient(cli *clientv3.Client, root string) *Client {
// NewClientFromCfg returns a wrapped etcd client
func NewClientFromCfg(endpoints []string, dialTimeout time.Duration, root string, security *tls.Config) (*Client, error) {
cli, err := clientv3.New(clientv3.Config{
Endpoints: endpoints,
DialTimeout: dialTimeout,
TLS: security,
Endpoints: endpoints,
DialTimeout: dialTimeout,
TLS: security,
AutoSyncInterval: 30 * time.Second,
})
if err != nil {
return nil, errors.Trace(err)
Expand Down