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

txnkv: convert LOCK on untouched kv into PUT #752

Merged
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
38 changes: 37 additions & 1 deletion integration_tests/2pc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,7 @@ func (s *testCommitterSuite) TestFlagsInMemBufferMutations() {
func (s *testCommitterSuite) TestSetLockedKeyValue() {
ctx := context.Background()
k1 := []byte("k1")
k2 := []byte("t00000001_i000000001")
v1 := []byte("v1")
v2 := []byte("v2")

Expand Down Expand Up @@ -1801,6 +1802,26 @@ func (s *testCommitterSuite) TestSetLockedKeyValue() {
checkByOpVals(kvrpcpb.Op_Put, v2),
checkByOpVals(kvrpcpb.Op_Put, v2),
},
{
"LockAndSetUnnecessaryKeyWithSameValue",
[]func(txn transaction.TxnProbe){
func(txn transaction.TxnProbe) { txn.SetLockedKeyValue(k2, v2) },
func(txn transaction.TxnProbe) { mustLockKey(txn, k2) },
func(txn transaction.TxnProbe) { s.Require().NoError(txn.Set(k2, v2)) },
},
checkByOpVals(kvrpcpb.Op_Put, v2),
checkByOpVals(kvrpcpb.Op_Lock, v2),
},
{
"LockAndSetUnnecessaryKeyWithDiffValue",
[]func(txn transaction.TxnProbe){
func(txn transaction.TxnProbe) { txn.SetLockedKeyValue(k2, v1) },
func(txn transaction.TxnProbe) { mustLockKey(txn, k2) },
func(txn transaction.TxnProbe) { s.Require().NoError(txn.Set(k2, v2)) },
},
checkByOpVals(kvrpcpb.Op_Lock, v2),
checkByOpVals(kvrpcpb.Op_Lock, v2),
},
{
"LockAndDelete",
[]func(txn transaction.TxnProbe){
Expand All @@ -1811,12 +1832,26 @@ func (s *testCommitterSuite) TestSetLockedKeyValue() {
checkByOpVals(kvrpcpb.Op_Del, []byte{}),
checkByOpVals(kvrpcpb.Op_Del, []byte{}),
},
{
"LockAndDeleteYourWrite",
[]func(txn transaction.TxnProbe){
func(txn transaction.TxnProbe) { txn.SetLockedKeyValue(k1, v1) },
func(txn transaction.TxnProbe) { mustLockKey(txn, k1) },
func(txn transaction.TxnProbe) {
s.Require().NoError(txn.GetMemBuffer().DeleteWithFlags(k1, kv.SetNewlyInserted))
},
},
checkByOpVals(kvrpcpb.Op_Lock, []byte{}),
checkByOpVals(kvrpcpb.Op_Lock, []byte{}),
},
cfzjywxk marked this conversation as resolved.
Show resolved Hide resolved
} {
var testAll func(name string, state []bool, actions []func(txn transaction.TxnProbe))
testAll = func(name string, state []bool, actions []func(txn transaction.TxnProbe)) {
if len(actions) == len(tt.actions) {
s.Run("Pessimistic"+name, func() {
txn := s.begin()
defer txn.Rollback()
txn.SetKVFilter(kvFilter{})
txn.SetPessimistic(true)
for _, action := range actions {
action(txn)
Expand All @@ -1828,13 +1863,14 @@ func (s *testCommitterSuite) TestSetLockedKeyValue() {
})
s.Run("Optimistic"+name, func() {
txn := s.begin()
defer txn.Rollback()
txn.SetKVFilter(kvFilter{})
for _, action := range actions {
action(txn)
}
c, err := txn.NewCommitter(1)
s.Require().NoError(err)
tt.checkOptimisitc(c.GetMutations())
s.Require().NoError(txn.Rollback())
})
return
}
Expand Down
17 changes: 12 additions & 5 deletions txnkv/transaction/2pc.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,18 @@ func (c *twoPhaseCommitter) initKeysAndMutations(ctx context.Context) error {
if !flags.HasLocked() {
continue
}
// If the key was locked before, we should prewrite the lock even if
// the KV needn't be committed according to the filter. Otherwise, we
// were forgetting removing pessimistic locks added before.
op = kvrpcpb.Op_Lock
lockCnt++
if val, ok := txn.getValueByLockedKey(key); ok && bytes.Equal(val, value) && c.isPessimistic {
// Change the LOCK into PUT if the value of this key has a cached value.
cachedValue = val
op = kvrpcpb.Op_Put
putFromLockCnt++
} else {
// If the key was locked before, we should prewrite the lock even if
// the KV needn't be committed according to the filter. Otherwise, we
// were forgetting removing pessimistic locks added before.
op = kvrpcpb.Op_Lock
lockCnt++
}
} else {
op = kvrpcpb.Op_Put
if flags.HasPresumeKeyNotExists() {
Expand Down