最近在看tikv的api文档。交易中有一个 LockKey api。当我调用它进行操作时,我直接恐慌。我有薮问题?
- 我想知道它为什么会恐慌
- lockKeys api 的用途是什么?有什么可以帮助我的
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)
}
恐慌堆栈是:
panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
github.com/pingcap/tidb/store/tikv.actionPessimisticLock.handleSingleBatch(0xc0002d6380, 0xc00036c3c0, 0xc0002d6400, 0x4, 0x1, 0x2, 0x0, 0x0, 0x0, 0xc000288bd0, ...)
/Users/go/pkg/mod/github.com/pingcap/tidb@v1.1.0-beta.0.20210419034717-00632fb3c710/store/tikv/2pc.go:922 +0x1442
github.com/pingcap/tidb/store/tikv.(*twoPhaseCommitter).doActionOnBatches(0xc00036c3c0, 0xc0002d6400, 0x4d430e0, 0xc0002d6380, 0xc0002d6500, 0x1, 0x1, 0x0, 0x0)
/Users/go/pkg/mod/github.com/pingcap/tidb@v1.1.0-beta.0.20210419034717-00632fb3c710/store/tikv/2pc.go:678 +0x110
github.com/pingcap/tidb/store/tikv.(*twoPhaseCommitter).doActionOnGroupMutations(0xc00036c3c0, 0xc0002d6400, 0x4d430e0, 0xc0002d6380, 0xc0002d6480, 0x1, 0x1, 0x2, 0x0)
/Users/go/pkg/mod/github.com/pingcap/tidb@v1.1.0-beta.0.20210419034717-00632fb3c710/store/tikv/2pc.go:623 +0x442
github.com/pingcap/tidb/store/tikv.(*twoPhaseCommitter).doActionOnMutations(0xc00036c3c0, 0xc0002d6400, 0x4d430e0, 0xc0002d6380, 0x0, 0x0, 0x0, 0xc000288bd0, 0x2, 0x2, ...)
/Users/go/pkg/mod/github.com/pingcap/tidb@v1.1.0-beta.0.20210419034717-00632fb3c710/store/tikv/2pc.go:513 +0x1c3
github.com/pingcap/tidb/store/tikv.(*twoPhaseCommitter).pessimisticLockMutations(...)
/Users/go/pkg/mod/github.com/pingcap/tidb@v1.1.0-beta.0.20210419034717-00632fb3c710/store/tikv/2pc.go:1291
github.com/pingcap/tidb/store/tikv.(*tikvTxn).LockKeys(0xc0000f60c0, 0x4d44e60, 0xc00012c000, 0xc0002d6380, 0xc000288ba0, 0x2, 0x2, 0x0, 0x0)
/Users/go/pkg/mod/github.com/pingcap/tidb@v1.1.0-beta.0.20210419034717-00632fb3c710/store/tikv/txn.go:446 +0x5f9
main.testTxn(0xc00003e068, 0x4, 0x4, 0xc00003e088, 0x4, 0x4, 0xc00003e096, 0x7, 0x7, 0xc00003e0a0, ...)
它发生在疼痛发生的地方,m为nil
go mod 在下面
go 1.14
require (
github.com/juju/errors v0.0.0-20200330140219-3fe23663418f
github.com/juju/testing v0.0.0-20210324180055-18c50b0c2098 // indirect
github.com/pingcap/parser v0.0.0-20210107054750-53e33b4018fe
github.com/pingcap/tidb v1.1.0-beta.0.20210419034717-00632fb3c710
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
)