diff --git a/optimistic-transaction.md b/optimistic-transaction.md index 215c1df82d82c..cf3551cf51461 100644 --- a/optimistic-transaction.md +++ b/optimistic-transaction.md @@ -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 +``` 1. The client begins a transaction.