In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to understand MySQL 2PC and 3PC". In daily operation, I believe many people have doubts about how to understand MySQL 2PC and 3PC. Xiaobian consulted all kinds of information and sorted out simple and easy operation methods. I hope to help you answer the doubts of "how to understand MySQL 2PC and 3PC"! Next, please follow the small series to learn together!
Distributed Systems and Distributed Consistency Problems
Distributed systems are software and hardware systems that run on multiple different networked computers and communicate and coordinate only through message passing.
Distributed consensus problem is the problem of how independent nodes agree on a resolution.
PC(Two-Phase Commit)
Two-phase commit is the division of a transaction commit into two parts: a preparation phase and a commit phase. The initiator of the transaction is called the coordinator, and the executor of the transaction is called the participant.
Phase 1: Preparation phase
The coordinator initiates and passes a request with transaction information to each participant, asking if the transaction can be committed, and waits for the result to be returned.
Participants perform transaction operations, placing Undo and Redo in the transaction log (but not committing)
Returns YES if participant succeeds (transaction can be committed), fails NO(transaction cannot be committed)
Phase II: Submission phase
There are two scenarios for this phase: all participants return YES, and any participant returns NO.
When all participants respond YES, the transaction is committed.
When any one of the participants responds NO, the transaction is interrupted.
Submit transaction: (all participants responded YES)
The coordinator issues a formal commit request to all participants (i.e., a Commit request).
The participant executes the Commit request and releases the resources occupied during the entire transaction.
3. Each participant feeds back an Ack completion message to the coordinator.
4. After the coordinator receives the Ack message from all participants, the transaction submission is completed.
Interrupted transaction: (any participant feedback NO)
The coordinator issues a rollback request (i.e., a Rollback request) to all participants.
Participants use the Undo information from Phase 1 to perform a rollback operation and release resources consumed during the entire transaction.
3. Each participant feeds back an Ack completion message to the coordinator.
4. After the coordinator receives the Ack message from all participants, the transaction interrupt is completed.
2PC defects
Synchronous blocking: The biggest problem is synchronous blocking, that is, all logic involved in the transaction is in a blocking state.
Single point: The coordinator has a single point problem, and if the coordinator fails, the participant will always be locked.
Split brain: In phase 2, if only some participants receive and execute the Commit request, it will lead to inconsistent node data.
Because 2PC has the problems of synchronous block, single point and brain split, an improved scheme of 2PC, i.e. 3PC, appears.
PC (Three-Phase Commit Protocol)
3PC, a three-phase commit protocol, is an improved version of 2PC, that is, the transaction commit process is divided into three phases: CanCommit, PreCommit, and do Commit.
Stage 1: CanCommit
The coordinator sends a CanCommit request containing the transaction content to all participants, asking if the transaction can be committed, and waits for all participants to reply.
After receiving the CanCommit request, if the participant thinks that the transaction operation can be executed, the feedback is YES and enters the preparatory state, otherwise the feedback is NO.
Stage 2: Precommit
This phase is divided into two situations:
All participants are requested and return YES.
2. If any participant returns NO, or if any participant times out and the coordinator fails to receive feedback, the transaction is aborted.
Transaction pre-commit:(when all participants respond YES)
The coordinator sends a PreCommit request to all participants to enter the preparation phase.
After the participant receives the PreCommit request, it performs the transaction operation and records the Undo and Redo information in the transaction log (but does not commit the transaction).
3. Each participant feeds back an Ack response or a No response to the coordinator and waits for a final instruction.
Interrupted transaction: (No feedback from any participant, or wait timeout before coordinator receives feedback from all participants)
The coordinator sends an abort request to all participants.
Participants interrupt transactions regardless of whether they receive an abort request from the coordinator or a timeout occurs while waiting for the coordinator request.
Stage 3: Do Commit
There are also two situations at this stage:
1. All participants feedback Ack response, i.e. perform real transaction commit.
2, any one participant feedback NO, or wait for a timeout coordinator has not received feedback from all participants, that is, interrupt the transaction.
Submit transaction: (when all participants feedback Ack response)
If the coordinator is working, send a do Commit request to all participants.
After the participant receives the do Commit request, it will formally execute the transaction commit and release the resources occupied during the entire transaction.
3. Each participant feeds back an Ack completion message to the coordinator.
4. After the coordinator receives the Ack message from all participants, the transaction submission is completed.
Interrupted transaction: (No feedback from any participant, or wait timeout before coordinator receives feedback from all participants)
If the coordinator is working, send an abort request to all participants.
Participants use the Undo information from Phase 1 to perform a rollback operation and release resources consumed during the entire transaction.
3. Each participant feeds back an Ack completion message to the coordinator.
4. After the coordinator receives the Ack message from all participants, the transaction interrupt is completed.
Note: After entering Phase 3, either problems with the coordinator or problems with the coordinator and participant network will cause the participant to fail to receive the do Commit request or abort request from the coordinator. At this point, participants will wait for a timeout before proceeding with the transaction commit.
3PC advantages and disadvantages
Pros: Reduced blocking range, coordinator or participant aborts transaction after wait timeout. The coordinator single-point problem is avoided, and participants continue to commit transactions when the coordinator has a problem in phase 3.
Defect: The split brain problem still exists, i.e., waiting for the final instruction after the participant receives the PreCommit request. If the coordinator cannot communicate with the participant properly at this time, it will cause the participant to continue submitting the transaction, resulting in data inconsistency.
At this point, the study of "how to understand MySQL 2PC and 3PC" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.