MySQL  8.0.16
Source Code Documentation
Binary Log

Classes

class  MYSQL_BIN_LOG::Binlog_ofile
 Logical binlog file which wraps and hides the detail of lower layer storage implementation. More...
 
class  Thd_backup_and_restore
 Helper class to switch to a new thread and then go back to the previous one, when the object is destroyed using RAII. More...
 
class  binlog_cache_data
 Caches for non-transactional and transactional data before writing it to the binary log. More...
 
class  binlog_stmt_cache_data
 
class  binlog_trx_cache_data
 
class  binlog_cache_mngr
 
class  Binlog_event_writer
 Auxiliary class to copy serialized events to the binary log and correct some of the fields that are not known until just before writing the event. More...
 
class  Adjust_offset
 Adjust log offset in the binary log file for all running slaves This class implements call back function for do_for_all_thd(). More...
 
class  Log_in_use
 This class implements Call back function for do_for_all_thd(). More...
 

Macros

#define MY_OFF_T_UNDEF   (~(my_off_t)0UL)
 
#define LIMIT_UNSAFE_WARNING_ACTIVATION_TIMEOUT   50
 
#define LIMIT_UNSAFE_WARNING_ACTIVATION_THRESHOLD_COUNT   50
 

Enumerations

enum  enum_read_gtids_from_binlog_status {
  GOT_GTIDS, GOT_PREVIOUS_GTIDS, NO_GTIDS, ERROR,
  TRUNCATED
}
 Reads GTIDs from the given binlog file. More...
 

Functions

static int binlog_init (void *p)
 
static int binlog_start_trans_and_stmt (THD *thd, Log_event *start_event)
 Function to start a statement and optionally a transaction for the binary log. More...
 
static int binlog_close_connection (handlerton *hton, THD *thd)
 
static int binlog_savepoint_set (handlerton *, THD *thd, void *sv)
 
static int binlog_savepoint_rollback (handlerton *hton, THD *thd, void *sv)
 
static bool binlog_savepoint_rollback_can_release_mdl (handlerton *, THD *thd)
 Check whether binlog state allows to safely release MDL locks after rollback to savepoint. More...
 
static int binlog_commit (handlerton *, THD *, bool)
 This function is called once after each statement. More...
 
static int binlog_rollback (handlerton *, THD *thd, bool all)
 This function is called when a transaction or a statement is rolled back. More...
 
static int binlog_prepare (handlerton *hton, THD *thd, bool all)
 
static xa_status_code binlog_xa_commit (handlerton *hton, XID *xid)
 
static xa_status_code binlog_xa_rollback (handlerton *hton, XID *xid)
 
static void exec_binlog_error_action_abort (const char *err_string)
 When a fatal error occurs due to which binary logging becomes impossible and the user specified binlog_error_action= ABORT_SERVER the following function is invoked. More...
 
static int binlog_recover (Binlog_file_reader *binlog_file_reader, my_off_t *valid_pos)
 MYSQLD server recovers from last crashed binlog. More...
 
static void binlog_prepare_row_images (const THD *thd, TABLE *table)
 
static bool has_commit_order_manager (THD *thd)
 
bool normalize_binlog_name (char *to, const char *from, bool is_relay_log)
 Turns a relative log binary log path into a full path, based on the opt_bin_logname or opt_relay_logname. More...
 
static binlog_cache_mngrthd_get_cache_mngr (const THD *thd)
 
void check_binlog_cache_size (THD *thd)
 Checks if the BINLOG_CACHE_SIZE's value is greater than MAX_BINLOG_CACHE_SIZE. More...
 
void check_binlog_stmt_cache_size (THD *thd)
 Checks if the BINLOG_STMT_CACHE_SIZE's value is greater than MAX_BINLOG_STMT_CACHE_SIZE. More...
 
bool binlog_enabled ()
 
Check whether binlog_hton has valid slot and enabled More...
 
static void binlog_trans_log_savepos (THD *thd, my_off_t *pos)
 
static int binlog_dummy_recover (handlerton *, XA_recover_txn *, uint, MEM_ROOT *)
 
static int binlog_deinit (void *)
 
enum xa_option_words get_xa_opt (THD *thd)
 
bool is_loggable_xa_prepare (THD *thd)
 Predicate function yields true when XA transaction is being logged having a proper state ready for prepare or commit in one phase. More...
 
int do_binlog_xa_commit_rollback (THD *thd, XID *xid, bool commit)
 Logging XA commit/rollback of a prepared transaction. More...
 
xa_status_code binlog_xa_commit_or_rollback (THD *thd, XID *xid, bool commit)
 Logging XA commit/rollback of a prepared transaction in the case it was disconnected and resumed (recovered), or executed by a slave applier. More...
 
static uint purge_log_get_error_code (int res)
 purge logs, master and slave sides both, related error code convertor. More...
 
static void adjust_linfo_offsets (my_off_t purge_offset)
 
static int log_in_use (const char *log_name)
 
static bool purge_error_message (THD *thd, int res)
 
bool is_transaction_empty (THD *thd)
 Check if the the transaction is empty. More...
 
int check_trx_rw_engines (THD *thd, Transaction_ctx::enum_trx_scope trx_scope)
 Check if the transaction has no rw flag set for any of the storage engines. More...
 
bool is_empty_transaction_in_binlog_cache (const THD *thd)
 Check if at least one of transacaction and statement binlog caches contains an empty transaction, other one is empty or contains an empty transaction, which has two binlog events "BEGIN" and "COMMIT". More...
 
bool trans_has_updated_trans_table (const THD *thd)
 This function checks if a transactional table was updated by the current transaction. More...
 
bool stmt_has_updated_trans_table (Ha_trx_info *ha_list)
 This function checks if a transactional table was updated by the current statement. More...
 
bool ending_trans (THD *thd, const bool all)
 This function checks if a transaction, either a multi-statement or a single statement transaction is about to commit or not. More...
 
bool ending_single_stmt_trans (THD *thd, const bool all)
 This function checks if a single statement transaction is about to commit or not. More...
 
bool trans_cannot_safely_rollback (const THD *thd)
 This function checks if a transaction cannot be rolled back safely. More...
 
bool stmt_cannot_safely_rollback (const THD *thd)
 This function checks if current statement cannot be rollded back safely. More...
 
bool purge_master_logs (THD *thd, const char *to_log)
 Execute a PURGE BINARY LOGS TO <log> command. More...
 
bool purge_master_logs_before_date (THD *thd, time_t purge_time)
 Execute a PURGE BINARY LOGS BEFORE <date> command. More...
 
int query_error_code (const THD *thd, bool not_killed)
 
static bool copy_file (IO_CACHE *from, IO_CACHE *to, my_off_t offset)
 Copy content of 'from' file from offset to 'to' file. More...
 
int log_loaded_block (IO_CACHE *file)
 Load data's io cache specific hook to be executed before a chunk of data is being read into the cache's buffer The fuction instantianates and writes into the binlog replication events along LOAD DATA processing. More...
 
template<class BINLOG_FILE_READER >
bool show_binlog_events (THD *thd, MYSQL_BIN_LOG *binary_log)
 
bool mysql_show_binlog_events (THD *thd)
 Execute a SHOW BINLOG EVENTS statement. More...
 
static bool is_number (const char *str, ulong *res, bool allow_wildcards)
 Check if a string is a valid number. More...
 
static int find_uniq_filename (char *name, uint32 new_index_number)
 Find a unique filename for 'filename. More...
 
static bool read_gtids_and_update_trx_parser_from_relaylog (const char *filename, Gtid_set *retrieved_gtids, bool verify_checksum, Transaction_boundary_parser *trx_parser, Gtid_monitoring_info *partial_trx)
 Add the GTIDs from the given relaylog file and also update the IO thread transaction parser. More...
 
static enum_read_gtids_from_binlog_status read_gtids_from_binlog (const char *filename, Gtid_set *all_gtids, Gtid_set *prev_gtids, Gtid *first_gtid, Sid_map *sid_map, bool verify_checksum, bool is_relay_log)
 
static int compare_log_name (const char *log_1, const char *log_2)
 
static int call_after_sync_hook (THD *queue_head)
 Auxiliary function used in ordered_commit. More...
 
void register_binlog_handler (THD *thd, bool trx)
 
static bool has_write_table_with_auto_increment (TABLE_LIST *tables)
 
static bool has_write_table_with_auto_increment_and_select (TABLE_LIST *tables)
 
static bool has_write_table_auto_increment_not_first_in_pk (TABLE_LIST *tables)
 
static bool fulltext_unsafe_set (TABLE_SHARE *s)
 
const char * get_locked_tables_mode_name (enum_locked_tables_mode locked_tables_mode)
 Getter for the enum enum_locked_tables_mode. More...
 
static bool handle_gtid_consistency_violation (THD *thd, int error_code, int log_error_code)
 Given that a possible violation of gtid consistency has happened, checks if gtid-inconsistencies are forbidden by the current value of ENFORCE_GTID_CONSISTENCY and GTID_MODE. More...
 
static const char * show_query_type (THD::enum_binlog_query_type qtype)
 
static void reset_binlog_unsafe_suppression ()
 Auxiliary function to reset the limit unsafety warning suppression. More...
 
static void print_unsafe_warning_to_log (int unsafe_type, char *buf, const char *query)
 Auxiliary function to print warning in the error log. More...
 
static void do_unsafe_limit_checkout (char *buf, int unsafe_type, const char *query)
 Auxiliary function to check if the warning for limit unsafety should be thrown or suppressed. More...
 
int binlog_stmt_cache_data::finalize (THD *thd)
 
int binlog_cache_data::write_event (Log_event *event)
 
bool MYSQL_BIN_LOG::assign_automatic_gtids_to_flush_group (THD *first_seen)
 Assign automatic generated GTIDs for all commit group threads in the flush stage having gtid_next.type == AUTOMATIC_GTID. More...
 
bool MYSQL_BIN_LOG::write_gtid (THD *thd, binlog_cache_data *cache_data, class Binlog_event_writer *writer)
 Write the Gtid_log_event to the binary log (prior to writing the statement or transaction cache). More...
 
int MYSQL_BIN_LOG::gtid_end_transaction (THD *thd)
 If the thread owns a GTID, this function generates an empty transaction and releases ownership of the GTID. More...
 
bool MYSQL_BIN_LOG::reencrypt_logs ()
 Re-encrypt previous existent binary/relay logs as below. More...
 
int binlog_cache_data::finalize (THD *thd, Log_event *end_event)
 This function finalizes the cache preparing for commit or rollback. More...
 
int binlog_cache_data::finalize (THD *thd, Log_event *end_event, XID_STATE *xs)
 The method writes XA END query to XA-prepared transaction's cache and calls the "basic" finalize(). More...
 
int binlog_cache_data::flush (THD *thd, my_off_t *bytes, bool *wrote_xid)
 Flush caches to the binary log. More...
 
int binlog_trx_cache_data::truncate (THD *thd, bool all)
 This function truncates the transactional cache upon committing or rolling back either a transaction or a statement. More...
 
bool Stage_manager::Mutex_queue::append (THD *first)
 Append a linked list of threads to the queue. More...
 
std::pair< bool, THD * > Stage_manager::Mutex_queue::pop_front ()
 
bool Stage_manager::enroll_for (StageID stage, THD *first, mysql_mutex_t *stage_mutex)
 Enroll a set of sessions for a stage. More...
 
THDStage_manager::Mutex_queue::fetch_and_empty ()
 Fetch the entire queue for a stage. More...
 
void Stage_manager::wait_count_or_timeout (ulong count, long usec, StageID stage)
 Introduces a wait operation on the executing thread. More...
 
void Stage_manager::signal_done (THD *queue)
 
void Stage_manager::clear_preempt_status (THD *head)
 The method ensures the follower's execution path can be preempted by the leader's thread. More...
 
int MYSQL_BIN_LOG::rollback (THD *thd, bool all)
 Write a rollback record of the transaction to the binary log. More...
 
 MYSQL_BIN_LOG::MYSQL_BIN_LOG (uint *sync_period)
 
 MYSQL_BIN_LOG::~MYSQL_BIN_LOG ()
 
void MYSQL_BIN_LOG::cleanup ()
 
void MYSQL_BIN_LOG::init_pthread_objects ()
 
int MYSQL_BIN_LOG::generate_new_name (char *new_name, const char *log_name, uint32 new_index_number=0)
 
const char * MYSQL_BIN_LOG::generate_name (const char *log_name, const char *suffix, char *buff)
 
bool MYSQL_BIN_LOG::init_and_set_log_file_name (const char *log_name, const char *new_name, uint32 new_index_number)
 
bool MYSQL_BIN_LOG::open (PSI_file_key log_file_key, const char *log_name, const char *new_name, uint32 new_index_number)
 Open the logfile and init IO_CACHE. More...
 
bool MYSQL_BIN_LOG::open_index_file (const char *index_file_name_arg, const char *log_name, bool need_lock_index)
 
bool MYSQL_BIN_LOG::find_first_log_not_in_gtid_set (char *binlog_file_name, const Gtid_set *gtid_set, Gtid *first_gtid, const char **errmsg)
 Find the oldest binary log that contains any GTID that is not in the given gtid set. More...
 
bool MYSQL_BIN_LOG::init_gtid_sets (Gtid_set *gtid_set, Gtid_set *lost_groups, bool verify_checksum, bool need_lock, Transaction_boundary_parser *trx_parser, Gtid_monitoring_info *partial_trx, bool is_server_starting=false)
 Reads the set of all GTIDs in the binary/relay log, and the set of all lost GTIDs in the binary log, and stores each set in respective argument. More...
 
bool MYSQL_BIN_LOG::open_binlog (const char *log_name, const char *new_name, ulong max_size_arg, bool null_created_arg, bool need_lock_index, bool need_sid_lock, Format_description_log_event *extra_description_event, uint32 new_index_number=0)
 Create a new binary log. More...
 
int MYSQL_BIN_LOG::move_crash_safe_index_file_to_index_file (bool need_lock_index)
 Move crash safe index file to index file. More...
 
int MYSQL_BIN_LOG::add_log_to_index (uchar *log_file_name, size_t name_len, bool need_lock_index)
 Append log file name to index file. More...
 
int MYSQL_BIN_LOG::get_current_log (LOG_INFO *linfo, bool need_lock_log=true)
 
int MYSQL_BIN_LOG::raw_get_current_log (LOG_INFO *linfo)
 
bool MYSQL_BIN_LOG::check_write_error (const THD *thd)
 
void MYSQL_BIN_LOG::report_cache_write_error (THD *thd, bool is_transactional)
 
int MYSQL_BIN_LOG::find_log_pos (LOG_INFO *linfo, const char *log_name, bool need_lock_index)
 Find the position in the log-index-file for the given log name. More...
 
int MYSQL_BIN_LOG::find_next_log (LOG_INFO *linfo, bool need_lock_index)
 Find the position in the log-index-file for the given log name. More...
 
int MYSQL_BIN_LOG::find_next_relay_log (char log_name[FN_REFLEN+1])
 Find the relay log name following the given name from relay log index file. More...
 
std::pair< int, std::list< std::string > > MYSQL_BIN_LOG::get_log_index (bool need_lock_index=true)
 Retrieves the contents of the index file associated with this log object into an std::list<std::string> object. More...
 
bool MYSQL_BIN_LOG::reset_logs (THD *thd, bool delete_only=false)
 Removes files, as part of a RESET MASTER or RESET SLAVE statement, by deleting all logs referred to in the index file and the index file. More...
 
int MYSQL_BIN_LOG::set_crash_safe_index_file_name (const char *base_file_name)
 Set the name of crash safe index file. More...
 
int MYSQL_BIN_LOG::open_crash_safe_index_file ()
 Open a (new) crash safe index file. More...
 
int MYSQL_BIN_LOG::close_crash_safe_index_file ()
 Close the crash safe index file. More...
 
int MYSQL_BIN_LOG::remove_logs_from_index (LOG_INFO *linfo, bool need_update_threads)
 Remove logs from index file. More...
 
int MYSQL_BIN_LOG::purge_logs (const char *to_log, bool included, bool need_lock_index, bool need_update_threads, ulonglong *decrease_log_space, bool auto_purge)
 Remove all logs before the given log from disk and from the index file. More...
 
int MYSQL_BIN_LOG::set_purge_index_file_name (const char *base_file_name)
 
int MYSQL_BIN_LOG::open_purge_index_file (bool destroy)
 
int MYSQL_BIN_LOG::close_purge_index_file ()
 
bool MYSQL_BIN_LOG::is_inited_purge_index_file ()
 
int MYSQL_BIN_LOG::sync_purge_index_file ()
 
int MYSQL_BIN_LOG::register_purge_index_entry (const char *entry)
 
int MYSQL_BIN_LOG::register_create_index_entry (const char *entry)
 
int MYSQL_BIN_LOG::purge_index_entry (THD *thd, ulonglong *decrease_log_space, bool need_lock_index)
 
int MYSQL_BIN_LOG::purge_logs_before_date (time_t purge_time, bool auto_purge)
 Remove all logs before the given file date from disk and from the index file. More...
 
void MYSQL_BIN_LOG::make_log_name (char *buf, const char *log_ident)
 Create a new log file name. More...
 
bool MYSQL_BIN_LOG::is_active (const char *log_file_name)
 Check if we are writing/reading to the given log file. More...
 
void MYSQL_BIN_LOG::inc_prep_xids (THD *thd)
 Increment the prepared XID counter. More...
 
void MYSQL_BIN_LOG::dec_prep_xids (THD *thd)
 Decrement the prepared XID counter. More...
 
int MYSQL_BIN_LOG::new_file (Format_description_log_event *extra_description_event)
 
int MYSQL_BIN_LOG::new_file_without_locking (Format_description_log_event *extra_description_event)
 
int MYSQL_BIN_LOG::new_file_impl (bool need_lock, Format_description_log_event *extra_description_event)
 Start writing to a new log file or reopen the old file. More...
 
bool MYSQL_BIN_LOG::after_write_to_relay_log (Master_info *mi)
 Called after an event has been written to the relay log by the IO thread. More...
 
bool MYSQL_BIN_LOG::write_event (Log_event *ev, Master_info *mi)
 
bool MYSQL_BIN_LOG::write_buffer (const char *buf, uint len, Master_info *mi)
 
bool MYSQL_BIN_LOG::flush ()
 
bool MYSQL_BIN_LOG::flush_and_sync (const bool force=false)
 Flush binlog cache and synchronize to disk. More...
 
void MYSQL_BIN_LOG::start_union_events (THD *thd, query_id_t query_id_param)
 
void MYSQL_BIN_LOG::stop_union_events (THD *thd)
 
bool MYSQL_BIN_LOG::is_query_in_union (THD *thd, query_id_t query_id_param)
 
void MYSQL_BIN_LOG::update_thd_next_event_pos (THD *thd)
 
int MYSQL_BIN_LOG::flush_and_set_pending_rows_event (THD *thd, Rows_log_event *event, bool is_transactional)
 
bool MYSQL_BIN_LOG::write_event (Log_event *event_info)
 Write an event to the binary log cache. More...
 
int MYSQL_BIN_LOG::rotate (bool force_rotate, bool *check_purge)
 The method executes rotation when LOCK_log is already acquired by the caller. More...
 
void MYSQL_BIN_LOG::purge ()
 The method executes logs purging routine. More...
 
int MYSQL_BIN_LOG::rotate_and_purge (THD *thd, bool force_rotate)
 Execute a FLUSH LOGS statement. More...
 
uint MYSQL_BIN_LOG::next_file_id ()
 
int MYSQL_BIN_LOG::get_gtid_executed (Sid_map *sid_map, Gtid_set *gtid_set)
 Deep copy global_sid_map and gtid_executed. More...
 
bool MYSQL_BIN_LOG::do_write_cache (Binlog_cache_storage *cache, class Binlog_event_writer *writer)
 Write the contents of the given IO_CACHE to the binary log. More...
 
bool MYSQL_BIN_LOG::write_incident (Incident_log_event *ev, THD *thd, bool need_lock_log, const char *err_msg, bool do_flush_and_sync=true)
 Writes an incident event to stmt_cache. More...
 
bool MYSQL_BIN_LOG::write_dml_directly (THD *thd, const char *stmt, size_t stmt_len)
 Write a dml into statement cache and then flush it into binlog. More...
 
bool MYSQL_BIN_LOG::write_incident (THD *thd, bool need_lock_log, const char *err_msg, bool do_flush_and_sync=true)
 Creates an incident event and writes it to the binary log. More...
 
bool MYSQL_BIN_LOG::write_event_to_binlog (Log_event *ev)
 
bool MYSQL_BIN_LOG::write_event_to_binlog_and_sync (Log_event *ev)
 
bool MYSQL_BIN_LOG::write_cache (THD *thd, class binlog_cache_data *cache_data, class Binlog_event_writer *writer)
 Write the contents of the statement or transaction cache to the binary log. More...
 
void MYSQL_BIN_LOG::report_binlog_write_error ()
 
int MYSQL_BIN_LOG::wait_for_update (const struct timespec *timeout)
 Wait until we get a signal that the binary log has been updated. More...
 
void MYSQL_BIN_LOG::close (uint exiting, bool need_lock_log, bool need_lock_index)
 Close the log file. More...
 
void MYSQL_BIN_LOG::harvest_bytes_written (Relay_log_info *rli, bool need_log_space_lock)
 
void MYSQL_BIN_LOG::set_max_size (ulong max_size_arg)
 
int MYSQL_BIN_LOG::open_binlog (const char *opt_name)
 
bool MYSQL_BIN_LOG::truncate_relaylog_file (Master_info *mi, my_off_t valid_pos)
 
Truncate the active relay log file in the specified position. More...
 
void MYSQL_BIN_LOG::close ()
 This is called on shutdown, after ha_panic. More...
 
int MYSQL_BIN_LOG::prepare (THD *thd, bool all)
 Log a prepare record of the transaction to the storage engines. More...
 
enum_result MYSQL_BIN_LOG::commit (THD *thd, bool all)
 Commit the transaction in the transaction coordinator. More...
 
std::pair< int, my_off_tMYSQL_BIN_LOG::flush_thread_caches (THD *thd)
 Flush caches for session. More...
 
int MYSQL_BIN_LOG::process_flush_stage_queue (my_off_t *total_bytes_var, bool *rotate_var, THD **out_queue_var)
 Execute the flush stage. More...
 
void MYSQL_BIN_LOG::process_commit_stage_queue (THD *thd, THD *queue)
 Commit a sequence of sessions. More...
 
void MYSQL_BIN_LOG::process_after_commit_stage_queue (THD *thd, THD *first)
 Process after commit for a sequence of sessions. More...
 
bool MYSQL_BIN_LOG::change_stage (THD *thd, Stage_manager::StageID stage, THD *queue, mysql_mutex_t *leave, mysql_mutex_t *enter)
 Enter a stage of the ordered commit procedure. More...
 
int MYSQL_BIN_LOG::flush_cache_to_file (my_off_t *flush_end_pos)
 Flush the I/O cache to file. More...
 
std::pair< bool, boolMYSQL_BIN_LOG::sync_binlog_file (bool force)
 Call fsync() to sync the file to disk. More...
 
int MYSQL_BIN_LOG::finish_commit (THD *thd)
 Helper function executed when leaving ordered_commit. More...
 
void MYSQL_BIN_LOG::handle_binlog_flush_or_sync_error (THD *thd, bool need_lock_log)
 Helper function to handle flush or sync stage errors. More...
 
int MYSQL_BIN_LOG::ordered_commit (THD *thd, bool all, bool skip_commit=false)
 Flush and commit the transaction. More...
 
void MYSQL_BIN_LOG::update_binlog_end_pos (bool need_lock=true)
 
void MYSQL_BIN_LOG::update_binlog_end_pos (const char *file, my_off_t pos)
 
bool THD::is_binlog_cache_empty (bool is_transactional) const
 Return true if the statement/transaction cache is currently empty, false otherwise. More...
 
int THD::binlog_setup_trx_data ()
 
int THD::binlog_write_table_map (TABLE *table, bool is_transactional, bool binlog_rows_query)
 This function writes a table map to the binary log. More...
 
Rows_log_eventTHD::binlog_get_pending_rows_event (bool is_transactional) const
 This function retrieves a pending row event from a cache which is specified through the parameter is_transactional. More...
 
void THD::add_to_binlog_accessed_dbs (const char *db)
 
int THD::decide_logging_format (TABLE_LIST *tables)
 Decide on logging format to use for the statement and issue errors or warnings as needed. More...
 
bool THD::is_ddl_gtid_compatible ()
 
bool THD::is_dml_gtid_compatible (bool some_transactional_table, bool some_non_transactional_table, bool non_transactional_tables_are_tmp)
 is_dml_gtid_compatible() and is_ddl_gtid_compatible() check if the statement that is about to be processed will safely get a GTID. More...
 
template<class RowsEventT >
Rows_log_eventTHD::binlog_prepare_pending_rows_event (TABLE *table, uint32 serv_id, size_t needed, bool is_transactional, const unsigned char *extra_row_info, uint32 source_part_id=INT_MAX)
 
int THD::binlog_write_row (TABLE *table, bool is_transactional, const uchar *new_data, const unsigned char *extra_row_info)
 
int THD::binlog_update_row (TABLE *table, bool is_transactional, const uchar *old_data, const uchar *new_data, const uchar *extra_row_info)
 
int THD::binlog_delete_row (TABLE *table, bool is_transactional, const uchar *old_data, const unsigned char *extra_row_info)
 
int THD::binlog_flush_pending_rows_event (bool stmt_end, bool is_transactional)
 
void THD::issue_unsafe_warnings ()
 Auxiliary method used by binlog_query() to raise warnings. More...
 
int THD::binlog_query (enum_binlog_query_type qtype, const char *query, size_t query_len, bool is_trans, bool direct, bool suppress_use, int errcode)
 Log the current query. More...
 

Variables

static ulonglong limit_unsafe_suppression_start_time = 0
 
static bool unsafe_warning_suppression_is_activated = false
 
static int limit_unsafe_warning_count = 0
 
static handlertonbinlog_hton
 
bool opt_binlog_order_commits = true
 
const char * log_bin_index = 0
 
const char * log_bin_basename = 0
 
ulong rpl_read_size
 
MYSQL_BIN_LOG mysql_bin_logsync_binlog_period
 
static const char * g_stage_name []
 Names for the stages. More...
 
struct st_mysql_storage_engine binlog_storage_engine
 

Detailed Description

Macro Definition Documentation

◆ LIMIT_UNSAFE_WARNING_ACTIVATION_THRESHOLD_COUNT

#define LIMIT_UNSAFE_WARNING_ACTIVATION_THRESHOLD_COUNT   50

◆ LIMIT_UNSAFE_WARNING_ACTIVATION_TIMEOUT

#define LIMIT_UNSAFE_WARNING_ACTIVATION_TIMEOUT   50

◆ MY_OFF_T_UNDEF

#define MY_OFF_T_UNDEF   (~(my_off_t)0UL)

Enumeration Type Documentation

◆ enum_read_gtids_from_binlog_status

Reads GTIDs from the given binlog file.

Parameters
filenameFile to read from.
all_gtidsIf not NULL, then the GTIDs from the Previous_gtids_log_event and from all Gtid_log_events are stored in this object.
prev_gtidsIf not NULL, then the GTIDs from the Previous_gtids_log_events are stored in this object.
first_gtidIf not NULL, then the first GTID information from the file will be stored in this object.
sid_mapThe sid_map object to use in the rpl_sidno generation of the Gtid_log_event. If lock is needed in the sid_map, the caller must hold it.
verify_checksumSet to true to verify event checksums.
Return values
GOT_GTIDSThe file was successfully read and it contains both Gtid_log_events and Previous_gtids_log_events. This is only possible if either all_gtids or first_gtid are not null.
GOT_PREVIOUS_GTIDSThe file was successfully read and it contains Previous_gtids_log_events but no Gtid_log_events. For binary logs, if no all_gtids and no first_gtid are specified, this function will be done right after reading the PREVIOUS_GTIDS regardless of the rest of the content of the binary log file.
NO_GTIDSThe file was successfully read and it does not contain GTID events.
ERROROut of memory, or IO error, or malformed event structure, or the file is malformed (e.g., contains Gtid_log_events but no Previous_gtids_log_event).
TRUNCATEDThe file was truncated before the end of the first Previous_gtids_log_event.
Enumerator
GOT_GTIDS 
GOT_PREVIOUS_GTIDS 
NO_GTIDS 
ERROR 
TRUNCATED 

Function Documentation

◆ MYSQL_BIN_LOG()

MYSQL_BIN_LOG::MYSQL_BIN_LOG ( uint sync_period)

◆ ~MYSQL_BIN_LOG()

MYSQL_BIN_LOG::~MYSQL_BIN_LOG ( )

◆ add_log_to_index()

int MYSQL_BIN_LOG::add_log_to_index ( uchar log_name,
size_t  log_name_len,
bool  need_lock_index 
)

Append log file name to index file.

  • To make crash safe, we copy all the content of index file to crash safe index file firstly and then append the log file name to the crash safe index file. Finally move the crash safe index file to index file.
Return values
0ok
-1error

◆ add_to_binlog_accessed_dbs()

void THD::add_to_binlog_accessed_dbs ( const char *  db_param)
Parameters
db_paramdb name c-string to be inserted into alphabetically sorted THD::binlog_accessed_db_names list.

Note, that space for both the data and the node struct are allocated in THD::main_mem_root. The list lasts for the top-level query time and is reset in THD::cleanup_after_query().

◆ adjust_linfo_offsets()

static void adjust_linfo_offsets ( my_off_t  purge_offset)
static

◆ after_write_to_relay_log()

bool MYSQL_BIN_LOG::after_write_to_relay_log ( Master_info mi)
private

Called after an event has been written to the relay log by the IO thread.

This flushes and possibly syncs the file (according to the sync options), rotates the file if it has grown over the limit, and finally calls signal_update().

Note
The caller must hold LOCK_log before invoking this function.
Parameters
miMaster_info for the IO thread.
Return values
falsesuccess
trueerror

◆ append()

bool Stage_manager::Mutex_queue::append ( THD first)

Append a linked list of threads to the queue.

Return values
trueThe queue was empty before this operation.
falseThe queue was non-empty before this operation.

◆ assign_automatic_gtids_to_flush_group()

bool MYSQL_BIN_LOG::assign_automatic_gtids_to_flush_group ( THD first_seen)

Assign automatic generated GTIDs for all commit group threads in the flush stage having gtid_next.type == AUTOMATIC_GTID.

Parameters
first_seenThe first thread seen entering the flush stage.
Returns
Returns false if succeeds, otherwise true is returned.

◆ binlog_close_connection()

static int binlog_close_connection ( handlerton hton,
THD thd 
)
static

◆ binlog_commit()

static int binlog_commit ( handlerton ,
THD ,
bool   
)
static

This function is called once after each statement.

See also
MYSQL_BIN_LOG::commit
handlerton::commit

◆ binlog_deinit()

static int binlog_deinit ( void *  )
static

◆ binlog_delete_row()

int THD::binlog_delete_row ( TABLE table,
bool  is_transactional,
const uchar old_data,
const unsigned char *  extra_row_info 
)

Save a reference to the original read and write set bitmaps. We will need this to restore the bitmaps at the end.

This will remove spurious fields required during execution but not needed for binlogging. This is done according to the: binlog-row-image option.

◆ binlog_dummy_recover()

static int binlog_dummy_recover ( handlerton ,
XA_recover_txn ,
uint  ,
MEM_ROOT  
)
static

◆ binlog_enabled()

bool binlog_enabled ( )


Check whether binlog_hton has valid slot and enabled

◆ binlog_flush_pending_rows_event()

int THD::binlog_flush_pending_rows_event ( bool  stmt_end,
bool  is_transactional 
)

◆ binlog_get_pending_rows_event()

Rows_log_event * THD::binlog_get_pending_rows_event ( bool  is_transactional) const

This function retrieves a pending row event from a cache which is specified through the parameter is_transactional.

Respectively, when it is true, the pending event is returned from the transactional cache. Otherwise from the non-transactional cache.

Parameters
is_transactionaltrue indicates a transactional cache, otherwise false a non-transactional.
Returns
The row event if any.

◆ binlog_init()

static int binlog_init ( void *  p)
static

◆ binlog_prepare()

static int binlog_prepare ( handlerton hton,
THD thd,
bool  all 
)
static

◆ binlog_prepare_pending_rows_event()

template<class RowsEventT >
Rows_log_event * THD::binlog_prepare_pending_rows_event ( TABLE table,
uint32  serv_id,
size_t  needed,
bool  is_transactional,
const unsigned char *  extra_row_info,
uint32  source_part_id = INT_MAX 
)

◆ binlog_prepare_row_images()

void binlog_prepare_row_images ( const THD thd,
TABLE table 
)
static

Remove from read_set spurious columns. The write_set has been handled before in table->mark_columns_needed_for_update.

if there is a primary key in the table (ie, user declared PK or a non-null unique index) and we dont want to ship the entire image, and the handler involved supports this.

Just to be sure that tmp_set is currently not in use as the read_set already.

NOBLOB: Remove unnecessary BLOB fields from read_set (the ones that are not part of PK).

◆ binlog_query()

int THD::binlog_query ( THD::enum_binlog_query_type  qtype,
const char *  query_arg,
size_t  query_len,
bool  is_trans,
bool  direct,
bool  suppress_use,
int  errcode 
)

Log the current query.

The query will be logged in either row format or statement format depending on the value of current_stmt_binlog_format_row field and the value of the qtype parameter.

This function must be called:

  • After the all calls to ha_*_row() functions have been issued.
  • After any writes to system tables. Rationale: if system tables were written after a call to this function, and the master crashes after the call to this function and before writing the system tables, then the master and slave get out of sync.
  • Before tables are unlocked and closed.
See also
decide_logging_format
Return values
0Success
nonzeroIf there is a failure when writing the query (e.g., write failure), then the error code is returned.

◆ binlog_recover()

static int binlog_recover ( Binlog_file_reader binlog_file_reader,
my_off_t valid_pos 
)
static

MYSQLD server recovers from last crashed binlog.

Parameters
[in]binlog_file_readerBinlog_file_reader of the crashed binlog.
[out]valid_posThe position of the last valid transaction or event(non-transaction) of the crashed binlog. valid_pos must be non-NULL.

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

The function collects the XIDs of all transactions that are completely written to the binary log into a hash, and passes this hash 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.

To compute the hash, this function iterates over the last binary log only (i.e. it assumes that 'log' is the last binary log). It instantiates each event. For XID-events (i.e. commit to InnoDB), it extracts the xid from the event and stores it in the hash.

It is enough to iterate over only the last binary log because when the binary log is rotated we force engines to commit (and we fsync the old binary log).

Return values
0Success
1Out of memory, or storage engine returns error.

◆ binlog_rollback()

static int binlog_rollback ( handlerton hton,
THD thd,
bool  all 
)
static

This function is called when a transaction or a statement is rolled back.

◆ binlog_savepoint_rollback()

static int binlog_savepoint_rollback ( handlerton hton,
THD thd,
void *  sv 
)
static

◆ binlog_savepoint_rollback_can_release_mdl()

static bool binlog_savepoint_rollback_can_release_mdl ( handlerton ,
THD thd 
)
static

Check whether binlog state allows to safely release MDL locks after rollback to savepoint.

Parameters
thdThe client thread that executes the transaction.
Returns
true - It is safe to release MDL locks. false - If it is not.

If we have not updated any non-transactional tables rollback to savepoint will simply truncate binlog cache starting from SAVEPOINT command. So it should be safe to release MDL acquired after SAVEPOINT command in this case.

◆ binlog_savepoint_set()

static int binlog_savepoint_set ( handlerton ,
THD thd,
void *  sv 
)
static
Note
How do we handle this (unlikely but legal) case:
  [transaction] + [update to non-trans table] + [rollback to savepoint] ?
The problem occurs when a savepoint is before the update to the non-transactional table. Then when there's a rollback to the savepoint, if we simply truncate the binlog cache, we lose the part of the binlog cache where the update is. If we want to not lose it, we need to write the SAVEPOINT command and the ROLLBACK TO SAVEPOINT command to the binlog cache. The latter is easy: it's just write at the end of the binlog cache, but the former should be inserted to the place where the user called SAVEPOINT. The solution is that when the user calls SAVEPOINT, we write it to the binlog cache (so no need to later insert it). As transactions are never intermixed in the binary log (i.e. they are serialized), we won't have conflicts with savepoint names when using mysqlbinlog or in the slave SQL thread. Then when ROLLBACK TO SAVEPOINT is called, if we updated some non-transactional table, we don't truncate the binlog cache but instead write ROLLBACK TO SAVEPOINT to it; otherwise we truncate the binlog cache (which will chop the SAVEPOINT command from the binlog cache, which is good as in that case there is no need to have it in the binlog).

◆ binlog_setup_trx_data()

int THD::binlog_setup_trx_data ( )

◆ binlog_start_trans_and_stmt()

static int binlog_start_trans_and_stmt ( THD thd,
Log_event start_event 
)
static

Function to start a statement and optionally a transaction for the binary log.

This function does three things:

  • Starts a transaction if not in autocommit mode or if a BEGIN statement has been seen.
  • Start a statement transaction to allow us to truncate the cache.
  • Save the currrent binlog position so that we can roll back the statement by truncating the cache.

    We only update the saved position if the old one was undefined, the reason is that there are some cases (e.g., for CREATE-SELECT) where the position is saved twice (e.g., both in Query_result_create::prepare() and THD::binlog_write_table_map()), but we should use the first. This means that calls to this function can be used to start the statement before the first table map event, to include some extra events.

Note however that IMMEDIATE_LOGGING implies that the statement is written without BEGIN/COMMIT.

Parameters
thdThread variable
start_eventThe first event requested to be written into the binary log

◆ binlog_trans_log_savepos()

static void binlog_trans_log_savepos ( THD thd,
my_off_t pos 
)
static

◆ binlog_update_row()

int THD::binlog_update_row ( TABLE table,
bool  is_transactional,
const uchar old_data,
const uchar new_data,
const uchar extra_row_info 
)

Save a reference to the original read and write set bitmaps. We will need this to restore the bitmaps at the end.

This will remove spurious fields required during execution but not needed for binlogging. This is done according to the: binlog-row-image option.

◆ binlog_write_row()

int THD::binlog_write_row ( TABLE table,
bool  is_transactional,
const uchar new_data,
const unsigned char *  extra_row_info 
)

◆ binlog_write_table_map()

int THD::binlog_write_table_map ( TABLE table,
bool  is_transactional,
bool  binlog_rows_query 
)

This function writes a table map to the binary log.

Note that in order to keep the signature uniform with related methods, we use a redundant parameter to indicate whether a transactional table was changed or not. Sometimes it will write a Rows_query_log_event into binary log before the table map too.

Parameters
tablea pointer to the table.
is_transactionaltrue indicates a transactional table, otherwise false a non-transactional.
binlog_rows_querytrue indicates a Rows_query log event will be binlogged before table map, otherwise false indicates it will not be binlogged.
Returns
nonzero if an error pops up when writing the table map event or the Rows_query log event.

◆ binlog_xa_commit()

static xa_status_code binlog_xa_commit ( handlerton hton,
XID xid 
)
static

◆ binlog_xa_commit_or_rollback()

xa_status_code binlog_xa_commit_or_rollback ( THD thd,
XID xid,
bool  commit 
)
inline

Logging XA commit/rollback of a prepared transaction in the case it was disconnected and resumed (recovered), or executed by a slave applier.

Parameters
thdTHD handle
xida pointer to XID object
commitwhen true XA-COMMIT is logged, otherwise XA-ROLLBACK
Returns
error code, 0 success

◆ binlog_xa_rollback()

static xa_status_code binlog_xa_rollback ( handlerton hton,
XID xid 
)
static

◆ call_after_sync_hook()

static int call_after_sync_hook ( THD queue_head)
inlinestatic

Auxiliary function used in ordered_commit.

◆ change_stage()

bool MYSQL_BIN_LOG::change_stage ( THD thd,
Stage_manager::StageID  stage,
THD queue,
mysql_mutex_t leave_mutex,
mysql_mutex_t enter_mutex 
)
private

Enter a stage of the ordered commit procedure.

Entering is stage is done by:

  • Atomically enqueueing a queue of processes (which is just one for the first phase).
  • If the queue was empty, the thread is the leader for that stage and it should process the entire queue for that stage.
  • If the queue was not empty, the thread is a follower and can go waiting for the commit to finish.

The function will lock the stage mutex if it was designated the leader for the phase.

Parameters
thdSession structure
stageThe stage to enter
queueQueue of threads to enqueue for the stage
leave_mutexMutex that will be released when changing stage
enter_mutexMutex that will be taken when changing stage
Return values
trueThe thread should "bail out" and go waiting for the commit to finish
falseThe thread is the leader for the stage and should do the processing.

◆ check_binlog_cache_size()

void check_binlog_cache_size ( THD thd)

Checks if the BINLOG_CACHE_SIZE's value is greater than MAX_BINLOG_CACHE_SIZE.

If this happens, the BINLOG_CACHE_SIZE is set to MAX_BINLOG_CACHE_SIZE.

◆ check_binlog_stmt_cache_size()

void check_binlog_stmt_cache_size ( THD thd)

Checks if the BINLOG_STMT_CACHE_SIZE's value is greater than MAX_BINLOG_STMT_CACHE_SIZE.

If this happens, the BINLOG_STMT_CACHE_SIZE is set to MAX_BINLOG_STMT_CACHE_SIZE.

◆ check_trx_rw_engines()

int check_trx_rw_engines ( THD thd,
Transaction_ctx::enum_trx_scope  trx_scope 
)

Check if the transaction has no rw flag set for any of the storage engines.

Parameters
thdThe client thread that executed the current statement.
trx_scopeThe transaction scope to look into.
Return values
thenumber of engines which have actual changes.

◆ check_write_error()

bool MYSQL_BIN_LOG::check_write_error ( const THD thd)

◆ cleanup()

void MYSQL_BIN_LOG::cleanup ( )

◆ clear_preempt_status()

void Stage_manager::clear_preempt_status ( THD head)

The method ensures the follower's execution path can be preempted by the leader's thread.

Preempt status of head follower is checked to engange the leader into waiting when set.

Parameters
headTHD* of a follower thread

◆ close() [1/2]

void MYSQL_BIN_LOG::close ( void  )
virtual

This is called on shutdown, after ha_panic.

Implements TC_LOG.

◆ close() [2/2]

void MYSQL_BIN_LOG::close ( uint  exiting,
bool  need_lock_log,
bool  need_lock_index 
)

Close the log file.

Parameters
exitingBitmask for one or more of the following bits:
  • LOG_CLOSE_INDEX : if we should close the index file
  • LOG_CLOSE_TO_BE_OPENED : if we intend to call open at once after close.
  • LOG_CLOSE_STOP_EVENT : write a 'stop' event to the log
need_lock_logIf true, this function acquires LOCK_log; otherwise the caller should already have acquired it.
need_lock_indexIf true, this function acquires LOCK_index; otherwise the caller should already have acquired it.
Note
One can do an open on the object at once after doing a close. The internal structures are not freed until cleanup() is called

TODO(WL#7546): Change the implementation to Stop_event after write() is moved into libbinlogevents

◆ close_crash_safe_index_file()

int MYSQL_BIN_LOG::close_crash_safe_index_file ( )

Close the crash safe index file.

Note
The crash safe file is just closed, is not deleted. Because it is moved to index file later on.
Return values
0ok
1error

◆ close_purge_index_file()

int MYSQL_BIN_LOG::close_purge_index_file ( )

◆ commit()

TC_LOG::enum_result MYSQL_BIN_LOG::commit ( THD thd,
bool  all 
)
virtual

Commit the transaction in the transaction coordinator.

This function will commit the sessions transaction in the binary log and in the storage engines (by calling ha_commit_low). If the transaction was successfully logged (or not successfully unlogged) but the commit in the engines did not succed, there is a risk of inconsistency between the engines and the binary log.

For binary log group commit, the commit is separated into three parts:

  1. First part consists of filling the necessary caches and finalizing them (if they need to be finalized). After this, nothing is added to any of the caches.
  2. Second part execute an ordered flush and commit. This will be done using the group commit functionality in ordered_commit.
  3. Third part checks any errors resulting from the ordered commit and handles them appropriately.
Return values
RESULT_SUCCESSsuccess
RESULT_ABORTEDerror, transaction was neither logged nor committed
RESULT_INCONSISTENTerror, transaction was logged but not committed

Implements TC_LOG.

◆ compare_log_name()

static int compare_log_name ( const char *  log_1,
const char *  log_2 
)
static

◆ copy_file()

static bool copy_file ( IO_CACHE from,
IO_CACHE to,
my_off_t  offset 
)
static

Copy content of 'from' file from offset to 'to' file.

  • We do the copy outside of the IO_CACHE as the cache buffers would just make things slower and more complicated. In most cases the copy loop should only do one read.
Parameters
fromFile to copy.
toFile to copy to.
offsetOffset in 'from' file.
Return values
0ok
-1error

◆ dec_prep_xids()

void MYSQL_BIN_LOG::dec_prep_xids ( THD thd)
private

Decrement the prepared XID counter.

Signal m_prep_xids_cond if the counter reaches zero.

◆ decide_logging_format()

int THD::decide_logging_format ( TABLE_LIST tables)

Decide on logging format to use for the statement and issue errors or warnings as needed.

The decision depends on the following parameters:

  • The logging mode, i.e., the value of binlog_format. Can be statement, mixed, or row.
  • The type of statement. There are three types of statements: "normal" safe statements; unsafe statements; and row injections. An unsafe statement is one that, if logged in statement format, might produce different results when replayed on the slave (e.g., queries with a LIMIT clause). A row injection is either a BINLOG statement, or a row event executed by the slave's SQL thread.
  • The capabilities of tables modified by the statement. The capabilities vector* for a table is a set of flags associated with the table. Currently, it only includes two flags: row capability flag and statement capability flag.

    The row capability flag is set if and only if the engine can handle row-based logging. The statement capability flag is set if and only if the table can handle statement-based logging.

Decision table for logging format

The following table summarizes how the format and generated warning/error depends on the tables' capabilities, the statement type, and the current binlog_format.

Row capable N NNNNNNNNN YYYYYYYYY YYYYYYYYY Statement capable N YYYYYYYYY NNNNNNNNN YYYYYYYYY

Statement type * SSSUUUIII SSSUUUIII SSSUUUIII

binlog_format * SMRSMRSMR SMRSMRSMR SMRSMRSMR

Logged format - SS-S–— -RR-RR-RR SRRSRR-RR Warning/Error 1 –2732444 5–5–6– —7–6–

Legend

Row capable: N - Some table not row-capable, Y - All tables row-capable Stmt capable: N - Some table not stmt-capable, Y - All tables stmt-capable Statement type: (S)afe, (U)nsafe, or Row (I)njection binlog_format: (S)TATEMENT, (M)IXED, or (R)OW Logged format: (S)tatement or (R)ow Warning/Error: Warnings and error messages are as follows:

  1. Error: Cannot execute statement: binlogging impossible since both row-incapable engines and statement-incapable engines are involved.
  2. Error: Cannot execute statement: binlogging impossible since BINLOG_FORMAT = ROW and at least one table uses a storage engine limited to statement-logging.
  3. Error: Cannot execute statement: binlogging of unsafe statement is impossible when storage engine is limited to statement-logging and BINLOG_FORMAT = MIXED.
  4. Error: Cannot execute row injection: binlogging impossible since at least one table uses a storage engine limited to statement-logging.
  5. Error: Cannot execute statement: binlogging impossible since BINLOG_FORMAT = STATEMENT and at least one table uses a storage engine limited to row-logging.
  6. Error: Cannot execute row injection: binlogging impossible since BINLOG_FORMAT = STATEMENT.
  7. Warning: Unsafe statement binlogged in statement format since BINLOG_FORMAT = STATEMENT.

In addition, we can produce the following error (not depending on the variables of the decision diagram):

  1. Error: Cannot execute statement: binlogging impossible since more than one engine is involved and at least one engine is self-logging.
  2. Error: Do not allow users to modify a gtid_executed table explicitly by a XA transaction.

For each error case above, the statement is prevented from being logged, we report an error, and roll back the statement. For warnings, we set the thd->binlog_flags variable: the warning will be printed only if the statement is successfully logged.

See also
THD::binlog_query
Parameters
[in]tablesTables involved in the query
Return values
0No error; statement can be logged.
-1One of the error conditions above applies (1, 2, 4, 5, 6 or 9).

The number of tables used in the current statement, that should be replicated.

The number of tables written to in the current statement, that should not be replicated. A table should not be replicated when it is considered 'local' to a MySQL instance. Currently, these tables are:

  • mysql.slow_log
  • mysql.general_log
  • mysql.slave_relay_log_info
  • mysql.slave_master_info
  • mysql.slave_worker_info
  • performance_schema.*
  • TODO: information_schema.* In practice, from this list, only performance_schema.* tables are written to by user queries.

Indicate whether we alreadly reported a warning on modifying gtid_executed table.

◆ do_binlog_xa_commit_rollback()

int do_binlog_xa_commit_rollback ( THD thd,
XID xid,
bool  commit 
)
inline

Logging XA commit/rollback of a prepared transaction.

The function is called at XA-commit or XA-rollback logging via two paths: the recovered-or-slave-applier or immediately through the XA-prepared transaction connection itself. It fills in appropiate event in the statement cache whenever xid state is marked with is_binlogged() flag that indicates the prepared part of the transaction must've been logged.

About early returns from the function. In the recovered-or-slave-applier case the function may be called for the 2nd time, which has_logged_xid monitors. ONE_PHASE option to XA-COMMIT is handled to skip writing XA-commit event now. And the final early return check is for the read-only XA that is not to be logged.

Parameters
thdTHD handle
xida pointer to XID object that is serialized
commitwhen true XA-COMMIT is to be logged, and false when it's XA-ROLLBACK.
Returns
error code, 0 success

◆ do_unsafe_limit_checkout()

static void do_unsafe_limit_checkout ( char *  buf,
int  unsafe_type,
const char *  query 
)
static

Auxiliary function to check if the warning for limit unsafety should be thrown or suppressed.

Details of the implementation can be found in the comments inline.

Parameters
bufBuffer to hold the warning message text
unsafe_typeThe type of unsafety.
queryThe actual query statement.

TODO: Remove this function and implement a general service for all warnings that would prevent flooding the error log. => switch to log_throttle class?

◆ do_write_cache()

bool MYSQL_BIN_LOG::do_write_cache ( Binlog_cache_storage cache,
class Binlog_event_writer writer 
)
private

Write the contents of the given IO_CACHE to the binary log.

The cache will be reset as a READ_CACHE to be able to read the contents from it.

The data will be post-processed: see class Binlog_event_writer for details.

Parameters
cacheEvents will be read from this IO_CACHE.
writerEvents will be written to this Binlog_event_writer.
Return values
trueIO error.
falseSuccess.
See also
MYSQL_BIN_LOG::write_cache

◆ ending_single_stmt_trans()

bool ending_single_stmt_trans ( THD thd,
const bool  all 
)

This function checks if a single statement transaction is about to commit or not.

Parameters
thdThe client thread that executed the current statement.
allCommitting a transaction (i.e. true) or a statement (i.e. false).
Returns
true if committing a single statement transaction, otherwise false.

◆ ending_trans()

bool ending_trans ( THD thd,
const bool  all 
)

This function checks if a transaction, either a multi-statement or a single statement transaction is about to commit or not.

Parameters
thdThe client thread that executed the current statement.
allCommitting a transaction (i.e. true) or a statement (i.e. false).
Returns
true if committing a transaction, otherwise false.

◆ enroll_for()

bool Stage_manager::enroll_for ( StageID  stage,
THD first,
mysql_mutex_t stage_mutex 
)

Enroll a set of sessions for a stage.

This will queue the session thread for writing and flushing.

If the thread being queued is assigned as stage leader, it will return immediately.

If wait_if_follower is true the thread is not the stage leader, the thread will be wait for the queue to be processed by the leader before it returns. In DBUG-ON version the follower marks is preempt status as ready.

Parameters
stageStage identifier for the queue to append to.
firstQueue to append.
stage_mutexPointer to the currently held stage mutex, or NULL if we're not in a stage.
Return values
trueThread is stage leader.
falseThread was not stage leader and processing has been done.

◆ exec_binlog_error_action_abort()

static void exec_binlog_error_action_abort ( const char *  err_string)
static

When a fatal error occurs due to which binary logging becomes impossible and the user specified binlog_error_action= ABORT_SERVER the following function is invoked.

This function pushes the appropriate error message to client and logs the same to server error log and then aborts the server.

Parameters
err_stringError string which specifies the exact error message from the caller.
Return values
none

◆ fetch_and_empty()

THD * Stage_manager::Mutex_queue::fetch_and_empty ( )

Fetch the entire queue for a stage.

This will fetch the entire queue in one go.

◆ finalize() [1/3]

int binlog_cache_data::finalize ( THD thd,
Log_event end_event 
)

This function finalizes the cache preparing for commit or rollback.

The function just writes all the necessary events to the cache but does not flush the data to the binary log file. That is the role of the binlog_cache_data::flush function.

See also
binlog_cache_data::flush
Parameters
thdThe thread whose transaction should be flushed
end_eventThe end event either commit/rollback
Returns
nonzero if an error pops up when flushing the cache.

◆ finalize() [2/3]

int binlog_cache_data::finalize ( THD thd,
Log_event end_event,
XID_STATE xs 
)

The method writes XA END query to XA-prepared transaction's cache and calls the "basic" finalize().

Returns
error code, 0 success

◆ finalize() [3/3]

int binlog_stmt_cache_data::finalize ( THD thd)

◆ find_first_log_not_in_gtid_set()

bool MYSQL_BIN_LOG::find_first_log_not_in_gtid_set ( char *  binlog_file_name,
const Gtid_set gtid_set,
Gtid first_gtid,
const char **  errmsg 
)

Find the oldest binary log that contains any GTID that is not in the given gtid set.

Parameters
[out]binlog_file_namethe file name of oldest binary log found
[in]gtid_setthe given gtid set
[out]first_gtidthe first GTID information from the binary log file returned at binlog_file_name
[out]errmsgthe error message outputted, which is left untouched if the function returns false
Returns
false on success, true on error.

◆ find_log_pos()

int MYSQL_BIN_LOG::find_log_pos ( LOG_INFO linfo,
const char *  log_name,
bool  need_lock_index 
)

Find the position in the log-index-file for the given log name.

Parameters
[out]linfoThe found log file name will be stored here, along with the byte offset of the next log file name in the index file.
log_nameFilename to find in the index file, or NULL if we want to read the first entry.
need_lock_indexIf false, this function acquires LOCK_index; otherwise the lock should already be held by the caller.
Note
On systems without the truncate function the file will end with one or more empty lines. These will be ignored when reading the file.
Return values
0ok
LOG_INFO_EOFEnd of log-index-file found
LOG_INFO_IOGot IO error while reading file

◆ find_next_log()

int MYSQL_BIN_LOG::find_next_log ( LOG_INFO linfo,
bool  need_lock_index 
)

Find the position in the log-index-file for the given log name.

Parameters
[out]linfoThe filename will be stored here, along with the byte offset of the next filename in the index file.
need_lock_indexIf true, LOCK_index will be acquired; otherwise it should already be held by the caller.
Note
  • Before calling this function, one has to call find_log_pos() to set up 'linfo'
  • Mutex needed because we need to make sure the file pointer does not move from under our feet
Return values
0ok
LOG_INFO_EOFEnd of log-index-file found
LOG_INFO_IOGot IO error while reading file

◆ find_next_relay_log()

int MYSQL_BIN_LOG::find_next_relay_log ( char  log_name[FN_REFLEN+1])

Find the relay log name following the given name from relay log index file.

Parameters
[in,out]log_nameThe name is full path name.
Returns
return 0 if it finds next relay log. Otherwise return the error code.

◆ find_uniq_filename()

static int find_uniq_filename ( char *  name,
uint32  new_index_number 
)
static

Find a unique filename for 'filename.

#'.

Set '#' to the highest existing log file extension plus one.

This function will return nonzero if: (i) the generated name exceeds FN_REFLEN; (ii) if the number of extensions is exhausted; or (iii) some other error happened while examining the filesystem.

Returns
nonzero if not possible to get unique filename.

◆ finish_commit()

int MYSQL_BIN_LOG::finish_commit ( THD thd)
private

Helper function executed when leaving ordered_commit.

This function contain the necessary code for fetching the error code, doing post-commit checks, and wrapping up the commit if necessary.

It is typically called when enter_stage indicates that the thread should bail out, and also when the ultimate leader thread finishes executing ordered_commit.

It is typically used in this manner:

if (enter_stage(thd, Thread_queue::FLUSH_STAGE, thd, &LOCK_log))
return finish_commit(thd);
Returns
Error code if the session commit failed, or zero on success.

◆ flush() [1/2]

int binlog_cache_data::flush ( THD thd,
my_off_t bytes_written,
bool wrote_xid 
)

Flush caches to the binary log.

If the cache is finalized, the cache will be flushed to the binary log file. If the cache is not finalized, nothing will be done.

If flushing fails for any reason, an error will be reported and the cache will be reset. Flushing can fail in two circumstances:

  • It was not possible to write the cache to the file. In this case, it does not make sense to keep the cache.
  • The cache was successfully written to disk but post-flush actions (such as binary log rotation) failed. In this case, the cache is already written to disk and there is no reason to keep it.
See also
binlog_cache_data::finalize

◆ flush() [2/2]

bool MYSQL_BIN_LOG::flush ( )

◆ flush_and_set_pending_rows_event()

int MYSQL_BIN_LOG::flush_and_set_pending_rows_event ( THD thd,
Rows_log_event event,
bool  is_transactional 
)

◆ flush_and_sync()

bool MYSQL_BIN_LOG::flush_and_sync ( const bool  force = false)

Flush binlog cache and synchronize to disk.

This function flushes events in binlog cache to binary log file, it will do synchronizing according to the setting of system variable 'sync_binlog'. If file is synchronized, synced will be set to 1, otherwise 0.

Parameters
[in]forceif true, ignores the 'sync_binlog' and synchronizes the file.
Return values
0Success
otherFailure

◆ flush_cache_to_file()

int MYSQL_BIN_LOG::flush_cache_to_file ( my_off_t end_pos_var)
private

Flush the I/O cache to file.

Flush the binary log to the binlog file if any byte where written and signal that the binary log file has been updated if the flush succeeds.

◆ flush_thread_caches()

std::pair< int, my_off_t > MYSQL_BIN_LOG::flush_thread_caches ( THD thd)
private

Flush caches for session.

Note
set_trans_pos is called with a pointer to the file name that the binary log currently use and a rotation will change the contents of the variable.

The position is used when calling the after_flush, after_commit, and after_rollback hooks, but these have been placed so that they occur before a rotation is executed.

It is the responsibility of any plugin that use this position to copy it if they need it after the hook has returned.

The current "global" transaction_counter is stepped and its new value is assigned to the transaction.

◆ fulltext_unsafe_set()

static bool fulltext_unsafe_set ( TABLE_SHARE s)
inlinestatic

◆ generate_name()

const char * MYSQL_BIN_LOG::generate_name ( const char *  log_name,
const char *  suffix,
char *  buff 
)

◆ generate_new_name()

int MYSQL_BIN_LOG::generate_new_name ( char *  new_name,
const char *  log_name,
uint32  new_index_number = 0 
)
private

◆ get_current_log()

int MYSQL_BIN_LOG::get_current_log ( LOG_INFO linfo,
bool  need_lock_log = true 
)

◆ get_gtid_executed()

int MYSQL_BIN_LOG::get_gtid_executed ( Sid_map sid_map,
Gtid_set gtid_set 
)

Deep copy global_sid_map and gtid_executed.

Both operations are done under LOCK_commit and global_sid_lock protection.

Parameters
[out]sid_mapThe Sid_map to which global_sid_map will be copied.
[out]gtid_setThe Gtid_set to which gtid_executed will be copied.
Returns
the operation status
Return values
0OK
!=0Error

◆ get_locked_tables_mode_name()

const char* get_locked_tables_mode_name ( enum_locked_tables_mode  locked_tables_mode)

Getter for the enum enum_locked_tables_mode.

Parameters
locked_tables_modeenum for types of locked tables mode
Returns
The string represantation of that enum value

◆ get_log_index()

std::pair< int, std::list< std::string > > MYSQL_BIN_LOG::get_log_index ( bool  need_lock_index = true)

Retrieves the contents of the index file associated with this log object into an std::list<std::string> object.

The order held by the index file is kept.

Parameters
need_lock_indexwhether or not the lock over the index file should be acquired inside the function.
Returns
a pair: a function status code; a list of std::string objects with the content of the log index file.

◆ get_xa_opt()

enum xa_option_words get_xa_opt ( THD thd)
inline

◆ gtid_end_transaction()

int MYSQL_BIN_LOG::gtid_end_transaction ( THD thd)

If the thread owns a GTID, this function generates an empty transaction and releases ownership of the GTID.

  • If the binary log is disabled for this thread, the GTID is inserted directly into the mysql.gtid_executed table and the GTID is included in @global.gtid_executed. (This only happens for DDL, since DML will save the GTID into table and release ownership inside ha_commit_trans.)
  • If the binary log is enabled for this thread, an empty transaction consisting of GTID, BEGIN, COMMIT is written to the binary log, the GTID is included in @global.gtid_executed, and the GTID is added to the mysql.gtid_executed table on the next binlog rotation.

This function must be called by any committing statement (COMMIT, implicitly committing statements, or Xid_log_event), after the statement has completed execution, regardless of whether the statement updated the database.

This logic ensures that an empty transaction is generated for the following cases:

  • Explicit empty transaction: SET GTID_NEXT = 'UUID:NUMBER'; BEGIN; COMMIT;
  • Transaction or DDL that gets completely filtered out in the slave thread.
Parameters
thdThe committing thread
Return values
0Success
nonzeroError

◆ handle_binlog_flush_or_sync_error()

void MYSQL_BIN_LOG::handle_binlog_flush_or_sync_error ( THD thd,
bool  need_lock_log 
)
private

Helper function to handle flush or sync stage errors.

If binlog_error_action= ABORT_SERVER, server will be aborted after reporting the error to the client. If binlog_error_action= IGNORE_ERROR, binlog will be closed for the reset of the life time of the server. close() call is protected with LOCK_log to avoid any parallel operations on binary log.

Parameters
thdThread object that faced flush/sync error
need_lock_log> Indicates true if LOCk_log is needed before closing binlog (happens when we are handling sync error) > Indicates false if LOCK_log is already acquired by the thread (happens when we are handling flush error)

◆ handle_gtid_consistency_violation()

static bool handle_gtid_consistency_violation ( THD thd,
int  error_code,
int  log_error_code 
)
static

Given that a possible violation of gtid consistency has happened, checks if gtid-inconsistencies are forbidden by the current value of ENFORCE_GTID_CONSISTENCY and GTID_MODE.

If forbidden, generates error or warning accordingly.

Parameters
thdThe thread that has issued the GTID-violating statement.
error_codeThe error code to use, if error or warning is to be generated.
log_error_codeThe error code to use, if error message is to be logged.
Return values
falseError was generated.
trueNo error was generated (possibly a warning was generated).

◆ harvest_bytes_written()

void MYSQL_BIN_LOG::harvest_bytes_written ( Relay_log_info rli,
bool  need_log_space_lock 
)

◆ has_commit_order_manager()

static bool has_commit_order_manager ( THD thd)
inlinestatic

◆ has_write_table_auto_increment_not_first_in_pk()

static bool has_write_table_auto_increment_not_first_in_pk ( TABLE_LIST tables)
static

◆ has_write_table_with_auto_increment()

static bool has_write_table_with_auto_increment ( TABLE_LIST tables)
static

◆ has_write_table_with_auto_increment_and_select()

static bool has_write_table_with_auto_increment_and_select ( TABLE_LIST tables)
static

◆ inc_prep_xids()

void MYSQL_BIN_LOG::inc_prep_xids ( THD thd)
private

Increment the prepared XID counter.

◆ init_and_set_log_file_name()

bool MYSQL_BIN_LOG::init_and_set_log_file_name ( const char *  log_name,
const char *  new_name,
uint32  new_index_number 
)
private

◆ init_gtid_sets()

bool MYSQL_BIN_LOG::init_gtid_sets ( Gtid_set gtid_set,
Gtid_set lost_groups,
bool  verify_checksum,
bool  need_lock,
Transaction_boundary_parser trx_parser,
Gtid_monitoring_info partial_trx,
bool  is_server_starting = false 
)

Reads the set of all GTIDs in the binary/relay log, and the set of all lost GTIDs in the binary log, and stores each set in respective argument.

Parameters
gtid_setWill be filled with all GTIDs in this binary/relay log.
lost_groupsWill be filled with all GTIDs in the Previous_gtids_log_event of the first binary log that has a Previous_gtids_log_event. This is requested to binary logs but not to relay logs.
verify_checksumIf true, checksums will be checked.
need_lockIf true, LOCK_log, LOCK_index, and global_sid_lock->wrlock are acquired; otherwise they are asserted to be taken already.
[out]trx_parserThis will be used to return the actual relaylog transaction parser state because of the possibility of partial transactions.
[out]partial_trxIf a transaction was left incomplete on the relaylog, its GTID information should be returned to be used in the case of the rest of the transaction be added to the relaylog.
is_server_startingTrue if the server is starting.
Returns
false on success, true on error.

◆ init_pthread_objects()

void MYSQL_BIN_LOG::init_pthread_objects ( )

◆ is_active()

bool MYSQL_BIN_LOG::is_active ( const char *  log_file_name)

Check if we are writing/reading to the given log file.

◆ is_binlog_cache_empty()

bool THD::is_binlog_cache_empty ( bool  is_transactional) const

Return true if the statement/transaction cache is currently empty, false otherwise.

Parameters
is_transactionalif true, check the transaction cache. If false, check the statement cache.

◆ is_ddl_gtid_compatible()

bool THD::is_ddl_gtid_compatible ( )

◆ is_dml_gtid_compatible()

bool THD::is_dml_gtid_compatible ( bool  some_transactional_table,
bool  some_non_transactional_table,
bool  non_transactional_tables_are_tmp 
)

is_dml_gtid_compatible() and is_ddl_gtid_compatible() check if the statement that is about to be processed will safely get a GTID.

Currently, the following cases may lead to errors (e.g. duplicated GTIDs) and as such are forbidden:

  1. DML statements that mix non-transactional updates with transactional updates.
  2. Transactions that use non-transactional tables after having used transactional tables.
  3. CREATE...SELECT statement;
  4. CREATE TEMPORARY TABLE or DROP TEMPORARY TABLE within a transaction

The first two conditions have to be checked in decide_logging_format, because that's where we know if the table is transactional or not. These are implemented in is_dml_gtid_compatible().

The third and fourth conditions have to be checked in mysql_execute_command because (1) that prevents implicit commit from being executed if the statement fails; (2) DROP TEMPORARY TABLE does not invoke decide_logging_format. These are implemented in is_ddl_gtid_compatible().

In the cases where GTID violations generate errors, is_ddl_gtid_compatible() needs to be called before the implicit pre-commit, so that there is no implicit commit if the statement fails.

In the cases where GTID violations do not generate errors, is_ddl_gtid_compatible() needs to be called after the implicit pre-commit, because in these cases the function will increase the global counter automatic_gtid_violating_transaction_count or anonymous_gtid_violating_transaction_count. If there is an ongoing transaction, the implicit commit will commit the transaction, which will call update_gtids_impl, which should decrease the counters depending on whether the old was violating GTID-consistency or not. Thus, we should increase the counters only after the old transaction is committed.

Parameters
some_transactional_tabletrue if the statement updates some transactional table; false otherwise.
some_non_transactional_tabletrue if the statement updates some non-transactional table; false otherwise.
non_transactional_tables_are_tmptrue if all updated non-transactional tables are temporary.
Return values
trueif the statement is compatible;
falseif the statement is not compatible.

◆ is_empty_transaction_in_binlog_cache()

bool is_empty_transaction_in_binlog_cache ( const THD thd)

Check if at least one of transacaction and statement binlog caches contains an empty transaction, other one is empty or contains an empty transaction, which has two binlog events "BEGIN" and "COMMIT".

Parameters
thdThe client thread that executed the current statement.
Return values
trueAt least one of transacaction and statement binlog caches contains an empty transaction, other one is empty or contains an empty transaction.
falseOtherwise.

◆ is_inited_purge_index_file()

bool MYSQL_BIN_LOG::is_inited_purge_index_file ( )

◆ is_loggable_xa_prepare()

bool is_loggable_xa_prepare ( THD thd)
inline

Predicate function yields true when XA transaction is being logged having a proper state ready for prepare or commit in one phase.

Parameters
thdTHD pointer of running transaction
Returns
true When the being prepared transaction should be binlogged, false otherwise.

◆ is_number()

static bool is_number ( const char *  str,
ulong res,
bool  allow_wildcards 
)
static

Check if a string is a valid number.

Parameters
strString to test
resStore value here
allow_wildcardsSet to 1 if we should ignore '' and '_'
Note
For the moment the allow_wildcards argument is not used Should be moved to some other file.
Return values
1String is a number
0String is not a number

◆ is_query_in_union()

bool MYSQL_BIN_LOG::is_query_in_union ( THD thd,
query_id_t  query_id_param 
)

◆ is_transaction_empty()

bool is_transaction_empty ( THD thd)

Check if the the transaction is empty.

Parameters
thdThe client thread that executed the current statement.
Return values
trueNo changes found in any storage engine
falseOtherwise.

◆ issue_unsafe_warnings()

void THD::issue_unsafe_warnings ( )

Auxiliary method used by binlog_query() to raise warnings.

The type of warning and the type of unsafeness is stored in THD::binlog_unsafe_warning_flags.

◆ log_in_use()

static int log_in_use ( const char *  log_name)
static

◆ log_loaded_block()

int log_loaded_block ( IO_CACHE file)

Load data's io cache specific hook to be executed before a chunk of data is being read into the cache's buffer The fuction instantianates and writes into the binlog replication events along LOAD DATA processing.

Parameters
filepointer to io-cache
Return values
0success
1failure

◆ make_log_name()

void MYSQL_BIN_LOG::make_log_name ( char *  buf,
const char *  log_ident 
)

Create a new log file name.

Parameters
[out]bufBuffer allocated with at least FN_REFLEN bytes where new name is stored.
log_identIdentity of the binary/relay log.
Note
If file name will be longer then FN_REFLEN it will be truncated

◆ move_crash_safe_index_file_to_index_file()

int MYSQL_BIN_LOG::move_crash_safe_index_file_to_index_file ( bool  need_lock_index)

Move crash safe index file to index file.

Parameters
need_lock_indexIf true, LOCK_index will be acquired; otherwise it should already be held.
Return values
0ok
-1error

◆ mysql_show_binlog_events()

bool mysql_show_binlog_events ( THD thd)

Execute a SHOW BINLOG EVENTS statement.

Parameters
thdPointer to THD object for the client thread executing the statement.
Return values
falsesuccess
truefailure

◆ new_file()

int MYSQL_BIN_LOG::new_file ( Format_description_log_event extra_description_event)

◆ new_file_impl()

int MYSQL_BIN_LOG::new_file_impl ( bool  need_lock_log,
Format_description_log_event extra_description_event 
)
private

Start writing to a new log file or reopen the old file.

Parameters
need_lock_logIf true, this function acquires LOCK_log; otherwise the caller should already have acquired it.
extra_description_eventThe master's FDE to be written by the I/O thread while creating a new relay log file. This should be NULL for binary log files.
Return values
0success
nonzero- error
Note
The new file name is stored last in the index file

◆ new_file_without_locking()

int MYSQL_BIN_LOG::new_file_without_locking ( Format_description_log_event extra_description_event)

◆ next_file_id()

uint MYSQL_BIN_LOG::next_file_id ( )

◆ normalize_binlog_name()

bool normalize_binlog_name ( char *  to,
const char *  from,
bool  is_relay_log 
)

Turns a relative log binary log path into a full path, based on the opt_bin_logname or opt_relay_logname.

Also trims the cr-lf at the end of the full_path before return to avoid any server startup problem on windows.

Parameters
fromThe log name we want to make into an absolute path.
toThe buffer where to put the results of the normalization.
is_relay_logSwitch that makes is used inside to choose which option (opt_bin_logname or opt_relay_logname) to use when calculating the base path.
Returns
true if a problem occurs, false otherwise.

◆ open()

bool MYSQL_BIN_LOG::open ( PSI_file_key  log_file_key,
const char *  log_name,
const char *  new_name,
uint32  new_index_number 
)
private

Open the logfile and init IO_CACHE.

Parameters
log_file_keyThe file instrumentation key for this file
log_nameThe name of the log to open
new_nameThe new name for the logfile. NULL forces generate_new_name() to be called.
new_index_numberThe binary log file index number to start from after the RESET MASTER TO command is called.
Returns
true if error, false otherwise.

◆ open_binlog() [1/2]

int MYSQL_BIN_LOG::open_binlog ( const char *  opt_name)

◆ open_binlog() [2/2]

bool MYSQL_BIN_LOG::open_binlog ( const char *  log_name,
const char *  new_name,
ulong  max_size_arg,
bool  null_created_arg,
bool  need_lock_index,
bool  need_sid_lock,
Format_description_log_event extra_description_event,
uint32  new_index_number = 0 
)

Create a new binary log.

Open a (new) binlog file.

Parameters
log_nameName of binlog
new_nameName of binlog, too. todo: what's the difference between new_name and log_name?
max_size_argThe size at which this binlog will be rotated.
null_created_argIf false, and a Format_description_log_event is written, then the Format_description_log_event will have the timestamp 0. Otherwise, it the timestamp will be the time when the event was written to the log.
need_lock_indexIf true, LOCK_index is acquired; otherwise LOCK_index must be taken by the caller.
need_sid_lockIf true, the read lock on global_sid_lock will be acquired. Otherwise, the caller must hold the read lock on global_sid_lock.
extra_description_eventThe master's FDE to be written by the I/O thread while creating a new relay log file. This should be NULL for binary log files.
new_index_numberThe binary log file index number to start from after the RESET MASTER TO command is called.
  • Open the log file and the index file. Register the new file name in it
  • When calling this when the file is in use, you must have a locks on LOCK_log and LOCK_index.
Return values
0ok
1error

Perhaps we might need the code below in MYSQL_BIN_LOG::cleanup for "real life" purposes as well?

◆ open_crash_safe_index_file()

int MYSQL_BIN_LOG::open_crash_safe_index_file ( )

Open a (new) crash safe index file.

Note
The crash safe index file is a special file used for guaranteeing index file crash safe.
Return values
0ok
1error

◆ open_index_file()

bool MYSQL_BIN_LOG::open_index_file ( const char *  index_file_name_arg,
const char *  log_name,
bool  need_lock_index 
)

◆ open_purge_index_file()

int MYSQL_BIN_LOG::open_purge_index_file ( bool  destroy)

◆ ordered_commit()

int MYSQL_BIN_LOG::ordered_commit ( THD thd,
bool  all,
bool  skip_commit = false 
)
private

Flush and commit the transaction.

This will execute an ordered flush and commit of all outstanding transactions and is the main function for the binary log group commit logic. The function performs the ordered commit in two phases.

The first phase flushes the caches to the binary log and under LOCK_log and marks all threads that were flushed as not pending.

The second phase executes under LOCK_commit and commits all transactions in order.

The procedure is:

  1. Queue ourselves for flushing.
  2. Grab the log lock, which might result is blocking if the mutex is already held by another thread.
  3. If we were not committed while waiting for the lock
    1. Fetch the queue
    2. For each thread in the queue: a. Attach to it b. Flush the caches, saving any error code
    3. Flush and sync (depending on the value of sync_binlog).
    4. Signal that the binary log was updated
  4. Release the log lock
  5. Grab the commit lock
    1. For each thread in the queue: a. If there were no error when flushing and the transaction shall be committed:
      • Commit the transaction, saving the result of executing the commit.
  6. Release the commit lock
  7. Call purge, if any of the committed thread requested a purge.
  8. Return with the saved error code
Parameters
thdSession to commit transaction for
allThis is true if this is a real transaction commit, and false otherwise.
skip_commitThis is true if the call to ha_commit_low should be skipped (it is handled by the caller somehow) and false otherwise (the normal case).

◆ pop_front()

std::pair< bool, THD * > Stage_manager::Mutex_queue::pop_front ( )

◆ prepare()

int MYSQL_BIN_LOG::prepare ( THD thd,
bool  all 
)
virtual

Log a prepare record of the transaction to the storage engines.

Parameters
thdSession to log transaction record for.
alltrue if an explicit commit or an implicit commit for a statement, false if an internal commit of the statement.
Returns
Error code on failure, zero on success.

Implements TC_LOG.

◆ print_unsafe_warning_to_log()

static void print_unsafe_warning_to_log ( int  unsafe_type,
char *  buf,
const char *  query 
)
static

Auxiliary function to print warning in the error log.

◆ process_after_commit_stage_queue()

void MYSQL_BIN_LOG::process_after_commit_stage_queue ( THD thd,
THD first 
)
private

Process after commit for a sequence of sessions.

Parameters
thdThe "master" thread
firstFirst thread in the queue of threads to commit

◆ process_commit_stage_queue()

void MYSQL_BIN_LOG::process_commit_stage_queue ( THD thd,
THD first 
)
private

Commit a sequence of sessions.

This function commit an entire queue of sessions starting with the session in first. If there were an error in the flushing part of the ordered commit, the error code is passed in and all the threads are marked accordingly (but not committed).

It will also add the GTIDs of the transactions to gtid_executed.

See also
MYSQL_BIN_LOG::ordered_commit
Parameters
thdThe "master" thread
firstFirst thread in the queue of threads to commit

◆ process_flush_stage_queue()

int MYSQL_BIN_LOG::process_flush_stage_queue ( my_off_t total_bytes_var,
bool rotate_var,
THD **  out_queue_var 
)
private

Execute the flush stage.

Parameters
[out]total_bytes_varPointer to variable that will be set to total number of bytes flushed, or NULL.
[out]rotate_varPointer to variable that will be set to true if binlog rotation should be performed after releasing locks. If rotate is not necessary, the variable will not be touched.
[out]out_queue_varPointer to the sessions queue in flush stage.
Returns
Error code on error, zero on success

◆ purge()

void MYSQL_BIN_LOG::purge ( )

The method executes logs purging routine.

◆ purge_error_message()

static bool purge_error_message ( THD thd,
int  res 
)
static

◆ purge_index_entry()

int MYSQL_BIN_LOG::purge_index_entry ( THD thd,
ulonglong decrease_log_space,
bool  need_lock_index 
)

◆ purge_log_get_error_code()

static uint purge_log_get_error_code ( int  res)
static

purge logs, master and slave sides both, related error code convertor.

Called from purge_error_message(), MYSQL_BIN_LOG::reset_logs()

Parameters
resan error code as used by purging routines
Returns
the user level error code ER_*

◆ purge_logs()

int MYSQL_BIN_LOG::purge_logs ( const char *  to_log,
bool  included,
bool  need_lock_index,
bool  need_update_threads,
ulonglong decrease_log_space,
bool  auto_purge 
)

Remove all logs before the given log from disk and from the index file.

Parameters
to_logDelete all log file name before this file.
includedIf true, to_log is deleted too.
need_lock_index
need_update_threadsIf we want to update the log coordinates of all threads. False for relay logs, true otherwise.
decrease_log_spaceIf not null, decrement this variable of the amount of log space freed
auto_purgeTrue if this is an automatic purge.
Note
If any of the logs before the deleted one is in use, only purge logs up to this one.
Return values
0ok
LOG_INFO_EOFto_log not found LOG_INFO_EMFILE too many files opened LOG_INFO_FATAL if any other than ENOENT error from mysql_file_stat() or mysql_file_delete()

◆ purge_logs_before_date()

int MYSQL_BIN_LOG::purge_logs_before_date ( time_t  purge_time,
bool  auto_purge 
)

Remove all logs before the given file date from disk and from the index file.

Parameters
purge_timeDelete all log files before given date.
auto_purgeTrue if this is an automatic purge.
Note
If any of the logs before the deleted one is in use, only purge logs up to this one.
Return values
0ok
LOG_INFO_PURGE_NO_ROTATEBinary file that can't be rotated LOG_INFO_FATAL if any other than ENOENT error from mysql_file_stat() or mysql_file_delete()

◆ purge_master_logs()

bool purge_master_logs ( THD thd,
const char *  to_log 
)

Execute a PURGE BINARY LOGS TO <log> command.

Parameters
thdPointer to THD object for the client thread executing the statement.
to_logName of the last log to purge.
Return values
falsesuccess
truefailure

◆ purge_master_logs_before_date()

bool purge_master_logs_before_date ( THD thd,
time_t  purge_time 
)

Execute a PURGE BINARY LOGS BEFORE <date> command.

Parameters
thdPointer to THD object for the client thread executing the statement.
purge_timeDate before which logs should be purged.
Return values
falsesuccess
truefailure

◆ query_error_code()

int query_error_code ( const THD thd,
bool  not_killed 
)

◆ raw_get_current_log()

int MYSQL_BIN_LOG::raw_get_current_log ( LOG_INFO linfo)

◆ read_gtids_and_update_trx_parser_from_relaylog()

static bool read_gtids_and_update_trx_parser_from_relaylog ( const char *  filename,
Gtid_set retrieved_gtids,
bool  verify_checksum,
Transaction_boundary_parser trx_parser,
Gtid_monitoring_info partial_trx 
)
static

Add the GTIDs from the given relaylog file and also update the IO thread transaction parser.

Parameters
filenameRelaylog file to read from.
retrieved_gtidsGtid_set to store the GTIDs found on the relaylog file.
verify_checksumSet to true to verify event checksums.
trx_parserThe transaction boundary parser to be used in order to only add a GTID to the gtid_set after ensuring the transaction is fully stored on the relay log.
partial_trxThe trx_monitoring_info of the last incomplete transaction found in the relay log.
Return values
falseThe file was successfully read and all GTIDs from Previous_gtids and Gtid_log_event from complete transactions were added to the retrieved_set.
trueThere was an error during the procedure.

◆ read_gtids_from_binlog()

static enum_read_gtids_from_binlog_status read_gtids_from_binlog ( const char *  filename,
Gtid_set all_gtids,
Gtid_set prev_gtids,
Gtid first_gtid,
Sid_map sid_map,
bool  verify_checksum,
bool  is_relay_log 
)
static

◆ reencrypt_logs()

bool MYSQL_BIN_LOG::reencrypt_logs ( )

Re-encrypt previous existent binary/relay logs as below.

Starting from the next to last entry on the index file, iterating down to the first one:

  • If the file is encrypted, re-encrypt it. Otherwise, skip it.
  • If failed to open the file, report an error.
Return values
FalseSuccess
TrueError

◆ register_binlog_handler()

void register_binlog_handler ( THD thd,
bool  trx 
)

◆ register_create_index_entry()

int MYSQL_BIN_LOG::register_create_index_entry ( const char *  entry)

◆ register_purge_index_entry()

int MYSQL_BIN_LOG::register_purge_index_entry ( const char *  entry)

◆ remove_logs_from_index()

int MYSQL_BIN_LOG::remove_logs_from_index ( LOG_INFO log_info,
bool  need_update_threads 
)

Remove logs from index file.

  • To make crash safe, we copy the content of index file from index_file_start_offset recored in log_info to crash safe index file firstly and then move the crash safe index file to index file.
Parameters
log_infoStore here the found log file name and position to the NEXT log file name in the index file.
need_update_threadsIf we want to update the log coordinates of all threads. False for relay logs, true otherwise.
Return values
0ok
LOG_INFO_IOGot IO error while reading/writing file

◆ report_binlog_write_error()

void MYSQL_BIN_LOG::report_binlog_write_error ( )
private

◆ report_cache_write_error()

void MYSQL_BIN_LOG::report_cache_write_error ( THD thd,
bool  is_transactional 
)

◆ reset_binlog_unsafe_suppression()

static void reset_binlog_unsafe_suppression ( )
static

Auxiliary function to reset the limit unsafety warning suppression.

◆ reset_logs()

bool MYSQL_BIN_LOG::reset_logs ( THD thd,
bool  delete_only = false 
)

Removes files, as part of a RESET MASTER or RESET SLAVE statement, by deleting all logs referred to in the index file and the index file.

Then, it creates a new index file and a new log file.

The new index file will only contain the new log file.

Parameters
thdThread
delete_onlyIf true, do not create a new index file and a new log file.
Note
If not called from slave thread, write start event to new log
Return values
0ok
1error

◆ rollback()

int MYSQL_BIN_LOG::rollback ( THD thd,
bool  all 
)
virtual

Write a rollback record of the transaction to the binary log.

For binary log group commit, the rollback is separated into three parts:

  1. First part consists of filling the necessary caches and finalizing them (if they need to be finalized). After a cache is finalized, nothing can be added to the cache.
  2. Second part execute an ordered flush and commit. This will be done using the group commit functionality in ordered_commit.

    Since we roll back the transaction early, we call ordered_commit with the skip_commit flag set. The ha_commit_low call inside ordered_commit will then not be called.

  3. Third part checks any errors resulting from the flush and handles them appropriately.
See also
MYSQL_BIN_LOG::ordered_commit
ha_commit_low
ha_rollback_low
Parameters
thdSession to commit
allThis is true if this is a real transaction rollback, and false otherwise.
Returns
Error code, or zero if there were no error.

Implements TC_LOG.

◆ rotate()

int MYSQL_BIN_LOG::rotate ( bool  force_rotate,
bool check_purge 
)

The method executes rotation when LOCK_log is already acquired by the caller.

Parameters
force_rotatecaller can request the log rotation
check_purgeis set to true if rotation took place
Note
If rotation fails, for instance the server was unable to create a new log file, we still try to write an incident event to the current log.
The caller must hold LOCK_log when invoking this function.
Return values
nonzero- error in rotating routine.

◆ rotate_and_purge()

int MYSQL_BIN_LOG::rotate_and_purge ( THD thd,
bool  force_rotate 
)

Execute a FLUSH LOGS statement.

The method is a shortcut of rotate() and purge(). LOCK_log is acquired prior to rotate and is released after it.

Parameters
thdCurrent session.
force_rotatecaller can request the log rotation
Return values
nonzero- error in rotating routine.

◆ set_crash_safe_index_file_name()

int MYSQL_BIN_LOG::set_crash_safe_index_file_name ( const char *  base_file_name)

Set the name of crash safe index file.

Return values
0ok
1error

◆ set_max_size()

void MYSQL_BIN_LOG::set_max_size ( ulong  max_size_arg)

◆ set_purge_index_file_name()

int MYSQL_BIN_LOG::set_purge_index_file_name ( const char *  base_file_name)

◆ show_binlog_events()

template<class BINLOG_FILE_READER >
bool show_binlog_events ( THD thd,
MYSQL_BIN_LOG binary_log 
)

◆ show_query_type()

static const char* show_query_type ( THD::enum_binlog_query_type  qtype)
static

◆ signal_done()

void Stage_manager::signal_done ( THD queue)

◆ start_union_events()

void MYSQL_BIN_LOG::start_union_events ( THD thd,
query_id_t  query_id_param 
)

◆ stmt_cannot_safely_rollback()

bool stmt_cannot_safely_rollback ( const THD thd)

This function checks if current statement cannot be rollded back safely.

Parameters
thdThe client thread that executed the current statement.
Returns
true if cannot be safely rolled back, false otherwise.

◆ stmt_has_updated_trans_table()

bool stmt_has_updated_trans_table ( Ha_trx_info ha_list)

This function checks if a transactional table was updated by the current statement.

Parameters
ha_listRegistered storage engine handler list.
Returns
true if a transactional table was updated, false otherwise.

◆ stop_union_events()

void MYSQL_BIN_LOG::stop_union_events ( THD thd)

◆ sync_binlog_file()

std::pair< bool, bool > MYSQL_BIN_LOG::sync_binlog_file ( bool  force)
private

Call fsync() to sync the file to disk.

◆ sync_purge_index_file()

int MYSQL_BIN_LOG::sync_purge_index_file ( )

◆ thd_get_cache_mngr()

static binlog_cache_mngr* thd_get_cache_mngr ( const THD thd)
static

◆ trans_cannot_safely_rollback()

bool trans_cannot_safely_rollback ( const THD thd)

This function checks if a transaction cannot be rolled back safely.

Parameters
thdThe client thread that executed the current statement.
Returns
true if cannot be safely rolled back, false otherwise.

◆ trans_has_updated_trans_table()

bool trans_has_updated_trans_table ( const THD thd)

This function checks if a transactional table was updated by the current transaction.

Parameters
thdThe client thread that executed the current statement.
Returns
true if a transactional table was updated, false otherwise.

◆ truncate()

int binlog_trx_cache_data::truncate ( THD thd,
bool  all 
)

This function truncates the transactional cache upon committing or rolling back either a transaction or a statement.

Parameters
thdThe thread whose transaction should be flushed
alltrue means truncate the transaction, otherwise the statement must be truncated.
Returns
nonzero if an error pops up when truncating the transactional cache.

◆ truncate_relaylog_file()

bool MYSQL_BIN_LOG::truncate_relaylog_file ( Master_info mi,
my_off_t  truncate_pos 
)


Truncate the active relay log file in the specified position.

Parameters
miMaster_info of the channel going to truncate the relay log file.
truncate_posThe position to truncate the active relay log file.
Returns
False on success and true on failure.

◆ update_binlog_end_pos() [1/2]

void MYSQL_BIN_LOG::update_binlog_end_pos ( bool  need_lock = true)

◆ update_binlog_end_pos() [2/2]

void MYSQL_BIN_LOG::update_binlog_end_pos ( const char *  file,
my_off_t  pos 
)
inline

◆ update_thd_next_event_pos()

void MYSQL_BIN_LOG::update_thd_next_event_pos ( THD thd)

◆ wait_count_or_timeout()

void Stage_manager::wait_count_or_timeout ( ulong  count,
long  usec,
StageID  stage 
)

Introduces a wait operation on the executing thread.

The waiting is done until the timeout elapses or count is reached (whichever comes first).

If count == 0, then the session will wait until the timeout elapses. If timeout == 0, then there is no waiting.

Parameters
usecthe number of microseconds to wait.
countwait for as many as count to join the queue the session is waiting on
stagewhich stage queue size to compare count against.

◆ wait_for_update()

int MYSQL_BIN_LOG::wait_for_update ( const struct timespec *  timeout)

Wait until we get a signal that the binary log has been updated.

Applies to master only.

NOTES

Parameters
[in]timeouta pointer to a timespec; NULL means to wait w/o timeout.
Return values
0if got signalled on update
non-0if wait timeout elapsed
Note
LOCK_binlog_end_pos must be taken before calling this function. LOCK_binlog_end_pos is being released while the thread is waiting. LOCK_binlog_end_pos is released by the caller.

◆ write_buffer()

bool MYSQL_BIN_LOG::write_buffer ( const char *  buf,
uint  len,
Master_info mi 
)

◆ write_cache()

bool MYSQL_BIN_LOG::write_cache ( THD thd,
class binlog_cache_data cache_data,
class Binlog_event_writer writer 
)

Write the contents of the statement or transaction cache to the binary log.

Comparison with do_write_cache:

  • do_write_cache is a lower-level function that only performs the actual write.
  • write_cache is a higher-level function that calls do_write_cache and additionally performs some maintenance tasks, including:
    • report any errors that occurred
    • write incident event if needed
    • update gtid_state
    • update thd.binlog_next_event_pos
Parameters
thdThread variable
cache_dataEvents will be read from the IO_CACHE of this cache_data object.
writerEvents will be written to this Binlog_event_writer.
Return values
trueIO error.
falseSuccess.
Note
We only come here if there is something in the cache.
Whatever is in the cache is always a complete transaction.
'cache' needs to be reinitialized after this functions returns.

◆ write_dml_directly()

bool MYSQL_BIN_LOG::write_dml_directly ( THD thd,
const char *  stmt,
size_t  stmt_len 
)

Write a dml into statement cache and then flush it into binlog.

It writes Gtid_log_event and BEGIN, COMMIT automatically.

It is aimed to handle cases of "background" logging where a statement is logged indirectly, like "DELETE FROM a_memory_table". So don't use it on any normal statement.

Parameters
[in]thdthe THD object of current thread.
[in]stmtthe DELETE statement.
[in]stmt_lenthe length of DELETE statement.
Returns
Returns false if succeeds, otherwise true is returned.

◆ write_event() [1/3]

int binlog_cache_data::write_event ( Log_event event)

◆ write_event() [2/3]

bool MYSQL_BIN_LOG::write_event ( Log_event event_info)

Write an event to the binary log cache.

◆ write_event() [3/3]

bool MYSQL_BIN_LOG::write_event ( Log_event ev,
Master_info mi 
)

◆ write_event_to_binlog()

bool MYSQL_BIN_LOG::write_event_to_binlog ( Log_event ev)
inline

◆ write_event_to_binlog_and_sync()

bool MYSQL_BIN_LOG::write_event_to_binlog_and_sync ( Log_event ev)

◆ write_gtid()

bool MYSQL_BIN_LOG::write_gtid ( THD thd,
binlog_cache_data cache_data,
class Binlog_event_writer writer 
)

Write the Gtid_log_event to the binary log (prior to writing the statement or transaction cache).

Parameters
thdThread that is committing.
cache_dataThe cache that is flushing.
writerThe event will be written to this Binlog_event_writer object.
Return values
falseSuccess.
trueError.

◆ write_incident() [1/2]

bool MYSQL_BIN_LOG::write_incident ( THD thd,
bool  need_lock_log,
const char *  err_msg,
bool  do_flush_and_sync = true 
)

Creates an incident event and writes it to the binary log.

Parameters
thdThread variable
need_lock_logIf the binary lock should be locked or not
err_msgError message written to log file for the incident.
do_flush_and_syncIf true, will call flush_and_sync(), rotate() and purge().
Return values
0error
1success

◆ write_incident() [2/2]

bool MYSQL_BIN_LOG::write_incident ( Incident_log_event ev,
THD thd,
bool  need_lock_log,
const char *  err_msg,
bool  do_flush_and_sync = true 
)

Writes an incident event to stmt_cache.

Parameters
evIncident event to be written
thdThread variable
need_lock_logIf true, will acquire LOCK_log; otherwise the caller should already have acquired LOCK_log.
err_msgError message written to log file for the incident.
do_flush_and_syncIf true, will call flush_and_sync(), rotate() and purge().
Return values
falseerror
truesuccess

Variable Documentation

◆ binlog_hton

handlerton* binlog_hton
static

◆ binlog_storage_engine

struct st_mysql_storage_engine binlog_storage_engine
Initial value:
= {
#define MYSQL_HANDLERTON_INTERFACE_VERSION
Definition: plugin.h:704

◆ g_stage_name

const char* g_stage_name[]
static
Initial value:
= {
"FLUSH",
"SYNC",
"COMMIT",
}

Names for the stages.

◆ limit_unsafe_suppression_start_time

ulonglong limit_unsafe_suppression_start_time = 0
static

◆ limit_unsafe_warning_count

int limit_unsafe_warning_count = 0
static

◆ log_bin_basename

const char* log_bin_basename = 0

◆ log_bin_index

const char* log_bin_index = 0

◆ opt_binlog_order_commits

bool opt_binlog_order_commits = true

◆ rpl_read_size

ulong rpl_read_size

◆ sync_binlog_period

MYSQL_BIN_LOG mysql_bin_log& sync_binlog_period

◆ unsafe_warning_suppression_is_activated

bool unsafe_warning_suppression_is_activated = false
static