13 commit after update

来源:互联网 发布:九丁软件 编辑:程序博客网 时间:2024/06/06 13:23

一、准备工作

1、准备知识

参考:《InnoDB 日志 回滚段 & 崩溃恢复实现详解——修改版》by 何登成



当事务提交时,与undo相关的操作:

insert undo:释放Rollback Segment Header Page中的Undo Slot;直接释放Insert_Undo对应的所有Undo Page,回收空间。

Update undo:释放Rollback Segment Header Page中的Undo Slot;将事务在Undo Log Header Page上当前事务的Undo Log Header链接到Rollback Segment 
Header Page上 ,等待Purge


2、实例运行

本例只看update undo,在客户端使用start transaction; update nkeys set c5=1 where c1=50003;  commit; 当commit后,就会发生如下的流程。


二、执行流程

dispatch_command-->mysql_execute_command---> trans_commit---> ha_commit_trans---->ha_commit_one_phase----> innobase_commit---> innobase_commit_low--->trx_commit_for_mysql--->trx_commit_off_kernel--->trx_write_serialisation_history---> trx_undo_update_cleanup---->trx_purge_add_update_undo_to_history

备注:

a.  对于trx_commit_off_kerne(),If the transaction made any updates then we need to write the UNDO logs for the updates to the assigned rollback segment, 于是调用lsn = trx_write_serialisation_history(trx)。之后trx->conc_state 由TRX_PREPARED变为TRX_COMMITTED_IN_MEMORY,再变为TRX_NOT_STARTE(可能在此之前需要写log),最后将trx从trx_sys->trx_list中移除。

b. trx_write_serialisation_history()中调用trx_serialisation_number_get(trx)获得trx->no,即max trx id when the transaction is moved to COMMITTED_IN_MEMORY state,猜测这是用于在purge线程中判断是否可以purge的。然后调用 trx_undo_update_cleanup(trx, undo_hdr_page, &mtr)。

c. trx_undo_update_cleanup() Adds the update undo log header as the first in the history list(调用trx_purge_add_update_undo_to_history(trx, undo_page, mtr)), and frees the memory object, or puts it to the list of cached update undo log segments(加入rseg->update_undo_cache)。

d. trx_purge_add_update_undo_to_history()调用flst_add_first(rseg_header + TRX_RSEG_HISTORY, undo_header + TRX_UNDO_HISTORY_NODE, mtr)将undo log header链入rseg_header中。

/** The undo log header. There can be several undo log headers on the firstpage of an update undo log segment. *//* @{ *//*-------------------------------------------------------------*/#defineTRX_UNDO_TRX_ID0/*!< Transaction id */#defineTRX_UNDO_TRX_NO8/*!< Transaction number of thetransaction; defined only if the logis in a history list */#define TRX_UNDO_DEL_MARKS16/*!< Defined only in an update undolog: TRUE if the transaction may havedone delete markings of records, andthus purge is necessary */#defineTRX_UNDO_LOG_START18/*!< Offset of the first undo log recordof this log on the header page; purgemay remove undo log record from thelog start, and therefore this is notnecessarily the same as this logheader end offset */#defineTRX_UNDO_XID_EXISTS20/*!< TRUE if undo log header includesX/Open XA transaction identificationXID */#defineTRX_UNDO_DICT_TRANS21/*!< TRUE if the transaction is a tablecreate, index create, or droptransaction: in recoverythe transaction cannot be rolled backin the usual way: a 'rollback' rathermeans dropping the created or droppedtable, if it still exists */#define TRX_UNDO_TABLE_ID22/*!< Id of the table if the precedingfield is TRUE */#defineTRX_UNDO_NEXT_LOG30/*!< Offset of the next undo log headeron this page, 0 if none */ //一个页中可能有多个undo log header#defineTRX_UNDO_PREV_LOG32/*!< Offset of the previous undo logheader on this page, 0 if none */#define TRX_UNDO_HISTORY_NODE34/*!< If the log is put to the historylist, the file list node is here *//*-------------------------------------------------------------*//** Size of the undo log header without XID information */#define TRX_UNDO_LOG_OLD_HDR_SIZE (34 + FLST_NODE_SIZE

三、purge

选择系统中最老的提交事务(所有Rollback Segment中最老提交事务,这是按提交顺序来排列的,即选择trx->no最小的,而不是事务本身trx->id);正向遍历事务的Update_Undo记录,彻底删除聚簇/二级索引上对应的DEL_BIT=1的项。


0 0