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: Exchange part schema load fix (#46126) #46191

Merged
Merged
Show file tree
Hide file tree
Changes from 13 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
82 changes: 82 additions & 0 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2513,6 +2513,88 @@ func TestExchangePartitionTableCompatiable(t *testing.T) {
require.NoError(t, err)
}

func TestExchangePartitionMultiTable(t *testing.T) {
store := testkit.CreateMockStore(t)
tk1 := testkit.NewTestKit(t, store)

dbName := "ExchangeMultiTable"
tk1.MustExec(`create schema ` + dbName)
tk1.MustExec(`use ` + dbName)
tk1.MustExec(`CREATE TABLE t1 (a int)`)
tk1.MustExec(`CREATE TABLE t2 (a int)`)
tk1.MustExec(`CREATE TABLE tp (a int) partition by hash(a) partitions 3`)
tk1.MustExec(`set @@global.tidb_enable_metadata_lock = ON`)
tk1.MustExec(`insert into t1 values (0)`)
tk1.MustExec(`insert into t2 values (3)`)
tk1.MustExec(`insert into tp values (6)`)

tk2 := testkit.NewTestKit(t, store)
tk2.MustExec(`use ` + dbName)
tk3 := testkit.NewTestKit(t, store)
tk3.MustExec(`use ` + dbName)
tk4 := testkit.NewTestKit(t, store)
tk4.MustExec(`use ` + dbName)
waitFor := func(col int, tableName, s string) {
for {
tk4 := testkit.NewTestKit(t, store)
tk4.MustExec(`use test`)
sql := `admin show ddl jobs where db_name = '` + strings.ToLower(dbName) + `' and table_name = '` + tableName + `' and job_type = 'exchange partition'`
res := tk4.MustQuery(sql).Rows()
if len(res) == 1 && res[0][col] == s {
break
}
logutil.BgLogger().Info("No match", zap.String("sql", sql), zap.String("s", s))
sql = `admin show ddl jobs`
res = tk4.MustQuery(sql).Rows()
for _, row := range res {
strs := make([]string, 0, len(row))
for _, c := range row {
strs = append(strs, c.(string))
}
logutil.BgLogger().Info("admin show ddl jobs", zap.Strings("row", strs))
}
time.Sleep(100 * time.Millisecond)
}
}
var wg sync.WaitGroup
wg.Add(1)
dumpChan := make(chan struct{})
defer func() {
close(dumpChan)
wg.Wait()
}()
go testkit.DebugDumpOnTimeout(&wg, dumpChan, 20*time.Second)
alterChan1 := make(chan error)
alterChan2 := make(chan error)
tk3.MustExec(`BEGIN`)
tk3.MustExec(`insert into tp values (1)`)
tk3.MustExec(`insert into t1 values (2)`)
tk3.MustExec(`insert into t2 values (4)`)
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/exchangePartitionAutoID", `pause`))
go func() {
alterChan1 <- tk1.ExecToErr(`alter table tp exchange partition p0 with table t1`)
}()
waitFor(11, "t1", "running")
go func() {
alterChan2 <- tk2.ExecToErr(`alter table tp exchange partition p0 with table t2`)
}()
waitFor(11, "t2", "queueing")
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/exchangePartitionAutoID"))
tk3.MustExec(`rollback`)
logutil.BgLogger().Info("rollback done")
require.NoError(t, <-alterChan1)
logutil.BgLogger().Info("alter1 done")
err := <-alterChan2
logutil.BgLogger().Info("alter2 done")
tk3.MustQuery(`select * from t1`).Check(testkit.Rows("6"))
logutil.BgLogger().Info("select t1 done")
tk3.MustQuery(`select * from t2`).Check(testkit.Rows("0"))
logutil.BgLogger().Info("select t2 done")
tk3.MustQuery(`select * from tp`).Check(testkit.Rows("3"))
logutil.BgLogger().Info("select tp done")
require.NoError(t, err)
}

func TestExchangePartitionValidation(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down
45 changes: 29 additions & 16 deletions ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1556,27 +1556,40 @@ func updateSchemaVersion(d *ddlCtx, t *meta.Meta, job *model.Job, multiInfos ...
diff.OldSchemaID = oldSchemaIDs[0]
diff.AffectedOpts = affects
case model.ActionExchangeTablePartition:
// From start of function: diff.SchemaID = job.SchemaID
// Old is original non partitioned table
diff.OldTableID = job.TableID
diff.OldSchemaID = job.SchemaID
// Update the partitioned table (it is only done in the last state)
var (
ptSchemaID int64
ptTableID int64
ptDefID int64
partName string // Not used
withValidation bool // Not used
)
// See ddl.ExchangeTablePartition
err = job.DecodeArgs(&ptDefID, &ptSchemaID, &ptTableID, &partName, &withValidation)
if err != nil {
return 0, errors.Trace(err)
}
// This is needed for not crashing TiFlash!
// TODO: Update TiFlash, to handle StateWriteOnly
diff.AffectedOpts = []*model.AffectedOption{{
TableID: ptTableID,
}}
if job.SchemaState != model.StatePublic {
// No change, just to refresh the non-partitioned table
// with its new ExchangePartitionInfo.
diff.TableID = job.TableID
diff.SchemaID = job.SchemaID
// Keep this as Schema ID of non-partitioned table
// to avoid trigger early rename in TiFlash
diff.AffectedOpts[0].SchemaID = job.SchemaID
} else {
// Update the partitioned table (it is only done in the last state)
var (
ptSchemaID int64
ptTableID int64
ptDefID int64 // Not needed, will reload the whole table
partName string // Not used
withValidation bool // Not used
)
// See ddl.ExchangeTablePartition
err = job.DecodeArgs(&ptDefID, &ptSchemaID, &ptTableID, &partName, &withValidation)
if err != nil {
return 0, errors.Trace(err)
}
diff.SchemaID = ptSchemaID
diff.TableID = ptTableID
// Swap
diff.TableID = ptDefID
// Also add correct SchemaID in case different schemas
diff.AffectedOpts[0].SchemaID = ptSchemaID
}
case model.ActionTruncateTablePartition:
diff.TableID = job.TableID
Expand Down
23 changes: 23 additions & 0 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -2073,6 +2073,16 @@ func (w *worker) onExchangeTablePartition(d *ddlCtx, t *meta.Meta, job *model.Jo
return ver, errors.Trace(err)
}

if defID != partDef.ID {
logutil.BgLogger().Info("Exchange partition id changed, updating to actual id", zap.String("category", "ddl"),
zap.String("job", job.String()), zap.Int64("defID", defID), zap.Int64("partDef.ID", partDef.ID))
job.Args[0] = partDef.ID
defID = partDef.ID
err = updateDDLJob2Table(w.sess, job, true)
if err != nil {
return ver, errors.Trace(err)
}
}
nt.ExchangePartitionInfo = &model.ExchangePartitionInfo{
ExchangePartitionID: ptID,
ExchangePartitionDefID: defID,
Expand All @@ -2094,6 +2104,18 @@ func (w *worker) onExchangeTablePartition(d *ddlCtx, t *meta.Meta, job *model.Jo
delayForAsyncCommit()
}

if defID != partDef.ID {
// Should never happen, should have been updated above, in previous state!
logutil.BgLogger().Error("Exchange partition id changed, updating to actual id", zap.String("category", "ddl"),
zap.String("job", job.String()), zap.Int64("defID", defID), zap.Int64("partDef.ID", partDef.ID))
job.Args[0] = partDef.ID
defID = partDef.ID
err = updateDDLJob2Table(w.sess, job, true)
if err != nil {
return ver, errors.Trace(err)
}
}

if withValidation {
err = checkExchangePartitionRecordValidation(w, pt, index, ntDbInfo.Name, nt.Name)
if err != nil {
Expand Down Expand Up @@ -2201,6 +2223,7 @@ func (w *worker) onExchangeTablePartition(d *ddlCtx, t *meta.Meta, job *model.Jo
ntr := rules[ntrID]
ptr := rules[ptrID]

// This must be a bug, nt cannot be partitioned!
partIDs := getPartitionIDs(nt)

var setRules []*label.Rule
Expand Down
28 changes: 22 additions & 6 deletions infoschema/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,31 +296,46 @@ func (b *Builder) applyExchangeTablePartition(m *meta.Meta, diff *model.SchemaDi
ntID := diff.OldTableID
ptSchemaID := diff.SchemaID
ptID := diff.TableID
partID := diff.TableID
if len(diff.AffectedOpts) > 0 {
// From old version
ptID = diff.AffectedOpts[0].TableID
ptSchemaID = diff.AffectedOpts[0].SchemaID
if diff.AffectedOpts[0].SchemaID != 0 {
ptSchemaID = diff.AffectedOpts[0].SchemaID
}
}
// The normal table needs to be updated first:
// Just update the tables separately
currDiff := &model.SchemaDiff{
// This is only for the case since https://github.com/pingcap/tidb/pull/45877
// Fixed now, by adding back the AffectedOpts
// to carry the partitioned Table ID.
Type: diff.Type,
Version: diff.Version,
TableID: ntID,
SchemaID: ntSchemaID,
}
if ptID != partID {
currDiff.TableID = partID
currDiff.OldTableID = ntID
currDiff.OldSchemaID = ntSchemaID
}
ntIDs, err := b.applyTableUpdate(m, currDiff)
if err != nil {
return nil, errors.Trace(err)
}
b.markPartitionBundleShouldUpdate(ntID)
// Then the partitioned table
// partID is the new id for the non-partitioned table!
b.markTableBundleShouldUpdate(partID)
// Then the partitioned table, will re-read the whole table, including all partitions!
currDiff.TableID = ptID
currDiff.SchemaID = ptSchemaID
currDiff.OldTableID = ptID
currDiff.OldSchemaID = ptSchemaID
ptIDs, err := b.applyTableUpdate(m, currDiff)
if err != nil {
return nil, errors.Trace(err)
}
b.markTableBundleShouldUpdate(ptID)
// ntID is the new id for the partition!
b.markPartitionBundleShouldUpdate(ntID)
err = updateAutoIDForExchangePartition(b.store, ptSchemaID, ptID, ntSchemaID, ntID)
if err != nil {
return nil, errors.Trace(err)
Expand Down Expand Up @@ -426,7 +441,8 @@ func (b *Builder) applyTableUpdate(m *meta.Meta, diff *model.SchemaDiff) ([]int6
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 Down
2 changes: 2 additions & 0 deletions table/tables/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ go_test(
"//util/codec",
"//util/collate",
"//util/dbterror",
"//util/logutil",
"//util/mock",
"//util/rowcodec",
"//util/stmtsummary",
Expand All @@ -113,5 +114,6 @@ go_test(
"@com_github_tikv_client_go_v2//oracle",
"@org_golang_google_grpc//:grpc",
"@org_uber_go_goleak//:goleak",
"@org_uber_go_zap//:zap",
],
)
Loading