Skip to content

Commit 2c90c07

Browse files
committed
Merge branch 'master' into release-4.0
2 parents 69444ab + 591406d commit 2c90c07

File tree

25 files changed

+619
-111
lines changed

25 files changed

+619
-111
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ backupmeta
99
coverage.txt
1010
docker/data/
1111
docker/logs/
12+
*.swp

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# BR
22

3-
[![Build Status](https://internal.pingcap.net/idc-jenkins/job/build_br_master/badge/icon)](https://internal.pingcap.net/idc-jenkins/job/build_br_master/)
3+
[![Build Status](https://internal.pingcap.net/idc-jenkins/job/build_br_multi_branch/job/master/badge/icon)](https://internal.pingcap.net/idc-jenkins/job/build_br_multi_branch/job/master/)
44
[![codecov](https://codecov.io/gh/pingcap/br/branch/master/graph/badge.svg)](https://codecov.io/gh/pingcap/br)
55
[![LICENSE](https://img.shields.io/github/license/pingcap/br.svg)](https://github.com/pingcap/br/blob/master/LICENSE)
66
[![Language](https://img.shields.io/badge/Language-Go-blue.svg)](https://golang.org/)

cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const (
4747
)
4848

4949
func timestampLogFileName() string {
50-
return filepath.Join(os.TempDir(), "br.log."+time.Now().Format(time.RFC3339))
50+
return filepath.Join(os.TempDir(), time.Now().Format("br.log.2006-01-02T15.04.05Z0700"))
5151
}
5252

5353
// AddFlags adds flags to the given cmd.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/onsi/gomega v1.8.1 // indirect
2222
github.com/pingcap/check v0.0.0-20200212061837-5e12011dc712
2323
github.com/pingcap/errors v0.11.5-0.20190809092503-95897b64e011
24-
github.com/pingcap/kvproto v0.0.0-20200330093347-98f910b71904
24+
github.com/pingcap/kvproto v0.0.0-20200423024253-3500763f0214
2525
github.com/pingcap/log v0.0.0-20200117041106-d28c14d3b1cd
2626
github.com/pingcap/parser v0.0.0-20200326020624-68d423641be5
2727
github.com/pingcap/pd/v4 v4.0.0-beta.1.0.20200305072537-61d9f9cc35d3

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ github.com/pingcap/kvproto v0.0.0-20200214064158-62d31900d88e/go.mod h1:IOdRDPLy
365365
github.com/pingcap/kvproto v0.0.0-20200221034943-a2aa1d1e20a8/go.mod h1:IOdRDPLyda8GX2hE/jO7gqaCV/PNFh8BZQCQZXfIOqI=
366366
github.com/pingcap/kvproto v0.0.0-20200330093347-98f910b71904 h1:pMFUXvhJ62hX8m0Q4RsL7L+hSW1mAMG26So5eFMoAtI=
367367
github.com/pingcap/kvproto v0.0.0-20200330093347-98f910b71904/go.mod h1:IOdRDPLyda8GX2hE/jO7gqaCV/PNFh8BZQCQZXfIOqI=
368+
github.com/pingcap/kvproto v0.0.0-20200423024253-3500763f0214 h1:UfGPQQ1EANWEk8e/11fYjo7E1K98c6+WDT4q5k5R3Ko=
369+
github.com/pingcap/kvproto v0.0.0-20200423024253-3500763f0214/go.mod h1:IOdRDPLyda8GX2hE/jO7gqaCV/PNFh8BZQCQZXfIOqI=
368370
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9 h1:AJD9pZYm72vMgPcQDww9rkZ1DnWfl0pXV3BOWlkYIjA=
369371
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
370372
github.com/pingcap/log v0.0.0-20200117041106-d28c14d3b1cd h1:CV3VsP3Z02MVtdpTMfEgRJ4T9NGgGTxdHpJerent7rM=

pkg/backup/client.go

Lines changed: 82 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ type ClientMgr interface {
4848
Close()
4949
}
5050

51+
// Checksum is the checksum of some backup files calculated by CollectChecksums.
52+
type Checksum struct {
53+
Crc64Xor uint64
54+
TotalKvs uint64
55+
TotalBytes uint64
56+
}
57+
5158
// Maximum total sleep time(in ms) for kv/cop commands.
5259
const (
5360
backupFineGrainedMaxBackoff = 80000
@@ -748,8 +755,59 @@ func SendBackup(
748755
return nil
749756
}
750757

751-
// FastChecksum check data integrity by xor all(sst_checksum) per table
752-
func (bc *Client) FastChecksum() (bool, error) {
758+
// ChecksumMatches tests whether the "local" checksum matches the checksum from TiKV.
759+
func (bc *Client) ChecksumMatches(local []Checksum) (bool, error) {
760+
if len(local) != len(bc.backupMeta.Schemas) {
761+
return false, nil
762+
}
763+
764+
for i, schema := range bc.backupMeta.Schemas {
765+
localChecksum := local[i]
766+
dbInfo := &model.DBInfo{}
767+
err := json.Unmarshal(schema.Db, dbInfo)
768+
if err != nil {
769+
log.Error("failed in fast checksum, and cannot parse db info.")
770+
return false, err
771+
}
772+
tblInfo := &model.TableInfo{}
773+
err = json.Unmarshal(schema.Table, tblInfo)
774+
if err != nil {
775+
log.Error("failed in fast checksum, and cannot parse table info.")
776+
return false, err
777+
}
778+
if localChecksum.Crc64Xor != schema.Crc64Xor ||
779+
localChecksum.TotalBytes != schema.TotalBytes ||
780+
localChecksum.TotalKvs != schema.TotalKvs {
781+
log.Error("failed in fast checksum",
782+
zap.Stringer("db", dbInfo.Name),
783+
zap.Stringer("table", tblInfo.Name),
784+
zap.Uint64("origin tidb crc64", schema.Crc64Xor),
785+
zap.Uint64("calculated crc64", localChecksum.Crc64Xor),
786+
zap.Uint64("origin tidb total kvs", schema.TotalKvs),
787+
zap.Uint64("calculated total kvs", localChecksum.TotalKvs),
788+
zap.Uint64("origin tidb total bytes", schema.TotalBytes),
789+
zap.Uint64("calculated total bytes", localChecksum.TotalBytes),
790+
)
791+
return false, nil
792+
}
793+
log.Info("fast checksum success",
794+
zap.String("database", dbInfo.Name.L),
795+
zap.String("table", tblInfo.Name.L))
796+
}
797+
return true, nil
798+
}
799+
800+
// CollectFileInfo collects ungrouped file summary information, like kv count and size.
801+
func (bc *Client) CollectFileInfo() {
802+
for _, file := range bc.backupMeta.Files {
803+
summary.CollectSuccessUnit(summary.TotalKV, 1, file.TotalKvs)
804+
summary.CollectSuccessUnit(summary.TotalBytes, 1, file.TotalBytes)
805+
}
806+
}
807+
808+
// CollectChecksums check data integrity by xor all(sst_checksum) per table
809+
// it returns the checksum of all local files.
810+
func (bc *Client) CollectChecksums() ([]Checksum, error) {
753811
start := time.Now()
754812
defer func() {
755813
elapsed := time.Since(start)
@@ -758,19 +816,20 @@ func (bc *Client) FastChecksum() (bool, error) {
758816

759817
dbs, err := utils.LoadBackupTables(&bc.backupMeta)
760818
if err != nil {
761-
return false, err
819+
return nil, err
762820
}
763821

822+
checksums := make([]Checksum, 0, len(bc.backupMeta.Schemas))
764823
for _, schema := range bc.backupMeta.Schemas {
765824
dbInfo := &model.DBInfo{}
766825
err = json.Unmarshal(schema.Db, dbInfo)
767826
if err != nil {
768-
return false, err
827+
return nil, err
769828
}
770829
tblInfo := &model.TableInfo{}
771830
err = json.Unmarshal(schema.Table, tblInfo)
772831
if err != nil {
773-
return false, err
832+
return nil, err
774833
}
775834
tbl := dbs[dbInfo.Name.String()].GetTable(tblInfo.Name.String())
776835

@@ -785,25 +844,16 @@ func (bc *Client) FastChecksum() (bool, error) {
785844

786845
summary.CollectSuccessUnit(summary.TotalKV, 1, totalKvs)
787846
summary.CollectSuccessUnit(summary.TotalBytes, 1, totalBytes)
788-
789-
if schema.Crc64Xor == checksum && schema.TotalKvs == totalKvs && schema.TotalBytes == totalBytes {
790-
log.Info("fast checksum success", zap.Stringer("db", dbInfo.Name), zap.Stringer("table", tblInfo.Name))
791-
} else {
792-
log.Error("failed in fast checksum",
793-
zap.String("database", dbInfo.Name.String()),
794-
zap.String("table", tblInfo.Name.String()),
795-
zap.Uint64("origin tidb crc64", schema.Crc64Xor),
796-
zap.Uint64("calculated crc64", checksum),
797-
zap.Uint64("origin tidb total kvs", schema.TotalKvs),
798-
zap.Uint64("calculated total kvs", totalKvs),
799-
zap.Uint64("origin tidb total bytes", schema.TotalBytes),
800-
zap.Uint64("calculated total bytes", totalBytes),
801-
)
802-
return false, nil
847+
log.Info("fast checksum calculated", zap.Stringer("db", dbInfo.Name), zap.Stringer("table", tblInfo.Name))
848+
localChecksum := Checksum{
849+
Crc64Xor: checksum,
850+
TotalKvs: totalKvs,
851+
TotalBytes: totalBytes,
803852
}
853+
checksums = append(checksums, localChecksum)
804854
}
805855

806-
return true, nil
856+
return checksums, nil
807857
}
808858

809859
// CompleteMeta wait response of admin checksum from TiDB to complete backup meta
@@ -815,3 +865,14 @@ func (bc *Client) CompleteMeta(backupSchemas *Schemas) error {
815865
bc.backupMeta.Schemas = schemas
816866
return nil
817867
}
868+
869+
// CopyMetaFrom copies schema metadata directly from pending backupSchemas, without calculating checksum.
870+
// use this when user skip the checksum generating.
871+
func (bc *Client) CopyMetaFrom(backupSchemas *Schemas) {
872+
schemas := make([]*kvproto.Schema, 0, len(backupSchemas.schemas))
873+
for _, v := range backupSchemas.schemas {
874+
schema := v
875+
schemas = append(schemas, &schema)
876+
}
877+
bc.backupMeta.Schemas = schemas
878+
}

pkg/backup/schema.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ type Schemas struct {
3636
backupSchemaCh chan backup.Schema
3737
errCh chan error
3838
wg *sync.WaitGroup
39-
skipChecksum bool
4039
}
4140

4241
func newBackupSchemas() *Schemas {
@@ -57,11 +56,6 @@ func (pending *Schemas) pushPending(
5756
pending.schemas[name] = schema
5857
}
5958

60-
// SetSkipChecksum sets whether it should skip checksum
61-
func (pending *Schemas) SetSkipChecksum(skip bool) {
62-
pending.skipChecksum = skip
63-
}
64-
6559
// Start backups schemas
6660
func (pending *Schemas) Start(
6761
ctx context.Context,
@@ -81,12 +75,6 @@ func (pending *Schemas) Start(
8175
workerPool.Apply(func() {
8276
defer pending.wg.Done()
8377

84-
if pending.skipChecksum {
85-
pending.backupSchemaCh <- schema
86-
updateCh.Inc()
87-
return
88-
}
89-
9078
start := time.Now()
9179
table := model.TableInfo{}
9280
err := json.Unmarshal(schema.Table, &table)

pkg/restore/client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,14 @@ func (rc *Client) ValidateChecksum(
650650
workers.Apply(func() {
651651
defer wg.Done()
652652

653+
if table.NoChecksum() {
654+
log.Info("table doesn't have checksum, skipping checksum",
655+
zap.Stringer("db", table.Db.Name),
656+
zap.Stringer("table", table.Info.Name))
657+
updateCh.Inc()
658+
return
659+
}
660+
653661
startTS, err := rc.GetTS(ctx)
654662
if err != nil {
655663
errCh <- errors.Trace(err)

pkg/restore/db.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,27 @@ func NewDB(g glue.Glue, store kv.Storage) (*DB, error) {
4545
// ExecDDL executes the query of a ddl job.
4646
func (db *DB) ExecDDL(ctx context.Context, ddlJob *model.Job) error {
4747
var err error
48-
if ddlJob.BinlogInfo.TableInfo != nil {
48+
tableInfo := ddlJob.BinlogInfo.TableInfo
49+
dbInfo := ddlJob.BinlogInfo.DBInfo
50+
switch ddlJob.Type {
51+
case model.ActionCreateSchema:
52+
err = db.se.CreateDatabase(ctx, dbInfo)
53+
if err != nil {
54+
log.Error("create database failed", zap.Stringer("db", dbInfo.Name), zap.Error(err))
55+
}
56+
return errors.Trace(err)
57+
case model.ActionCreateTable:
58+
err = db.se.CreateTable(ctx, model.NewCIStr(ddlJob.SchemaName), tableInfo)
59+
if err != nil {
60+
log.Error("create table failed",
61+
zap.Stringer("db", dbInfo.Name),
62+
zap.Stringer("table", tableInfo.Name),
63+
zap.Error(err))
64+
}
65+
return errors.Trace(err)
66+
}
67+
68+
if tableInfo != nil {
4969
switchDbSQL := fmt.Sprintf("use %s;", utils.EncloseName(ddlJob.SchemaName))
5070
err = db.se.Execute(ctx, switchDbSQL)
5171
if err != nil {

pkg/restore/util.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func getSSTMetaFromFile(
115115
Start: rangeStart,
116116
End: rangeEnd,
117117
},
118+
Length: file.GetSize_(),
118119
RegionId: region.GetId(),
119120
RegionEpoch: region.GetRegionEpoch(),
120121
}

0 commit comments

Comments
 (0)