mirror of
https://github.com/MariaDB/server.git
synced 2025-01-17 04:22:27 +01:00
0b47c126e3
The approach to handling corruption that was chosen by Oracle in
commit 177d8b0c12
is not really useful. Not only did it actually fail to prevent InnoDB
from crashing, but it is making things worse by blocking attempts to
rescue data from or rebuild a partially readable table.
We will try to prevent crashes in a different way: by propagating
errors up the call stack. We will never mark the clustered index
persistently corrupted, so that data recovery may be attempted by
reading from the table, or by rebuilding the table.
This should also fix MDEV-13680 (crash on btr_page_alloc() failure);
it was extensively tested with innodb_file_per_table=0 and a
non-autoextend system tablespace.
We should now avoid crashes in many cases, such as when a page
cannot be read or allocated, or an inconsistency is detected when
attempting to update multiple pages. We will not crash on double-free,
such as on the recovery of DDL in system tablespace in case something
was corrupted.
Crashes on corrupted data are still possible. The fault injection mechanism
that is introduced in the subsequent commit may help catch more of them.
buf_page_import_corrupt_failure: Remove the fault injection, and instead
corrupt some pages using Perl code in the tests.
btr_cur_pessimistic_insert(): Always reserve extents (except for the
change buffer), in order to prevent a subsequent allocation failure.
btr_pcur_open_at_rnd_pos(): Merged to the only caller ibuf_merge_pages().
btr_assert_not_corrupted(), btr_corruption_report(): Remove.
Similar checks are already part of btr_block_get().
FSEG_MAGIC_N_BYTES: Replaces FSEG_MAGIC_N_VALUE.
dict_hdr_get(), trx_rsegf_get_new(), trx_undo_page_get(),
trx_undo_page_get_s_latched(): Replaced with error-checking calls.
trx_rseg_t::get(mtr_t*): Replaces trx_rsegf_get().
trx_rseg_header_create(): Let the caller update the TRX_SYS page if needed.
trx_sys_create_sys_pages(): Merged with trx_sysf_create().
dict_check_tablespaces_and_store_max_id(): Do not access
DICT_HDR_MAX_SPACE_ID, because it was already recovered in dict_boot().
Merge dict_check_sys_tables() with this function.
dir_pathname(): Replaces os_file_make_new_pathname().
row_undo_ins_remove_sec(): Do not modify the undo page by adding
a terminating NUL byte to the record.
btr_decryption_failed(): Report decryption failures
dict_set_corrupted_by_space(), dict_set_encrypted_by_space(),
dict_set_corrupted_index_cache_only(): Remove.
dict_set_corrupted(): Remove the constant parameter dict_locked=false.
Never flag the clustered index corrupted in SYS_INDEXES, because
that would deny further access to the table. It might be possible to
repair the table by executing ALTER TABLE or OPTIMIZE TABLE, in case
no B-tree leaf page is corrupted.
dict_table_skip_corrupt_index(), dict_table_next_uncorrupted_index(),
row_purge_skip_uncommitted_virtual_index(): Remove, and refactor
the callers to read dict_index_t::type only once.
dict_table_is_corrupted(): Remove.
dict_index_t::is_btree(): Determine if the index is a valid B-tree.
BUF_GET_NO_LATCH, BUF_EVICT_IF_IN_POOL: Remove.
UNIV_BTR_DEBUG: Remove. Any inconsistency will no longer trigger
assertion failures, but error codes being returned.
buf_corrupt_page_release(): Replaced with a direct call to
buf_pool.corrupted_evict().
fil_invalid_page_access_msg(): Never crash on an invalid read;
let the caller of buf_page_get_gen() decide.
btr_pcur_t::restore_position(): Propagate failure status to the caller
by returning CORRUPTED.
opt_search_plan_for_table(): Simplify the code.
row_purge_del_mark(), row_purge_upd_exist_or_extern_func(),
row_undo_ins_remove_sec_rec(), row_undo_mod_upd_del_sec(),
row_undo_mod_del_mark_sec(): Avoid mem_heap_create()/mem_heap_free()
when no secondary indexes exist.
row_undo_mod_upd_exist_sec(): Simplify the code.
row_upd_clust_step(), dict_load_table_one(): Return DB_TABLE_CORRUPT
if the clustered index (and therefore the table) is corrupted, similar
to what we do in row_insert_for_mysql().
fut_get_ptr(): Replace with buf_page_get_gen() calls.
buf_page_get_gen(): Return nullptr and *err=DB_CORRUPTION
if the page is marked as freed. For other modes than
BUF_GET_POSSIBLY_FREED or BUF_PEEK_IF_IN_POOL this will
trigger a debug assertion failure. For BUF_GET_POSSIBLY_FREED,
we will return nullptr for freed pages, so that the callers
can be simplified. The purge of transaction history will be
a new user of BUF_GET_POSSIBLY_FREED, to avoid crashes on
corrupted data.
buf_page_get_low(): Never crash on a corrupted page, but simply
return nullptr.
fseg_page_is_allocated(): Replaces fseg_page_is_free().
fts_drop_common_tables(): Return an error if the transaction
was rolled back.
fil_space_t::set_corrupted(): Report a tablespace as corrupted if
it was not reported already.
fil_space_t::io(): Invoke fil_space_t::set_corrupted() to report
out-of-bounds page access or other errors.
Clean up mtr_t::page_lock()
buf_page_get_low(): Validate the page identifier (to check for
recently read corrupted pages) after acquiring the page latch.
buf_page_t::read_complete(): Flag uninitialized (all-zero) pages
with DB_FAIL. Return DB_PAGE_CORRUPTED on page number mismatch.
mtr_t::defer_drop_ahi(): Renamed from mtr_defer_drop_ahi().
recv_sys_t::free_corrupted_page(): Only set_corrupt_fs()
if any log records exist for the page. We do not mind if read-ahead
produces corrupted (or all-zero) pages that were not actually needed
during recovery.
recv_recover_page(): Return whether the operation succeeded.
recv_sys_t::recover_low(): Simplify the logic. Check for recovery error.
Thanks to Matthias Leich for testing this extensively and to the
authors of https://rr-project.org for making it easy to diagnose
and fix any failures that were found during the testing.
129 lines
4.6 KiB
C++
129 lines
4.6 KiB
C++
/*****************************************************************************
|
|
|
|
Copyright (c) 1996, 2013, Oracle and/or its affiliates. All Rights Reserved.
|
|
Copyright (c) 2017, 2022, MariaDB Corporation.
|
|
|
|
This program is free software; you can redistribute it and/or modify it under
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
Foundation; version 2 of the License.
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
this program; if not, write to the Free Software Foundation, Inc.,
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
|
|
|
|
*****************************************************************************/
|
|
|
|
/**************************************************//**
|
|
@file include/trx0undo.ic
|
|
Transaction undo log
|
|
|
|
Created 3/26/1996 Heikki Tuuri
|
|
*******************************************************/
|
|
|
|
#include "data0type.h"
|
|
#include "page0page.h"
|
|
|
|
/***********************************************************************//**
|
|
Builds a roll pointer.
|
|
@return roll pointer */
|
|
UNIV_INLINE
|
|
roll_ptr_t
|
|
trx_undo_build_roll_ptr(
|
|
/*====================*/
|
|
bool is_insert, /*!< in: TRUE if insert undo log */
|
|
ulint rseg_id, /*!< in: rollback segment id */
|
|
uint32_t page_no, /*!< in: page number */
|
|
uint16_t offset) /*!< in: offset of the undo entry within page */
|
|
{
|
|
compile_time_assert(DATA_ROLL_PTR_LEN == 7);
|
|
ut_ad(rseg_id < TRX_SYS_N_RSEGS);
|
|
|
|
return roll_ptr_t{is_insert} << ROLL_PTR_INSERT_FLAG_POS |
|
|
roll_ptr_t{rseg_id} << ROLL_PTR_RSEG_ID_POS |
|
|
roll_ptr_t{page_no} << ROLL_PTR_PAGE_POS | offset;
|
|
}
|
|
|
|
/***********************************************************************//**
|
|
Decodes a roll pointer. */
|
|
UNIV_INLINE
|
|
void
|
|
trx_undo_decode_roll_ptr(
|
|
/*=====================*/
|
|
roll_ptr_t roll_ptr, /*!< in: roll pointer */
|
|
bool* is_insert, /*!< out: TRUE if insert undo log */
|
|
ulint* rseg_id, /*!< out: rollback segment id */
|
|
uint32_t* page_no, /*!< out: page number */
|
|
uint16_t* offset) /*!< out: offset of the undo
|
|
entry within page */
|
|
{
|
|
compile_time_assert(DATA_ROLL_PTR_LEN == 7);
|
|
ut_ad(roll_ptr < (1ULL << 56));
|
|
*offset= static_cast<uint16_t>(roll_ptr);
|
|
*page_no= static_cast<uint32_t>(roll_ptr >> 16);
|
|
*rseg_id= static_cast<ulint>(roll_ptr >> 48 & 0x7F);
|
|
*is_insert= static_cast<bool>(roll_ptr >> 55);
|
|
}
|
|
|
|
/***********************************************************************//**
|
|
Determine if DB_ROLL_PTR is of the insert type.
|
|
@return true if insert */
|
|
UNIV_INLINE
|
|
bool
|
|
trx_undo_roll_ptr_is_insert(
|
|
/*========================*/
|
|
roll_ptr_t roll_ptr) /*!< in: roll pointer */
|
|
{
|
|
compile_time_assert(DATA_ROLL_PTR_LEN == 7);
|
|
ut_ad(roll_ptr < (1ULL << (ROLL_PTR_INSERT_FLAG_POS + 1)));
|
|
return static_cast<bool>(roll_ptr >> ROLL_PTR_INSERT_FLAG_POS);
|
|
}
|
|
|
|
/***********************************************************************//**
|
|
Returns true if the record is of the insert type.
|
|
@return true if the record was freshly inserted (not updated). */
|
|
UNIV_INLINE
|
|
bool
|
|
trx_undo_trx_id_is_insert(
|
|
/*======================*/
|
|
const byte* trx_id) /*!< in: DB_TRX_ID, followed by DB_ROLL_PTR */
|
|
{
|
|
compile_time_assert(DATA_TRX_ID + 1 == DATA_ROLL_PTR);
|
|
return bool(trx_id[DATA_TRX_ID_LEN] >> 7);
|
|
}
|
|
|
|
/** Determine the end offset of undo log records of an undo log page.
|
|
@param[in] undo_page undo log page
|
|
@param[in] page_no undo log header page number
|
|
@param[in] offset undo log header offset
|
|
@return end offset */
|
|
inline
|
|
uint16_t trx_undo_page_get_end(const buf_block_t *undo_page, uint32_t page_no,
|
|
uint16_t offset)
|
|
{
|
|
if (page_no == undo_page->page.id().page_no())
|
|
if (uint16_t end = mach_read_from_2(TRX_UNDO_NEXT_LOG + offset +
|
|
undo_page->page.frame))
|
|
return end;
|
|
|
|
return mach_read_from_2(TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE +
|
|
undo_page->page.frame);
|
|
}
|
|
|
|
/** Get the next record in an undo log.
|
|
@param[in] undo_page undo log page
|
|
@param[in] rec undo record offset in the page
|
|
@param[in] page_no undo log header page number
|
|
@param[in] offset undo log header offset on page
|
|
@return undo log record, the page latched, NULL if none */
|
|
inline trx_undo_rec_t*
|
|
trx_undo_page_get_next_rec(const buf_block_t *undo_page, uint16_t rec,
|
|
uint32_t page_no, uint16_t offset)
|
|
{
|
|
uint16_t end= trx_undo_page_get_end(undo_page, page_no, offset);
|
|
uint16_t next= mach_read_from_2(undo_page->page.frame + rec);
|
|
return next == end ? nullptr : undo_page->page.frame + next;
|
|
}
|