Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 21 additions & 2 deletions cdc/entry/schema/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -1089,14 +1089,33 @@ func (s *snapshot) renameTables(job *timodel.Job, currentTs uint64) error {
if len(job.BinlogInfo.MultipleTableInfos) < len(args.RenameTableInfos) {
return cerror.ErrInvalidDDLJob.GenWithStackByArgs(job.ID)
}
// NOTE: should handle failures in halfway better.
for _, info := range args.RenameTableInfos {

// For cyclic renames,
// [0]: a -> tmp, [1]: b -> a, [2]: tmp -> b
// we should drop [0]:a and [1]:b, then create [1]:a and [2]:b.
// The lastIndex records last appearance for each table, so we can
// skip creating [0]:tmp, because that table last appears in [2].
// The droppedTables records dropped table, so we can skip dropping
// [2]:tmp because it was dropped as [0]:a.
// Otherwise, dropTable/createTable will error table not found/already
// exists.
lastIndex := make(map[int64]int, len(args.RenameTableInfos))
droppedTables := make(map[int64]struct{}, len(args.RenameTableInfos))
for i, info := range args.RenameTableInfos {
lastIndex[info.TableID] = i
if _, ok := droppedTables[info.TableID]; ok {
continue
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
droppedTables[info.TableID] = struct{}{}
if err := s.dropTable(info.TableID, currentTs); err != nil {
return errors.Trace(err)
}
}
for i, tableInfo := range job.BinlogInfo.MultipleTableInfos {
info := args.RenameTableInfos[i]
if lastIndex[tableInfo.ID] != i {
continue
}
newSchema, ok := s.schemaByID(info.NewSchemaID)
if !ok {
return cerror.ErrSnapshotSchemaNotFound.GenWithStackByArgs(info.NewSchemaID)
Expand Down
44 changes: 43 additions & 1 deletion cdc/entry/schema_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/log"
tidbkv "github.com/pingcap/tidb/pkg/kv"
timodel "github.com/pingcap/tidb/pkg/meta/model"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pingcap/tiflow/cdc/entry/schema"
"github.com/pingcap/tiflow/cdc/kv"
"github.com/pingcap/tiflow/cdc/model"
Expand Down Expand Up @@ -500,6 +501,14 @@ func (s *schemaStorage) buildRenameEvents(
return nil, errors.Trace(err)
}

// Trace the current names by table ID, so that a later rename entry of
// the same table gets the intermediate name, even when the old table
// name in DDL job args is empty (TiDB <= 8.1).
type tableNameState struct {
schema string
table string
}
states := make(map[int64]tableNameState, len(multiTableInfos))
for i, tableInfo := range multiTableInfos {
info := args.RenameTableInfos[i]
newSchema, ok := preSnap.SchemaByID(info.NewSchemaID)
Expand All @@ -508,16 +517,49 @@ func (s *schemaStorage) buildRenameEvents(
info.NewSchemaID)
}
newSchemaName := newSchema.Name.O
oldSchemaName := info.OldSchemaName.O
event := new(model.DDLEvent)
preTableInfo, ok := preSnap.PhysicalTableByID(tableInfo.ID)
if !ok {
return nil, cerror.ErrSchemaStorageTableMiss.GenWithStackByArgs(
job.TableID)
}
// Initialize old names from the pre-statement snapshot.
state, ok := states[tableInfo.ID]
if !ok {
state = tableNameState{
schema: preTableInfo.TableName.Schema,
table: preTableInfo.Name.O,
}
}
oldSchemaName := state.schema
oldTableName := state.table
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

tableInfo := model.WrapTableInfo(info.NewSchemaID, newSchemaName,
job.BinlogInfo.FinishedTS, tableInfo)

// For `a TO tmp`, use intermediate name "tmp" as new name.
if info.NewTableName.O != "" && tableInfo.Name.O != info.NewTableName.O {
tableInfo = tableInfo.Clone()
// table.TableName.Schema already set correctly in WrapTableInfo
tableInfo.TableName.Table = info.NewTableName.O
tableInfo.TableInfo.Name = ast.NewCIStr(info.NewTableName.O)
}

// For `tmp TO b`, use intermediate name "tmp" as old name.
if oldTableName != "" &&
(preTableInfo.TableName.Schema != oldSchemaName ||
preTableInfo.Name.O != oldTableName) {
preTableInfo = preTableInfo.Clone()
preTableInfo.TableName.Schema = oldSchemaName
preTableInfo.TableName.Table = oldTableName
preTableInfo.TableInfo.Name = ast.NewCIStr(oldTableName)
}
// Record the new names, which become the old names for the next
// entry of the same table.
states[tableInfo.ID] = tableNameState{
schema: newSchemaName,
table: tableInfo.Name.O,
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
event.FromJobWithArgs(job, preTableInfo, tableInfo, oldSchemaName, newSchemaName)
event.Seq = uint64(i)
ddlEvents = append(ddlEvents, event)
Expand Down
185 changes: 174 additions & 11 deletions cdc/entry/schema_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func TestHandleDDL(t *testing.T) {
}
}

func TestHandleRenameTables(t *testing.T) {
func newCrossSchemaTablesSnapshot(t *testing.T) *schema.Snapshot {
// Initial schema: db_1.table_1 and db_2.table_2.
snap := schema.NewEmptySnapshot(true)
var i int64
Expand Down Expand Up @@ -433,6 +433,11 @@ func TestHandleRenameTables(t *testing.T) {
err := snap.HandleDDL(job)
require.Nil(t, err)
}
return snap
}

func TestHandleRenameTables(t *testing.T) {
snap := newCrossSchemaTablesSnapshot(t)

// rename table db1.table_1 to db2.x, db2.table_2 to db1.y
oldSchemaIDs := []int64{1, 2}
Expand All @@ -454,35 +459,193 @@ func TestHandleRenameTables(t *testing.T) {
}
job.BinlogInfo.MultipleTableInfos = append(job.BinlogInfo.MultipleTableInfos,
&timodel.TableInfo{
ID: 13,
ID: 11,
Name: pmodel.NewCIStr("x"),
State: timodel.StatePublic,
})
job.BinlogInfo.MultipleTableInfos = append(job.BinlogInfo.MultipleTableInfos,
&timodel.TableInfo{
ID: 14,
ID: 12,
Name: pmodel.NewCIStr("y"),
State: timodel.StatePublic,
})
testDoDDLAndCheck(t, snap, job, false)

var ok bool
_, ok = snap.PhysicalTableByID(13)
require.True(t, ok)
_, ok = snap.PhysicalTableByID(14)
require.True(t, ok)
// Real TiDB keeps the physical table ID across renames, so the
// renamed tables are still found by their original IDs.
_, ok = snap.PhysicalTableByID(11)
require.False(t, ok)
require.True(t, ok)
_, ok = snap.PhysicalTableByID(12)
require.False(t, ok)
require.True(t, ok)

n1, _ := snap.TableIDByName("db_2", "x")
require.Equal(t, n1, int64(13))
require.Equal(t, n1, int64(11))
n2, _ := snap.TableIDByName("db_1", "y")
require.Equal(t, n2, int64(14))
require.Equal(t, n2, int64(12))
require.Equal(t, uint64(11112222), snap.CurrentTs())
}

func TestHandleCrossSchemaCyclicRenameTables(t *testing.T) {
snap := newCrossSchemaTablesSnapshot(t)

// Cross schema cyclic renames:
// db_1.x -> db_1.tmp,
// db_2.y -> db_1.x,
// db_1.tmp -> db_2.y.
oldSchemaIDs := []int64{1, 2, 1}
newSchemaIDs := []int64{1, 1, 2}
oldTableIDs := []int64{11, 12, 11}
newTableNames := []pmodel.CIStr{
pmodel.NewCIStr("tmp"),
pmodel.NewCIStr("x"),
pmodel.NewCIStr("y"),
}
oldTableNames := []pmodel.CIStr{
pmodel.NewCIStr("x"),
pmodel.NewCIStr("y"),
pmodel.NewCIStr("tmp"),
}
oldSchemaNames := []pmodel.CIStr{
pmodel.NewCIStr("db_1"),
pmodel.NewCIStr("db_2"),
pmodel.NewCIStr("db_1"),
}
args := []interface{}{
oldSchemaIDs, newSchemaIDs, newTableNames, oldTableIDs, oldSchemaNames, oldTableNames,
}
rawArgs, err := json.Marshal(args)
require.Nil(t, err)
job := &timodel.Job{
Version: timodel.JobVersion1,
Type: timodel.ActionRenameTables,
RawArgs: rawArgs,
BinlogInfo: &timodel.HistoryInfo{
FinishedTS: 11112223,
},
}
job.BinlogInfo.MultipleTableInfos = append(job.BinlogInfo.MultipleTableInfos,
&timodel.TableInfo{
ID: 11,
Name: pmodel.NewCIStr("tmp"),
State: timodel.StatePublic,
})
job.BinlogInfo.MultipleTableInfos = append(job.BinlogInfo.MultipleTableInfos,
&timodel.TableInfo{
ID: 12,
Name: pmodel.NewCIStr("x"),
State: timodel.StatePublic,
})
job.BinlogInfo.MultipleTableInfos = append(job.BinlogInfo.MultipleTableInfos,
&timodel.TableInfo{
ID: 11,
Name: pmodel.NewCIStr("y"),
State: timodel.StatePublic,
})
testDoDDLAndCheck(t, snap, job, false)

n1, ok := snap.TableIDByName("db_1", "x")
require.True(t, ok)
require.Equal(t, n1, int64(12))
n2, ok := snap.TableIDByName("db_2", "y")
require.True(t, ok)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
require.Equal(t, n2, int64(11))
_, ok = snap.TableByName("db_1", "y")
require.False(t, ok)
_, ok = snap.TableByName("db_1", "tmp")
require.False(t, ok)
_, ok = snap.TableByName("db_2", "x")
require.False(t, ok)
require.Equal(t, uint64(11112223), snap.CurrentTs())
}

func TestPartitionTableCyclicRename(t *testing.T) {
helper := NewSchemaTestHelper(t)
defer helper.Close()

schemaStorage := helper.SchemaStorage()

createPartitionTable := func(name string) (*timodel.TableInfo, []int64) {
createJob := helper.DDL2Job(fmt.Sprintf(`create table test.%s (
id int primary key,
val int
) partition by range(id) (
partition p0 values less than (10),
partition p1 values less than (100)
)`, name))
err := schemaStorage.HandleDDLJob(createJob)
require.NoError(t, err)
info := createJob.BinlogInfo.TableInfo
pi := info.GetPartitionInfo()
require.NotNil(t, pi)
partitionIDs := make([]int64, 0, 2)
for _, def := range pi.Definitions {
partitionIDs = append(partitionIDs, def.ID)
}
return info, partitionIDs
}

pt1, prePT1PartitionIDs := createPartitionTable("pt1")
pt2, prePT2PartitionIDs := createPartitionTable("pt2")
prePT1ID, prePT2ID := pt1.ID, pt2.ID

job := helper.DDL2Job("rename table test.pt1 to test.tmp, test.pt2 to test.pt1, test.tmp to test.pt2")
oldSchemaIDs := []int64{job.SchemaID, job.SchemaID, job.SchemaID}
newSchemaIDs := []int64{job.SchemaID, job.SchemaID, job.SchemaID}
newTableNames := []pmodel.CIStr{
pmodel.NewCIStr("tmp"), pmodel.NewCIStr("pt1"), pmodel.NewCIStr("pt2"),
}
tableIDs := []int64{prePT1ID, prePT2ID, prePT1ID}
oldSchemaNames := []pmodel.CIStr{
pmodel.NewCIStr("test"), pmodel.NewCIStr("test"), pmodel.NewCIStr("test"),
}
rawArgs, err := json.Marshal([]interface{}{
oldSchemaIDs, newSchemaIDs, newTableNames, tableIDs, oldSchemaNames,
})
require.NoError(t, err)
job.Version = timodel.JobVersion1
job.RawArgs = rawArgs

ctx := context.Background()

schemaStorage.AdvanceResolvedTs(job.BinlogInfo.FinishedTS - 1)
events, err := schemaStorage.BuildDDLEvents(ctx, job)
require.NoError(t, err)
err = schemaStorage.HandleDDLJob(job)
require.NoError(t, err)
schemaStorage.AdvanceResolvedTs(job.BinlogInfo.FinishedTS)
require.Len(t, events, 3)

require.Equal(t, "RENAME TABLE `test`.`pt1` TO `test`.`tmp`", events[0].Query)
require.Equal(t, "RENAME TABLE `test`.`pt2` TO `test`.`pt1`", events[1].Query)
require.Equal(t, "RENAME TABLE `test`.`tmp` TO `test`.`pt2`", events[2].Query)

snap := schemaStorage.GetLastSnapshot()
finalPT1, ok := snap.TableByName("test", "pt1")
require.True(t, ok)
finalPT2, ok := snap.TableByName("test", "pt2")
require.True(t, ok)
require.Equal(t, prePT2ID, finalPT1.ID)
require.Equal(t, prePT1ID, finalPT2.ID)
_, ok = snap.TableByName("test", "tmp")
require.False(t, ok)

for _, partitionID := range prePT1PartitionIDs {
info, ok := snap.PhysicalTableByID(partitionID)
require.True(t, ok)
require.Equal(t, "test", info.GetSchemaName())
require.Equal(t, "pt2", info.GetTableName())
require.Equal(t, prePT1ID, info.ID)
}
for _, partitionID := range prePT2PartitionIDs {
info, ok := snap.PhysicalTableByID(partitionID)
require.True(t, ok)
require.Equal(t, "test", info.GetSchemaName())
require.Equal(t, "pt1", info.GetTableName())
require.Equal(t, prePT2ID, info.ID)
}
}

func testDoDDLAndCheck(t *testing.T, snap *schema.Snapshot, job *timodel.Job, isErr bool) {
err := snap.HandleDDL(job)
require.Equal(t, err != nil, isErr)
Expand Down
Loading