Skip to content

Commit

Permalink
linter: check AutoSyncInterval for etcd config (#44081) (#44093)
Browse files Browse the repository at this point in the history
close #42643
  • Loading branch information
ti-chi-bot authored Jun 1, 2023
1 parent c66ac24 commit 653370a
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 7 deletions.
1 change: 1 addition & 0 deletions build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ nogo(
"//build/linter/asciicheck",
"//build/linter/bodyclose",
"//build/linter/durationcheck",
"//build/linter/etcdconfig",
"//build/linter/exportloopref",
"//build/linter/forcetypeassert",
"//build/linter/gofmt",
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 @@ -1019,5 +1019,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
5 changes: 3 additions & 2 deletions meta/autoid/autoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,9 @@ func newSinglePointAlloc(store kv.Storage, dbID, tblID int64, isUnsigned bool) *
}
if len(addrs) > 0 {
etcdCli, err := clientv3.New(clientv3.Config{
Endpoints: addrs,
TLS: ebd.TLSConfig(),
Endpoints: addrs,
TLS: ebd.TLSConfig(),
AutoSyncInterval: 30 * time.Second,
})
if err != nil {
logutil.BgLogger().Error("[autoid client] fail to connect etcd, fallback to default", zap.Error(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 @@ -79,9 +79,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

0 comments on commit 653370a

Please sign in to comment.