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

variables: return error when setting statement summary variables to invalid values #17108

Merged
merged 7 commits into from
May 12, 2020
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
19 changes: 15 additions & 4 deletions sessionctx/variable/varsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,17 @@ func checkInt64SystemVar(name, value string, min, max int64, vars *SessionVars)
return value, nil
}

func checkInt64SystemVarWithError(name, value string, min, max int64) (string, error) {
val, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return value, ErrWrongTypeForVar.GenWithStackByArgs(name)
}
if val < min || val > max {
return value, ErrWrongValueForVar.GenWithStackByArgs(name, value)
}
return value, nil
}

const (
// initChunkSizeUpperBound indicates upper bound value of tidb_init_chunk_size.
initChunkSizeUpperBound = 32
Expand Down Expand Up @@ -703,22 +714,22 @@ func ValidateSetSystemVar(vars *SessionVars, name string, value string, scope Sc
if value == "" && scope == ScopeSession {
return "", nil
}
return checkUInt64SystemVar(name, value, 1, math.MaxInt32, vars)
return checkInt64SystemVarWithError(name, value, 1, math.MaxInt32)
case TiDBStmtSummaryHistorySize:
if value == "" && scope == ScopeSession {
return "", nil
}
return checkUInt64SystemVar(name, value, 0, math.MaxUint8, vars)
return checkInt64SystemVarWithError(name, value, 0, math.MaxUint8)
case TiDBStmtSummaryMaxStmtCount:
if value == "" && scope == ScopeSession {
return "", nil
}
return checkInt64SystemVar(name, value, 1, math.MaxInt16, vars)
return checkInt64SystemVarWithError(name, value, 1, math.MaxInt16)
case TiDBStmtSummaryMaxSQLLength:
if value == "" && scope == ScopeSession {
return "", nil
}
return checkInt64SystemVar(name, value, 0, math.MaxInt32, vars)
return checkInt64SystemVarWithError(name, value, 0, math.MaxInt32)
case TiDBIsolationReadEngines:
engines := strings.Split(value, ",")
var formatVal string
Expand Down
25 changes: 10 additions & 15 deletions sessionctx/variable/varsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,16 +410,6 @@ func (s *testVarsutilSuite) TestVarsutil(c *C) {
val, err = GetSessionSystemVar(v, TiDBStmtSummaryMaxStmtCount)
c.Assert(err, IsNil)
c.Assert(val, Equals, "10")
err = SetSessionSystemVar(v, TiDBStmtSummaryMaxStmtCount, types.NewStringDatum("0"))
c.Assert(err, IsNil)
val, err = GetSessionSystemVar(v, TiDBStmtSummaryMaxStmtCount)
c.Assert(err, IsNil)
c.Assert(val, Equals, "1")
err = SetSessionSystemVar(v, TiDBStmtSummaryMaxStmtCount, types.NewStringDatum("1000000"))
c.Assert(err, IsNil)
val, err = GetSessionSystemVar(v, TiDBStmtSummaryMaxStmtCount)
c.Assert(err, IsNil)
c.Assert(val, Equals, "32767")
err = SetSessionSystemVar(v, TiDBStmtSummaryMaxStmtCount, types.NewStringDatum("a"))
c.Assert(err, ErrorMatches, ".*Incorrect argument type to variable 'tidb_stmt_summary_max_stmt_count'")

Expand All @@ -428,11 +418,6 @@ func (s *testVarsutilSuite) TestVarsutil(c *C) {
val, err = GetSessionSystemVar(v, TiDBStmtSummaryMaxSQLLength)
c.Assert(err, IsNil)
c.Assert(val, Equals, "10")
err = SetSessionSystemVar(v, TiDBStmtSummaryMaxSQLLength, types.NewStringDatum("-1"))
c.Assert(err, IsNil)
val, err = GetSessionSystemVar(v, TiDBStmtSummaryMaxSQLLength)
c.Assert(err, IsNil)
c.Assert(val, Equals, "0")
err = SetSessionSystemVar(v, TiDBStmtSummaryMaxSQLLength, types.NewStringDatum("a"))
c.Assert(err, ErrorMatches, ".*Incorrect argument type to variable 'tidb_stmt_summary_max_sql_length'")

Expand Down Expand Up @@ -572,15 +557,25 @@ func (s *testVarsutilSuite) TestValidateStmtSummary(c *C) {
{TiDBStmtSummaryRefreshInterval, "a", true, ScopeSession},
{TiDBStmtSummaryRefreshInterval, "", false, ScopeSession},
{TiDBStmtSummaryRefreshInterval, "", true, ScopeGlobal},
{TiDBStmtSummaryRefreshInterval, "0", true, ScopeGlobal},
{TiDBStmtSummaryRefreshInterval, "99999999999", true, ScopeGlobal},
{TiDBStmtSummaryHistorySize, "a", true, ScopeSession},
{TiDBStmtSummaryHistorySize, "", false, ScopeSession},
{TiDBStmtSummaryHistorySize, "", true, ScopeGlobal},
{TiDBStmtSummaryHistorySize, "0", false, ScopeGlobal},
{TiDBStmtSummaryHistorySize, "-1", true, ScopeGlobal},
{TiDBStmtSummaryHistorySize, "99999999", true, ScopeGlobal},
{TiDBStmtSummaryMaxStmtCount, "a", true, ScopeSession},
{TiDBStmtSummaryMaxStmtCount, "", false, ScopeSession},
{TiDBStmtSummaryMaxStmtCount, "", true, ScopeGlobal},
{TiDBStmtSummaryMaxStmtCount, "0", true, ScopeGlobal},
{TiDBStmtSummaryMaxStmtCount, "99999999", true, ScopeGlobal},
{TiDBStmtSummaryMaxSQLLength, "a", true, ScopeSession},
{TiDBStmtSummaryMaxSQLLength, "", false, ScopeSession},
{TiDBStmtSummaryMaxSQLLength, "", true, ScopeGlobal},
{TiDBStmtSummaryMaxSQLLength, "0", false, ScopeGlobal},
{TiDBStmtSummaryMaxSQLLength, "-1", true, ScopeGlobal},
{TiDBStmtSummaryMaxSQLLength, "99999999999", true, ScopeGlobal},
}

for _, t := range tests {
Expand Down