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

添加lock_wait_timeout,控制普通SQL 执行时的锁等待超时时间 #224

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ type Inc struct {
// 1 表示开启安全更新
SqlSafeUpdates int `toml:"sql_safe_updates" json:"sql_safe_updates"`

// 设置执行SQL时,会话变量
// 0 表示不做操作,基于远端数据库【默认值】
// > 0 值表示,会话在执行SQL 时获取锁超时的时间
LockWaitTimeout int `toml:"lock_wait_timeout" json:"lock_wait_timeout"`

// 支持的字符集
SupportCharset string `toml:"support_charset" json:"support_charset"`

Expand Down Expand Up @@ -719,6 +724,7 @@ var defaultConf = Config{
CheckFloatDouble: false,
CheckIdentifierUpper: false,
SqlSafeUpdates: -1,
LockWaitTimeout: -1,
SupportCharset: "utf8,utf8mb4",
SupportEngine: "innodb",
Lang: "en-US",
Expand Down
5 changes: 5 additions & 0 deletions config/config.toml.default
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ explain_rule = "first"
# 1 表示开启安全更新
sql_safe_updates = -1

# 设置执行SQL时,会话变量
# -1 表示不做操作,基于远端数据库【默认值】
# > 0 值表示,会话在执行SQL 时获取锁超时的时间
lock_wait_timeout = -1

support_charset = "utf8,utf8mb4"
support_engine = "innodb"

Expand Down
5 changes: 5 additions & 0 deletions config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ explain_rule = "first"
# 1 表示开启安全更新
sql_safe_updates = -1

# 设置执行SQL时,会话变量
# -1 表示不做操作,基于远端数据库【默认值】
# > 0 值表示,会话在执行SQL 时获取锁超时的时间
lock_wait_timeout = -1

skip_grant_table = true

support_charset = "utf8,utf8mb4"
Expand Down
1 change: 1 addition & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ merge_alter_table | false | true,false | 在多个改同一个表
must_have_columns `v0.6.3` | '' | string | 用以指定建表时必须创建的列。多个列时以逗号分隔(`格式: 列名 [列类型,可选]`)
skip_sqls `v1.0-rc3` | '' | string | 指定不再审核的SQL.该参数指定要跳过的客户端/框架默认SQL,以实现客户端兼容
sql_safe_updates | -1 | -1,0,1 | 安全更新.-1表示不做操作,基于远端数据库,0表示关闭安全更新,1表示开启安全更新
lock_wait_timeout | -1 | -1, N+ | 会话在执行SQL 时获取锁超时的时间, -1 表示基于远端数据库,正整数表示获取锁的超时时间
support_charset | utf8,utf8mb4 | string | 支持的字符集,多个时以逗号分隔
support_collation `v0.7` | '' | string | 支持的排序规则,多个时以逗号分隔
support_engine `v1.0-rc4` | 'innodb' | string | 支持的存储引擎类型.默认为`innodb`,此处可以设置多个,以逗号分隔,或者修改默认的存在引擎类型
Expand Down
1 change: 1 addition & 0 deletions session/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ func (s *session) checkOptions() error {

s.mysqlServerVersion()
s.setSqlSafeUpdates()
s.setLockWaitTimeout()

if s.opt.Backup && s.dbType == DBTypeTiDB {
s.appendErrorMessage("TiDB暂不支持备份功能.")
Expand Down
20 changes: 20 additions & 0 deletions session/session_inception.go
Original file line number Diff line number Diff line change
Expand Up @@ -1873,6 +1873,26 @@ func (s *session) setSqlSafeUpdates() {
}
}

func (s *session) setLockWaitTimeout() {
log.Debug("setLockWaitTimeout")

var sql string
if s.inc.LockWaitTimeout > 0 {
sql = fmt.Sprintf("set session lock_wait_timeout=%d;", s.inc.LockWaitTimeout)
} else {
return
}

if _, err := s.exec(sql, true); err != nil {
log.Errorf("con:%d %v", s.sessionVars.ConnectionID, err)
if myErr, ok := err.(*mysqlDriver.MySQLError); ok {
s.appendErrorMessage(myErr.Message)
} else {
s.appendErrorMessage(err.Error())
}
}
}

func (s *session) checkBinlogIsOn() bool {
log.Debug("checkBinlogIsOn")

Expand Down
2 changes: 2 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,8 @@ const (
ForeignKeyChecks = "foreign_key_checks"
// SQLSafeUpdates is the name for 'sql_safe_updates' system variable.
SQLSafeUpdates = "sql_safe_updates"
// LockWaitTimeout is the name for 'lock_wait_timeout' system variable.
LockWaitTimeout = "lock_wait_timeout"
// WarningCount is the name for 'warning_count' system variable.
WarningCount = "warning_count"
// ErrorCount is the name for 'error_count' system variable.
Expand Down