slave IO流程之二:注册slave请求和dump请求

时间:2023-03-08 16:07:44

slave IO流程已经在http://www.cnblogs.com/onlyac/p/5815566.html中有介绍

这次我们要探索注册slave请求和dump请求的报文格式和主要流程。

一、注册slave请求

在slave IO连接完数据库后,slave IO接着在主库里注册自己,以便后续不需要提供slave IO登陆的信息如用户名密码等。

1.注册slave请求的报文格式

               [] COM_REGISTER_SLAVE
server-id
slaves hostname length
string[$len] slaves hostname
slaves user len
string[$len] slaves user
slaves password len
string[$len] slaves password
slaves mysql-port
replication rank
master-id

(1)报文的类型COM_REGISTER_SLAVE

(2)slave服务器的id,该id唯一并且只能通过my.cnf配置文件改变

(3)slave主机名长度

(4)slave主机名

(5)slave在主库登陆用户名长度

(6)slave在主库登陆用户名

(7)slave在主库登陆的密码长度

(8)slave在主库登陆的密码

(9)slave的mysql端口

(10)(11)这两个都是0,不用去关注

2.在register_slave_on_master中

   int4store(pos, server_id); pos+= ;
pos= net_store_data(pos, (uchar*) report_host, report_host_len);
pos= net_store_data(pos, (uchar*) report_user, report_user_len);
pos= net_store_data(pos, (uchar*) report_password, report_password_len);
int2store(pos, (uint16) report_port); pos+= ;
/*
Fake rpl_recovery_rank, which was removed in BUG#13963,
so that this server can register itself on old servers,
see BUG#49259.
*/
int4store(pos, /* rpl_recovery_rank */ ); pos+= ;
/* The master will fill in master_id */
int4store(pos, ); pos+= ;

这是除了第一个没有的在1中的报文格式,然后通过simple_command发送出去。

 #define simple_command(mysql, command, arg, length, skip_check) \
((mysql)->methods \
? (*(mysql)->methods->advanced_command)(mysql, command, , \
, arg, length, skip_check, NULL) \
: (set_mysql_error(mysql, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate), ))

该函数指向cli_advanced_command。

(1)在li_advanced_command中

在通过函数net_write_command写该报文

   if (net_write_command(net,(uchar) command, header, header_length,
arg, arg_length))
{
DBUG_PRINT("error",("Can't send command to server. Error: %d",
socket_errno));
if (net->last_errno == ER_NET_PACKET_TOO_LARGE)
{
set_mysql_error(mysql, CR_NET_PACKET_TOO_LARGE, unknown_sqlstate);
goto end;
}
end_server(mysql);
if (mysql_reconnect(mysql) || stmt_skip)
goto end; MYSQL_TRACE(SEND_COMMAND, mysql, (command, header_length, arg_length, header, arg));
if (net_write_command(net,(uchar) command, header, header_length,
arg, arg_length))
{
set_mysql_error(mysql, CR_SERVER_GONE_ERROR, unknown_sqlstate);
goto end;
}
}

(2)在net_write_command中

   buff[]=command;                /* For first packet */

这个写了该报文的类型,

   if (length >= MAX_PACKET_LENGTH)
{
/* Take into account that we have the command in the first header */
len= MAX_PACKET_LENGTH - - head_len;
do
{
int3store(buff, MAX_PACKET_LENGTH);
buff[]= (uchar) net->pkt_nr++;
if (net_write_buff(net, buff, header_size) ||
net_write_buff(net, header, head_len) ||
net_write_buff(net, packet, len))
{
MYSQL_NET_WRITE_DONE();
DBUG_RETURN();
}
packet+= len;
length-= MAX_PACKET_LENGTH;
len= MAX_PACKET_LENGTH;
head_len= ;
header_size= NET_HEADER_SIZE;
} while (length >= MAX_PACKET_LENGTH);
len=length; /* Data left to be written */
}
int3store(buff, static_cast<uint>(length));
buff[]= (uchar) net->pkt_nr++;
rc= MY_TEST(net_write_buff(net, buff, header_size) ||
(head_len && net_write_buff(net, header, head_len)) ||
net_write_buff(net, packet, len) || net_flush(net));

这所以这样写是因为每个报文有个这样头

  if (!skip_check)
{
result= ((mysql->packet_length= cli_safe_read_with_ok(mysql, , NULL)) ==
packet_error ? : );

(3)mysql协议的公共报文头部

每个报文都有一个这样的头,这是忘记在上一章讲的


第一个是这个报文的长度(以字节为单位),第二个是这个报文的系列号,当然发送的内容原来就是一个报文,但是太长分成多个,第三个就是报文本身

对于超过16M的报文会这样发送

二、dump请求

1.dump请求的报文格式

dump有两种格式:COM_BINLOG_DUMP_GTID和COM_BINLOG_DUMP

在slave IO的情形下,一般会使用COM_BINLOG_DUMP

为此,在此仅仅介绍COM_BINLOG_DUMP的格式

               [] COM_BINLOG_DUMP
binlog-pos
flags
server-id
string[EOF] binlog-filename

(1)报文的类型COM_BINLOG_DUMP

(2)请求binlog的写的位置

(3)一般为COM_BINLOG_DUMP_NO_BLOCK类型

(4)slave IO的所在服务器的服务器编号,和slave注册协议中的那个一致

(5)当前需要读的binlog文件名

2.在函数request_dump中

     int4store(ptr_buffer, DBUG_EVALUATE_IF("request_master_log_pos_3", ,
static_cast<uint32>(mi->get_master_log_pos())));
ptr_buffer+= ::BINLOG_POS_OLD_INFO_SIZE;
// See comment regarding binlog_flags above.
int2store(ptr_buffer, binlog_flags);
ptr_buffer+= ::BINLOG_FLAGS_INFO_SIZE;
int4store(ptr_buffer, server_id);
ptr_buffer+= ::BINLOG_SERVER_ID_INFO_SIZE;
memcpy(ptr_buffer, mi->get_master_log_name(), BINLOG_NAME_INFO_SIZE);
ptr_buffer+= BINLOG_NAME_INFO_SIZE; command_size= ptr_buffer - command_buffer;
DBUG_ASSERT(command_size == (allocation_size - ));

同样地是COM_BINLOG_DUMP的格式。

这边也使用simple_command(mysql, command, command_buffer, command_size, 1)写入。