MySQL  8.0.18
Source Code Documentation
Binlog_sender Class Reference

The major logic of dump thread is implemented in this class. More...

#include <rpl_binlog_sender.h>

Inheritance diagram for Binlog_sender:
Gtid_mode_copy

Classes

class  Event_allocator
 Binlog_sender reads events one by one. More...
 

Public Member Functions

 Binlog_sender (THD *thd, const char *start_file, my_off_t start_pos, Gtid_set *exclude_gtids, uint32 flag)
 
 ~Binlog_sender ()
 
void run ()
 It checks the dump reqest and sends events to the client until it finish all events(for mysqlbinlog) or encounters an error. More...
 

Private Types

typedef Basic_binlog_file_reader< Binlog_ifile, Binlog_event_data_istream, Binlog_event_object_istream, Event_allocatorFile_reader
 

Private Member Functions

void init ()
 
void cleanup ()
 
void init_heartbeat_period ()
 
void init_checksum_alg ()
 
int check_start_file ()
 Check if the requested binlog file and position are valid. More...
 
const char * log_read_error_msg (Binlog_read_error::Error_type error)
 Transform read error numbers to error messages. More...
 
int send_binlog (File_reader *reader, my_off_t start_pos)
 It dumps a binlog file. More...
 
int send_events (File_reader *reader, my_off_t end_pos)
 It sends some events in a binlog file to the client. More...
 
int get_binlog_end_pos (File_reader *reader, my_off_t *end_pos)
 It gets the end position of the binlog file. More...
 
int has_previous_gtid_log_event (File_reader *reader, bool *found)
 It checks if a binlog file has Previous_gtid_log_event. More...
 
int fake_rotate_event (const char *next_log_file, my_off_t log_pos)
 
It sends a faked rotate event which does not exist physically in any binlog to the slave. More...
 
int send_format_description_event (File_reader *reader, my_off_t start_pos)
 When starting to dump a binlog file, Format_description_log_event is read and sent first. More...
 
int send_heartbeat_event (my_off_t log_pos)
 It sends a heartbeat to the client. More...
 
int read_event (File_reader *reader, uchar **event_ptr, uint32 *event_len)
 It reads an event from binlog file. More...
 
bool check_event_type (binary_log::Log_event_type type, const char *log_file, my_off_t log_pos)
 Check if it is allowed to send this event type. More...
 
bool skip_event (const uchar *event_ptr, bool in_exclude_group)
 It checks if the event is in m_exclude_gtid. More...
 
void calc_event_checksum (uchar *event_ptr, size_t event_len)
 
int flush_net ()
 
int send_packet ()
 
int send_packet_and_flush ()
 
int before_send_hook (const char *log_file, my_off_t log_pos)
 
int after_send_hook (const char *log_file, my_off_t log_pos)
 
int reset_transmit_packet (ushort flags, size_t event_len=0)
 
int wait_new_events (my_off_t log_pos)
 It waits until receiving an update_cond signal. More...
 
int wait_with_heartbeat (my_off_t log_pos)
 
int wait_without_heartbeat ()
 
int check_event_count ()
 
bool has_error ()
 
void set_error (int errorno, const char *errmsg)
 
void set_unknown_error (const char *errmsg)
 
void set_fatal_error (const char *errmsg)
 
bool is_fatal_error ()
 
bool event_checksum_on ()
 
void set_last_pos (my_off_t log_pos)
 
void set_last_file (const char *log_file)
 
bool grow_packet (size_t extra_size)
 This function SHALL grow the buffer of the packet if needed. More...
 
bool shrink_packet ()
 This function SHALL shrink the size of the buffer used. More...
 
size_t calc_grow_buffer_size (size_t current_size, size_t min_size)
 
Helper function to recalculate a new size for the growing buffer. More...
 
void calc_shrink_buffer_size (size_t current_size)
 
Helper function to recalculate the new size for the m_new_shrink_size. More...
 
- Private Member Functions inherited from Gtid_mode_copy
enum_gtid_mode get_gtid_mode_from_copy (enum_gtid_mode_lock have_lock)
 Return the current server GTID_MODE without acquiring locks if possible. More...
 

Private Attributes

THDm_thd
 
Stringm_packet
 
const char * m_start_file
 
my_off_t m_start_pos
 
Gtid_setm_exclude_gtid
 
bool m_using_gtid_protocol
 
bool m_check_previous_gtid_event
 
bool m_gtid_clear_fd_created_flag
 
LOG_INFO m_linfo
 
binary_log::enum_binlog_checksum_alg m_event_checksum_alg
 
binary_log::enum_binlog_checksum_alg m_slave_checksum_alg
 
std::chrono::nanoseconds m_heartbeat_period
 
std::chrono::nanoseconds m_last_event_sent_ts
 
bool m_wait_new_events
 
Diagnostics_area m_diag_area
 
char m_errmsg_buf [MYSQL_ERRMSG_SIZE]
 
const char * m_errmsg
 
int m_errno
 
char m_last_file_buf [FN_REFLEN]
 
const char * m_last_file
 
my_off_t m_last_pos
 
ushort m_half_buffer_size_req_counter
 
size_t m_new_shrink_size
 
uint32 m_flag
 
bool m_observe_transmission
 
bool m_transmit_started
 
int m_event_count
 

Static Private Attributes

static const uint32 PACKET_MAX_SIZE = UINT_MAX32
 
static const ushort PACKET_SHRINK_COUNTER_THRESHOLD = 100
 
static const uint32 PACKET_MIN_SIZE = 4096
 The minimum size of the buffer. More...
 
static const float PACKET_GROW_FACTOR = 2.0
 How much to grow the buffer each time we need to accommodate more bytes than it currently can hold. More...
 
static const float PACKET_SHRINK_FACTOR = 0.5
 The dual of PACKET_GROW_FACTOR. More...
 

Detailed Description

The major logic of dump thread is implemented in this class.

It sends required binlog events to clients according to their requests.

Member Typedef Documentation

◆ File_reader

Constructor & Destructor Documentation

◆ Binlog_sender()

Binlog_sender::Binlog_sender ( THD thd,
const char *  start_file,
my_off_t  start_pos,
Gtid_set exclude_gtids,
uint32  flag 
)

◆ ~Binlog_sender()

Binlog_sender::~Binlog_sender ( )
inline

Member Function Documentation

◆ after_send_hook()

int Binlog_sender::after_send_hook ( const char *  log_file,
my_off_t  log_pos 
)
inlineprivate

◆ before_send_hook()

int Binlog_sender::before_send_hook ( const char *  log_file,
my_off_t  log_pos 
)
inlineprivate

◆ calc_event_checksum()

void Binlog_sender::calc_event_checksum ( uchar event_ptr,
size_t  event_len 
)
inlineprivate

◆ calc_grow_buffer_size()

size_t Binlog_sender::calc_grow_buffer_size ( size_t  current_size,
size_t  min_size 
)
inlineprivate


Helper function to recalculate a new size for the growing buffer.

Parameters
current_sizeThe baseline (for instance, the current buffer size).
min_sizeThe resulting buffer size, needs to be at least as large as this parameter states.
Returns
The new buffer size, or 0 in the case of an error.

◆ calc_shrink_buffer_size()

void Binlog_sender::calc_shrink_buffer_size ( size_t  current_size)
private


Helper function to recalculate the new size for the m_new_shrink_size.

Parameters
current_sizeThe baseline (for instance, the current buffer size).

◆ check_event_count()

int Binlog_sender::check_event_count ( )
inlineprivate

◆ check_event_type()

bool Binlog_sender::check_event_type ( binary_log::Log_event_type  type,
const char *  log_file,
my_off_t  log_pos 
)
private

Check if it is allowed to send this event type.

The following are disallowed:

  • GTID_MODE=ON and type==ANONYMOUS_GTID_LOG_EVENT
  • AUTO_POSITION=1 and type==ANONYMOUS_GTID_LOG_EVENT
  • GTID_MODE=OFF and type==GTID_LOG_EVENT
Parameters
typeThe event type.
log_fileThe binary log file (used in error messages).
log_posThe binary log position (used in error messages).
Return values
trueThe event is not allowed. In this case, this function calls set_fatal_error().
falseThe event is allowed.

◆ check_start_file()

int Binlog_sender::check_start_file ( )
private

Check if the requested binlog file and position are valid.

◆ cleanup()

void Binlog_sender::cleanup ( )
private

◆ event_checksum_on()

bool Binlog_sender::event_checksum_on ( )
inlineprivate

◆ fake_rotate_event()

int Binlog_sender::fake_rotate_event ( const char *  next_log_file,
my_off_t  log_pos 
)
private


It sends a faked rotate event which does not exist physically in any binlog to the slave.

It contains the name of the binlog we are going to send to the slave.

Faked rotate event is required in a few cases, so slave can know which binlog the following events are from.

  • The binlog file slave requested is Empty. E.g. "CHANGE MASTER TO MASTER_LOG_FILE='', MASTER_LOG_POS=4", etc.
  • The position slave requested is exactly the end of a binlog file.
  • Previous binlog file does not include a rotate event. It happens when server is shutdown and restarted.
  • The previous binary log was GTID-free (does not contain a Previous_gtids_log_event) and the slave is connecting using the GTID protocol.
Parameters
[in]next_log_fileThe name of the binlog file will be sent after the rotate event.
[in]log_posThe start position of the binlog file.
Returns
It returns 0 if succeeds, otherwise 1 is returned.

◆ flush_net()

int Binlog_sender::flush_net ( )
inlineprivate

◆ get_binlog_end_pos()

int Binlog_sender::get_binlog_end_pos ( File_reader reader,
my_off_t end_pos 
)
private

It gets the end position of the binlog file.

Parameters
[in]readerFile_reader of binlog will be checked
[out]end_posWill be set to the end position of the reading binlog file. If this is an inactive file, it will be set to 0.
Return values
0Success
1Error (the thread was killed)

◆ grow_packet()

bool Binlog_sender::grow_packet ( size_t  extra_size)
inlineprivate

This function SHALL grow the buffer of the packet if needed.

If the buffer used for the packet is large enough to accommodate the requested extra bytes, then this function does not do anything.

On the other hand, if the requested size is bigger than the available free bytes in the buffer, the buffer is extended by a constant factor (PACKET_GROW_FACTOR).

Parameters
extra_sizeThe size in bytes that the caller wants to add to the buffer.
Returns
true if an error occurred, false otherwise.

◆ has_error()

bool Binlog_sender::has_error ( )
inlineprivate

◆ has_previous_gtid_log_event()

int Binlog_sender::has_previous_gtid_log_event ( File_reader reader,
bool found 
)
private

It checks if a binlog file has Previous_gtid_log_event.

Parameters
[in]readerFile_reader of binlog will be checked
[out]foundFound Previous_gtid_log_event or not
Returns
It returns 0 if succeeds, otherwise 1 is returned.

◆ init()

void Binlog_sender::init ( )
private

◆ init_checksum_alg()

void Binlog_sender::init_checksum_alg ( )
private

◆ init_heartbeat_period()

void Binlog_sender::init_heartbeat_period ( )
private

◆ is_fatal_error()

bool Binlog_sender::is_fatal_error ( )
inlineprivate

◆ log_read_error_msg()

const char * Binlog_sender::log_read_error_msg ( Binlog_read_error::Error_type  error)
private

Transform read error numbers to error messages.

◆ read_event()

int Binlog_sender::read_event ( File_reader reader,
uchar **  event_ptr,
uint32 event_len 
)
inlineprivate

It reads an event from binlog file.

this function can set event_ptr either a valid buffer pointer or nullptr. nullptr means it arrives at the end of the binlog file if no error happens.

Parameters
[in]readerFile_reader of the binlog file.
[out]event_ptrThe buffer used to store the event.
[out]event_lenLength of the event.
Return values
0Succeed
1Fail

◆ reset_transmit_packet()

int Binlog_sender::reset_transmit_packet ( ushort  flags,
size_t  event_len = 0 
)
inlineprivate

◆ run()

void Binlog_sender::run ( )

It checks the dump reqest and sends events to the client until it finish all events(for mysqlbinlog) or encounters an error.

◆ send_binlog()

int Binlog_sender::send_binlog ( File_reader reader,
my_off_t  start_pos 
)
private

It dumps a binlog file.

Events are read and sent one by one. If it need to wait for new events, it will wait after already reading all events in the active log file.

Parameters
[in]readerFile_reader of binlog will be sent
[in]start_posPosition requested by the slave's IO thread. Only the events after the position are sent.
Returns
It returns 0 if succeeds, otherwise 1 is returned.

◆ send_events()

int Binlog_sender::send_events ( File_reader reader,
my_off_t  end_pos 
)
private

It sends some events in a binlog file to the client.

Parameters
[in]readerFile_reader of binlog will be sent
[in]end_posOnly the events before end_pos are sent
Returns
It returns 0 if succeeds, otherwise 1 is returned.

◆ send_format_description_event()

int Binlog_sender::send_format_description_event ( File_reader reader,
my_off_t  start_pos 
)
private

When starting to dump a binlog file, Format_description_log_event is read and sent first.

If the requested position is after Format_description_log_event, log_pos field in the first Format_description_log_event has to be set to 0. So the slave will not increment its master's binlog position.

Parameters
[in]readerFile_reader of the binlog will be dumpped
[in]start_posPosition requested by the slave's IO thread. Only the events after the position are sent.
Returns
It returns 0 if succeeds, otherwise 1 is returned.

◆ send_heartbeat_event()

int Binlog_sender::send_heartbeat_event ( my_off_t  log_pos)
private

It sends a heartbeat to the client.

Parameters
[in]log_posThe log position that events before it are sent.
Returns
It returns 0 if succeeds, otherwise 1 is returned.

◆ send_packet()

int Binlog_sender::send_packet ( )
inlineprivate

◆ send_packet_and_flush()

int Binlog_sender::send_packet_and_flush ( )
inlineprivate

◆ set_error()

void Binlog_sender::set_error ( int  errorno,
const char *  errmsg 
)
inlineprivate

◆ set_fatal_error()

void Binlog_sender::set_fatal_error ( const char *  errmsg)
inlineprivate

◆ set_last_file()

void Binlog_sender::set_last_file ( const char *  log_file)
inlineprivate

◆ set_last_pos()

void Binlog_sender::set_last_pos ( my_off_t  log_pos)
inlineprivate

◆ set_unknown_error()

void Binlog_sender::set_unknown_error ( const char *  errmsg)
inlineprivate

◆ shrink_packet()

bool Binlog_sender::shrink_packet ( )
inlineprivate

This function SHALL shrink the size of the buffer used.

If less than half of the buffer was used in the last N (PACKET_SHRINK_COUNTER_THRESHOLD) consecutive times this function was called, then the buffer gets shrunk by a constant factor (PACKET_SHRINK_FACTOR).

The buffer is never shrunk less than a minimum size (PACKET_MIN_SIZE).

◆ skip_event()

bool Binlog_sender::skip_event ( const uchar event_ptr,
bool  in_exclude_group 
)
inlineprivate

It checks if the event is in m_exclude_gtid.

Clients may request to exclude some GTIDs. The events include in the GTID groups will be skipped. We call below events sequence as a goup, Gtid_log_event BEGIN ... COMMIT or ROLLBACK

or Gtid_log_event DDL statement

Parameters
[in]event_ptrBuffer of the event
[in]in_exclude_groupIf it is in a execude group
Returns
It returns true if it should be skipped, otherwise false is turned.

◆ wait_new_events()

int Binlog_sender::wait_new_events ( my_off_t  log_pos)
inlineprivate

It waits until receiving an update_cond signal.

It will send heartbeat periodically if m_heartbeat_period is set.

Parameters
[in]log_posThe end position of the last event it already sent. It is required by heartbeat events.
Returns
It returns 0 if succeeds, otherwise 1 is returned.

◆ wait_with_heartbeat()

int Binlog_sender::wait_with_heartbeat ( my_off_t  log_pos)
inlineprivate

◆ wait_without_heartbeat()

int Binlog_sender::wait_without_heartbeat ( )
inlineprivate

Member Data Documentation

◆ m_check_previous_gtid_event

bool Binlog_sender::m_check_previous_gtid_event
private

◆ m_diag_area

Diagnostics_area Binlog_sender::m_diag_area
private

◆ m_errmsg

const char* Binlog_sender::m_errmsg
private

◆ m_errmsg_buf

char Binlog_sender::m_errmsg_buf[MYSQL_ERRMSG_SIZE]
private

◆ m_errno

int Binlog_sender::m_errno
private

◆ m_event_checksum_alg

binary_log::enum_binlog_checksum_alg Binlog_sender::m_event_checksum_alg
private

◆ m_event_count

int Binlog_sender::m_event_count
private

◆ m_exclude_gtid

Gtid_set* Binlog_sender::m_exclude_gtid
private

◆ m_flag

uint32 Binlog_sender::m_flag
private

◆ m_gtid_clear_fd_created_flag

bool Binlog_sender::m_gtid_clear_fd_created_flag
private

◆ m_half_buffer_size_req_counter

ushort Binlog_sender::m_half_buffer_size_req_counter
private

◆ m_heartbeat_period

std::chrono::nanoseconds Binlog_sender::m_heartbeat_period
private

◆ m_last_event_sent_ts

std::chrono::nanoseconds Binlog_sender::m_last_event_sent_ts
private

◆ m_last_file

const char* Binlog_sender::m_last_file
private

◆ m_last_file_buf

char Binlog_sender::m_last_file_buf[FN_REFLEN]
private

◆ m_last_pos

my_off_t Binlog_sender::m_last_pos
private

◆ m_linfo

LOG_INFO Binlog_sender::m_linfo
private

◆ m_new_shrink_size

size_t Binlog_sender::m_new_shrink_size
private

◆ m_observe_transmission

bool Binlog_sender::m_observe_transmission
private

◆ m_packet

String& Binlog_sender::m_packet
private

◆ m_slave_checksum_alg

binary_log::enum_binlog_checksum_alg Binlog_sender::m_slave_checksum_alg
private

◆ m_start_file

const char* Binlog_sender::m_start_file
private

◆ m_start_pos

my_off_t Binlog_sender::m_start_pos
private

◆ m_thd

THD* Binlog_sender::m_thd
private

◆ m_transmit_started

bool Binlog_sender::m_transmit_started
private

◆ m_using_gtid_protocol

bool Binlog_sender::m_using_gtid_protocol
private

◆ m_wait_new_events

bool Binlog_sender::m_wait_new_events
private

◆ PACKET_GROW_FACTOR

const float Binlog_sender::PACKET_GROW_FACTOR = 2.0
staticprivate

How much to grow the buffer each time we need to accommodate more bytes than it currently can hold.

◆ PACKET_MAX_SIZE

const uint32 Binlog_sender::PACKET_MAX_SIZE = UINT_MAX32
staticprivate

◆ PACKET_MIN_SIZE

const uint32 Binlog_sender::PACKET_MIN_SIZE = 4096
staticprivate

The minimum size of the buffer.

◆ PACKET_SHRINK_COUNTER_THRESHOLD

const ushort Binlog_sender::PACKET_SHRINK_COUNTER_THRESHOLD = 100
staticprivate

◆ PACKET_SHRINK_FACTOR

const float Binlog_sender::PACKET_SHRINK_FACTOR = 0.5
staticprivate

The dual of PACKET_GROW_FACTOR.

How much to shrink the buffer each time it is deemed to being underused.


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