From c55ef2d96d543bc152fbb1120585d2d7d5e8b8f2 Mon Sep 17 00:00:00 2001 From: Chao Wang Date: Fri, 8 Apr 2022 14:48:36 +0800 Subject: [PATCH] *: forbid using cache when stale read in binary proto --- executor/stale_txn_test.go | 13 +++++++++++++ session/session.go | 6 +++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/executor/stale_txn_test.go b/executor/stale_txn_test.go index 771c3fa8c7299..1789e4dd4e612 100644 --- a/executor/stale_txn_test.go +++ b/executor/stale_txn_test.go @@ -1371,6 +1371,19 @@ func TestPlanCacheWithStaleReadByBinaryProto(t *testing.T) { 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) { diff --git a/session/session.go b/session/session.go index 3bfa493e5b6a9..15e29a9523ab8 100644 --- a/session/session.go +++ b/session/session.go @@ -2268,9 +2268,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 @@ -2351,7 +2351,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 }