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 read-only statement from transaction auto retry (#5026) #5051

Merged
merged 2 commits into from
Nov 14, 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
Next Next commit
remove read-only statement from transaction auto retry (#5026)
* *: Ignore readonly statement when retrying

* remove explain statement from retry
* remove all select statement from retry except It has setvar functions
* remove execute statement from retry if it is read-only statement
  • Loading branch information
wentaoxu committed Nov 14, 2017
commit 5f5e810156916d781e3f33f8cad17e9a3158ae7e
3 changes: 3 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ type Statement interface {

// IsPrepared returns whether this statement is prepared statement.
IsPrepared() bool

// IsReadOnly returns if the statement is read only. For example: SelectStmt without lock.
IsReadOnly() bool
}

// Visitor visits a Node.
Expand Down
61 changes: 61 additions & 0 deletions ast/read_only_checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package ast

// IsReadOnly checks whether the input ast is readOnly.
func IsReadOnly(node Node) bool {
switch st := node.(type) {
case *SelectStmt:
if st.LockTp == SelectLockForUpdate {
return false
}

checker := readOnlyChecker{
readOnly: true,
}

node.Accept(&checker)
return checker.readOnly
case *ExplainStmt:
return true
default:
return false
}
}

// readOnlyChecker checks whether a query's ast is readonly, if it satisfied
// 1. selectstmt;
// 2. need not to set var;
// it is readonly statement.
type readOnlyChecker struct {
readOnly bool
}

// Enter implements Visitor interface.
func (checker *readOnlyChecker) Enter(in Node) (out Node, skipChildren bool) {
switch node := in.(type) {
case *VariableExpr:
// like func rewriteVariable(), this stands for SetVar.
if !node.IsSystem && node.Value != nil {
checker.readOnly = false
return in, true
}
}
return in, false
}

// Leave implements Visitor interface.
func (checker *readOnlyChecker) Leave(in Node) (out Node, ok bool) {
return in, checker.readOnly
}
41 changes: 41 additions & 0 deletions ast/read_only_checker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package ast

import (
. "github.com/pingcap/check"
)

var _ = Suite(&testCacheableSuite{})

type testCacheableSuite struct {
}

func (s *testCacheableSuite) TestCacheable(c *C) {
// test non-SelectStmt
var stmt Node = &DeleteStmt{}
c.Assert(IsReadOnly(stmt), IsFalse)

stmt = &InsertStmt{}
c.Assert(IsReadOnly(stmt), IsFalse)

stmt = &UpdateStmt{}
c.Assert(IsReadOnly(stmt), IsFalse)

stmt = &ExplainStmt{}
c.Assert(IsReadOnly(stmt), IsTrue)

stmt = &ExplainStmt{}
c.Assert(IsReadOnly(stmt), IsTrue)
}
8 changes: 8 additions & 0 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ type ExecStmt struct {
ctx context.Context
startTime time.Time
isPreparedStmt bool

// ReadOnly represents the statement is read-only.
ReadOnly bool
}

// OriginText implements ast.Statement interface.
Expand All @@ -125,6 +128,11 @@ func (a *ExecStmt) IsPrepared() bool {
return a.isPreparedStmt
}

// IsReadOnly implements ast.Statement interface.
func (a *ExecStmt) IsReadOnly() bool {
return a.ReadOnly
}

// Exec implements the ast.Statement Exec interface.
// 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
Expand Down
6 changes: 6 additions & 0 deletions executor/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ func (c *Compiler) Compile(ctx context.Context, stmtNode ast.StmtNode) (*ExecStm
return nil, errors.Trace(err)
}

readOnlyCheckStmt := stmtNode
if checkPlan, ok := finalPlan.(*plan.Execute); ok {
readOnlyCheckStmt = checkPlan.Stmt
}

return &ExecStmt{
InfoSchema: infoSchema,
Plan: finalPlan,
Expensive: stmtCount(stmtNode, finalPlan, ctx.GetSessionVars().InRestrictedSQL),
Cacheable: plan.Cacheable(stmtNode),
Text: stmtNode.Text(),
ReadOnly: ast.IsReadOnly(readOnlyCheckStmt),
}, nil
}

Expand Down
7 changes: 7 additions & 0 deletions executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,16 @@ func CompileExecutePreparedStmt(ctx context.Context, ID uint32, args ...interfac
value := ast.NewValueExpr(val)
execPlan.UsingVars[i] = &expression.Constant{Value: value.Datum, RetType: &value.Type}
}

readOnly := false
if execute, ok := execPlan.(*plan.Execute); ok {
readOnly = ast.IsReadOnly(execute.Stmt)
}

stmt := &ExecStmt{
InfoSchema: GetInfoSchema(ctx),
Plan: execPlan,
ReadOnly: readOnly,
}
if prepared, ok := ctx.GetSessionVars().PreparedStmts[ID].(*Prepared); ok {
stmt.Text = prepared.Stmt.Text()
Expand Down
4 changes: 4 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,9 @@ func (s *session) retry(maxCnt int, infoSchemaChanged bool) error {
s.sessionVars.RetryInfo.ResetOffset()
for i, sr := range nh.history {
st := sr.st
if st.IsReadOnly() {
continue
}
txt := st.OriginText()
if infoSchemaChanged {
st, err = updateStatement(st, s, txt)
Expand Down Expand Up @@ -694,6 +697,7 @@ func (s *session) Execute(sql string) (recordSets []ast.RecordSet, err error) {
Plan: cacheValue.(*cache.SQLCacheValue).Plan,
Expensive: cacheValue.(*cache.SQLCacheValue).Expensive,
Text: stmtNode.Text(),
ReadOnly: ast.IsReadOnly(stmtNode),
}

s.PrepareTxnCtx()
Expand Down