Skip to content

Commit

Permalink
config, session: promise the compatibility of oom-action when upgradi…
Browse files Browse the repository at this point in the history
  • Loading branch information
XuHuaiyu authored and wshwsh12 committed Dec 30, 2020
1 parent 496f969 commit acb3f99
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 3 deletions.
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,10 @@ func isAllDeprecatedConfigItems(items []string) bool {
// is set by the user.
var IsMemoryQuotaQuerySetByUser bool

// IsOOMActionSetByUser indicates whether the config item mem-action is set by
// the user.
var IsOOMActionSetByUser bool

// InitializeConfig initialize the global config handler.
// The function enforceCmdArgs is used to merge the config file with command arguments:
// For example, if you start TiDB by the command "./tidb-server --port=3000", the port number should be
Expand Down Expand Up @@ -911,6 +915,9 @@ func (c *Config) Load(confFile string) error {
if metaData.IsDefined("mem-quota-query") {
IsMemoryQuotaQuerySetByUser = true
}
if metaData.IsDefined("oom-action") {
IsOOMActionSetByUser = true
}
if len(c.ServerVersion) > 0 {
mysql.ServerVersion = c.ServerVersion
}
Expand Down
34 changes: 31 additions & 3 deletions session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ const (
// The variable name in mysql.tidb table and it records the default value of
// mem-quota-query when upgrade from v3.0.x to v4.0.9+.
tidbDefMemoryQuotaQuery = "default_memory_quota_query"
// 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"
// Const for TiDB server version 2.
version2 = 2
version3 = 3
Expand Down Expand Up @@ -434,7 +437,7 @@ const (
version52 = 52
// version53 introduce Global variable tidb_enable_strict_double_type_check
version53 = 53
// version54 writes a variable `mem_quota_query` to mysql.tidb if it's a cluster upgraded from v3.0.x to v4.0.9.
// version54 writes a variable `mem_quota_query` to mysql.tidb if it's a cluster upgraded from v3.0.x to v4.0.9+.
version54 = 54
// version55 fixes the bug that upgradeToVer48 would be missed when upgrading from v4.0 to a new version
version55 = 55
Expand All @@ -444,9 +447,11 @@ const (
version57 = 57
// version58 add `Repl_client_priv` and `Repl_slave_priv` to `mysql.user`
version58 = 58
// version59 add writes a variable `oom-action` to mysql.tidb if it's a cluster upgraded from v3.0.x to v4.0.11+.
version59 = 59

// please make sure this is the largest version
currentBootstrapVersion = version58
currentBootstrapVersion = version59
)

var (
Expand Down Expand Up @@ -509,6 +514,7 @@ var (
upgradeToVer56,
upgradeToVer57,
upgradeToVer58,
upgradeToVer59,
}
)

Expand Down Expand Up @@ -1267,13 +1273,35 @@ func upgradeToVer58(s Session, ver int64) {
mustExecute(s, "UPDATE HIGH_PRIORITY mysql.user SET Repl_slave_priv='Y',Repl_client_priv='Y'")
}

func upgradeToVer59(s Session, ver int64) {
if ver >= version59 {
return
}
// The oom-action default value is log by default in v3.0, and cancel by
// default in v4.0.11+.
// If a cluster is upgraded from v3.0.x (bootstrapVer <= version59) to
// v4.0.11+, we'll write the default value to mysql.tidb. Thus we can get
// the default value of oom-action, and promise the compatibility even if
// the tidb-server restarts.
// If it's a newly deployed cluster, we do not need to write the value into
// mysql.tidb, since no compatibility problem will happen.
writeOOMAction(s)
}

func writeMemoryQuotaQuery(s Session) {
comment := "memory_quota_query is 32GB by default in v3.0.x, 1GB by default in v4.0.x"
comment := "memory_quota_query is 32GB by default in v3.0.x, 1GB by default in v4.0.x+"
sql := fmt.Sprintf(`INSERT HIGH_PRIORITY INTO %s.%s VALUES ("%s", '%d', '%s') ON DUPLICATE KEY UPDATE VARIABLE_VALUE='%d'`,
mysql.SystemDB, mysql.TiDBTable, tidbDefMemoryQuotaQuery, 32<<30, comment, 32<<30)
mustExecute(s, sql)
}

func writeOOMAction(s Session) {
comment := "oom-action is `log` by default in v3.0.x, `cancel` by default in v4.0.11+"
sql := fmt.Sprintf(`INSERT HIGH_PRIORITY INTO %s.%s VALUES ("%s", '%s', '%s') ON DUPLICATE KEY UPDATE VARIABLE_VALUE='%s'`,
mysql.SystemDB, mysql.TiDBTable, tidbDefOOMAction, config.OOMActionLog, comment, config.OOMActionLog)
mustExecute(s, sql)
}

// updateBootstrapVer updates bootstrap version variable in mysql.TiDB table.
func updateBootstrapVer(s Session) {
// Update bootstrap version.
Expand Down
87 changes: 87 additions & 0 deletions session/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/parser"
"github.com/pingcap/parser/auth"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
Expand Down Expand Up @@ -271,6 +272,92 @@ func (s *testBootstrapSuite) TestUpgrade(c *C) {
c.Assert(r.Close(), IsNil)
}

func (s *testBootstrapSuite) TestIssue17979_1(c *C) {
oomAction := config.GetGlobalConfig().OOMAction
defer func() {
config.UpdateGlobal(func(conf *config.Config) {
conf.OOMAction = oomAction
})
}()
ctx := context.Background()
defer testleak.AfterTest(c)()
store, _ := newStoreWithBootstrap(c, s.dbName)
defer store.Close()

// test issue 20900, upgrade from v3.0 to v4.0.11+
seV3 := newSession(c, store, s.dbName)
txn, err := store.Begin()
c.Assert(err, IsNil)
m := meta.NewMeta(txn)
err = m.FinishBootstrap(int64(58))
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
mustExecSQL(c, seV3, "update mysql.tidb set variable_value='58' where variable_name='tidb_server_version'")
mustExecSQL(c, seV3, "delete from mysql.tidb where variable_name='default_oom_action'")
mustExecSQL(c, seV3, "commit")
unsetStoreBootstrapped(store.UUID())
ver, err := getBootstrapVersion(seV3)
c.Assert(err, IsNil)
c.Assert(ver, Equals, int64(58))

domV4, err := BootstrapSession(store)
c.Assert(err, IsNil)
defer domV4.Close()
seV4 := newSession(c, store, s.dbName)
ver, err = getBootstrapVersion(seV4)
c.Assert(err, IsNil)
c.Assert(ver, Equals, int64(currentBootstrapVersion))
r := mustExecSQL(c, seV4, "select variable_value from mysql.tidb where variable_name='default_oom_action'")
req := r.NewChunk()
r.Next(ctx, req)
c.Assert(req.GetRow(0).GetString(0), Equals, "log")
c.Assert(config.GetGlobalConfig().OOMAction, Equals, config.OOMActionLog)
}

func (s *testBootstrapSuite) TestIssue17979_2(c *C) {
oomAction := config.GetGlobalConfig().OOMAction
defer func() {
config.UpdateGlobal(func(conf *config.Config) {
conf.OOMAction = oomAction
})
}()
ctx := context.Background()
defer testleak.AfterTest(c)()
store, _ := newStoreWithBootstrap(c, s.dbName)
defer store.Close()

// test issue 20900, upgrade from v4.0.11 to v4.0.11
seV3 := newSession(c, store, s.dbName)
txn, err := store.Begin()
c.Assert(err, IsNil)
m := meta.NewMeta(txn)
err = m.FinishBootstrap(int64(59))
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
mustExecSQL(c, seV3, "update mysql.tidb set variable_value=59 where variable_name='tidb_server_version'")
mustExecSQL(c, seV3, "delete from mysql.tidb where variable_name='default_iim_action'")
mustExecSQL(c, seV3, "commit")
unsetStoreBootstrapped(store.UUID())
ver, err := getBootstrapVersion(seV3)
c.Assert(err, IsNil)
c.Assert(ver, Equals, int64(59))

domV4, err := BootstrapSession(store)
c.Assert(err, IsNil)
defer domV4.Close()
seV4 := newSession(c, store, s.dbName)
ver, err = getBootstrapVersion(seV4)
c.Assert(err, IsNil)
c.Assert(ver, Equals, int64(currentBootstrapVersion))
r := mustExecSQL(c, seV4, "select variable_value from mysql.tidb where variable_name='default_oom_action'")
req := r.NewChunk()
r.Next(ctx, req)
c.Assert(req.NumRows(), Equals, 0)
c.Assert(config.GetGlobalConfig().OOMAction, Equals, config.OOMActionCancel)
}

func (s *testBootstrapSuite) TestIssue20900_1(c *C) {
ctx := context.Background()
defer testleak.AfterTest(c)()
Expand Down
24 changes: 24 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,21 @@ func loadDefMemQuotaQuery(se *session) (int64, error) {
return 32 << 30, nil
}

func loadDefOOMAction(se *session) (string, error) {
defOOMAction, err := loadParameter(se, tidbDefOOMAction)
if err != nil {
if err == errResultIsEmpty {
return config.GetGlobalConfig().OOMAction, nil
}
return config.GetGlobalConfig().OOMAction, err
}
if defOOMAction != config.OOMActionLog {
logutil.BgLogger().Warn("Unexpected value of 'default_oom_action' in 'mysql.tidb', use 'log' instead",
zap.String("value", defOOMAction))
}
return defOOMAction, nil
}

var (
errResultIsEmpty = dbterror.ClassExecutor.NewStd(errno.ErrResultIsEmpty)
)
Expand Down Expand Up @@ -1974,6 +1989,15 @@ func BootstrapSession(store kv.Storage) (*domain.Domain, error) {
config.StoreGlobalConfig(&newCfg)
variable.SetSysVar(variable.TIDBMemQuotaQuery, strconv.FormatInt(newCfg.MemQuotaQuery, 10))
}
newOOMAction, err := loadDefOOMAction(se)
if err != nil {
return nil, err
}
if !config.IsOOMActionSetByUser {
config.UpdateGlobal(func(conf *config.Config) {
conf.OOMAction = newOOMAction
})
}

dom := domain.GetDomain(se)
dom.InitExpensiveQueryHandle()
Expand Down

0 comments on commit acb3f99

Please sign in to comment.