/********************************************************************//**
Removes unnecessary history data from a rollback segment. */
static
void
trx_purge_truncate_rseg_history(
    /*============================*/
    trx_rseg_t*	rseg,		/*!< in: rollback segment */
    trx_id_t	limit_trx_no,	/*!< in: remove update undo logs whose
					trx number is < limit_trx_no */
    undo_no_t	limit_undo_no)	/*!< in: if transaction number is equal
					to limit_trx_no, truncate undo records
					with undo number < limit_undo_no */
{
    fil_addr_t	hdr_addr;
    fil_addr_t	prev_hdr_addr;
    trx_rsegf_t*	rseg_hdr;
    page_t*		undo_page;
    trx_ulogf_t*	log_hdr;
    trx_usegf_t*	seg_hdr;
    ulint		n_removed_logs	= 0;
    mtr_t		mtr;
    trx_id_t	undo_trx_no;

    mtr_start(&mtr);
    mutex_enter(&(rseg->mutex));

    rseg_hdr = trx_rsegf_get(rseg->space, rseg->zip_size,
                             rseg->page_no, &mtr);

    hdr_addr = trx_purge_get_log_from_hist(
                   flst_get_last(rseg_hdr + TRX_RSEG_HISTORY, &mtr));
loop:
    if (hdr_addr.page == FIL_NULL) {

        mutex_exit(&(rseg->mutex));

        mtr_commit(&mtr);

        return;
    }

    undo_page = trx_undo_page_get(rseg->space, rseg->zip_size,
                                  hdr_addr.page, &mtr);

    log_hdr = undo_page + hdr_addr.boffset;
    undo_trx_no = mach_read_from_8(log_hdr + TRX_UNDO_TRX_NO);

    if (undo_trx_no >= limit_trx_no) {
        if (undo_trx_no == limit_trx_no) {
            trx_undo_truncate_start(rseg, rseg->space,
                                    hdr_addr.page,
                                    hdr_addr.boffset,
                                    limit_undo_no);
        }

        mutex_enter(&kernel_mutex);
        ut_a(trx_sys->rseg_history_len >= n_removed_logs);
        trx_sys->rseg_history_len -= n_removed_logs;
        mutex_exit(&kernel_mutex);

        flst_truncate_end(rseg_hdr + TRX_RSEG_HISTORY,
                          log_hdr + TRX_UNDO_HISTORY_NODE,
                          n_removed_logs, &mtr);

        mutex_exit(&(rseg->mutex));
        mtr_commit(&mtr);

        return;
    }

    prev_hdr_addr = trx_purge_get_log_from_hist(
                        flst_get_prev_addr(log_hdr + TRX_UNDO_HISTORY_NODE, &mtr));
    n_removed_logs++;

    seg_hdr = undo_page + TRX_UNDO_SEG_HDR;

    if ((mach_read_from_2(seg_hdr + TRX_UNDO_STATE) == TRX_UNDO_TO_PURGE)
            && (mach_read_from_2(log_hdr + TRX_UNDO_NEXT_LOG) == 0)) {

        /* We can free the whole log segment */

        mutex_exit(&(rseg->mutex));
        mtr_commit(&mtr);

        trx_purge_free_segment(rseg, hdr_addr, n_removed_logs);

        n_removed_logs = 0;
    } else {
        mutex_exit(&(rseg->mutex));
        mtr_commit(&mtr);
    }

    mtr_start(&mtr);
    mutex_enter(&(rseg->mutex));

    rseg_hdr = trx_rsegf_get(rseg->space, rseg->zip_size,
                             rseg->page_no, &mtr);

    hdr_addr = prev_hdr_addr;

    goto loop;
}
/********************************************************************//**
Adds the update undo log as the first log in the history list. Removes the
update undo log segment from the rseg slot if it is too big for reuse. */
UNIV_INTERN
void
trx_purge_add_update_undo_to_history(
    /*=================================*/
    trx_t*	trx,		/*!< in: transaction */
    page_t*	undo_page,	/*!< in: update undo log header page,
				x-latched */
    mtr_t*	mtr)		/*!< in: mtr */
{
    trx_undo_t*	undo;
    trx_rsegf_t*	rseg_header;
    trx_ulogf_t*	undo_header;

    undo = trx->update_undo;

    ut_ad(undo);

    ut_ad(mutex_own(&undo->rseg->mutex));

    rseg_header = trx_rsegf_get(
                      undo->rseg->space, undo->rseg->zip_size, undo->rseg->page_no,
                      mtr);

    undo_header = undo_page + undo->hdr_offset;
    /* Add the log as the first in the history list */

    if (undo->state != TRX_UNDO_CACHED) {
        ulint		hist_size;
#ifdef UNIV_DEBUG
        trx_usegf_t*	seg_header = undo_page + TRX_UNDO_SEG_HDR;
#endif /* UNIV_DEBUG */

        /* The undo log segment will not be reused */

        if (UNIV_UNLIKELY(undo->id >= TRX_RSEG_N_SLOTS)) {
            fprintf(stderr,
                    "InnoDB: Error: undo->id is %lu\n",
                    (ulong) undo->id);
            ut_error;
        }

        trx_rsegf_set_nth_undo(rseg_header, undo->id, FIL_NULL, mtr);

        hist_size = mtr_read_ulint(
                        rseg_header + TRX_RSEG_HISTORY_SIZE, MLOG_4BYTES, mtr);

        ut_ad(undo->size == flst_get_len(
                  seg_header + TRX_UNDO_PAGE_LIST, mtr));

        mlog_write_ulint(
            rseg_header + TRX_RSEG_HISTORY_SIZE,
            hist_size + undo->size, MLOG_4BYTES, mtr);
    }

    flst_add_first(
        rseg_header + TRX_RSEG_HISTORY,
        undo_header + TRX_UNDO_HISTORY_NODE, mtr);

    /* Write the trx number to the undo log header */

    mlog_write_ull(undo_header + TRX_UNDO_TRX_NO, trx->no, mtr);

    /* Write information about delete markings to the undo log header */

    if (!undo->del_marks) {
        mlog_write_ulint(
            undo_header + TRX_UNDO_DEL_MARKS, FALSE,
            MLOG_2BYTES, mtr);
    }

    if (undo->rseg->last_page_no == FIL_NULL) {
        undo->rseg->last_trx_no = trx->no;
        undo->rseg->last_offset = undo->hdr_offset;
        undo->rseg->last_page_no = undo->hdr_page_no;
        undo->rseg->last_del_marks = undo->del_marks;

        /* FIXME: Add a bin heap validate function to check that
        the rseg exists. */
    }

    mutex_enter(&kernel_mutex);
    trx_sys->rseg_history_len++;
    mutex_exit(&kernel_mutex);

//	if (!(trx_sys->rseg_history_len % srv_purge_batch_size)) { /*should wake up always*/
    /* Inform the purge thread that there is work to do. */
    srv_wake_purge_thread_if_not_active();
//	}
}
/**********************************************************************//**
Frees an undo log segment which is in the history list. Cuts the end of the
history list at the youngest undo log in this segment. */
static
void
trx_purge_free_segment(
    /*===================*/
    trx_rseg_t*	rseg,		/*!< in: rollback segment */
    fil_addr_t	hdr_addr,	/*!< in: the file address of log_hdr */
    ulint		n_removed_logs)	/*!< in: count of how many undo logs we
					will cut off from the end of the
					history list */
{
    page_t*		undo_page;
    trx_rsegf_t*	rseg_hdr;
    trx_ulogf_t*	log_hdr;
    trx_usegf_t*	seg_hdr;
    ibool		freed;
    ulint		seg_size;
    ulint		hist_size;
    ibool		marked		= FALSE;
    mtr_t		mtr;

    /*	fputs("Freeing an update undo log segment\n", stderr); */

loop:
    mtr_start(&mtr);
    mutex_enter(&(rseg->mutex));

    rseg_hdr = trx_rsegf_get(rseg->space, rseg->zip_size,
                             rseg->page_no, &mtr);

    undo_page = trx_undo_page_get(rseg->space, rseg->zip_size,
                                  hdr_addr.page, &mtr);
    seg_hdr = undo_page + TRX_UNDO_SEG_HDR;
    log_hdr = undo_page + hdr_addr.boffset;

    /* Mark the last undo log totally purged, so that if the system
    crashes, the tail of the undo log will not get accessed again. The
    list of pages in the undo log tail gets inconsistent during the
    freeing of the segment, and therefore purge should not try to access
    them again. */

    if (!marked) {
        mlog_write_ulint(log_hdr + TRX_UNDO_DEL_MARKS, FALSE,
                         MLOG_2BYTES, &mtr);
        marked = TRUE;
    }

    freed = fseg_free_step_not_header(seg_hdr + TRX_UNDO_FSEG_HEADER,
                                      &mtr);
    if (!freed) {
        mutex_exit(&(rseg->mutex));
        mtr_commit(&mtr);

        goto loop;
    }

    /* The page list may now be inconsistent, but the length field
    stored in the list base node tells us how big it was before we
    started the freeing. */

    seg_size = flst_get_len(seg_hdr + TRX_UNDO_PAGE_LIST, &mtr);

    /* We may free the undo log segment header page; it must be freed
    within the same mtr as the undo log header is removed from the
    history list: otherwise, in case of a database crash, the segment
    could become inaccessible garbage in the file space. */

    flst_cut_end(rseg_hdr + TRX_RSEG_HISTORY,
                 log_hdr + TRX_UNDO_HISTORY_NODE, n_removed_logs, &mtr);

    mutex_enter(&kernel_mutex);
    ut_ad(trx_sys->rseg_history_len >= n_removed_logs);
    trx_sys->rseg_history_len -= n_removed_logs;
    mutex_exit(&kernel_mutex);

    freed = FALSE;

    while (!freed) {
        /* Here we assume that a file segment with just the header
        page can be freed in a few steps, so that the buffer pool
        is not flooded with bufferfixed pages: see the note in
        fsp0fsp.c. */

        freed = fseg_free_step(seg_hdr + TRX_UNDO_FSEG_HEADER,
                               &mtr);
    }

    hist_size = mtr_read_ulint(rseg_hdr + TRX_RSEG_HISTORY_SIZE,
                               MLOG_4BYTES, &mtr);
    ut_ad(hist_size >= seg_size);

    mlog_write_ulint(rseg_hdr + TRX_RSEG_HISTORY_SIZE,
                     hist_size - seg_size, MLOG_4BYTES, &mtr);

    ut_ad(rseg->curr_size >= seg_size);

    rseg->curr_size -= seg_size;

    mutex_exit(&(rseg->mutex));

    mtr_commit(&mtr);
}
Beispiel #4
0
/********************************************************************//**
Adds the update undo log as the first log in the history list. Removes the
update undo log segment from the rseg slot if it is too big for reuse. */
UNIV_INTERN
void
trx_purge_add_update_undo_to_history(
/*=================================*/
	trx_t*	trx,		/*!< in: transaction */
	page_t*	undo_page,	/*!< in: update undo log header page,
				x-latched */
	mtr_t*	mtr)		/*!< in: mtr */
{
	trx_undo_t*	undo;
	trx_rseg_t*	rseg;
	trx_rsegf_t*	rseg_header;
#ifdef UNIV_DEBUG
	trx_usegf_t*	seg_header;
#endif /* UNIV_DEBUG */
	trx_ulogf_t*	undo_header;
	ulint		hist_size;

	undo = trx->update_undo;

	ut_ad(undo);

	rseg = undo->rseg;

	ut_ad(mutex_own(&(rseg->mutex)));

	rseg_header = trx_rsegf_get(rseg->space, rseg->zip_size,
				    rseg->page_no, mtr);

	undo_header = undo_page + undo->hdr_offset;
#ifdef UNIV_DEBUG
	seg_header  = undo_page + TRX_UNDO_SEG_HDR;
#endif /* UNIV_DEBUG */

	if (undo->state != TRX_UNDO_CACHED) {
		/* The undo log segment will not be reused */

		if (undo->id >= TRX_RSEG_N_SLOTS) {
			fprintf(stderr,
				"InnoDB: Error: undo->id is %lu\n",
				(ulong) undo->id);
			ut_error;
		}

		trx_rsegf_set_nth_undo(rseg_header, undo->id, FIL_NULL, mtr);

		hist_size = mtr_read_ulint(rseg_header + TRX_RSEG_HISTORY_SIZE,
					   MLOG_4BYTES, mtr);
		ut_ad(undo->size == flst_get_len(
			      seg_header + TRX_UNDO_PAGE_LIST, mtr));

		mlog_write_ulint(rseg_header + TRX_RSEG_HISTORY_SIZE,
				 hist_size + undo->size, MLOG_4BYTES, mtr);
	}

	/* Add the log as the first in the history list */
	flst_add_first(rseg_header + TRX_RSEG_HISTORY,
		       undo_header + TRX_UNDO_HISTORY_NODE, mtr);
	mutex_enter(&kernel_mutex);
	trx_sys->rseg_history_len++;
	mutex_exit(&kernel_mutex);

	/* Write the trx number to the undo log header */
	mlog_write_dulint(undo_header + TRX_UNDO_TRX_NO, trx->no, mtr);
	/* Write information about delete markings to the undo log header */

	if (!undo->del_marks) {
		mlog_write_ulint(undo_header + TRX_UNDO_DEL_MARKS, FALSE,
				 MLOG_2BYTES, mtr);
	}

	if (rseg->last_page_no == FIL_NULL) {

		rseg->last_page_no = undo->hdr_page_no;
		rseg->last_offset = undo->hdr_offset;
		rseg->last_trx_no = trx->no;
		rseg->last_del_marks = undo->del_marks;
	}
}