MySQL 26.7.0
Source Code Documentation
log0recv.cc File Reference

Recovery. More...

#include "ha_prototypes.h"
#include <my_aes.h>
#include <sys/types.h>
#include <ostream>
#include <array>
#include <iomanip>
#include <map>
#include <new>
#include <optional>
#include <span>
#include <string>
#include <vector>
#include "arch0arch.h"
#include "btr0btr.h"
#include "btr0cur.h"
#include "buf0buf.h"
#include "buf0flu.h"
#include "clone0api.h"
#include "dict0dd.h"
#include "fil0fil.h"
#include "fil0pages_persistence_interface.h"
#include "ibuf0ibuf.h"
#include "log0chkp.h"
#include "log0encryption.h"
#include "log0files_io.h"
#include "log0log.h"
#include "log0pre_8_0_30.h"
#include "log0recv.h"
#include "log0test.h"
#include "mem0mem.h"
#include "mtr0log.h"
#include "mtr0mtr.h"
#include "os0thread-create.h"
#include "page0cur.h"
#include "page0zip.h"
#include "trx0rec.h"
#include "trx0undo.h"
#include "ut0new.h"
#include "my_dbug.h"
#include "buf0rea.h"
#include "ddl0ddl.h"
#include "log0handler.h"
#include "srv0srv.h"
#include "srv0start.h"
#include "trx0purge.h"

Macros

#define RECV_DATA_BLOCK_SIZE   (MEM_MAX_ALLOC_IN_BUF - sizeof(recv_data_t))
 Log records are stored in the hash table in chunks at most of this size; this must be less than UNIV_PAGE_SIZE as it is stored in the buffer pool. More...
 

Functions

static bool recv_writer_is_active ()
 
static void recv_init_crash_recovery ()
 Initialize crash recovery environment. More...
 
lsn_t recv_calc_lsn_on_data_add (lsn_t lsn, os_offset_t len)
 Calculates the new value for lsn when more data is added to the log. More...
 
void recv_sys_create ()
 Creates the recovery system. More...
 
bool recv_sys_resize_buf ()
 Resize the recovery parsing buffer up to log_buffer_size. More...
 
static void recv_sys_finish ()
 Free up recovery data structures. More...
 
void recv_sys_close ()
 Release recovery system mutexes. More...
 
void recv_sys_var_init ()
 Reset the state of the recovery system variables. More...
 
static size_t recv_heap_used ()
 Get the number of bytes used by all the heaps. More...
 
static void recv_report_corrupt_log (std::span< const byte > buffer, size_t pos, lsn_t lsn)
 Prints diagnostic info of corrupt log. More...
 
void recv_sys_init ()
 Inits the recovery system for a recovery operation. More...
 
static void recv_sys_empty_hash ()
 Empties the hash table when it has been fully processed. More...
 
static recv_sys_t::Spacerecv_get_page_map (space_id_t space_id, bool create)
 Get the page map for a tablespace. More...
 
static recv_addr_trecv_get_rec (const page_id_t &page_id)
 Gets the list of log records for a <space, page>. More...
 
static void recv_writer_thread ()
 recv_writer thread tasked with flushing dirty pages from the buffer pools. More...
 
void recv_sys_free ()
 Frees the recovery system. More...
 
static void one_less_page_to_recover ()
 
static void recv_read_in_area (const page_id_t &requested_page_id, const page_size_t &page_size)
 Reads in pages which have hashed log records, from an area around a given page number. More...
 
static void recv_apply_log_rec (recv_addr_t *recv_addr)
 Apply the log records to a page. More...
 
static void recv_apply_hashed_log_recs ()
 Empties the hash table of stored log records, applying them to appropriate pages. More...
 
static void recv_add_to_hash_table (mlog_id_t type, space_id_t space_id, page_no_t page_no, std::span< const byte > body, lsn_t start_lsn, lsn_t end_lsn)
 Adds a new log record to the hash table of log records. More...
 
static void recv_data_copy_to_buf (byte *buf, recv_t *recv)
 Copies the log record body from recv to buf. More...
 
bool recv_page_is_brand_new (buf_block_t *block)
 Returns true if the page is brand new (the next log record is init_file_page or no records to apply). More...
 
static void apply_tablespace_side_effects (const ib::redo::Record_handle &record_handle, const ib::redo::Page_handle &page_handle)
 Apply tablespace metadata side-effects after page record application. More...
 
void recv_recover_page_func (bool just_read_in, buf_block_t *block)
 Applies the hashed log records to the page, if the page lsn is less than the lsn of a log record. More...
 
void recv_track_changes_of_recovered_lsn ()
 
static bool recv_process_space_record (const ib::redo::Record_view &rec, lsn_t start_lsn)
 Process a table space record. More...
 
static bool recv_process_page_record (const ib::redo::Record_view &rec, lsn_t start_lsn, lsn_t end_lsn)
 Process a page record. More...
 
static bool process_mtr (const ib::redo::Mtr_view &mtr, lsn_t start_lsn, lsn_t end_lsn)
 Process an mtr. More...
 
static std::optional< ib::redo::Mtr_viewparse_buffer (std::span< const byte > buffer)
 Parses the buffer starting from the beginning. More...
 
static bool process_mtr_and_possibly_apply_records (ib::redo::Mtr_view mtr)
 mtr buffer is processed if the start_lsn is more than checkpoint_lsn. More...
 
dberr_t recv_parse_and_apply_log_recs (size_t mem_limit)
 Parse log records from a buffer and stores them to a hash table. More...
 
static void recv_reset_buffer ()
 Moves the parsing buffer data left to the buffer start. More...
 
dberr_t recv_recovery_begin (lsn_t checkpoint_lsn)
 Scans log from a stream and stores new log data to the parsing buffer. More...
 
dberr_t recv_recovery_from_checkpoint_start (lsn_t flush_lsn)
 Recovers all tablespaces from the redo log. More...
 
MetadataRecoverrecv_recovery_from_checkpoint_finish (bool aborting)
 Complete the recovery from the latest checkpoint. More...
 
const char * get_mlog_string (mlog_id_t type)
 Return string name of the redo log record type. More...
 

Variables

static const size_t RECV_READ_AHEAD_AREA = 32
 Read-ahead area in applying log records to file pages. More...
 
recv_sys_trecv_sys = nullptr
 The recovery system. More...
 
volatile bool recv_recovery_on
 true when applying redo log records during crash recovery; false otherwise. More...
 
static const size_t PARSING_BUF_MINIMUM_SIZE = 2048
 
PSI_memory_key mem_log_recv_space_hash_key
 
bool recv_needed_recovery
 true when recv_init_crash_recovery() has been called. More...
 
bool recv_lsn_checks_on
 true if buf_page_is_corrupted() should check if the log sequence number (FIL_PAGE_LSN) is in the future. More...
 
static ulint recv_scan_print_counter
 The following counter is used to decide when to print info on log scan. More...
 
size_t recv_n_frames_for_pages_per_pool_instance
 This many blocks must be left in each Buffer Pool instance to be managed by the LRU when we scan the log and store the scanned log records in a hashmap allocated in the Buffer Pool in frames of non-LRU managed blocks. More...
 
static lsn_t recv_max_page_lsn
 The maximum lsn we see for a page during the recovery process. More...
 

Detailed Description

Recovery.

Created 9/20/1997 Heikki Tuuri

Macro Definition Documentation

◆ RECV_DATA_BLOCK_SIZE

#define RECV_DATA_BLOCK_SIZE   (MEM_MAX_ALLOC_IN_BUF - sizeof(recv_data_t))

Log records are stored in the hash table in chunks at most of this size; this must be less than UNIV_PAGE_SIZE as it is stored in the buffer pool.

Function Documentation

◆ apply_tablespace_side_effects()

static void apply_tablespace_side_effects ( const ib::redo::Record_handle record_handle,
const ib::redo::Page_handle page_handle 
)
static

Apply tablespace metadata side-effects after page record application.

This function handles tablespace-level side-effects that occur when applying certain redo log records.

Note
This function should be called immediately after redo_applier->apply()
Parameters
record_handleHandle containing the redo log record type and body
page_handleHandle containing the page buffer and metadata after the page modifications have been applied.

◆ get_mlog_string()

const char * get_mlog_string ( mlog_id_t  type)

Return string name of the redo log record type.

Parameters
[in]typerecord log record enum
Returns
string name of record log record

◆ one_less_page_to_recover()

static void one_less_page_to_recover ( )
static

◆ parse_buffer()

static std::optional< ib::redo::Mtr_view > parse_buffer ( std::span< const byte buffer)
static

Parses the buffer starting from the beginning.

If the buffer content causes parse error, it sets the found_corrupt_log flag to true. The function assumes recovered_lsn is the start_lsn of the mtr.

Parameters
[in]bufferThe buffer to parse
Returns
an mtr with parsed log records if parsing was successful, otherwise an empty container that indicates end of the processing. End of the processing may happen either due to a parsing error or, buffer does not start with a full mtr

◆ process_mtr()

static bool process_mtr ( const ib::redo::Mtr_view mtr,
lsn_t  start_lsn,
lsn_t  end_lsn 
)
static

Process an mtr.

Parameters
[in]mtrmtr to process
[in]start_lsnmtr start lsn
[in]end_lsnmtr end lsn
Returns
true on success, false otherwise.

◆ process_mtr_and_possibly_apply_records()

static bool process_mtr_and_possibly_apply_records ( ib::redo::Mtr_view  mtr)
static

mtr buffer is processed if the start_lsn is more than checkpoint_lsn.

The function assumes recovered_lsn is the start_lsn of the mtr. Processing involves the following two steps :

  • Optionally applying all earlier records from the hashmap : We follow the order that changes to the space has to happen before to the page because a change which relates to whole space also implicitly applies to each of its pages. As a consequence, changes to the page which have lsn smaller than the change to the space are applied before the change to the space.
  • Adding page records to hashmap and, additionally executing some actions like tablespace file manipulations, updating tablespace flags, dealing with encryption etc.
Parameters
[in]mtrmtr to process
Returns
true if end of processing

◆ recv_add_to_hash_table()

static void recv_add_to_hash_table ( mlog_id_t  type,
space_id_t  space_id,
page_no_t  page_no,
std::span< const byte body,
lsn_t  start_lsn,
lsn_t  end_lsn 
)
static

Adds a new log record to the hash table of log records.

Parameters
[in]typelog record type
[in]space_idTablespace id
[in]page_nopage number
[in]bodylog record body
[in]start_lsnstart lsn of the mtr
[in]end_lsnend lsn of the mtr

◆ recv_apply_hashed_log_recs()

static void recv_apply_hashed_log_recs ( )
static

Empties the hash table of stored log records, applying them to appropriate pages.

◆ recv_apply_log_rec()

static void recv_apply_log_rec ( recv_addr_t recv_addr)
static

Apply the log records to a page.

Parameters
[in,out]recv_addrRedo log records to apply

◆ recv_calc_lsn_on_data_add()

lsn_t recv_calc_lsn_on_data_add ( lsn_t  lsn,
os_offset_t  len 
)

Calculates the new value for lsn when more data is added to the log.

Parameters
[in]lsnOld LSN
[in]lenThis many bytes of data is added, log block headers not included
Returns
LSN after data addition

◆ recv_data_copy_to_buf()

static void recv_data_copy_to_buf ( byte buf,
recv_t recv 
)
static

Copies the log record body from recv to buf.

Parameters
[in]bufBuffer of length at least recv->len
[in]recvLog record

◆ recv_get_page_map()

static recv_sys_t::Space * recv_get_page_map ( space_id_t  space_id,
bool  create 
)
static

Get the page map for a tablespace.

It will create one if one isn't found.

Parameters
[in]space_idTablespace ID for which page map required.
[in]createfalse if lookup only
Returns
the space data or null if not found

◆ recv_get_rec()

static recv_addr_t * recv_get_rec ( const page_id_t page_id)
static

Gets the list of log records for a <space, page>.

Parameters
[in]page_idThe <Tablespace ID,Page number> pair
Returns
the redo log entries or nullptr if not found

◆ recv_heap_used()

static size_t recv_heap_used ( )
static

Get the number of bytes used by all the heaps.

Returns
number of bytes used

◆ recv_init_crash_recovery()

static void recv_init_crash_recovery ( )
static

Initialize crash recovery environment.

Can be called iff recv_needed_recovery == false.

◆ recv_page_is_brand_new()

bool recv_page_is_brand_new ( buf_block_t block)

Returns true if the page is brand new (the next log record is init_file_page or no records to apply).

Parameters
[in]blockbuffer block
Returns
true if brand new

◆ recv_parse_and_apply_log_recs()

dberr_t recv_parse_and_apply_log_recs ( size_t  mem_limit)

Parse log records from a buffer and stores them to a hash table.

Some (, none or all) of the oldest redo log records from the hash table may be applied as a side effect, when the hash table overflows or an mtr requires strict ordering.

Parameters
mem_limithash table threshold. max() value would skip applying the log records
Returns
DB_SUCCESS if all goes well

◆ recv_process_page_record()

static bool recv_process_page_record ( const ib::redo::Record_view rec,
lsn_t  start_lsn,
lsn_t  end_lsn 
)
static

Process a page record.

Parameters
[in]recrecord to process
[in]start_lsnstart lsn of mtr containing the record
[in]end_lsnend lsn of mtr containing the record
Returns
true on success, false otherwise.

◆ recv_process_space_record()

static bool recv_process_space_record ( const ib::redo::Record_view rec,
lsn_t  start_lsn 
)
static

Process a table space record.

Parameters
[in]recrecord to process
[in]start_lsnstart lsn of mtr containing the record
Returns
true on success, false otherwise.

◆ recv_read_in_area()

static void recv_read_in_area ( const page_id_t requested_page_id,
const page_size_t page_size 
)
static

Reads in pages which have hashed log records, from an area around a given page number.

Parameters
[in]requested_page_idThe page which has to be read in anyway, so we have an opportunity to read pages nearby.
[in]page_sizeSize of pages in this page's space

◆ recv_recover_page_func()

void recv_recover_page_func ( bool  just_read_in,
buf_block_t block 
)

Applies the hashed log records to the page, if the page lsn is less than the lsn of a log record.

This can be called when a buffer page has just been read in, or also for a page already in the buffer pool.

Parameters
[in]just_read_intrue if the IO handler calls this for a freshly read page
[in,out]blockbuffer block

◆ recv_recovery_begin()

dberr_t recv_recovery_begin ( lsn_t  checkpoint_lsn)

Scans log from a stream and stores new log data to the parsing buffer.

Parses and hashes the log records if new data found. Each time the hash table gets full (see max_mem), it will apply all the records from the hashmap to the pages ("a batch"), clearing the hashmap, and continuing this way till the end of stream.

Parameters
[in]checkpoint_lsnlog sequence number found in checkpoint header. May be inexact (in a middle of an mtr which we can ignore, as it is already applied to tablespace files) until which all redo log has been scanned

◆ recv_recovery_from_checkpoint_finish()

MetadataRecover * recv_recovery_from_checkpoint_finish ( bool  aborting)

Complete the recovery from the latest checkpoint.

Parameters
[in]abortingtrue if the server has to abort due to an error
Returns
recovered persistent metadata or nullptr if aborting

◆ recv_recovery_from_checkpoint_start()

dberr_t recv_recovery_from_checkpoint_start ( lsn_t  flush_lsn)

Recovers all tablespaces from the redo log.

See also
recv_recovery_from_checkpoint_finish
Parameters
[in]flush_lsnlsn stored at offset FIL_PAGE_FILE_FLUSH_LSN in the system tablespace header
Returns
error code or DB_SUCCESS

◆ recv_report_corrupt_log()

static void recv_report_corrupt_log ( std::span< const byte buffer,
size_t  pos,
lsn_t  lsn 
)
static

Prints diagnostic info of corrupt log.

Parameters
[in]bufferbuffer containing corrupted data
[in]posposition in buffer where the error occurred
[in]lsnlast processed lsn before the error occurred

◆ recv_reset_buffer()

static void recv_reset_buffer ( )
static

Moves the parsing buffer data left to the buffer start.

◆ recv_sys_close()

void recv_sys_close ( )

Release recovery system mutexes.

◆ recv_sys_create()

void recv_sys_create ( )

Creates the recovery system.

◆ recv_sys_empty_hash()

static void recv_sys_empty_hash ( )
static

Empties the hash table when it has been fully processed.

◆ recv_sys_finish()

static void recv_sys_finish ( )
static

Free up recovery data structures.

◆ recv_sys_free()

void recv_sys_free ( )

Frees the recovery system.

◆ recv_sys_init()

void recv_sys_init ( )

Inits the recovery system for a recovery operation.

◆ recv_sys_resize_buf()

bool recv_sys_resize_buf ( )

Resize the recovery parsing buffer up to log_buffer_size.

◆ recv_sys_var_init()

void recv_sys_var_init ( )

Reset the state of the recovery system variables.

◆ recv_track_changes_of_recovered_lsn()

void recv_track_changes_of_recovered_lsn ( )

◆ recv_writer_is_active()

static bool recv_writer_is_active ( )
static

◆ recv_writer_thread()

static void recv_writer_thread ( )
static

recv_writer thread tasked with flushing dirty pages from the buffer pools.

Variable Documentation

◆ mem_log_recv_space_hash_key

PSI_memory_key mem_log_recv_space_hash_key

◆ PARSING_BUF_MINIMUM_SIZE

const size_t PARSING_BUF_MINIMUM_SIZE = 2048
static

◆ recv_lsn_checks_on

bool recv_lsn_checks_on

true if buf_page_is_corrupted() should check if the log sequence number (FIL_PAGE_LSN) is in the future.

Initially false, and set by recv_recovery_from_checkpoint_start().

◆ recv_max_page_lsn

lsn_t recv_max_page_lsn
static

The maximum lsn we see for a page during the recovery process.

If this is bigger than the lsn we are able to scan up to, that is an indication that the recovery failed and the database may be corrupt.

◆ recv_n_frames_for_pages_per_pool_instance

size_t recv_n_frames_for_pages_per_pool_instance

This many blocks must be left in each Buffer Pool instance to be managed by the LRU when we scan the log and store the scanned log records in a hashmap allocated in the Buffer Pool in frames of non-LRU managed blocks.

We will use these free blocks to read in pages when we start applying the log records to the database.

◆ recv_needed_recovery

bool recv_needed_recovery

true when recv_init_crash_recovery() has been called.

◆ RECV_READ_AHEAD_AREA

const size_t RECV_READ_AHEAD_AREA = 32
static

Read-ahead area in applying log records to file pages.

◆ recv_recovery_on

volatile bool recv_recovery_on

true when applying redo log records during crash recovery; false otherwise.

Note that this is false while a background thread is rolling back incomplete transactions.

◆ recv_scan_print_counter

ulint recv_scan_print_counter
static

The following counter is used to decide when to print info on log scan.

◆ recv_sys

recv_sys_t* recv_sys = nullptr

The recovery system.