Skip to content

Commit

Permalink
cherry pick pingcap#33818 to release-6.0
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <[email protected]>
  • Loading branch information
lcwangchao authored and ti-srebot committed Apr 8, 2022
1 parent 36a9810 commit d722ad1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
44 changes: 44 additions & 0 deletions executor/stale_txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1310,9 +1310,53 @@ func TestPlanCacheWithStaleReadByBinaryProto(t *testing.T) {
stmtID1, _, _, err := se.PrepareStmt("select * from t1 as of timestamp @a where id=1")
require.NoError(t, err)

<<<<<<< HEAD
rs, err := se.ExecutePreparedStmt(context.TODO(), stmtID1, nil)
require.NoError(t, err)
tk.ResultSetToResult(rs, fmt.Sprintf("%v", rs)).Check(testkit.Rows("1 10"))
=======
for i := 0; i < 3; i++ {
rs, err := se.ExecutePreparedStmt(context.TODO(), stmtID1, nil)
require.NoError(t, err)
tk.ResultSetToResult(rs, fmt.Sprintf("%v", rs)).Check(testkit.Rows("1 10"))
}

stmtID2, _, _, err := se.PrepareStmt("select * from t1 where id=1")
require.NoError(t, err)
for i := 0; i < 2; i++ {
rs, err := se.ExecutePreparedStmt(context.TODO(), stmtID2, nil)
require.NoError(t, err)
tk.ResultSetToResult(rs, fmt.Sprintf("%v", rs)).Check(testkit.Rows("1 100"))
}
tk.MustExec("set @@tx_read_ts=@a")
rs, err := se.ExecutePreparedStmt(context.TODO(), stmtID2, nil)
require.NoError(t, err)
// will fail
tk.ResultSetToResult(rs, fmt.Sprintf("%v", rs)).Check(testkit.Rows("1 10"))
}

func TestIssue30872(t *testing.T) {
store, _, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_txn_mode='pessimistic'")
tk.MustExec("set tx_isolation = 'READ-COMMITTED'")
tk.MustExec("create table t1 (id int primary key, v int)")
tk.MustExec("insert into t1 values(1, 10)")
time.Sleep(time.Millisecond * 100)
tk.MustExec("set @a=now(6)")
time.Sleep(time.Millisecond * 100)
tk.MustExec("update t1 set v=100 where id=1")
tk.MustExec("set autocommit=0")
tk.MustQuery("select * from t1 as of timestamp @a").Check(testkit.Rows("1 10"))
}

func TestIssue33728(t *testing.T) {
store, _, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()
>>>>>>> f04dfc4c6... *: forbid using cache when stale read in binary proto (#33818)

rs, err = se.ExecutePreparedStmt(context.TODO(), stmtID1, nil)
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2244,9 +2244,9 @@ func (s *session) cachedPlanExec(ctx context.Context,
// IsCachedExecOk check if we can execute using plan cached in prepared structure
// Be careful for the short path, current precondition is ths cached plan satisfying
// IsPointGetWithPKOrUniqueKeyByAutoCommit
func (s *session) IsCachedExecOk(ctx context.Context, preparedStmt *plannercore.CachedPrepareStmt) (bool, error) {
func (s *session) IsCachedExecOk(ctx context.Context, preparedStmt *plannercore.CachedPrepareStmt, isStaleness bool) (bool, error) {
prepared := preparedStmt.PreparedAst
if prepared.CachedPlan == nil || preparedStmt.SnapshotTSEvaluator != nil {
if isStaleness {
return false, nil
}
// check auto commit
Expand Down Expand Up @@ -2325,7 +2325,7 @@ func (s *session) ExecutePreparedStmt(ctx context.Context, stmtID uint32, args [
}

executor.CountStmtNode(preparedStmt.PreparedAst.Stmt, s.sessionVars.InRestrictedSQL)
ok, err = s.IsCachedExecOk(ctx, preparedStmt)
ok, err = s.IsCachedExecOk(ctx, preparedStmt, snapshotTS != 0)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit d722ad1

Please sign in to comment.