Skip to content

Commit

Permalink
*: remove fail field from TxnState (#8712) (#8764)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored and jackysp committed Dec 25, 2018
1 parent 8d1db42 commit 305a083
Show file tree
Hide file tree
Showing 36 changed files with 552 additions and 275 deletions.
16 changes: 13 additions & 3 deletions ddl/column_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ func (s *testColumnChangeSuite) TestColumnChange(c *C) {
row := types.MakeDatums(1, 2)
h, err := originTable.AddRecord(ctx, row, false)
c.Assert(err, IsNil)
err = ctx.Txn(true).Commit(context.Background())
txn, err := ctx.Txn(true)
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)

var mu sync.Mutex
Expand Down Expand Up @@ -125,7 +127,11 @@ func (s *testColumnChangeSuite) TestColumnChange(c *C) {
}
mu.Unlock()
}
err = hookCtx.Txn(true).Commit(context.Background())
txn, err := hookCtx.Txn(true)
if err != nil {
checkErr = errors.Trace(err)
}
err = txn.Commit(context.Background())
if err != nil {
checkErr = errors.Trace(err)
}
Expand Down Expand Up @@ -177,7 +183,11 @@ func (s *testColumnChangeSuite) testAddColumnNoDefault(c *C, ctx sessionctx.Cont
checkErr = errors.Trace(err)
}
}
err = hookCtx.Txn(true).Commit(context.TODO())
txn, err := hookCtx.Txn(true)
if err != nil {
checkErr = errors.Trace(err)
}
err = txn.Commit(context.TODO())
if err != nil {
checkErr = errors.Trace(err)
}
Expand Down
28 changes: 22 additions & 6 deletions ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,17 @@ func (s *testColumnSuite) checkColumnKVExist(ctx sessionctx.Context, t table.Tab
if err != nil {
return errors.Trace(err)
}
defer ctx.Txn(true).Commit(context.Background())
defer func() {
if txn, err1 := ctx.Txn(true); err1 == nil {
txn.Commit(context.Background())
}
}()
key := t.RecordKey(handle)
data, err := ctx.Txn(true).Get(key)
txn, err := ctx.Txn(true)
if err != nil {
return errors.Trace(err)
}
data, err := txn.Get(key)
if !isExist {
if terror.ErrorEqual(err, kv.ErrNotExist) {
return nil
Expand Down Expand Up @@ -751,7 +759,9 @@ func (s *testColumnSuite) TestAddColumn(c *C) {
handle, err := t.AddRecord(ctx, oldRow, false)
c.Assert(err, IsNil)

err = ctx.Txn(true).Commit(context.Background())
txn, err := ctx.Txn(true)
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)

newColName := "c4"
Expand Down Expand Up @@ -814,7 +824,9 @@ func (s *testColumnSuite) TestAddColumn(c *C) {
job = testDropTable(c, ctx, d, s.dbInfo, tblInfo)
testCheckJobDone(c, d, job, false)

err = ctx.Txn(true).Commit(context.Background())
txn, err = ctx.Txn(true)
c.Assert(err, IsNil)
txn.Commit(context.Background())
c.Assert(err, IsNil)

d.Stop()
Expand All @@ -838,7 +850,9 @@ func (s *testColumnSuite) TestDropColumn(c *C) {
_, err = t.AddRecord(ctx, append(row, types.NewDatum(defaultColValue)), false)
c.Assert(err, IsNil)

err = ctx.Txn(true).Commit(context.Background())
txn, err := ctx.Txn(true)
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)

checkOK := false
Expand Down Expand Up @@ -887,7 +901,9 @@ func (s *testColumnSuite) TestDropColumn(c *C) {
job = testDropTable(c, ctx, d, s.dbInfo, tblInfo)
testCheckJobDone(c, d, job, false)

err = ctx.Txn(true).Commit(context.Background())
txn, err = ctx.Txn(true)
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)

d.Stop()
Expand Down
43 changes: 31 additions & 12 deletions ddl/ddl_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,14 +374,18 @@ func (s *testDBSuite) TestCancelAddIndex(c *C) {
}
hookCtx := mock.NewContext()
hookCtx.Store = s.store
var err error
err = hookCtx.NewTxn()
err := hookCtx.NewTxn()
if err != nil {
checkErr = errors.Trace(err)
return
}
txn, err := hookCtx.Txn(true)
if err != nil {
checkErr = errors.Trace(err)
return
}
jobIDs := []int64{job.ID}
errs, err := admin.CancelJobs(hookCtx.Txn(true), jobIDs)
errs, err := admin.CancelJobs(txn, jobIDs)
if err != nil {
checkErr = errors.Trace(err)
return
Expand All @@ -391,7 +395,7 @@ func (s *testDBSuite) TestCancelAddIndex(c *C) {
checkErr = errors.Trace(errs[0])
return
}
err = hookCtx.Txn(true).Commit(context.Background())
err = txn.Commit(context.Background())
if err != nil {
checkErr = errors.Trace(err)
}
Expand Down Expand Up @@ -467,7 +471,12 @@ func (s *testDBSuite) TestCancelAddIndex1(c *C) {
checkErr = errors.Trace(err)
return
}
errs, err := admin.CancelJobs(hookCtx.Txn(true), jobIDs)
txn, err := hookCtx.Txn(true)
if err != nil {
checkErr = errors.Trace(err)
return
}
errs, err := admin.CancelJobs(txn, jobIDs)
if err != nil {
checkErr = errors.Trace(err)
return
Expand All @@ -476,7 +485,7 @@ func (s *testDBSuite) TestCancelAddIndex1(c *C) {
checkErr = errors.Trace(errs[0])
return
}
checkErr = hookCtx.Txn(true).Commit(context.Background())
checkErr = txn.Commit(context.Background())
}
}
s.dom.DDL().SetHook(hook)
Expand Down Expand Up @@ -708,12 +717,14 @@ LOOP:
// Make sure there is index with name c3_index.
c.Assert(nidx, NotNil)
c.Assert(nidx.Meta().ID, Greater, int64(0))
ctx.Txn(true).Rollback()
txn, err := ctx.Txn(true)
c.Assert(err, IsNil)
txn.Rollback()

c.Assert(ctx.NewTxn(), IsNil)
defer ctx.Txn(true).Rollback()
defer txn.Rollback()

it, err := nidx.SeekFirst(ctx.Txn(true))
it, err := nidx.SeekFirst(txn)
c.Assert(err, IsNil)
defer it.Close()

Expand Down Expand Up @@ -809,9 +820,13 @@ func checkDelRangeDone(c *C, ctx sessionctx.Context, idx table.Index) {
handles := make(map[int64]struct{})

c.Assert(ctx.NewTxn(), IsNil)
defer ctx.Txn(true).Rollback()
txn, err := ctx.Txn(true)
c.Assert(err, IsNil)
defer txn.Rollback()

it, err := idx.SeekFirst(ctx.Txn(true))
txn, err = ctx.Txn(true)
c.Assert(err, IsNil)
it, err := idx.SeekFirst(txn)
c.Assert(err, IsNil)
defer it.Close()

Expand Down Expand Up @@ -1037,7 +1052,11 @@ LOOP:
i := 0
j := 0
ctx.NewTxn()
defer ctx.Txn(true).Rollback()
defer func() {
if txn, err1 := ctx.Txn(true); err1 == nil {
txn.Rollback()
}
}()
err = t.IterRecords(ctx, t.FirstKey(), t.Cols(),
func(h int64, data []types.Datum, cols []*table.Column) (bool, error) {
i++
Expand Down
8 changes: 6 additions & 2 deletions ddl/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ func testNewDDL(ctx context.Context, etcdCli *clientv3.Client, store kv.Storage,
func getSchemaVer(c *C, ctx sessionctx.Context) int64 {
err := ctx.NewTxn()
c.Assert(err, IsNil)
m := meta.NewMeta(ctx.Txn(true))
txn, err := ctx.Txn(true)
c.Assert(err, IsNil)
m := meta.NewMeta(txn)
ver, err := m.GetSchemaVersion()
c.Assert(err, IsNil)
return ver
Expand Down Expand Up @@ -91,7 +93,9 @@ func checkHistoryJob(c *C, job *model.Job) {
}

func checkHistoryJobArgs(c *C, ctx sessionctx.Context, id int64, args *historyJobArgs) {
t := meta.NewMeta(ctx.Txn(true))
txn, err := ctx.Txn(true)
c.Assert(err, IsNil)
t := meta.NewMeta(txn)
historyJob, err := t.GetHistoryDDLJob(id)
c.Assert(err, IsNil)
c.Assert(historyJob.BinlogInfo.FinishedTS, Greater, uint64(0))
Expand Down
18 changes: 13 additions & 5 deletions ddl/ddl_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,9 @@ func (s *testDDLSuite) TestCancelJob(c *C) {
row := types.MakeDatums(1, 2)
_, err = originTable.AddRecord(ctx, row, false)
c.Assert(err, IsNil)
err = ctx.Txn(true).Commit(context.Background())
txn, err := ctx.Txn(true)
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)

tc := &TestDDLCallback{}
Expand All @@ -561,12 +563,16 @@ func (s *testDDLSuite) TestCancelJob(c *C) {
checkErr = errors.Trace(err1)
return
}
checkErr = checkCancelState(hookCtx.Txn(true), job, test)
txn, err1 = hookCtx.Txn(true)
if err1 != nil {
checkErr = errors.Trace(err1)
return
}
checkErr = checkCancelState(txn, job, test)
if checkErr != nil {
return
}
checkCancelState(hookCtx.Txn(true), job, test)
err1 = hookCtx.Txn(true).Commit(context.Background())
err1 = txn.Commit(context.Background())
if err1 != nil {
checkErr = errors.Trace(err1)
return
Expand Down Expand Up @@ -598,7 +604,9 @@ func (s *testDDLSuite) TestCancelJob(c *C) {
test = &tests[3]
testCreateIndex(c, ctx, d, dbInfo, tblInfo, false, "idx", "c2")
c.Check(errors.ErrorStack(checkErr), Equals, "")
c.Assert(ctx.Txn(true).Commit(context.Background()), IsNil)
txn, err = ctx.Txn(true)
c.Assert(err, IsNil)
c.Assert(txn.Commit(context.Background()), IsNil)
s.checkAddIdx(c, d, dbInfo.ID, tblInfo.ID, idxOrigName, true)

// for dropping index
Expand Down
4 changes: 3 additions & 1 deletion ddl/fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func (s *testColumnChangeSuite) TestFailBeforeDecodeArgs(c *C) {
row := types.MakeDatums(1, 2)
_, err = originTable.AddRecord(ctx, row, false)
c.Assert(err, IsNil)
err = ctx.Txn(true).Commit(context.Background())
txn, err := ctx.Txn(true)
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)

tc := &TestDDLCallback{}
Expand Down
12 changes: 9 additions & 3 deletions ddl/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ func (s *testForeighKeySuite) TestForeignKey(c *C) {

testCreateTable(c, ctx, d, s.dbInfo, tblInfo)

err = ctx.Txn(true).Commit(context.Background())
txn, err := ctx.Txn(true)
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)

// fix data race
Expand Down Expand Up @@ -160,7 +162,9 @@ func (s *testForeighKeySuite) TestForeignKey(c *C) {

job := s.testCreateForeignKey(c, tblInfo, "c1_fk", []string{"c1"}, "t2", []string{"c1"}, ast.ReferOptionCascade, ast.ReferOptionSetNull)
testCheckJobDone(c, d, job, true)
err = ctx.Txn(true).Commit(context.Background())
txn, err = ctx.Txn(true)
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
mu.Lock()
hErr := hookErr
Expand Down Expand Up @@ -218,6 +222,8 @@ func (s *testForeighKeySuite) TestForeignKey(c *C) {
job = testDropTable(c, ctx, d, s.dbInfo, tblInfo)
testCheckJobDone(c, d, job, false)

err = ctx.Txn(true).Commit(context.Background())
txn, err = ctx.Txn(true)
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
}
33 changes: 26 additions & 7 deletions ddl/index_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func (s *testIndexChangeSuite) TestIndexChange(c *C) {
_, err = originTable.AddRecord(ctx, types.MakeDatums(3, 3), false)
c.Assert(err, IsNil)

err = ctx.Txn(true).Commit(context.Background())
txn, err := ctx.Txn(true)
c.Assert(err, IsNil)
txn.Commit(context.Background())
c.Assert(err, IsNil)

tc := &TestDDLCallback{}
Expand All @@ -92,7 +94,6 @@ func (s *testIndexChangeSuite) TestIndexChange(c *C) {
}
ctx1 := testNewContext(d)
prevState = job.SchemaState
var err error
switch job.SchemaState {
case model.StateDeleteOnly:
deleteOnlyTable, err = getCurrentTable(d, s.dbInfo.ID, tblInfo.ID)
Expand Down Expand Up @@ -125,7 +126,9 @@ func (s *testIndexChangeSuite) TestIndexChange(c *C) {
d.SetHook(tc)
testCreateIndex(c, ctx, d, s.dbInfo, originTable.Meta(), false, "c2", "c2")
c.Check(errors.ErrorStack(checkErr), Equals, "")
c.Assert(ctx.Txn(true).Commit(context.Background()), IsNil)
txn, err = ctx.Txn(true)
c.Assert(err, IsNil)
c.Assert(txn.Commit(context.Background()), IsNil)
d.Stop()
prevState = model.StateNone
var noneTable table.Table
Expand Down Expand Up @@ -172,7 +175,11 @@ func (s *testIndexChangeSuite) TestIndexChange(c *C) {

func checkIndexExists(ctx sessionctx.Context, tbl table.Table, indexValue interface{}, handle int64, exists bool) error {
idx := tbl.Indices()[0]
doesExist, _, err := idx.Exist(ctx.GetSessionVars().StmtCtx, ctx.Txn(true), types.MakeDatums(indexValue), handle)
txn, err := ctx.Txn(true)
if err != nil {
return errors.Trace(err)
}
doesExist, _, err := idx.Exist(ctx.GetSessionVars().StmtCtx, txn, types.MakeDatums(indexValue), handle)
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -322,7 +329,11 @@ func (s *testIndexChangeSuite) checkAddPublic(d *ddl, ctx sessionctx.Context, wr
return errors.Trace(err)
}
}
return ctx.Txn(true).Commit(context.Background())
txn, err := ctx.Txn(true)
if err != nil {
return errors.Trace(err)
}
return txn.Commit(context.Background())
}

func (s *testIndexChangeSuite) checkDropWriteOnly(d *ddl, ctx sessionctx.Context, publicTbl, writeTbl table.Table) error {
Expand Down Expand Up @@ -362,7 +373,11 @@ func (s *testIndexChangeSuite) checkDropWriteOnly(d *ddl, ctx sessionctx.Context
if err != nil {
return errors.Trace(err)
}
return ctx.Txn(true).Commit(context.Background())
txn, err := ctx.Txn(true)
if err != nil {
return errors.Trace(err)
}
return txn.Commit(context.Background())
}

func (s *testIndexChangeSuite) checkDropDeleteOnly(d *ddl, ctx sessionctx.Context, writeTbl, delTbl table.Table) error {
Expand Down Expand Up @@ -407,5 +422,9 @@ func (s *testIndexChangeSuite) checkDropDeleteOnly(d *ddl, ctx sessionctx.Contex
if err != nil {
return errors.Trace(err)
}
return ctx.Txn(true).Commit(context.Background())
txn, err := ctx.Txn(true)
if err != nil {
return errors.Trace(err)
}
return txn.Commit(context.Background())
}
Loading

0 comments on commit 305a083

Please sign in to comment.