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

Locking a key #37

Open
darren-west opened this issue Dec 9, 2019 · 3 comments
Open

Locking a key #37

darren-west opened this issue Dec 9, 2019 · 3 comments

Comments

@darren-west
Copy link

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?
@Hoverbear
Copy link

Hoverbear commented Dec 9, 2019

Seems like

client-go/txnkv/txn.go

Lines 280 to 286 in a72c652

func (txn *Transaction) LockKeys(keys ...key.Key) error {
start := time.Now()
defer func() { metrics.TxnCmdHistogram.WithLabelValues("lock_keys").Observe(time.Since(start).Seconds()) }()
for _, key := range keys {
txn.lockKeys = append(txn.lockKeys, key)
}
return nil
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?

@disksing
Copy link
Collaborator

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.

@John520
Copy link

John520 commented Jun 14, 2021

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/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)
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants