Skip to content

Commit 3714915

Browse files
authored
*: fix golangci-lint issues and delete unused code (#5037)
ref #5032
1 parent 136d2d3 commit 3714915

90 files changed

Lines changed: 481 additions & 635 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.golangci.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ linters:
1111
- bodyclose
1212
# copyloopvar: detects redundant "x := x" in Go 1.22+ loop variables.
1313
- copyloopvar
14+
# depguard: forbids direct imports that bypass repository wrappers.
15+
- depguard
1416
# durationcheck: checks for suspicious duration multiplication.
1517
- durationcheck
1618
# errname: checks sentinel error naming (Err prefix, Error suffix).
@@ -19,6 +21,8 @@ linters:
1921
- errorlint
2022
# gosec: inspects source code for security problems.
2123
- gosec
24+
# importas: enforces consistent import aliases.
25+
- importas
2226
# misspell: finds commonly misspelled English words.
2327
- misspell
2428
# nilerr: finds code that returns nil even if err is not nil.
@@ -35,6 +39,23 @@ linters:
3539
- unconvert
3640

3741
settings:
42+
depguard:
43+
rules:
44+
no-direct-errors:
45+
files:
46+
- $all
47+
- '!pkg/errors/**'
48+
deny:
49+
- pkg: errors$
50+
desc: use github.com/pingcap/ticdc/pkg/errors outside pkg/errors
51+
- pkg: github.com/pingcap/errors$
52+
desc: use github.com/pingcap/ticdc/pkg/errors outside pkg/errors
53+
54+
importas:
55+
alias:
56+
- pkg: github.com/pingcap/ticdc/pkg/errors
57+
alias: ""
58+
3859
revive:
3960
rules:
4061
- name: blank-imports
@@ -67,10 +88,12 @@ linters:
6788
- G104
6889

6990
# ST1000: don't require package comments.
91+
# ST1003: don't enforce naming conventions on legacy identifiers.
7092
staticcheck:
7193
checks:
7294
- all
7395
- -ST1000
96+
- -ST1003
7497

7598
exclusions:
7699
# Strict: exclude all known generated file patterns.

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Phony targets are targets that are not associated with files.
22
# Add new phony targets here to make them available in the `make` command.
3-
.PHONY: clean fmt check check-static tidy \
3+
.PHONY: clean fmt check check-static local-static-check tidy \
44
generate-protobuf generate_mock \
55
cdc kafka_consumer storage_consumer pulsar_consumer filter_helper \
66
prepare_test_binaries \
@@ -320,6 +320,14 @@ else
320320
tools/bin/golangci-lint run --timeout 10m0s
321321
endif
322322

323+
# Lint only code changed on the current branch (vs upstream/master by default).
324+
# Override base with LINT_BASE=<ref>.
325+
# make local-static-check
326+
# make local-static-check LINT_BASE=HEAD~3
327+
local-static-check: tools/bin/golangci-lint
328+
$(eval BASE := $(if $(LINT_BASE),$(LINT_BASE),upstream/master))
329+
tools/bin/golangci-lint run --timeout 10m0s --new-from-rev=$(BASE)
330+
323331
check-ticdc-dashboard:
324332
@echo "check-ticdc-dashboard"
325333
@./scripts/check-ticdc-dashboard.sh

api/status/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (h *statusAPI) handleDebugInfo(w http.ResponseWriter, req *http.Request) {
8787
h.writeEtcdInfo(ctx, h.server.GetEtcdClient(), w)
8888
}
8989

90-
func (h *statusAPI) handleStatus(w http.ResponseWriter, req *http.Request) {
90+
func (h *statusAPI) handleStatus(w http.ResponseWriter, _ *http.Request) {
9191
st := status{
9292
Version: version.ReleaseVersion,
9393
GitHash: version.GitHash,

api/v2/model.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/pingcap/ticdc/pkg/errors"
2525
"github.com/pingcap/ticdc/pkg/integrity"
2626
"github.com/pingcap/ticdc/pkg/liveness"
27-
"github.com/pingcap/ticdc/pkg/security"
2827
"github.com/pingcap/ticdc/pkg/util"
2928
)
3029

@@ -85,12 +84,6 @@ type VerifyTableConfig struct {
8584
SinkURI string `json:"sink_uri"`
8685
}
8786

88-
func getDefaultVerifyTableConfig() *VerifyTableConfig {
89-
return &VerifyTableConfig{
90-
ReplicaConfig: GetDefaultReplicaConfig(),
91-
}
92-
}
93-
9487
// ResumeChangefeedConfig is used by resume changefeed api
9588
type ResumeChangefeedConfig struct {
9689
PDConfig
@@ -1350,18 +1343,6 @@ type SyncedStatus struct {
13501343
Info string `json:"info"`
13511344
}
13521345

1353-
// toCredential generates a security.Credential from a PDConfig
1354-
func (cfg *PDConfig) toCredential() *security.Credential {
1355-
credential := &security.Credential{
1356-
CAPath: cfg.CAPath,
1357-
CertPath: cfg.CertPath,
1358-
KeyPath: cfg.KeyPath,
1359-
}
1360-
credential.CertAllowedCN = make([]string, len(cfg.CertAllowedCN))
1361-
copy(credential.CertAllowedCN, cfg.CertAllowedCN)
1362-
return credential
1363-
}
1364-
13651346
// Marshal returns the json marshal format of a ChangeFeedInfo
13661347
func (info *ChangeFeedInfo) Marshal() (string, error) {
13671348
data, err := json.Marshal(info)

cmd/cdc/cli/cli_changefeed_helper.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414
package cli
1515

1616
import (
17-
"bufio"
1817
"fmt"
19-
"os"
2018
"strings"
2119
"time"
2220

2321
v2 "github.com/pingcap/ticdc/api/v2"
24-
cerror "github.com/pingcap/ticdc/pkg/errors"
22+
"github.com/pingcap/ticdc/pkg/errors"
2523
"github.com/spf13/cobra"
2624
"github.com/tikv/client-go/v2/oracle"
2725
)
@@ -32,15 +30,6 @@ const (
3230
tsGapWarning = 86400 * 1000
3331
)
3432

35-
func readInput() (string, error) {
36-
reader := bufio.NewReader(os.Stdin)
37-
msg, err := reader.ReadString('\n')
38-
if err != nil {
39-
return "", err
40-
}
41-
return strings.TrimSpace(msg), nil
42-
}
43-
4433
func readYOrN(cmd *cobra.Command) bool {
4534
var yOrN string
4635
_, err := fmt.Scan(&yOrN)
@@ -66,7 +55,7 @@ func confirmLargeDataGap(cmd *cobra.Command, currentPhysical int64, startTs uint
6655
confirmed := readYOrN(cmd)
6756
if !confirmed {
6857
cmd.Printf("Abort changefeed %s.\n", command)
69-
return cerror.ErrCliAborted.FastGenByArgs(fmt.Sprintf("cli changefeed %s", command))
58+
return errors.ErrCliAborted.FastGen("cli changefeed %s", command)
7059
}
7160
}
7261

@@ -84,7 +73,7 @@ func confirmOverwriteCheckpointTs(
8473
confirmed := readYOrN(cmd)
8574
if !confirmed {
8675
cmd.Printf("Abort changefeed resume.\n")
87-
return cerror.ErrCliAborted.FastGenByArgs("cli changefeed resume")
76+
return errors.ErrCliAborted.FastGenByArgs("cli changefeed resume")
8877
}
8978

9079
return nil
@@ -99,7 +88,7 @@ func confirmIgnoreIneligibleTables(cmd *cobra.Command) (bool, error) {
9988
confirmed := readYOrN(cmd)
10089
if !confirmed {
10190
cmd.Printf("No changefeed is created because you don't want to ignore some tables.\n")
102-
return false, cerror.ErrCliAborted.FastGenByArgs("cli changefeed create")
91+
return false, errors.ErrCliAborted.FastGenByArgs("cli changefeed create")
10392
}
10493

10594
return true, nil

cmd/config-converter/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func main() {
5858
}
5959
}
6060

61-
func run(cmd *cobra.Command, args []string) {
61+
func run(_ *cobra.Command, _ []string) {
6262
if cfgPath != "" && modelPath != "" {
6363
fmt.Fprintln(os.Stderr, "can't specify both config and model")
6464
os.Exit(ExitCodeInvalidFlag)

cmd/storage-consumer/consumer.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import (
2121
"strings"
2222
"time"
2323

24-
"github.com/pingcap/errors"
2524
"github.com/pingcap/log"
2625
"github.com/pingcap/ticdc/cmd/util"
2726
"github.com/pingcap/ticdc/downstreamadapter/sink"
2827
"github.com/pingcap/ticdc/downstreamadapter/sink/helper"
2928
commonType "github.com/pingcap/ticdc/pkg/common"
3029
"github.com/pingcap/ticdc/pkg/common/event"
3130
"github.com/pingcap/ticdc/pkg/config"
31+
"github.com/pingcap/ticdc/pkg/errors"
3232
"github.com/pingcap/ticdc/pkg/sink/cloudstorage"
3333
"github.com/pingcap/ticdc/pkg/sink/codec/canal"
3434
"github.com/pingcap/ticdc/pkg/sink/codec/common"
@@ -244,7 +244,7 @@ func (c *consumer) getNewFiles(
244244
origDMLIdxMap[k] = m
245245
}
246246

247-
err := c.externalStorage.WalkDir(ctx, opt, func(path string, size int64) error {
247+
err := c.externalStorage.WalkDir(ctx, opt, func(path string, _ int64) error {
248248
if cloudstorage.IsSchemaFile(path) {
249249
err := c.parseSchemaFilePath(ctx, path)
250250
if err != nil {
@@ -713,7 +713,9 @@ func (c *consumer) handleNewFiles(
713713
}
714714
}
715715
}
716-
c.flushDMLEvents(ctx, tableID)
716+
if err := c.flushDMLEvents(ctx, tableID); err != nil {
717+
return err
718+
}
717719
}
718720

719721
return nil

cmd/storage-consumer/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/pingcap/log"
2828
"github.com/pingcap/ticdc/pkg/config"
29+
"github.com/pingcap/ticdc/pkg/errors"
2930
"github.com/pingcap/ticdc/pkg/logger"
3031
"github.com/pingcap/ticdc/pkg/version"
3132
"go.uber.org/zap"
@@ -103,7 +104,7 @@ func main() {
103104
if consumer != nil {
104105
consumer.sink.Close()
105106
}
106-
if err != nil && err != context.Canceled {
107+
if err != nil && !errors.Is(err, context.Canceled) {
107108
return 1
108109
}
109110
return 0

coordinator/controller.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func NewController(
174174
// detect the capture changes
175175
c.nodeManager.RegisterNodeChangeHandler(
176176
nodeChangeHandlerID,
177-
func(allNodes map[node.ID]*node.Info) {
177+
func(_ map[node.ID]*node.Info) {
178178
c.nodeChanged.Lock()
179179
defer c.nodeChanged.Unlock()
180180
c.nodeChanged.changed = true
@@ -355,9 +355,14 @@ func (c *Controller) RequestResolvedTsFromLogCoordinator(ctx context.Context, ch
355355
changefeedID := c.changefeedDB.GetChangefeedIDByName(changefeedDisplayName)
356356
ids := c.nodeManager.GetAliveNodeIDs()
357357
for _, id := range ids {
358-
c.messageCenter.SendEvent(messaging.NewSingleTargetMessage(id, messaging.LogCoordinatorTopic, &heartbeatpb.LogCoordinatorResolvedTsRequest{
358+
if err := c.messageCenter.SendEvent(messaging.NewSingleTargetMessage(id, messaging.LogCoordinatorTopic, &heartbeatpb.LogCoordinatorResolvedTsRequest{
359359
ChangefeedID: changefeedID.ToPB(),
360-
}))
360+
})); err != nil {
361+
log.Warn("failed to request resolved ts from log coordinator",
362+
zap.Stringer("target", id),
363+
zap.String("changefeed", changefeedID.DisplayName.String()),
364+
zap.Error(err))
365+
}
361366
}
362367

363368
// wait for some time to get the resolved ts
@@ -686,7 +691,7 @@ func (c *Controller) CreateChangefeed(ctx context.Context, info *config.ChangeFe
686691
return errors.Trace(ctx.Err())
687692
case <-ticker.C:
688693
log.Warn("changefeed is in scheduling, wait a moment", zap.String("changefeed", info.ChangefeedID.DisplayName.String()))
689-
count += 1
694+
count++
690695
}
691696
}
692697

@@ -728,7 +733,7 @@ func (c *Controller) RemoveChangefeed(ctx context.Context, id common.ChangeFeedI
728733
case <-ctx.Done():
729734
return 0, errors.Trace(ctx.Err())
730735
case <-ticker.C:
731-
count += 1
736+
count++
732737
log.Info("wait for stop changefeed operator finished", zap.Int("count", count), zap.Any("id", id))
733738
}
734739
}
@@ -766,7 +771,7 @@ func (c *Controller) PauseChangefeed(ctx context.Context, id common.ChangeFeedID
766771
case <-ctx.Done():
767772
return errors.Trace(ctx.Err())
768773
case <-ticker.C:
769-
count += 1
774+
count++
770775
log.Info("wait for stop changefeed operator finished", zap.Int("count", count), zap.Any("id", id))
771776
}
772777
}

coordinator/coordinator_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/pingcap/ticdc/pkg/common"
3636
appcontext "github.com/pingcap/ticdc/pkg/common/context"
3737
"github.com/pingcap/ticdc/pkg/config"
38+
"github.com/pingcap/ticdc/pkg/errors"
3839
"github.com/pingcap/ticdc/pkg/etcd"
3940
"github.com/pingcap/ticdc/pkg/eventservice"
4041
"github.com/pingcap/ticdc/pkg/messaging"
@@ -682,7 +683,7 @@ func TestConcurrentStopAndSendEvents(t *testing.T) {
682683
ctxRun, cancelRun := context.WithCancel(ctx)
683684
go func() {
684685
err := cr.Run(ctxRun)
685-
if err != nil && err != context.Canceled {
686+
if err != nil && !errors.Is(err, context.Canceled) {
686687
t.Errorf("Coordinator Run returned unexpected error: %v", err)
687688
}
688689
}()
@@ -715,7 +716,7 @@ func TestConcurrentStopAndSendEvents(t *testing.T) {
715716

716717
// Use recvMessages to send event to channel
717718
err := co.recvMessages(ctx, msg)
718-
if err != nil && err != context.Canceled {
719+
if err != nil && !errors.Is(err, context.Canceled) {
719720
t.Logf("Failed to send event in goroutine %d: %v", id, err)
720721
}
721722

0 commit comments

Comments
 (0)