Questions about Write Conflict Testing in Pessimistic Mode

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

Original topic: 悲观模式下写写冲突测试的疑问

| username: HACK

【TiDB Usage Environment】Production, Testing, Research
【TiDB Version】
【Encountered Problem】

The current environment is in pessimistic transaction mode. Why does the following test result in a write-write conflict message when session1 commits its transaction after session2 has already committed?


mysql> 
mysql> show variables like '%txn_mode%';
+---------------+-------------+
| Variable_name | Value       |
+---------------+-------------+
| tidb_txn_mode | PESSIMISTIC |
+---------------+-------------+
1 row in set (0.01 sec)


mysql> show create table zjp;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                      |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| zjp   | CREATE TABLE `zjp` (
  `id` int(11) NOT NULL,
  `name` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

 

SESSION1: Start transaction
mysql>
mysql> begin;
Query OK, 0 rows affected (0.00 sec)

SESSION2: Update record with id=13
mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> update zjp set name=‘ABC’ where id=13;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

SESSION1: Update record with id=13, backend log reports write conflict
mysql> update zjp set name=‘ABC’ where id=13;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0

tidb.log
[2022/08/09 10:57:24.191 +08:00] [INFO] [adapter.go:633] [“pessimistic write conflict, retry statement”] [conn=446] [txn=435162658714681346] [forUpdateTS=435162658714681346] [err=“[kv:9007]Write conflict, txnStartTS=435162658714681346, conflictStartTS=435162659501113346, conflictCommitTS=435162668178079745, key={tableID=534, handle=12} primary={tableID=534, handle=12} [try again later]”]

【Reproduction Path】What operations were performed to encounter the problem
【Problem Phenomenon and Impact】

【Attachments】

Please provide the version information of each component, such as cdc/tikv, which can be obtained by executing cdc version/tikv-server --version.

| username: h5n1 | Original post link

Which version are you using? I couldn’t reproduce the issue on version 5.2.3 following your steps.

| username: HACK | Original post link

My version is 4.0.16

| username: h5n1 | Original post link

  1. In newer versions, tidb.log may only have such write conflict records at the debug log level.
  2. TiDB’s pessimistic transactions lock during the DML phase, i.e., writing lock info to TiKV. During the write, conflict detection occurs, checking if there is a committed transaction with a commit_ts greater than the current transaction’s forUpdateTS. If so, it returns a write conflict message.
    Session1 starts the transaction with txnStartTS=435162658714681346 and executes DML with forUpdateTS=435162658714681346. After session2 commits, conflictCommitTS=435162668178079745. Since session1’s forUpdateTS is less than session2’s CommitTS, a conflict is reported.
  3. It is speculated that in pessimistic transactions, a new ts from PD is used as forUpdateTS, and then the SQL execution is checked.

Below is the pessimistic lock locking process:

| username: HACK | Original post link

I feel that according to this logic, when the business is relatively busy, there will be a lot of such prompt messages. I wonder if that’s the case.

| username: h5n1 | Original post link

Sorry, I can’t translate images. Please provide the text you need translated.