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

executor: do not read from lock cache when snapshot read (#21529) #21539

Merged
merged 3 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion executor/batch_point_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ func (e *BatchPointGetExec) initialize(ctx context.Context) error {
snapshot.SetOption(kv.TaskID, e.ctx.GetSessionVars().StmtCtx.TaskID)
var batchGetter kv.BatchGetter = snapshot
if sessVars.InTxn() {
batchGetter = kv.NewBufferBatchGetter(txn.GetMemBuffer(), &PessimisticLockCacheGetter{txnCtx: txnCtx}, snapshot)
if e.lock {
batchGetter = kv.NewBufferBatchGetter(txn.GetMemBuffer(), &PessimisticLockCacheGetter{txnCtx: txnCtx}, snapshot)
} else {
batchGetter = kv.NewBufferBatchGetter(txn.GetMemBuffer(), nil, snapshot)
}
}

var handleVals map[string][]byte
Expand Down
10 changes: 6 additions & 4 deletions executor/point_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,12 @@ func (e *PointGetExecutor) get(ctx context.Context, key kv.Key) ([]byte, error)
return nil, err
}
// key does not exist in mem buffer, check the lock cache
var ok bool
val, ok = e.ctx.GetSessionVars().TxnCtx.GetKeyInPessimisticLockCache(key)
if ok {
return val, nil
if e.lock {
var ok bool
val, ok = e.ctx.GetSessionVars().TxnCtx.GetKeyInPessimisticLockCache(key)
if ok {
return val, nil
}
}
// fallthrough to snapshot get.
}
Expand Down
24 changes: 24 additions & 0 deletions executor/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,27 @@ func (s *testUpdateSuite) TestUpdateMultiDatabaseTable(c *C) {
tk.MustExec("create table test2.t(a int, b int generated always as (a+1) virtual)")
tk.MustExec("update t, test2.t set test.t.a=1")
}

func (s *testPointGetSuite) TestIssue21447(c *C) {
tk1, tk2 := testkit.NewTestKit(c, s.store), testkit.NewTestKit(c, s.store)
tk1.MustExec("use test")
tk2.MustExec("use test")

tk1.MustExec("drop table if exists t1")
tk1.MustExec("create table t1(id int primary key, name varchar(40))")
tk1.MustExec("insert into t1 values(1, 'abc')")

tk1.MustExec("begin pessimistic")
tk2.MustExec("begin pessimistic")
tk2.MustExec("update t1 set name='xyz' where id=1")
tk2.CheckExecResult(1, 0)
tk2.MustQuery("select * from t1 where id = 1").Check(testkit.Rows("1 xyz"))
tk2.MustExec("commit")
tk1.MustExec("update t1 set name='xyz' where id=1")
tk1.CheckExecResult(0, 0)
tk1.MustQuery("select * from t1 where id = 1").Check(testkit.Rows("1 abc"))
tk1.MustQuery("select * from t1 where id = 1 for update").Check(testkit.Rows("1 xyz"))
tk1.MustQuery("select * from t1 where id in (1, 2)").Check(testkit.Rows("1 abc"))
tk1.MustQuery("select * from t1 where id in (1, 2) for update").Check(testkit.Rows("1 xyz"))
tk1.MustExec("commit")
}