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

*: add a variable tidb_slow_log_threshold to set the slow log threshold dynamically #8094

Merged
merged 5 commits into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,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"`
winoros marked this conversation as resolved.
Show resolved Hide resolved
ExpensiveThreshold uint `toml:"expensive-threshold" json:"expensive-threshold"`
QueryLogMaxLen uint `toml:"query-log-max-len" json:"query-log-max-len"`
}
Expand Down Expand Up @@ -275,7 +275,7 @@ var defaultConf = Config{
LogRotate: true,
MaxSize: logutil.DefaultLogMaxSize,
},
SlowThreshold: 300,
SlowThreshold: logutil.DefaultSlowThreshold,
ExpensiveThreshold: 10000,
QueryLogMaxLen: 2048,
},
Expand Down
7 changes: 7 additions & 0 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ func (s *testSuite) TestSetVar(c *C) {
tk.MustQuery(`select @@session.tidb_constraint_check_in_place;`).Check(testkit.Rows("1"))
tk.MustExec("set global tidb_constraint_check_in_place = 0")
tk.MustQuery(`select @@global.tidb_constraint_check_in_place;`).Check(testkit.Rows("0"))

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) {
Expand Down
3 changes: 3 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/timeutil"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -607,6 +608,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 TiDBRetryLimit:
s.RetryLimit = tidbOptInt64(val, DefTiDBRetryLimit)
case TiDBDisableTxnAutoRetry:
Expand Down
2 changes: 2 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/util/logutil"
)

// ScopeFlag is for system variable whether can be changed in global/session dynamically or not.
Expand Down Expand Up @@ -662,6 +663,7 @@ var defaultSysVars = []*SysVar{
{ScopeSession, TiDBOptimizerSelectivityLevel, strconv.Itoa(DefTiDBOptimizerSelectivityLevel)},
/* 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)},
{ScopeSession, TiDBDDLReorgPriority, "PRIORITY_LOW"},
Expand Down
5 changes: 4 additions & 1 deletion sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ const (
// tidb_general_log is used to log every query in the server in info level.
TiDBGeneralLog = "tidb_general_log"

// tidb_retry_limit is the maximun number of retries when committing a transaction.
// tidb_slow_log_threshold is used to set the slow log threshold in the server.
TiDBSlowLogThreshold = "tidb_slow_log_threshold"

// tidb_retry_limit is the maximum number of retries when committing a transaction.
TiDBRetryLimit = "tidb_retry_limit"

// tidb_disable_txn_auto_retry disables transaction auto retry.
Expand Down
4 changes: 3 additions & 1 deletion sessionctx/variable/varsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func GetSessionOnlySysVars(s *SessionVars, key string) (string, bool, error) {
return string(j), true, nil
case TiDBForcePriority:
return mysql.Priority2Str[mysql.PriorityEnum(atomic.LoadInt32(&ForcePriority))], true, nil
case TiDBSlowLogThreshold:
return strconv.FormatUint(atomic.LoadUint64(&config.GetGlobalConfig().Log.SlowThreshold), 10), true, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, adapator.go:351 also need change to atomic.LoadUnit64 to keep visibility guarantee?

}
sVal, ok := s.systems[key]
if ok {
Expand Down Expand Up @@ -322,7 +324,7 @@ func ValidateSetSystemVar(vars *SessionVars, name string, value string) (string,
TIDBMemQuotaIndexLookupReader,
TIDBMemQuotaIndexLookupJoin,
TIDBMemQuotaNestedLoopApply,
TiDBRetryLimit:
TiDBRetryLimit, TiDBSlowLogThreshold:
_, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return value, ErrWrongValueForVar.GenWithStackByArgs(name)
Expand Down
2 changes: 2 additions & 0 deletions util/logutil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down