mirror of
https://github.com/MariaDB/server.git
synced 2026-10-23 18:49:29 +03:00
Problem: ======= A transaction being rolled back during recovery holds LOCK_IX on the table(not the metadata locks), and the rollback thread holds a reference on it. An online ALTER TABLE on that table falls back to acquiring LOCK_S, which conflicts and fails. prepare_inplace_alter_table_dict() asserted that the reference count is 1 before checking whether the table lock was acquired, so the reference still held by the rollback thread makes the assertion fail. Solution: ======== recovery_mdl: New class that holds the metadata locks which trx_resurrect_table_locks() acquires for recovered transactions, together with the background connection that owns them. One MDL_ticket is acquired per table and shared by every recovered transaction that modified the table, with a reference count, so that the lock is released once the last of those transactions has been completed. trx_sys_t::recovery: Pointer to recovery_mdl, wrapped in std::atomic, next to rw_trx_hash, which trx_t::free() accesses anyway. Null pointer means that no recovered transaction holds metadata locks, which is the normal state once the recovered transaction have been completed. trx_lists_init_at_db_start(): Create recovery_mdl and publish it in trx_sys.recovery before any metadata lock can be acquired, and delete it again if no recovered transaction acquired any. recovery_mdl::release(): Drop the references of a transaction, releasing each metadata lock whose last reference is dropped. This is the only place that releases them, so that a transaction which was rolled back by trx_rollback_recovered(false) will not keep its locks until shutdown. trx_resurrect_table_locks(): Acquire a shared metadata lock on each table that the recovered transaction had modified, unless a lock on the table was acquired for an earlier one, and reference it for this transaction. Record every table in trx_t::mod_tables, so that trx_t::commit_in_memory() will find it. This includes transactions that were recovered in the XA PREPARED state. They are completed by a user connection, which may issue XA COMMIT or XA ROLLBACK at any time, or not at all before the next shutdown. Until then, DDL must be blocked on the tables that they had modified. trx_resurrect_table_lock(): Resurrect one table lock and record the table in trx_t::mod_tables. trx_sys_t::recovery_release(): Release the metadata locks that were acquired for a recovered transaction, and delete trx_sys.recovery if this was the last recovered transaction that held any. It is invoked by trx_t::commit_in_memory(), while trx_t::mod_tables and trx_t::is_recovered are still valid. This is the only place that releases the locks of a transaction, so that a transaction which was rolled back by trx_rollback_recovered(false) will not keep its locks until shutdown. trx_sys_t::recovery_release_table(): Drop the reference that a recovered transaction holds on the metadata lock of a table, because its trx_t::mod_tables entry is being removed by lock_release_on_rollback(). trx_sys_t::recovery_prune_low(): Detach trx_sys.recovery once no recovered transaction holds metadata locks any longer, so that neither the locks nor the connection will outlive the recovered transactions. The caller will delete the object outside recovery_mutex. This is skipped when the caller has a current_thd, such as a user connection that is completing a transaction which was recovered in the XA PREPARED state, and during shutdown, because destroy_background_thd() would clear the current_thd of the caller. trx_sys_t::recovery_shutdown(): Destroy trx_sys.recovery, releasing anything that is left. It is invoked at the end of trx_sys_t::close() only, after every transaction has been freed. No other thread can be accessing trx_sys.recovery, and therefore recovery_mutex need not be acquired. trx_sys_t::recovery_thd(): Return the background connection, for the rollback_wait debug injection in row_undo_mod(). prepare_inplace_alter_table_dict(): Remove the acquisition of an InnoDB table lock for online ADD INDEX when a recovered transaction holds a lock on the table.