Skip to content

Commit

Permalink
*: refactor context.Context (pingcap#2298)
Browse files Browse the repository at this point in the history
Make transaction life cycle under control, prepare for correctly handle schema out of date error.
The old `GetTxn(forceNew boll)` is replaced by `Txn` and `NewTxn`,
`CommitTxn` and `RollbackTxn` is removed.
  • Loading branch information
coocood authored Dec 23, 2016
1 parent 2acbf24 commit 64484a4
Show file tree
Hide file tree
Showing 33 changed files with 359 additions and 440 deletions.
15 changes: 7 additions & 8 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@ import (

// Context is an interface for transaction and executive args environment.
type Context interface {
// GetTxn gets a transaction for further execution.
GetTxn(forceNew bool) (kv.Transaction, error)
// NewTxn creates a new transaction for further execution.
// If old transaction is valid, it is committed first.
// It's used in BEGIN statement and DDL statements to commit old transaction.
NewTxn() error

// Txn returns the current transaction which is created before executing a statement.
Txn() kv.Transaction

// GetClient gets a kv.Client.
GetClient() kv.Client

// 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
40 changes: 32 additions & 8 deletions ddl/column_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func (s *testColumnChangeSuite) TestColumnChange(c *C) {
// create table t (c1 int, c2 int);
tblInfo := testTableInfo(c, d, "t", 2)
ctx := testNewContext(c, d)
_, err := ctx.GetTxn(true)
err := ctx.NewTxn()
c.Assert(err, IsNil)
testCreateTable(c, ctx, d, s.dbInfo, tblInfo)
// insert t values (1, 2);
originTable := testGetTable(c, d, s.dbInfo.ID, tblInfo.ID)
row := types.MakeDatums(1, 2)
_, err = originTable.AddRecord(ctx, row)
c.Assert(err, IsNil)
err = ctx.CommitTxn()
err = ctx.Txn().Commit()
c.Assert(err, IsNil)

var mu sync.Mutex
Expand All @@ -87,6 +87,10 @@ func (s *testColumnChangeSuite) TestColumnChange(c *C) {
hookCtx.Store = s.store
prevState = job.SchemaState
var err error
err = hookCtx.NewTxn()
if err != nil {
checkErr = errors.Trace(err)
}
switch job.SchemaState {
case model.StateDeleteOnly:
deleteOnlyTable, err = getCurrentTable(d, s.dbInfo.ID, tblInfo.ID)
Expand Down Expand Up @@ -114,6 +118,10 @@ func (s *testColumnChangeSuite) TestColumnChange(c *C) {
}
mu.Unlock()
}
err = hookCtx.Txn().Commit()
if err != nil {
checkErr = errors.Trace(err)
}
}
d.setHook(tc)
defaultValue := int64(3)
Expand Down Expand Up @@ -142,6 +150,10 @@ func (s *testColumnChangeSuite) testAddColumnNoDefault(c *C, ctx context.Context
hookCtx.Store = s.store
prevState = job.SchemaState
var err error
err = hookCtx.NewTxn()
if err != nil {
checkErr = errors.Trace(err)
}
switch job.SchemaState {
case model.StateWriteOnly:
writeOnlyTable, err = getCurrentTable(d, s.dbInfo.ID, tblInfo.ID)
Expand All @@ -158,6 +170,10 @@ func (s *testColumnChangeSuite) testAddColumnNoDefault(c *C, ctx context.Context
checkErr = errors.Trace(err)
}
}
err = hookCtx.Txn().Commit()
if err != nil {
checkErr = errors.Trace(err)
}
}
d.setHook(tc)
d.start()
Expand Down Expand Up @@ -196,11 +212,15 @@ func (s *testColumnChangeSuite) testColumnDrop(c *C, ctx context.Context, d *ddl

func (s *testColumnChangeSuite) checkAddWriteOnly(d *ddl, ctx context.Context, deleteOnlyTable, writeOnlyTable table.Table) error {
// WriteOnlyTable: insert t values (2, 3)
_, err := writeOnlyTable.AddRecord(ctx, types.MakeDatums(2, 3))
err := ctx.NewTxn()
if err != nil {
return errors.Trace(err)
}
err = ctx.CommitTxn()
_, err = writeOnlyTable.AddRecord(ctx, types.MakeDatums(2, 3))
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
if err != nil {
return errors.Trace(err)
}
Expand All @@ -222,7 +242,7 @@ func (s *testColumnChangeSuite) checkAddWriteOnly(d *ddl, ctx context.Context, d
if err != nil {
return errors.Trace(err)
}
err = ctx.CommitTxn()
err = ctx.NewTxn()
if err != nil {
return errors.Trace(err)
}
Expand All @@ -236,7 +256,7 @@ func (s *testColumnChangeSuite) checkAddWriteOnly(d *ddl, ctx context.Context, d
if err != nil {
return errors.Trace(err)
}
err = ctx.CommitTxn()
err = ctx.NewTxn()
if err != nil {
return errors.Trace(err)
}
Expand All @@ -255,11 +275,15 @@ func touchedMap(t table.Table) map[int]bool {

func (s *testColumnChangeSuite) checkAddPublic(d *ddl, ctx context.Context, writeOnlyTable, publicTable table.Table) error {
// publicTable Insert t values (4, 4, 4)
err := ctx.NewTxn()
if err != nil {
return errors.Trace(err)
}
h, err := publicTable.AddRecord(ctx, types.MakeDatums(4, 4, 4))
if err != nil {
return errors.Trace(err)
}
err = ctx.CommitTxn()
err = ctx.NewTxn()
if err != nil {
return errors.Trace(err)
}
Expand All @@ -276,7 +300,7 @@ func (s *testColumnChangeSuite) checkAddPublic(d *ddl, ctx context.Context, writ
if err != nil {
return errors.Trace(err)
}
err = ctx.CommitTxn()
err = ctx.NewTxn()
if err != nil {
return errors.Trace(err)
}
Expand Down
Loading

0 comments on commit 64484a4

Please sign in to comment.