Skip to content

Commit

Permalink
*: change Context method FinishTxn to CommitTxn and RollbackTxn. (pin…
Browse files Browse the repository at this point in the history
…gcap#1253)

Makes it easier to read.
  • Loading branch information
coocood committed May 24, 2016
1 parent 622f8fc commit d3cb905
Show file tree
Hide file tree
Showing 19 changed files with 88 additions and 59 deletions.
2 changes: 1 addition & 1 deletion bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func checkBootstrappedVar(s Session) (bool, error) {
if isBootstrapped {
// Make sure that doesn't affect the following operations.

if err = s.FinishTxn(false); err != nil {
if err = s.CommitTxn(); err != nil {
return false, errors.Trace(err)
}
}
Expand Down
7 changes: 5 additions & 2 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ type Context interface {
// GetTxn gets a transaction for further execution.
GetTxn(forceNew bool) (kv.Transaction, error)

// FinishTxn commits or rolls back the current transaction.
FinishTxn(rollback bool) error
// RollbackTxn rolls back the current transaction.
RollbackTxn() error

// CommitTxn commits the current transaction.
CommitTxn() error

// SetValue saves a value associated with this context for key.
SetValue(key fmt.Stringer, value interface{})
Expand Down
21 changes: 11 additions & 10 deletions ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ func testDropColumn(c *C, ctx context.Context, d *ddl, dbInfo *model.DBInfo, tbl
func (s *testColumnSuite) TestColumn(c *C) {
defer testleak.AfterTest(c)()
tblInfo := testTableInfo(c, s.d, "t1", 3)
ctx := testNewContext(c, s.d)
defer ctx.FinishTxn(true)
var ctx context.Context
ctx = testNewContext(c, s.d)
defer ctx.RollbackTxn()

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

Expand All @@ -119,7 +120,7 @@ func (s *testColumnSuite) TestColumn(c *C) {
c.Assert(err, IsNil)
}

err := ctx.FinishTxn(false)
err := ctx.CommitTxn()
c.Assert(err, IsNil)

i := int64(0)
Expand Down Expand Up @@ -155,7 +156,7 @@ func (s *testColumnSuite) TestColumn(c *C) {

h, err := t.AddRecord(ctx, types.MakeDatums(11, 12, 13, 14))
c.Assert(err, IsNil)
err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)
values, err := t.RowWithCols(ctx, h, t.Cols())
c.Assert(err, IsNil)
Expand Down Expand Up @@ -274,7 +275,7 @@ func (s *testColumnSuite) checkColumnKVExist(c *C, ctx context.Context, t table.
c.Assert(err, NotNil)
}

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)
}

Expand Down Expand Up @@ -519,7 +520,7 @@ func (s *testColumnSuite) checkPublicColumn(c *C, ctx context.Context, d *ddl, t
})
c.Assert(i, Equals, int64(1))

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)
s.testGetColumn(c, t, col.Name.L, true)
}
Expand Down Expand Up @@ -567,7 +568,7 @@ func (s *testColumnSuite) TestAddColumn(c *C) {
handle, err := t.AddRecord(ctx, row)
c.Assert(err, IsNil)

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)

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

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)

d.close()
Expand All @@ -636,7 +637,7 @@ func (s *testColumnSuite) TestDropColumn(c *C) {
handle, err := t.AddRecord(ctx, append(row, types.NewDatum(defaultColValue)))
c.Assert(err, IsNil)

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)

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

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)

d.close()
Expand Down
8 changes: 4 additions & 4 deletions ddl/ddl_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ LOOP:
idx := tables.NewIndex(t.IndexPrefix(), "c3_index", nidx.ID, false)
txn, err := ctx.GetTxn(true)
c.Assert(err, IsNil)
defer ctx.FinishTxn(true)
defer ctx.RollbackTxn()

it, err := idx.SeekFirst(txn)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -285,7 +285,7 @@ LOOP:
idx := tables.NewIndex(t.IndexPrefix(), "c3_index", c3idx.ID, false)
txn, err := ctx.GetTxn(true)
c.Assert(err, IsNil)
defer ctx.FinishTxn(true)
defer ctx.RollbackTxn()

it, err := idx.SeekFirst(txn)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -380,7 +380,7 @@ LOOP:
t := s.testGetTable(c, "t2")
i := 0
j := 0
defer ctx.FinishTxn(true)
defer ctx.RollbackTxn()
err := t.IterRecords(ctx, t.FirstKey(), t.Cols(), func(h int64, data []types.Datum, cols []*table.Column) (bool, error) {
i++
// c4 must be -1 or > 0
Expand Down Expand Up @@ -457,7 +457,7 @@ LOOP:

txn, err := ctx.GetTxn(true)
c.Assert(err, IsNil)
defer ctx.FinishTxn(false)
defer ctx.CommitTxn()

i := 0
t = s.testGetTable(c, "t2")
Expand Down
2 changes: 1 addition & 1 deletion ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

func (d *ddl) doDDLJob(ctx context.Context, job *model.Job) error {
// for every DDL, we must commit current transaction.
if err := ctx.FinishTxn(false); err != nil {
if err := ctx.CommitTxn(); err != nil {
return errors.Trace(err)
}

Expand Down
6 changes: 3 additions & 3 deletions ddl/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (s *testForeighKeySuite) TestForeignKey(c *C) {

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

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)

checkOK := false
Expand All @@ -158,7 +158,7 @@ func (s *testForeighKeySuite) TestForeignKey(c *C) {

testCheckJobDone(c, d, job, true)

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)
c.Assert(checkOK, IsTrue)

Expand Down Expand Up @@ -191,7 +191,7 @@ func (s *testForeighKeySuite) TestForeignKey(c *C) {
job = testDropTable(c, ctx, d, s.dbInfo, tblInfo)
testCheckJobDone(c, d, job, false)

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)

d.close()
Expand Down
20 changes: 10 additions & 10 deletions ddl/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (s *testIndexSuite) TestIndex(c *C) {
defer testleak.AfterTest(c)()
tblInfo := testTableInfo(c, s.d, "t1", 3)
ctx := testNewContext(c, s.d)
defer ctx.FinishTxn(true)
defer ctx.RollbackTxn()

txn, err := ctx.GetTxn(true)
c.Assert(err, IsNil)
Expand All @@ -106,7 +106,7 @@ func (s *testIndexSuite) TestIndex(c *C) {
c.Assert(err, IsNil)
}

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)

i := int64(0)
Expand Down Expand Up @@ -188,7 +188,7 @@ func (s *testIndexSuite) checkIndexKVExist(c *C, ctx context.Context, t table.Ta
c.Assert(err, IsNil)
c.Assert(exist, Equals, isExist)

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)
}

Expand Down Expand Up @@ -579,7 +579,7 @@ func (s *testIndexSuite) TestAddIndex(c *C) {
handle, err := t.AddRecord(ctx, row)
c.Assert(err, IsNil)

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)

checkOK := false
Expand Down Expand Up @@ -623,7 +623,7 @@ func (s *testIndexSuite) TestAddIndex(c *C) {
job = testDropTable(c, ctx, d, s.dbInfo, tblInfo)
testCheckJobDone(c, d, job, false)

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)

d.close()
Expand All @@ -647,13 +647,13 @@ func (s *testIndexSuite) TestDropIndex(c *C) {
handle, err := t.AddRecord(ctx, row)
c.Assert(err, IsNil)

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)

job := testCreateIndex(c, ctx, s.d, s.dbInfo, tblInfo, true, "c1_uni", "c1")
testCheckJobDone(c, d, job, true)

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)

checkOK := false
Expand Down Expand Up @@ -694,7 +694,7 @@ func (s *testIndexSuite) TestDropIndex(c *C) {
job = testDropTable(c, ctx, d, s.dbInfo, tblInfo)
testCheckJobDone(c, d, job, false)

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)

d.close()
Expand All @@ -721,7 +721,7 @@ func (s *testIndexSuite) TestAddIndexWithNullColumn(c *C) {
handle, err := t.AddRecord(ctx, row)
c.Assert(err, IsNil)

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)

checkOK := false
Expand Down Expand Up @@ -760,7 +760,7 @@ func (s *testIndexSuite) TestAddIndexWithNullColumn(c *C) {
job = testDropTable(c, ctx, d, s.dbInfo, tblInfo)
testCheckJobDone(c, d, job, false)

err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)

d.close()
Expand Down
10 changes: 9 additions & 1 deletion ddl/reorg.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c *reorgContext) GetTxn(forceNew bool) (kv.Transaction, error) {
return c.txn, nil
}

func (c *reorgContext) FinishTxn(rollback bool) error {
func (c *reorgContext) finishTxn(rollback bool) error {
if c.txn == nil {
return nil
}
Expand All @@ -74,6 +74,14 @@ func (c *reorgContext) FinishTxn(rollback bool) error {
return errors.Trace(err)
}

func (c *reorgContext) RollbackTxn() error {
return c.finishTxn(true)
}

func (c *reorgContext) CommitTxn() error {
return c.finishTxn(false)
}

func (c *reorgContext) SetValue(key fmt.Stringer, value interface{}) {
c.m[key] = value
}
Expand Down
6 changes: 3 additions & 3 deletions ddl/reorg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ func (s *testDDLSuite) TestReorg(c *C) {
txn, err := ctx.GetTxn(true)
c.Assert(err, IsNil)
txn.Set([]byte("a"), []byte("b"))
err = ctx.FinishTxn(true)
err = ctx.RollbackTxn()
c.Assert(err, IsNil)

txn, err = ctx.GetTxn(false)
c.Assert(err, IsNil)
txn.Set([]byte("a"), []byte("b"))
err = ctx.FinishTxn(false)
err = ctx.CommitTxn()
c.Assert(err, IsNil)

done := make(chan struct{})
Expand Down Expand Up @@ -144,7 +144,7 @@ func (s *testDDLSuite) TestReorgOwner(c *C) {
c.Assert(err, IsNil)
}

err := ctx.FinishTxn(false)
err := ctx.CommitTxn()
c.Assert(err, IsNil)

tc := &testDDLCallback{}
Expand Down
2 changes: 1 addition & 1 deletion ddl/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (s *testTableSuite) TestTable(c *C) {
d := s.d

ctx := testNewContext(c, d)
defer ctx.FinishTxn(true)
defer ctx.RollbackTxn()

tblInfo := testTableInfo(c, d, "t", 3)

Expand Down
2 changes: 1 addition & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (c *driverConn) Commit() error {
return errors.Trace(err)
}

err = c.s.FinishTxn(false)
err = c.s.CommitTxn()
return errors.Trace(err)
}

Expand Down
4 changes: 2 additions & 2 deletions executor/executor_simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,13 @@ func (e *SimpleExec) executeBegin(s *ast.BeginStmt) error {
}

func (e *SimpleExec) executeCommit(s *ast.CommitStmt) error {
err := e.ctx.FinishTxn(false)
err := e.ctx.CommitTxn()
variable.GetSessionVars(e.ctx).SetStatusFlag(mysql.ServerStatusInTrans, false)
return errors.Trace(err)
}

func (e *SimpleExec) executeRollback(s *ast.RollbackStmt) error {
err := e.ctx.FinishTxn(true)
err := e.ctx.RollbackTxn()
variable.GetSessionVars(e.ctx).SetStatusFlag(mysql.ServerStatusInTrans, false)
return errors.Trace(err)
}
Expand Down
4 changes: 2 additions & 2 deletions inspectkv/inspectkv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (s *testSuite) TestScan(c *C) {
indices := tb.Indices()
_, err = tb.AddRecord(s.ctx, types.MakeDatums(10, 11))
c.Assert(err, IsNil)
s.ctx.FinishTxn(false)
s.ctx.CommitTxn()

record1 := &RecordData{Handle: int64(1), Values: types.MakeDatums(int64(10), int64(11))}
record2 := &RecordData{Handle: int64(2), Values: types.MakeDatums(int64(20), int64(21))}
Expand All @@ -197,7 +197,7 @@ func (s *testSuite) TestScan(c *C) {

_, err = tb.AddRecord(s.ctx, record2.Values)
c.Assert(err, IsNil)
s.ctx.FinishTxn(false)
s.ctx.CommitTxn()
txn, err := s.store.Begin()
c.Assert(err, IsNil)

Expand Down
Loading

0 comments on commit d3cb905

Please sign in to comment.