What specific objects are latches in TiKV memory applied to?

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: TiKV内存中的latch具体是加在什么对象上的?

| username: alfred

【TiDB Usage Environment】Production\Test Environment\POC
【TiDB Version】
【Encountered Issues】
【Reproduction Path】What operations were performed that caused the issue
【Issue Phenomenon and Impact】
【TiDB Operator Version】:
【K8s Version】:
【Attachments】:

| username: TiDBer_jYQINSnf | Original post link

A latch is not an object; it can be understood as a bucket. When gRPC receives a request and parses it, it needs to call the scheduler. Before the scheduler processes it, it needs to get a ticket from the bucket. Once it gets the ticket, it waits. If it gets the ticket, it continues to execute.

| username: alfred | Original post link

A latch can be understood as a type of resource, but it should be held by one or more objects in memory, right?

| username: Aunt-Shirly | Original post link

The latch in TiKV is mainly used for concurrency control in the TiKV transaction layer. The specific locking object is the key object in the TiKV transaction request, preventing multiple concurrent operations on the same key.

To quickly understand, you can read the test cases at the end of this file for a simple understanding of this latch.

TiKV itself supports the ACID transaction model externally, and all transaction interfaces must ensure their atomicity. The transaction implementation of TiKV is based on the underlying raftstore. Currently, the atomic operations that raftstore can provide are only two:

  • Snapshot level reading
  • Batch writing

TiKV needs to implement the atomicity of transaction interfaces based on the above interfaces, and most transaction interfaces require:

  • Read, check feasibility
  • Batch writing

To ensure its atomicity, TiKV uses in-memory locks (latches) to solve this problem.

Here is a simple introduction to an interface in the transaction model as an example:
prewrite(keys): The prewrite of optimistic locking. After TiKV receives this request, it will check and physically lock all keys. After this request is successful, any other client attempting to lock any of the already locked keys will fail.

Assume:
client1 prewrite(k1, k2, k3)
client2 prewrite(k3, k5)

To ensure the atomicity of prewrite, when receiving the request from client1, latches are used to lock k1, k2, and k3 until the entire request is processed before returning. When receiving the request from client2, it attempts to use latches to lock k3 and k5. At this time, it finds that k3 is already locked, so client2 registers in the latches for k3 and queues up. Only after the lock on k3 is released will client2 be reawakened to continue processing.

| username: alfred | Original post link

Oh, it’s mainly used for concurrency control at the KV transaction layer. How can I check the usage of latches? For example, whether there is severe contention or not.

| username: Aunt-Shirly | Original post link

On the tikv-details monitoring panel, each scheduler, which is the transaction layer API, has a scheduler latch wait duration panel. The longer this wait time, the more severe the contention for the same key.


| username: alfred | Original post link

Thank you very much.

| username: system | Original post link

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.