当前位置:网站首页>Technology sharing | MySQL: how about copying half a transaction from the database?
Technology sharing | MySQL: how about copying half a transaction from the database?
2022-07-01 12:00:00 【ActionTech】
author : Hu Chengqing
Love can be born DBA Team members , Good at fault analysis 、 performance optimization , Personal blog :https://www.jianshu.com/u/a95ec11f67a8, Welcome to discuss .
In this paper, the source : Original contribution
* Produced by aikesheng open source community , Original content is not allowed to be used without authorization , For reprint, please contact the editor and indicate the source .
Copy exception
In the process of copying , The main database fails or the network is interrupted , Will cause slave io thread interrupt , It is possible that only half a transaction is copied from the Library . For example, the transactions executed by the main database are as follows :
begin;
insert 1;
insert 2;
commit;
Received from library binlog It may only contain part of the transaction , such as :
- situation 1: Contains only begin;
- situation 2: Contains only begin;insert 1;
- situation 3: Contains only begin;insert 1;insert 2;
From library slave sql thread Play back this part binlog after , Will wait for slave io thread Read the remaining... From the main library binlog, And before that sql The thread plays back the half transaction , It's the same as we manually perform half a transaction , Neither commit nor rollback .
How should we deal with this anomaly ?
- When slave io thread recovery , What to do ?
- When slave io thread Can't recover , What to do ?
Experimental process
The test method :
##1. Used in slave Library tc Simulate network latency , Intended to make reading binlog It's slowing down
tc qdisc add dev eth0 root netem delay 3000ms 3000ms 100%
##2. Execute a multi statement transaction in the main database
begin;
update t2 set pad='4' where id < 40;
update t2 set pad='5' where id < 50;
update t2 set pad='6' where id < 60;
commit;
##3. Execute... In the main library commit After success , Use immediately iptables Cut off the network between master and slave
iptables -A OUTPUT -d 172.16.21.4 -j DROP
iptables -A INPUT -s 172.16.21.4 -j DROP
In this way, the phenomenon we can observe from the library is :
- One of them worker The thread state is
Waiting for an event from Coordinator, This status indicates work The thread has finished its work and is waiting Coordinator ( Coordination thread ) Assign a new relay log event , But it also shows that it is executingupdate t2 set pad='5' where id < 50, This is a contradiction 1:

show slave statusOutput in progress ,Retrieved_Gtid_SetAndExecuted_Gtid_Setequal ( signify sql The thread has played back all relay log), But above worker The thread is playing back again SQL , This is a contradiction 2:

Finally, we passed relay log Compaction hammer , You can see this transaction relay log Incomplete , To update t2 set pad='5' where id < 50; This Rows_query event It's over :

When slave io thread Can't recover
If slave io thread Can't recover for a long time , that sql Threads can't wait for the rest binlog, Never commit or rollback , Will always hold the lock of this transaction :

If it is caused by the failure of the main library slave io thread abnormal , It is likely to switch between master and slave , After upgrading from the library to the main ,SQL Transaction locks held by threads may block business requests .
It should be at this time stop slave stop it sql Threads , Let the transaction rollback release the lock . It should be noted that : In this case stop slave Will wait for 60 second ( etc. slave io thread Receive the remaining binlog),60 Seconds before it stops sql Threads :

When slave io thread recovery
slave io thread After an abnormal interrupt ,sql Threads work normally ,sql The thread executes some transactions , And will wait io Thread sends new binlog.slave io thread After thread recovery , If it is based on GTID Copy , From the present GTID The transaction begins to retrieve the complete binlog, The current transaction will be rolled back first from the Library , Then replay the newly received binlog.
边栏推荐
猜你喜欢

Neo4j Chinese developer monthly - issue 202206

消息队列之监控退款任务批处理过程

The Missing Semester

Emotion analysis based on IMDB comment data set

About keil compiler, "file has been changed outside the editor, reload?" Solutions for

redis配置环境变量

自组织是管理者和成员的双向奔赴

Redis的攻击手法

Joint Time-Frequency and Time Domain Learning for Speech Enhancement

Adjacency matrix undirected graph (I) - basic concepts and C language
随机推荐
Acly and metabolic diseases
Is it safe for Huatai Securities to open an account online?
CAD如何设置标注小数位
usb peripheral 驱动 - cable connect/disconnect
指定的服务已标记为删除
Le semester manquant
C summary of knowledge points 1
耐克如何常年霸榜第一名?最新財報答案來了
Unity XLua 协程封装
二叉堆(一) - 原理与C实现
Are the consequences of securities account cancellation safe
ABBIRB120工业机器人机械零点位置
Compile and debug net6 source code
Joint Time-Frequency and Time Domain Learning for Speech Enhancement
I'm in Zhongshan. Where is a better place to open an account? Is it actually safe to open an account online?
Redis common sense
Value/list in redis
Botu V15 add GSD file
Istio、eBPF 和 RSocket Broker:深入研究服务网格
Activity workflow engine