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

*: reduce ResetContextOfStmt() object allocation #26241

Merged
merged 12 commits into from
Aug 9, 2021
Merged
16 changes: 11 additions & 5 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1656,12 +1656,18 @@ func (e *UnionExec) Close() error {
// Before every execution, we must clear statement context.
func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
vars := ctx.GetSessionVars()
sc := &stmtctx.StatementContext{
TimeZone: vars.Location(),
TaskID: stmtctx.AllocateTaskID(),
CTEStorageMap: map[int]*CTEStorages{},
IsStaleness: false,
var sc *stmtctx.StatementContext
if vars.TxnCtx.CouldRetry {
// Must construct new statement context object, the retry history need context for every statement.
// TODO: Maybe one day we can get rid of transaction retry, then this logic can be deleted.
sc = &stmtctx.StatementContext{}
} else {
sc = vars.InitStatementContext()
}
sc.TimeZone = vars.Location()
sc.TaskID = stmtctx.AllocateTaskID()
sc.CTEStorageMap = map[int]*CTEStorages{}
sc.IsStaleness = false

sc.InitMemTracker(memory.LabelForSQLText, vars.MemQuotaQuery)
sc.InitDiskTracker(memory.LabelForSQLText, -1)
Expand Down
1 change: 0 additions & 1 deletion executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2169,7 +2169,6 @@ func (s *testSuite4) TestLoadData(c *C) {
{[]byte("\t2\t3"), []byte("\t4\t5"), nil, []byte("\t2\t3\t4\t5"), "Records: 0 Deleted: 0 Skipped: 0 Warnings: 0"},
}
checkCases(tests, ld, c, tk, ctx, selectSQL, deleteSQL)
c.Assert(sc.WarningCount(), Equals, uint16(1))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This check is incorrect. The warnings is reset after the next statement.

The correct behavior should be check warning immediately after a statement.
The old code rely on every time a new statement context is allocated ... so even it does not checked immediately, the data is not overwritten.


// lines starting symbol is "" and terminated symbol length is 2, InsertData returns data is nil
ld.LinesInfo.Terminated = "||"
Expand Down
2 changes: 0 additions & 2 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,6 @@ func (s *testTableSuite) TestSomeTables(c *C) {
Command: byte(1),
Digest: "abc1",
State: 1,
StmtCtx: tk.Se.GetSessionVars().StmtCtx,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Take out tk.Se.GetSessionVars().StmtCtx for future use is not correct, it's not a snapshot.
The correct usage should be Lock -> Read data -> Unlock for process info ...
See #26196

}
sm.processInfoMap[2] = &util.ProcessInfo{
ID: 2,
Expand All @@ -553,7 +552,6 @@ func (s *testTableSuite) TestSomeTables(c *C) {
Digest: "abc2",
State: 2,
Info: strings.Repeat("x", 101),
StmtCtx: tk.Se.GetSessionVars().StmtCtx,
CurTxnStartTS: 410090409861578752,
}
tk.Se.SetSessionManager(sm)
Expand Down
12 changes: 12 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,18 @@ type SessionVars struct {

// TemporaryTableData stores committed kv values for temporary table for current session.
TemporaryTableData kv.MemBuffer

// cached is used to optimze the object allocation.
cached struct {
curr int8
data [2]stmtctx.StatementContext
}
}

func (s *SessionVars) InitStatementContext() *stmtctx.StatementContext {
s.cached.curr = (s.cached.curr + 1) % 2
s.cached.data[s.cached.curr] = stmtctx.StatementContext{}
return &s.cached.data[s.cached.curr]
}

// AllocMPPTaskID allocates task id for mpp tasks. It will reset the task id if the query's
Expand Down