Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
2b58575
*: fix lightning unparam
YuJuncen Mar 1, 2021
f50fac7
*: fix lightning maligned
YuJuncen Mar 1, 2021
0404f77
*: fix lightning gocritic
YuJuncen Mar 1, 2021
20c7b3f
lightning: fix stylecheck
YuJuncen Mar 1, 2021
4e8b7c7
lightning: fix golint
YuJuncen Mar 1, 2021
bda1d1f
lightning: disable noctx for tests
YuJuncen Mar 1, 2021
3836715
lightning: fixs rowsercheck
YuJuncen Mar 1, 2021
0b4938f
lightning: fixed test and fmt
YuJuncen Mar 1, 2021
592559a
*: disable errorlint
YuJuncen Mar 2, 2021
929bb64
*: fix errorcheck
YuJuncen Mar 2, 2021
b552dfb
*: fix deadcode
YuJuncen Mar 2, 2021
daa8d54
lightning: fix structcheck
YuJuncen Mar 2, 2021
b123583
lightning: fix duplicated code and cyclomatic complexity too high
YuJuncen Mar 2, 2021
5200410
lightning: fix noinit and govet
YuJuncen Mar 2, 2021
99c75de
lightning: fixed goconst, nakedret, interfacer
YuJuncen Mar 2, 2021
38600e6
lightning: fixed scopelint and static check
YuJuncen Mar 2, 2021
bcc6528
lightning: disabled wrapcheck
YuJuncen Mar 2, 2021
a58edbb
Merge branch 'master' into lightning-lint
YuJuncen Mar 2, 2021
35b11bb
*: fix make file format
YuJuncen Mar 2, 2021
e9c44d0
Merge branch 'lightning-lint' of https://github.com/YuJuncen/br into …
YuJuncen Mar 2, 2021
1fc6996
Merge branch 'master' into lightning-lint
YuJuncen Mar 2, 2021
ea04beb
Merge branch 'master' into lightning-lint
YuJuncen Mar 2, 2021
c4e36ba
Merge branch 'master' into lightning-lint
YuJuncen Mar 3, 2021
9f23fbf
Merge branch 'master' of https://github.com/pingcap/br into lightning…
YuJuncen Mar 4, 2021
8c17234
Merge branch 'master' into lightning-lint
YuJuncen Mar 8, 2021
caf5e22
lightning: address comments
YuJuncen Mar 9, 2021
ae58bac
Merge branch 'master' into lightning-lint
YuJuncen Mar 9, 2021
80b8c18
lightning: add nolint:interfacer for TestClose
YuJuncen Mar 9, 2021
0521944
Merge branch 'lightning-lint' of https://github.com/YuJuncen/br into …
YuJuncen Mar 9, 2021
90cc82c
go.sum: run go mod tidy
YuJuncen Mar 9, 2021
69b5f96
go.sum: run go mod tidy
YuJuncen Mar 9, 2021
3cdd59e
lightning: address comments
YuJuncen Mar 9, 2021
d3efc70
test: added a check to encode with _tidb_rows
YuJuncen Mar 9, 2021
b6bd8dc
Merge branch 'master' into lightning-lint
YuJuncen Mar 10, 2021
11f5728
lightning: merge master and resolve some conflicts
YuJuncen Mar 15, 2021
0f3276a
Merge branch 'master' into lightning-lint
YuJuncen Mar 15, 2021
2c789ed
lightning/restore: rollback a refactor
YuJuncen Mar 16, 2021
9a58d6b
Merge branch 'master' into lightning-lint
YuJuncen Mar 17, 2021
9a2d1e7
lightning: merge master
YuJuncen Mar 17, 2021
330ed2f
lightning: fix some new lints
YuJuncen Mar 17, 2021
5afa235
Merge branch 'lightning-lint' of https://github.com/YuJuncen/br into …
YuJuncen Mar 17, 2021
ea537cd
Merge branch 'master' into lightning-lint
YuJuncen Mar 17, 2021
a4fb6dd
*: merge from 'master'.
YuJuncen Mar 29, 2021
099b5ba
Apply suggestions from code review
YuJuncen Mar 29, 2021
e3f45d1
Merge branch 'master' into lightning-lint
YuJuncen Mar 30, 2021
65d7ab9
Merge branch 'master' into lightning-lint
YuJuncen Apr 2, 2021
6dd05e7
*: fix some new linter error
YuJuncen Apr 2, 2021
e6e150a
*: merge from master
YuJuncen Apr 6, 2021
9baf6ea
Update pkg/lightning/checkpoints/checkpoints.go
YuJuncen Apr 7, 2021
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
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ static: prepare tools
@# exhaustivestruct - Protobuf structs have hidden fields, like "XXX_NoUnkeyedLiteral"
@# exhaustive - no need to check exhaustiveness of enum switch statements
@# gosec - too many false positive
@# errorlint - pingcap/errors is incompatible with std errors.
@# wrapcheck - there are too many unwrapped errors in tidb-lightning
CGO_ENABLED=0 tools/bin/golangci-lint run --enable-all --deadline 120s \
--disable gochecknoglobals \
--disable goimports \
Expand All @@ -206,7 +208,9 @@ static: prepare tools
--disable exhaustive \
--disable godot \
--disable gosec \
$$($(PACKAGE_DIRECTORIES) | grep -v "lightning")
--disable errorlint \
--disable wrapcheck \
$(PACKAGE_DIRECTORIES)
# pingcap/errors APIs are mixed with multiple patterns 'pkg/errors',
# 'juju/errors' and 'pingcap/parser'. To avoid confusion and mistake,
# we only allow a subset of APIs, that's "Normalize|Annotate|Trace|Cause".
Expand Down
9 changes: 5 additions & 4 deletions cmd/tidb-lightning-ctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func run() error {
// there is a check if `-d` points to a valid storage, and '' is not.
// since tidb-lightning-ctl does not need `-d` we change the default to a valid but harmless value.
dFlag := fs.Lookup("d")
dFlag.Value.Set("noop://")
_ = dFlag.Value.Set("noop://")
dFlag.DefValue = "noop://"

compact = fs.Bool("compact", false, "do manual compaction on the target cluster")
Expand Down Expand Up @@ -247,8 +247,8 @@ func checkpointErrorDestroy(ctx context.Context, cfg *config.Config, tls *common
if err != nil {
fmt.Fprintln(os.Stderr, "* Encountered error while closing engine:", err)
lastErr = err
} else {
closedEngine.Cleanup(ctx)
} else if err := closedEngine.Cleanup(ctx); err != nil {
lastErr = err
}
}
}
Expand All @@ -263,7 +263,7 @@ func checkpointErrorDestroy(ctx context.Context, cfg *config.Config, tls *common
for engineID := table.MinEngineID; engineID <= table.MaxEngineID; engineID++ {
fmt.Fprintln(os.Stderr, "Closing and cleaning up engine:", table.TableName, engineID)
_, eID := backend.MakeUUID(table.TableName, engineID)
file := local.File{Uuid: eID}
file := local.File{UUID: eID}
err := file.Cleanup(cfg.TikvImporter.SortedKVDir)
if err != nil {
fmt.Fprintln(os.Stderr, "* Encountered error while cleanup engine:", err)
Expand Down Expand Up @@ -321,6 +321,7 @@ func checkpointDump(ctx context.Context, cfg *config.Config, dumpFolder string)
}

func getLocalStoringTables(ctx context.Context, cfg *config.Config) (err2 error) {
//nolint:prealloc // This is a placeholder.
var tables []string
defer func() {
if err2 == nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/tidb-lightning-ctl/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"testing"
)

func TestRunMain(t *testing.T) {
func TestRunMain(_ *testing.T) {
if _, isIntegrationTest := os.LookupEnv("INTEGRATION_TEST"); !isIntegrationTest {
// override exit to pass unit test.
exit = func(code int) {}
Expand Down
11 changes: 5 additions & 6 deletions cmd/tidb-lightning/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@ func main() {
err = func() error {
if globalCfg.App.ServerMode {
return app.RunServer()
} else {
cfg := config.NewConfig()
if err := cfg.LoadFromGlobal(globalCfg); err != nil {
return err
}
return app.RunOnce(context.Background(), cfg, nil)
}
cfg := config.NewConfig()
if err := cfg.LoadFromGlobal(globalCfg); err != nil {
return err
}
return app.RunOnce(context.Background(), cfg, nil)
}()

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/tidb-lightning/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"testing"
)

func TestRunMain(t *testing.T) {
func TestRunMain(_ *testing.T) {
if _, isIntegrationTest := os.LookupEnv("INTEGRATION_TEST"); !isIntegrationTest {
// override exit to pass unit test.
exit = func(code int) {}
Expand Down
3 changes: 2 additions & 1 deletion pkg/lightning/backend/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Test(t *testing.T) {
// FIXME: Cannot use the real SetUpTest/TearDownTest to set up the mock
// otherwise the mock error will be ignored.

func (s *backendSuite) setUpTest(c *C) {
func (s *backendSuite) setUpTest(c gomock.TestReporter) {
s.controller = gomock.NewController(c)
s.mockBackend = mock.NewMockBackend(s.controller)
s.backend = backend.MakeBackend(s.mockBackend)
Expand Down Expand Up @@ -298,6 +298,7 @@ func (s *backendSuite) TestImportFailedRecovered(c *C) {
c.Assert(err, IsNil)
}

//nolint:interfacer // change test case signature causes check panicking.
func (s *backendSuite) TestClose(c *C) {
s.setUpTest(c)
defer s.tearDownTest()
Expand Down
9 changes: 5 additions & 4 deletions pkg/lightning/backend/importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ outside:

func (importer *importer) WriteRowsToImporter(
ctx context.Context,
//nolint:interfacer // false positive
engineUUID uuid.UUID,
ts uint64,
rows kv.Rows,
Expand Down Expand Up @@ -326,18 +327,18 @@ func (importer *importer) ResetEngine(context.Context, uuid.UUID) error {
}

func (importer *importer) LocalWriter(ctx context.Context, engineUUID uuid.UUID) (backend.EngineWriter, error) {
return &ImporterWriter{importer: importer, engineUUID: engineUUID}, nil
return &Writer{importer: importer, engineUUID: engineUUID}, nil
}

type ImporterWriter struct {
type Writer struct {
importer *importer
engineUUID uuid.UUID
}

func (w *ImporterWriter) Close() error {
func (w *Writer) Close() error {
return nil
}

func (w *ImporterWriter) AppendRows(ctx context.Context, tableName string, columnNames []string, ts uint64, rows kv.Rows) error {
func (w *Writer) AppendRows(ctx context.Context, tableName string, columnNames []string, ts uint64, rows kv.Rows) error {
return w.importer.WriteRows(ctx, w.engineUUID, tableName, columnNames, ts, rows)
}
20 changes: 12 additions & 8 deletions pkg/lightning/backend/kv/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (
"github.com/pingcap/tidb/sessionctx/variable"

"github.com/pingcap/br/pkg/lightning/common"
"github.com/pingcap/br/pkg/lightning/log"

"go.uber.org/zap"
)

// invalidIterator is a trimmed down Iterator type which is invalid.
Expand Down Expand Up @@ -115,12 +118,6 @@ type transaction struct {
kvUnionStore
}

func NewTransaction() *transaction {
return &transaction{
kvUnionStore: kvUnionStore{},
}
}

func (t *transaction) GetMemBuffer() kv.MemBuffer {
return &t.kvUnionStore.kvMemBuf
}
Expand Down Expand Up @@ -204,11 +201,18 @@ func newSession(options *SessionOptions) *session {
vars.SQLMode = sqlMode
if options.SysVars != nil {
for k, v := range options.SysVars {
vars.SetSystemVar(k, v)
if err := vars.SetSystemVar(k, v); err != nil {
log.L().DPanic("new session: failed to set system var",
log.ShortError(err),
zap.String("key", k))
}
}
}
vars.StmtCtx.TimeZone = vars.Location()
vars.SetSystemVar("timestamp", strconv.FormatInt(options.Timestamp, 10))
if err := vars.SetSystemVar("timestamp", strconv.FormatInt(options.Timestamp, 10)); err != nil {
log.L().Warn("new session: failed to set timestamp",
log.ShortError(err))
}
vars.TxnCtx = nil

s := &session{
Expand Down
45 changes: 28 additions & 17 deletions pkg/lightning/backend/kv/sql2kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func autoRandomIncrementBits(col *table.Column, randomBits int) int {
incrementalBits := typeBitsLength - randomBits
hasSignBit := !mysql.HasUnsignedFlag(col.Flag)
if hasSignBit {
incrementalBits -= 1
incrementalBits--
}
return incrementalBits
}
Expand Down Expand Up @@ -215,11 +215,13 @@ func (row RowArrayMarshaler) MarshalLogArray(encoder zapcore.ArrayEncoder) error
return err
}
}
encoder.AppendObject(zapcore.ObjectMarshalerFunc(func(enc zapcore.ObjectEncoder) error {
if err := encoder.AppendObject(zapcore.ObjectMarshalerFunc(func(enc zapcore.ObjectEncoder) error {
enc.AddString("kind", kindStr[kind])
enc.AddString("val", log.RedactString(str))
return nil
}))
})); err != nil {
return err
}
}
return nil
}
Expand Down Expand Up @@ -281,6 +283,7 @@ func MakeRowFromKvPairs(pairs []common.KvPair) Row {
// KvPairsFromRows converts a Rows instance constructed from MakeRowsFromKvPairs
// back into a slice of KvPair. This method panics if the Rows is not
// constructed in such way.
// nolint:golint // kv.KvPairsFromRows sounds good.
func KvPairsFromRows(rows Rows) []common.KvPair {
return []common.KvPair(rows.(kvPairs))
}
Expand All @@ -299,6 +302,7 @@ func (kvcodec *tableKVEncoder) Encode(

var value types.Datum
var err error
//nolint:prealloc // This is a placeholder.
var record []types.Datum

if kvcodec.recordCache != nil {
Expand All @@ -312,27 +316,28 @@ func (kvcodec *tableKVEncoder) Encode(
j := columnPermutation[i]
isAutoIncCol := mysql.HasAutoIncrementFlag(col.Flag)
isPk := mysql.HasPriKeyFlag(col.Flag)
if j >= 0 && j < len(row) {
switch {
case j >= 0 && j < len(row):
value, err = table.CastValue(kvcodec.se, row[j], col.ToInfo(), false, false)
if err == nil {
err = col.HandleBadNull(&value, kvcodec.se.vars.StmtCtx)
}
} else if isAutoIncCol {
case isAutoIncCol:
// we still need a conversion, e.g. to catch overflow with a TINYINT column.
value, err = table.CastValue(kvcodec.se, types.NewIntDatum(rowID), col.ToInfo(), false, false)
} else if isAutoRandom && isPk {
case isAutoRandom && isPk:
var val types.Datum
if mysql.HasUnsignedFlag(col.Flag) {
val = types.NewUintDatum(uint64(kvcodec.autoRandomHeaderBits | rowID))
} else {
val = types.NewIntDatum(kvcodec.autoRandomHeaderBits | rowID)
}
value, err = table.CastValue(kvcodec.se, val, col.ToInfo(), false, false)
} else if col.IsGenerated() {
case col.IsGenerated():
// inject some dummy value for gen col so that MutRowFromDatums below sees a real value instead of nil.
// if MutRowFromDatums sees a nil it won't initialize the underlying storage and cause SetDatum to panic.
value = types.GetMinValue(&col.FieldType)
} else {
default:
value, err = table.GetColDefaultValue(kvcodec.se, col.ToInfo())
}
if err != nil {
Expand All @@ -343,10 +348,14 @@ func (kvcodec *tableKVEncoder) Encode(

if isAutoRandom && isPk {
incrementalBits := autoRandomIncrementBits(col, int(kvcodec.tbl.Meta().AutoRandomBits))
kvcodec.tbl.RebaseAutoID(kvcodec.se, value.GetInt64()&((1<<incrementalBits)-1), false, autoid.AutoRandomType)
if err := kvcodec.tbl.RebaseAutoID(kvcodec.se, value.GetInt64()&((1<<incrementalBits)-1), false, autoid.AutoRandomType); err != nil {
return nil, errors.Trace(err)
}
}
if isAutoIncCol {
kvcodec.tbl.RebaseAutoID(kvcodec.se, value.GetInt64(), false, autoid.AutoIncrementType)
if err := kvcodec.tbl.RebaseAutoID(kvcodec.se, value.GetInt64(), false, autoid.AutoIncrementType); err != nil {
return nil, errors.Trace(err)
}
}
}

Expand All @@ -361,7 +370,9 @@ func (kvcodec *tableKVEncoder) Encode(
return nil, logKVConvertFailed(logger, row, j, ExtraHandleColumnInfo, err)
}
record = append(record, value)
kvcodec.tbl.RebaseAutoID(kvcodec.se, value.GetInt64(), false, autoid.RowIDAllocType)
if err := kvcodec.tbl.RebaseAutoID(kvcodec.se, value.GetInt64(), false, autoid.RowIDAllocType); err != nil {
return nil, errors.Trace(err)
}
}

if len(kvcodec.genCols) > 0 {
Expand Down Expand Up @@ -418,28 +429,28 @@ func (kvs kvPairs) ClassifyAndAppend(
*indices = indexKVs
}

func (totalKVs kvPairs) SplitIntoChunks(splitSize int) []Rows {
if len(totalKVs) == 0 {
func (kvs kvPairs) SplitIntoChunks(splitSize int) []Rows {
if len(kvs) == 0 {
return nil
}

res := make([]Rows, 0, 1)
i := 0
cumSize := 0

for j, pair := range totalKVs {
for j, pair := range kvs {
size := len(pair.Key) + len(pair.Val)
if i < j && cumSize+size > splitSize {
res = append(res, kvPairs(totalKVs[i:j]))
res = append(res, kvs[i:j])
i = j
cumSize = 0
}
cumSize += size
}

return append(res, kvPairs(totalKVs[i:]))
return append(res, kvs[i:])
}

func (kvs kvPairs) Clear() Rows {
return kvPairs(kvs[:0])
return kvs[:0]
}
9 changes: 5 additions & 4 deletions pkg/lightning/backend/kv/sql2kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (s *kvSuite) TestEncode(c *C) {
types.NewIntDatum(1),
types.NewStringDatum("invalid-pk"),
}
pairs, err = strictMode.Encode(logger, rowsWithPk, 2, []int{0, 1})
_, err = strictMode.Encode(logger, rowsWithPk, 2, []int{0, 1})
c.Assert(err, ErrorMatches, "failed to cast value as bigint\\(20\\) for column `_tidb_rowid`.*Truncated.*")

rowsWithPk2 := []types.Datum{
Expand All @@ -118,7 +118,7 @@ func (s *kvSuite) TestEncode(c *C) {
Timestamp: 1234567891,
})
c.Assert(err, IsNil)
pairs, err = mockMode.Encode(logger, rowsWithPk2, 2, []int{0, 1})
_, err = mockMode.Encode(logger, rowsWithPk2, 2, []int{0, 1})
c.Assert(err, ErrorMatches, "mock error")

// Non-strict mode
Expand Down Expand Up @@ -215,9 +215,9 @@ func (s *kvSuite) TestEncodeTimestamp(c *C) {
}))
}

func mockTableInfo(c *C, createSql string) *model.TableInfo {
func mockTableInfo(c *C, createSQL string) *model.TableInfo {
parser := parser.New()
node, err := parser.ParseOneStmt(createSql, "", "")
node, err := parser.ParseOneStmt(createSQL, "", "")
c.Assert(err, IsNil)
sctx := mock.NewContext()
info, err := ddl.MockTableInfo(sctx, node.(*ast.CreateTableStmt), 1)
Expand Down Expand Up @@ -399,6 +399,7 @@ func (s *benchSQL2KVSuite) SetUpTest(c *C) {
tbl, err := tables.TableFromMeta(NewPanickingAllocators(0), tableInfo)
c.Assert(err, IsNil)
s.encoder, err = NewTableKVEncoder(tbl, &SessionOptions{SysVars: map[string]string{"tidb_row_format_version": "2"}})
c.Assert(err, IsNil)
s.logger = log.Logger{Logger: zap.NewNop()}

// Prepare the row to insert.
Expand Down
Loading