Skip to content

Commit

Permalink
sessionctx: support parse more format for memory limit systemvar (#38965
Browse files Browse the repository at this point in the history
)

ref #37816
  • Loading branch information
wshwsh12 authored Nov 22, 2022
1 parent 94d53c6 commit f4fa414
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 6 deletions.
1 change: 1 addition & 0 deletions sessionctx/variable/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ go_test(
"//types",
"//util/chunk",
"//util/execdetails",
"//util/gctuner",
"//util/memory",
"//util/mock",
"@com_github_pingcap_failpoint//:failpoint",
Expand Down
20 changes: 15 additions & 5 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,14 +791,19 @@ var defaultSysVars = []*SysVar{
return nil
},
},
{Scope: ScopeGlobal, Name: TiDBServerMemoryLimitSessMinSize, Value: strconv.FormatUint(DefTiDBServerMemoryLimitSessMinSize, 10), Type: TypeUnsigned, MinValue: 0, MaxValue: math.MaxUint64,
{Scope: ScopeGlobal, Name: TiDBServerMemoryLimitSessMinSize, Value: strconv.FormatUint(DefTiDBServerMemoryLimitSessMinSize, 10), Type: TypeStr,
GetGlobal: func(_ context.Context, s *SessionVars) (string, error) {
return memory.ServerMemoryLimitSessMinSize.String(), nil
},
Validation: func(s *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
intVal, err := strconv.ParseUint(normalizedValue, 10, 64)
if err != nil {
return "", err
bt, str := parseByteSize(normalizedValue)
if str != "" {
intVal = bt
} else {
return "", err
}
}
if intVal > 0 && intVal < 128 { // 128 Bytes
s.StmtCtx.AppendWarning(ErrTruncatedWrongValue.GenWithStackByArgs(TiDBServerMemoryLimitSessMinSize, originalValue))
Expand All @@ -815,17 +820,22 @@ var defaultSysVars = []*SysVar{
return nil
},
},
{Scope: ScopeGlobal, Name: TiDBServerMemoryLimitGCTrigger, Value: strconv.FormatFloat(DefTiDBServerMemoryLimitGCTrigger, 'f', -1, 64), Type: TypeFloat, MinValue: 0, MaxValue: math.MaxUint64,
{Scope: ScopeGlobal, Name: TiDBServerMemoryLimitGCTrigger, Value: strconv.FormatFloat(DefTiDBServerMemoryLimitGCTrigger, 'f', -1, 64), Type: TypeStr,
GetGlobal: func(_ context.Context, s *SessionVars) (string, error) {
return strconv.FormatFloat(gctuner.GlobalMemoryLimitTuner.GetPercentage(), 'f', -1, 64), nil
},
Validation: func(s *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
floatValue, err := strconv.ParseFloat(normalizedValue, 64)
if err != nil {
return "", err
perc, str := parsePercentage(normalizedValue)
if len(str) != 0 {
floatValue = float64(perc) / 100
} else {
return "", err
}
}
gogcTunerThreshold := GOGCTunerThreshold.Load()
if floatValue < 0.51 && floatValue > 1 { // 51% ~ 100%
if floatValue < 0.51 || floatValue > 1 { // 51% ~ 100%
return "", ErrWrongValueForVar.GenWithStackByArgs(TiDBServerMemoryLimitGCTrigger, normalizedValue)
}
// gogcTunerThreshold must not be 0. it will be 0 when tidb_gogc_tuner_threshold is not set during startup.
Expand Down
87 changes: 86 additions & 1 deletion sessionctx/variable/sysvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/util/gctuner"
"github.com/pingcap/tidb/util/memory"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -843,7 +844,6 @@ func TestTiDBServerMemoryLimit2(t *testing.T) {
mock.SessionVars = vars
vars.GlobalVarsAccessor = mock
var (
//mb uint64 = 1 << 20
err error
val string
)
Expand Down Expand Up @@ -951,6 +951,91 @@ func TestTiDBServerMemoryLimit2(t *testing.T) {
require.Error(t, err)
}

func TestTiDBServerMemoryLimitSessMinSize(t *testing.T) {
vars := NewSessionVars(nil)
mock := NewMockGlobalAccessor4Tests()
mock.SessionVars = vars
vars.GlobalVarsAccessor = mock

var (
err error
val string
)

serverMemroyLimitSessMinSize := GetSysVar(TiDBServerMemoryLimitSessMinSize)
// Check default value
require.Equal(t, serverMemroyLimitSessMinSize.Value, strconv.FormatInt(DefTiDBServerMemoryLimitSessMinSize, 10))

err = mock.SetGlobalSysVar(context.Background(), TiDBServerMemoryLimitSessMinSize, "123456")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBServerMemoryLimitSessMinSize)
require.NoError(t, err)
require.Equal(t, memory.ServerMemoryLimitSessMinSize.Load(), uint64(123456))
require.Equal(t, "123456", val)

err = mock.SetGlobalSysVar(context.Background(), TiDBServerMemoryLimitSessMinSize, "100")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBServerMemoryLimitSessMinSize)
require.NoError(t, err)
require.Equal(t, memory.ServerMemoryLimitSessMinSize.Load(), uint64(128))
require.Equal(t, "128", val)

err = mock.SetGlobalSysVar(context.Background(), TiDBServerMemoryLimitSessMinSize, "123MB")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBServerMemoryLimitSessMinSize)
require.NoError(t, err)
require.Equal(t, memory.ServerMemoryLimitSessMinSize.Load(), uint64(123<<20))
require.Equal(t, "128974848", val)
}

func TestTiDBServerMemoryLimitGCTrigger(t *testing.T) {
vars := NewSessionVars(nil)
mock := NewMockGlobalAccessor4Tests()
mock.SessionVars = vars
vars.GlobalVarsAccessor = mock

var (
err error
val string
)

serverMemroyLimitGCTrigger := GetSysVar(TiDBServerMemoryLimitGCTrigger)
// Check default value
require.Equal(t, serverMemroyLimitGCTrigger.Value, strconv.FormatFloat(DefTiDBServerMemoryLimitGCTrigger, 'f', -1, 64))
defer func() {
err = mock.SetGlobalSysVar(context.Background(), TiDBServerMemoryLimitGCTrigger, strconv.FormatFloat(DefTiDBServerMemoryLimitGCTrigger, 'f', -1, 64))
require.NoError(t, err)
}()

err = mock.SetGlobalSysVar(context.Background(), TiDBServerMemoryLimitGCTrigger, "0.8")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBServerMemoryLimitGCTrigger)
require.NoError(t, err)
require.Equal(t, gctuner.GlobalMemoryLimitTuner.GetPercentage(), 0.8)
require.Equal(t, "0.8", val)

err = mock.SetGlobalSysVar(context.Background(), TiDBServerMemoryLimitGCTrigger, "90%")
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBServerMemoryLimitGCTrigger)
require.NoError(t, err)
require.Equal(t, gctuner.GlobalMemoryLimitTuner.GetPercentage(), 0.9)
require.Equal(t, "0.9", val)

err = mock.SetGlobalSysVar(context.Background(), TiDBServerMemoryLimitGCTrigger, "100%")
require.Error(t, err)
err = mock.SetGlobalSysVar(context.Background(), TiDBServerMemoryLimitGCTrigger, "101%")
require.Error(t, err)
err = mock.SetGlobalSysVar(context.Background(), TiDBServerMemoryLimitGCTrigger, "99%")
require.NoError(t, err)

err = mock.SetGlobalSysVar(context.Background(), TiDBGOGCTunerThreshold, "0.4")
require.NoError(t, err)
err = mock.SetGlobalSysVar(context.Background(), TiDBServerMemoryLimitGCTrigger, "49%")
require.Error(t, err)
err = mock.SetGlobalSysVar(context.Background(), TiDBServerMemoryLimitGCTrigger, "51%")
require.NoError(t, err)
}

func TestSetAggPushDownGlobally(t *testing.T) {
vars := NewSessionVars(nil)
mock := NewMockGlobalAccessor4Tests()
Expand Down

0 comments on commit f4fa414

Please sign in to comment.