-
Notifications
You must be signed in to change notification settings - Fork 227
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
Locking a key #37
Comments
Seems like Lines 280 to 286 in a72c652
Hm... I think this might be the optimistic lock. I am not sure the status of our pessimistic locks in this package... @disksing do you know? |
hi @darren-west It is because txn1 only executes the lockKey without any writes, it is considered to be an empty transaction and is directly optimized when it is committed. Try to write another key after lockKey in txn1. |
when i use the goroutine 1 [running]:
github.com/pingcap/tidb/store/tikv.actionPessimisticLock.handleSingleBatch(0xc0004ee180, 0xc000616000, 0xc00061c000, 0x4, 0x1, 0x2, 0x0, 0x0, 0x0, 0xc000410000, ...)
/Users/go/pkg/mod/github.com/pingcap/tidb@v1.1.0-beta.0.20210419034717-00632fb3c710/store/tikv/2pc.go:922 +0x194b
github.com/pingcap/tidb/store/tikv.(*twoPhaseCommitter).doActionOnBatches(0xc000616000, 0xc00061c000, 0x5184f20, 0xc0004ee180, 0xc00061c100, 0x1, 0x1, 0x0, 0x0)
/Users/go/pkg/mod/github.com/pingcap/tidb@v1.1.0-beta.0.20210419034717-00632fb3c710/store/tikv/2pc.go:678 +0x1c0
github.com/pingcap/tidb/store/tikv.(*twoPhaseCommitter).doActionOnGroupMutations(0xc000616000, 0xc00061c000, 0x5184f20, 0xc0004ee180, 0xc00061c080, 0x1, 0x1, 0x0, 0x0)
/Users/go/pkg/mod/github.com/pingcap/tidb@v1.1.0-beta.0.20210419034717-00632fb3c710/store/tikv/2pc.go:623 +0x983
github.com/pingcap/tidb/store/tikv.(*twoPhaseCommitter).doActionOnMutations(0xc000616000, 0xc00061c000, 0x5184f20, 0xc0004ee180, 0x0, 0x0, 0x0, 0xc000410000, 0x2, 0x2, ...)
/Users/go/pkg/mod/github.com/pingcap/tidb@v1.1.0-beta.0.20210419034717-00632fb3c710/store/tikv/2pc.go:513 +0x7e3
github.com/pingcap/tidb/store/tikv.(*twoPhaseCommitter).pessimisticLockMutations(0xc000616000, 0xc00061c000, 0xc0004ee180, 0x0, 0x0, 0x0, 0xc000410000, 0x2, 0x2, 0x0, ...)
/Users/go/pkg/mod/github.com/pingcap/tidb@v1.1.0-beta.0.20210419034717-00632fb3c710/store/tikv/2pc.go:1291 +0xa5
github.com/pingcap/tidb/store/tikv.(*tikvTxn).LockKeys(0xc0000e8180, 0x51865a0, 0xc00003c068, 0xc0004ee180, 0xc0003b2600, 0x2, 0x2, 0x0, 0x0)
/Users/go/pkg/mod/github.com/pingcap/tidb@v1.1.0-beta.0.20210419034717-00632fb3c710/store/tikv/txn.go:446 +0xc41
main.testTxn(0xc00029a080, 0x4, 0x4, 0xc00029a08c, 0x4, 0x4, 0xc00029a096, 0x7, 0x7, 0xc00029a0a0, ...)
/Users/angjiang.xu/jigo/src/tikv-demo/main2.go:229 +0x89a the test method snippit is below: txn1, txn2 := begin(), begin()
fmt.Println("txn1 before:", txn1.IsPessimistic())
txn1.SetOption(kv.Pessimistic, true)
fmt.Println("txn1 after:", txn1.IsPessimistic())
fmt.Println("txn2 before:", txn2.IsPessimistic())
txn2.SetOption(kv.Pessimistic, true)
fmt.Println("txn2 after:", txn2.IsPessimistic())
err := txn1.Set(k2, v22)
if err != nil {
panic(err)
}
err = txn1.Set(k1, v22)
if err != nil {
panic(err)
}
lockCtx1 := &kv.LockCtx{ForUpdateTS: txn1.StartTS(), WaitStartTime: time.Now()}
err = txn1.LockKeys(context.Background(), lockCtx1, k1,k2)
if err!=nil{
panic(err)
}
err = txn2.Set(k2, v23)
if err != nil {
panic(err)
}
err = txn2.Set(k1, v23)
if err != nil {
panic(err)
}
lockCtx2 := &kv.LockCtx{ForUpdateTS: txn2.StartTS(), WaitStartTime: time.Now()}
err = txn2.LockKeys(context.Background(), lockCtx2, k1,k2)
if err!=nil{
panic(err)
}
err = txn1.Commit(context.Background())
if err != nil {
panic(err)
}
fmt.Println(get(k2))
err = txn2.Commit(context.Background())
if err != nil {
panic(err)
} |
Hi,
I want to lock a key so that it cannot be edited in another transaction. I have looked at LockKeys() but looking at the implementation this doesn't seem to do what I want.
The only way I have been able to achieve this is by setting the value to what it already is and having it as part of the transaction. This means I have to read the value and then write it.
Something like below:
Is there nothing I could do such as
The text was updated successfully, but these errors were encountered: