Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

br: support batch create table for restore #29380

Merged
merged 239 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
239 commits
Select commit Hold shift + click to select a range
cc5c8d5
fix issue 27015
fengou1 Sep 27, 2021
65ccd96
Merge branch 'master' into issue_27015
fengou1 Sep 27, 2021
bd531c8
fix comments - add general function isRetryableError
fengou1 Sep 28, 2021
a3b0df0
Merge branch 'issue_27015' of https://github.com/fengou1/tidb into is…
fengou1 Sep 28, 2021
c1ddff9
Merge branch 'master' into issue_27015
fengou1 Sep 28, 2021
0fbd620
remote uncessary package errors
fengou1 Sep 28, 2021
0f1976c
Merge branch 'issue_27015' of https://github.com/fengou1/tidb into is…
fengou1 Sep 28, 2021
6dcdbdb
reused the retry code from lightning
fengou1 Sep 29, 2021
fe147b9
Merge branch 'master' into issue_27015
fengou1 Sep 29, 2021
8dcda02
Merge branch 'master' into issue_27015
fengou1 Oct 7, 2021
e97b450
refactoring retryable
fengou1 Oct 7, 2021
a825bea
Merge branch 'master' into issue_27015
fengou1 Oct 9, 2021
440cdf5
Merge branch 'master' into issue_27015
fengou1 Oct 12, 2021
69a3c9e
ddl: add batch create table api
xhebox Oct 12, 2021
3b5e644
ddl: add unit tests
xhebox Oct 12, 2021
5a4c8ac
ddl: fix fmt
xhebox Oct 12, 2021
4da4374
Merge branch 'master' into batch_1
xhebox Oct 12, 2021
ef68f70
ddl: typo
xhebox Oct 18, 2021
8ea8a36
ddl: fix tests
xhebox Oct 19, 2021
e98c8bd
Merge branch 'master' into batch_1
fengou1 Oct 19, 2021
ab46d6d
Merge branch 'master' into batch_1
fengou1 Oct 20, 2021
ab1af6f
ddl: rename to BatchCreateTableWithInfo
xhebox Oct 20, 2021
879101a
ddl: trace the error
xhebox Oct 20, 2021
d42c5eb
ddl: comments
xhebox Oct 20, 2021
7fb3819
ddl: cancle the job right
xhebox Oct 20, 2021
be50b96
rebase
fengou1 Oct 21, 2021
610beaa
Merge branch 'master' of https://github.com/pingcap/tidb
fengou1 Oct 23, 2021
89999bc
ddl: cancel the job right 2
xhebox Oct 25, 2021
3a927ab
br ddl code
fengou1 Oct 27, 2021
28cbb5f
Merge branch 'master' of https://github.com/fengou1/tidb into br_ddl
fengou1 Nov 3, 2021
91ceec5
parallel create tables in br
fengou1 Dec 15, 2021
14aca77
ddl: add batch create table api
xhebox Oct 12, 2021
73a7b8a
ddl: add unit tests
xhebox Oct 12, 2021
dee6c35
ddl: fix fmt
xhebox Oct 12, 2021
5b46f1b
ddl: typo
xhebox Oct 18, 2021
88a0e3f
ddl: fix tests
xhebox Oct 19, 2021
5c50048
ddl: rename to BatchCreateTableWithInfo
xhebox Oct 20, 2021
d35fc31
ddl: trace the error
xhebox Oct 20, 2021
f43fa0f
ddl: comments
xhebox Oct 20, 2021
7348348
ddl: cancle the job right
xhebox Oct 20, 2021
ad5812e
ddl: cancel the job right 2
xhebox Oct 25, 2021
1b70b8b
ddl: report error if entry too large
xhebox Oct 26, 2021
05aa57c
ddl: report error when table is duplicated
xhebox Oct 27, 2021
e4582a7
ddl: go fmt
xhebox Oct 27, 2021
7913c49
infoschema: improve batch memory perf
xhebox Oct 27, 2021
84bdeaf
ddl: retain ID
xhebox Oct 29, 2021
bd0b779
ddl: reduce log frequency
xhebox Dec 15, 2021
c5e7cb8
ddl: fix tests
xhebox Dec 15, 2021
fd44939
br: pick the latest to feature branch (#30782)
fengou1 Dec 20, 2021
497bc0f
fix conflict
fengou1 Dec 21, 2021
ced6a21
fix merge issue
fengou1 Dec 21, 2021
501fc52
fix issue that loss table restore
fengou1 Dec 22, 2021
17bd4dd
refactoring code
fengou1 Dec 22, 2021
4115494
refactoring code
fengou1 Dec 22, 2021
6d971dc
Merge branch 'br_ddl' into merge_ddl
fengou1 Dec 26, 2021
6fa8234
br: batch ddl for binlog (#31028)
fengou1 Dec 26, 2021
cb4da54
remote uncessary package errors
fengou1 Sep 28, 2021
f677a58
reused the retry code from lightning
fengou1 Sep 29, 2021
afb61f3
refactoring retryable
fengou1 Oct 7, 2021
4ac9368
ddl: add batch create table api
xhebox Oct 12, 2021
79424ba
ddl: add unit tests
xhebox Oct 12, 2021
1dab65a
ddl: fix fmt
xhebox Oct 12, 2021
608b3bf
br ddl code
fengou1 Oct 27, 2021
a460ca2
parallel create tables in br
fengou1 Dec 15, 2021
44b5e6f
ddl: add batch create table api
xhebox Oct 12, 2021
ecc3abc
ddl: add unit tests
xhebox Oct 12, 2021
8bb0b00
ddl: fix fmt
xhebox Oct 12, 2021
4dba2ba
ddl: typo
xhebox Oct 18, 2021
5c77213
ddl: fix tests
xhebox Oct 19, 2021
ba18d94
ddl: rename to BatchCreateTableWithInfo
xhebox Oct 20, 2021
f5cc598
ddl: trace the error
xhebox Oct 20, 2021
64229c6
ddl: comments
xhebox Oct 20, 2021
57b528d
ddl: cancle the job right
xhebox Oct 20, 2021
b493b9d
ddl: cancel the job right 2
xhebox Oct 25, 2021
e11349d
ddl: report error if entry too large
xhebox Oct 26, 2021
9e1adb8
ddl: report error when table is duplicated
xhebox Oct 27, 2021
3044bec
ddl: go fmt
xhebox Oct 27, 2021
b044171
infoschema: improve batch memory perf
xhebox Oct 27, 2021
745ad90
ddl: retain ID
xhebox Oct 29, 2021
8716356
ddl: reduce log frequency
xhebox Dec 15, 2021
2e6504f
ddl: fix tests
xhebox Dec 15, 2021
6d2320d
ddl: remove retainID from the interface
xhebox Dec 22, 2021
5a0bd46
ddl: fix tests
xhebox Dec 22, 2021
779071b
executor: fix rebasing problem
xhebox Dec 27, 2021
b2b0a12
merge from tidb batch ddl
fengou1 Dec 27, 2021
94b56a1
Merge branch 'br_ddl' into br_ddl
fengou1 Dec 27, 2021
609aa00
br: merge into feature branch br_ddl for binlog test (#31044)
fengou1 Dec 27, 2021
b71db7e
Merge branch 'master' into br_ddl
fengou1 Dec 27, 2021
4687a70
Merge branch 'master' into batch_1
fengou1 Dec 27, 2021
e55fefb
merge from tidb batch_1
fengou1 Dec 27, 2021
806da18
Merge branch 'br_ddl' into br_ddl
fengou1 Dec 27, 2021
1d5cde2
fix panic
fengou1 Dec 27, 2021
1b49cc0
ddl: test commit
xhebox Dec 28, 2021
45ea4b8
Merge branch 'batch_1' of https://github.com/xhebox/tidb into br_ddl
fengou1 Dec 28, 2021
163c6a5
fix multi-db with multi-tables
fengou1 Dec 28, 2021
e3cf009
Merge branch 'master' into br_ddl
fengou1 Dec 28, 2021
dad1ac0
Merge branch 'master' into br_ddl
fengou1 Dec 29, 2021
0b0cf00
Merge branch 'batch_1' of https://github.com/xhebox/tidb into br_ddl
fengou1 Dec 29, 2021
df3bdb8
Add unit test
fengou1 Dec 29, 2021
de33dfd
Merge branch 'master' into br_ddl
fengou1 Dec 29, 2021
74735e3
refactoring code and fix comments from PR review
fengou1 Dec 29, 2021
e1ebfbb
1, remove some log, 2, reuse utils.MinInt, 3, some format and descrip…
fengou1 Dec 30, 2021
da65fb0
1, correct comments. 2, move a for loop into if condition
fengou1 Dec 30, 2021
fba39c3
defer close channel is unnecessary
fengou1 Dec 30, 2021
a627086
fix merge conflict
fengou1 Dec 30, 2021
7c4c445
Merge branch 'master' into br_ddl
fengou1 Dec 30, 2021
54f7da9
refactoring execution sequence sql into a function
fengou1 Dec 31, 2021
f0f8b68
Merge branch 'br_ddl' of https://github.com/fengou1/tidb into br_ddl
fengou1 Dec 31, 2021
89d4406
refactoring the log message and fix magic number
fengou1 Dec 31, 2021
28949a0
Merge branch 'master' into br_ddl
fengou1 Dec 31, 2021
8544446
Merge branch 'master' into br_ddl
fengou1 Dec 31, 2021
ef0b957
Merge branch 'master' into br_ddl
fengou1 Dec 31, 2021
e9493f2
Merge branch 'master' into br_ddl
fengou1 Dec 31, 2021
4ec05fb
Merge branch 'master' into br_ddl
fengou1 Dec 31, 2021
77b36c1
rebase from upstream
fengou1 Dec 31, 2021
4c1a9c0
Merge branch 'master' into br_ddl
fengou1 Dec 31, 2021
3779d62
Merge branch 'master' into br_ddl
fengou1 Dec 31, 2021
9dd77bb
Merge branch 'master' into br_ddl
ti-chi-bot Jan 1, 2022
58b4e06
Merge branch 'master' into br_ddl
ti-chi-bot Jan 1, 2022
5dad568
Merge branch 'master' into br_ddl
ti-chi-bot Jan 1, 2022
17fd4c5
Merge branch 'master' into br_ddl
ti-chi-bot Jan 1, 2022
6c4e30c
Merge branch 'master' into br_ddl
ti-chi-bot Jan 3, 2022
e8b3805
Merge branch 'master' into br_ddl
ti-chi-bot Jan 3, 2022
e5645e7
Merge branch 'master' into br_ddl
ti-chi-bot Jan 4, 2022
677d48c
update UT remote create database test since this db existed by default.
fengou1 Jan 4, 2022
19a33a6
Merge branch 'br_ddl' of https://github.com/fengou1/tidb into br_ddl
fengou1 Jan 4, 2022
6e954ab
Unit Test update
fengou1 Jan 4, 2022
2cc353f
Merge branch 'master' into br_ddl
fengou1 Jan 4, 2022
2d6205c
Merge branch 'master' into br_ddl
fengou1 Jan 4, 2022
57b6f1a
merge from master to br_ddl
fengou1 Jan 4, 2022
709bd25
Merge branch 'master' into br_ddl
YuJuncen Jan 4, 2022
804f05d
Merge branch 'master' into br_ddl
ti-chi-bot Jan 4, 2022
de66de3
Merge branch 'master' into br_ddl
ti-chi-bot Jan 4, 2022
46994e9
Merge branch 'master' into br_ddl
ti-chi-bot Jan 4, 2022
38f228e
Merge branch 'master' into br_ddl
ti-chi-bot Jan 4, 2022
20be0e5
Merge branch 'master' into br_ddl
ti-chi-bot Jan 4, 2022
e50507f
Merge branch 'master' into br_ddl
ti-chi-bot Jan 5, 2022
064a189
Merge branch 'master' into br_ddl
ti-chi-bot Jan 5, 2022
5d464be
Merge branch 'master' into br_ddl
ti-chi-bot Jan 5, 2022
88b3566
Merge branch 'master' into br_ddl
ti-chi-bot Jan 5, 2022
731a018
Merge branch 'master' into br_ddl
ti-chi-bot Jan 5, 2022
ea945b8
Merge branch 'master' into br_ddl
ti-chi-bot Jan 5, 2022
25a5ee6
Merge branch 'master' into br_ddl
ti-chi-bot Jan 5, 2022
c36e6dd
Merge branch 'master' into br_ddl
ti-chi-bot Jan 5, 2022
ece7461
Merge branch 'master' into br_ddl
ti-chi-bot Jan 5, 2022
36fa887
Merge branch 'master' into br_ddl
ti-chi-bot Jan 5, 2022
a30ebde
Merge branch 'master' into br_ddl
ti-chi-bot Jan 5, 2022
80b4f56
Merge branch 'master' into br_ddl
ti-chi-bot Jan 5, 2022
d001506
Merge branch 'master' into br_ddl
ti-chi-bot Jan 5, 2022
6585eb3
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
828abda
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
6487f02
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
6841a23
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
f369c49
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
5cd1494
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
48e2791
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
3f72f15
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
155c9bf
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
e48352a
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
f5f279d
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
73afe81
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
71e7047
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
ddf7670
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
e2c070e
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
adb90eb
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
b5847ec
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
219d8c7
Merge branch 'master' into br_ddl
ti-chi-bot Jan 6, 2022
05d4b29
Merge branch 'master' into br_ddl
ti-chi-bot Jan 7, 2022
11e8eec
Merge branch 'master' into br_ddl
ti-chi-bot Jan 7, 2022
bb71693
Merge branch 'master' into br_ddl
ti-chi-bot Jan 7, 2022
d0335bf
Merge branch 'master' into br_ddl
ti-chi-bot Jan 7, 2022
eb3c673
Merge branch 'master' into br_ddl
ti-chi-bot Jan 7, 2022
3417b56
Merge branch 'master' into br_ddl
ti-chi-bot Jan 7, 2022
81493ac
Merge branch 'master' into br_ddl
ti-chi-bot Jan 7, 2022
834f135
Merge branch 'master' into br_ddl
ti-chi-bot Jan 7, 2022
01f55a3
Merge branch 'master' into br_ddl
ti-chi-bot Jan 8, 2022
89a2d21
Merge branch 'master' into br_ddl
ti-chi-bot Jan 8, 2022
c0f6991
Merge branch 'master' into br_ddl
ti-chi-bot Jan 8, 2022
cff388c
Merge branch 'master' into br_ddl
ti-chi-bot Jan 8, 2022
3b4bf21
Merge branch 'master' into br_ddl
ti-chi-bot Jan 9, 2022
a86fa1b
Merge branch 'master' into br_ddl
ti-chi-bot Jan 10, 2022
b73a386
Merge branch 'master' into br_ddl
ti-chi-bot Jan 10, 2022
fb22aba
Merge branch 'master' into br_ddl
ti-chi-bot Jan 10, 2022
fb0069e
Merge branch 'master' into br_ddl
ti-chi-bot Jan 10, 2022
92b8af8
Merge branch 'master' into br_ddl
ti-chi-bot Jan 10, 2022
1ef2ff9
Merge branch 'master' into br_ddl
ti-chi-bot Jan 10, 2022
1f7ccd5
Merge branch 'master' into br_ddl
ti-chi-bot Jan 10, 2022
4415678
Merge branch 'master' into br_ddl
ti-chi-bot Jan 10, 2022
54afa43
Merge branch 'master' into br_ddl
ti-chi-bot Jan 10, 2022
8f591e4
Merge branch 'master' into br_ddl
ti-chi-bot Jan 10, 2022
0be8d66
Merge branch 'master' into br_ddl
ti-chi-bot Jan 10, 2022
8c40558
Merge branch 'master' into br_ddl
ti-chi-bot Jan 10, 2022
d1023f9
Merge branch 'master' into br_ddl
ti-chi-bot Jan 10, 2022
8af2451
Merge branch 'master' into br_ddl
ti-chi-bot Jan 10, 2022
00972cf
Merge branch 'master' into br_ddl
ti-chi-bot Jan 10, 2022
193c8d8
Merge branch 'master' into br_ddl
ti-chi-bot Jan 11, 2022
ba57d15
Merge branch 'master' into br_ddl
ti-chi-bot Jan 11, 2022
5d59f49
Merge branch 'master' into br_ddl
ti-chi-bot Jan 11, 2022
8ae46e1
Merge branch 'master' into br_ddl
ti-chi-bot Jan 11, 2022
9e66ea3
Merge branch 'master' into br_ddl
ti-chi-bot Jan 11, 2022
d5b6da5
Merge branch 'master' into br_ddl
ti-chi-bot Jan 11, 2022
23f6295
Merge branch 'master' into br_ddl
ti-chi-bot Jan 11, 2022
600313c
Merge branch 'master' into br_ddl
ti-chi-bot Jan 11, 2022
c0ca2c1
Merge branch 'master' into br_ddl
ti-chi-bot Jan 11, 2022
5c73e5c
Merge branch 'master' into br_ddl
ti-chi-bot Jan 11, 2022
8783d94
Merge branch 'master' into br_ddl
ti-chi-bot Jan 11, 2022
f181627
Merge branch 'master' into br_ddl
ti-chi-bot Jan 12, 2022
d2b7979
Merge branch 'master' into br_ddl
ti-chi-bot Jan 12, 2022
bf3cfe0
Merge branch 'master' into br_ddl
ti-chi-bot Jan 12, 2022
d41b0ef
Merge branch 'master' into br_ddl
ti-chi-bot Jan 12, 2022
2d8b7a9
Merge branch 'master' into br_ddl
ti-chi-bot Jan 12, 2022
8cf0295
Merge branch 'master' into br_ddl
ti-chi-bot Jan 12, 2022
f14e18c
Merge branch 'master' into br_ddl
ti-chi-bot Jan 12, 2022
3087d0f
Merge branch 'master' into br_ddl
ti-chi-bot Jan 12, 2022
974d262
Merge branch 'master' into br_ddl
ti-chi-bot Jan 12, 2022
df980de
Merge branch 'master' into br_ddl
ti-chi-bot Jan 12, 2022
a08b008
Merge branch 'master' into br_ddl
ti-chi-bot Jan 12, 2022
91fb4bd
Merge branch 'master' into br_ddl
ti-chi-bot Jan 12, 2022
8e87dbb
Merge branch 'master' into br_ddl
ti-chi-bot Jan 13, 2022
1500965
Merge branch 'master' into br_ddl
ti-chi-bot Jan 13, 2022
1c4d693
Merge branch 'master' into br_ddl
ti-chi-bot Jan 13, 2022
3f6905f
Merge branch 'master' into br_ddl
ti-chi-bot Jan 13, 2022
e72ea37
Merge branch 'master' into br_ddl
ti-chi-bot Jan 13, 2022
10eda5d
Merge branch 'master' into br_ddl
ti-chi-bot Jan 13, 2022
15a5ebc
Merge branch 'master' into br_ddl
ti-chi-bot Jan 13, 2022
54eb32e
Merge branch 'master' into br_ddl
ti-chi-bot Jan 13, 2022
8d7ac8e
Merge branch 'master' into br_ddl
ti-chi-bot Jan 13, 2022
596338b
Merge branch 'master' into br_ddl
ti-chi-bot Jan 14, 2022
2935e77
Merge branch 'master' into br_ddl
ti-chi-bot Jan 14, 2022
0b714e5
Merge branch 'master' into br_ddl
ti-chi-bot Jan 14, 2022
8e4df25
Merge branch 'master' into br_ddl
ti-chi-bot Jan 14, 2022
75c79d0
Merge branch 'master' into br_ddl
ti-chi-bot Jan 14, 2022
e8e7e30
Merge branch 'master' into br_ddl
ti-chi-bot Jan 14, 2022
6f11510
Merge branch 'master' into br_ddl
ti-chi-bot Jan 14, 2022
502c76d
Merge branch 'master' into br_ddl
ti-chi-bot Jan 14, 2022
4ac08f1
Merge branch 'master' into br_ddl
ti-chi-bot Jan 14, 2022
ec578f0
ensure dbpool is not 0, otherwise we use legacy way, anyway it can be…
fengou1 Jan 15, 2022
4df5c5d
Merge branch 'master' into br_ddl
ti-chi-bot Jan 17, 2022
95b3436
Merge branch 'master' into br_ddl
ti-chi-bot Jan 17, 2022
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
5 changes: 5 additions & 0 deletions br/pkg/glue/glue.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ type Session interface {
Close()
}

// BatchCreateTableSession is an interface to batch create table parallelly
type BatchCreateTableSession interface {
CreateTables(ctx context.Context, tables map[string][]*model.TableInfo) error
}

// Progress is an interface recording the current execution progress.
type Progress interface {
// Inc increases the progress. This method must be goroutine-safe, and can
Expand Down
44 changes: 44 additions & 0 deletions br/pkg/gluetidb/glue.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package gluetidb
import (
"bytes"
"context"
"strings"

"github.com/pingcap/errors"
"github.com/pingcap/log"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
pd "github.com/tikv/pd/client"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -126,6 +128,48 @@ func (gs *tidbSession) CreateDatabase(ctx context.Context, schema *model.DBInfo)
schema.Charset = mysql.DefaultCharset
}
return d.CreateSchemaWithInfo(gs.se, schema, ddl.OnExistIgnore)

}

// CreateTables implements glue.BatchCreateTableSession.
func (gs *tidbSession) CreateTables(ctx context.Context, tables map[string][]*model.TableInfo) error {
d := domain.GetDomain(gs.se).DDL()
var dbName model.CIStr

for db, tablesInDB := range tables {
dbName = model.NewCIStr(db)
queryBuilder := strings.Builder{}
cloneTables := make([]*model.TableInfo, 0, len(tablesInDB))
for _, table := range tablesInDB {
query, err := gs.showCreateTable(table)
if err != nil {
return errors.Trace(err)
}

queryBuilder.WriteString(query)
queryBuilder.WriteString(";")

table = table.Clone()
// Clone() does not clone partitions yet :(
if table.Partition != nil {
newPartition := *table.Partition
newPartition.Definitions = append([]model.PartitionDefinition{}, table.Partition.Definitions...)
table.Partition = &newPartition
}
cloneTables = append(cloneTables, table)
}
gs.se.SetValue(sessionctx.QueryString, queryBuilder.String())
err := d.BatchCreateTableWithInfo(gs.se, dbName, cloneTables, ddl.OnExistIgnore)
if err != nil {
//It is possible to failure when TiDB does not support model.ActionCreateTables.
//In this circumstance, BatchCreateTableWithInfo returns errno.ErrInvalidDDLJob,
//we fall back to old way that creating table one by one
log.Warn("batch create table from tidb failure", zap.Error(err))
return err
}
}

return nil
}

// CreateTable implements glue.Session.
Expand Down
111 changes: 111 additions & 0 deletions br/pkg/restore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ import (
"github.com/pingcap/tidb/br/pkg/summary"
"github.com/pingcap/tidb/br/pkg/utils"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/statistics/handle"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/util/codec"
Expand All @@ -52,6 +54,7 @@ import (
// defaultChecksumConcurrency is the default number of the concurrent
// checksum tasks.
const defaultChecksumConcurrency = 64
const minBatchDdlSize = 1

// Client sends requests to restore files.
type Client struct {
Expand Down Expand Up @@ -93,6 +96,8 @@ type Client struct {
// and restore stats with #dump.LoadStatsFromJSON
statsHandler *handle.Handle
dom *domain.Domain

batchDdlSize uint
}

// NewRestoreClient returns a new RestoreClient.
Expand Down Expand Up @@ -165,6 +170,14 @@ func (rc *Client) SetSwitchModeInterval(interval time.Duration) {
rc.switchModeInterval = interval
}

func (rc *Client) SetBatchDdlSize(batchDdlsize uint) {
rc.batchDdlSize = batchDdlsize
}

func (rc *Client) GetBatchDdlSize() uint {
return rc.batchDdlSize
}

// Close a client.
func (rc *Client) Close() {
// rc.db can be nil in raw kv mode.
Expand Down Expand Up @@ -400,6 +413,46 @@ func (rc *Client) CreateTables(
}
return rewriteRules, newTables, nil
}
func (rc *Client) createTables(
ctx context.Context,
db *DB,
dom *domain.Domain,
tables []*metautil.Table,
newTS uint64,
) ([]CreatedTable, error) {
log.Info("client to create tables")
if rc.IsSkipCreateSQL() {
log.Info("skip create table and alter autoIncID")
} else {
err := db.CreateTables(ctx, tables)
if err != nil {
return nil, errors.Trace(err)
}
}
cts := make([]CreatedTable, 0, len(tables))
for _, table := range tables {
newTableInfo, err := rc.GetTableSchema(dom, table.DB.Name, table.Info.Name)
if err != nil {
return nil, errors.Trace(err)
}
if newTableInfo.IsCommonHandle != table.Info.IsCommonHandle {
return nil, errors.Annotatef(berrors.ErrRestoreModeMismatch,
"Clustered index option mismatch. Restored cluster's @@tidb_enable_clustered_index should be %v (backup table = %v, created table = %v).",
transferBoolToValue(table.Info.IsCommonHandle),
table.Info.IsCommonHandle,
newTableInfo.IsCommonHandle)
}
rules := GetRewriteRules(newTableInfo, table.Info, newTS)
ct := CreatedTable{
RewriteRule: rules,
Table: newTableInfo,
OldTable: table,
}
log.Debug("new created tables", zap.Any("table", ct))
cts = append(cts, ct)
}
return cts, nil
}

func (rc *Client) createTable(
ctx context.Context,
Expand Down Expand Up @@ -459,11 +512,33 @@ func (rc *Client) GoCreateTables(
}
outCh := make(chan CreatedTable, len(tables))
rater := logutil.TraceRateOver(logutil.MetricTableCreatedCounter)

var err error

if rc.batchDdlSize > minBatchDdlSize && len(dbPool) > 0 {

err = rc.createTablesInWorkerPool(ctx, dom, tables, dbPool, newTS, outCh)

if err == nil {
defer log.Debug("all tables are created")
close(outCh)
return outCh
// fall back to old create table (sequential create table)
} else if errors.Cause(err).(*terror.Error).Code() == errno.ErrInvalidDDLJob {
log.Info("fall back to the sequential create table")
} else {
errCh <- err
close(outCh)
return outCh
kennytm marked this conversation as resolved.
Show resolved Hide resolved
}
}

createOneTable := func(c context.Context, db *DB, t *metautil.Table) error {
select {
case <-c.Done():
return c.Err()
default:

}
rt, err := rc.createTable(c, db, dom, t, newTS, ddlTables)
if err != nil {
Expand Down Expand Up @@ -497,6 +572,7 @@ func (rc *Client) GoCreateTables(
errCh <- err
}
}()

return outCh
}

Expand Down Expand Up @@ -526,6 +602,41 @@ func (rc *Client) createTablesWithDBPool(ctx context.Context,
return eg.Wait()
}

func (rc *Client) createTablesInWorkerPool(ctx context.Context, dom *domain.Domain, tables []*metautil.Table, dbPool []*DB, newTS uint64, outCh chan<- CreatedTable) error {
eg, ectx := errgroup.WithContext(ctx)
rater := logutil.TraceRateOver(logutil.MetricTableCreatedCounter)
workers := utils.NewWorkerPool(uint(len(dbPool)), "Create Tables Worker")
numOfTables := len(tables)

for lastSent := 0; lastSent < numOfTables; lastSent += int(rc.batchDdlSize) {
end := utils.MinInt(lastSent+int(rc.batchDdlSize), len(tables))
log.Info("create tables", zap.Int("table start", lastSent), zap.Int("table end", end))

tableSlice := tables[lastSent:end]
workers.ApplyWithIDInErrorGroup(eg, func(id uint64) error {
db := dbPool[id%uint64(len(dbPool))]
cts, err := rc.createTables(ectx, db, dom, tableSlice, newTS) // ddl job for [lastSent:i)
if err != nil {
log.Error("create tables fail")
return err
}
for _, ct := range cts {
log.Debug("table created and send to next",
zap.Int("output chan size", len(outCh)),
zap.Stringer("table", ct.OldTable.Info.Name),
zap.Stringer("database", ct.OldTable.DB.Name))
outCh <- ct
rater.Inc()
rater.L().Info("table created",
zap.Stringer("table", ct.OldTable.Info.Name),
zap.Stringer("database", ct.OldTable.DB.Name))
}
return err
})
}
return eg.Wait()
}

// ExecDDLs executes the queries of the ddl jobs.
func (rc *Client) ExecDDLs(ctx context.Context, ddlJobs []*model.Job) error {
// Sort the ddl jobs by schema version in ascending order.
Expand Down
1 change: 1 addition & 0 deletions br/pkg/restore/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestCreateTables(t *testing.T) {
dbSchema, isExist := info.SchemaByName(model.NewCIStr("test"))
require.True(t, isExist)

client.SetBatchDdlSize(1)
tables := make([]*metautil.Table, 4)
intField := types.NewFieldType(mysql.TypeLong)
intField.Charset = "binary"
Expand Down
74 changes: 53 additions & 21 deletions br/pkg/restore/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,11 @@ func (db *DB) CreateDatabase(ctx context.Context, schema *model.DBInfo) error {
return errors.Trace(err)
}

// CreateTable executes a CREATE TABLE SQL.
func (db *DB) CreateTable(ctx context.Context, table *metautil.Table, ddlTables map[UniqueTableName]bool) error {
err := db.se.CreateTable(ctx, table.DB.Name, table.Info)
if err != nil {
log.Error("create table failed",
zap.Stringer("db", table.DB.Name),
zap.Stringer("table", table.Info.Name),
zap.Error(err))
return errors.Trace(err)
}

//
func (db *DB) restoreSequence(ctx context.Context, table *metautil.Table) error {
var restoreMetaSQL string
switch {
case table.Info.IsView():
return nil
case table.Info.IsSequence():
var err error
if table.Info.IsSequence() {
setValFormat := fmt.Sprintf("do setval(%s.%s, %%d);",
utils.EncloseName(table.DB.Name.O),
utils.EncloseName(table.Info.Name.O))
Expand Down Expand Up @@ -168,7 +157,6 @@ func (db *DB) CreateTable(ctx context.Context, table *metautil.Table, ddlTables
zap.Error(err))
return errors.Trace(err)
}

// trigger cycle round > 0
err = db.se.Execute(ctx, nextSeqSQL)
if err != nil {
Expand All @@ -182,12 +170,56 @@ func (db *DB) CreateTable(ctx context.Context, table *metautil.Table, ddlTables
}
restoreMetaSQL = fmt.Sprintf(setValFormat, table.Info.AutoIncID)
err = db.se.Execute(ctx, restoreMetaSQL)
}
if err != nil {
log.Error("restore meta sql failed",
zap.String("query", restoreMetaSQL),
zap.Stringer("db", table.DB.Name),
zap.Stringer("table", table.Info.Name),
zap.Error(err))
return errors.Trace(err)
}
return errors.Trace(err)
}

// CreateTables execute a internal CREATE TABLES.
func (db *DB) CreateTables(ctx context.Context, tables []*metautil.Table) error {
if batchSession, ok := db.se.(glue.BatchCreateTableSession); ok {
m := map[string][]*model.TableInfo{}
for _, table := range tables {
m[table.DB.Name.L] = append(m[table.DB.Name.L], table.Info)
}
if err := batchSession.CreateTables(ctx, m); err != nil {
return err
}

for _, table := range tables {
err := db.restoreSequence(ctx, table)
return err
}
}

return nil
}

// CreateTable executes a CREATE TABLE SQL.
func (db *DB) CreateTable(ctx context.Context, table *metautil.Table, ddlTables map[UniqueTableName]bool) error {
err := db.se.CreateTable(ctx, table.DB.Name, table.Info)
if err != nil {
log.Error("create table failed",
zap.Stringer("db", table.DB.Name),
zap.Stringer("table", table.Info.Name),
zap.Error(err))
return errors.Trace(err)
}

var restoreMetaSQL string
switch {
case table.Info.IsView():
return nil
case table.Info.IsSequence():
err = db.restoreSequence(ctx, table)
if err != nil {
log.Error("restore meta sql failed",
zap.String("query", restoreMetaSQL),
zap.Stringer("db", table.DB.Name),
zap.Stringer("table", table.Info.Name),
zap.Error(err))
return errors.Trace(err)
}
// only table exists in ddlJobs during incremental restoration should do alter after creation.
Expand Down
Loading