diff --git a/config/config.go b/config/config.go index 84e637bf24012..1d97ce823e8ae 100644 --- a/config/config.go +++ b/config/config.go @@ -81,7 +81,7 @@ type Log struct { File logutil.FileLogConfig `toml:"file" json:"file"` SlowQueryFile string `toml:"slow-query-file" json:"slow-query-file"` - SlowThreshold uint `toml:"slow-threshold" json:"slow-threshold"` + SlowThreshold uint64 `toml:"slow-threshold" json:"slow-threshold"` ExpensiveThreshold uint `toml:"expensive-threshold" json:"expensive-threshold"` QueryLogMaxLen uint `toml:"query-log-max-len" json:"query-log-max-len"` } @@ -256,7 +256,7 @@ var defaultConf = Config{ LogRotate: true, MaxSize: logutil.DefaultLogMaxSize, }, - SlowThreshold: 300, + SlowThreshold: logutil.DefaultSlowThreshold, ExpensiveThreshold: 10000, QueryLogMaxLen: 2048, }, diff --git a/executor/adapter.go b/executor/adapter.go index 3001bda1567b7..4037d6fc3c2ac 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -17,6 +17,7 @@ import ( "fmt" "math" "strings" + "sync/atomic" "time" "github.com/juju/errors" @@ -337,7 +338,7 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool) { } cfg := config.GetGlobalConfig() costTime := time.Since(a.StartTime) - threshold := time.Duration(cfg.Log.SlowThreshold) * time.Millisecond + threshold := time.Duration(atomic.LoadUint64(&cfg.Log.SlowThreshold)) * time.Millisecond if costTime < threshold && level < log.DebugLevel { return } diff --git a/executor/set_test.go b/executor/set_test.go index 3c0a6772e57b4..bf7981df61a66 100644 --- a/executor/set_test.go +++ b/executor/set_test.go @@ -211,6 +211,13 @@ func (s *testSuite) TestSetVar(c *C) { tk.MustQuery(`select @@session.sql_log_bin;`).Check(testkit.Rows("off")) tk.MustExec("set @@sql_log_bin = on") tk.MustQuery(`select @@session.sql_log_bin;`).Check(testkit.Rows("ON")) + + tk.MustExec("set tidb_slow_log_threshold = 0") + tk.MustQuery("select @@session.tidb_slow_log_threshold;").Check(testkit.Rows("0")) + tk.MustExec("set tidb_slow_log_threshold = 1") + tk.MustQuery("select @@session.tidb_slow_log_threshold;").Check(testkit.Rows("1")) + _, err = tk.Exec("set global tidb_slow_log_threshold = 0") + c.Assert(err, NotNil) } func (s *testSuite) TestSetCharset(c *C) { diff --git a/sessionctx/variable/session.go b/sessionctx/variable/session.go index 9be2fd2173dd9..6f0a8a00804e2 100644 --- a/sessionctx/variable/session.go +++ b/sessionctx/variable/session.go @@ -28,6 +28,7 @@ import ( "github.com/pingcap/tidb/terror" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/auth" + "github.com/pingcap/tidb/util/logutil" ) const ( @@ -567,6 +568,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error { s.MemQuotaNestedLoopApply = tidbOptInt64(val, DefTiDBMemQuotaNestedLoopApply) case TiDBGeneralLog: atomic.StoreUint32(&ProcessGeneralLog, uint32(tidbOptPositiveInt32(val, DefTiDBGeneralLog))) + case TiDBSlowLogThreshold: + atomic.StoreUint64(&config.GetGlobalConfig().Log.SlowThreshold, uint64(tidbOptInt64(val, logutil.DefaultSlowThreshold))) case TiDBEnableStreaming: s.EnableStreaming = TiDBOptOn(val) case TiDBOptimizerSelectivityLevel: diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index 5cfa461a08915..b7df726523d21 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -20,6 +20,7 @@ import ( "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/mysql" "github.com/pingcap/tidb/terror" + "github.com/pingcap/tidb/util/logutil" ) // ScopeFlag is for system variable whether can be changed in global/session dynamically or not. @@ -641,6 +642,7 @@ var defaultSysVars = []*SysVar{ {ScopeGlobal | ScopeSession, TiDBDisableTxnAutoRetry, boolToIntStr(DefTiDBDisableTxnAutoRetry)}, /* The following variable is defined as session scope but is actually server scope. */ {ScopeSession, TiDBGeneralLog, strconv.Itoa(DefTiDBGeneralLog)}, + {ScopeSession, TiDBSlowLogThreshold, strconv.Itoa(logutil.DefaultSlowThreshold)}, {ScopeSession, TiDBConfig, ""}, {ScopeGlobal | ScopeSession, TiDBDDLReorgWorkerCount, strconv.Itoa(DefTiDBDDLReorgWorkerCount)}, } diff --git a/sessionctx/variable/tidb_vars.go b/sessionctx/variable/tidb_vars.go index 74ae0379b9667..171c27e0ad7ca 100644 --- a/sessionctx/variable/tidb_vars.go +++ b/sessionctx/variable/tidb_vars.go @@ -102,6 +102,9 @@ const ( // tidb_general_log is used to log every query in the server in info level. TiDBGeneralLog = "tidb_general_log" + // tidb_slow_log_threshold is used to set the slow log threshold in the server. + TiDBSlowLogThreshold = "tidb_slow_log_threshold" + // tidb_enable_streaming enables TiDB to use streaming API for coprocessor requests. TiDBEnableStreaming = "tidb_enable_streaming" diff --git a/sessionctx/variable/varsutil.go b/sessionctx/variable/varsutil.go index c94b09335bc18..e367bf4ee9ca5 100644 --- a/sessionctx/variable/varsutil.go +++ b/sessionctx/variable/varsutil.go @@ -78,6 +78,8 @@ func GetSessionOnlySysVars(s *SessionVars, key string) (string, bool, error) { return "", false, errors.Trace(err) } return string(j), true, nil + case TiDBSlowLogThreshold: + return strconv.FormatUint(atomic.LoadUint64(&config.GetGlobalConfig().Log.SlowThreshold), 10), true, nil } sVal, ok := s.systems[key] if ok { diff --git a/util/logutil/log.go b/util/logutil/log.go index 81f3908b0bb07..58c2e76e145d6 100644 --- a/util/logutil/log.go +++ b/util/logutil/log.go @@ -33,6 +33,8 @@ const ( DefaultLogMaxSize = 300 // MB defaultLogFormat = "text" defaultLogLevel = log.InfoLevel + // DefaultSlowThreshold is the default slow log threshold in millisecond. + DefaultSlowThreshold = 300 ) // FileLogConfig serializes file log related config in toml/json.