Skip to content

Commit

Permalink
*: add a variable to force statement priority of TiDB server (#7694)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackysp authored and zz-jason committed Sep 20, 2018
1 parent cc09eca commit 334e925
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 5 deletions.
8 changes: 8 additions & 0 deletions executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ package executor
import (
"math"
"sort"
"sync/atomic"

"fmt"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/plan"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/memory"
Expand Down Expand Up @@ -345,6 +348,11 @@ func ResetStmtCtx(ctx sessionctx.Context, s ast.StmtNode) (err error) {
sc.IgnoreTruncate = true
sc.IgnoreZeroInDate = true
}
if !sessVars.InRestrictedSQL {
if priority := mysql.PriorityEnum(atomic.LoadInt32(&variable.ForcePriority)); priority != mysql.NoPriority {
sc.Priority = priority
}
}
if sessVars.LastInsertID > 0 {
sessVars.PrevLastInsertID = sessVars.LastInsertID
sessVars.LastInsertID = 0
Expand Down
13 changes: 13 additions & 0 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,19 @@ func (s *testSuite) TestSetVar(c *C) {

tk.MustExec("set @@tidb_general_log = 1")
tk.MustExec("set @@tidb_general_log = 0")

tk.MustExec(`set tidb_force_priority = "no_priority"`)
tk.MustQuery(`select @@tidb_force_priority;`).Check(testkit.Rows("NO_PRIORITY"))
tk.MustExec(`set tidb_force_priority = "low_priority"`)
tk.MustQuery(`select @@tidb_force_priority;`).Check(testkit.Rows("LOW_PRIORITY"))
tk.MustExec(`set tidb_force_priority = "high_priority"`)
tk.MustQuery(`select @@tidb_force_priority;`).Check(testkit.Rows("HIGH_PRIORITY"))
tk.MustExec(`set tidb_force_priority = "delayed"`)
tk.MustQuery(`select @@tidb_force_priority;`).Check(testkit.Rows("DELAYED"))
tk.MustExec(`set tidb_force_priority = "abc"`)
tk.MustQuery(`select @@tidb_force_priority;`).Check(testkit.Rows("NO_PRIORITY"))
_, err = tk.Exec(`set global tidb_force_priority = ""`)
c.Assert(err, NotNil)
}

func (s *testSuite) TestSetCharset(c *C) {
Expand Down
25 changes: 25 additions & 0 deletions mysql/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,31 @@ const (
DelayedPriority
)

// Priority2Str is used to convert the statement priority to string.
var Priority2Str = map[PriorityEnum]string{
NoPriority: "NO_PRIORITY",
LowPriority: "LOW_PRIORITY",
HighPriority: "HIGH_PRIORITY",
DelayedPriority: "DELAYED",
}

// Str2Priority is used to convert a string to a priority.
func Str2Priority(val string) PriorityEnum {
val = strings.ToUpper(val)
switch val {
case "NO_PRIORITY":
return NoPriority
case "HIGH_PRIORITY":
return HighPriority
case "LOW_PRIORITY":
return LowPriority
case "DELAYED":
return DelayedPriority
default:
return NoPriority
}
}

// PrimaryKeyName defines primary key name.
const (
PrimaryKeyName = "PRIMARY"
Expand Down
2 changes: 2 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
SetDDLReorgWorkerCounter(int32(tidbOptPositiveInt32(val, DefTiDBDDLReorgWorkerCount)))
case TiDBDDLReorgPriority:
s.setDDLReorgPriority(val)
case TiDBForcePriority:
atomic.StoreInt32(&ForcePriority, int32(mysql.Str2Priority(val)))
}
s.systems[name] = val
return nil
Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ var defaultSysVars = []*SysVar{
{ScopeSession, TiDBConfig, ""},
{ScopeGlobal | ScopeSession, TiDBDDLReorgWorkerCount, strconv.Itoa(DefTiDBDDLReorgWorkerCount)},
{ScopeSession, TiDBDDLReorgPriority, "PRIORITY_LOW"},
{ScopeSession, TiDBForcePriority, mysql.Priority2Str[DefTiDBForcePriority]},
}

// SynonymsSysVariables is synonyms of system variables.
Expand Down
17 changes: 12 additions & 5 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package variable

import (
"os"

"github.com/pingcap/tidb/mysql"
)

/*
Expand Down Expand Up @@ -185,6 +187,10 @@ const (
// tidb_ddl_reorg_priority defines the operations priority of adding indices.
// It can be: PRIORITY_LOW, PRIORITY_NORMAL, PRIORITY_HIGH
TiDBDDLReorgPriority = "tidb_ddl_reorg_priority"

// tidb_force_priority defines the operations priority of all statements.
// It can be "NO_PRIORITY", "LOW_PRIORITY", "HIGH_PRIORITY", "DELAYED"
TiDBForcePriority = "tidb_force_priority"
)

// Default TiDB system variable values.
Expand Down Expand Up @@ -224,14 +230,15 @@ const (
DefTiDBDDLReorgWorkerCount = 16
DefTiDBHashAggPartialConcurrency = 4
DefTiDBHashAggFinalConcurrency = 4
DefTiDBForcePriority = mysql.NoPriority
)

// Process global variables.
var (
ProcessGeneralLog uint32
ddlReorgWorkerCounter int32 = DefTiDBDDLReorgWorkerCount
maxDDLReorgWorkerCount int32 = 128
// DDLSlowOprThreshold is the threshold for ddl slow operations, uint is millisecond.
DDLSlowOprThreshold uint32 = 300
ServerHostname, _ = os.Hostname()
ddlReorgWorkerCounter int32 = DefTiDBDDLReorgWorkerCount
maxDDLReorgWorkerCount int32 = 128
DDLSlowOprThreshold uint32 = 300 // DDLSlowOprThreshold is the threshold for ddl slow operations, uint is millisecond.
ForcePriority = int32(DefTiDBForcePriority)
ServerHostname, _ = os.Hostname()
)
2 changes: 2 additions & 0 deletions sessionctx/variable/varsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func GetSessionOnlySysVars(s *SessionVars, key string) (string, bool, error) {
return "", false, errors.Trace(err)
}
return string(j), true, nil
case TiDBForcePriority:
return mysql.Priority2Str[mysql.PriorityEnum(atomic.LoadInt32(&ForcePriority))], true, nil
}
sVal, ok := s.systems[key]
if ok {
Expand Down

0 comments on commit 334e925

Please sign in to comment.