client-go icon indicating copy to clipboard operation
client-go copied to clipboard

Locking a key

Open darren-west opened this issue 5 years ago • 3 comments

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:

func TestLock(t *testing.T) {
	client, err := txnkv.NewClient(context.TODO(), []string{"127.0.0.1:2379"}, config.Default())
	require.NoError(t, err)

	txn, err := client.Begin(context.TODO())
	require.NoError(t, err)

	txn1, err := client.Begin(context.TODO())
	require.NoError(t, err)
	require.NoError(t, txn1.Set([]byte("bar"), []byte{'0'}))
	require.NoError(t, txn1.Commit(context.TODO()))

	require.NoError(t, txn.Set([]byte("bar"), []byte{'0'}))
	require.NoError(t, txn.Commit(context.TODO()))

}

Is there nothing I could do such as

func TestLock(t *testing.T) {
	client, err := txnkv.NewClient(context.TODO(), []string{"127.0.0.1:2379"}, config.Default())
	require.NoError(t, err)

	txn, err := client.Begin(context.TODO())
	require.NoError(t, err)

	txn1, err := client.Begin(context.TODO())
	require.NoError(t, err)
	require.NoError(t, txn1.LockKeys([]byte("bar")))
	require.NoError(t, txn1.Commit(context.TODO()))

	require.NoError(t, txn.Set([]byte("bar"), []byte{'0'}))
	require.NoError(t, txn.Commit(context.TODO()))

}
``` that would achieve the same?

darren-west avatar Dec 09 '19 19:12 darren-west

Seems like https://github.com/tikv/client-go/blob/a72c652ef3f8f5d8b51855c14b4f63143413ce8f/txnkv/txn.go#L280-L286 can do this?

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?

Hoverbear avatar Dec 09 '19 19:12 Hoverbear

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.

disksing avatar Dec 10 '19 02:12 disksing

when i use the LockKeys api , it painc ,can anyone help me? @disksing

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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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)
	}

John520 avatar Jun 14 '21 12:06 John520