Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion optimistic-transaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,56 @@ Before enabling optimistic transactions, make sure that your application correct

To support distributed transactions, TiDB adopts two-phase commit (2PC) in optimistic transactions. The procedure is as follows:

![2PC in TiDB](/media/2pc-in-tidb.png)
```mermaid
---
title: 2PC in TiDB
---
sequenceDiagram
participant client
participant TiDB
participant PD
participant TiKV

client->>TiDB: begin
TiDB->>PD: get ts as start_ts

loop excute SQL
alt do read
TiDB->>PD: get region from PD or cache
TiDB->>TiKV: get data from TiKV or cache with start_ts
TiDB-->>client: return read result
end
alt do write
TiDB-->>TiDB: write in cache
TiDB-->>client: return write result
end
end

client->>TiDB: commit

opt start 2PC
TiDB-->>TiDB: for all keys need to write,choose first one as primary
TiDB->>PD: locate each key
TiDB-->>TiDB: group keys by region to [](region,keys)

opt prewrite with start_ts
TiDB->>TiKV: prewrite(primary_key,start_ts)
loop prewrite to each region in [](region,keys) parallelly
TiDB->>TiKV: prewrite(keys,primary_key,start_ts)
end
end

opt commit
TiDB-->>PD: get ts as commit_ts
TiDB-->>TiKV: commit primary with commit_ts
loop send commit to each region in [](region,keys) parallelly
TiDB->>TiKV: commit(keys,commit_ts)
end
end
end

TiDB-->>client: success
```
Comment on lines +21 to +70
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

To improve clarity and readability, I've made several wording adjustments, corrected typos, and ensured consistent capitalization throughout the diagram. This makes the sequence of events in the 2PC process easier to follow.

---
title: 2PC in TiDB
---
sequenceDiagram
    participant client
    participant TiDB
    participant PD
    participant TiKV

    client->>TiDB: begin
    TiDB->>PD: Get timestamp as start_ts

    loop Execute SQL statements
        alt Read data
            TiDB->>PD: Get Region from PD or cache
            TiDB->>TiKV: Get data from TiKV or cache with start_ts
            TiDB-->>client: Return read result
        end
        alt Write data
            TiDB-->>TiDB: Write data to the memory cache
            TiDB-->>client: Return write result
        end
    end

    client->>TiDB: commit

    opt Start 2PC
        TiDB-->>TiDB: Select a primary key from the keys to be written
        TiDB->>TiKV: Locate the Region for each key
        TiDB-->>TiDB: Group keys by Region

        opt Prewrite phase
            TiDB->>TiKV: prewrite(primary_key, start_ts)
            loop Prewrite secondary keys for each Region in parallel
                TiDB->>TiKV: prewrite(keys, primary_key, start_ts)
            end
        end

        opt Commit phase
            TiDB->>PD: Get timestamp as commit_ts
            TiDB->>TiKV: commit(primary_key, commit_ts)
            loop Commit secondary keys for each Region in parallel
                TiDB->>TiKV: commit(keys, commit_ts)
            end
        end
    end

    TiDB-->>client: success
References
  1. The suggested changes improve the diagram's clarity, readability, and consistency, and correct grammar and spelling issues, as outlined in the repository's documentation style guide (lines 14, 20, 25). (link)


1. The client begins a transaction.

Expand Down
Loading