Slave IO process 2: Register slave request and dump request, ioslave
The slave IO process has been introduced in http://www.cnblogs.com/onlyac/p/5815566.html
This time we will explore the message formats and main processes for registering slave requests and dump requests.
1. register an slave request
After slave IO connects to the database, slave IO registers itself in the master database so that it does not need to provide slave IO login information, such as the user name and password.
1. Message format for registering slave requests
1 1 [15] COM_REGISTER_SLAVE 2 4 server-id 3 1 slaves hostname length 4 string [$ len] slaves hostname 5 1 slaves user len 6 string [$ len] slaves user 7 1 slaves password len 8 string [$ len] slaves password 9 2 slaves mysql-port10 4 replication rank11 4 master-idView Code
(1) Message Type COM_REGISTER_SLAVE
(2) id of the slave server, which is unique and can only be changed through the my. cnf configuration file
(3) slave Host Name Length
(4) slave Host Name
(5) Length of the slave username to log on to the master database
(6) slave login username in the master database
(7) Length of the slave password to log on to the master database
(8) slave login password in the master database
(9) slave mysql Port
(10) (11) both are 0, so you don't need to pay attention to them.
2. In register_slave_on_master
1 int4store (pos, server_id); pos + = 4; 2 pos = net_store_data (pos, (uchar *) report_host, report_host_len); 3 pos = net_store_data (pos, (uchar *) report_user, report_user_len); 4 pos = net_store_data (pos, (uchar *) report_password, report_password_len); 5 int2store (pos, (uint16) report_port); pos + = 2; 6/* 7 Fake rpl_recovery_rank, which was removed in BUG #13963, 8 so that this server can register itself on old servers, 9 see BUG #49259.10 */11 int4store (pos, /* rpl_recovery_rank */0); pos + = 4; 12/* The master will fill in master_id */13 int4store (pos, 0); pos + = 4;View Code
This is the first non-existing packet format in 1, which is then sent through simple_command.
1 # define simple_command (mysql, command, arg, length, skip_check) \ 2 (mysql)-> methods \ 3? (* (Mysql)-> methods-> advanced_command) (mysql, command, 0, \ 4 0, arg, length, skip_check, NULL) \ 5: (set_mysql_error (mysql, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate), 1 ))View Code
This function points to cli_advanced_command.
(1) In li_advanced_command
Write the message using the net_write_command function.
1 if (net_write_command (net, (uchar) command, header, header_length, 2 arg, arg_length) 3 {4 DBUG_PRINT ("error ", ("Can't send command to server. error: % d ", 5 socket_errno); 6 if (net-> last_errno = ER_NET_PACKET_TOO_LARGE) 7 {8 set_mysql_error (mysql, unknown, unknown_sqlstate); 9 goto end; 10} 11 end_server (mysql); 12 if (mysql_reconnect (mysql) | stmt_skip) 13 goto end; 14 15 MYSQL_TRACE (SEND_COMMAND, mysql, (command, header_length, arg_length, header, arg); 16 if (net_write_command (net, (uchar) command, header, header_length, 17 arg, arg_length) 18 {19 set_mysql_error (mysql, CR_SERVER_GONE_ERROR, unknown_sqlstate ); 20 goto end; 21} 22}View Code
(2) In net_write_command
1 buff [4] = command;/* For first packet */View Code
The type of the message,
1 if (length> = MAX_PACKET_LENGTH) 2 {3/* Take into account that we have the command in the first header */4 len = MAX_PACKET_LENGTH-1-head_len; 5 do 6 {7 int3store (buff, MAX_PACKET_LENGTH); 8 buff [3] = (uchar) net-> pkt_nr ++; 9 if (net_write_buff (net, buff, header_size) | 10 net_write_buff (net, header, head_len) | 11 net_write_buff (net, packet, len) 12 {13 MYSQL_NET_WRITE_DONE (1); 14 DBUG_RETURN (1 ); 15} 16 packet + = len; 17 length-= MAX_PACKET_LENGTH; 18 len = MAX_PACKET_LENGTH; 19 head_len = 0; 20 header_size = NET_HEADER_SIZE; 21} while (length> = MAX_PACKET_LENGTH ); 22 len = length;/* Data left to be written */23} 24 int3store (buff, static_cast <uint> (length); 25 buff [3] = (uchar) net-> pkt_nr ++; 26 rc = MY_TEST (net_write_buff (net, buff, header_size) | 27 (head_len & net_write_buff (net, header, head_len )) | 28 net_write_buff (net, packet, len) | net_flush (net ));View Code
This is because each packet has such a header.
1 if (! Skip_check) 2 {3 result = (mysql-> packet_length = cli_safe_read_with_ OK (mysql, 1, NULL) = 4 packet_error? 1: 0 );View Code
(3) mysql Protocol public packet header
Each article has a header like this, which is missing from the previous chapter.
View Code
The first is the length (in bytes) of the message, and the second is the serial number of the message. Of course, the sent content is originally a packet, but it is too long to be divided into multiple, the third is the packet itself.
Messages larger than 16 MB are sent in this way.
View Code
Ii. dump Request
1. dump Request Message format
Dump has two formats: COM_BINLOG_DUMP_GTID and COM_BINLOG_DUMP.
In the case of slave IO, COM_BINLOG_DUMP is usually used.
Therefore, only the format of COM_BINLOG_DUMP is described here.
1 [12] COM_BINLOG_DUMP2 4 binlog-pos3 2 flags4 4 server-id5 string [EOF] binlog-filenameView Code
(1) Message Type COM_BINLOG_DUMP
(2) location of the binlog Write Request
(3) It is generally of the COM_BINLOG_DUMP_NO_BLOCK type.
(4) The server ID of the server where slave IO is located, which is consistent with the one in the slave registration protocol.
(5) binlog file name to be read
2. In the request_dump Function
1 int4store (ptr_buffer, DBUG_EVALUATE_IF ("request_master_log_pos_3", 3, 2 static_cast <uint32> (mi-> get_master_log_pos (); 3 ptr_buffer + =: accept; 4 // See comment regarding binlog_flags above. 5 int2store (ptr_buffer, binlog_flags); 6 ptr_buffer + =: Unknown; 7 int4store (ptr_buffer, server_id); 8 ptr_buffer + =: Unknown; 9 memcpy (ptr_buffer, mi-> get_master_log_name (), bytes); 10 ptr_buffer + = BINLOG_NAME_INFO_SIZE; 11 12 command_size = ptr_buffer-command_buffer; 13 DBUG_ASSERT (command_size = (allocation_size-1 ));View Code
It is also in the COM_BINLOG_DUMP format.
Simple_command (mysql, command, command_buffer, command_size, 1) is also used for writing.