From 7f2c249d127991b25a6d107e66204e3d3d641249 Mon Sep 17 00:00:00 2001 From: jianhaiqing Date: Thu, 28 May 2020 09:17:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0lock=5Fwait=5Ftimeout,?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E6=99=AE=E9=80=9ASQL=20=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E9=94=81=E7=AD=89=E5=BE=85=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 6 ++++++ config/config.toml.default | 5 +++++ config/config.toml.example | 5 +++++ docs/options.md | 1 + session/core.go | 1 + session/session_inception.go | 20 ++++++++++++++++++++ sessionctx/variable/sysvar.go | 2 ++ 7 files changed, 40 insertions(+) diff --git a/config/config.go b/config/config.go index 6f4911190..858c18748 100644 --- a/config/config.go +++ b/config/config.go @@ -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"` @@ -719,6 +724,7 @@ var defaultConf = Config{ CheckFloatDouble: false, CheckIdentifierUpper: false, SqlSafeUpdates: -1, + LockWaitTimeout: -1, SupportCharset: "utf8,utf8mb4", SupportEngine: "innodb", Lang: "en-US", diff --git a/config/config.toml.default b/config/config.toml.default index d97c79adf..9de018b31 100644 --- a/config/config.toml.default +++ b/config/config.toml.default @@ -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" diff --git a/config/config.toml.example b/config/config.toml.example index d53997774..34a3456af 100644 --- a/config/config.toml.example +++ b/config/config.toml.example @@ -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" diff --git a/docs/options.md b/docs/options.md index 248357477..648908250 100644 --- a/docs/options.md +++ b/docs/options.md @@ -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`,此处可以设置多个,以逗号分隔,或者修改默认的存在引擎类型 diff --git a/session/core.go b/session/core.go index 2b0e5463d..f87ef2152 100644 --- a/session/core.go +++ b/session/core.go @@ -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暂不支持备份功能.") diff --git a/session/session_inception.go b/session/session_inception.go index c096c14f4..1418f3c17 100644 --- a/session/session_inception.go +++ b/session/session_inception.go @@ -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") diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index 90ac41af8..563c4cb3d 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -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.