MariaDB 10.3.8 半同步复制(semi-synchronous Replication)

--半同步复制:
自MariaDB10.3.3版本开始经过阿里巴巴贡献的代码 replication plugin被合并到server里不需要手动安装插件了。
但是MySQL和Percona和MariaDB 10.3.3早前的版本则需要手动安装插件。
--手动安装:
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
--在线开启半同步复制:(10.3版本中默认是关闭同步复制的)
--Master库:
MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_enabled=on;
Query OK, 0 rows affected (0.011 sec)
--slave库:
MariaDB [(none)]> set global rpl_semi_sync_slave_enabled=on;
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> STOP SLAVE IO_THREAD;
START SLAVE IO_THREAD;Query OK, 0 rows affected (1.896 sec)

MariaDB [(none)]> START SLAVE IO_THREAD;
Query OK, 0 rows affected (0.011 sec)
--可以在从库的错误日志中查看到有传统的异步复制转换为半同步复制:
2018-07-19 14:52:10 20 [Note] Slave I/O thread exiting, read up to log 'node4_bin.000002', position 777
2018-07-19 14:52:13 54 [Note] Slave I/O thread: Start semi-sync replication to master '[email protected]:4000' in log 'node4_bin.000002' at position 777
2018-07-19 14:52:13 54 [Note] Slave I/O thread: connected to master '[email protected]:4000',replication started in log 'node4_bin.000002' at position 777

注释:若从库在运行不重启IO thread则还是传统的异步复制。
--相关参数:
MariaDB [(none)]> show variables like 'rpl%';
+---------------------------------------+--------------+
| Variable_name                         | Value        |
+---------------------------------------+--------------+
| rpl_semi_sync_master_enabled          | OFF          |
| rpl_semi_sync_master_timeout          | 10000        |
| rpl_semi_sync_master_trace_level      | 32           |
| rpl_semi_sync_master_wait_no_slave    | ON           |
| rpl_semi_sync_master_wait_point       | AFTER_COMMIT |
| rpl_semi_sync_slave_delay_master      | OFF          |
| rpl_semi_sync_slave_enabled           | OFF          |
| rpl_semi_sync_slave_kill_conn_timeout | 5            |
| rpl_semi_sync_slave_trace_level       | 32           |
+---------------------------------------+--------------+
9 rows in set (0.340 sec)
在MariaDB10.3.3版本新加了参数rpl_semi_sync_slave_delay_master和rpl_semi_sync_slave_kill_conn_timeout。
+---------------------------------------+--------------+-------------------------------------+------------------+
| Variable_name                         | Default Value|Data Type  | Range                   | Desc
+---------------------------------------+--------------+-----------|-------------------------+------------------+
| rpl_semi_sync_master_enabled          | OFF          |boolean    |on or off                |Set to ON to enable semi-synchronous replication master.
| rpl_semi_sync_master_timeout          | 10000(10s)   |numeric    |0 to 18446744073709551615|The timeout value, in milliseconds, for semi-synchronous replication in the master. If this timeout is exceeded in waiting on a commit for acknowledgement from a slave, the master will revert to asynchronous replication, setting the Rpl_semi_sync_master_status status variable to OFF as it does so.
| rpl_semi_sync_master_trace_level      | 32           |numeric    |0 to 18446744073709551615|The tracing level for semi-sync replication.
| rpl_semi_sync_master_wait_no_slave    | ON           |boolean    |on or off                |If set to ON, the default, the slave count (recorded by Rpl_semi_sync_master_clients) may drop to zero, and the master will still wait for the timeout period. If set to OFF, the master will revert to asynchronous replication as soon as the slave count drops to zero.
| rpl_semi_sync_master_wait_point       | AFTER_COMMIT |enum       |AFTER_SYNC, AFTER_COMMIT | 
| rpl_semi_sync_slave_enabled           | OFF          |boolean    |on or off                |Set to ON to enable semi-synchronous replication slave. Disabled by default.
| rpl_semi_sync_slave_kill_conn_timeout | 5            |numeric    |0 to 4294967295          |Timeout for the mysql connection used to kill the slave io_thread's connection on master. This timeout comes into play when stop slave is executed.
| rpl_semi_sync_slave_trace_level       | 32           |numeric    |0 to 18446744073709551615|the tracing level for semi-sync replication. The levels are the same as for rpl_semi_sync_master_trace_level.
+---------------------------------------+--------------+-----------+-------------------------+------------------+

解释:
rpl_semi_sync_master_trace_level
半同步复制的跟踪级别共4个级别:
1: General level, including for example time function failures.
16: More detailed level, with more verbose information.
32: Net wait level, including more information about network waits.
64: Function level, including information about function entries and exits.
rpl_semi_sync_master_wait_point:
默认是after_sync模式即事务在同步到binlog后等待半同步复制的确认信息。
after_commit 则需要提交到存储引擎。
after_sync模式(新):
整个流程: master write binlog -> slave sync binlog -> salve ack -> master commit -> master return result。这样就保证 master commit 的事务已经同步到 slave 了,防止数据丢失
1.保证所有客户端查询到的结果都是相同的;
2.保证 slave 不会丢数据;
3.master 宕机虽然不会导致数据丢失,但有一种情况可能会出现,那就是 salve 的数据比 master 多。

所有客户端在master上同时看到相同的数据;在slave确认已经提交到master的存储引擎。
若master崩溃(crash)所有的事务都提交到master,此时所有的slave都已经复制到slave库,所以failover是无损的。
after_commit模式(旧):
整个流程: master write binlog -> slave sync binlog -> master commit -> salve ack -> master return result。
可能出现的问题:
1.会出现脏读的情况,也就是在事务提交之后,slave 确认之前,客户端A还没有获得结果返回,但是客户端B能够读取A提交的结果;
2.数据可能丢失。
 仅当server提交到存储引擎并且接收到slave的确认信息才由事务接受返回状态。其他的clients可能看到的是提交前的事务。
 若master奔溃,客户端则可能比master库少的数据。

--查看半同步复制的状态:

--Master库:
MariaDB [(none)]> show status like 'rpl%';                           
+--------------------------------------------+-------------+
| Variable_name                              | Value       |
+--------------------------------------------+-------------+
| Rpl_semi_sync_master_clients               | 1           |
| Rpl_semi_sync_master_get_ack               | 1           |
| Rpl_semi_sync_master_net_avg_wait_time     | 0           |
| Rpl_semi_sync_master_net_wait_time         | 0           |
| Rpl_semi_sync_master_net_waits             | 1           |
| Rpl_semi_sync_master_no_times              | 0           |
| Rpl_semi_sync_master_no_tx                 | 0           |
| Rpl_semi_sync_master_request_ack           | 1           |
| Rpl_semi_sync_master_status                | ON          |
| Rpl_semi_sync_master_timefunc_failures     | 0           |
| Rpl_semi_sync_master_tx_avg_wait_time      | 501         |
| Rpl_semi_sync_master_tx_wait_time          | 501         |
| Rpl_semi_sync_master_tx_waits              | 1           |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0           |
| Rpl_semi_sync_master_wait_sessions         | 0           |
| Rpl_semi_sync_master_yes_tx                | 1           |
| Rpl_semi_sync_slave_send_ack               | 0           |
| Rpl_semi_sync_slave_status                 | OFF         |
| Rpl_status                                 | AUTH_MASTER |
| Rpl_transactions_multi_engine              | 0           |
+--------------------------------------------+-------------+
20 rows in set (0.015 sec)

--Slave库:
MariaDB [(none)]> show  status like 'rpl%';
+--------------------------------------------+-------------+
| Variable_name                              | Value       |
+--------------------------------------------+-------------+
| Rpl_semi_sync_master_clients               | 0           |
| Rpl_semi_sync_master_get_ack               | 0           |
| Rpl_semi_sync_master_net_avg_wait_time     | 0           |
| Rpl_semi_sync_master_net_wait_time         | 0           |
| Rpl_semi_sync_master_net_waits             | 0           |
| Rpl_semi_sync_master_no_times              | 0           |
| Rpl_semi_sync_master_no_tx                 | 0           |
| Rpl_semi_sync_master_request_ack           | 0           |
| Rpl_semi_sync_master_status                | OFF         |
| Rpl_semi_sync_master_timefunc_failures     | 0           |
| Rpl_semi_sync_master_tx_avg_wait_time      | 0           |
| Rpl_semi_sync_master_tx_wait_time          | 0           |
| Rpl_semi_sync_master_tx_waits              | 0           |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0           |
| Rpl_semi_sync_master_wait_sessions         | 0           |
| Rpl_semi_sync_master_yes_tx                | 0           |
| Rpl_semi_sync_slave_send_ack               | 1           |
| Rpl_semi_sync_slave_status                 | ON          |
| Rpl_status                                 | AUTH_MASTER |
| Rpl_transactions_multi_engine              | 0           |
+--------------------------------------------+-------------+
20 rows in set (0.001 sec)
重点关注状态中的几个参数:
Rpl_semi_sync_master_clients 半同步复制中有几个客户端。
Rpl_semi_sync_master_get_ack 收到的ack确认消息。
Rpl_semi_sync_slave_status 半同步复制中的状态,在master库是off,若是半同步的slave库则为on。而Rpl_semi_sync_master_status刚好相反。

可以看到半同步复制在如下情况下回退化到异步复制:

1.slave库中设置了半同步但是没有重启IO thread。

2.在rpl_semi_sync_master_timeout 设置的时间超时会暂时关闭半同步复制退化到异步复制,当master dump线程发送完一个事务的所有事件之后,如果在rpl_semi_sync_master_timeout内,收到了从库的响应,则主从又重新恢复为半同步复制。

在MySQL 5.7版本中rpl_semi_sync_master_wait_point参数的值默认为after_sync。

猜你喜欢

转载自blog.csdn.net/vkingnew/article/details/81118373
今日推荐