Skip to content

Commit

Permalink
planner: enable non-prep plan cache by default (#43145)
Browse files Browse the repository at this point in the history
ref #36598
  • Loading branch information
qw4990 authored Apr 19, 2023
1 parent 8eae738 commit cd33faf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
25 changes: 24 additions & 1 deletion session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,11 +857,14 @@ const (
// version 141 set the value of `tidb_session_plan_cache_size` to "tidb_prepared_plan_cache_size" if there is no `tidb_session_plan_cache_size`.
// This will only happens when we upgrade a cluster before 7.1.
version141 = 141
// version 142 insert "tidb_enable_non_prepared_plan_cache|0" to mysql.GLOBAL_VARIABLES if there is no tidb_enable_non_prepared_plan_cache.
// This will only happens when we upgrade a cluster before 6.5.
version142 = 142
)

// 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 = version140
var currentBootstrapVersion int64 = version142

// DDL owner key's expired time is ManagerSessionTTL seconds, we should wait the time and give more time to have a chance to finish it.
var internalSQLTimeout = owner.ManagerSessionTTL + 15
Expand Down Expand Up @@ -991,6 +994,7 @@ var (
upgradeToVer139,
upgradeToVer140,
upgradeToVer141,
upgradeToVer142,
}
)

Expand Down Expand Up @@ -2467,6 +2471,25 @@ func upgradeToVer141(s Session, ver int64) {
mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBSessionPlanCacheSize, val)
}

func upgradeToVer142(s Session, ver int64) {
if ver >= version142 {
return
}
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap)
rs, err := s.ExecuteInternal(ctx, "SELECT VARIABLE_VALUE FROM %n.%n WHERE VARIABLE_NAME=%?;",
mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableNonPreparedPlanCache)
terror.MustNil(err)
req := rs.NewChunk(nil)
err = rs.Next(ctx, req)
terror.MustNil(err)
if req.NumRows() != 0 {
return
}

mustExecute(s, "INSERT HIGH_PRIORITY IGNORE INTO %n.%n VALUES (%?, %?);",
mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableNonPreparedPlanCache, variable.Off)
}

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
12 changes: 10 additions & 2 deletions session/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1903,12 +1903,20 @@ func TestTiDBNonPrepPlanCacheUpgradeFrom540To700(t *testing.T) {
err = txn.Commit(context.Background())
require.NoError(t, err)
mustExec(t, seV54, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver54))
mustExec(t, seV54, fmt.Sprintf("delete from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBEnableNonPreparedPlanCache))
mustExec(t, seV54, "commit")
unsetStoreBootstrapped(store.UUID())
ver, err := getBootstrapVersion(seV54)
require.NoError(t, err)
require.Equal(t, int64(ver54), ver)

// We are now in 5.4, check TiDBCostModelVersion should not exist.
res := mustExecToRecodeSet(t, seV54, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBEnableNonPreparedPlanCache))
chk := res.NewChunk(nil)
err = res.Next(ctx, chk)
require.NoError(t, err)
require.Equal(t, 0, chk.NumRows())

// Upgrade to 7.0
domCurVer, err := BootstrapSession(store)
require.NoError(t, err)
Expand All @@ -1919,8 +1927,8 @@ func TestTiDBNonPrepPlanCacheUpgradeFrom540To700(t *testing.T) {
require.Equal(t, currentBootstrapVersion, ver)

// We are now in 7.0
res := mustExecToRecodeSet(t, seCurVer, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBEnableNonPreparedPlanCache))
chk := res.NewChunk(nil)
res = mustExecToRecodeSet(t, seCurVer, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBEnableNonPreparedPlanCache))
chk = res.NewChunk(nil)
err = res.Next(ctx, chk)
require.NoError(t, err)
require.Equal(t, 1, chk.NumRows())
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ const (
DefTiDBEnableFastReorg = true
DefTiDBDDLDiskQuota = 100 * 1024 * 1024 * 1024 // 100GB
DefExecutorConcurrency = 5
DefTiDBEnableNonPreparedPlanCache = false
DefTiDBEnableNonPreparedPlanCache = true
DefTiDBNonPreparedPlanCacheSize = 100
DefTiDBPlanCacheMaxPlanSize = 2 * size.MB
DefTiDBEnableTiFlashReadForWriteStmt = true
Expand Down

0 comments on commit cd33faf

Please sign in to comment.