Skip to content

Commit

Permalink
cherry pick pingcap#30743 to release-5.3
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
winoros authored and ti-srebot committed Dec 15, 2021
1 parent 83b273a commit 096f104
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
48 changes: 48 additions & 0 deletions session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,23 @@ const (
version77 = 77
// version78 updates mysql.stats_buckets.lower_bound, mysql.stats_buckets.upper_bound and mysql.stats_histograms.last_analyze_pos from BLOB to LONGBLOB.
version78 = 78
<<<<<<< HEAD
=======
// version79 adds the mysql.table_cache_meta table
version79 = 79
// version80 fixes the issue https://github.com/pingcap/tidb/issues/25422.
// If the TiDB upgrading from the 4.x to a newer version, we keep the tidb_analyze_version to 1.
version80 = 80
>>>>>>> d660e483c... sessionctx: fix the value of analyze_version when upgrading 4.x to 5.… (#30743)
)

// 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 = version78
=======
var currentBootstrapVersion int64 = version80
>>>>>>> d660e483c... sessionctx: fix the value of analyze_version when upgrading 4.x to 5.… (#30743)

var (
bootstrapVersion = []func(Session, int64){
Expand Down Expand Up @@ -614,6 +626,11 @@ var (
upgradeToVer76,
upgradeToVer77,
upgradeToVer78,
<<<<<<< HEAD
=======
upgradeToVer79,
upgradeToVer80,
>>>>>>> d660e483c... sessionctx: fix the value of analyze_version when upgrading 4.x to 5.… (#30743)
}
)

Expand Down Expand Up @@ -1612,6 +1629,37 @@ func upgradeToVer78(s Session, ver int64) {
doReentrantDDL(s, "ALTER TABLE mysql.stats_histograms MODIFY last_analyze_pos LONGBLOB DEFAULT NULL")
}

<<<<<<< HEAD
=======
func upgradeToVer79(s Session, ver int64) {
if ver >= version79 {
return
}
doReentrantDDL(s, CreateTableCacheMetaTable)
}

func upgradeToVer80(s Session, ver int64) {
if ver >= version80 {
return
}
// Check if tidb_analyze_version exists in mysql.GLOBAL_VARIABLES.
// If not, insert "tidb_analyze_version | 1" since this is the old behavior before we introduce this variable.
ctx := context.Background()
rs, err := s.ExecuteInternal(ctx, "SELECT VARIABLE_VALUE FROM %n.%n WHERE VARIABLE_NAME=%?;",
mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBAnalyzeVersion)
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.TiDBAnalyzeVersion, 1)
}

>>>>>>> d660e483c... sessionctx: fix the value of analyze_version when upgrading 4.x to 5.… (#30743)
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
49 changes: 49 additions & 0 deletions session/bootstrap_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,3 +879,52 @@ func TestReferencesPrivilegeOnColumn(t *testing.T) {
mustExec(t, se, "create table t1 (a int)")
mustExec(t, se, "GRANT select (a), update (a),insert(a), references(a) on t1 to issue28531")
}

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

// Upgrade from 3.0.0 to 5.1+ or above.
ver300 := 33
seV3 := createSessionAndSetID(t, store)
txn, err := store.Begin()
require.NoError(t, err)
m := meta.NewMeta(txn)
err = m.FinishBootstrap(int64(ver300))
require.NoError(t, err)
err = txn.Commit(context.Background())
require.NoError(t, err)
mustExec(t, seV3, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver300))
mustExec(t, seV3, fmt.Sprintf("delete from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBAnalyzeVersion))
mustExec(t, seV3, "commit")
unsetStoreBootstrapped(store.UUID())
ver, err := getBootstrapVersion(seV3)
require.NoError(t, err)
require.Equal(t, int64(ver300), ver)

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

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 version no lower than 5.x, tidb_enable_index_merge should be 1.
res = mustExec(t, seCurVer, "select @@tidb_analyze_version")
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())
require.Equal(t, "1", row.GetString(0))
}

0 comments on commit 096f104

Please sign in to comment.