What different betweent optimistic and pessimistic in tikv?

257 Views Asked by At

When I use tikv api, and I found it has an option in TxnKV client, then I test it, but I can not find what the difference between optimistic and pessimistic is in tikv?

The test code is this:

func begin() kv.Transaction{
    transaction, err := store.Begin()
    if err!=nil{
        panic(err)
    }
    return transaction
}
func main() {
    pdAddr := os.Getenv("PD_ADDR")
    if pdAddr != "" {
        os.Args = append(os.Args, "-pd", pdAddr)
    }
    flag.Parse()
    initStore()
    k2 := []byte("key2")
    v22 := []byte("value22")
    v23 := []byte("value22")
    testTxn(k2,v22,v23)
}
func testTxn(k2 []byte, v22 []byte, v23 []byte) {
    txn1, txn2 := begin(), begin()
    txn1.SetOption(kv.Pessimistic, true)
    fmt.Println("txn1 after:", txn1.IsPessimistic())

    txn2.SetOption(kv.Pessimistic, true)
    fmt.Println("txn2 after:", txn2.IsPessimistic())

    err := txn1.Set(k2, v22)
    if err != nil {
        panic(err)
    }

    err = txn2.Set(k2, v23)
    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)
    }
}

No matter whether I set txn1.SetOption(kv.Pessimistic, true) and txn2.SetOption(kv.Pessimistic, true) or not, I haven't found the difference between them.

But in tidb or mysql, modify the same records with a pessimistic transaction, it will block.

Such as transaction A:

begin;
updata t1 set col ="value1" where id=1;

transaction B:

begin;
updata t1 set col ="value2" where id=1; //it will block until transaction A commit or rollback

I have two question :

  1. What's the difference between optimistic and pessimistic in tikv?
  2. What's the difference between tikv's pessimistic-lock and mysql/tidb's pessimistic-lock?

If anyone has any idea, please share it with me, thanks

1

There are 1 best solutions below

2
On
  1. What's the difference between optimistic and pessimistic in tikv?

TiKV adopts Google’s Percolator transaction model to implement the optimistic transaction model. Clients don't write data until the transaction commits.

The pessimistic transaction model is built on the optimistic transaction model which can lock keys before committing the transaction to prevent concurrent modifications.

  1. What's the difference between tikv's pessimistic-lock and mysql/tidb's pessimistic-lock?

TiDB follows the protocol of TiKV's pessimistic transaction model to implement the pessimistic-lock. The different between MySQL and TiDB is documented here.

The kv.Pessimistic option just indicates the transaction is a pessimistic one. Doesn't affect its behavior.

Pessimistic transaction in client-go is not an out-of-the-box feature for now(2021.06). It just provides the basic toolkit and users have to follow the undocumented pessimistic transaction protocol to use it which is error-prone. AFAIK, the only user is TiDB, and it's recommended to use the optimistic transaction model instead.