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

*: remove goCtx from session struct #5174

Merged
merged 8 commits into from
Nov 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
goctx "golang.org/x/net/context"
)

// Node is the basic element of the AST.
Expand Down Expand Up @@ -129,7 +130,6 @@ type ResultField struct {

// RecordSet is an abstract result set interface to help get data from Plan.
type RecordSet interface {

// Fields gets result fields.
Fields() []*ResultField

Expand Down Expand Up @@ -182,7 +182,7 @@ type Statement interface {
OriginText() string

// Exec executes SQL and gets a Recordset.
Exec(ctx context.Context) (RecordSet, error)
Exec(goCtx goctx.Context, ctx context.Context) (RecordSet, error)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems we don't need the second context.Context.
The Statement has one in its struct.


// IsPrepared returns whether this statement is prepared statement.
IsPrepared() bool
Expand Down
5 changes: 1 addition & 4 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ type Context interface {
// Txn returns the current transaction which is created before executing a statement.
Txn() kv.Transaction

// GoCtx returns the standard context.Context which is bound with current transaction.
GoCtx() goctx.Context

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

Expand All @@ -55,7 +52,7 @@ type Context interface {
// RefreshTxnCtx commits old transaction without retry,
// and creates a new transaction.
// now just for load data and batch insert.
RefreshTxnCtx() error
RefreshTxnCtx(goctx.Context) error

// ActivePendingTxn receives the pending transaction from the transaction channel.
// It should be called right before we builds an executor.
Expand Down
2 changes: 1 addition & 1 deletion ddl/column_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (s *testColumnChangeSuite) testAddColumnNoDefault(c *C, ctx context.Context
checkErr = errors.Trace(err)
}
}
err = hookCtx.Txn().Commit(ctx.GoCtx())
err = hookCtx.Txn().Commit(goctx.TODO())
if err != nil {
checkErr = errors.Trace(err)
}
Expand Down
9 changes: 5 additions & 4 deletions ddl/ddl_db_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,12 @@ func (t *testExecInfo) compileSQL(idx int) (err error) {
for _, info := range t.sqlInfos {
c := info.cases[idx]
se := c.session
se.PrepareTxnCtx(se.GoCtx())
goCtx := goctx.TODO()
se.PrepareTxnCtx(goCtx)
ctx := se.(context.Context)
executor.ResetStmtCtx(ctx, c.rawStmt)

c.stmt, err = compiler.Compile(ctx, c.rawStmt)
c.stmt, err = compiler.Compile(goCtx, ctx, c.rawStmt)
if err != nil {
return errors.Trace(err)
}
Expand All @@ -264,7 +265,7 @@ func (t *testExecInfo) execSQL(idx int) error {
for _, sqlInfo := range t.sqlInfos {
c := sqlInfo.cases[idx]
ctx := c.session.(context.Context)
_, err := c.stmt.Exec(ctx)
_, err := c.stmt.Exec(goctx.TODO(), ctx)
if c.expectedErr != nil {
if err == nil {
err = errors.Errorf("expected error %s but got nil", c.expectedErr)
Expand All @@ -275,7 +276,7 @@ func (t *testExecInfo) execSQL(idx int) error {
if err != nil {
return errors.Trace(err)
}
err = c.session.CommitTxn(c.session.GoCtx())
err = c.session.CommitTxn(goctx.TODO())
if err != nil {
return errors.Trace(err)
}
Expand Down
18 changes: 3 additions & 15 deletions ddl/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ func (t DelRangeTask) Range() ([]byte, []byte) {
// LoadDeleteRanges loads delete range tasks from gc_delete_range table.
func LoadDeleteRanges(ctx context.Context, safePoint uint64) (ranges []DelRangeTask, _ error) {
sql := fmt.Sprintf(loadDeleteRangeSQL, safePoint)
goCtx := ctx.GoCtx()
if goCtx == nil {
goCtx = goctx.Background()
}
rss, err := ctx.(sqlexec.SQLExecutor).Execute(goCtx, sql)
rss, err := ctx.(sqlexec.SQLExecutor).Execute(goctx.TODO(), sql)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down Expand Up @@ -84,11 +80,7 @@ func LoadDeleteRanges(ctx context.Context, safePoint uint64) (ranges []DelRangeT
// NOTE: This function WILL NOT start and run in a new transaction internally.
func CompleteDeleteRange(ctx context.Context, dr DelRangeTask) error {
sql := fmt.Sprintf(completeDeleteRangeSQL, dr.JobID, dr.ElementID)
goCtx := ctx.GoCtx()
if goCtx == nil {
goCtx = goctx.Background()
}
_, err := ctx.(sqlexec.SQLExecutor).Execute(goCtx, sql)
_, err := ctx.(sqlexec.SQLExecutor).Execute(goctx.TODO(), sql)
return errors.Trace(err)
}

Expand All @@ -97,10 +89,6 @@ func UpdateDeleteRange(ctx context.Context, dr DelRangeTask, newStartKey, oldSta
newStartKeyHex := hex.EncodeToString(newStartKey)
oldStartKeyHex := hex.EncodeToString(oldStartKey)
sql := fmt.Sprintf(updateDeleteRangeSQL, newStartKeyHex, dr.JobID, dr.ElementID, oldStartKeyHex)
goCtx := ctx.GoCtx()
if goCtx == nil {
goCtx = goctx.Background()
}
_, err := ctx.(sqlexec.SQLExecutor).Execute(goCtx, sql)
_, err := ctx.(sqlexec.SQLExecutor).Execute(goctx.TODO(), sql)
return errors.Trace(err)
}
5 changes: 3 additions & 2 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/logutil"
goctx "golang.org/x/net/context"
)

type processinfoSetter interface {
Expand Down Expand Up @@ -167,7 +168,7 @@ func (a *ExecStmt) IsReadOnly() bool {
// This function builds an Executor from a plan. If the Executor doesn't return result,
// like the INSERT, UPDATE statements, it executes in this function, if the Executor returns
// result, execution is done after this function returns, in the returned ast.RecordSet Next method.
func (a *ExecStmt) Exec(ctx context.Context) (ast.RecordSet, error) {
func (a *ExecStmt) Exec(goCtx goctx.Context, ctx context.Context) (ast.RecordSet, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second ctx can be removed.
We can set ctx of ExecStmt at creation time.

a.startTime = time.Now()
a.ctx = ctx

Expand All @@ -193,7 +194,7 @@ func (a *ExecStmt) Exec(ctx context.Context) (ast.RecordSet, error) {
return nil, errors.Trace(err)
}

if err := e.Open(); err != nil {
if err := e.Open(goCtx); err != nil {
return nil, errors.Trace(err)
}

Expand Down
9 changes: 5 additions & 4 deletions executor/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/mvmap"
goctx "golang.org/x/net/context"
)

type aggCtxsMapper map[string][]*aggregation.AggEvaluateContext
Expand Down Expand Up @@ -52,12 +53,12 @@ func (e *HashAggExec) Close() error {
}

// Open implements the Executor Open interface.
func (e *HashAggExec) Open() error {
func (e *HashAggExec) Open(goCtx goctx.Context) error {
e.executed = false
e.groupMap = mvmap.NewMVMap()
e.groupIterator = e.groupMap.NewIterator()
e.aggCtxsMap = make(aggCtxsMapper, 0)
return errors.Trace(e.children[0].Open())
return errors.Trace(e.children[0].Open(goCtx))
}

// Next implements the Executor Next interface.
Expand Down Expand Up @@ -172,11 +173,11 @@ type StreamAggExec struct {
}

// Open implements the Executor Open interface.
func (e *StreamAggExec) Open() error {
func (e *StreamAggExec) Open(goCtx goctx.Context) error {
e.executed = false
e.hasData = false
e.aggCtxs = make([]*aggregation.AggEvaluateContext, 0, len(e.AggFuncs))
return errors.Trace(e.children[0].Open())
return errors.Trace(e.children[0].Open(goCtx))
}

// Next implements the Executor Next interface.
Expand Down
13 changes: 8 additions & 5 deletions executor/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tipb/go-tipb"
goctx "golang.org/x/net/context"
)

var _ Executor = &AnalyzeExec{}
Expand All @@ -51,7 +52,7 @@ const (
)

// Open implements the Executor Open interface.
func (e *AnalyzeExec) Open() error {
func (e *AnalyzeExec) Open(goctx.Context) error {
return nil
}

Expand Down Expand Up @@ -193,11 +194,12 @@ func (e *AnalyzeIndexExec) open() error {
Build()
kvReq.Concurrency = e.concurrency
kvReq.IsolationLevel = kv.RC
e.result, err = distsql.Analyze(e.ctx.GoCtx(), e.ctx.GetClient(), kvReq)
goCtx := goctx.TODO()
e.result, err = distsql.Analyze(goCtx, e.ctx.GetClient(), kvReq)
if err != nil {
return errors.Trace(err)
}
e.result.Fetch(e.ctx.GoCtx())
e.result.Fetch(goCtx)
return nil
}

Expand Down Expand Up @@ -286,11 +288,12 @@ func (e *AnalyzeColumnsExec) open() error {
if err != nil {
return errors.Trace(err)
}
e.result, err = distsql.Analyze(e.ctx.GoCtx(), e.ctx.GetClient(), kvReq)
goCtx := goctx.TODO()
e.result, err = distsql.Analyze(goCtx, e.ctx.GetClient(), kvReq)
if err != nil {
return errors.Trace(err)
}
e.result.Fetch(e.ctx.GoCtx())
e.result.Fetch(goCtx)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ func (builder *dataReaderBuilder) buildIndexReaderForDatums(goCtx goctx.Context,
if err != nil {
return nil, errors.Trace(err)
}
e.result, err = distsql.SelectDAG(e.ctx.GoCtx(), e.ctx.GetClient(), kvReq, e.schema.GetTypes(), builder.ctx.GetSessionVars().GetTimeZone())
e.result, err = distsql.SelectDAG(goCtx, e.ctx.GetClient(), kvReq, e.schema.GetTypes(), builder.ctx.GetSessionVars().GetTimeZone())
if err != nil {
return nil, errors.Trace(err)
}
Expand All @@ -1226,6 +1226,6 @@ func (builder *dataReaderBuilder) buildIndexLookUpReaderForDatums(goCtx goctx.Co
if err != nil {
return nil, errors.Trace(err)
}
err = e.open(kvRanges)
err = e.open(goCtx, kvRanges)
return e, errors.Trace(err)
}
11 changes: 5 additions & 6 deletions executor/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@ import (
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/plan"
goctx "golang.org/x/net/context"
)

// Compiler compiles an ast.StmtNode to a physical plan.
type Compiler struct {
}

// Compile compiles an ast.StmtNode to a physical plan.
func (c *Compiler) Compile(ctx context.Context, stmtNode ast.StmtNode) (*ExecStmt, error) {
if ctx.GoCtx() != nil {
if span := opentracing.SpanFromContext(ctx.GoCtx()); span != nil {
span1 := opentracing.StartSpan("executor.Compile", opentracing.ChildOf(span.Context()))
defer span1.Finish()
}
func (c *Compiler) Compile(goCtx goctx.Context, ctx context.Context, stmtNode ast.StmtNode) (*ExecStmt, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove the second ctx, add it to the field of Compiler.

if span := opentracing.SpanFromContext(goCtx); span != nil {
span1 := opentracing.StartSpan("executor.Compile", opentracing.ChildOf(span.Context()))
defer span1.Finish()
}

infoSchema := GetInfoSchema(ctx)
Expand Down
3 changes: 2 additions & 1 deletion executor/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/sessionctx/varsutil"
"github.com/pingcap/tidb/types"
goctx "golang.org/x/net/context"
)

// DDLExec represents a DDL executor.
Expand Down Expand Up @@ -85,7 +86,7 @@ func (e *DDLExec) Close() error {
}

// Open implements the Executor Open interface.
func (e *DDLExec) Open() error {
func (e *DDLExec) Open(goCtx goctx.Context) error {
return nil
}

Expand Down
16 changes: 8 additions & 8 deletions executor/distsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ func (e *TableReaderExecutor) NextChunk(chk *chunk.Chunk) error {
}

// Open implements the Executor Open interface.
func (e *TableReaderExecutor) Open() error {
span, goCtx := startSpanFollowsContext(e.ctx.GoCtx(), "executor.TableReader.Open")
func (e *TableReaderExecutor) Open(goCtx goctx.Context) error {
span, goCtx := startSpanFollowsContext(goCtx, "executor.TableReader.Open")
defer span.Finish()

var builder requestBuilder
Expand Down Expand Up @@ -507,8 +507,8 @@ func (e *IndexReaderExecutor) NextChunk(chk *chunk.Chunk) error {
}

// Open implements the Executor Open interface.
func (e *IndexReaderExecutor) Open() error {
span, goCtx := startSpanFollowsContext(e.ctx.GoCtx(), "executor.IndexReader.Open")
func (e *IndexReaderExecutor) Open(goCtx goctx.Context) error {
span, goCtx := startSpanFollowsContext(goCtx, "executor.IndexReader.Open")
defer span.Finish()

var builder requestBuilder
Expand Down Expand Up @@ -683,16 +683,16 @@ func (worker *tableWorker) close() {
}

// Open implements the Executor Open interface.
func (e *IndexLookUpExecutor) Open() error {
func (e *IndexLookUpExecutor) Open(goCtx goctx.Context) error {
kvRanges, err := e.indexRangesToKVRanges()
if err != nil {
return errors.Trace(err)
}
return e.open(kvRanges)
return e.open(goCtx, kvRanges)
}

func (e *IndexLookUpExecutor) open(kvRanges []kv.KeyRange) error {
span, goCtx := startSpanFollowsContext(e.ctx.GoCtx(), "executor.IndexLookUp.Open")
func (e *IndexLookUpExecutor) open(goCtx goctx.Context, kvRanges []kv.KeyRange) error {
span, goCtx := startSpanFollowsContext(goCtx, "executor.IndexLookUp.Open")
defer span.Finish()

e.finished = make(chan struct{})
Expand Down
Loading