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

tidb,config: limit statement count in a transaction #5754

Merged
merged 2 commits into from
Jan 30, 2018
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
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type Performance struct {
CrossJoin bool `toml:"cross-join" json:"cross-join"`
StatsLease string `toml:"stats-lease" json:"stats-lease"`
RunAutoAnalyze bool `toml:"run-auto-analyze" json:"run-auto-analyze"`
StmtCountLimit int `toml:"stmt-count-limit" json:"stmt-count-limit"`
}

// XProtocol is the XProtocol section of the config.
Expand Down Expand Up @@ -126,6 +127,7 @@ var defaultConf = Config{
CrossJoin: true,
StatsLease: "3s",
RunAutoAnalyze: true,
StmtCountLimit: 5000,
},
XProtocol: XProtocol{
XHost: "0.0.0.0",
Expand Down
2 changes: 2 additions & 0 deletions config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ metrics-interval = 15
[performance]
# Set keep alive option for tcp connection.
tcp-keep-alive = true
# StmtCountLimit limits the max count of statement inside a transaction.
stmt-count-limit = 5000

# The maximum number of retries when commit a transaction.
retry-limit = 10
Expand Down
24 changes: 24 additions & 0 deletions new_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

. "github.com/pingcap/check"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/executor"
Expand Down Expand Up @@ -1351,6 +1352,29 @@ type testSchemaSuite struct {
checkLeak func()
}

func (s *testSessionSuite) TestStatementCountLimit(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("create table stmt_count_limit (id int)")
saved := config.GetGlobalConfig().Performance.StmtCountLimit
config.GetGlobalConfig().Performance.StmtCountLimit = 3
defer func() {
config.GetGlobalConfig().Performance.StmtCountLimit = saved
}()
tk.MustExec("begin")
tk.MustExec("insert into stmt_count_limit values (1)")
tk.MustExec("insert into stmt_count_limit values (2)")
_, err := tk.Exec("insert into stmt_count_limit values (3)")
c.Assert(err, NotNil)

// begin is counted into history but this one is not.
tk.MustExec("SET SESSION autocommit = false")
tk.MustExec("insert into stmt_count_limit values (1)")
tk.MustExec("insert into stmt_count_limit values (2)")
tk.MustExec("insert into stmt_count_limit values (3)")
_, err = tk.Exec("insert into stmt_count_limit values (4)")
c.Assert(err, NotNil)
}

func (s *testSchemaSuite) TearDownTest(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
r := tk.MustQuery("show tables")
Expand Down
11 changes: 11 additions & 0 deletions tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/juju/errors"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/executor"
Expand Down Expand Up @@ -170,6 +171,16 @@ func runStmt(ctx context.Context, s ast.Statement) (ast.RecordSet, error) {
} else {
err = se.CommitTxn()
}
} else {
// If the user insert, insert, insert ... but never commit, TiDB would OOM.
// So we limit the statement count in a transaction here.
history := GetHistory(ctx)
if history.Count() > config.GetGlobalConfig().Performance.StmtCountLimit {
err1 := se.RollbackTxn()
terror.Log(errors.Trace(err1))
return rs, errors.Errorf("statement count %d exceeds the transaction limitation, autocommit = %t",
history.Count(), ctx.GetSessionVars().IsAutocommit())
}
}
return rs, errors.Trace(err)
}
Expand Down