Skip to content

Commit

Permalink
session: add testcases for staleness transaction (#22438)
Browse files Browse the repository at this point in the history
Signed-off-by: Song Gao <disxiaofei@163.com>
  • Loading branch information
Yisaer authored Jan 21, 2021
1 parent ac17ae9 commit dd8fd16
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
23 changes: 23 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7561,6 +7561,29 @@ func (s *testSuite) TestStalenessTransaction(c *C) {
}
}

func (s *testSuite) TestStalenessAndHistoryRead(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
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)
// set @@tidb_snapshot before staleness txn
tk.MustExec(`set @@tidb_snapshot="2016-10-08 16:45:26";`)
tk.MustExec(`START TRANSACTION READ ONLY WITH TIMESTAMP BOUND READ TIMESTAMP '2020-09-06 00:00:00';`)
c.Assert(oracle.ExtractPhysical(tk.Se.GetSessionVars().TxnCtx.StartTS), Equals, int64(1599321600000))
tk.MustExec("commit")
// set @@tidb_snapshot during staleness txn
tk.MustExec(`START TRANSACTION READ ONLY WITH TIMESTAMP BOUND READ TIMESTAMP '2020-09-06 00:00:00';`)
tk.MustExec(`set @@tidb_snapshot="2016-10-08 16:45:26";`)
c.Assert(oracle.ExtractPhysical(tk.Se.GetSessionVars().TxnCtx.StartTS), Equals, int64(1599321600000))
tk.MustExec("commit")
}

func (s *testSuite) TestIssue22231(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
96 changes: 96 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3944,12 +3944,54 @@ func (s *testSessionSuite) TestValidateReadOnlyInStalenessTransaction(c *C) {
sql: `DO SLEEP(1);`,
isValidate: true,
},
{
name: "select for update",
sql: "select * from t where id = 1 for update",
isValidate: false,
},
{
name: "select lock in share mode",
sql: "select * from t where id = 1 lock in share mode",
isValidate: true,
},
{
name: "select for update union statement",
sql: "select * from t for update union select * from t;",
isValidate: false,
},
{
name: "replace statement",
sql: "replace into t(id) values (1)",
isValidate: false,
},
{
name: "load data statement",
sql: "LOAD DATA LOCAL INFILE '/mn/asa.csv' INTO TABLE t FIELDS TERMINATED BY x'2c' ENCLOSED BY b'100010' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (id);",
isValidate: false,
},
{
name: "update multi tables",
sql: "update t,t1 set t.id = 1,t1.id = 2 where t.1 = 2 and t1.id = 3;",
isValidate: false,
},
{
name: "delete multi tables",
sql: "delete t from t1 where t.id = t1.id",
isValidate: false,
},
{
name: "insert select",
sql: "insert into t select * from t1;",
isValidate: false,
},
}
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t (id int);")
tk.MustExec("create table t1 (id int);")
tk.MustExec(`PREPARE stmt1 FROM 'insert into t(id) values (5);';`)
tk.MustExec(`PREPARE stmt2 FROM 'select * from t';`)
tk.MustExec(`set @@tidb_enable_noop_functions=1;`)
for _, testcase := range testcases {
c.Log(testcase.name)
tk.MustExec(`START TRANSACTION READ ONLY WITH TIMESTAMP BOUND READ TIMESTAMP '2020-09-06 00:00:00';`)
Expand All @@ -3965,6 +4007,60 @@ func (s *testSessionSuite) TestValidateReadOnlyInStalenessTransaction(c *C) {
}
}

func (s *testSessionSerialSuite) TestSpecialSQLInStalenessTxn(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
testcases := []struct {
name string
sql string
sameSession bool
}{
{
name: "ddl",
sql: "create table t (id int, b int,INDEX(b));",
sameSession: false,
},
{
name: "set global session",
sql: `SET GLOBAL sql_mode = 'STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER';`,
sameSession: true,
},
{
name: "analyze table",
sql: "analyze table t",
sameSession: true,
},
{
name: "session binding",
sql: "CREATE SESSION BINDING FOR SELECT * FROM t WHERE b = 123 USING SELECT * FROM t IGNORE INDEX (b) WHERE b = 123;",
sameSession: true,
},
{
name: "global binding",
sql: "CREATE GLOBAL BINDING FOR SELECT * FROM t WHERE b = 123 USING SELECT * FROM t IGNORE INDEX (b) WHERE b = 123;",
sameSession: true,
},
{
name: "grant statements",
sql: "GRANT ALL ON test.* TO 'newuser';",
sameSession: false,
},
{
name: "revoke statements",
sql: "REVOKE ALL ON test.* FROM 'newuser';",
sameSession: false,
},
}
tk.MustExec("CREATE USER 'newuser' IDENTIFIED BY 'mypassword';")
for _, testcase := range testcases {
comment := Commentf(testcase.name)
tk.MustExec(`START TRANSACTION READ ONLY WITH TIMESTAMP BOUND READ TIMESTAMP '2020-09-06 00:00:00';`)
c.Assert(tk.Se.GetSessionVars().TxnCtx.IsStaleness, Equals, true, comment)
tk.MustExec(testcase.sql)
c.Assert(tk.Se.GetSessionVars().TxnCtx.IsStaleness, Equals, testcase.sameSession, comment)
}
}

func (s *testSessionSerialSuite) TestRemovedSysVars(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)

Expand Down

0 comments on commit dd8fd16

Please sign in to comment.