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

table partition: enhance exchange partition with table to reset auto id #36913

Merged
merged 20 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,18 @@ func TestAlterTableExchangePartition(t *testing.T) {
require.NoError(t, err)

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

tk.MustExec("create table e17 (a int)")
tk.MustExec("alter table e17 set tiflash replica 1")
tk.MustExec("insert into e17 values (1)")

tk.MustExec("create table e18 (a int) partition by range (a) (partition p0 values less than (4), partition p1 values less than (10))")
tk.MustExec("alter table e18 set tiflash replica 1")
tk.MustExec("insert into e18 values (2)")

tk.MustExec("alter table e18 exchange partition p0 with table e17")
tk.MustQuery("select * /*+ read_from_storage(tiflash[e18]) */ from e18").Check(testkit.Rows("1"))
tk.MustQuery("select * /*+ read_from_storage(tiflash[e17]) */ from e17").Check(testkit.Rows("2"))
}

func TestExchangePartitionTableCompatiable(t *testing.T) {
Expand Down Expand Up @@ -2377,6 +2389,43 @@ func TestExchangePartitionHook(t *testing.T) {
tk.MustQuery("select * from pt partition(p0)").Check(testkit.Rows("1"))
}

func TestExchangePartitionAutoID(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("set @@tidb_enable_exchange_partition=1")
defer tk.MustExec("set @@tidb_enable_exchange_partition=0")

tk.MustExec("use test")
tk.MustExec(`create table pt (a int primary key auto_increment) partition by range(a) (
partition p0 values less than (3),
partition p1 values less than (6),
PARTITION p2 values less than (9),
PARTITION p3 values less than (50000000)
);`)
tk.MustExec(`create table nt(a int primary key auto_increment);`)

tk.MustExec(`insert into pt values (0), (4)`)
tk.MustExec("insert into nt values (1)")

hook := &ddl.TestDDLCallback{Do: dom}
dom.DDL().SetHook(hook)
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/exchangePartitionAutoID", `return(true)`))
defer require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/exchangePartitionAutoID"))

hookFunc := func(job *model.Job) {
if job.Type == model.ActionExchangeTablePartition && job.State == model.JobStateRunning {
ymkzpx marked this conversation as resolved.
Show resolved Hide resolved
tk.MustExec(`insert into pt values (40000000)`)
}
}
hook.OnJobUpdatedExported = hookFunc

tk.MustExec("alter table pt exchange partition p0 with table nt")
tk.MustExec("insert into nt values (NULL)")
tk.MustQuery("select count(*) from nt where a >= 4000000").Check(testkit.Rows("1"))
tk.MustQuery("select count(*) from pt where a >= 4000000").Check(testkit.Rows("1"))
}

func TestExchangePartitionExpressIndex(t *testing.T) {
restore := config.RestoreFunc()
defer restore()
Expand Down
8 changes: 8 additions & 0 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,14 @@ func (w *worker) onExchangeTablePartition(d *ddlCtx, t *meta.Meta, job *model.Jo
return ver, errors.Trace(err)
}

failpoint.Inject("exchangePartitionAutoID", func(val failpoint.Value) {
if val.(bool) {
d.mu.RLock()
d.mu.hook.OnJobUpdated(job)
ymkzpx marked this conversation as resolved.
Show resolved Hide resolved
d.mu.RUnlock()
}
})

err = checkExchangePartitionPlacementPolicy(t, partDef.PlacementPolicyRef, nt.PlacementPolicyRef)
if err != nil {
job.State = model.JobStateCancelled
Expand Down
6 changes: 3 additions & 3 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (do *Domain) loadInfoSchema(startTS uint64) (infoschema.InfoSchema, bool, i
// 3. There are less 100 diffs.
startTime := time.Now()
if currentSchemaVersion != 0 && neededSchemaVersion > currentSchemaVersion && neededSchemaVersion-currentSchemaVersion < 100 {
is, relatedChanges, err := do.tryLoadSchemaDiffs(m, currentSchemaVersion, neededSchemaVersion)
is, relatedChanges, err := do.tryLoadSchemaDiffs(do.store, m, currentSchemaVersion, neededSchemaVersion)
ymkzpx marked this conversation as resolved.
Show resolved Hide resolved
if err == nil {
do.infoCache.Insert(is, startTS)
logutil.BgLogger().Info("diff load InfoSchema success",
Expand Down Expand Up @@ -281,7 +281,7 @@ func (do *Domain) fetchSchemasWithTables(schemas []*model.DBInfo, m *meta.Meta,
// Return true if the schema is loaded successfully.
// Return false if the schema can not be loaded by schema diff, then we need to do full load.
// The second returned value is the delta updated table and partition IDs.
func (do *Domain) tryLoadSchemaDiffs(m *meta.Meta, usedVersion, newVersion int64) (infoschema.InfoSchema, *transaction.RelatedSchemaChange, error) {
func (do *Domain) tryLoadSchemaDiffs(store kv.Storage, m *meta.Meta, usedVersion, newVersion int64) (infoschema.InfoSchema, *transaction.RelatedSchemaChange, error) {
ymkzpx marked this conversation as resolved.
Show resolved Hide resolved
var diffs []*model.SchemaDiff
for usedVersion < newVersion {
usedVersion++
Expand All @@ -301,7 +301,7 @@ func (do *Domain) tryLoadSchemaDiffs(m *meta.Meta, usedVersion, newVersion int64
phyTblIDs := make([]int64, 0, len(diffs))
actions := make([]uint64, 0, len(diffs))
for _, diff := range diffs {
IDs, err := builder.ApplyDiff(m, diff)
IDs, err := builder.ApplyDiff(store, m, diff)
ymkzpx marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, nil, err
}
Expand Down
85 changes: 78 additions & 7 deletions infoschema/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package infoschema

import (
"context"
"fmt"
"strings"

Expand All @@ -32,6 +33,7 @@ import (
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/util/domainutil"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/mathutil"
"github.com/pingcap/tidb/util/sqlexec"
"go.uber.org/zap"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -190,7 +192,7 @@ type Builder struct {

// ApplyDiff applies SchemaDiff to the new InfoSchema.
// Return the detail updated table IDs that are produced from SchemaDiff and an error.
func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, error) {
func (b *Builder) ApplyDiff(store kv.Storage, m *meta.Meta, diff *model.SchemaDiff) ([]int64, error) {
ymkzpx marked this conversation as resolved.
Show resolved Hide resolved
b.is.schemaMetaVersion = diff.Version
switch diff.Type {
case model.ActionCreateSchema:
Expand All @@ -214,13 +216,15 @@ func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, erro
case model.ActionRecoverTable:
return b.applyRecoverTable(m, diff)
case model.ActionCreateTables:
return b.applyCreateTables(m, diff)
return b.applyCreateTables(store, m, diff)
case model.ActionExchangeTablePartition:
return b.applyExchangeTablePartition(store, m, diff)
default:
return b.applyDefaultAction(m, diff)
return b.applyDefaultAction(store, m, diff)
}
}

func (b *Builder) applyCreateTables(m *meta.Meta, diff *model.SchemaDiff) ([]int64, error) {
func (b *Builder) applyCreateTables(store kv.Storage, m *meta.Meta, diff *model.SchemaDiff) ([]int64, error) {
tblIDs := make([]int64, 0, len(diff.AffectedOpts))
if diff.AffectedOpts != nil {
for _, opt := range diff.AffectedOpts {
Expand All @@ -232,7 +236,7 @@ func (b *Builder) applyCreateTables(m *meta.Meta, diff *model.SchemaDiff) ([]int
OldSchemaID: opt.OldSchemaID,
OldTableID: opt.OldTableID,
}
affectedIDs, err := b.ApplyDiff(m, affectedDiff)
affectedIDs, err := b.ApplyDiff(store, m, affectedDiff)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down Expand Up @@ -287,7 +291,74 @@ func (b *Builder) applyRecoverTable(m *meta.Meta, diff *model.SchemaDiff) ([]int
return tblIDs, nil
}

func (b *Builder) applyDefaultAction(m *meta.Meta, diff *model.SchemaDiff) ([]int64, error) {
func (b *Builder) applyExchangeTablePartition(store kv.Storage, m *meta.Meta, diff *model.SchemaDiff) ([]int64, error) {
tblIDs, err := b.applyTableUpdate(m, diff)
if err != nil {
return nil, errors.Trace(err)
}

for _, opt := range diff.AffectedOpts {
affectedDiff := &model.SchemaDiff{
Version: diff.Version,
Type: diff.Type,
SchemaID: opt.SchemaID,
TableID: opt.TableID,
OldSchemaID: opt.OldSchemaID,
OldTableID: opt.OldTableID,
}
affectedIDs, err := b.ApplyDiff(store, m, affectedDiff)
ymkzpx marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, errors.Trace(err)
}
tblIDs = append(tblIDs, affectedIDs...)

// handle partition table and table AutoID
err = updateAutoIDForExchangePartition(store, affectedDiff.SchemaID, affectedDiff.TableID, diff.SchemaID, diff.TableID)
ymkzpx marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, errors.Trace(err)
}
}

return tblIDs, nil
}

func updateAutoIDForExchangePartition(store kv.Storage, ptSchemaID, ptID, ntSchemaID, ntID int64) error {
// partition table auto IDs.
ymkzpx marked this conversation as resolved.
Show resolved Hide resolved

ymkzpx marked this conversation as resolved.
Show resolved Hide resolved
err := kv.RunInNewTxn(kv.WithInternalSourceType(context.Background(), kv.InternalTxnDDL), store, true, func(ctx context.Context, txn kv.Transaction) error {
t := meta.NewMeta(txn)
ptAutoIDs, err := t.GetAutoIDAccessors(ptSchemaID, ptID).Get()
if err != nil {
return err
}

// non-partition table auto IDs.
ntAutoIDs, err := t.GetAutoIDAccessors(ntSchemaID, ntID).Get()
if err != nil {
return err
}

// Set both tables to the maximum auto IDs between normal table and partitioned table.
newAutoIDs := meta.AutoIDGroup{
RowID: mathutil.Max(ptAutoIDs.RowID, ntAutoIDs.RowID),
IncrementID: mathutil.Max(ptAutoIDs.IncrementID, ntAutoIDs.IncrementID),
RandomID: mathutil.Max(ptAutoIDs.RandomID, ntAutoIDs.RandomID),
}
err = t.GetAutoIDAccessors(ptSchemaID, ptID).Put(newAutoIDs)
if err != nil {
return err
}
err = t.GetAutoIDAccessors(ntSchemaID, ntID).Put(newAutoIDs)
if err != nil {
return err
}
return nil
})

return err
}

func (b *Builder) applyDefaultAction(store kv.Storage, m *meta.Meta, diff *model.SchemaDiff) ([]int64, error) {
ymkzpx marked this conversation as resolved.
Show resolved Hide resolved
tblIDs, err := b.applyTableUpdate(m, diff)
if err != nil {
return nil, errors.Trace(err)
Expand All @@ -303,7 +374,7 @@ func (b *Builder) applyDefaultAction(m *meta.Meta, diff *model.SchemaDiff) ([]in
OldSchemaID: opt.OldSchemaID,
OldTableID: opt.OldTableID,
}
affectedIDs, err := b.ApplyDiff(m, affectedDiff)
affectedIDs, err := b.ApplyDiff(store, m, affectedDiff)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
14 changes: 7 additions & 7 deletions infoschema/infoschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func TestBasic(t *testing.T) {

txn, err := store.Begin()
require.NoError(t, err)
checkApplyCreateNonExistsSchemaDoesNotPanic(t, txn, builder)
checkApplyCreateNonExistsTableDoesNotPanic(t, txn, builder, dbID)
checkApplyCreateNonExistsSchemaDoesNotPanic(t, txn, builder, store)
checkApplyCreateNonExistsTableDoesNotPanic(t, txn, builder, dbID, store)
err = txn.Rollback()
require.NoError(t, err)

Expand Down Expand Up @@ -202,7 +202,7 @@ func TestBasic(t *testing.T) {
require.NoError(t, err)
txn, err = store.Begin()
require.NoError(t, err)
_, err = builder.ApplyDiff(meta.NewMeta(txn), &model.SchemaDiff{Type: model.ActionRenameTable, SchemaID: dbID, TableID: tbID, OldSchemaID: dbID})
_, err = builder.ApplyDiff(store, meta.NewMeta(txn), &model.SchemaDiff{Type: model.ActionRenameTable, SchemaID: dbID, TableID: tbID, OldSchemaID: dbID})
require.NoError(t, err)
err = txn.Rollback()
require.NoError(t, err)
Expand Down Expand Up @@ -235,15 +235,15 @@ func TestMockInfoSchema(t *testing.T) {
require.Equal(t, colInfo, tbl.Cols()[0].ColumnInfo)
}

func checkApplyCreateNonExistsSchemaDoesNotPanic(t *testing.T, txn kv.Transaction, builder *infoschema.Builder) {
func checkApplyCreateNonExistsSchemaDoesNotPanic(t *testing.T, txn kv.Transaction, builder *infoschema.Builder, store kv.Storage) {
m := meta.NewMeta(txn)
_, err := builder.ApplyDiff(m, &model.SchemaDiff{Type: model.ActionCreateSchema, SchemaID: 999})
_, err := builder.ApplyDiff(store, m, &model.SchemaDiff{Type: model.ActionCreateSchema, SchemaID: 999})
require.True(t, infoschema.ErrDatabaseNotExists.Equal(err))
}

func checkApplyCreateNonExistsTableDoesNotPanic(t *testing.T, txn kv.Transaction, builder *infoschema.Builder, dbID int64) {
func checkApplyCreateNonExistsTableDoesNotPanic(t *testing.T, txn kv.Transaction, builder *infoschema.Builder, dbID int64, store kv.Storage) {
m := meta.NewMeta(txn)
_, err := builder.ApplyDiff(m, &model.SchemaDiff{Type: model.ActionCreateTable, SchemaID: dbID, TableID: 999})
_, err := builder.ApplyDiff(store, m, &model.SchemaDiff{Type: model.ActionCreateTable, SchemaID: dbID, TableID: 999})
require.True(t, infoschema.ErrTableNotExists.Equal(err))
}

Expand Down