Principles
In MySQL OLTP applications, a large number of DML statements (Insert, Update, and Delete) are concurrently executed on key data structures in the trx_sys global structure, causing resource contention and synchronization bottlenecks in the critical region. To solve this problem, Kunpeng BoostKit reconstructs the MySQL transaction manager. As shown in Figure 1, a lock-free hash table is used to maintain transaction units. In read/write scenarios, the synchronous lock mechanism is used to implement transaction isolation levels and multi-version concurrency control (MVCC), reducing lock conflicts and improving concurrency.
The MySQL transaction manager uses data structures, such as linked lists and arrays, to maintain global transaction records. Trx-sys is a global instance in MySQL. It maintains transaction system information, like the following transaction instance containers:
- rw_trx_list: a linked list of read-write transaction instances.
- mysql_trx_list: a linked list of all user transaction instances.
- rw_trx_ids: an array of read-write transaction IDs for quick copy of snapshots. It is implemented by using std::vector.
- rw_trx_set: mapping from transaction IDs to transaction instances. It is used to quickly locate a transaction instance based on a trx_id and is implemented by using std::set.
These containers, however, are not thread-safe. Sometimes, multiple data objects (including these containers and other data) in Trx-sys need to be synchronized. In the original code, Trx-sys->mutex is used to achieve this purpose. For example, to set a transaction as a read-write transaction through trx_set_rw_mode, the code is as follows:

In a high-concurrency write scenario, a large number of read/write transaction operations exist in the system, and the contention for Trx-sys->mutex becomes a throughput bottleneck.
Among the operations in the
In this feature, the lock-free hash rw_trx_hash is used to replace the function of rw_trx_set to reduce the time consumed in the critical region of Trx-sys->mutex and relieve the contention for Trx-sys->mutex, thereby improving system throughput. MySQL has used lock-free hash in performance schema and MDL. The original code has LF_HASH implementation and can be reused. Although thread security is ensured for rw_trx_hash access, the rw_trx_hash and data objects in the critical region are not synchronized because related operations have been moved out of the critical region. The logic must be correct during code implementation.
