Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#41438
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
XuHuaiyu authored and ti-chi-bot committed Feb 16, 2023
1 parent 5f9f3c9 commit ef7436a
Show file tree
Hide file tree
Showing 2 changed files with 310 additions and 0 deletions.
68 changes: 68 additions & 0 deletions session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,11 +734,34 @@ const (
version108 = 108
// version109 sets tidb_enable_gc_aware_memory_track to off when a cluster upgrades from some version lower than v6.5.0.
version109 = 109
<<<<<<< HEAD
=======
// version110 sets tidb_enable_gc_aware_memory_track to off when a cluster upgrades from some version lower than v6.5.0.
version110 = 110
// version111 adds the table tidb_ttl_task and tidb_ttl_job_history
version111 = 111
// version112 modifies the view tidb_mdl_view
version112 = 112
// ...
// [version113, version119] is the version range reserved for patches of 6.5.x
// ...
// version113 sets tidb_server_memory_limit to "80%"
version113 = 113
// version120 modifies the following global variables default value:
// - foreign_key_checks: off -> on
// - tidb_enable_foreign_key: off -> on
// - tidb_store_batch_size: 0 -> 4
version120 = 120
>>>>>>> d0919b907a5 (session: set tidb_server_memory_limit to defValue during bootstrap (#41438))
)

// currentBootstrapVersion is defined as a variable, so we can modify its value for testing.
// please make sure this is the largest version
<<<<<<< HEAD
var currentBootstrapVersion int64 = version109
=======
var currentBootstrapVersion int64 = version120
>>>>>>> d0919b907a5 (session: set tidb_server_memory_limit to defValue during bootstrap (#41438))

// 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 @@ -852,6 +875,14 @@ var (
upgradeToVer107,
upgradeToVer108,
upgradeToVer109,
<<<<<<< HEAD
=======
upgradeToVer110,
upgradeToVer111,
upgradeToVer112,
upgradeToVer113,
upgradeToVer120,
>>>>>>> d0919b907a5 (session: set tidb_server_memory_limit to defValue during bootstrap (#41438))
}
)

Expand Down Expand Up @@ -2203,6 +2234,43 @@ func upgradeToVer109(s Session, ver int64) {
mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableGCAwareMemoryTrack, 0)
}

<<<<<<< HEAD
=======
func upgradeToVer111(s Session, ver int64) {
if ver >= version111 {
return
}
doReentrantDDL(s, CreateTTLTask)
doReentrantDDL(s, CreateTTLJobHistory)
}

func upgradeToVer112(s Session, ver int64) {
if ver >= version112 {
return
}
doReentrantDDL(s, CreateMDLView)
}

func upgradeToVer113(s Session, ver int64) {
if ver >= version113 {
return
}
mustExecute(s, "UPDATE HIGH_PRIORITY %n.%n set VARIABLE_VALUE = %? where VARIABLE_NAME = %? and VARIABLE_VALUE = %?;",
mysql.SystemDB, mysql.GlobalVariablesTable, variable.DefTiDBServerMemoryLimit, variable.TiDBServerMemoryLimit, "0")
}

func upgradeToVer120(s Session, ver int64) {
if ver >= version120 {
return
}
mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.ForeignKeyChecks, variable.On)
mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableForeignKey, variable.On)
mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableHistoricalStats, variable.On)
mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnablePlanReplayerCapture, variable.On)
mustExecute(s, "UPDATE HIGH_PRIORITY %n.%n SET VARIABLE_VALUE = %? WHERE VARIABLE_NAME = %? AND VARIABLE_VALUE = %?;", mysql.SystemDB, mysql.GlobalVariablesTable, "4", variable.TiDBStoreBatchSize, "0")
}

>>>>>>> d0919b907a5 (session: set tidb_server_memory_limit to defValue during bootstrap (#41438))
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
242 changes: 242 additions & 0 deletions session/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1328,3 +1328,245 @@ func TestTiDBGCAwareUpgradeFrom630To650(t *testing.T) {
require.Equal(t, 2, row.Len())
require.Equal(t, "0", row.GetString(1))
}
<<<<<<< HEAD
=======

func TestTiDBServerMemoryLimitUpgradeTo651_1(t *testing.T) {
ctx := context.Background()
store, _ := createStoreAndBootstrap(t)
defer func() { require.NoError(t, store.Close()) }()

// upgrade from 6.5.0 to 6.5.1+.
ver112 := version112
seV112 := createSessionAndSetID(t, store)
txn, err := store.Begin()
require.NoError(t, err)
m := meta.NewMeta(txn)
err = m.FinishBootstrap(int64(ver112))
require.NoError(t, err)
err = txn.Commit(context.Background())
require.NoError(t, err)
mustExec(t, seV112, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver112))
mustExec(t, seV112, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "0", variable.TiDBServerMemoryLimit))
mustExec(t, seV112, "commit")
unsetStoreBootstrapped(store.UUID())
ver, err := getBootstrapVersion(seV112)
require.NoError(t, err)
require.Equal(t, int64(ver112), ver)

// We are now in 6.5.0, tidb_server_memory_limit is 0.
res := mustExecToRecodeSet(t, seV112, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBServerMemoryLimit))
chk := res.NewChunk(nil)
err = res.Next(ctx, chk)
require.NoError(t, err)
require.Equal(t, 1, chk.NumRows())
row := chk.GetRow(0)
require.Equal(t, 2, row.Len())
require.Equal(t, "0", row.GetString(1))

// Upgrade to 6.5.1+.
domCurVer, err := BootstrapSession(store)
require.NoError(t, err)
defer domCurVer.Close()
seCurVer := createSessionAndSetID(t, store)
ver, err = getBootstrapVersion(seCurVer)
require.NoError(t, err)
require.Equal(t, currentBootstrapVersion, ver)

// We are now in 6.5.1+.
res = mustExecToRecodeSet(t, seCurVer, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBServerMemoryLimit))
chk = res.NewChunk(nil)
err = res.Next(ctx, chk)
require.NoError(t, err)
require.Equal(t, 1, chk.NumRows())
row = chk.GetRow(0)
require.Equal(t, 2, row.Len())
require.Equal(t, variable.DefTiDBServerMemoryLimit, row.GetString(1))
}

func TestTiDBServerMemoryLimitUpgradeTo651_2(t *testing.T) {
ctx := context.Background()
store, _ := createStoreAndBootstrap(t)
defer func() { require.NoError(t, store.Close()) }()

// upgrade from 6.5.0 to 6.5.1+.
ver112 := version112
seV112 := createSessionAndSetID(t, store)
txn, err := store.Begin()
require.NoError(t, err)
m := meta.NewMeta(txn)
err = m.FinishBootstrap(int64(ver112))
require.NoError(t, err)
err = txn.Commit(context.Background())
require.NoError(t, err)
mustExec(t, seV112, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver112))
mustExec(t, seV112, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "70%", variable.TiDBServerMemoryLimit))
mustExec(t, seV112, "commit")
unsetStoreBootstrapped(store.UUID())
ver, err := getBootstrapVersion(seV112)
require.NoError(t, err)
require.Equal(t, int64(ver112), ver)

// We are now in 6.5.0, tidb_server_memory_limit is "70%".
res := mustExecToRecodeSet(t, seV112, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBServerMemoryLimit))
chk := res.NewChunk(nil)
err = res.Next(ctx, chk)
require.NoError(t, err)
require.Equal(t, 1, chk.NumRows())
row := chk.GetRow(0)
require.Equal(t, 2, row.Len())
require.Equal(t, "70%", row.GetString(1))

// Upgrade to 6.5.1+.
domCurVer, err := BootstrapSession(store)
require.NoError(t, err)
defer domCurVer.Close()
seCurVer := createSessionAndSetID(t, store)
ver, err = getBootstrapVersion(seCurVer)
require.NoError(t, err)
require.Equal(t, currentBootstrapVersion, ver)

// We are now in 6.5.1+.
res = mustExecToRecodeSet(t, seCurVer, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBServerMemoryLimit))
chk = res.NewChunk(nil)
err = res.Next(ctx, chk)
require.NoError(t, err)
require.Equal(t, 1, chk.NumRows())
row = chk.GetRow(0)
require.Equal(t, 2, row.Len())
require.Equal(t, "70%", row.GetString(1))
}

func TestTiDBGlobalVariablesDefaultValueUpgradeFrom630To660(t *testing.T) {
ctx := context.Background()
store, _ := createStoreAndBootstrap(t)
defer func() { require.NoError(t, store.Close()) }()

// upgrade from 6.3.0 to 6.6.0.
ver630 := version93
seV630 := createSessionAndSetID(t, store)
txn, err := store.Begin()
require.NoError(t, err)
m := meta.NewMeta(txn)
err = m.FinishBootstrap(int64(ver630))
require.NoError(t, err)
err = txn.Commit(context.Background())
require.NoError(t, err)
mustExec(t, seV630, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver630))
mustExec(t, seV630, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "OFF", variable.TiDBEnableForeignKey))
mustExec(t, seV630, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "OFF", variable.ForeignKeyChecks))
mustExec(t, seV630, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "OFF", variable.TiDBEnableHistoricalStats))
mustExec(t, seV630, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "OFF", variable.TiDBEnablePlanReplayerCapture))
mustExec(t, seV630, "commit")
unsetStoreBootstrapped(store.UUID())
ver, err := getBootstrapVersion(seV630)
require.NoError(t, err)
require.Equal(t, int64(ver630), ver)

// We are now in 6.3.0.
upgradeVars := []string{variable.TiDBEnableForeignKey, variable.ForeignKeyChecks, variable.TiDBEnableHistoricalStats, variable.TiDBEnablePlanReplayerCapture}
varsValueList := []string{"OFF", "OFF", "OFF", "OFF"}
for i := range upgradeVars {
res := mustExecToRecodeSet(t, seV630, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", upgradeVars[i]))
chk := res.NewChunk(nil)
err = res.Next(ctx, chk)
require.NoError(t, err)
require.Equal(t, 1, chk.NumRows())
row := chk.GetRow(0)
require.Equal(t, 2, row.Len())
require.Equal(t, varsValueList[i], row.GetString(1))
}

// Upgrade to 6.6.0.
domCurVer, err := BootstrapSession(store)
require.NoError(t, err)
defer domCurVer.Close()
seV660 := createSessionAndSetID(t, store)
ver, err = getBootstrapVersion(seV660)
require.NoError(t, err)
require.Equal(t, currentBootstrapVersion, ver)

// We are now in 6.6.0.
varsValueList = []string{"ON", "ON", "ON", "ON"}
for i := range upgradeVars {
res := mustExecToRecodeSet(t, seV660, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", upgradeVars[i]))
chk := res.NewChunk(nil)
err = res.Next(ctx, chk)
require.NoError(t, err)
require.Equal(t, 1, chk.NumRows())
row := chk.GetRow(0)
require.Equal(t, 2, row.Len())
require.Equal(t, varsValueList[i], row.GetString(1))
}
}

func TestTiDBStoreBatchSizeUpgradeFrom650To660(t *testing.T) {
for i := 0; i < 2; i++ {
func() {
ctx := context.Background()
store, dom := createStoreAndBootstrap(t)
defer func() { require.NoError(t, store.Close()) }()

// upgrade from 6.5 to 6.6.
ver65 := version112
seV65 := createSessionAndSetID(t, store)
txn, err := store.Begin()
require.NoError(t, err)
m := meta.NewMeta(txn)
err = m.FinishBootstrap(int64(ver65))
require.NoError(t, err)
err = txn.Commit(context.Background())
require.NoError(t, err)
mustExec(t, seV65, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver65))
mustExec(t, seV65, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "0", variable.TiDBStoreBatchSize))
mustExec(t, seV65, "commit")
unsetStoreBootstrapped(store.UUID())
ver, err := getBootstrapVersion(seV65)
require.NoError(t, err)
require.Equal(t, int64(ver65), ver)

// We are now in 6.5, tidb_store_batch_size is 0.
res := mustExecToRecodeSet(t, seV65, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBStoreBatchSize))
chk := res.NewChunk(nil)
err = res.Next(ctx, chk)
require.NoError(t, err)
require.Equal(t, 1, chk.NumRows())
row := chk.GetRow(0)
require.Equal(t, 2, row.Len())
require.Equal(t, "0", row.GetString(1))
res.Close()

if i == 0 {
// For the first time, We set tidb_store_batch_size to 1.
// And after upgrade to 6.6, tidb_store_batch_size should be 1.
// For the second it should be the latest default value.
mustExec(t, seV65, "set global tidb_store_batch_size = 1")
}
dom.Close()
// Upgrade to 6.6.
domCurVer, err := BootstrapSession(store)
require.NoError(t, err)
defer domCurVer.Close()
seCurVer := createSessionAndSetID(t, store)
ver, err = getBootstrapVersion(seCurVer)
require.NoError(t, err)
require.Equal(t, currentBootstrapVersion, ver)

// We are now in 6.6.
res = mustExecToRecodeSet(t, seCurVer, "select @@tidb_store_batch_size")
chk = res.NewChunk(nil)
err = res.Next(ctx, chk)
require.NoError(t, err)
require.Equal(t, 1, chk.NumRows())
row = chk.GetRow(0)
require.Equal(t, 1, row.Len())
if i == 0 {
require.Equal(t, "1", row.GetString(0))
} else {
require.Equal(t, "4", row.GetString(0))
}
res.Close()
}()
}
}
>>>>>>> d0919b907a5 (session: set tidb_server_memory_limit to defValue during bootstrap (#41438))

0 comments on commit ef7436a

Please sign in to comment.