Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: add index acceleration introduce sysvars and config param #36088

Merged
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ const (
DefExpensiveQueryTimeThreshold = 60
// DefMemoryUsageAlarmRatio is the threshold triggering an alarm which the memory usage of tidb-server instance exceeds.
DefMemoryUsageAlarmRatio = 0.8
// DefTempDir is the default temporary directory path for TiDB.
DefTempDir = "/tmp/tidb"
)

// Valid config maps
Expand Down Expand Up @@ -170,6 +172,7 @@ type Config struct {
RunDDL bool `toml:"run-ddl" json:"run-ddl"`
SplitTable bool `toml:"split-table" json:"split-table"`
TokenLimit uint `toml:"token-limit" json:"token-limit"`
TempDir string `toml:"temp-dir" json:"temp-dir"`
TempStoragePath string `toml:"tmp-storage-path" json:"tmp-storage-path"`
// TempStorageQuota describe the temporary storage Quota during query exector when TiDBEnableTmpStorageOnOOM is enabled
// If the quota exceed the capacity of the TempStoragePath, the tidb-server would exit with fatal error
Expand Down Expand Up @@ -810,6 +813,7 @@ var defaultConf = Config{
Lease: "45s",
TokenLimit: 1000,
OOMUseTmpStorage: true,
TempDir: DefTempDir,
TempStorageQuota: -1,
TempStoragePath: tempStorageDirName,
MemQuotaQuery: 1 << 30,
Expand Down
3 changes: 3 additions & 0 deletions config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ split-table = true
# The limit of concurrent executed sessions.
token-limit = 1000

# The temporary directory to store the intermediate compute results.
temp-dir = "/tmp/tidb"

# Specifies the temporary storage path for some operators when a single SQL statement exceeds the memory quota specified by the memory quota.
# It defaults to a generated directory in `<TMPDIR>/<os/user.Current().Uid>_tidb/` if it is unset.
# It only takes effect when `tidb_enable_tmp_storage_on_oom` is `true`.
Expand Down
13 changes: 13 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,19 @@ var defaultSysVars = []*SysVar{
s.DefaultStrMatchSelectivity = tidbOptFloat64(val, DefTiDBDefaultStrMatchSelectivity)
return nil
}},
{Scope: ScopeGlobal, Name: TiDBDDLEnableFastReorg, Value: BoolToOnOff(DefTiDBEnableFastReorg), Type: TypeBool, GetGlobal: func(sv *SessionVars) (string, error) {
return BoolToOnOff(EnableFastReorg.Load()), nil
}, SetGlobal: func(s *SessionVars, val string) error {
EnableFastReorg.Store(TiDBOptOn(val))
return nil
}},
// This system var is set disk quota for lightning sort dir, from 100 GB to 1PB.
{Scope: ScopeGlobal, Name: TiDBDDLDiskQuota, Value: strconv.Itoa(DefTiDBDDLDiskQuota), Type: TypeInt, MinValue: DefTiDBDDLDiskQuota, MaxValue: 1024 * 1024 * DefTiDBDDLDiskQuota / 100, GetGlobal: func(sv *SessionVars) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will be a big number for 100GB shows in bytes, can we use GB directly, like set @@tidb_ddl_disk_quota=100

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other options like tidb_memory_quota_query, tidb_stats_cache_quota and tidb_memory_quota_apply_cache are counted in bytes. Let's improve them systematically: #17847.

return strconv.FormatInt(DDLDiskQuota.Load(), 10), nil
}, SetGlobal: func(s *SessionVars, val string) error {
DDLDiskQuota.Store(TidbOptInt64(val, DefTiDBDDLDiskQuota))
return nil
}},
}

// FeedbackProbability points to the FeedbackProbability in statistics package.
Expand Down
76 changes: 76 additions & 0 deletions sessionctx/variable/sysvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1083,3 +1083,79 @@ func TestDefaultMemoryDebugModeValue(t *testing.T) {
require.NoError(t, err)
require.Equal(t, val, "0")
}

func TestSetTIDBFastDDL(t *testing.T) {
vars := NewSessionVars()
mock := NewMockGlobalAccessor4Tests()
mock.SessionVars = vars
vars.GlobalVarsAccessor = mock
fastDDL := GetSysVar(TiDBDDLEnableFastReorg)

// Default off
require.Equal(t, fastDDL.Value, Off)

// Set to On
err := mock.SetGlobalSysVar(TiDBDDLEnableFastReorg, On)
require.NoError(t, err)
val, err1 := mock.GetGlobalSysVar(TiDBDDLEnableFastReorg)
require.NoError(t, err1)
require.Equal(t, On, val)

// Set to off
err = mock.SetGlobalSysVar(TiDBDDLEnableFastReorg, Off)
require.NoError(t, err)
val, err1 = mock.GetGlobalSysVar(TiDBDDLEnableFastReorg)
require.NoError(t, err1)
require.Equal(t, Off, val)
}

func TestSetTIDBDiskQuota(t *testing.T) {
vars := NewSessionVars()
mock := NewMockGlobalAccessor4Tests()
mock.SessionVars = vars
vars.GlobalVarsAccessor = mock
diskQuota := GetSysVar(TiDBDDLDiskQuota)
var (
gb int64 = 1024 * 1024 * 1024
pb int64 = 1024 * 1024 * 1024 * 1024 * 1024
err error
val string
)
// Default 100 GB
require.Equal(t, diskQuota.Value, strconv.FormatInt(100*gb, 10))

// MinValue is 100 GB, set to 50 Gb is not allowed
err = mock.SetGlobalSysVar(TiDBDDLDiskQuota, strconv.FormatInt(50*gb, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBDDLDiskQuota)
require.NoError(t, err)
require.Equal(t, strconv.FormatInt(100*gb, 10), val)

// Set to 100 GB
err = mock.SetGlobalSysVar(TiDBDDLDiskQuota, strconv.FormatInt(100*gb, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBDDLDiskQuota)
require.NoError(t, err)
require.Equal(t, strconv.FormatInt(100*gb, 10), val)

// Set to 200 GB
err = mock.SetGlobalSysVar(TiDBDDLDiskQuota, strconv.FormatInt(200*gb, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBDDLDiskQuota)
require.NoError(t, err)
require.Equal(t, strconv.FormatInt(200*gb, 10), val)

// Set to 1 Pb
err = mock.SetGlobalSysVar(TiDBDDLDiskQuota, strconv.FormatInt(pb, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBDDLDiskQuota)
require.NoError(t, err)
require.Equal(t, strconv.FormatInt(pb, 10), val)

// MaxValue is 1 PB, set to 2 Pb is not allowed, it will set back to 1 PB max allowed value.
err = mock.SetGlobalSysVar(TiDBDDLDiskQuota, strconv.FormatInt(2*pb, 10))
require.NoError(t, err)
val, err = mock.GetGlobalSysVar(TiDBDDLDiskQuota)
require.NoError(t, err)
require.Equal(t, strconv.FormatInt(pb, 10), val)
}
11 changes: 11 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,10 @@ const (
// TiDBEnableTmpStorageOnOOM controls whether to enable the temporary storage for some operators
// when a single SQL statement exceeds the memory quota specified by the memory quota.
TiDBEnableTmpStorageOnOOM = "tidb_enable_tmp_storage_on_oom"
// TiDBDDLEnableFastReorg indicates whether to use lighting backfill process for adding index.
TiDBDDLEnableFastReorg = "tidb_ddl_fast_reorg"
// TiDBDDLDiskQuota used to set disk quota for lightning add index.
TiDBDDLDiskQuota = "tidb_ddl_disk_quota"
)

// TiDB intentional limits
Expand Down Expand Up @@ -1000,6 +1004,9 @@ const (
DefEnableTiDBGCAwareMemoryTrack = true
DefTiDBDefaultStrMatchSelectivity = 0.8
DefTiDBEnableTmpStorageOnOOM = true
DefTiDBEnableFastReorg = false
DefTiDBDDLEnableFastReorg = false
DefTiDBDDLDiskQuota = 100 * 1024 * 1024 * 1024 // 100GB
)

// Process global variables.
Expand Down Expand Up @@ -1048,6 +1055,10 @@ var (
EnableConcurrentDDL = atomic.NewBool(DefTiDBEnableConcurrentDDL)
DDLForce2Queue = atomic.NewBool(false)
EnableNoopVariables = atomic.NewBool(DefTiDBEnableNoopVariables)
// EnableFastReorg indicates whether to use lightning to enhance DDL reorg performance.
EnableFastReorg = atomic.NewBool(DefTiDBEnableFastReorg)
// DDLDiskQuota is the temporary variable for set disk quota for lightning
DDLDiskQuota = atomic.NewInt64(DefTiDBDDLDiskQuota)
)

var (
Expand Down
5 changes: 5 additions & 0 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const (
nmPluginLoad = "plugin-load"
nmRepairMode = "repair-mode"
nmRepairList = "repair-list"
nmTempDir = "temp-dir"

nmProxyProtocolNetworks = "proxy-protocol-networks"
nmProxyProtocolHeaderTimeout = "proxy-protocol-header-timeout"
Expand Down Expand Up @@ -142,6 +143,7 @@ var (
affinityCPU = flag.String(nmAffinityCPU, "", "affinity cpu (cpu-no. separated by comma, e.g. 1,2,3)")
repairMode = flagBoolean(nmRepairMode, false, "enable admin repair mode")
repairList = flag.String(nmRepairList, "", "admin repair table list")
tempDir = flag.String(nmTempDir, config.DefTempDir, "tidb temporary directory")

// Log
logLevel = flag.String(nmLogLevel, "info", "log level: info, debug, warn, error, fatal")
Expand Down Expand Up @@ -474,6 +476,9 @@ func overrideConfig(cfg *config.Config) {
cfg.RepairTableList = stringToList(*repairList)
}
}
if actualFlags[nmTempDir] {
cfg.TempDir = *tempDir
}

// Log
if actualFlags[nmLogLevel] {
Expand Down