Skip to content

Commit

Permalink
*: move config file option enable-batch-dml to sysvar (#33803)
Browse files Browse the repository at this point in the history
ref #33769
  • Loading branch information
morgo authored May 13, 2022
1 parent c68b617 commit bbfbe13
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 30 deletions.
8 changes: 7 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ type Config struct {
// TempStorageQuota describe the temporary storage Quota during query exector when OOMUseTmpStorage is enabled
// If the quota exceed the capacity of the TempStoragePath, the tidb-server would exit with fatal error
TempStorageQuota int64 `toml:"tmp-storage-quota" json:"tmp-storage-quota"` // Bytes
EnableBatchDML bool `toml:"enable-batch-dml" json:"enable-batch-dml"`
TxnLocalLatches tikvcfg.TxnLocalLatches `toml:"-" json:"-"`
ServerVersion string `toml:"server-version" json:"server-version"`
VersionComment string `toml:"version-comment" json:"version-comment"`
Expand Down Expand Up @@ -248,6 +247,12 @@ type Config struct {
MaxBallastObjectSize int `toml:"max-ballast-object-size" json:"max-ballast-object-size"`
// BallastObjectSize set the initial size of the ballast object, the unit is byte.
BallastObjectSize int `toml:"ballast-object-size" json:"ballast-object-size"`

// The following items are deprecated. We need to keep them here temporarily
// to support the upgrade process. They can be removed in future.

// EnableBatchDML, unused since bootstrap v90
EnableBatchDML bool `toml:"enable-batch-dml" json:"enable-batch-dml"`
}

// UpdateTempStoragePath is to update the `TempStoragePath` if port/statusPort was changed
Expand Down Expand Up @@ -930,6 +935,7 @@ var deprecatedConfig = map[string]struct{}{
"stmt-summary.max-sql-length": {},
"stmt-summary.refresh-interval": {},
"stmt-summary.history-size": {},
"enable-batch-dml": {}, // use tidb_enable_batch_dml
"mem-quota-query": {},
"query-log-max-len": {},
}
Expand Down
3 changes: 0 additions & 3 deletions config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ tmp-storage-quota = -1
# Valid options: ["log", "cancel"]
oom-action = "cancel"

# Enable batch commit for the DMLs.
enable-batch-dml = false

# Make "kill query" behavior compatible with MySQL. It's not recommend to
# turn on this option when TiDB server is behind a proxy.
compatible-kill-query = false
Expand Down
2 changes: 0 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ enable-table-lock = true
alter-primary-key = true
delay-clean-table-lock = 5
split-region-max-num=10000
enable-batch-dml = true
server-version = "test_version"
repair-mode = true
max-server-connections = 200
Expand Down Expand Up @@ -283,7 +282,6 @@ grpc-max-send-msg-size = 40960
require.True(t, conf.EnableTableLock)
require.Equal(t, uint64(5), conf.DelayCleanTableLock)
require.Equal(t, uint64(10000), conf.SplitRegionMaxNum)
require.True(t, conf.EnableBatchDML)
require.True(t, conf.RepairMode)
require.Equal(t, uint64(16), conf.TiKVClient.ResolveLockLiteThreshold)
require.Equal(t, uint32(200), conf.MaxServerConnections)
Expand Down
4 changes: 2 additions & 2 deletions executor/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"context"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/model"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/types"
Expand Down Expand Up @@ -89,7 +89,7 @@ func (e *DeleteExec) deleteSingleTableByChunk(ctx context.Context) error {
batchDMLSize := e.ctx.GetSessionVars().DMLBatchSize
// If tidb_batch_delete is ON and not in a transaction, we could use BatchDelete mode.
batchDelete := e.ctx.GetSessionVars().BatchDelete && !e.ctx.GetSessionVars().InTxn() &&
config.GetGlobalConfig().EnableBatchDML && batchDMLSize > 0
variable.EnableBatchDML.Load() && batchDMLSize > 0
fields := retTypes(e.children[0])
chk := newFirstChunk(e.children[0])
columns := e.children[0].Schema().Columns
Expand Down
6 changes: 3 additions & 3 deletions executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/opentracing/opentracing-go"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/kv"
Expand All @@ -33,6 +32,7 @@ import (
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
Expand Down Expand Up @@ -232,7 +232,7 @@ func insertRows(ctx context.Context, base insertCommon) (err error) {
}
sessVars := e.ctx.GetSessionVars()
batchSize := sessVars.DMLBatchSize
batchInsert := sessVars.BatchInsert && !sessVars.InTxn() && config.GetGlobalConfig().EnableBatchDML && batchSize > 0
batchInsert := sessVars.BatchInsert && !sessVars.InTxn() && variable.EnableBatchDML.Load() && batchSize > 0

e.lazyFillAutoID = true
evalRowFunc := e.fastEvalRow
Expand Down Expand Up @@ -435,7 +435,7 @@ func insertRowsFromSelect(ctx context.Context, base insertCommon) error {

sessVars := e.ctx.GetSessionVars()
batchSize := sessVars.DMLBatchSize
batchInsert := sessVars.BatchInsert && !sessVars.InTxn() && config.GetGlobalConfig().EnableBatchDML && batchSize > 0
batchInsert := sessVars.BatchInsert && !sessVars.InTxn() && variable.EnableBatchDML.Load() && batchSize > 0
memUsageOfRows := int64(0)
memUsageOfExtraCols := int64(0)
memTracker := e.memTracker
Expand Down
8 changes: 3 additions & 5 deletions executor/seqtest/seq_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1001,12 +1001,10 @@ func TestBatchInsertDelete(t *testing.T) {
r = tk.MustQuery("select count(*) from batch_insert;")
r.Check(testkit.Rows("320"))

defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.EnableBatchDML = true
})
tk.MustExec("SET GLOBAL tidb_enable_batch_dml = 1")
defer tk.MustExec("SET GLOBAL tidb_enable_batch_dml = 0")

// Change to batch inset mode and batch size to 50.
// Change to batch insert mode and batch size to 50.
tk.MustExec("set @@session.tidb_batch_insert=1;")
tk.MustExec("set @@session.tidb_dml_batch_size=50;")
tk.MustExec("insert into batch_insert (c) select * from batch_insert;")
Expand Down
7 changes: 2 additions & 5 deletions executor/write_concurrent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"math/rand"
"testing"

"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/testkit"
)

Expand All @@ -35,10 +34,8 @@ func TestBatchInsertWithOnDuplicate(t *testing.T) {
tk.MustExec(ctx, "create table duplicate_test(id int auto_increment, k1 int, primary key(id), unique key uk(k1))")
tk.MustExec(ctx, "insert into duplicate_test(k1) values(?),(?),(?),(?),(?)", permInt(5)...)

defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.EnableBatchDML = true
})
tk.MustExec(ctx, "SET GLOBAL tidb_enable_batch_dml = 1")
defer tk.MustExec(ctx, "SET GLOBAL tidb_enable_batch_dml = 0")

tk.ConcurrentRun(
3,
Expand Down
28 changes: 27 additions & 1 deletion session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,13 @@ const (
version88 = 88
// version89 adds the tables mysql.advisory_locks
version89 = 89
// version90 converts enable-batch-dml to a sysvar
version90 = 90
)

// currentBootstrapVersion is defined as a variable, so we can modify its value for testing.
// please make sure this is the largest version
var currentBootstrapVersion int64 = version89
var currentBootstrapVersion int64 = version90

var (
bootstrapVersion = []func(Session, int64){
Expand Down Expand Up @@ -712,6 +714,7 @@ var (
upgradeToVer87,
upgradeToVer88,
upgradeToVer89,
upgradeToVer90,
}
)

Expand Down Expand Up @@ -1822,6 +1825,29 @@ func upgradeToVer89(s Session, ver int64) {
doReentrantDDL(s, CreateAdvisoryLocks)
}

// importConfigOption is a one-time import.
// It is intended to be used to convert a config option to a sysvar.
// It reads the config value from the tidb-server executing the bootstrap
// (not guaranteed to be the same on all servers), and writes a message
// to the error log. The message is important since the behavior is weird
// (changes to the config file will no longer take effect past this point).
func importConfigOption(s Session, configName, svName, valStr string) {
message := fmt.Sprintf("%s is now configured by the system variable %s. One-time importing the value specified in tidb.toml file", configName, svName)
logutil.BgLogger().Warn(message, zap.String("value", valStr))
// We use insert ignore, since if its a duplicate we don't want to overwrite any user-set values.
sql := fmt.Sprintf("INSERT IGNORE INTO %s.%s (`VARIABLE_NAME`, `VARIABLE_VALUE`) VALUES ('%s', '%s')",
mysql.SystemDB, mysql.GlobalVariablesTable, svName, valStr)
mustExecute(s, sql)
}

func upgradeToVer90(s Session, ver int64) {
if ver >= version90 {
return
}
valStr := variable.BoolToOnOff(config.GetGlobalConfig().EnableBatchDML)
importConfigOption(s, "enable-batch-dml", variable.TiDBEnableBatchDML, valStr)
}

func writeOOMAction(s Session) {
comment := "oom-action is `log` by default in v3.0.x, `cancel` by default in v4.0.11+"
mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, %?) ON DUPLICATE KEY UPDATE VARIABLE_VALUE= %?`,
Expand Down
3 changes: 2 additions & 1 deletion session/nontransactional.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/opcode"
"github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
driver "github.com/pingcap/tidb/types/parser_driver"
"github.com/pingcap/tidb/util/chunk"
Expand Down Expand Up @@ -136,7 +137,7 @@ func checkConstraint(stmt *ast.NonTransactionalDeleteStmt, se Session) error {
return errors.Errorf("non-transactional DML can only run in auto-commit mode. auto-commit:%v, inTxn:%v",
se.GetSessionVars().IsAutocommit(), se.GetSessionVars().InTxn())
}
if config.GetGlobalConfig().EnableBatchDML && sessVars.DMLBatchSize > 0 && (sessVars.BatchDelete || sessVars.BatchInsert) {
if variable.EnableBatchDML.Load() && sessVars.DMLBatchSize > 0 && (sessVars.BatchDelete || sessVars.BatchInsert) {
return errors.Errorf("can't run non-transactional DML with batch-dml")
}

Expand Down
13 changes: 6 additions & 7 deletions session/nontransactional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"time"

"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/require"
tikvutil "github.com/tikv/client-go/v2/util"
Expand Down Expand Up @@ -325,15 +324,15 @@ func TestNonTransactionalDeleteCheckConstraint(t *testing.T) {
tk.MustQuery("select count(*) from t").Check(testkit.Rows("100"))
tk.MustExec("commit")

config.GetGlobalConfig().EnableBatchDML = true
tk.Session().GetSessionVars().BatchInsert = true
tk.Session().GetSessionVars().DMLBatchSize = 1
tk.MustExec("SET GLOBAL tidb_enable_batch_dml = 1")
tk.MustExec("SET tidb_batch_insert = 1")
tk.MustExec("SET tidb_dml_batch_size = 1")
err = tk.ExecToErr("batch on a limit 10 delete from t")
require.Error(t, err)
tk.MustQuery("select count(*) from t").Check(testkit.Rows("100"))
config.GetGlobalConfig().EnableBatchDML = false
tk.Session().GetSessionVars().BatchInsert = false
tk.Session().GetSessionVars().DMLBatchSize = 0
tk.MustExec("SET GLOBAL tidb_enable_batch_dml = 0")
tk.MustExec("SET tidb_batch_insert = 0")
tk.MustExec("SET tidb_dml_batch_size = 0")

err = tk.ExecToErr("batch on a limit 10 delete from t limit 10")
require.EqualError(t, err, "Non-transactional delete doesn't support limit")
Expand Down
6 changes: 6 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,12 @@ var defaultSysVars = []*SysVar{
return nil
},
},
{Scope: ScopeGlobal, Name: TiDBEnableBatchDML, Value: BoolToOnOff(DefTiDBEnableBatchDML), Type: TypeBool, SetGlobal: func(s *SessionVars, val string) error {
EnableBatchDML.Store(TiDBOptOn(val))
return nil
}, GetGlobal: func(s *SessionVars) (string, error) {
return BoolToOnOff(EnableBatchDML.Load()), nil
}},
{Scope: ScopeGlobal, Name: TiDBStatsCacheMemQuota, Value: strconv.Itoa(DefTiDBStatsCacheMemQuota),
MinValue: 0, MaxValue: MaxTiDBStatsCacheMemQuota, Type: TypeInt,
GetGlobal: func(vars *SessionVars) (string, error) {
Expand Down
4 changes: 4 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,8 @@ const (
TiDBMemQuotaBindingCache = "tidb_mem_quota_binding_cache"
// TiDBRCReadCheckTS indicates the tso optimization for read-consistency read is enabled.
TiDBRCReadCheckTS = "tidb_rc_read_check_ts"
// TiDBEnableBatchDML enables batch dml.
TiDBEnableBatchDML = "tidb_enable_batch_dml"
// TiDBStatsCacheMemQuota records stats cache quota
TiDBStatsCacheMemQuota = "tidb_stats_cache_mem_quota"
// TiDBMemQuotaAnalyze indicates the memory quota for all analyze jobs.
Expand Down Expand Up @@ -848,6 +850,7 @@ const (
DefTiDBReadStaleness = 0
DefTiDBGCMaxWaitTime = 24 * 60 * 60
DefMaxAllowedPacket uint64 = 67108864
DefTiDBEnableBatchDML = false
DefTiDBMemQuotaQuery = 1073741824 // 1GB
DefTiDBStatsCacheMemQuota = 0
MaxTiDBStatsCacheMemQuota = 1024 * 1024 * 1024 * 1024 // 1TB
Expand All @@ -862,6 +865,7 @@ var (
GlobalLogMaxDays = atomic.NewInt32(int32(config.GetGlobalConfig().Log.File.MaxDays))
QueryLogMaxLen = atomic.NewInt32(DefTiDBQueryLogMaxLen)
EnablePProfSQLCPU = atomic.NewBool(false)
EnableBatchDML = atomic.NewBool(false)
ddlReorgWorkerCounter int32 = DefTiDBDDLReorgWorkerCount
ddlReorgBatchSize int32 = DefTiDBDDLReorgBatchSize
ddlErrorCountlimit int64 = DefTiDBDDLErrorCountLimit
Expand Down

0 comments on commit bbfbe13

Please sign in to comment.