what dose lockkeys in tikv api use for?

129 Views Asked by At

Recently, I was looking at tikv's api document. There is a LockKey api in transaction. When I call it for operation, I directly panic. i have serval question?

  1. i want to know why it panic
  2. what does the lockKeys api use for? can any help me
    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 stack is :

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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/store/tikv/2pc.go:513 +0x1c3
github.com/pingcap/tidb/store/tikv.(*twoPhaseCommitter).pessimisticLockMutations(...)
        /Users/go/pkg/mod/github.com/pingcap/[email protected]/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/[email protected]/store/tikv/txn.go:446 +0x5f9
main.testTxn(0xc00003e068, 0x4, 0x4, 0xc00003e088, 0x4, 0x4, 0xc00003e096, 0x7, 0x7, 0xc00003e0a0, ...)

it occure in where painc happen, m is nil

the go mod is below

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
)

1

There are 1 best solutions below

1
On

For question2, lockKeys will lock the entry for the keys you provided in KVStore(TiKV)

For question1, please share the whole panic stack.