📄 trx0undo.c
字号:
/******************************************************Transaction undo log(c) 1996 Innobase OyCreated 3/26/1996 Heikki Tuuri*******************************************************/#include "trx0undo.h"#ifdef UNIV_NONINL#include "trx0undo.ic"#endif#include "fsp0fsp.h"#include "mach0data.h"#include "trx0rseg.h"#include "trx0trx.h"#include "srv0srv.h"#include "trx0rec.h"#include "trx0purge.h"#include "trx0xa.h"/* How should the old versions in the history list be managed? ----------------------------------------------------------If each transaction is given a whole page for its update undo log, filespace consumption can be 10 times higher than necessary. Therefore,partly filled update undo log pages should be reusable. But then thereis no way individual pages can be ordered so that the ordering agreeswith the serialization numbers of the transactions on the pages. Thus,the history list must be formed of undo logs, not their header pages asit was in the old implementation. However, on a single header page the transactions are placed inthe order of their serialization numbers. As old versions are purged, wemay free the page when the last transaction on the page has been purged. A problem is that the purge has to go through the transactionsin the serialization order. This means that we have to look through allrollback segments for the one that has the smallest transaction numberin its history list. When should we do a purge? A purge is necessary when space isrunning out in any of the rollback segments. Then we may have to purgealso old version which might be needed by some consistent read. How dowe trigger the start of a purge? When a transaction writes to an undo log,it may notice that the space is running out. When a read view is closed,it may make some history superfluous. The server can have an utility whichperiodically checks if it can purge some history. In a parallellized purge we have the problem that a query threadcan remove a delete marked clustered index record before another querythread has processed an earlier version of the record, which cannot thenbe done because the row cannot be constructed from the clustered indexrecord. To avoid this problem, we will store in the update and delete markundo record also the columns necessary to construct the secondary indexentries which are modified. We can latch the stack of versions of a single clustered index recordby taking a latch on the clustered index page. As long as the latch is held,no new versions can be added and no versions removed by undo. But, a purgecan still remove old versions from the bottom of the stack. *//* How to protect rollback segments, undo logs, and history lists with -------------------------------------------------------------------latches?-------The contention of the kernel mutex should be minimized. When a transactiondoes its first insert or modify in an index, an undo log is assigned for it.Then we must have an x-latch to the rollback segment header. When the transaction does more modifys or rolls back, the undo log isprotected with undo_mutex in the transaction. When the transaction commits, its insert undo log is either reset andcached for a fast reuse, or freed. In these cases we must have an x-latch onthe rollback segment page. The update undo log is put to the history list. Ifit is not suitable for reuse, its slot in the rollback segment is reset. Inboth cases, an x-latch must be acquired on the rollback segment. The purge operation steps through the history list without modifyingit until a truncate operation occurs, which can remove undo logs from the endof the list and release undo log segments. In stepping through the list,s-latches on the undo log pages are enough, but in a truncate, x-latches mustbe obtained on the rollback segment and individual pages. *//************************************************************************Initializes the fields in an undo log segment page. */staticvoidtrx_undo_page_init(/*================*/ page_t* undo_page, /* in: undo log segment page */ ulint type, /* in: undo log segment type */ mtr_t* mtr); /* in: mtr *//************************************************************************Creates and initializes an undo log memory object. */statictrx_undo_t*trx_undo_mem_create(/*================*/ /* out, own: the undo log memory object */ trx_rseg_t* rseg, /* in: rollback segment memory object */ ulint id, /* in: slot index within rseg */ ulint type, /* in: type of the log: TRX_UNDO_INSERT or TRX_UNDO_UPDATE */ dulint trx_id, /* in: id of the trx for which the undo log is created */ XID* xid, /* in: X/Open XA transaction identification*/ ulint page_no,/* in: undo log header page number */ ulint offset);/* in: undo log header byte offset on page *//*******************************************************************Initializes a cached insert undo log header page for new use. NOTE that thisfunction has its own log record type MLOG_UNDO_HDR_REUSE. You must NOT changethe operation of this function! */staticulinttrx_undo_insert_header_reuse(/*=========================*/ /* out: undo log header byte offset on page */ page_t* undo_page, /* in: insert undo log segment header page, x-latched */ dulint trx_id, /* in: transaction id */ mtr_t* mtr); /* in: mtr *//**************************************************************************If an update undo log can be discarded immediately, this function frees thespace, resetting the page to the proper state for caching. */staticvoidtrx_undo_discard_latest_update_undo(/*================================*/ page_t* undo_page, /* in: header page of an undo log of size 1 */ mtr_t* mtr); /* in: mtr *//***************************************************************************Gets the previous record in an undo log from the previous page. */statictrx_undo_rec_t*trx_undo_get_prev_rec_from_prev_page(/*=================================*/ /* out: undo log record, the page s-latched, NULL if none */ trx_undo_rec_t* rec, /* in: undo record */ ulint page_no,/* in: undo log header page number */ ulint offset, /* in: undo log header offset on page */ mtr_t* mtr) /* in: mtr */{ ulint prev_page_no; page_t* prev_page; page_t* undo_page; undo_page = buf_frame_align(rec); prev_page_no = flst_get_prev_addr(undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_NODE, mtr) .page; if (prev_page_no == FIL_NULL) { return(NULL); } prev_page = trx_undo_page_get_s_latched( buf_frame_get_space_id(undo_page), prev_page_no, mtr); return(trx_undo_page_get_last_rec(prev_page, page_no, offset));}/***************************************************************************Gets the previous record in an undo log. */trx_undo_rec_t*trx_undo_get_prev_rec(/*==================*/ /* out: undo log record, the page s-latched, NULL if none */ trx_undo_rec_t* rec, /* in: undo record */ ulint page_no,/* in: undo log header page number */ ulint offset, /* in: undo log header offset on page */ mtr_t* mtr) /* in: mtr */{ trx_undo_rec_t* prev_rec; prev_rec = trx_undo_page_get_prev_rec(rec, page_no, offset); if (prev_rec) { return(prev_rec); } /* We have to go to the previous undo log page to look for the previous record */ return(trx_undo_get_prev_rec_from_prev_page(rec, page_no, offset, mtr));}/***************************************************************************Gets the next record in an undo log from the next page. */statictrx_undo_rec_t*trx_undo_get_next_rec_from_next_page(/*=================================*/ /* out: undo log record, the page latched, NULL if none */ page_t* undo_page, /* in: undo log page */ ulint page_no,/* in: undo log header page number */ ulint offset, /* in: undo log header offset on page */ ulint mode, /* in: latch mode: RW_S_LATCH or RW_X_LATCH */ mtr_t* mtr) /* in: mtr */{ trx_ulogf_t* log_hdr; ulint next_page_no; page_t* next_page; ulint space; ulint next; if (page_no == buf_frame_get_page_no(undo_page)) { log_hdr = undo_page + offset; next = mach_read_from_2(log_hdr + TRX_UNDO_NEXT_LOG); if (next != 0) { return(NULL); } } space = buf_frame_get_space_id(undo_page); next_page_no = flst_get_next_addr(undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_NODE, mtr) .page; if (next_page_no == FIL_NULL) { return(NULL); } if (mode == RW_S_LATCH) { next_page = trx_undo_page_get_s_latched(space, next_page_no, mtr); } else { ut_ad(mode == RW_X_LATCH); next_page = trx_undo_page_get(space, next_page_no, mtr); } return(trx_undo_page_get_first_rec(next_page, page_no, offset));}/***************************************************************************Gets the next record in an undo log. */trx_undo_rec_t*trx_undo_get_next_rec(/*==================*/ /* out: undo log record, the page s-latched, NULL if none */ trx_undo_rec_t* rec, /* in: undo record */ ulint page_no,/* in: undo log header page number */ ulint offset, /* in: undo log header offset on page */ mtr_t* mtr) /* in: mtr */{ trx_undo_rec_t* next_rec; next_rec = trx_undo_page_get_next_rec(rec, page_no, offset); if (next_rec) { return(next_rec); } return(trx_undo_get_next_rec_from_next_page(buf_frame_align(rec), page_no, offset, RW_S_LATCH, mtr));}/***************************************************************************Gets the first record in an undo log. */trx_undo_rec_t*trx_undo_get_first_rec(/*===================*/ /* out: undo log record, the page latched, NULL if none */ ulint space, /* in: undo log header space */ ulint page_no,/* in: undo log header page number */ ulint offset, /* in: undo log header offset on page */ ulint mode, /* in: latching mode: RW_S_LATCH or RW_X_LATCH */ mtr_t* mtr) /* in: mtr */{ page_t* undo_page; trx_undo_rec_t* rec; if (mode == RW_S_LATCH) { undo_page = trx_undo_page_get_s_latched(space, page_no, mtr); } else { undo_page = trx_undo_page_get(space, page_no, mtr); } rec = trx_undo_page_get_first_rec(undo_page, page_no, offset); if (rec) { return(rec); } return(trx_undo_get_next_rec_from_next_page(undo_page, page_no, offset, mode, mtr));}/*============== UNDO LOG FILE COPY CREATION AND FREEING ==================*//**************************************************************************Writes the mtr log entry of an undo log page initialization. */UNIV_INLINEvoidtrx_undo_page_init_log(/*====================*/ page_t* undo_page, /* in: undo log page */ ulint type, /* in: undo log type */ mtr_t* mtr) /* in: mtr */{ mlog_write_initial_log_record(undo_page, MLOG_UNDO_INIT, mtr); mlog_catenate_ulint_compressed(mtr, type);} /***************************************************************Parses the redo log entry of an undo log page initialization. */byte*trx_undo_parse_page_init(/*======================*/ /* out: end of log record or NULL */ byte* ptr, /* in: buffer */ byte* end_ptr,/* in: buffer end */ page_t* page, /* in: page or NULL */ mtr_t* mtr) /* in: mtr or NULL */{ ulint type; ptr = mach_parse_compressed(ptr, end_ptr, &type); if (ptr == NULL) { return(NULL); } if (page) { trx_undo_page_init(page, type, mtr); } return(ptr);} /************************************************************************Initializes the fields in an undo log segment page. */staticvoidtrx_undo_page_init(/*================*/ page_t* undo_page, /* in: undo log segment page */ ulint type, /* in: undo log segment type */ mtr_t* mtr) /* in: mtr */{ trx_upagef_t* page_hdr; page_hdr = undo_page + TRX_UNDO_PAGE_HDR; mach_write_to_2(page_hdr + TRX_UNDO_PAGE_TYPE, type); mach_write_to_2(page_hdr + TRX_UNDO_PAGE_START, TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_HDR_SIZE); mach_write_to_2(page_hdr + TRX_UNDO_PAGE_FREE, TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_HDR_SIZE); fil_page_set_type(undo_page, FIL_PAGE_UNDO_LOG); trx_undo_page_init_log(undo_page, type, mtr);}/*******************************************************************Creates a new undo log segment in file. */staticpage_t*trx_undo_seg_create(/*================*/ /* out: segment header page x-latched, NULL if no space left */ trx_rseg_t* rseg __attribute__((unused)),/* in: rollback segment */ trx_rsegf_t* rseg_hdr,/* in: rollback segment header, page x-latched */ ulint type, /* in: type of the segment: TRX_UNDO_INSERT or TRX_UNDO_UPDATE */ ulint* id, /* out: slot index within rseg header */ mtr_t* mtr) /* in: mtr */{ ulint slot_no; ulint space; page_t* undo_page; trx_upagef_t* page_hdr; trx_usegf_t* seg_hdr; ulint n_reserved; ibool success; ut_ad(mtr && id && rseg_hdr);#ifdef UNIV_SYNC_DEBUG ut_ad(mutex_own(&(rseg->mutex)));#endif /* UNIV_SYNC_DEBUG *//* fputs(type == TRX_UNDO_INSERT ? "Creating insert undo log segment\n" : "Creating update undo log segment\n", stderr); */ slot_no = trx_rsegf_undo_find_free(rseg_hdr, mtr); if (slot_no == ULINT_UNDEFINED) { ut_print_timestamp(stderr); fprintf(stderr,"InnoDB: Warning: cannot find a free slot for an undo log. Do you have too\n""InnoDB: many active transactions running concurrently?\n"); return(NULL); } space = buf_frame_get_space_id(rseg_hdr); success = fsp_reserve_free_extents(&n_reserved, space, 2, FSP_UNDO, mtr); if (!success) { return(NULL); } /* Allocate a new file segment for the undo log */ undo_page = fseg_create_general(space, 0, TRX_UNDO_SEG_HDR + TRX_UNDO_FSEG_HEADER, TRUE, mtr); fil_space_release_free_extents(space, n_reserved); if (undo_page == NULL) { /* No space left */ return(NULL); }#ifdef UNIV_SYNC_DEBUG buf_page_dbg_add_level(undo_page, SYNC_TRX_UNDO_PAGE);#endif /* UNIV_SYNC_DEBUG */ page_hdr = undo_page + TRX_UNDO_PAGE_HDR; seg_hdr = undo_page + TRX_UNDO_SEG_HDR; trx_undo_page_init(undo_page, type, mtr); mlog_write_ulint(page_hdr + TRX_UNDO_PAGE_FREE, TRX_UNDO_SEG_HDR + TRX_UNDO_SEG_HDR_SIZE, MLOG_2BYTES, mtr); mlog_write_ulint(seg_hdr + TRX_UNDO_LAST_LOG, 0, MLOG_2BYTES, mtr); flst_init(seg_hdr + TRX_UNDO_PAGE_LIST, mtr); flst_add_last(seg_hdr + TRX_UNDO_PAGE_LIST, page_hdr + TRX_UNDO_PAGE_NODE, mtr); trx_rsegf_set_nth_undo(rseg_hdr, slot_no, buf_frame_get_page_no(undo_page), mtr); *id = slot_no; return(undo_page);}/**************************************************************************Writes the mtr log entry of an undo log header initialization. */UNIV_INLINEvoidtrx_undo_header_create_log(/*=======================*/ page_t* undo_page, /* in: undo log header page */ dulint trx_id, /* in: transaction id */ mtr_t* mtr) /* in: mtr */{ mlog_write_initial_log_record(undo_page, MLOG_UNDO_HDR_CREATE, mtr);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -