当前位置:网站首页>Two stage submission and three stage submission
Two stage submission and three stage submission
2022-07-26 01:19:00 【Long time no see, happy】
List of articles
Two stage submission and three stage submission
Two stage submission agreement
The establishment of the two-stage submission algorithm is based on the following assumptions :
- In this distributed system , There is a node as The coordinator (Coordinator), Other nodes act as participants (Participants), And the nodes can communicate with each other ;
- All nodes use write ahead logging , After being written, the log is saved on a reliable storage device , Even if the node is damaged, it will not cause the loss of log data ;
- All nodes will not be permanently damaged , Even if it is damaged, it can still be recovered .
Two phases in a two-phase submission , refer to Commit-request Stage and Commit Stage , The two-stage submission process is as follows :

Submit request phase
- Business inquiry
The coordinator sends the transaction content to all participants , Ask whether you can perform the operation of transaction submission , And start waiting for the response of each participant . - Perform transactions
Each participant node performs transaction operations , And will undo and Redo Information is logged in the transaction log . - Each participant feeds back the response of the transaction inquiry to the coordinator
Submission phase
The coordinator will decide whether the transaction submission operation can be carried out finally according to the feedback of each participant , There are two possibilities :
The first possibility : If the coordinator's feedback from all participants is yes Respond to , Then it will carry out Transaction submission .
- Send submit request
The coordinator sends Commit request . - Transaction submission
The participants received Commit After the request , The transaction commit operation will be formally executed , And release the transaction resources occupied during the whole transaction execution after the commit . - Feedback transaction submission results
After the participant completes the transaction commit , Send... To the coordinator ack news . - Complete the business
The coordinator receives feedback from all participants ack After the message , Complete the business .
The second possibility : If any participant gives feedback to the coordinator No Respond to , Or after waiting for a timeout , The coordinator has yet to receive feedback from all participants , Then it will Interrupt the business .
- Send rollback request
The coordinator sends RollBack request . - Transaction rollback
The participants received RollBack After the request , Will use it to record a kind of Undo Information to perform a transaction rollback operation , And release the resources occupied during the whole transaction execution after the rollback . - Feedback transaction rollback result
After the participant completes the transaction rollback , Send... To the coordinator ack news . - Interrupt the business
The coordinator receives feedback from all participants ack After the news , Complete transaction interrupt .
Problems with two phase commit
- Resources are blocked by synchronization
In the process of execution , All participating nodes are transaction exclusive , When participants have public resources , Then the access of third-party nodes to public resources will be blocked .
- The coordinator may have a single point of failure
Once the coordinator breaks down , The participants will keep blocking .
- stay Commit Data inconsistencies occurred in phase
In the second phase , Suppose the coordinator issues the transaction Commit The notice of , However, due to network problems, the notification was only received and executed by some participants Commit, The rest of the participants were not notified , It's stuck , that , During this time, data inconsistency occurs .
Three stage submission agreement
In order to solve the problems of synchronization blocking in two-stage protocol , The three-phase commit protocol introduces a timeout mechanism in both coordinators and participants , And split the first phase of the two-phase commit protocol into two steps : inquiry , Then lock the resources , Finally, the real submission .
Of the three stages Three Phase Respectively CanCommit、PreCommit、DoCommit Stage .
CanCommit Stage
- Business inquiry
The coordinator sends a... With transaction content to all participants canCommit request , Ask if the transaction commit operation can be performed , And start waiting for the response of participants . - The participants responded to the coordinator's inquiry by giving feedback
Participants are receiving canCommit After the request , Under normal circumstances , If he thinks that the affairs can be carried out smoothly , Then feedback yes Respond to , And get ready , Otherwise feedback No Respond to .
PreCommit Stage
The coordinator decides whether to continue the transaction according to the response of the participants PreCommit operation . According to the response , There are two possibilities .
A. If the coordinator's feedback from all participants is Yes Respond to , Then the transaction will be pre executed :
- Send pre submit request , The coordinator sends... To the participants PreCommit request , And enter Prepared Stage ;
- Transaction pre commit , Participant received PreCommit After the request , Will perform transaction operations ;
- Respond to feedback , If the participant successfully performs the transaction operation , Then return to ACK Respond to , And start waiting for the final order .
B. If any of the participants sent No Respond to , Or wait for the timeout , None of the coordinators received a response from the participants , Then interrupt the transaction :
- Send interrupt request , The coordinator sends... To all participants abort request ;
- Interrupt the business , The participants received... From the coordinator abort After the request , The interruption of the execution of a transaction .
DoCommit Stage
In this phase, the real transaction commit , It can also be divided into the following two situations .
A. Execute commit
- Send submit request . The coordinator received... From the participant ACK After response , Then it will go from pre submission to submission , And send it to all participants doCommit request .
- Transaction submission . Participant received doCommit After the request , Perform formal transaction submission , And release all transaction resources after transaction commit .
- Respond to feedback . After the transaction is committed , Send... To the coordinator ACK Respond to .
- Complete the business . The coordinator receives... From all participants ACK After responding , Complete the business .
B. Interrupt the business
Enter this stage , Suppose the coordination is really in normal working state , And any one of the participants fed back to the coordinator No Respond to , Or after waiting for a timeout , The coordinator has not yet been able to receive feedback from all participants , Then the transaction will be interrupted .
- Send interrupt request
The coordinator sends abort request - Transaction rollback
Participant received abort After the request , Will use it to record in phase two Undo Information to perform a transaction rollback operation , And release the resources occupied during the whole transaction execution after rollback . - Feedback transaction rollback result
After completing the transaction rollback meeting , Send... To the coordinator Ack news . - Interrupt the business
The coordinator receives feedback from all participants Ack After the news , Interrupt the business .
What improvements have been made in the three-stage submission
Introduce timeout mechanism
stay 2PC in , Only the coordinator has a timeout mechanism , If the participant's message is not received within a certain period of time, it will fail by default ,3PC At the same time, the timeout mechanism is introduced in both the coordinator and the participants .
Add pre submit phase
stay 2PC Between the preparation stage and the submission stage , Insert a preparation stage , send 3PC Have CanCommit、PreCommit、DoCommit Three stages ,PreCommit It's a buffer , It ensures that the states of participating nodes are consistent before the final submission stage .
Problems in the three-stage submission agreement
The three-stage submission agreement also has problems , Specific performance: , In stage three , If participants receive PreCommit After the news , There is a problem that the coordinator cannot communicate normally , under these circumstances , Participants will still commit transactions , This leads to data inconsistency .
Applications submitted in two and three phases
Two phase commit is a relatively simplified consistency algorithm / agreement , Many relational databases use two-phase commit protocol to complete distributed transaction processing , A typical example is MySQL Of XA standard .
In transaction processing 、 Databases and Computer Networks , The two-phase commit protocol provides the guarantee of data consistency in distributed design , The participants of the whole transaction either commit all the consistency successfully , Or roll back all .MySQL Cluster Internal data synchronization is used 2PC agreement .
MySQL Master-slave replication of
stay MySQL in , The binary log is server layer , It's mainly used to do Master slave copy and Instant point recovery The use of ; Transaction log (Redo Log) yes InnoDB Storage engine layer , Used to secure transactions .
While the database is running , Need assurance Binlog and Redo Log The consistency of , If the order is inconsistent , Means that the Master-Slave It may not be the same .
In the open Binlog after , How to ensure Binlog and InnoDB redo The consistency of logs ?MySQL It uses two-phase submission , The internal system will automatically treat ordinary transactions as a XA Business ( Internal distributed transactions ) To deal with it :
- Commit Will be automatically divided into Prepare and Commit Two phases ;
- Binlog Will be regarded as the coordinator of affairs (Transaction Coordinator),Binlog Event It will be used as the coordinator log .
边栏推荐
- 《自然语言处理实战入门》深度学习基础 ---- attention 注意力机制 ,Transformer 深度解析与学习材料汇总
- Modify CSV
- How to obtain the cash flow data of advertising services to help analyze the advertising effect?
- ORACLE——iSupplier 门户开票错误
- Half of the people in the country run in Changsha. Where do half of the people in Changsha run?
- Dot screen precautions
- 谷歌浏览器调试工具使用基础版(一)
- Spine_ Adnexal skin
- Web middleware log analysis script 3.0 (shell script)
- android sqlite先分组后排序左连查询
猜你喜欢
![[software development specification III] [software version naming Specification]](/img/dd/86f3323591ec688d6828d7075db59d.png)
[software development specification III] [software version naming Specification]

Linked list related interview questions

格式化JS代码,调试JS代码

Tencent employees' salary: the real 985 graduation salary, do you think I can be saved? Netizen: daily salary?

Handler消息机制-FWK层

【Code】剑指offer 03数组中重复的数字

Arthas watch command to view the properties of objects in the array

PyCharm在创建py文件时自动添加头部注释
![[Go]三、最简单的RestFul API服务器](/img/1f/f6fc8cc9a3891d01a25e709170188d.png)
[Go]三、最简单的RestFul API服务器

【纪中】2022.7.16 1432.输油管道
随机推荐
Tencent employees' salary: the real 985 graduation salary, do you think I can be saved? Netizen: daily salary?
[ickim 2022] the Fourth International Conference on knowledge and information management
电视机软件烧录
Should we test the Dao layer?
链表相关面试题
格式化JS代码,调试JS代码
Linear relationship between vectors
【ICKIM 2022】第四届知识与信息管理国际会议
The application and principle of changing IP software are very wide. Four methods of dynamic IP replacement are used to protect network privacy
MDK编译过程及ARM编译工具链
Failed to load DLL
MulDA: A Multilingual Data Augmentation Framework for Low-Resource Cross-Lingual NER 阅读笔记
API测试简介
Leetcode537. 复数乘法(可以,已解决)
机器学习:贝叶斯网络
Docker高级篇-Mysql主从复制
Tutorial on principles and applications of database system (055) -- MySQL query (XVII): usage of mathematical functions
Fundamentals of MATLAB shift operation
如何获取广告服务流量变现数据,助力广告效果分析?
android sqlite先分组后排序左连查询