MySQL 8.0.37
Source Code Documentation
binlog::Binlog_recovery Class Reference

Recovers from last crashed binlog at server start. More...

#include <recovery.h>

Public Member Functions

 Binlog_recovery (Binlog_file_reader &binlog_file_reader)
 Class constructor. More...
 
virtual ~Binlog_recovery ()=default
 
my_off_t get_valid_pos () const
 Retrieves the position of the last binlog event that ended a transaction. More...
 
bool has_failures () const
 Retrieves whether or not the recovery process ended successfully. More...
 
bool is_binlog_malformed () const
 Retrieves whether or not the binary log was correctly processed in full. More...
 
bool has_engine_recovery_failed () const
 Retrieves whether or not the storage engines XA recovery process completed successfully. More...
 
std::string const & get_failure_message () const
 Retrieves the textual representation of the encontered failure, if any. More...
 
Binlog_recoveryrecover ()
 Uses the provided binary log file reader to inspect the binary log and extract transaction information. More...
 

Private Member Functions

void process_query_event (Query_log_event const &ev)
 Invoked when a Query_log_event is read from the binary log file reader. More...
 
void process_xid_event (Xid_log_event const &ev)
 Invoked when a Xid_log_event is read from the binary log file reader. More...
 
void process_xa_prepare_event (XA_prepare_log_event const &ev)
 Invoked when a XA_prepare_log_event is read from the binary log file reader. More...
 
void process_start ()
 Invoked when a BEGIN or an ‘XA START’ is found in a Query_log_event. More...
 
void process_commit ()
 Invoked when a COMMIT is found in a Query_log_event. More...
 
void process_rollback ()
 Invoked when a ROLLBACK is found in a Query_log_event. More...
 
void process_atomic_ddl (Query_log_event const &ev)
 Invoked when a DDL is found in a Query_log_event. More...
 
void process_xa_commit (std::string const &query)
 Invoked when an XA COMMIT is found in a Query_log_event. More...
 
void process_xa_rollback (std::string const &query)
 Invoked when an XA ROLLBACK is found in a Query_log_event. More...
 
void add_external_xid (std::string const &query, enum_ha_recover_xa_state state)
 Parses the provided string for an XID and adds it to the externally coordinated transactions map, along side the provided state. More...
 

Private Attributes

Binlog_file_readerm_reader
 File reader for the last available binary log file. More...
 
my_off_t m_valid_pos {0}
 Position of the last binlog event that ended a transaction. More...
 
bool m_in_transaction {false}
 Whether or not the event being processed is within a transaction. More...
 
bool m_is_malformed {false}
 Whether or not the binary log is malformed/corrupted. More...
 
bool m_no_engine_recovery {false}
 Whether or not the recovery in the storage engines failed. More...
 
std::string m_failure_message {""}
 Textual representation of the encountered failure. More...
 
MEM_ROOT m_mem_root
 Memory pool to use for the XID lists. More...
 
Mem_root_allocator< my_xidm_set_alloc
 Memory pool allocator to use with the normal transaction list. More...
 
Mem_root_allocator< std::pair< const XID, XID_STATE::xa_states > > m_map_alloc
 Memory pool allocator to use with the XA transaction list. More...
 
Xid_commit_list m_internal_xids
 List of normal transactions fully written to the binary log. More...
 
Xa_state_list::list m_external_xids
 List of XA transactions and states that appear in the binary log. More...
 

Detailed Description

Recovers from last crashed binlog at server start.

After a crash, storage engines may contain transactions that are prepared but not committed (in theory any engine, in practice InnoDB). This classe's methods use the binary log as the source of truth to determine which of these transactions should be committed and which should be rolled back.

The Binlog::recovery() method collects the following from the last available binary log:

  • the list of internally coordinated transactions (normal) that are completely written to the binary log.
  • the list of externally coordinated transactions (XA) that appear in the binary log, along the state those transactions are in.

The list of XIDs of all internally coordinated transactions that are completely written to the binary log is passed to the storage engines through the ha_recover function in the handler interface. This tells the storage engines to commit all prepared transactions that are in the set, and to roll back all prepared transactions that are not in the set.

The list of XIDs of all externally coordinated transactions that appear in the binary log, along with the state they are in, is passed to the storage engines through the ha_recover function in the handler interface. The storage engine will determine if the transaction is to be kept at PREPARE, is to be COMMITTED or ROLLED BACK, in accordance with: the state that is provided in the list; the internal storage engine state for the transaction.

Constructor & Destructor Documentation

◆ Binlog_recovery()

binlog::Binlog_recovery::Binlog_recovery ( Binlog_file_reader binlog_file_reader)

Class constructor.

Parameters
binlog_file_readerThe already instantiated and initialized file reader for the last available binary log file.

◆ ~Binlog_recovery()

virtual binlog::Binlog_recovery::~Binlog_recovery ( )
virtualdefault

Member Function Documentation

◆ add_external_xid()

void binlog::Binlog_recovery::add_external_xid ( std::string const &  query,
enum_ha_recover_xa_state  state 
)
private

Parses the provided string for an XID and adds it to the externally coordinated transactions map, along side the provided state.

Parameters
queryThe query to search and retrieve the XID from
stateThe state to add to the map, along side the XID

◆ get_failure_message()

std::string const & binlog::Binlog_recovery::get_failure_message ( ) const

Retrieves the textual representation of the encontered failure, if any.

Returns
the string containing the textual representation of the failure, an empty string otherwise.

◆ get_valid_pos()

my_off_t binlog::Binlog_recovery::get_valid_pos ( ) const

Retrieves the position of the last binlog event that ended a transaction.

Returns
The position of the last binlog event that ended a transaction.

◆ has_engine_recovery_failed()

bool binlog::Binlog_recovery::has_engine_recovery_failed ( ) const

Retrieves whether or not the storage engines XA recovery process completed successfully.

Returns
false if the storge engines completed the XA recovery process successfully, true otherwise.

◆ has_failures()

bool binlog::Binlog_recovery::has_failures ( ) const

Retrieves whether or not the recovery process ended successfully.

See also
Binlog_recovery::is_binlog_malformed()
Binlog_recovery::has_engine_recovery_failed()
Returns
true if the recovery process ended with errors, false otherwise.

◆ is_binlog_malformed()

bool binlog::Binlog_recovery::is_binlog_malformed ( ) const

Retrieves whether or not the binary log was correctly processed in full.

Returns
true if the binary log processing ended with errors, false otherwise.

◆ process_atomic_ddl()

void binlog::Binlog_recovery::process_atomic_ddl ( Query_log_event const &  ev)
private

Invoked when a DDL is found in a Query_log_event.

Actions taken to process the statement:

  • If m_in_transaction flag is set to true, m_is_malformed is set to true, indicating that the binary log is malformed.
  • The XID of the transaction is extracted and added to the list of internally coordinated transactions m_internal_xids.
  • If the XID already exists in the list, m_is_malformed is set to true, indicating that the binary log is malformed.
Parameters
evThe Query_log_event to process

◆ process_commit()

void binlog::Binlog_recovery::process_commit ( )
private

Invoked when a COMMIT is found in a Query_log_event.

Actions taken to process the statement:

  • If m_in_transaction flag is set to false, m_is_malformed is set to true, indicating that the binary log is malformed.
  • The m_in_transaction flag is set to false, indicating that the event starts a transaction.

◆ process_query_event()

void binlog::Binlog_recovery::process_query_event ( Query_log_event const &  ev)
private

Invoked when a Query_log_event is read from the binary log file reader.

The underlying query string is inspected to determine if the SQL command starts or ends a transaction. The following commands are searched for:

  • BEGIN
  • COMMIT
  • ROLLBACK
  • DDL
  • XA START
  • XA COMMIT
  • XA ROLLBACK

Check below for the description of the action that is taken for each.

Parameters
evThe Query_log_event to process

◆ process_rollback()

void binlog::Binlog_recovery::process_rollback ( )
private

Invoked when a ROLLBACK is found in a Query_log_event.

Actions taken to process the statement:

  • If m_in_transaction flag is set to false, m_is_malformed is set to true, indicating that the binary log is malformed.
  • The m_in_transaction flag is set to false, indicating that the event starts a transaction.

◆ process_start()

void binlog::Binlog_recovery::process_start ( )
private

Invoked when a BEGIN or an ‘XA START’ is found in a Query_log_event.

Actions taken to process the statement:

  • If m_in_transaction flag is set to true, m_is_malformed is set to true, indicating that the binary log is malformed.
  • The m_in_transaction flag is set to true, indicating that the event starts a transaction.

◆ process_xa_commit()

void binlog::Binlog_recovery::process_xa_commit ( std::string const &  query)
private

Invoked when an XA COMMIT is found in a Query_log_event.

Actions taken to process the statement:

  • If m_in_transaction flag is set to true, m_is_malformed is set to true, indicating that the binary log is malformed.
  • The m_in_transaction flag is set to false, indicating that the event ends a transaction.
  • The XID of the transaction is extracted and added to the list of externally coordinated transactions m_external_xids, alongside the state COMMITTED.
  • If the XID already exists in the list associated with a state other than PREPARED, m_is_malformed is set to true, indicating that the binary log is malformed.
Parameters
queryThe query string to process

◆ process_xa_prepare_event()

void binlog::Binlog_recovery::process_xa_prepare_event ( XA_prepare_log_event const &  ev)
private

Invoked when a XA_prepare_log_event is read from the binary log file reader.

Actions taken to process the event:

  • If m_in_transaction flag is set to false, m_is_malformed is set to true, indicating that the binary log is malformed.
  • The m_in_transaction flag is set to false, indicating that the event ends a transaction.
  • The XID of the transaction is extracted and added to the list of externally coordinated transactions m_external_xids, along side the state COMMITTED if the event represents an XA COMMIT ONE_PHASE or PREPARED if not.
  • If the XID already exists in the list associated with a state other than COMMITTED or ROLLEDBACK, m_is_malformed is set to true, indicating that the binary log is malformed.
Parameters
evThe XA_prepare_log_event to process

◆ process_xa_rollback()

void binlog::Binlog_recovery::process_xa_rollback ( std::string const &  query)
private

Invoked when an XA ROLLBACK is found in a Query_log_event.

Actions taken to process the statement:

  • If m_in_transaction flag is set to true, m_is_malformed is set to true, indicating that the binary log is malformed.
  • The m_in_transaction flag is set to false, indicating that the event ends a transaction.
  • The XID of the transaction is extracted and added to the list of externally coordinated transactions m_external_xids, along side the state ROLLEDBACK.
  • If the XID already exists in the list associated with a state other than PREPARED, m_is_malformed is set to true, indicating that the binary log is malformed.
Parameters
queryThe query string to process

◆ process_xid_event()

void binlog::Binlog_recovery::process_xid_event ( Xid_log_event const &  ev)
private

Invoked when a Xid_log_event is read from the binary log file reader.

Actions taken to process the event:

  • If m_in_transaction flag is set to false, m_is_malformed is set to true, indicating that the binary log is malformed.
  • The m_in_transaction flag is set to false, indicating that the event ends a transaction.
  • The XID of the transaction is extracted and added to the list of internally coordinated transactions m_internal_xids.
  • If the XID already exists in the list, m_is_malformed is set to true, indicating that the binary log is malformed.
Parameters
evThe Xid_log_event to process

◆ recover()

binlog::Binlog_recovery & binlog::Binlog_recovery::recover ( )

Uses the provided binary log file reader to inspect the binary log and extract transaction information.

The following is collected from the provided binlog file reader:

  • the list of internally coordinated transactions (normal) that are completely written to the binary log.
  • the list of externally coordinated transactions (XA) that appear in the binary log, along the state those transactions are in.

The list of XIDs of all internally coordinated transactions that are completely written to the binary log is passed to the storage engines through the ha_recover function in the handler interface. This tells the storage engines to commit all prepared transactions that are in the set, and to roll back all prepared transactions that are not in the set.

The list of XIDs of all externally coordinated transactions that appear in the binary log, along with the state they are in, is passed to the storage engines through the ha_recover function in the handler interface. The storage engine will determine if the transaction is to be kept at PREPARE, is to be COMMITTED or ROLLED BACK, in accordance with: the state that is provided in the list; the internal storage engine state for the transaction.

After recover() returns, has_failures() should be invoked to determine if the recover process ended successfully. Additionally, is_binlog_malformed() and has_engine_recovery_failed() can be invoked to determine the type of error that occurred.

Returns
This instance's reference, for chaining purposes.

Member Data Documentation

◆ m_external_xids

Xa_state_list::list binlog::Binlog_recovery::m_external_xids
private

List of XA transactions and states that appear in the binary log.

◆ m_failure_message

std::string binlog::Binlog_recovery::m_failure_message {""}
private

Textual representation of the encountered failure.

◆ m_in_transaction

bool binlog::Binlog_recovery::m_in_transaction {false}
private

Whether or not the event being processed is within a transaction.

◆ m_internal_xids

Xid_commit_list binlog::Binlog_recovery::m_internal_xids
private

List of normal transactions fully written to the binary log.

◆ m_is_malformed

bool binlog::Binlog_recovery::m_is_malformed {false}
private

Whether or not the binary log is malformed/corrupted.

◆ m_map_alloc

Mem_root_allocator<std::pair<const XID, XID_STATE::xa_states> > binlog::Binlog_recovery::m_map_alloc
private

Memory pool allocator to use with the XA transaction list.

◆ m_mem_root

MEM_ROOT binlog::Binlog_recovery::m_mem_root
private

Memory pool to use for the XID lists.

◆ m_no_engine_recovery

bool binlog::Binlog_recovery::m_no_engine_recovery {false}
private

Whether or not the recovery in the storage engines failed.

◆ m_reader

Binlog_file_reader& binlog::Binlog_recovery::m_reader
private

File reader for the last available binary log file.

◆ m_set_alloc

Mem_root_allocator<my_xid> binlog::Binlog_recovery::m_set_alloc
private

Memory pool allocator to use with the normal transaction list.

◆ m_valid_pos

my_off_t binlog::Binlog_recovery::m_valid_pos {0}
private

Position of the last binlog event that ended a transaction.


The documentation for this class was generated from the following files: