Skip to content

Commit

Permalink
ddl: fix alter table exchange partition does not work if table has ti…
Browse files Browse the repository at this point in the history
…flash replica (#17940)

* exchange partition init push

* exchange partition: check table is compatiable

* add checke in ddl worker

* validate check

* Basic functions have been completed

* autoID added

* test add

* hash exchange partition add

* format

* test add

* test add

* auto id fix

* add cancel job test

* add exchange partition compatiable test

* add test of partition

* add more test and set go mod

* fix the conflict

* add more test of table compatible

* fix comment

* rename function

* fix wrong english spell

* add test for expression index

* add check in ddl worker

* rename word

* add global config of expression index

* add auto_random test

* comment fix

* more test added

* add failed point

* remove variable

* Update ddl/column.go

Co-authored-by: Lynn <[email protected]>

* Update ddl/ddl_api.go

Co-authored-by: Lynn <[email protected]>

* comment fix

* add test

* add test

* exchange auto_random

* fix range columns partition

* fix comment

* fix comment

* fix comment

* fix bug

* fix comment

* fix comment

* fix comment

* fix comment

* bug fix

* address comment

* address comment

* address comment

* bug fix

* bug fix

* sql fix

* add test of auto_random

* expand the implementation of ApplyDiff

* fix typo

* Update ddl/partition.go

Co-authored-by: Lynn <[email protected]>

* address comment

* fix bug of exchanging partition on tiflash replica

* delete duplicate code

* reset tiflash statu

* Update ddl/ddl_api.go

Co-authored-by: Lynn <[email protected]>

* Update ddl/ddl_api.go

Co-authored-by: Lynn <[email protected]>

* address comment

* exchange partition init push

* fix bug of exchanging partition on tiflash replica

* reset tiflash statu

* reset mod

* address comment

* address comment

Co-authored-by: Lynn <[email protected]>
Co-authored-by: crazycs <[email protected]>
Co-authored-by: tiancaiamao <[email protected]>
  • Loading branch information
4 people authored Jun 28, 2020
1 parent 43d7b0e commit 59cb7e1
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
71 changes: 70 additions & 1 deletion ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,76 @@ func (s *testIntegrationSuite4) TestAlterTableExchangePartition(c *C) {
tk.MustExec("alter table e12 add index (a);")
tk.MustGetErrCode("alter table e12 exchange partition p0 with table e14", tmysql.ErrPartitionExchangeDifferentOption)

// test for tiflash replica
tk.MustExec("create table e15 (a int) partition by hash(a) partitions 1;")
tk.MustExec("create table e16 (a int)")
tk.MustExec("alter table e15 set tiflash replica 1;")
tk.MustExec("alter table e16 set tiflash replica 2;")

e15 := testGetTableByName(c, s.ctx, "test", "e15")
partition := e15.Meta().Partition

err := domain.GetDomain(tk.Se).DDL().UpdateTableReplicaInfo(tk.Se, partition.Definitions[0].ID, true)
c.Assert(err, IsNil)

e16 := testGetTableByName(c, s.ctx, "test", "e16")
err = domain.GetDomain(tk.Se).DDL().UpdateTableReplicaInfo(tk.Se, e16.Meta().ID, true)
c.Assert(err, IsNil)

tk.MustGetErrCode("alter table e15 exchange partition p0 with table e16", tmysql.ErrTablesDifferentMetadata)
tk.MustExec("drop table e15, e16")

tk.MustExec("create table e15 (a int) partition by hash(a) partitions 1;")
tk.MustExec("create table e16 (a int)")
tk.MustExec("alter table e15 set tiflash replica 1;")
tk.MustExec("alter table e16 set tiflash replica 1;")

e15 = testGetTableByName(c, s.ctx, "test", "e15")
partition = e15.Meta().Partition

err = domain.GetDomain(tk.Se).DDL().UpdateTableReplicaInfo(tk.Se, partition.Definitions[0].ID, true)
c.Assert(err, IsNil)

e16 = testGetTableByName(c, s.ctx, "test", "e16")
err = domain.GetDomain(tk.Se).DDL().UpdateTableReplicaInfo(tk.Se, e16.Meta().ID, true)
c.Assert(err, IsNil)

tk.MustExec("alter table e15 exchange partition p0 with table e16")

e15 = testGetTableByName(c, s.ctx, "test", "e15")

partition = e15.Meta().Partition

c.Assert(e15.Meta().TiFlashReplica, NotNil)
c.Assert(e15.Meta().TiFlashReplica.Available, IsTrue)
c.Assert(e15.Meta().TiFlashReplica.AvailablePartitionIDs, DeepEquals, []int64{partition.Definitions[0].ID})

e16 = testGetTableByName(c, s.ctx, "test", "e16")
c.Assert(e16.Meta().TiFlashReplica, NotNil)
c.Assert(e16.Meta().TiFlashReplica.Available, IsTrue)

tk.MustExec("drop table e15, e16")
tk.MustExec("create table e15 (a int) partition by hash(a) partitions 1;")
tk.MustExec("create table e16 (a int)")
tk.MustExec("alter table e16 set tiflash replica 1;")

tk.MustExec("alter table e15 set tiflash replica 1 location labels 'a', 'b';")

tk.MustGetErrCode("alter table e15 exchange partition p0 with table e16", tmysql.ErrTablesDifferentMetadata)

tk.MustExec("alter table e16 set tiflash replica 1 location labels 'a', 'b';")

e15 = testGetTableByName(c, s.ctx, "test", "e15")
partition = e15.Meta().Partition

err = domain.GetDomain(tk.Se).DDL().UpdateTableReplicaInfo(tk.Se, partition.Definitions[0].ID, true)
c.Assert(err, IsNil)

e16 = testGetTableByName(c, s.ctx, "test", "e16")
err = domain.GetDomain(tk.Se).DDL().UpdateTableReplicaInfo(tk.Se, e16.Meta().ID, true)
c.Assert(err, IsNil)

tk.MustExec("alter table e15 exchange partition p0 with table e16")
}

func (s *testIntegrationSuite4) TestExchangePartitionTableCompatiable(c *C) {
Expand Down Expand Up @@ -1134,7 +1204,6 @@ func (s *testIntegrationSuite4) TestExchangePartitionTableCompatiable(c *C) {
tk.MustExec(t.exchangeSQL)
}
}

}

func (s *testIntegrationSuite7) TestExchangePartitionExpressIndex(c *C) {
Expand Down
21 changes: 20 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2852,14 +2852,33 @@ func checkFieldTypeCompatible(ft *types.FieldType, other *types.FieldType) bool
return true
}

func checkTiFlashReplicaCompatible(source *model.TiFlashReplicaInfo, target *model.TiFlashReplicaInfo) bool {
if source == target {
return true
}
if source == nil || target == nil {
return false
}
if source.Count != target.Count ||
source.Available != target.Available || len(source.LocationLabels) != len(target.LocationLabels) {
return false
}
for i, lable := range source.LocationLabels {
if target.LocationLabels[i] != lable {
return false
}
}
return true
}

func checkTableDefCompatible(source *model.TableInfo, target *model.TableInfo) error {
// check auto_random
if source.AutoRandomBits != target.AutoRandomBits ||
source.Charset != target.Charset ||
source.Collate != target.Collate ||
source.ShardRowIDBits != target.ShardRowIDBits ||
source.MaxShardRowIDBits != target.MaxShardRowIDBits ||
source.TiFlashReplica != target.TiFlashReplica {
!checkTiFlashReplicaCompatible(source.TiFlashReplica, target.TiFlashReplica) {
return errors.Trace(ErrTablesDifferentMetadata)
}
if len(source.Cols()) != len(target.Cols()) {
Expand Down
9 changes: 9 additions & 0 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,15 @@ func (w *worker) onExchangeTablePartition(d *ddlCtx, t *meta.Meta, job *model.Jo
// exchange table meta id
partDef.ID = nt.ID

if pt.TiFlashReplica != nil {
for i, id := range pt.TiFlashReplica.AvailablePartitionIDs {
if id == tempID {
pt.TiFlashReplica.AvailablePartitionIDs[i] = partDef.ID
break
}
}
}

err = t.UpdateTable(ptSchemaID, pt)
if err != nil {
job.State = model.JobStateCancelled
Expand Down

0 comments on commit 59cb7e1

Please sign in to comment.