Skip to content

Commit 38fa026

Browse files
cfzjywxkcfzjywxk
authored andcommitted
issue#4100 add new variable to default disable usage of get_lock and release_lock functions
1 parent a4e53f6 commit 38fa026

File tree

11 files changed

+72
-1
lines changed

11 files changed

+72
-1
lines changed

executor/set_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
. "github.com/pingcap/check"
2121
"github.com/pingcap/parser/terror"
2222
"github.com/pingcap/tidb/config"
23+
"github.com/pingcap/tidb/expression"
2324
"github.com/pingcap/tidb/sessionctx"
2425
"github.com/pingcap/tidb/sessionctx/variable"
2526
"github.com/pingcap/tidb/util/testkit"
@@ -741,3 +742,45 @@ func (s *testSuite2) TestSelectGlobalVar(c *C) {
741742
err = tk.ExecToErr("select @@global.invalid")
742743
c.Assert(terror.ErrorEqual(err, variable.UnknownSystemVar), IsTrue, Commentf("err %v", err))
743744
}
745+
746+
func (s *testSuite2) TestEnableNoopFunctionsVar(c *C) {
747+
tk := testkit.NewTestKit(c, s.store)
748+
749+
// test for tidb_enable_noop_functions
750+
tk.MustQuery(`select @@global.tidb_enable_noop_functions;`).Check(testkit.Rows("0"))
751+
tk.MustQuery(`select @@tidb_enable_noop_functions;`).Check(testkit.Rows("0"))
752+
753+
_, err := tk.Exec(`select get_lock('lock1', 2);`)
754+
c.Assert(terror.ErrorEqual(err, expression.ErrFunctionsNoopImpl), IsTrue, Commentf("err %v", err))
755+
_, err = tk.Exec(`select release_lock('lock1');`)
756+
c.Assert(terror.ErrorEqual(err, expression.ErrFunctionsNoopImpl), IsTrue, Commentf("err %v", err))
757+
758+
// change session var to 1
759+
tk.MustExec(`set tidb_enable_noop_functions=1;`)
760+
tk.MustQuery(`select @@tidb_enable_noop_functions;`).Check(testkit.Rows("1"))
761+
tk.MustQuery(`select @@global.tidb_enable_noop_functions;`).Check(testkit.Rows("0"))
762+
tk.MustQuery(`select get_lock("lock", 10)`).Check(testkit.Rows("1"))
763+
tk.MustQuery(`select release_lock("lock")`).Check(testkit.Rows("1"))
764+
765+
// restore to 0
766+
tk.MustExec(`set tidb_enable_noop_functions=0;`)
767+
tk.MustQuery(`select @@tidb_enable_noop_functions;`).Check(testkit.Rows("0"))
768+
tk.MustQuery(`select @@global.tidb_enable_noop_functions;`).Check(testkit.Rows("0"))
769+
770+
_, err = tk.Exec(`select get_lock('lock2', 10);`)
771+
c.Assert(terror.ErrorEqual(err, expression.ErrFunctionsNoopImpl), IsTrue, Commentf("err %v", err))
772+
_, err = tk.Exec(`select release_lock('lock2');`)
773+
c.Assert(terror.ErrorEqual(err, expression.ErrFunctionsNoopImpl), IsTrue, Commentf("err %v", err))
774+
775+
// set test
776+
_, err = tk.Exec(`set tidb_enable_noop_functions='abc'`)
777+
c.Assert(err, NotNil)
778+
_, err = tk.Exec(`set tidb_enable_noop_functions=11`)
779+
c.Assert(err, NotNil)
780+
tk.MustExec(`set tidb_enable_noop_functions="off";`)
781+
tk.MustQuery(`select @@tidb_enable_noop_functions;`).Check(testkit.Rows("off"))
782+
tk.MustExec(`set tidb_enable_noop_functions="on";`)
783+
tk.MustQuery(`select @@tidb_enable_noop_functions;`).Check(testkit.Rows("on"))
784+
tk.MustExec(`set tidb_enable_noop_functions=0;`)
785+
tk.MustQuery(`select @@tidb_enable_noop_functions;`).Check(testkit.Rows("0"))
786+
}

expression/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var (
2828
ErrRegexp = terror.ClassExpression.New(mysql.ErrRegexp, mysql.MySQLErrName[mysql.ErrRegexp])
2929
ErrOperandColumns = terror.ClassExpression.New(mysql.ErrOperandColumns, mysql.MySQLErrName[mysql.ErrOperandColumns])
3030
ErrCutValueGroupConcat = terror.ClassExpression.New(mysql.ErrCutValueGroupConcat, mysql.MySQLErrName[mysql.ErrCutValueGroupConcat])
31+
ErrFunctionsNoopImpl = terror.ClassExpression.New(mysql.ErrNotSupportedYet, "function %s has only noop implementation in tidb now, use tidb_enable_noop_functions to enable these functions")
3132

3233
// All the un-exported errors are defined here:
3334
errFunctionNotExists = terror.ClassExpression.New(mysql.ErrSpDoesNotExist, mysql.MySQLErrName[mysql.ErrSpDoesNotExist])

expression/function_traits.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,11 @@ var mutableEffectsFunctions = map[string]struct{}{
156156
ast.GetVar: {},
157157
ast.AnyValue: {},
158158
}
159+
160+
// some functions like "get_lock" and "release_lock" currently do NOT have
161+
// right implementations, but may have noop ones(like with any inputs, always return 1)
162+
// if apps really need these "funcs" to run, we offer sys var(tidb_enable_noop_functions) to enable noop usage
163+
var noopFuncs = map[string]struct{}{
164+
ast.GetLock: {},
165+
ast.ReleaseLock: {},
166+
}

expression/integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ func (s *testIntegrationSuite) TestMiscellaneousBuiltin(c *C) {
202202
tk.MustQuery("select a,any_value(b),sum(c) from t1 group by a order by a;").Check(testkit.Rows("1 10 0", "2 30 0"))
203203

204204
// for locks
205+
tk.MustExec(`set tidb_enable_noop_functions=1;`)
205206
result := tk.MustQuery(`SELECT GET_LOCK('test_lock1', 10);`)
206207
result.Check(testkit.Rows("1"))
207208
result = tk.MustQuery(`SELECT GET_LOCK('test_lock2', 10);`)

expression/scalar_function.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ func newFunctionImpl(ctx sessionctx.Context, fold bool, funcName string, retType
8282
if !ok {
8383
return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", funcName)
8484
}
85+
if !ctx.GetSessionVars().EnableNoopFuncs {
86+
if _, ok := noopFuncs[funcName]; ok {
87+
return nil, ErrFunctionsNoopImpl.GenWithStackByArgs(funcName)
88+
}
89+
}
8590
funcArgs := make([]Expression, len(args))
8691
copy(funcArgs, args)
8792
f, err := fc.getFunction(ctx, funcArgs)

expression/typeinfer_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func (s *testInferTypeSuite) TestInferType(c *C) {
103103
c_year year
104104
)`
105105
testKit.MustExec(sql)
106+
testKit.MustExec(`set tidb_enable_noop_functions=1;`)
106107

107108
var tests []typeInferTestCase
108109
tests = append(tests, s.createTestCase4Constants()...)

session/session.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,7 @@ var builtinGlobalVariable = []string{
17451745
variable.TiDBEnableWindowFunction,
17461746
variable.TiDBEnableFastAnalyze,
17471747
variable.TiDBExpensiveQueryTimeThreshold,
1748+
variable.TiDBEnableNoopFuncs,
17481749
}
17491750

17501751
var (

sessionctx/variable/session.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ type SessionVars struct {
392392

393393
// ConnectionInfo indicates current connection info used by current session, only be lazy assigned by plugin.
394394
ConnectionInfo *ConnectionInfo
395+
396+
// use noop funcs or not
397+
EnableNoopFuncs bool
395398
}
396399

397400
// ConnectionInfo present connection used by audit.
@@ -444,6 +447,7 @@ func NewSessionVars() *SessionVars {
444447
SlowQueryFile: config.GetGlobalConfig().Log.SlowQueryFile,
445448
WaitSplitRegionFinish: DefTiDBWaitSplitRegionFinish,
446449
WaitSplitRegionTimeout: DefWaitSplitRegionTimeout,
450+
EnableNoopFuncs: DefTiDBEnableNoopFuncs,
447451
}
448452
vars.Concurrency = Concurrency{
449453
IndexLookupConcurrency: DefIndexLookupConcurrency,
@@ -818,6 +822,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
818822
}
819823
case TiDBLowResolutionTSO:
820824
s.LowResolutionTSO = TiDBOptOn(val)
825+
case TiDBEnableNoopFuncs:
826+
s.EnableNoopFuncs = TiDBOptOn(val)
821827
}
822828
s.systems[name] = val
823829
return nil

sessionctx/variable/sysvar.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ var defaultSysVars = []*SysVar{
701701
{ScopeSession, TiDBWaitSplitRegionTimeout, strconv.Itoa(DefWaitSplitRegionTimeout)},
702702
{ScopeSession, TiDBLowResolutionTSO, "0"},
703703
{ScopeSession, TiDBExpensiveQueryTimeThreshold, strconv.Itoa(DefTiDBExpensiveQueryTimeThreshold)},
704+
{ScopeGlobal | ScopeSession, TiDBEnableNoopFuncs, BoolToIntStr(DefTiDBEnableNoopFuncs)},
704705
}
705706

706707
// SynonymsSysVariables is synonyms of system variables.

sessionctx/variable/tidb_vars.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ const (
277277

278278
// TiDBExpensiveQueryTimeThreshold indicates the time threshold of expensive query.
279279
TiDBExpensiveQueryTimeThreshold = "tidb_expensive_query_time_threshold"
280+
281+
// TiDBEnableNoopFuncs set true will enable using fake funcs(like get_lock release_lock)
282+
TiDBEnableNoopFuncs = "tidb_enable_noop_functions"
280283
)
281284

282285
// Default TiDB system variable values.
@@ -339,6 +342,7 @@ const (
339342
DefTiDBExpensiveQueryTimeThreshold = 60 // 60s
340343
DefTiDBWaitSplitRegionFinish = true
341344
DefWaitSplitRegionTimeout = 300 // 300s
345+
DefTiDBEnableNoopFuncs = false
342346
)
343347

344348
// Process global variables.

0 commit comments

Comments
 (0)