Skip to content

Commit

Permalink
session: fix set session variable make tidb_snapshot unwork (pingcap#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Oct 19, 2021
1 parent 333edd8 commit da1c21f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 5 additions & 2 deletions executor/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (e *SetExecutor) setSysVariable(ctx context.Context, name string, v *expres
}
}

err = e.loadSnapshotInfoSchemaIfNeeded(newSnapshotTS)
err = e.loadSnapshotInfoSchemaIfNeeded(name, newSnapshotTS)
if err != nil {
fallbackOldSnapshotTS()
return err
Expand Down Expand Up @@ -254,7 +254,10 @@ func (e *SetExecutor) getVarValue(v *expression.VarAssignment, sysVar *variable.
return nativeVal.ToString()
}

func (e *SetExecutor) loadSnapshotInfoSchemaIfNeeded(snapshotTS uint64) error {
func (e *SetExecutor) loadSnapshotInfoSchemaIfNeeded(name string, snapshotTS uint64) error {
if name != variable.TiDBSnapshot && name != variable.TiDBTxnReadTS {
return nil
}
vars := e.ctx.GetSessionVars()
if snapshotTS == 0 {
vars.SnapshotInfoschema = nil
Expand Down
24 changes: 24 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5575,3 +5575,27 @@ func (s *testSessionSuite) TestLocalTemporaryTableUpdate(c *C) {
tk.MustQuery("select * from tmp1").Check(testkit.Rows())
}
}

func (s *testSessionSuite) TestFixSetTiDBSnapshotTS(c *C) {
tk := testkit.NewTestKit(c, s.store)
safePointName := "tikv_gc_safe_point"
safePointValue := "20160102-15:04:05 -0700"
safePointComment := "All versions after safe point can be accessed. (DO NOT EDIT)"
updateSafePoint := fmt.Sprintf(`INSERT INTO mysql.tidb VALUES ('%[1]s', '%[2]s', '%[3]s')
ON DUPLICATE KEY
UPDATE variable_value = '%[2]s', comment = '%[3]s'`, safePointName, safePointValue, safePointComment)
tk.MustExec(updateSafePoint)
tk.MustExec("create database t123")
time.Sleep(time.Second)
ts := time.Now().Format("2006-1-2 15:04:05")
time.Sleep(time.Second)
tk.MustExec("drop database t123")
err := tk.ExecToErr("use t123")
c.Assert(err, NotNil)
c.Assert(err.Error(), Matches, ".*Unknown database.*")
tk.MustExec(fmt.Sprintf("set @@tidb_snapshot='%s'", ts))
tk.MustExec("use t123")
// update any session variable and assert whether infoschema is changed
tk.MustExec("SET SESSION sql_mode = 'STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER';")
tk.MustExec("use t123")
}

0 comments on commit da1c21f

Please sign in to comment.