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: refactor schema builder #31172

Merged
merged 20 commits into from
Dec 31, 2021
Merged
Changes from 3 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
159 changes: 105 additions & 54 deletions infoschema/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type Builder struct {
// 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) {
b.is.schemaMetaVersion = diff.Version
var tblIDs []int64
var err error
switch diff.Type {
case model.ActionCreateSchema:
return nil, b.applyCreateSchema(m, diff)
Expand All @@ -67,13 +69,110 @@ func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, erro
return b.applyDropPolicy(diff.SchemaID), nil
case model.ActionAlterPlacementPolicy:
return b.applyAlterPolicy(m, diff)
default:
roDBInfo, ok := b.is.SchemaByID(diff.SchemaID)
if !ok {
return nil, ErrDatabaseNotExists.GenWithStackByArgs(
fmt.Sprintf("(Schema ID %d)", diff.SchemaID),
)
}
dbInfo := b.copySchemaTables(roDBInfo.Name.L)
switch diff.Type {
case model.ActionTruncateTablePartition, model.ActionTruncateTable:
tblIDs, err = b.applyTruncateTableOrPartition(m, diff, dbInfo)
case model.ActionDropTable, model.ActionDropTablePartition:
tblIDs, err = b.applyDropTableOrParition(m, diff, dbInfo)
case model.ActionRecoverTable:
tblIDs, err = b.applyRecoverTable(m, diff, dbInfo)
default:
tblIDs, err = b.applyDefaultAction(m, diff, dbInfo)
}
}
roDBInfo, ok := b.is.SchemaByID(diff.SchemaID)
if !ok {
return nil, ErrDatabaseNotExists.GenWithStackByArgs(
fmt.Sprintf("(Schema ID %d)", diff.SchemaID),
)
return tblIDs, err
}

func (b *Builder) applyTruncateTableOrPartition(m *meta.Meta, diff *model.SchemaDiff, dbInfo *model.DBInfo) ([]int64, error) {
tblIDs, err := b.applyTableUpdate(m, diff, dbInfo)
if err != nil {
return nil, errors.Trace(err)
}

for _, opt := range diff.AffectedOpts {
if diff.Type == model.ActionTruncateTablePartition {
// Reduce the impact on DML when executing partition DDL. eg.
// While session 1 performs the DML operation associated with partition 1,
// the TRUNCATE operation of session 2 on partition 2 does not cause the operation of session 1 to fail.
tblIDs = append(tblIDs, opt.OldTableID)
}
b.applyPlacementDelete(placement.GroupID(opt.OldTableID))
err := b.applyPlacementUpdate(placement.GroupID(opt.TableID))
if err != nil {
return nil, errors.Trace(err)
}
}
return tblIDs, nil
}

func (b *Builder) applyDropTableOrParition(m *meta.Meta, diff *model.SchemaDiff, dbInfo *model.DBInfo) ([]int64, error) {
tblIDs, err := b.applyTableUpdate(m, diff, dbInfo)
if err != nil {
return nil, errors.Trace(err)
}

for _, opt := range diff.AffectedOpts {
b.applyPlacementDelete(placement.GroupID(opt.OldTableID))
}
return tblIDs, nil
}

func (b *Builder) applyRecoverTable(m *meta.Meta, diff *model.SchemaDiff, dbInfo *model.DBInfo) ([]int64, error) {
tblIDs, err := b.applyTableUpdate(m, diff, dbInfo)
if err != nil {
return nil, errors.Trace(err)
}

for _, opt := range diff.AffectedOpts {
err := b.applyPlacementUpdate(placement.GroupID(opt.TableID))
if err != nil {
return nil, errors.Trace(err)
}
}
return tblIDs, nil
}

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

typ := diff.Type
switch diff.Type {
case model.ActionRenameTables:
xhebox marked this conversation as resolved.
Show resolved Hide resolved
typ = model.ActionRenameTable
}

for _, opt := range diff.AffectedOpts {
var err error
affectedDiff := &model.SchemaDiff{
Version: diff.Version,
Type: typ,
SchemaID: opt.SchemaID,
TableID: opt.TableID,
OldSchemaID: opt.OldSchemaID,
OldTableID: opt.OldTableID,
}
affectedIDs, err := b.ApplyDiff(m, affectedDiff)
if err != nil {
return nil, errors.Trace(err)
}
tblIDs = append(tblIDs, affectedIDs...)
}

return tblIDs, nil
}

func (b *Builder) applyTableUpdate(m *meta.Meta, diff *model.SchemaDiff, dbInfo *model.DBInfo) ([]int64, error) {
var oldTableID, newTableID int64
switch diff.Type {
case model.ActionCreateTable, model.ActionCreateSequence, model.ActionRecoverTable:
Expand Down Expand Up @@ -109,7 +208,6 @@ func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, erro
return nil, errors.Trace(err)
}
}
dbInfo := b.copySchemaTables(roDBInfo.Name.L)
b.copySortedTables(oldTableID, newTableID)

tblIDs := make([]int64, 0, 2)
Expand All @@ -130,7 +228,7 @@ func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, erro
}

tmpIDs := tblIDs
if (diff.Type == model.ActionRenameTable || diff.Type == model.ActionRenameTables) && diff.OldSchemaID != diff.SchemaID {
if diff.Type == model.ActionRenameTable && diff.OldSchemaID != diff.SchemaID {
xhebox marked this conversation as resolved.
Show resolved Hide resolved
oldRoDBInfo, ok := b.is.SchemaByID(diff.OldSchemaID)
if !ok {
return nil, ErrDatabaseNotExists.GenWithStackByArgs(
Expand All @@ -156,53 +254,6 @@ func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, erro
return nil, errors.Trace(err)
}
}
if diff.AffectedOpts != nil {
for _, opt := range diff.AffectedOpts {
switch diff.Type {
case model.ActionTruncateTablePartition:
// Reduce the impact on DML when executing partition DDL. eg.
// While session 1 performs the DML operation associated with partition 1,
// the TRUNCATE operation of session 2 on partition 2 does not cause the operation of session 1 to fail.
tblIDs = append(tblIDs, opt.OldTableID)
b.applyPlacementDelete(placement.GroupID(opt.OldTableID))
err := b.applyPlacementUpdate(placement.GroupID(opt.TableID))
if err != nil {
return nil, errors.Trace(err)
}
continue
case model.ActionDropTable, model.ActionDropTablePartition:
b.applyPlacementDelete(placement.GroupID(opt.OldTableID))
continue
case model.ActionTruncateTable:
b.applyPlacementDelete(placement.GroupID(opt.OldTableID))
err := b.applyPlacementUpdate(placement.GroupID(opt.TableID))
if err != nil {
return nil, errors.Trace(err)
}
continue
case model.ActionRecoverTable:
err := b.applyPlacementUpdate(placement.GroupID(opt.TableID))
if err != nil {
return nil, errors.Trace(err)
}
continue
}
var err error
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(m, affectedDiff)
if err != nil {
return nil, errors.Trace(err)
}
tblIDs = append(tblIDs, affectedIDs...)
}
}
return tblIDs, nil
}

Expand Down