当前位置:网站首页>2pc and 3pc of consistency agreement
2pc and 3pc of consistency agreement
2022-07-18 06:30:00 【Cheng Ming, Cheng Ming, you're going to be famous】
In the distributed system, every machine node can know whether it succeeds or fails in the process of transaction operation , But we can't know the operation results of other nodes . So when a transaction needs to span multiple nodes , In order to guarantee the ACID Characteristics of , At this time, we need to introduce a " The coordinator " To uniformly schedule the logic of all nodes , These scheduled nodes are called " participants ". The coordinator is responsible for scheduling the execution logic of the participants , And finally decide whether these participants should really commit the transaction .
2PC
2PC, yes Two-Phase-Commit Abbreviation , That is, two-stage submission , It's computer networks, especially in the field of databases , A consistency algorithm designed to ensure atomicity and consistency in the transaction processing of all nodes under the distributed system architecture . Generally, two-phase submission is also considered as a consistency agreement , To ensure the consistency of distributed system data .
Stage 1 : Submit transaction request
- Business inquiry
The coordinator sends the transaction content to all participants , Ask if the transaction commit operation can be performed , And began to wait for the corresponding response from the participants .
- Perform transactions
Each participant node performs transaction operations , And will Undo Log and Redo Log Information is logged in the transaction log .
- Participants give feedback to the coordinator about the corresponding
If the participant successfully performs the transaction operation , Then give feedback to the coordinator YES, Indicates that the transaction can execute ; If the participant does not successfully execute the transaction , Then give it back to the coordinator NO, Indicates that the transaction cannot be executed .
To sum up, it is the voting and declaration process that the coordinator organizes each participant to conduct a transaction operation , Therefore, the first stage of two-stage submission is also called " Voting stage ", That is, each participant votes to indicate whether to continue the next transaction submission .
Stage two : Execute transaction commit
In phase 2, the coordinator will decide whether to commit the transaction according to the feedback of each participant , The normal situation should include two situations .
- Execute transaction commit
Suppose the coordinator gets feedback from all submitters YES, Then a transaction commit will be executed .
- Send submit request : The coordinator sends... To all participant nodes commit request
- Transaction submission : The participants received commit After the request , The transaction submission operation will be formally executed , And release the transaction resources occupied during the whole transaction execution after the commit
- Feedback 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 news , Complete the business
- Interrupt the business
Suppose any participant gives feedback to the coordinator NO, Or after the wait timeout , The coordinator has not received feedback from all participants , Then the transaction will be interrupted .
- Send rollback request : The coordinator sends a rollback request to all participants
- Transaction rollback : After the participant receives the rollback request , Make use of Undo Log To perform rollback , So as to ensure atomicity , And release the resources occupied during the whole transaction execution after rollback
- Feedback results : After all participants are rolled back , Send... To the coordinator ack news
- Interrupt the business : The coordinator receives all participants ack After the news , Complete transaction interrupt
2PC Advantages and disadvantages
2PC The advantages are obvious : The principle of simple 、 To achieve convenient .
Shortcomings are also easier to think of : Synchronous blocking 、 A single point of the problem 、 Data inconsistency .
- Synchronous blocking
2PC An obvious problem is synchronization blocking , This will greatly limit the performance of distributed systems . In the two-phase commit process , All participants' transaction processing logic is blocked , That is to say, each participant cannot perform any other operation while waiting for the response of other participants .
- A single point of the problem
stay 2PC The role of the coordinator is crucial , Once the coordinator has problems, the whole process will not work , As a result, participants cannot complete transaction operations .
- Data inconsistency
When executing transaction submission in phase 2 , When the coordinator sends commit After the request , If there is a network failure, some participants will not receive commit request , Received at this time commit The requested participant will commit the transaction , Have not received commit The requested participant will not commit the transaction , Thus, the phenomenon of inconsistent data appears .
3PC
In the above content, we know 2PC Principle and implementation mechanism of , It is clearly pointed out that there may be synchronous blocking during operation 、 Single point problems and data inconsistency , So in 2PC On the basis of , Put forward 3PC, That is, the three-stage submission agreement .
Agreement that
3PC, yes Three-Phase Commit Abbreviation , Three stage submission , yes 2PC Improved version , It will submit the agreement in two stages " Submit transaction request " The process is split in two , Formed by CanCommit、PreCommit and do Commit Three phase transaction protocol .
Stage 1 :CanCommit
- Business inquiry
The coordinator sends a... With transaction content to all participants canCommit request , Ask if the transaction commit operation can be performed , And begin to wait for the corresponding .
- Each participant feeds back the response of the transaction inquiry to the coordinator
Participants are receiving canCommit After the request , Under normal circumstances , If it thinks it can execute the transaction smoothly , Then feedback YES, Get ready , Otherwise feedback NO.
Stage two :PreCommit
In stage two , The coordinator will decide whether to proceed with the transaction according to the feedback status of each participant PreCommit operation , Under normal circumstances, there are two possibilities .
- Perform transaction pre commit
Suppose the coordinator gets feedback from all participants that the status is YES, Then the pre commit operation will be performed
- Send pre submit request
The coordinator sent... To all participants preCommit request , And enter Prepared Stage
- Transaction pre commit
Participants receive preCommit After the request , Will perform transaction operations , And will Undo Log and Redo Log Information is logged in the transaction log .
- Each participant feeds back the implementation results to the coordinator
If the participant successfully performs the transaction operation , Then it will be fed back to the coordinator ack Respond to , While waiting for the final implementation :commit or abort
- Interrupt the business
Suppose any participant returns to the coordinator NO Response or wait timeout , Then the transaction will be interrupted
- Send interrupt request
- Interrupt the business
Stage three :doCommit
In this phase, the real transaction commit , There are two possibilities
- Execute commit
- Send submit request
Enter this stage , Suppose the coordinator is in normal working condition , And it receives... From all participants ack Respond to , So it will be from " Pre submission " The state changes to " Submit " state , And send it to all participants doCommit request
- Transaction submission
Participant received doCommit After the request , It will actually execute the transaction submission operation , And release the resources occupied during the whole transaction execution after the completion of 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 participants ack After message , Complete the business
- Interrupt the business
- Send interrupt request
The coordinator sends... To all participants abort request
- Transaction rollback
Participant received abort After the request , Will use in phase II undo log To perform transaction rollback , And release the resources occupied during transaction execution
- 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 , Interrupt the business
3PC Advantages and disadvantages
advantage : Compared with the two-phase submission, the submission agreement , The biggest advantage of three-stage italics is that it reduces the blocking range of participants , And can continue to reach an agreement after a single point of failure .
shortcoming : Three phase commit also introduces new problems in removing blocking colleagues , That is when participants receive preCommit After the news , If there's a network partition , At this time, the coordinator cannot communicate with the participants on the node , In this case, the change participant will still commit the transaction , This will inevitably lead to data inconsistency .
边栏推荐
- Network socket programming
- 2022年智能运维企业50强,博睿数据实力入选
- 【LeetCode】26、删除有序数组中的重复项
- Redis (II) three special types of redis
- R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用exp函数、confint函数、coef函数获取模型中每个变量(自变量改变一个单位)对应的优势比
- 997. Find the judge of the town
- Iterators and generators
- New usage and deconstruction assignment of data types
- forEach、for in、for of三者区别
- 探秘ZGC
猜你喜欢

ECCV 2022 | 多域长尾分布学习,不平衡域泛化问题研究(开源)

MQ Series 2: technology selection of Message Oriented Middleware

Face the object

“沉浸式”住宿体验——酒店的新瓶,民宿的老酒

Diwen serial port screen tutorial (3)

裁员吵架散摊子, 马斯克:我太难了;狠心开源了一个舆情获取项目;取特征工程跟调参一样简单了?!NeRF大佬直呼卷不动了;前沿论文 | ShowMeAI资讯日报

Matplotlib绘图报错:“! LaTeX Error: File `type1cm.sty‘ not found.“ 解决办法

软件研发效能需求价值流分析专题

Special topic of software R & D efficiency demand value stream analysis

图扑 Web 可视化引擎在仿真分析领域的应用
随机推荐
图扑 Web 可视化引擎在仿真分析领域的应用
Special topic of software R & D efficiency demand value stream analysis
Symbol data type
flink. 14. How is the underlying layer of datastream module source implemented?
企业在创建产品帮助中心时需要注意的问题!
动画以及封装(offset、client、scroll系列)
如何将notepad++设置为默认打开方式
R language dplyr package summary_ The all function calculates the mean and median of all numerical data columns in the dataframe data, and filters the numerical data columns with happy (summarize all
“沉浸式”住宿体验——酒店的新瓶,民宿的老酒
SSM整合出现问题- org.apache.ibatis.transaction.TransactionFactory
【golang】基于go语言的堆结构模板
杰理之入耳检测功能【篇】
Functions and symbols
Jerry's n turns on the 1-2 function, which will affect the Bluetooth distance [article]
R language uses LM function to build linear regression model, uses I operator to embed expression, and uses expression to specify the form of regression equation
@EqualsAndHashCode注解的使用
【mysql学习笔记33】日志
函数与箭头函数
医疗单据OCR识别+知识库校验,赋能保险智能理赔
那些年我们用过的分布式锁,你都get到了吗