Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prepared stmt cannot execute when tidb_snapshot is set #22567

Closed
eurekaka opened this issue Jan 27, 2021 · 2 comments · Fixed by #22568
Closed

prepared stmt cannot execute when tidb_snapshot is set #22567

eurekaka opened this issue Jan 27, 2021 · 2 comments · Fixed by #22568
Assignees
Labels
severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@eurekaka
Copy link
Contributor

Development Task

Add the below test case into planner/core/prepare_test.go:

+func (s *testPlanSerialSuite) TestPreparedSnapshot(c *C) {
+       store, _, err := newStoreWithBootstrap()
+       c.Assert(err, IsNil)
+       tk := testkit.NewTestKit(c, store)
+
+       tk.MustExec("use test")
+       tk.MustExec("drop table if exists t")
+       tk.MustExec("create table t(id int)")
+       tk.MustExec("insert into t values (1),(2),(3),(4)")
+
+       // For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
+       timeSafe := time.Now().Add(-48 * 60 * 60 * time.Second).Format("20060102-15:04:05 -0700 MST")
+       safePointSQL := `INSERT HIGH_PRIORITY INTO mysql.tidb VALUES ('tikv_gc_safe_point', '%[1]s', '')
+                              ON DUPLICATE KEY
+                              UPDATE variable_value = '%[1]s'`
+       tk.MustExec(fmt.Sprintf(safePointSQL, timeSafe))
+
+       tk.MustExec("prepare stmt from 'select * from t where id=?'")
+       tk.MustExec("set @p = 1")
+       tk.MustQuery("execute stmt using @p").Check(testkit.Rows("1"))
+
+       // Record the current tso.
+       tk.MustExec("begin")
+       tso := tk.Se.GetSessionVars().TxnCtx.StartTS
+       tk.MustExec("rollback")
+       c.Assert(tso > 0, IsTrue)
+       // Insert one more row with id = 1.
+       tk.MustExec("insert into t values (1)")
+
+       tk.MustExec(fmt.Sprintf("set @@tidb_snapshot = '%d'", tso))
+       tk.MustQuery("select * from t where id = 1").Check(testkit.Rows("1"))
+       tk.MustQuery("execute stmt using @p").Check(testkit.Rows("1"))
+}

This test would fail with stack like:

----------------------------------------------------------------------
FAIL: prepare_test.go:926: testPlanSerialSuite.TestPreparedSnapshot

prepare_test.go:957:
    tk.MustQuery("execute stmt using @p").Check(testkit.Rows("1"))
/Users/cauchy/go/src/github.com/pingcap/tidb/util/testkit/testkit.go:270:
    tk.c.Assert(errors.ErrorStack(err), check.Equals, "", comment)
... obtained string = "" +
...     "[kv:8024]invalid transaction\n" +
...     "github.com/pingcap/errors.AddStack\n" +
...     "\t/Users/cauchy/go/pkg/mod/github.com/pingcap/[email protected]/errors.go:174\n" +
...     "github.com/pingcap/tidb/session.(*session).Txn\n" +
...     "\t/Users/cauchy/go/src/github.com/pingcap/tidb/session/session.go:1787\n" +
...     "github.com/pingcap/tidb/executor.(*executorBuilder).getSnapshotTS\n" +
...     "\t/Users/cauchy/go/src/github.com/pingcap/tidb/executor/builder.go:1387\n" +
...     "github.com/pingcap/tidb/executor.buildNoRangeTableReader\n" +
...     "\t/Users/cauchy/go/src/github.com/pingcap/tidb/executor/builder.go:2528\n" +
...     "github.com/pingcap/tidb/executor.(*executorBuilder).buildTableReader\n" +
...     "\t/Users/cauchy/go/src/github.com/pingcap/tidb/executor/builder.go:2596\n" +
...     "github.com/pingcap/tidb/executor.(*executorBuilder).build\n" +
...     "\t/Users/cauchy/go/src/github.com/pingcap/tidb/executor/builder.go:212\n" +
...     "github.com/pingcap/tidb/executor.(*ExecuteExec).Build\n" +
...     "\t/Users/cauchy/go/src/github.com/pingcap/tidb/executor/prepared.go:261\n" +
...     "github.com/pingcap/tidb/executor.(*ExecStmt).buildExecutor\n" +
...     "\t/Users/cauchy/go/src/github.com/pingcap/tidb/executor/adapter.go:769\n" +
...     "github.com/pingcap/tidb/executor.(*ExecStmt).Exec\n" +
...     "\t/Users/cauchy/go/src/github.com/pingcap/tidb/executor/adapter.go:330\n" +
...     "github.com/pingcap/tidb/session.runStmt\n" +
...     "\t/Users/cauchy/go/src/github.com/pingcap/tidb/session/session.go:1558\n" +
...     "github.com/pingcap/tidb/session.(*session).ExecuteStmt\n" +
...     "\t/Users/cauchy/go/src/github.com/pingcap/tidb/session/session.go:1463\n" +
...     "github.com/pingcap/tidb/util/testkit.(*TestKit).Exec\n" +
...     "\t/Users/cauchy/go/src/github.com/pingcap/tidb/util/testkit/testkit.go:170\n" +
...     "github.com/pingcap/tidb/util/testkit.(*TestKit).MustQuery\n" +
...     "\t/Users/cauchy/go/src/github.com/pingcap/tidb/util/testkit/testkit.go:269\n" +
...     "github.com/pingcap/tidb/planner/core_test.(*testPlanSerialSuite).TestPreparedSnapshot\n" +
...     "\t/Users/cauchy/go/src/github.com/pingcap/tidb/planner/core/prepare_test.go:957\n" +
...     "reflect.Value.call\n" +
...     "\t/usr/local/go/src/reflect/value.go:476\n" +
...     "reflect.Value.Call\n" +
...     "\t/usr/local/go/src/reflect/value.go:337\n" +
...     "github.com/pingcap/check.(*suiteRunner).forkTest.func1\n" +
...     "\t/Users/cauchy/go/pkg/mod/github.com/pingcap/[email protected]/check.go:850\n" +
...     "github.com/pingcap/check.(*suiteRunner).forkCall.func1\n" +
...     "\t/Users/cauchy/go/pkg/mod/github.com/pingcap/[email protected]/check.go:739\n" +
...     "runtime.goexit\n" +
...     "\t/usr/local/go/src/runtime/asm_amd64.s:1374"
... expected string = ""
... sql:execute stmt using @p, args:[]

Looks like this problem is introduced recently, the GitHash I used is 0ffa6057371d08443176065e54633d91c3da0960.

@eurekaka
Copy link
Contributor Author

4.0 has this problem as well, 167edacdbfb1db59b6d10c4f6a0ed2b08d0abca8.

@ti-srebot
Copy link
Contributor

ti-srebot commented Feb 1, 2021

Please edit this comment or add a new comment to complete the following information

Not a bug

  1. Remove the 'type/bug' label
  2. Add notes to indicate why it is not a bug

Duplicate bug

  1. Add the 'type/duplicate' label
  2. Add the link to the original bug

Bug

Note: Make Sure that 'component', and 'severity' labels are added
Example for how to fill out the template: #20100

1. Root Cause Analysis (RCA) (optional)

2. Symptom (optional)

3. All Trigger Conditions (optional)

4. Workaround (optional)

5. Affected versions

[v4.0.0:v4.0.10],[v5.0.0-rc]

6. Fixed versions

master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants