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

session, ddl: amend txn correctly when "modify column" needs reorg data with tidb_enable_amend_pessimistic_txn=on. (#26269) #26273

Merged
merged 2 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
69 changes: 69 additions & 0 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,75 @@ func (s *testDBSuite2) TestAddUniqueIndexRollback(c *C) {
testAddIndexRollback(c, s.store, s.lease, idxName, addIdxSQL, errMsg, hasNullValsInKey)
}

func (s *testSerialDBSuite) TestWriteReorgForColumnTypeChangeOnAmendTxn(c *C) {
tk2 := testkit.NewTestKit(c, s.store)
tk2.MustExec("use test_db")
tk2.MustExec("set global tidb_enable_amend_pessimistic_txn = ON;")
defer func() {
tk2.MustExec("set global tidb_enable_amend_pessimistic_txn = OFF;")
}()

d := s.dom.DDL()
originalHook := d.GetHook()
defer d.(ddl.DDLForTest).SetHook(originalHook)
testInsertOnModifyColumn := func(sql string, startColState, commitColState model.SchemaState, retStrs []string, retErr error) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test_db")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1 (c1 int, c2 int, c3 int, unique key(c1))")
tk.MustExec("insert into t1 values (20, 20, 20);")

var checkErr error
tk1 := testkit.NewTestKit(c, s.store)
hook := &ddl.TestDDLCallback{Do: s.dom}
times := 0
hook.OnJobUpdatedExported = func(job *model.Job) {
if job.Type != model.ActionModifyColumn || checkErr != nil ||
(job.SchemaState != startColState && job.SchemaState != commitColState) {
return
}

if job.SchemaState == startColState {
tk1.MustExec("use test_db")
tk1.MustExec("begin pessimistic;")
tk1.MustExec("insert into t1 values(101, 102, 103)")
return
}
if times == 0 {
_, checkErr = tk1.Exec("commit;")
}
times++
}
d.(ddl.DDLForTest).SetHook(hook)

tk.MustExec(sql)
if retErr == nil {
c.Assert(checkErr, IsNil)
} else {
c.Assert(strings.Contains(checkErr.Error(), retErr.Error()), IsTrue)
}
tk.MustQuery("select * from t1;").Check(testkit.Rows(retStrs...))

tk.MustExec("admin check table t1")
}

// Testing it needs reorg data.
ddlStatement := "alter table t1 change column c2 cc smallint;"
testInsertOnModifyColumn(ddlStatement, model.StateNone, model.StateWriteReorganization, []string{"20 20 20"}, domain.ErrInfoSchemaChanged)
testInsertOnModifyColumn(ddlStatement, model.StateDeleteOnly, model.StateWriteReorganization, []string{"20 20 20"}, domain.ErrInfoSchemaChanged)
testInsertOnModifyColumn(ddlStatement, model.StateWriteOnly, model.StateWriteReorganization, []string{"20 20 20"}, domain.ErrInfoSchemaChanged)
testInsertOnModifyColumn(ddlStatement, model.StateNone, model.StatePublic, []string{"20 20 20"}, domain.ErrInfoSchemaChanged)
testInsertOnModifyColumn(ddlStatement, model.StateDeleteOnly, model.StatePublic, []string{"20 20 20"}, domain.ErrInfoSchemaChanged)
testInsertOnModifyColumn(ddlStatement, model.StateWriteOnly, model.StatePublic, []string{"20 20 20"}, domain.ErrInfoSchemaChanged)

// Testing it needs not reorg data. This case only have two state: none, public.
ddlStatement = "alter table t1 change column c2 cc bigint;"
testInsertOnModifyColumn(ddlStatement, model.StateNone, model.StateWriteReorganization, []string{"20 20 20"}, nil)
testInsertOnModifyColumn(ddlStatement, model.StateWriteOnly, model.StateWriteReorganization, []string{"20 20 20"}, nil)
testInsertOnModifyColumn(ddlStatement, model.StateNone, model.StatePublic, []string{"20 20 20", "101 102 103"}, nil)
testInsertOnModifyColumn(ddlStatement, model.StateWriteOnly, model.StatePublic, []string{"20 20 20"}, nil)
}

func (s *testSerialDBSuite) TestAddExpressionIndexRollback(c *C) {
config.UpdateGlobal(func(conf *config.Config) {
conf.Experimental.AllowsExpressionIndex = true
Expand Down
4 changes: 3 additions & 1 deletion session/schema_amender.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,9 @@ func colChangeAmendable(colAtStart *model.ColumnInfo, colAtCommit *model.ColumnI
// collectModifyColAmendOps is used to check if there is only column size increasing change.Other column type changes
// such as column change from nullable to not null or column type change are not supported by now.
func (a *amendCollector) collectModifyColAmendOps(tblAtStart, tblAtCommit table.Table) ([]amendOp, error) {
for _, colAtCommit := range tblAtCommit.Cols() {
for _, colAtCommit := range tblAtCommit.WritableCols() {
colAtStart := findColByID(tblAtStart, colAtCommit.ID)
// It can't find colAtCommit's ID from tblAtStart's public columns when "modify/change column" needs reorg data.
if colAtStart != nil {
err := colChangeAmendable(colAtStart.ColumnInfo, colAtCommit.ColumnInfo)
if err != nil {
Expand All @@ -206,6 +207,7 @@ func (a *amendCollector) collectModifyColAmendOps(tblAtStart, tblAtCommit table.
// is newly added or modified from an original column.Report error to solve the issue
// https://github.com/pingcap/tidb/issues/21470. This change will make amend fail for adding column
// and modifying columns at the same time.
// In addition, amended operations are not currently supported and it goes to this logic when "modify/change column" needs reorg data.
return nil, errors.Errorf("column=%v id=%v is not found for table=%v checking column modify",
colAtCommit.Name, colAtCommit.ID, tblAtCommit.Meta().Name.String())
}
Expand Down