From 800351fbc8696f6a366c69c5248b5c4d527ce179 Mon Sep 17 00:00:00 2001 From: Morgan Tocker Date: Tue, 17 May 2022 09:16:10 -0600 Subject: [PATCH] session: improve bootstrap code --- session/bootstrap.go | 68 ++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/session/bootstrap.go b/session/bootstrap.go index 37d98a7155874..62aeca9596dfb 100644 --- a/session/bootstrap.go +++ b/session/bootstrap.go @@ -1944,6 +1944,12 @@ func doDDLWorks(s Session) { mustExecute(s, CreateAdvisoryLocks) } +// inTestSuite checks if we are bootstrapping in the context of tests. +// There are some historical differences in behavior between tests and non-tests. +func inTestSuite() bool { + return flag.Lookup("test.v") != nil || flag.Lookup("check.v") != nil +} + // doDMLWorks executes DML statements in bootstrap stage. // All the statements run in a single transaction. // TODO: sanitize. @@ -1964,47 +1970,47 @@ func doDMLWorks(s Session) { ("%", "root", "", "mysql_native_password", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "N", "Y", "Y", "Y", "Y", "Y")`) } - // Init global system variables table. + // For GLOBAL scoped system variables, insert the initial value + // into the mysql.global_variables table. This is only run on initial + // bootstrap, and in some cases we will use a different default value + // for new installs versus existing installs. + values := make([]string, 0, len(variable.GetSysVars())) for k, v := range variable.GetSysVars() { - // Only global variables should be inserted. - if v.HasGlobalScope() { - vVal := v.Value - if v.Name == variable.TiDBTxnMode && config.GetGlobalConfig().Store == "tikv" { + if !v.HasGlobalScope() { + continue + } + vVal := v.Value + switch v.Name { + case variable.TiDBTxnMode: + if config.GetGlobalConfig().Store == "tikv" { vVal = "pessimistic" } - if v.Name == variable.TiDBRowFormatVersion { - vVal = strconv.Itoa(variable.DefTiDBRowFormatV2) - } - if v.Name == variable.TiDBPartitionPruneMode { - vVal = variable.DefTiDBPartitionPruneMode - if flag.Lookup("test.v") != nil || flag.Lookup("check.v") != nil || config.CheckTableBeforeDrop { - // enable Dynamic Prune by default in test case. - vVal = string(variable.Dynamic) - } - } - if v.Name == variable.TiDBEnableChangeMultiSchema { - vVal = variable.Off - if flag.Lookup("test.v") != nil || flag.Lookup("check.v") != nil { - // enable change multi schema in test case for compatibility with old cases. - vVal = variable.On - } - } - if v.Name == variable.TiDBEnableAsyncCommit && config.GetGlobalConfig().Store == "tikv" { + case variable.TiDBEnableAsyncCommit, variable.TiDBEnable1PC: + if config.GetGlobalConfig().Store == "tikv" { vVal = variable.On } - if v.Name == variable.TiDBEnable1PC && config.GetGlobalConfig().Store == "tikv" { - vVal = variable.On + case variable.TiDBPartitionPruneMode: + if inTestSuite() || config.CheckTableBeforeDrop { + vVal = string(variable.Dynamic) } - if v.Name == variable.TiDBEnableMutationChecker { + case variable.TiDBEnableChangeMultiSchema: + if inTestSuite() { vVal = variable.On } - if v.Name == variable.TiDBTxnAssertionLevel { - vVal = variable.AssertionFastStr - } - value := fmt.Sprintf(`("%s", "%s")`, strings.ToLower(k), vVal) - values = append(values, value) + // For the following sysvars, we change the default + // FOR NEW INSTALLS ONLY. In most cases you don't want to do this. + // It is better to change the value in the Sysvar struct, so that + // all installs will have the same value. + case variable.TiDBRowFormatVersion: + vVal = strconv.Itoa(variable.DefTiDBRowFormatV2) + case variable.TiDBTxnAssertionLevel: + vVal = variable.AssertionFastStr + case variable.TiDBEnableMutationChecker: + vVal = variable.On } + value := fmt.Sprintf(`("%s", "%s")`, strings.ToLower(k), vVal) + values = append(values, value) } sql := fmt.Sprintf("INSERT HIGH_PRIORITY INTO %s.%s VALUES %s;", mysql.SystemDB, mysql.GlobalVariablesTable, strings.Join(values, ", "))