From a565692dab68be58b12c582470e241e1169f2022 Mon Sep 17 00:00:00 2001 From: Lakshmi Narayanan Sreethar <1407970+lkshminarayanan@users.noreply.github.com> Date: Thu, 26 Oct 2023 15:32:06 +0530 Subject: [PATCH] session: display mDDLTableVersion in mysql.tidb table (#47900) close pingcap/tidb#39778 --- pkg/session/bootstrap.go | 37 ++++++++++++++++++- pkg/session/bootstrap_test.go | 68 +++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/pkg/session/bootstrap.go b/pkg/session/bootstrap.go index 01b19a458eb86..27799f7b1a700 100644 --- a/pkg/session/bootstrap.go +++ b/pkg/session/bootstrap.go @@ -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 @@ -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 @@ -1171,6 +1177,7 @@ var ( upgradeToVer175, upgradeToVer176, upgradeToVer177, + upgradeToVer178, } ) @@ -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= %?`, @@ -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 { diff --git a/pkg/session/bootstrap_test.go b/pkg/session/bootstrap_test.go index 6b37120bec02b..a7c9965001eac 100644 --- a/pkg/session/bootstrap_test.go +++ b/pkg/session/bootstrap_test.go @@ -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()) +}