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

ddl: fix alter table exchange partition does not work if table has tiflash replica #17940

Merged
merged 97 commits into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
70f6354
exchange partition init push
zhaox1n May 4, 2020
57af241
exchange partition: check table is compatiable
zhaox1n May 4, 2020
af97469
Merge pull request #1 from pingcap/master
zhaox1n May 4, 2020
adef48e
add checke in ddl worker
zhaox1n May 5, 2020
1a7f599
validate check
zhaox1n May 6, 2020
11809d0
Basic functions have been completed
zhaox1n May 6, 2020
c8bc050
autoID added
zhaox1n May 7, 2020
5a32fe4
test add
zhaox1n May 8, 2020
35941aa
hash exchange partition add
zhaox1n May 9, 2020
7468924
format
zhaox1n May 9, 2020
64fd709
test add
zhaox1n May 9, 2020
cb88c18
Merge pull request #2 from pingcap/master
zhaox1n May 9, 2020
71cf840
test add
zhaox1n May 10, 2020
a902cdc
auto id fix
zhaox1n May 10, 2020
a228468
add cancel job test
zhaox1n May 10, 2020
9ff0790
add exchange partition compatiable test
zhaox1n May 11, 2020
dda9d75
add test of partition
zhaox1n May 12, 2020
32c6285
add more test and set go mod
zhaox1n May 13, 2020
899033f
deal with conflict
zhaox1n May 13, 2020
f4f5b38
fix the conflict
zhaox1n May 13, 2020
a38b5e0
deal with conflict
zhaox1n May 14, 2020
3cec6f2
add more test of table compatible
zhaox1n May 14, 2020
53f4f73
fix comment
zhaox1n May 14, 2020
101c673
rename function
zhaox1n May 14, 2020
a1ca360
fix wrong english spell
zhaox1n May 14, 2020
40db78a
Merge remote-tracking branch 'upstream/master'
zhaox1n May 16, 2020
c559f12
add test for expression index
zhaox1n May 16, 2020
f9fdd0c
add check in ddl worker
zhaox1n May 17, 2020
c979e69
rename word
zhaox1n May 18, 2020
c096c8c
add global config of expression index
zhaox1n May 18, 2020
4130a9a
deal conflict
zhaox1n May 18, 2020
0ffd1ab
deal conflict
zhaox1n May 21, 2020
488cb5f
add auto_random test
zhaox1n May 21, 2020
b5cfdd3
comment fix
zhaox1n May 26, 2020
fac04c1
comment fix
zhaox1n May 26, 2020
be92bb3
more test added
zhaox1n May 26, 2020
3f38e2b
Merge remote-tracking branch 'upstream/master'
zhaox1n May 26, 2020
d9bc6fa
add failed point
zhaox1n May 26, 2020
ea173bf
remove variable
zhaox1n May 26, 2020
57aeb95
Update ddl/column.go
zhaox1n May 27, 2020
5a29bdd
Update ddl/ddl_api.go
zhaox1n May 27, 2020
aa8da8e
comment fix
zhaox1n May 27, 2020
ebce341
add test
zhaox1n May 27, 2020
65f4c64
add test
zhaox1n May 27, 2020
5a46b18
exchange auto_random
zhaox1n May 27, 2020
ec51f5e
fix range columns partition
zhaox1n May 28, 2020
a93fe24
Merge remote-tracking branch 'upstream/master'
zhaox1n May 28, 2020
95b2554
fix comment
zhaox1n May 29, 2020
134c55d
fix comment
zhaox1n May 29, 2020
d724823
fix comment
zhaox1n May 29, 2020
894b7e5
fix bug
zhaox1n May 30, 2020
40729a1
fix comment
zhaox1n Jun 1, 2020
6ea633d
fix comment
zhaox1n Jun 1, 2020
d9b98c0
fix comment
zhaox1n Jun 1, 2020
b194f11
Merge remote-tracking branch 'upstream/master' into exchange_partitio…
zhaox1n Jun 1, 2020
7807380
fix comment
zhaox1n Jun 1, 2020
4769d3b
bug fix
zhaox1n Jun 2, 2020
325d596
address comment
zhaox1n Jun 2, 2020
33e0c36
Merge pull request #3 from zhaox1n/exchange_partition_test
zhaox1n Jun 2, 2020
6eeb3fd
Merge branch 'master' into master
crazycs520 Jun 2, 2020
b46a6bb
Merge branch 'master' into master
tiancaiamao Jun 3, 2020
85d71c0
address comment
zhaox1n Jun 4, 2020
a4d0161
Merge branch 'master' of https://github.com/zhaox1n/tidb
zhaox1n Jun 4, 2020
6c1d63a
address comment
zhaox1n Jun 6, 2020
b5b6824
bug fix
zhaox1n Jun 6, 2020
999b0d9
bug fix
zhaox1n Jun 6, 2020
2cf5d03
sql fix
zhaox1n Jun 6, 2020
52676f8
add test of auto_random
zhaox1n Jun 6, 2020
6bc1a4a
expand the implementation of ApplyDiff
zhaox1n Jun 7, 2020
9cd7caa
fix typo
zhaox1n Jun 7, 2020
771e4dd
Update ddl/partition.go
zhaox1n Jun 8, 2020
f2ab275
address conflict
zhaox1n Jun 8, 2020
dc8abd3
Merge branch 'master' into master
crazycs520 Jun 9, 2020
44f6018
address comment
zhaox1n Jun 9, 2020
3ec7b7a
merge master
zhaox1n Jun 9, 2020
f3fc2e7
address comment
zhaox1n Jun 10, 2020
273e911
fix bug of exchanging partition on tiflash replica
zhaox1n Jun 10, 2020
ffe48c0
merge master
zhaox1n Jun 11, 2020
bc77a30
merge master
zhaox1n Jun 11, 2020
db68d2a
delete duplicate code
zhaox1n Jun 11, 2020
437aab9
Merge branch 'master' into master
zhaox1n Jun 15, 2020
f7bd233
merge origin master
zhaox1n Jun 20, 2020
62b7e27
fix conclict
zhaox1n Jun 20, 2020
0acdba0
reset tiflash statu
zhaox1n Jun 20, 2020
134109d
Update ddl/ddl_api.go
zhaox1n Jun 22, 2020
aa789e8
Update ddl/ddl_api.go
zhaox1n Jun 22, 2020
72438b2
address comment
zhaox1n Jun 22, 2020
6a31a34
Merge branch 'master' into master
zhaox1n Jun 23, 2020
46115cb
exchange partition init push
zhaox1n May 4, 2020
ee85e37
fix bug of exchanging partition on tiflash replica
zhaox1n Jun 10, 2020
0858658
reset tiflash statu
zhaox1n Jun 20, 2020
2b74ae4
rebase
zhaox1n Jun 23, 2020
30b726a
reset mod
zhaox1n Jun 23, 2020
245cd92
address comment
zhaox1n Jun 26, 2020
ce74f1b
Merge branch 'master' into master
zhaox1n Jun 27, 2020
4dafc9b
address comment
zhaox1n Jun 28, 2020
f838184
Merge branch 'master' into master
zhaox1n Jun 28, 2020
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
4 changes: 2 additions & 2 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2917,7 +2917,7 @@ func (d *ddl) ExchangeTablePartition(ctx sessionctx.Context, ident ast.Ident, sp

// NOTE: if pt is subPartitioned, it should be checked

_, err = tables.FindPartitionByName(ptMeta, partName)
defID, err := tables.FindPartitionByName(ptMeta, partName)
if err != nil {
return errors.Trace(err)
}
Expand All @@ -2933,7 +2933,7 @@ func (d *ddl) ExchangeTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
SchemaName: ntSchema.Name.L,
Type: model.ActionExchangeTablePartition,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{ptSchema.ID, ptMeta.ID, partName, spec.WithValidation},
Args: []interface{}{defID, ptSchema.ID, ptMeta.ID, partName, spec.WithValidation},
}

err = d.doDDLJob(ctx, job)
Expand Down
6 changes: 6 additions & 0 deletions ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,12 @@ func updateSchemaVersion(t *meta.Meta, job *model.Job) (int64, error) {
return 0, errors.Trace(err)
}
diff.TableID = job.TableID
case model.ActionExchangeTablePartition:
err = job.DecodeArgs(&diff.TableID, &diff.PtSchemaID, &diff.PtTableID)
if err != nil {
return 0, errors.Trace(err)
}
diff.OldTableID = job.TableID
default:
diff.TableID = job.TableID
}
Expand Down
3 changes: 2 additions & 1 deletion ddl/ddl_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,8 @@ func (s *testDDLSuite) TestCancelJob(c *C) {
testCreateTable(c, ctx, d, dbInfo, nt)

updateTest(&tests[43])
exchangeTablePartition := []interface{}{dbInfo.ID, pt.ID, "p0", true}
defID := pt.Partition.Definitions[0].ID
exchangeTablePartition := []interface{}{defID, dbInfo.ID, pt.ID, "p0", true}
doDDLJobErrWithSchemaState(ctx, d, c, dbInfo.ID, nt.ID, test.act, exchangeTablePartition, &test.cancelState)
c.Check(checkErr, IsNil)
changedNtTable := testGetTable(c, d, dbInfo.ID, nt.ID)
Expand Down
24 changes: 12 additions & 12 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,18 +712,20 @@ func onTruncateTablePartition(d *ddlCtx, t *meta.Meta, job *model.Job) (int64, e
// onExchangeTablePartition exchange partition data
func (w *worker) onExchangeTablePartition(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error) {
var (
//defID only for updateSchemaVersion
defID int64
ptSchemaID int64
ptID int64
partName string
withValidation bool
)

if err := job.DecodeArgs(&ptSchemaID, &ptID, &partName, &withValidation); err != nil {
if err := job.DecodeArgs(&defID, &ptSchemaID, &ptID, &partName, &withValidation); err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}

ntDbInfo, err := t.GetDatabase(job.SchemaID)
ntDbInfo, err := checkSchemaExistAndCancelNotExistJob(t, job)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
Expand Down Expand Up @@ -754,8 +756,13 @@ func (w *worker) onExchangeTablePartition(d *ddlCtx, t *meta.Meta, job *model.Jo
return ver, errors.Trace(err)
}

index, _, err := getPartitionDef(pt, partName)
if err != nil {
return ver, errors.Trace(err)
}

if withValidation {
err = checkExchangePartitionRecordValidation(w, pt, partName, ntDbInfo.Name, nt.Name)
err = checkExchangePartitionRecordValidation(w, pt, index, ntDbInfo.Name, nt.Name)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
Expand Down Expand Up @@ -866,14 +873,10 @@ func (w *worker) onExchangeTablePartition(d *ddlCtx, t *meta.Meta, job *model.Jo
return ver, nil
}

func checkExchangePartitionRecordValidation(w *worker, pt *model.TableInfo, partName string, schemaName, tableName model.CIStr) error {
func checkExchangePartitionRecordValidation(w *worker, pt *model.TableInfo, index int, schemaName, tableName model.CIStr) error {
var sql string

pi := pt.Partition
index, _, err := getPartitionDef(pt, partName)
if err != nil {
return errors.Trace(err)
}

switch pi.Type {
case model.PartitionTypeHash:
Expand All @@ -882,9 +885,6 @@ func checkExchangePartitionRecordValidation(w *worker, pt *model.TableInfo, part
}
sql = fmt.Sprintf("select 1 from `%s`.`%s` where mod(%s, %d) != %d limit 1", schemaName.L, tableName.L, pi.Expr, pi.Num, index)
case model.PartitionTypeRange:
if err != nil {
return errors.Trace(err)
}
// Table has only one partition and has the maximum value
if len(pi.Definitions) == 1 && strings.EqualFold(pi.Definitions[index].LessThan[0], partitionMaxValue) {
return nil
Expand All @@ -900,7 +900,7 @@ func checkExchangePartitionRecordValidation(w *worker, pt *model.TableInfo, part
}

var ctx sessionctx.Context
ctx, err = w.sessPool.get()
ctx, err := w.sessPool.get()
if err != nil {
return errors.Trace(err)
}
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989
github.com/pingcap/kvproto v0.0.0-20200518112156-d4aeb467de29
github.com/pingcap/log v0.0.0-20200511115504-543df19646ad
github.com/pingcap/parser v0.0.0-20200522094936-3b720a0512a6
github.com/pingcap/parser v0.0.0-20200601053017-87d29a820b2d
github.com/pingcap/pd/v4 v4.0.0-rc.2.0.20200520083007-2c251bd8f181
github.com/pingcap/sysutil v0.0.0-20200408114249-ed3bd6f7fdb1
github.com/pingcap/tidb-tools v4.0.0-rc.1.0.20200514040632-f76b3e428e19+incompatible
Expand Down Expand Up @@ -63,3 +63,5 @@ require (
)

go 1.13

replace github.com/pingcap/parser => github.com/zhaox1n/parser v0.0.0-20200601191909-048d6ec9eef1
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,10 @@ github.com/yookoala/realpath v1.0.0/go.mod h1:gJJMA9wuX7AcqLy1+ffPatSCySA1FQ2S8Y
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/zhangjinpeng1987/raft v0.0.0-20190624145930-deeb32d6553d h1:rGkexfPDxNuTCObUwTbsRUlti+evR/Ksb4dKy6esXW0=
github.com/zhangjinpeng1987/raft v0.0.0-20190624145930-deeb32d6553d/go.mod h1:1KDQ09J8MRHEtHze4at7BJZDW/doUAgkJ8w9KjEUhSo=
github.com/zhaox1n/parser v0.0.0-20200601184100-acd1a1774f3b h1:ztkWO3u6NFlSkV3vr3vOdeOlh8owOpHx9t5qZvFO5IM=
github.com/zhaox1n/parser v0.0.0-20200601184100-acd1a1774f3b/go.mod h1:vQdbJqobJAgFyiRNNtXahpMoGWwPEuWciVEK5A20NS0=
github.com/zhaox1n/parser v0.0.0-20200601191909-048d6ec9eef1 h1:f4Qcit4fJibpvNmFSYMDS/O6KHOxQTnwFnfD1gvZ/O4=
github.com/zhaox1n/parser v0.0.0-20200601191909-048d6ec9eef1/go.mod h1:vQdbJqobJAgFyiRNNtXahpMoGWwPEuWciVEK5A20NS0=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.3 h1:MUGmc65QhB3pIlaQ5bB4LwqSj6GIonVJXpZiaKNyaKk=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
Expand Down
28 changes: 26 additions & 2 deletions infoschema/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, erro
newTableID = diff.TableID
case model.ActionDropTable, model.ActionDropView, model.ActionDropSequence:
oldTableID = diff.TableID
case model.ActionTruncateTable, model.ActionCreateView:
case model.ActionTruncateTable, model.ActionCreateView, model.ActionExchangeTablePartition:
oldTableID = diff.OldTableID
newTableID = diff.TableID
default:
Expand All @@ -74,7 +74,8 @@ func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, erro
// We try to reuse the old allocator, so the cached auto ID can be reused.
var allocs autoid.Allocators
if tableIDIsValid(oldTableID) {
if oldTableID == newTableID && diff.Type != model.ActionRenameTable {
if oldTableID == newTableID && diff.Type != model.ActionRenameTable &&
diff.Type != model.ActionExchangeTablePartition {
oldAllocs, _ := b.is.AllocByID(oldTableID)
allocs = filterAllocators(diff, oldAllocs)
}
Expand Down Expand Up @@ -106,6 +107,13 @@ func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, erro
return nil, errors.Trace(err)
}
}
if diff.Type == model.ActionExchangeTablePartition {
var err error
tblIDs, err = b.applyExchangePartition(m, diff, tblIDs)
if err != nil {
return nil, errors.Trace(err)
}
}
return tblIDs, nil
}

Expand Down Expand Up @@ -155,6 +163,22 @@ func (b *Builder) copySortedTables(oldTableID, newTableID int64) {
}
}

func (b *Builder) applyExchangePartition(m *meta.Meta, diff *model.SchemaDiff, affected []int64) ([]int64, error) {
ptDi, err := m.GetDatabase(diff.PtSchemaID)
if err != nil {
return affected, errors.Trace(err)
}
if tableIDIsValid(diff.PtTableID) {
affected = b.applyDropTable(ptDi, diff.PtTableID, affected)
var allocs autoid.Allocators
affected, err = b.applyCreateTable(m, ptDi, diff.PtTableID, allocs, diff.Type, affected)
if err != nil {
return affected, errors.Trace(err)
}
}
return affected, nil
}

func (b *Builder) applyCreateSchema(m *meta.Meta, diff *model.SchemaDiff) error {
di, err := m.GetDatabase(diff.SchemaID)
if err != nil {
Expand Down