diff --git a/.golangci.yml b/.golangci.yml index 4621e5c..e4ee4ab 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -4,14 +4,19 @@ linters: disable: - errcheck enable: + - bodyclose + - containedctx - contextcheck + - dogsled - durationcheck - errname + - errorlint - execinquery - gofmt - ifshort - misspell - nilerr + - nilnil - noctx - nolintlint - nosprintfhostport @@ -19,6 +24,7 @@ linters: - rowserrcheck - sqlclosecheck - unconvert + - unparam - unused - wastedassign - whitespace diff --git a/go/base/context.go b/go/base/context.go index f3fe712..270b7a0 100644 --- a/go/base/context.go +++ b/go/base/context.go @@ -858,7 +858,7 @@ func (this *MigrationContext) ReadConfigFile() error { if cfg.Section("osc").HasKey("chunk_size") { this.config.Osc.Chunk_Size, err = cfg.Section("osc").Key("chunk_size").Int64() if err != nil { - return fmt.Errorf("Unable to read osc chunk size: %s", err.Error()) + return fmt.Errorf("Unable to read osc chunk size: %w", err) } } @@ -873,7 +873,7 @@ func (this *MigrationContext) ReadConfigFile() error { if cfg.Section("osc").HasKey("max_lag_millis") { this.config.Osc.Max_Lag_Millis, err = cfg.Section("osc").Key("max_lag_millis").Int64() if err != nil { - return fmt.Errorf("Unable to read max lag millis: %s", err.Error()) + return fmt.Errorf("Unable to read max lag millis: %w", err) } } diff --git a/go/logic/applier.go b/go/logic/applier.go index 89e1995..50fd9bd 100644 --- a/go/logic/applier.go +++ b/go/logic/applier.go @@ -1134,7 +1134,7 @@ func (this *Applier) ApplyDMLEventQueries(dmlEvents [](*binlog.BinlogDMLEvent)) } result, err := tx.Exec(buildResult.query, buildResult.args...) if err != nil { - err = fmt.Errorf("%s; query=%s; args=%+v", err.Error(), buildResult.query, buildResult.args) + err = fmt.Errorf("%w; query=%s; args=%+v", err, buildResult.query, buildResult.args) return rollback(err) } diff --git a/go/logic/inspect.go b/go/logic/inspect.go index 4df80fe..3ece8ab 100644 --- a/go/logic/inspect.go +++ b/go/logic/inspect.go @@ -8,6 +8,7 @@ package logic import ( "context" gosql "database/sql" + "errors" "fmt" "reflect" "strings" @@ -554,13 +555,11 @@ func (this *Inspector) CountTableRows(ctx context.Context) error { query := fmt.Sprintf(`select /* gh-ost */ count(*) as count_rows from %s.%s`, sql.EscapeName(this.migrationContext.DatabaseName), sql.EscapeName(this.migrationContext.OriginalTableName)) var rowsEstimate int64 if err := conn.QueryRowContext(ctx, query).Scan(&rowsEstimate); err != nil { - switch err { - case context.Canceled, context.DeadlineExceeded: + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { this.migrationContext.Log.Infof("exact row count cancelled (%s), likely because I'm about to cut over. I'm going to kill that query.", ctx.Err()) return mysql.Kill(this.db, connectionID) - default: - return err } + return err } // row count query finished. nil out the cancel func, so the main migration thread diff --git a/go/logic/migrator.go b/go/logic/migrator.go index ccaf160..b443d69 100644 --- a/go/logic/migrator.go +++ b/go/logic/migrator.go @@ -391,9 +391,9 @@ func (this *Migrator) Migrate() (err error) { if err := this.applier.ReadMigrationRangeValues(); err != nil { return err } - if err := this.initiateThrottler(); err != nil { - return err - } + + this.initiateThrottler() + if err := this.hooksExecutor.onBeforeRowCopy(); err != nil { return err } @@ -1096,7 +1096,7 @@ func (this *Migrator) addDMLEventsListener() error { } // initiateThrottler kicks in the throttling collection and the throttling checks. -func (this *Migrator) initiateThrottler() error { +func (this *Migrator) initiateThrottler() { this.throttler = NewThrottler(this.migrationContext, this.applier, this.inspector, this.appVersion) go this.throttler.initiateThrottlerCollection(this.firstThrottlingCollected) @@ -1106,8 +1106,6 @@ func (this *Migrator) initiateThrottler() error { <-this.firstThrottlingCollected // other, general metrics this.migrationContext.Log.Infof("First throttle metrics collected") go this.throttler.initiateThrottlerChecks() - - return nil } func (this *Migrator) initiateApplier() error { diff --git a/go/logic/throttler.go b/go/logic/throttler.go index 1e7bc97..9c8dcfc 100644 --- a/go/logic/throttler.go +++ b/go/logic/throttler.go @@ -308,6 +308,8 @@ func (this *Throttler) collectThrottleHTTPStatus(firstThrottlingCollected chan<- if err != nil { return false, err } + defer resp.Body.Close() + atomic.StoreInt64(&this.migrationContext.ThrottleHTTPStatusCode, int64(resp.StatusCode)) return false, nil } diff --git a/go/sql/parser.go b/go/sql/parser.go index a72af33..2ddc60f 100644 --- a/go/sql/parser.go +++ b/go/sql/parser.go @@ -62,7 +62,7 @@ func NewParserFromAlterStatement(alterStatement string) *AlterTableParser { return parser } -func (this *AlterTableParser) tokenizeAlterStatement(alterStatement string) (tokens []string, err error) { +func (this *AlterTableParser) tokenizeAlterStatement(alterStatement string) (tokens []string) { terminatingQuote := rune(0) f := func(c rune) bool { switch { @@ -86,7 +86,7 @@ func (this *AlterTableParser) tokenizeAlterStatement(alterStatement string) (tok for i := range tokens { tokens[i] = strings.TrimSpace(tokens[i]) } - return tokens, nil + return tokens } func (this *AlterTableParser) sanitizeQuotesFromAlterStatement(alterStatement string) (strippedStatement string) { @@ -95,7 +95,7 @@ func (this *AlterTableParser) sanitizeQuotesFromAlterStatement(alterStatement st return strippedStatement } -func (this *AlterTableParser) parseAlterToken(alterToken string) (err error) { +func (this *AlterTableParser) parseAlterToken(alterToken string) { { // rename allStringSubmatch := renameColumnRegexp.FindAllStringSubmatch(alterToken, -1) @@ -131,7 +131,6 @@ func (this *AlterTableParser) parseAlterToken(alterToken string) (err error) { this.isAutoIncrementDefined = true } } - return nil } func (this *AlterTableParser) ParseAlterStatement(alterStatement string) (err error) { @@ -151,8 +150,7 @@ func (this *AlterTableParser) ParseAlterStatement(alterStatement string) (err er break } } - alterTokens, _ := this.tokenizeAlterStatement(this.alterStatementOptions) - for _, alterToken := range alterTokens { + for _, alterToken := range this.tokenizeAlterStatement(this.alterStatementOptions) { alterToken = this.sanitizeQuotesFromAlterStatement(alterToken) this.parseAlterToken(alterToken) this.alterTokens = append(this.alterTokens, alterToken) diff --git a/go/sql/parser_test.go b/go/sql/parser_test.go index 79a9b7b..df92842 100644 --- a/go/sql/parser_test.go +++ b/go/sql/parser_test.go @@ -99,37 +99,37 @@ func TestTokenizeAlterStatement(t *testing.T) { parser := NewAlterTableParser() { alterStatement := "add column t int" - tokens, _ := parser.tokenizeAlterStatement(alterStatement) + tokens := parser.tokenizeAlterStatement(alterStatement) test.S(t).ExpectTrue(reflect.DeepEqual(tokens, []string{"add column t int"})) } { alterStatement := "add column t int, change column i int" - tokens, _ := parser.tokenizeAlterStatement(alterStatement) + tokens := parser.tokenizeAlterStatement(alterStatement) test.S(t).ExpectTrue(reflect.DeepEqual(tokens, []string{"add column t int", "change column i int"})) } { alterStatement := "add column t int, change column i int 'some comment'" - tokens, _ := parser.tokenizeAlterStatement(alterStatement) + tokens := parser.tokenizeAlterStatement(alterStatement) test.S(t).ExpectTrue(reflect.DeepEqual(tokens, []string{"add column t int", "change column i int 'some comment'"})) } { alterStatement := "add column t int, change column i int 'some comment, with comma'" - tokens, _ := parser.tokenizeAlterStatement(alterStatement) + tokens := parser.tokenizeAlterStatement(alterStatement) test.S(t).ExpectTrue(reflect.DeepEqual(tokens, []string{"add column t int", "change column i int 'some comment, with comma'"})) } { alterStatement := "add column t int, add column d decimal(10,2)" - tokens, _ := parser.tokenizeAlterStatement(alterStatement) + tokens := parser.tokenizeAlterStatement(alterStatement) test.S(t).ExpectTrue(reflect.DeepEqual(tokens, []string{"add column t int", "add column d decimal(10,2)"})) } { alterStatement := "add column t int, add column e enum('a','b','c')" - tokens, _ := parser.tokenizeAlterStatement(alterStatement) + tokens := parser.tokenizeAlterStatement(alterStatement) test.S(t).ExpectTrue(reflect.DeepEqual(tokens, []string{"add column t int", "add column e enum('a','b','c')"})) } { alterStatement := "add column t int(11), add column e enum('a','b','c')" - tokens, _ := parser.tokenizeAlterStatement(alterStatement) + tokens := parser.tokenizeAlterStatement(alterStatement) test.S(t).ExpectTrue(reflect.DeepEqual(tokens, []string{"add column t int(11)", "add column e enum('a','b','c')"})) } }