-
Notifications
You must be signed in to change notification settings - Fork 1
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
support multi-schema-change for drop index #20
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,6 +217,9 @@ func onAddColumn(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, err error) | |
} | ||
|
||
func locateOffsetForColumn(pos *ast.ColumnPosition, tblInfo *model.TableInfo) (offset int, err error) { | ||
if pos == nil { | ||
return -1, nil | ||
} | ||
// Get column offset. | ||
switch pos.Tp { | ||
case ast.ColumnPositionFirst: | ||
|
@@ -362,7 +365,7 @@ func onAddColumns(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, err error | |
case model.StateWriteReorganization: | ||
// reorganization -> public | ||
// Adjust table column offsets. | ||
for i, newCol := range tblInfo.Columns[:len(tblInfo.Columns)-len(positions)] { | ||
for i, newCol := range tblInfo.Columns[len(tblInfo.Columns)-len(positions):] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added test in
|
||
offset, err := locateOffsetForColumn(positions[i], tblInfo) | ||
if err != nil { | ||
return ver, errors.Trace(err) | ||
|
@@ -540,10 +543,7 @@ func onDropColumn(t *meta.Meta, job *model.Job) (ver int64, _ error) { | |
} | ||
if job.MultiSchemaInfo != nil && job.MultiSchemaInfo.Revertible { | ||
job.MarkNonRevertible() | ||
ver, err = updateVersionAndTableInfoWithCheck(t, job, tblInfo, false) | ||
if err != nil { | ||
return ver, errors.Trace(err) | ||
} | ||
return updateVersionAndTableInfoWithCheck(t, job, tblInfo, false) | ||
} | ||
|
||
originalState := colInfo.State | ||
|
@@ -590,6 +590,7 @@ func onDropColumn(t *meta.Meta, job *model.Job) (ver int64, _ error) { | |
case model.StateDeleteReorganization: | ||
// reorganization -> absent | ||
// All reorganization jobs are done, drop this column. | ||
tblInfo.MoveColumnInfo(colInfo.Offset, len(tblInfo.Columns)-1) | ||
tblInfo.Columns = tblInfo.Columns[:len(tblInfo.Columns)-1] | ||
colInfo.State = model.StateNone | ||
ver, err = updateVersionAndTableInfo(t, job, tblInfo, originalState != colInfo.State) | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -16,6 +16,7 @@ package ddl | |||||||
|
||||||||
import ( | ||||||||
"context" | ||||||||
"sort" | ||||||||
"strings" | ||||||||
"sync/atomic" | ||||||||
"time" | ||||||||
|
@@ -621,11 +622,9 @@ func onDropIndex(t *meta.Meta, job *model.Job) (ver int64, _ error) { | |||||||
return ver, errors.Trace(dbterror.ErrOptOnCacheTable.GenWithStackByArgs("Drop Index")) | ||||||||
} | ||||||||
|
||||||||
dependentHiddenCols := make([]*model.ColumnInfo, 0) | ||||||||
for _, indexColumn := range indexInfo.Columns { | ||||||||
if tblInfo.Columns[indexColumn.Offset].Hidden { | ||||||||
dependentHiddenCols = append(dependentHiddenCols, tblInfo.Columns[indexColumn.Offset]) | ||||||||
} | ||||||||
if job.MultiSchemaInfo != nil && job.MultiSchemaInfo.Revertible { | ||||||||
job.MarkNonRevertible() | ||||||||
return updateVersionAndTableInfo(t, job, tblInfo, false) | ||||||||
} | ||||||||
|
||||||||
originalState := indexInfo.State | ||||||||
|
@@ -656,25 +655,11 @@ func onDropIndex(t *meta.Meta, job *model.Job) (ver int64, _ error) { | |||||||
job.SchemaState = model.StateDeleteReorganization | ||||||||
case model.StateDeleteReorganization: | ||||||||
// reorganization -> absent | ||||||||
if len(dependentHiddenCols) > 0 { | ||||||||
firstHiddenOffset := dependentHiddenCols[0].Offset | ||||||||
for i := 0; i < len(dependentHiddenCols); i++ { | ||||||||
// Set this column's offset to the last and reset all following columns' offsets. | ||||||||
adjustColumnInfoInDropColumn(tblInfo, firstHiddenOffset) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
newIndices := make([]*model.IndexInfo, 0, len(tblInfo.Indices)) | ||||||||
for _, idx := range tblInfo.Indices { | ||||||||
if idx.Name.L != indexInfo.Name.L { | ||||||||
newIndices = append(newIndices, idx) | ||||||||
} | ||||||||
} | ||||||||
tblInfo.Indices = newIndices | ||||||||
// Set column index flag. | ||||||||
dropIndexColumnFlag(tblInfo, indexInfo) | ||||||||
removeDependentHiddenColumns(tblInfo, indexInfo) | ||||||||
removeIndexInfo(tblInfo, indexInfo) | ||||||||
|
||||||||
tblInfo.Columns = tblInfo.Columns[:len(tblInfo.Columns)-len(dependentHiddenCols)] | ||||||||
failpoint.Inject("mockExceedErrorLimit", func(val failpoint.Value) { | ||||||||
if val.(bool) { | ||||||||
panic("panic test in cancelling add index") | ||||||||
|
@@ -702,6 +687,44 @@ func onDropIndex(t *meta.Meta, job *model.Job) (ver int64, _ error) { | |||||||
return ver, errors.Trace(err) | ||||||||
} | ||||||||
|
||||||||
func removeDependentHiddenColumns(tblInfo *model.TableInfo, idxInfo *model.IndexInfo) { | ||||||||
hiddenColOffs := make([]int, 0) | ||||||||
for _, indexColumn := range idxInfo.Columns { | ||||||||
col := tblInfo.Columns[indexColumn.Offset] | ||||||||
if col.Hidden { | ||||||||
hiddenColOffs = append(hiddenColOffs, col.Offset) | ||||||||
} | ||||||||
} | ||||||||
// Sort the offset in descending order. | ||||||||
sort.Slice(hiddenColOffs, func(i, j int) bool { | ||||||||
Defined2014 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
return hiddenColOffs[i] > hiddenColOffs[j] | ||||||||
}) | ||||||||
// Move all the dependent hidden columns to the end. | ||||||||
endOffset := len(tblInfo.Columns) - 1 | ||||||||
for _, offset := range hiddenColOffs { | ||||||||
tblInfo.MoveColumnInfo(offset, endOffset) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the order does not matter, because the columns will be removed anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will reduce the number of exchanges |
||||||||
} | ||||||||
tblInfo.Columns = tblInfo.Columns[:len(tblInfo.Columns)-len(hiddenColOffs)] | ||||||||
} | ||||||||
|
||||||||
func removeIndexInfo(tblInfo *model.TableInfo, idxInfo *model.IndexInfo) { | ||||||||
indices := tblInfo.Indices | ||||||||
offset := -1 | ||||||||
for i, idx := range indices { | ||||||||
if idxInfo.ID == idx.ID { | ||||||||
offset = i | ||||||||
break | ||||||||
} | ||||||||
} | ||||||||
if offset == -1 { | ||||||||
// The target index has been removed. | ||||||||
return | ||||||||
} | ||||||||
// Swap the target index to the end and remove it. | ||||||||
indices[offset], indices[len(indices)-1] = indices[len(indices)-1], indices[offset] | ||||||||
tblInfo.Indices = tblInfo.Indices[:len(tblInfo.Indices)-1] | ||||||||
} | ||||||||
|
||||||||
func checkDropIndex(t *meta.Meta, job *model.Job) (*model.TableInfo, *model.IndexInfo, error) { | ||||||||
schemaID := job.SchemaID | ||||||||
tblInfo, err := getTableInfoAndCancelFaultJob(t, job, schemaID) | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
locateOffsetForColumn
is also used byAddColumn
so the pos can be null.