ddl: fix alter table exchange partition does not work if table has tiflash 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 <zimu_xia@126.com> * Update ddl/ddl_api.go Co-authored-by: Lynn <zimu_xia@126.com> * 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 <zimu_xia@126.com> * 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 <zimu_xia@126.com> * Update ddl/ddl_api.go Co-authored-by: Lynn <zimu_xia@126.com> * 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 <zimu_xia@126.com> Co-authored-by: crazycs <crazycs520@gmail.com> Co-authored-by: tiancaiamao <tiancaiamao@gmail.com>
This commit is contained in:
@ -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) {
|
||||
@ -1134,7 +1204,6 @@ func (s *testIntegrationSuite4) TestExchangePartitionTableCompatiable(c *C) {
|
||||
tk.MustExec(t.exchangeSQL)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *testIntegrationSuite7) TestExchangePartitionExpressIndex(c *C) {
|
||||
|
||||
@ -2852,6 +2852,25 @@ 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 ||
|
||||
@ -2859,7 +2878,7 @@ func checkTableDefCompatible(source *model.TableInfo, target *model.TableInfo) e
|
||||
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()) {
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user