Skip to content

Commit 28e6294

Browse files
marsishandsomejackysp
authored andcommitted
add skip isolation level check variable (#10065)
1 parent 29e9a41 commit 28e6294

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

executor/set_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,49 @@ func (s *testSuite2) TestSetVar(c *C) {
285285
c.Assert(err, NotNil)
286286
_, err = tk.Exec("set global tidb_batch_commit = 2")
287287
c.Assert(err, NotNil)
288+
289+
// test skip isolation level check: init
290+
tk.MustExec("SET GLOBAL tidb_skip_isolation_level_check = 0")
291+
tk.MustExec("SET SESSION tidb_skip_isolation_level_check = 0")
292+
tk.MustExec("SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED")
293+
tk.MustExec("SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED")
294+
tk.MustQuery("select @@global.tx_isolation").Check(testkit.Rows("READ-COMMITTED"))
295+
tk.MustQuery("select @@global.transaction_isolation").Check(testkit.Rows("READ-COMMITTED"))
296+
tk.MustQuery("select @@session.tx_isolation").Check(testkit.Rows("READ-COMMITTED"))
297+
tk.MustQuery("select @@session.transaction_isolation").Check(testkit.Rows("READ-COMMITTED"))
298+
299+
// test skip isolation level check: error
300+
_, err = tk.Exec("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE")
301+
c.Assert(terror.ErrorEqual(err, variable.ErrUnsupportedIsolationLevel), IsTrue, Commentf("err %v", err))
302+
tk.MustQuery("select @@session.tx_isolation").Check(testkit.Rows("READ-COMMITTED"))
303+
tk.MustQuery("select @@session.transaction_isolation").Check(testkit.Rows("READ-COMMITTED"))
304+
305+
_, err = tk.Exec("SET GLOBAL TRANSACTION ISOLATION LEVEL SERIALIZABLE")
306+
c.Assert(terror.ErrorEqual(err, variable.ErrUnsupportedIsolationLevel), IsTrue, Commentf("err %v", err))
307+
tk.MustQuery("select @@global.tx_isolation").Check(testkit.Rows("READ-COMMITTED"))
308+
tk.MustQuery("select @@global.transaction_isolation").Check(testkit.Rows("READ-COMMITTED"))
309+
310+
// test skip isolation level check: success
311+
tk.MustExec("SET GLOBAL tidb_skip_isolation_level_check = 1")
312+
tk.MustExec("SET SESSION tidb_skip_isolation_level_check = 1")
313+
tk.MustExec("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE")
314+
tk.MustQuery("show warnings").Check(testkit.Rows(
315+
"Warning 1105 The isolation level 'SERIALIZABLE' is not supported. Set tidb_skip_isolation_level_check=1 to skip this error"))
316+
tk.MustQuery("select @@session.tx_isolation").Check(testkit.Rows("SERIALIZABLE"))
317+
tk.MustQuery("select @@session.transaction_isolation").Check(testkit.Rows("SERIALIZABLE"))
318+
319+
// test skip isolation level check: success
320+
tk.MustExec("SET GLOBAL tidb_skip_isolation_level_check = 0")
321+
tk.MustExec("SET SESSION tidb_skip_isolation_level_check = 1")
322+
tk.MustExec("SET GLOBAL TRANSACTION ISOLATION LEVEL READ UNCOMMITTED")
323+
tk.MustQuery("show warnings").Check(testkit.Rows(
324+
"Warning 1105 The isolation level 'READ-UNCOMMITTED' is not supported. Set tidb_skip_isolation_level_check=1 to skip this error"))
325+
tk.MustQuery("select @@global.tx_isolation").Check(testkit.Rows("READ-UNCOMMITTED"))
326+
tk.MustQuery("select @@global.transaction_isolation").Check(testkit.Rows("READ-UNCOMMITTED"))
327+
328+
// test skip isolation level check: reset
329+
tk.MustExec("SET GLOBAL tidb_skip_isolation_level_check = 0")
330+
tk.MustExec("SET SESSION tidb_skip_isolation_level_check = 0")
288331
}
289332

290333
func (s *testSuite2) TestSetCharset(c *C) {
@@ -584,6 +627,8 @@ func (s *testSuite2) TestValidateSetVar(c *C) {
584627
result = tk.MustQuery("select @@tx_isolation;")
585628
result.Check(testkit.Rows("REPEATABLE-READ"))
586629

630+
tk.MustExec("SET GLOBAL tidb_skip_isolation_level_check = 0")
631+
tk.MustExec("SET SESSION tidb_skip_isolation_level_check = 0")
587632
_, err = tk.Exec("set @@tx_isolation='SERIALIZABLE'")
588633
c.Assert(terror.ErrorEqual(err, variable.ErrUnsupportedValueForVar), IsTrue, Commentf("err %v", err))
589634
}

sessionctx/variable/session.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,21 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
603603
case TxnIsolationOneShot:
604604
switch val {
605605
case "SERIALIZABLE", "READ-UNCOMMITTED":
606-
return ErrUnsupportedValueForVar.GenWithStackByArgs(name, val)
606+
skipIsolationLevelCheck, err := GetSessionSystemVar(s, TiDBSkipIsolationLevelCheck)
607+
returnErr := ErrUnsupportedIsolationLevel.GenWithStackByArgs(val)
608+
if err != nil {
609+
returnErr = err
610+
}
611+
if !TiDBOptOn(skipIsolationLevelCheck) || err != nil {
612+
return returnErr
613+
}
614+
//SET TRANSACTION ISOLATION LEVEL will affect two internal variables:
615+
// 1. tx_isolation
616+
// 2. transaction_isolation
617+
// The following if condition is used to deduplicate two same warnings.
618+
if name == "transaction_isolation" {
619+
s.StmtCtx.AppendWarning(returnErr)
620+
}
607621
}
608622
s.TxnIsolationLevelOneShot.State = 1
609623
s.TxnIsolationLevelOneShot.Value = val

sessionctx/variable/sysvar.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ var (
8585
ErrTruncatedWrongValue = terror.ClassVariable.New(CodeTruncatedWrongValue, mysql.MySQLErrName[mysql.ErrTruncatedWrongValue])
8686
ErrMaxPreparedStmtCountReached = terror.ClassVariable.New(CodeMaxPreparedStmtCountReached, mysql.MySQLErrName[mysql.ErrMaxPreparedStmtCountReached])
8787
ErrUnsupportedValueForVar = terror.ClassVariable.New(CodeUnknownStatusVar, "variable '%s' does not yet support value: %s")
88+
ErrUnsupportedIsolationLevel = terror.ClassVariable.New(CodeUnknownStatusVar, "The isolation level '%s' is not supported. Set tidb_skip_isolation_level_check=1 to skip this error")
8889
)
8990

9091
func init() {
@@ -673,6 +674,7 @@ var defaultSysVars = []*SysVar{
673674
{ScopeSession, TiDBOptimizerSelectivityLevel, strconv.Itoa(DefTiDBOptimizerSelectivityLevel)},
674675
{ScopeGlobal | ScopeSession, TiDBEnableWindowFunction, BoolToIntStr(DefEnableWindowFunction)},
675676
{ScopeGlobal | ScopeSession, TiDBEnableFastAnalyze, BoolToIntStr(DefTiDBUseFastAnalyze)},
677+
{ScopeGlobal | ScopeSession, TiDBSkipIsolationLevelCheck, BoolToIntStr(DefTiDBSkipIsolationLevelCheck)},
676678
/* The following variable is defined as session scope but is actually server scope. */
677679
{ScopeSession, TiDBGeneralLog, strconv.Itoa(DefTiDBGeneralLog)},
678680
{ScopeSession, TiDBSlowLogThreshold, strconv.Itoa(logutil.DefaultSlowThreshold)},

sessionctx/variable/tidb_vars.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ const (
131131

132132
// TiDBCheckMb4ValueInUTF8 is used to control whether to enable the check wrong utf8 value.
133133
TiDBCheckMb4ValueInUTF8 = "tidb_check_mb4_value_in_utf8"
134+
135+
// tidb_skip_isolation_level_check is used to control whether to return error when set unsupported transaction
136+
// isolation level.
137+
TiDBSkipIsolationLevelCheck = "tidb_skip_isolation_level_check"
134138
)
135139

136140
// TiDB system variable names that both in session and global scope.
@@ -295,6 +299,7 @@ const (
295299
DefEnableWindowFunction = false
296300
DefTiDBDDLSlowOprThreshold = 300
297301
DefTiDBUseFastAnalyze = false
302+
DefTiDBSkipIsolationLevelCheck = false
298303
)
299304

300305
// Process global variables.

sessionctx/variable/varsutil.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,21 @@ func ValidateSetSystemVar(vars *SessionVars, name string, value string) (string,
429429
}
430430
switch upVal {
431431
case "SERIALIZABLE", "READ-UNCOMMITTED":
432-
return "", ErrUnsupportedValueForVar.GenWithStackByArgs(name, value)
432+
skipIsolationLevelCheck, err := GetSessionSystemVar(vars, TiDBSkipIsolationLevelCheck)
433+
returnErr := ErrUnsupportedIsolationLevel.GenWithStackByArgs(value)
434+
if err != nil {
435+
returnErr = err
436+
}
437+
if !TiDBOptOn(skipIsolationLevelCheck) || err != nil {
438+
return "", returnErr
439+
}
440+
//SET TRANSACTION ISOLATION LEVEL will affect two internal variables:
441+
// 1. tx_isolation
442+
// 2. transaction_isolation
443+
// The following if condition is used to deduplicate two same warnings.
444+
if name == "transaction_isolation" {
445+
vars.StmtCtx.AppendWarning(returnErr)
446+
}
433447
}
434448
return upVal, nil
435449
case TiDBInitChunkSize:

0 commit comments

Comments
 (0)