Skip to content

Commit

Permalink
session: display mDDLTableVersion in mysql.tidb table (#47900)
Browse files Browse the repository at this point in the history
close #39778
  • Loading branch information
lkshminarayanan authored Oct 26, 2023
1 parent fd08ae3 commit a565692
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
37 changes: 36 additions & 1 deletion pkg/session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,8 @@ const (
// The variable name in mysql.tidb table and it records the default value of
// oom-action when upgrade from v3.0.x to v4.0.11+.
tidbDefOOMAction = "default_oom_action"
// The variable name in mysql.tidb table and it records the current DDLTableVersion
tidbDDLTableVersion = "ddl_table_version"
// Const for TiDB server version 2.
version2 = 2
version3 = 3
Expand Down Expand Up @@ -1014,11 +1016,15 @@ const (
// version 177
// add `mysql.dist_framework_meta`
version177 = 177

// version 178
// write mDDLTableVersion into `mysql.tidb` table
version178 = 178
)

// 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 = version177
var currentBootstrapVersion int64 = version178

// 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 @@ -1171,6 +1177,7 @@ var (
upgradeToVer175,
upgradeToVer176,
upgradeToVer177,
upgradeToVer178,
}
)

Expand Down Expand Up @@ -2846,6 +2853,32 @@ func upgradeToVer177(s Session, ver int64) {
}
}

// writeDDLTableVersion writes mDDLTableVersion into mysql.tidb
func writeDDLTableVersion(s Session) {
var err error
var ddlTableVersion meta.DDLTableVersion
err = kv.RunInNewTxn(kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap), s.GetStore(), true, func(ctx context.Context, txn kv.Transaction) error {
t := meta.NewMeta(txn)
ddlTableVersion, err = t.CheckDDLTableVersion()
return err
})
terror.MustNil(err)
mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, "DDL Table Version. Do not delete.") ON DUPLICATE KEY UPDATE VARIABLE_VALUE= %?`,
mysql.SystemDB,
mysql.TiDBTable,
tidbDDLTableVersion,
ddlTableVersion,
ddlTableVersion,
)
}

func upgradeToVer178(s Session, ver int64) {
if ver >= version178 {
return
}
writeDDLTableVersion(s)
}

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 Expand Up @@ -3102,6 +3135,8 @@ func doDMLWorks(s Session) {

writeStmtSummaryVars(s)

writeDDLTableVersion(s)

ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap)
_, err := s.ExecuteInternal(ctx, "COMMIT")
if err != nil {
Expand Down
68 changes: 68 additions & 0 deletions pkg/session/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2118,3 +2118,71 @@ func TestTiDBUpgradeToVer177(t *testing.T) {
MustExec(t, seV176, "SELECT * from mysql.dist_framework_meta")
dom.Close()
}

func TestWriteDDLTableVersionToMySQLTiDB(t *testing.T) {
ctx := context.Background()
store, dom := CreateStoreAndBootstrap(t)
defer func() { require.NoError(t, store.Close()) }()

txn, err := store.Begin()
require.NoError(t, err)
m := meta.NewMeta(txn)
ddlTableVer, err := m.CheckDDLTableVersion()
require.NoError(t, err)

// Verify that 'ddl_table_version' has been set to the correct value
se := CreateSessionAndSetID(t, store)
r := MustExecToRecodeSet(t, se, fmt.Sprintf(`SELECT VARIABLE_VALUE from mysql.TiDB where VARIABLE_NAME='%s'`, tidbDDLTableVersion))
req := r.NewChunk(nil)
err = r.Next(ctx, req)
require.NoError(t, err)
require.Equal(t, 1, req.NumRows())
require.Equal(t, []byte(fmt.Sprintf("%d", ddlTableVer)), req.GetRow(0).GetBytes(0))
require.NoError(t, r.Close())
dom.Close()
}

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

txn, err := store.Begin()
require.NoError(t, err)
m := meta.NewMeta(txn)
ddlTableVer, err := m.CheckDDLTableVersion()
require.NoError(t, err)

// bootstrap as version177
ver177 := version177
seV177 := CreateSessionAndSetID(t, store)
err = m.FinishBootstrap(int64(ver177))
require.NoError(t, err)
MustExec(t, seV177, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver177))
// remove the ddl_table_version entry from mysql.tidb table
MustExec(t, seV177, fmt.Sprintf("delete from mysql.tidb where VARIABLE_NAME='%s'", tidbDDLTableVersion))
err = txn.Commit(ctx)
require.NoError(t, err)
unsetStoreBootstrapped(store.UUID())
ver, err := getBootstrapVersion(seV177)
require.NoError(t, err)
require.Equal(t, int64(ver177), ver)

// upgrade to current version
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)

// check if the DDLTableVersion has been set in the `mysql.tidb` table during upgrade
r := MustExecToRecodeSet(t, seCurVer, fmt.Sprintf(`SELECT VARIABLE_VALUE from mysql.TiDB where VARIABLE_NAME='%s'`, tidbDDLTableVersion))
req := r.NewChunk(nil)
err = r.Next(ctx, req)
require.NoError(t, err)
require.Equal(t, 1, req.NumRows())
require.Equal(t, []byte(fmt.Sprintf("%d", ddlTableVer)), req.GetRow(0).GetBytes(0))
require.NoError(t, r.Close())
}

0 comments on commit a565692

Please sign in to comment.