Skip to content

Commit

Permalink
table partition: update job dependence for exchange partition with ta…
Browse files Browse the repository at this point in the history
…ble (#35751)

close #35842
  • Loading branch information
ymkzpx authored Jul 20, 2022
1 parent 460d57c commit c087f6d
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 0 deletions.
65 changes: 65 additions & 0 deletions parser/model/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,63 @@ func (job *Job) hasDependentSchema(other *Job) (bool, error) {
return true, nil
}
}
if job.Type == ActionExchangeTablePartition {
var (
defID int64
ptSchemaID int64
ptID int64
partName string
withValidation bool
)
if err := job.DecodeArgs(&defID, &ptSchemaID, &ptID, &partName, &withValidation); err != nil {
return false, errors.Trace(err)
}
if other.SchemaID == ptSchemaID {
return true, nil
}
}
}
return false, nil
}

func (job *Job) hasDependentTableForExchangePartition(other *Job) (bool, error) {
if job.Type == ActionExchangeTablePartition {
var (
defID int64
ptSchemaID int64
ptID int64
partName string
withValidation bool
)

if err := job.DecodeArgs(&defID, &ptSchemaID, &ptID, &partName, &withValidation); err != nil {
return false, errors.Trace(err)
}
if ptID == other.TableID || defID == other.TableID {
return true, nil
}

if other.Type == ActionExchangeTablePartition {
var (
otherDefID int64
otherPtSchemaID int64
otherPtID int64
otherPartName string
otherWithValidation bool
)
if err := other.DecodeArgs(&otherDefID, &otherPtSchemaID, &otherPtID, &otherPartName, &otherWithValidation); err != nil {
return false, errors.Trace(err)
}
if job.TableID == other.TableID || job.TableID == otherPtID || job.TableID == otherDefID {
return true, nil
}
if ptID == other.TableID || ptID == otherPtID || ptID == otherDefID {
return true, nil
}
if defID == other.TableID || defID == otherPtID || defID == otherDefID {
return true, nil
}
}
}
return false, nil
}
Expand All @@ -605,6 +662,14 @@ func (job *Job) IsDependentOn(other *Job) (bool, error) {
if other.TableID == job.TableID {
return true, nil
}
isDependent, err = job.hasDependentTableForExchangePartition(other)
if err != nil || isDependent {
return isDependent, errors.Trace(err)
}
isDependent, err = other.hasDependentTableForExchangePartition(job)
if err != nil || isDependent {
return isDependent, errors.Trace(err)
}
return false, nil
}

Expand Down
169 changes: 169 additions & 0 deletions parser/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,175 @@ func TestJobCodec(t *testing.T) {
require.NoError(t, err)
require.True(t, isDependent)

// Test IsDependentOn for exchange partition with table.
// test ActionCreateSchema and ActionExchangeTablePartition is dependent.
job3 := &Job{
ID: 4,
TableID: 4,
SchemaID: 4,
Type: ActionExchangeTablePartition,
BinlogInfo: &HistoryInfo{},
Args: []interface{}{int64(6), int64(3), int64(5), "pt", true},
}
job3.RawArgs, err = json.Marshal(job3.Args)
require.NoError(t, err)
isDependent, err = job3.IsDependentOn(job2)
require.NoError(t, err)
require.True(t, isDependent)

// test random and ActionExchangeTablePartition is dependent because TableID is same.
job4 := &Job{
ID: 5,
TableID: 5,
SchemaID: 3,
Type: ActionExchangeTablePartition,
BinlogInfo: &HistoryInfo{},
Args: []interface{}{6, 4, 2, "pt", true},
}
job4.RawArgs, err = json.Marshal(job4.Args)
require.NoError(t, err)
isDependent, err = job4.IsDependentOn(job)
require.NoError(t, err)
require.True(t, isDependent)

// test ActionExchangeTablePartition and ActionExchangeTablePartition is dependent.
job5 := &Job{
ID: 6,
TableID: 6,
SchemaID: 6,
Type: ActionExchangeTablePartition,
BinlogInfo: &HistoryInfo{},
Args: []interface{}{2, 6, 5, "pt", true},
}
job5.RawArgs, err = json.Marshal(job5.Args)
require.NoError(t, err)
isDependent, err = job5.IsDependentOn(job4)
require.NoError(t, err)
require.True(t, isDependent)

job6 := &Job{
ID: 7,
TableID: 7,
SchemaID: 7,
Type: ActionExchangeTablePartition,
BinlogInfo: &HistoryInfo{},
Args: []interface{}{6, 4, 2, "pt", true},
}
job6.RawArgs, err = json.Marshal(job6.Args)
require.NoError(t, err)
isDependent, err = job6.IsDependentOn(job5)
require.NoError(t, err)
require.True(t, isDependent)

job7 := &Job{
ID: 8,
TableID: 8,
SchemaID: 8,
Type: ActionExchangeTablePartition,
BinlogInfo: &HistoryInfo{},
Args: []interface{}{8, 4, 6, "pt", true},
}
job7.RawArgs, err = json.Marshal(job7.Args)
require.NoError(t, err)
isDependent, err = job7.IsDependentOn(job6)
require.NoError(t, err)
require.True(t, isDependent)

job8 := &Job{
ID: 9,
TableID: 9,
SchemaID: 9,
Type: ActionExchangeTablePartition,
BinlogInfo: &HistoryInfo{},
Args: []interface{}{8, 9, 9, "pt", true},
}
job8.RawArgs, err = json.Marshal(job8.Args)
require.NoError(t, err)
isDependent, err = job8.IsDependentOn(job7)
require.NoError(t, err)
require.True(t, isDependent)

job9 := &Job{
ID: 10,
TableID: 10,
SchemaID: 10,
Type: ActionExchangeTablePartition,
BinlogInfo: &HistoryInfo{},
Args: []interface{}{10, 10, 8, "pt", true},
}
job9.RawArgs, err = json.Marshal(job9.Args)
require.NoError(t, err)
isDependent, err = job9.IsDependentOn(job8)
require.NoError(t, err)
require.True(t, isDependent)

// test ActionDropSchema and ActionExchangeTablePartition is dependent.
job10 := &Job{
ID: 11,
TableID: 11,
SchemaID: 11,
Type: ActionDropSchema,
BinlogInfo: &HistoryInfo{},
}
job10.RawArgs, err = json.Marshal(job10.Args)
require.NoError(t, err)

job11 := &Job{
ID: 12,
TableID: 12,
SchemaID: 11,
Type: ActionExchangeTablePartition,
BinlogInfo: &HistoryInfo{},
Args: []interface{}{10, 10, 8, "pt", true},
}
job11.RawArgs, err = json.Marshal(job11.Args)
require.NoError(t, err)
isDependent, err = job11.IsDependentOn(job10)
require.NoError(t, err)
require.True(t, isDependent)

// test ActionDropTable and ActionExchangeTablePartition is dependent.
job12 := &Job{
ID: 13,
TableID: 13,
SchemaID: 11,
Type: ActionDropTable,
BinlogInfo: &HistoryInfo{},
}
job12.RawArgs, err = json.Marshal(job12.Args)
require.NoError(t, err)
isDependent, err = job11.IsDependentOn(job12)
require.NoError(t, err)
require.False(t, isDependent)

job13 := &Job{
ID: 14,
TableID: 12,
SchemaID: 14,
Type: ActionDropTable,
BinlogInfo: &HistoryInfo{},
}
job13.RawArgs, err = json.Marshal(job13.Args)
require.NoError(t, err)
isDependent, err = job11.IsDependentOn(job13)
require.NoError(t, err)
require.True(t, isDependent)

// test ActionDropTable and ActionExchangeTablePartition is dependent.
job14 := &Job{
ID: 15,
TableID: 15,
SchemaID: 15,
Type: ActionExchangeTablePartition,
BinlogInfo: &HistoryInfo{},
Args: []interface{}{16, 17, 12, "pt", true},
}
job14.RawArgs, err = json.Marshal(job14.Args)
require.NoError(t, err)
isDependent, err = job13.IsDependentOn(job14)
require.NoError(t, err)
require.True(t, isDependent)

require.Equal(t, false, job.IsCancelled())
b, err := job.Encode(false)
require.NoError(t, err)
Expand Down

0 comments on commit c087f6d

Please sign in to comment.