Skip to content

Commit

Permalink
session: fix upgrade (#40182)
Browse files Browse the repository at this point in the history
close #41502
  • Loading branch information
chrysan authored Feb 16, 2023
1 parent d579dd1 commit 5df1913
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
2 changes: 1 addition & 1 deletion server/tidb_library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestMemoryLeak(t *testing.T) {
runtime.ReadMemStats(&memStat)
// before the fix, initAndCloseTiDB for 20 times will cost 900 MB memory, so we test for a quite loose upper bound.
if syncutil.EnableDeadlock {
require.Less(t, memStat.HeapInuse-oldHeapInUse, uint64(1100*units.MiB))
require.Less(t, memStat.HeapInuse-oldHeapInUse, uint64(1500*units.MiB))
} else {
require.Less(t, memStat.HeapInuse-oldHeapInUse, uint64(300*units.MiB))
}
Expand Down
64 changes: 32 additions & 32 deletions session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,29 +786,29 @@ const (
version107 = 107
// version108 adds the table tidb_ttl_table_status
version108 = 108
// version109 add column source to mysql.stats_meta_history
// version109 sets tidb_enable_gc_aware_memory_track to off when a cluster upgrades from some version lower than v6.5.0.
version109 = 109
// 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
// [version110, version129] 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:
// version130 add column source to mysql.stats_meta_history
version130 = 130
// version131 adds the table tidb_ttl_task and tidb_ttl_job_history
version131 = 131
// version132 modifies the view tidb_mdl_view
version132 = 132
// version133 sets tidb_server_memory_limit to "80%"
version133 = 133
// version134 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
version134 = 134
)

// 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 = version120
var currentBootstrapVersion int64 = version134

// 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 @@ -925,11 +925,11 @@ var (
upgradeToVer107,
upgradeToVer108,
upgradeToVer109,
upgradeToVer110,
upgradeToVer111,
upgradeToVer112,
upgradeToVer113,
upgradeToVer120,
upgradeToVer130,
upgradeToVer131,
upgradeToVer132,
upgradeToVer133,
upgradeToVer134,
}
)

Expand Down Expand Up @@ -2266,47 +2266,47 @@ func upgradeToVer108(s Session, ver int64) {
doReentrantDDL(s, CreateTTLTableStatus)
}

// For users that upgrade TiDB from a 6.2-6.4 version, we want to disable tidb gc_aware_memory_track by default.
func upgradeToVer109(s Session, ver int64) {
if ver >= version109 {
return
}
doReentrantDDL(s, "ALTER TABLE mysql.stats_meta_history ADD COLUMN IF NOT EXISTS `source` varchar(40) NOT NULL after `version`;")
mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);",
mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableGCAwareMemoryTrack, 0)
}

// For users that upgrade TiDB from a 6.2-6.4 version, we want to disable tidb gc_aware_memory_track by default.
func upgradeToVer110(s Session, ver int64) {
if ver >= version110 {
func upgradeToVer130(s Session, ver int64) {
if ver >= version130 {
return
}
mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);",
mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableGCAwareMemoryTrack, 0)
doReentrantDDL(s, "ALTER TABLE mysql.stats_meta_history ADD COLUMN IF NOT EXISTS `source` varchar(40) NOT NULL after `version`;")
}

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

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

func upgradeToVer113(s Session, ver int64) {
if ver >= version113 {
func upgradeToVer133(s Session, ver int64) {
if ver >= version133 {
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 {
func upgradeToVer134(s Session, ver int64) {
if ver >= version134 {
return
}
mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.ForeignKeyChecks, variable.On)
Expand Down
38 changes: 19 additions & 19 deletions session/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1424,25 +1424,25 @@ func TestTiDBServerMemoryLimitUpgradeTo651_1(t *testing.T) {
defer func() { require.NoError(t, store.Close()) }()

// upgrade from 6.5.0 to 6.5.1+.
ver112 := version112
seV112 := createSessionAndSetID(t, store)
ver132 := version132
seV132 := createSessionAndSetID(t, store)
txn, err := store.Begin()
require.NoError(t, err)
m := meta.NewMeta(txn)
err = m.FinishBootstrap(int64(ver112))
err = m.FinishBootstrap(int64(ver132))
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")
mustExec(t, seV132, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver132))
mustExec(t, seV132, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "0", variable.TiDBServerMemoryLimit))
mustExec(t, seV132, "commit")
unsetStoreBootstrapped(store.UUID())
ver, err := getBootstrapVersion(seV112)
ver, err := getBootstrapVersion(seV132)
require.NoError(t, err)
require.Equal(t, int64(ver112), ver)
require.Equal(t, int64(ver132), 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))
res := mustExecToRecodeSet(t, seV132, 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)
Expand Down Expand Up @@ -1477,25 +1477,25 @@ func TestTiDBServerMemoryLimitUpgradeTo651_2(t *testing.T) {
defer func() { require.NoError(t, store.Close()) }()

// upgrade from 6.5.0 to 6.5.1+.
ver112 := version112
seV112 := createSessionAndSetID(t, store)
ver132 := version132
seV132 := createSessionAndSetID(t, store)
txn, err := store.Begin()
require.NoError(t, err)
m := meta.NewMeta(txn)
err = m.FinishBootstrap(int64(ver112))
err = m.FinishBootstrap(int64(ver132))
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")
mustExec(t, seV132, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver132))
mustExec(t, seV132, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "70%", variable.TiDBServerMemoryLimit))
mustExec(t, seV132, "commit")
unsetStoreBootstrapped(store.UUID())
ver, err := getBootstrapVersion(seV112)
ver, err := getBootstrapVersion(seV132)
require.NoError(t, err)
require.Equal(t, int64(ver112), ver)
require.Equal(t, int64(ver132), 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))
res := mustExecToRecodeSet(t, seV132, 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)
Expand Down Expand Up @@ -1595,7 +1595,7 @@ func TestTiDBStoreBatchSizeUpgradeFrom650To660(t *testing.T) {
defer func() { require.NoError(t, store.Close()) }()

// upgrade from 6.5 to 6.6.
ver65 := version112
ver65 := version132
seV65 := createSessionAndSetID(t, store)
txn, err := store.Begin()
require.NoError(t, err)
Expand Down

0 comments on commit 5df1913

Please sign in to comment.