Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#55991
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <[email protected]>
  • Loading branch information
winoros authored and ti-chi-bot committed Sep 10, 2024
1 parent 5665343 commit 15f790d
Show file tree
Hide file tree
Showing 3 changed files with 283 additions and 4 deletions.
4 changes: 0 additions & 4 deletions pkg/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1317,10 +1317,6 @@ func createSessionFunc(store kv.Storage) pools.Factory {
if err != nil {
return nil, errors.Trace(err)
}
err = se.sessionVars.SetSystemVar(variable.TiDBEnableWindowFunction, variable.BoolToOnOff(variable.DefEnableWindowFunction))
if err != nil {
return nil, errors.Trace(err)
}
err = se.sessionVars.SetSystemVar(variable.TiDBConstraintCheckInPlacePessimistic, variable.On)
if err != nil {
return nil, errors.Trace(err)
Expand Down
6 changes: 6 additions & 0 deletions pkg/sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2022,6 +2022,12 @@ func NewSessionVars(hctx HookContext) *SessionVars {
TiFlashComputeDispatchPolicy: tiflashcompute.DispatchPolicyConsistentHash,
ResourceGroupName: resourcegroup.DefaultResourceGroupName,
DefaultCollationForUTF8MB4: mysql.DefaultCollationName,
<<<<<<< HEAD
=======
GroupConcatMaxLen: DefGroupConcatMaxLen,
EnableRedactLog: DefTiDBRedactLog,
EnableWindowFunction: DefEnableWindowFunction,
>>>>>>> 2c30f865e32 (session: set EnableWindowFunction for all SessionVars (#55991))
}
vars.KVVars = tikvstore.NewVariables(&vars.Killed)
vars.StmtCtx.ResourceGroupName = resourcegroup.DefaultResourceGroupName
Expand Down
277 changes: 277 additions & 0 deletions pkg/sessionctx/variable/sysvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1437,3 +1437,280 @@ func TestSetTiDBCloudStorageURI(t *testing.T) {
require.Len(t, val, 0)
cancel()
}
<<<<<<< HEAD
=======

func TestGlobalSystemVariableInitialValue(t *testing.T) {
vars := []struct {
name string
val string
initVal string
}{
{
TiDBTxnMode,
DefTiDBTxnMode,
"pessimistic",
},
{
TiDBEnableAsyncCommit,
BoolToOnOff(DefTiDBEnableAsyncCommit),
BoolToOnOff(DefTiDBEnableAsyncCommit),
},
{
TiDBEnable1PC,
BoolToOnOff(DefTiDBEnable1PC),
BoolToOnOff(DefTiDBEnable1PC),
},
{
TiDBMemOOMAction,
DefTiDBMemOOMAction,
OOMActionLog,
},
{
TiDBEnableAutoAnalyze,
BoolToOnOff(DefTiDBEnableAutoAnalyze),
Off,
},
{
TiDBRowFormatVersion,
strconv.Itoa(DefTiDBRowFormatV1),
strconv.Itoa(DefTiDBRowFormatV2),
},
{
TiDBTxnAssertionLevel,
DefTiDBTxnAssertionLevel,
AssertionFastStr,
},
{
TiDBEnableMutationChecker,
BoolToOnOff(DefTiDBEnableMutationChecker),
On,
},
{
TiDBPessimisticTransactionFairLocking,
BoolToOnOff(DefTiDBPessimisticTransactionFairLocking),
On,
},
}
for _, v := range vars {
initVal := GlobalSystemVariableInitialValue(v.name, v.val)
require.Equal(t, v.initVal, initVal)
}
}

func TestTiDBOptTxnAutoRetry(t *testing.T) {
sv := GetSysVar(TiDBDisableTxnAutoRetry)
vars := NewSessionVars(nil)

for _, scope := range []ScopeFlag{ScopeSession, ScopeGlobal} {
val, err := sv.Validate(vars, "OFF", scope)
require.NoError(t, err)
require.Equal(t, "ON", val)
warn := vars.StmtCtx.GetWarnings()[0].Err
require.Equal(t, "[variable:1287]'OFF' is deprecated and will be removed in a future release. Please use ON instead", warn.Error())
}
}

func TestTiDBLowResTSOUpdateInterval(t *testing.T) {
sv := GetSysVar(TiDBLowResolutionTSOUpdateInterval)
vars := NewSessionVars(nil)

// Too low, will get raised to the min value
val, err := sv.Validate(vars, "0", ScopeGlobal)
require.NoError(t, err)
require.Equal(t, strconv.FormatInt(GetSysVar(TiDBLowResolutionTSOUpdateInterval).MinValue, 10), val)
warn := vars.StmtCtx.GetWarnings()[0].Err
require.Equal(t, "[variable:1292]Truncated incorrect tidb_low_resolution_tso_update_interval value: '0'", warn.Error())

// Too high, will get lowered to the max value
val, err = sv.Validate(vars, "100000", ScopeGlobal)
require.NoError(t, err)
require.Equal(t, strconv.FormatUint(GetSysVar(TiDBLowResolutionTSOUpdateInterval).MaxValue, 10), val)
warn = vars.StmtCtx.GetWarnings()[1].Err
require.Equal(t, "[variable:1292]Truncated incorrect tidb_low_resolution_tso_update_interval value: '100000'", warn.Error())

// valid
val, err = sv.Validate(vars, "1000", ScopeGlobal)
require.NoError(t, err)
require.Equal(t, "1000", val)
}

func TestTiDBSchemaCacheSize(t *testing.T) {
vars := NewSessionVars(nil)
mock := NewMockGlobalAccessor4Tests()
mock.SessionVars = vars
vars.GlobalVarsAccessor = mock
var (
mb uint64 = 1 << 20
err error
val string
maxValue uint64 = math.MaxInt64
)
// Test tidb_schema_cache_size
schemaCacheSize := GetSysVar(TiDBSchemaCacheSize)
// Check default value
require.Equal(t, schemaCacheSize.Value, strconv.Itoa(DefTiDBSchemaCacheSize))

// MinValue is 512 MB
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, strconv.FormatUint(100*mb, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, "512MB", val)

// MaxValue is 9223372036854775807
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, strconv.FormatUint(maxValue, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, strconv.FormatUint(maxValue, 10), val)

// test MinValue-1
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, strconv.FormatUint(100*mb-1, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, "512MB", val)

// test MaxValue+1
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, strconv.FormatUint(maxValue+1, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, strconv.FormatUint(maxValue, 10), val)

// Test Normal Value
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, strconv.FormatUint(1024*mb, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, strconv.FormatUint(1024*mb, 10), val)

// Test Close
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, strconv.FormatUint(0, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, "0", val)

// Test byteSize format
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "1234567890123")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, SchemaCacheSize.Load(), uint64(1234567890123))
require.Equal(t, "1234567890123", val)

err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "10KB")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, SchemaCacheSize.Load(), uint64(512<<20))
require.Equal(t, "512MB", val)

err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "12345678KB")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, SchemaCacheSize.Load(), uint64(12345678<<10))
require.Equal(t, "12345678KB", val)

err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "700MB")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, SchemaCacheSize.Load(), uint64(700<<20))
require.Equal(t, "700MB", val)

err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "20GB")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, SchemaCacheSize.Load(), uint64(20<<30))
require.Equal(t, "20GB", val)

err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "2TB")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBSchemaCacheSize)
require.NoError(t, err)
require.Equal(t, SchemaCacheSize.Load(), uint64(2<<40))
require.Equal(t, "2TB", val)

// Test error
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "123aaa123")
require.Error(t, err)
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "700MBaa")
require.Error(t, err)
err = mock.SetGlobalSysVar(context.Background(), TiDBSchemaCacheSize, "a700MB")
require.Error(t, err)
}

func TestEnableWindowFunction(t *testing.T) {
vars := NewSessionVars(nil)
require.Equal(t, vars.EnableWindowFunction, DefEnableWindowFunction)
require.NoError(t, vars.SetSystemVar(TiDBEnableWindowFunction, "on"))
require.Equal(t, vars.EnableWindowFunction, true)
require.NoError(t, vars.SetSystemVar(TiDBEnableWindowFunction, "0"))
require.Equal(t, vars.EnableWindowFunction, false)
require.NoError(t, vars.SetSystemVar(TiDBEnableWindowFunction, "1"))
require.Equal(t, vars.EnableWindowFunction, true)
}

func TestTiDBAutoAnalyzeConcurrencyValidation(t *testing.T) {
vars := NewSessionVars(nil)

tests := []struct {
name string
autoAnalyze bool
autoAnalyzePriority bool
input string
expectError bool
}{
{
name: "Both enabled, valid input",
autoAnalyze: true,
autoAnalyzePriority: true,
input: "10",
expectError: false,
},
{
name: "Auto analyze disabled",
autoAnalyze: false,
autoAnalyzePriority: true,
input: "10",
expectError: true,
},
{
name: "Auto analyze priority queue disabled",
autoAnalyze: true,
autoAnalyzePriority: false,
input: "10",
expectError: true,
},
{
name: "Both disabled",
autoAnalyze: false,
autoAnalyzePriority: false,
input: "10",
expectError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
RunAutoAnalyze.Store(tt.autoAnalyze)
EnableAutoAnalyzePriorityQueue.Store(tt.autoAnalyzePriority)

sysVar := GetSysVar(TiDBAutoAnalyzeConcurrency)
require.NotNil(t, sysVar)

_, err := sysVar.Validate(vars, tt.input, ScopeGlobal)
if tt.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
>>>>>>> 2c30f865e32 (session: set EnableWindowFunction for all SessionVars (#55991))

0 comments on commit 15f790d

Please sign in to comment.