当前位置:网站首页>MySQL semi sync replication

MySQL semi sync replication

2022-06-25 00:12:00 Greatsql community

  • GreatSQL The original content of the community shall not be used without authorization , For reprint, please contact the editor and indicate the source .

Introduction to semi synchronization

  • MASTER The node does not immediately return the result to the client after executing the transaction submitted by the client , But waiting for at least one SLAVE The node receives and writes relay log Is returned to the client .
  • Semi synchronous versus asynchronous replication , Improved data security , At the same time, it also causes a certain degree of delay , The delay is at least one TCP Round-trip time . therefore , Semi synchronous replication is best used in low latency networks .
  • MySQL from 5.5 Support semi synchronous replication from the beginning , stay 5.7.2 An improvement to semi synchronous replication was made in version ; The original semi synchronization strategy is AFTER_COMMIT The improved strategy is AFTER_SYNC The difference between the two is SLAVE node ACK The reply MASTER The timing is different .

Two modes are introduced

  • AFTER_COMMIT Model is introduced
MASTER Write each transaction to the binary log and save it on the disk , Send the transaction to SLAVE, Then submit the transaction to the storage engine for processing and submission , And then wait SLAVE Return confirmation , After receiving the confirmation message ,MASTER Returns the result to the client , Then the current client can continue to work .
  • AFTER_SYNC Model is introduced
MASTER Write each transaction to the binary log and save it on the disk , Send the transaction to SLAVE, And then wait SLAVE Return confirmation , After receiving the confirmation message , Submit the transaction to the storage engine for processing and submission , And return the result to the client , Then the current client can continue to work .

The two ways are compared

  • For the first AFTER_COMMIT The way , At present, the client only submits data to the storage engine and receives SLAVE After the confirmation of the return , Will receive the return result of the transaction . Received after transaction commit SLAVE Before returning the confirmation message , At this moment, other clients can see the transaction information submitted by the current client .

    If SLAVE The node did not receive due to network and other reasons MASTER Transactions passed by nodes , and MASTER Node now crash 了 .HA failover , Clients are connected to SLAVE Node , At this time, I was in MASTER The transactions seen by the node are SLAVE The node does not see , There will be a loss of transactions .
  • For the second AFTER_SYNC The way , When the transaction is SLAVE After the confirmation MASTER After committing the transaction at the storage engine level , Only all clients can see the data changes caused by the transaction . therefore , All clients in MASTER See the same data at the same time .

    When MASTER node crash Under the circumstances , All in MASTER All transactions committed on are copied to SLAVE( Save to relay log ). MASTER Server unexpected crash. At the moment HA Fail over to SALVE after , The data seen by the client is lossless , because SLAVE Is the latest .
    Be careful , However , under these circumstances ,MASTER Cannot be directly restored to use , Because its binary log may contain uncommitted transactions , At this point, when binary logs are recovered and used for business needs , May result in contact with SLAVE The conflict of .

How to turn on semi synchronization

  • The way 1: Semi synchronization exists in the form of plug-ins , We can open it online directly ( This time use this method )
#  The master node is turned on 
[[email protected]][(none)]>INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.02 sec)

#  Open from node 
[[email protected]][(none)]>INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.02 sec)

# PS: Generally, all nodes are deployed synchronously master and slave plug-in unit , In this way, it will be more convenient to handle the fault switching 
  • The way 2: stay my.cnf Start in configuration
#  Both the master and slave nodes are configured synchronously 
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_sync_slave=semisync_slave.so"
rpl_semi_sync_master_enabled=1
rpl_semi_sync_slave_enabled=1

Check the opening of the plug-in

  • The way 1: Inquire about plugin
#  Master node view 
[[email protected]][test]>show plugins;
| rpl_semi_sync_master | ACTIVE | REPLICATION | semisync_master.so | GPL |

#  View from the node 
[[email protected]][(none)]>show plugins;
| rpl_semi_sync_slave | ACTIVE | REPLICATION | semisync_slave.so | GPL |
  • Mode two : Inquire about information_schema.plugins More comprehensive information
#  Master node information 
(Thu Feb 17 03:03:12 2022)[[email protected]][(none)]>select * from information_schema.plugins where plugin_name like "%semi%"\G;
*************************** 1. row ***************************
           PLUGIN_NAME: rpl_semi_sync_master
        PLUGIN_VERSION: 1.0
         PLUGIN_STATUS: ACTIVE
           PLUGIN_TYPE: REPLICATION
   PLUGIN_TYPE_VERSION: 4.0
        PLUGIN_LIBRARY: semisync_master.so
PLUGIN_LIBRARY_VERSION: 1.10
         PLUGIN_AUTHOR: Oracle Corporation
    PLUGIN_DESCRIPTION: Semi-synchronous replication master
        PLUGIN_LICENSE: GPL
           LOAD_OPTION: ON
1 row in set (0.00 sec)

ERROR:
No query specified

#  From node information 
(Thu Feb 17 16:05:19 2022)[[email protected]][(none)]>select * from information_schema.plugins where plugin_name like "%semi%"\G;
*************************** 1. row ***************************
           PLUGIN_NAME: rpl_semi_sync_slave
        PLUGIN_VERSION: 1.0
         PLUGIN_STATUS: ACTIVE
           PLUGIN_TYPE: REPLICATION
   PLUGIN_TYPE_VERSION: 4.0
        PLUGIN_LIBRARY: semisync_slave.so
PLUGIN_LIBRARY_VERSION: 1.10
         PLUGIN_AUTHOR: Oracle Corporation
    PLUGIN_DESCRIPTION: Semi-synchronous replication slave
        PLUGIN_LICENSE: GPL
           LOAD_OPTION: ON
1 row in set (0.00 sec

Turn on the semi synchronous function

  • Because the plug-ins are installed online , So after the plug-in installation is completed , The service needs to be started
#  The master node enables semi synchronous replication 
[[email protected]][test]>SET GLOBAL rpl_semi_sync_master_enabled = on;
Query OK, 0 rows affected (0.00 sec)

#  Enable semi synchronous replication from node 
[[email protected]][(none)]>SET GLOBAL rpl_semi_sync_slave_enabled = on;
Query OK, 0 rows affected (0.00 sec)
  • After the above settings are completed , Restart from node IO Threads
(Mon Feb 14 15:19:58 2022)[[email protected]][(none)]>
(Mon Feb 14 15:19:58 2022)[[email protected]][(none)]>STOP SLAVE IO_THREAD;
Query OK, 0 rows affected, 1 warning (0.01 sec)

(Mon Feb 14 15:21:41 2022)[[email protected]][(none)]>START SLAVE IO_THREAD;
Query OK, 0 rows affected, 1 warning (0.01 sec)

See if semi synchronization is running

#  Master node 
[[email protected]][test]>show status like 'Rpl_semi_sync_master_status';
+-----------------------------+-------+
| Variable_name               | Value |
+-----------------------------+-------+
| Rpl_semi_sync_master_status | ON    |
+-----------------------------+-------+
1 row in set (0.00 sec)

#  From the node 
[[email protected]][(none)]>show status like 'Rpl_semi_sync_slave_status';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON    |
+----------------------------+-------+
1 row in set (0.01 sec)
  • Look at the master node error.log, It can be seen that the slave node has enabled semi synchronous replication
#  The key information  Start semi-sync binlog_dump to slave (server_id: 3306)
2022-02-14T02:16:35.411061-05:00 13 [Note] [MY-010014] [Repl] While initializing dump thread for slave with UUID <652ade08-8b1c-11ec-9f62-00155dcff90a>, found a zombie dump thread with the same UUID. Master is killing the zombie dump thread(12).
2022-02-14T02:16:35.411236-05:00 13 [Note] [MY-010462] [Repl] Start binlog_dump to master_thread_id(13) slave_server(3306), pos(, 4)
2022-02-14T02:16:35.411263-05:00 13 [Note] [MY-011170] [Repl] Start asynchronous binlog_dump to slave (server_id: 3306), pos(, 4).
2022-02-14T02:16:35.411419-05:00 12 [Note] [MY-011171] [Repl] Stop asynchronous binlog_dump to slave (server_id: 3306).
2022-02-14T02:19:33.913084-05:00 9 [Note] [MY-011130] [Repl] Semi-sync replication initialized for transactions.
2022-02-14T02:19:33.913133-05:00 9 [Note] [MY-011142] [Repl] Semi-sync replication enabled on the master.
2022-02-14T02:19:33.913638-05:00 0 [Note] [MY-011166] [Repl] Starting ack receiver thread.
2022-02-14T02:21:46.899725-05:00 14 [Note] [MY-010014] [Repl] While initializing dump thread for slave with UUID <652ade08-8b1c-11ec-9f62-00155dcff90a>, found a zombie dump thread with the same UUID. Master is killing the zombie dump thread(13).
2022-02-14T02:21:46.899894-05:00 14 [Note] [MY-010462] [Repl] Start binlog_dump to master_thread_id(14) slave_server(3306), pos(, 4)
2022-02-14T02:21:46.899953-05:00 14 [Note] [MY-011170] [Repl] Start semi-sync binlog_dump to slave (server_id: 3306), pos(, 4).

above ,MySQL Semi synchronous replication is completed !

Semi synchronous parameter information

#  Master node parameter information 
[[email protected]][test]>show variables like '%Rpl%';
+-------------------------------------------+------------+
| Variable_name                             | Value      |
+-------------------------------------------+------------+
| rpl_read_size                             | 8192       |
| rpl_semi_sync_master_enabled              | ON         |
| rpl_semi_sync_master_timeout              | 10000      |
| rpl_semi_sync_master_trace_level          | 32         |
| rpl_semi_sync_master_wait_for_slave_count | 1          |
| rpl_semi_sync_master_wait_no_slave        | ON         |
| rpl_semi_sync_master_wait_point           | AFTER_SYNC |
| rpl_stop_slave_timeout                    | 31536000   |
+-------------------------------------------+------------+
8 rows in set (0.00 sec)
  • Brief description of some parameters
Parameter name purpose
rpl_semi_sync_master_enabled Whether to enable semi synchronous replication ,ON For opening ,OFF To close
rpl_semi_sync_master_timeoutMaster wait for Slave Of ack Timeout for reply , The default is 10 second ( The basic unit is milliseconds )
rpl_semi_sync_master_trace_level Debug level
rpl_semi_sync_master_wait_for_slave_countMaster Required after submission ACK Number of replies . If Slave The quantity is greater than or equal to this value , that Master It's normal , If it's below this value ,Master A timeout wait may occur during the transaction commit phase , When the wait exceeds the parameter (rpl_semi_sync_master_timeout) Set time ,Master It goes to asynchronous mode
rpl_semi_sync_master_wait_no_slave Is in the timeout period , If Slave The number of downtime exceeds what should be received ack Number ,Master Whether to demote to asynchronous replication
rpl_semi_sync_master_wait_point What is the way of semi synchronization , The default is AFTER_SYNC ,5.7.2 It used to be AFTER_COMMIT
#  From node parameter information 
[[email protected]][test]>show variables like '%Rpl%';
+---------------------------------+----------+
| Variable_name                   | Value    |
+---------------------------------+----------+
| rpl_read_size                   | 8192     |
| rpl_semi_sync_slave_enabled     | ON       |
| rpl_semi_sync_slave_trace_level | 32       |
| rpl_stop_slave_timeout          | 31536000 |
+---------------------------------+----------+
4 rows in set (0.00 sec)
  • Brief description of some parameters
Parameter name purpose
rpl_semi_sync_master_enabled Whether to enable semi synchronous replication ,ON For opening ,OFF To close
rpl_semi_sync_slave_trace_level Debug level

Semi synchronous status information

  • Master node view
[[email protected]][test]> show status like '%Rpl_semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |
| 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_status                | ON    |
| 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     |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
  • Brief description of the use of some parameters
Parameter name purpose
Rpl_semi_sync_master_clients How many slave libraries are currently set to semi synchronous mode , Does not contain asynchronous replication Slave example
Rpl_semi_sync_master_net_avg_wait_time Waiting to return from the library ACK The average time to acknowledge a message ( Company :ms)
Rpl_semi_sync_master_net_wait_time Wait until you return from the library ACK Total time to acknowledge messages ( Company :ms)
Rpl_semi_sync_master_net_waits Waiting to return from the library ACK Total number of acknowledgement messages
Rpl_semi_sync_master_no_tx The number of times a transaction is committed without a confirmation message returned from the library
Rpl_semi_sync_master_status When semi synchronous replication is on , This variable is used to dynamically display the status of semi synchronous replication . The duty of OFF, Indicates that you are currently under asynchronous replication ; When waiting for a confirmation message to be returned from the library for more than rpl_semi_sync_master_timeout Change the value to OFF. When the value is set to ON, Indicates that you are currently under semi synchronous replication . When the slave database catches up with the master database, it is modified to ON.
Rpl_semi_sync_master_timefunc_failures Call function gettimeofday() The number of failures
Rpl_semi_sync_master_tx_avg_wait_timeMASTER The average waiting time per transaction on the
Rpl_semi_sync_master_tx_wait_timeMASTER Total waiting time of transaction on
Rpl_semi_sync_master_tx_waitsMASTER Number of transactions waiting on
Rpl_semi_sync_master_wait_pos_backtraverseMASTER The binary coordinate of the waiting event is lower than the total number of previous waiting events . When the order in which a transaction starts waiting for a reply is different from the order in which its binary log events are written , That's what happens . This situation is closing binlog_order_commit In this case, there may be .
Rpl_semi_sync_master_wait_sessions The current number of replies waiting to return confirmation messages from the library
Rpl_semi_sync_master_yes_txSlave The number of successfully committed transactions confirmed by the node
#  State transition information from node 
show global status like '%semi%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON    |
+----------------------------+-------+
1 row in set (0.00 sec)
  • Brief description of parameters
Parameter name purpose
Rpl_semi_sync_slave_status Dynamic parameters ,ON Indicates that semi synchronous replication is in use ,OFF Indicates asynchronous replication

Test the synchronization of semi synchronization

  • Whether semi synchronous will be degraded to asynchronous replication ? Yes, it will .
  • When a semi synchronous replication timeout occurs ( from rpl_semi_sync_master_timeout Parameter control , In milliseconds , The default is 10000, namely 10s), Will temporarily turn off semi synchronous replication , Switch to asynchronous replication .
  • When MASTER DUMP After the thread has sent all the events of a transaction , If in rpl_semi_sync_master_timeout Inside , Received a response from the library , Then the master and slave are restored to semi synchronous replication .
# 1. Turn off the slave node temporarily IO Threads 
[[email protected]][(none)]>STOP SLAVE IO_THREAD;
Query OK, 0 rows affected, 1 warning (0.02 sec)

# 2. The master node writes several pieces of test data 
[[email protected]][test]>insert into ptype values(4,'4','4'),(5,'5','5'),(6,'6','6');
Query OK, 3 rows affected (0.02 sec)

# 3. wait for 10s View replication status after , Semi synchronous has been turned off 
[[email protected]][test]>show status like 'Rpl_semi_sync_slave_status';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | OFF   |
+----------------------------+-------+
1 row in set (0.00 sec)

# 4. Open from node IO Threads 
[[email protected]][(none)]>START SLAVE IO_THREAD;
Query OK, 0 rows affected, 1 warning (0.02 sec)

# 5. Check the replication status again , Semi synchronous replication is automatically turned on 
[[email protected]][test]>show status like 'Rpl_semi_sync_slave_status';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON    |
+----------------------------+-------+
1 row in set (0.00 sec)

Enjoy GreatSQL :)

Article recommendation :

GreatSQL Quarterly Report (2021.12.26)
https://mp.weixin.qq.com/s/FZ...

Technology sharing |sysbench Usage analysis of pressure measuring tools
https://mp.weixin.qq.com/s/m1...

Fault analysis | linux disk io High utilization , Analyze the correct posture
https://mp.weixin.qq.com/s/7c...

Technology sharing | Flashback at MySQL Implementation and improvement in
https://mp.weixin.qq.com/s/6j...

Wan Da #20, How to filter data in index push down
https://mp.weixin.qq.com/s/pt...

About GreatSQL

GreatSQL It is maintained by Wanli database MySQL Branch , Focus on Improvement MGR Reliability and performance , Support InnoDB Parallel query feature , It is suitable for financial grade applications MySQL Branch version .

Gitee:
https://gitee.com/GreatSQL/Gr...

GitHub:
https://github.com/GreatSQL/G...

Bilibili:
https://space.bilibili.com/13...

WeChat &QQ Group :
Searchable add GreatSQL Community assistant wechat friend , Send verification information “ Add group ” Join in GreatSQL/MGR Exchange wechat group

QQ Group :533341697
Wechat assistant :wanlidbc

This article by the blog one article many sends the platform OpenWrite Release !
原网站

版权声明
本文为[Greatsql community]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211044460725.html